blob: 8300f8186ef4ba768a2475ba79124b35ff9226f4 [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 Markocac5a7e2016-02-22 10:39:50 +00002398 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002399 pc_relative_method_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)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00003556 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3557 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3558 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003559 return;
3560 }
3561
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003562 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00003563
3564 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3565 if (invoke->HasPcRelativeDexCache()) {
3566 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3567 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003568}
3569
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003570static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3571 if (invoke->GetLocations()->Intrinsified()) {
3572 IntrinsicCodeGeneratorARM intrinsic(codegen);
3573 intrinsic.Dispatch(invoke);
3574 return true;
3575 }
3576 return false;
3577}
3578
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003579void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003580 // Explicit clinit checks triggered by static invokes must have been pruned by
3581 // art::PrepareForRegisterAllocation.
3582 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003583
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003584 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3585 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003586 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003587
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003588 LocationSummary* locations = invoke->GetLocations();
3589 codegen_->GenerateStaticOrDirectCall(
3590 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003591}
3592
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003593void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003594 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003595 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003596}
3597
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003598void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003599 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003600 if (intrinsic.TryDispatch(invoke)) {
3601 return;
3602 }
3603
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003604 HandleInvoke(invoke);
3605}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003606
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003607void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003608 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3609 return;
3610 }
3611
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003612 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003613 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003614}
3615
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003616void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3617 HandleInvoke(invoke);
3618 // Add the hidden argument.
3619 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3620}
3621
3622void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3623 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003624 LocationSummary* locations = invoke->GetLocations();
3625 Register temp = locations->GetTemp(0).AsRegister<Register>();
3626 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003627 Location receiver = locations->InAt(0);
3628 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3629
Roland Levillain3b359c72015-11-17 19:35:12 +00003630 // Set the hidden argument. This is safe to do this here, as R12
3631 // won't be modified thereafter, before the `blx` (call) instruction.
3632 DCHECK_EQ(R12, hidden_reg);
3633 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003634
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003635 if (receiver.IsStackSlot()) {
3636 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003637 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003638 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3639 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003640 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003641 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003642 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003643 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003644 // Instead of simply (possibly) unpoisoning `temp` here, we should
3645 // emit a read barrier for the previous class reference load.
3646 // However this is not required in practice, as this is an
3647 // intermediate/temporary reference and because the current
3648 // concurrent copying collector keeps the from-space memory
3649 // intact/accessible until the end of the marking phase (the
3650 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003651 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003652 __ LoadFromOffset(kLoadWord, temp, temp,
3653 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3654 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003655 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003656 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003657 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003658 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003659 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003660 // LR = temp->GetEntryPoint();
3661 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3662 // LR();
3663 __ blx(LR);
3664 DCHECK(!codegen_->IsLeafMethod());
3665 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3666}
3667
Orion Hodsonac141392017-01-13 11:53:47 +00003668void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3669 HandleInvoke(invoke);
3670}
3671
3672void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3673 codegen_->GenerateInvokePolymorphicCall(invoke);
3674}
3675
Roland Levillain88cb1752014-10-20 16:36:47 +01003676void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3677 LocationSummary* locations =
3678 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3679 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003680 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003681 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003682 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3683 break;
3684 }
3685 case Primitive::kPrimLong: {
3686 locations->SetInAt(0, Location::RequiresRegister());
3687 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003688 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003689 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003690
Roland Levillain88cb1752014-10-20 16:36:47 +01003691 case Primitive::kPrimFloat:
3692 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003693 locations->SetInAt(0, Location::RequiresFpuRegister());
3694 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003695 break;
3696
3697 default:
3698 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3699 }
3700}
3701
3702void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3703 LocationSummary* locations = neg->GetLocations();
3704 Location out = locations->Out();
3705 Location in = locations->InAt(0);
3706 switch (neg->GetResultType()) {
3707 case Primitive::kPrimInt:
3708 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003709 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003710 break;
3711
3712 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003713 DCHECK(in.IsRegisterPair());
3714 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3715 __ rsbs(out.AsRegisterPairLow<Register>(),
3716 in.AsRegisterPairLow<Register>(),
3717 ShifterOperand(0));
3718 // We cannot emit an RSC (Reverse Subtract with Carry)
3719 // instruction here, as it does not exist in the Thumb-2
3720 // instruction set. We use the following approach
3721 // using SBC and SUB instead.
3722 //
3723 // out.hi = -C
3724 __ sbc(out.AsRegisterPairHigh<Register>(),
3725 out.AsRegisterPairHigh<Register>(),
3726 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3727 // out.hi = out.hi - in.hi
3728 __ sub(out.AsRegisterPairHigh<Register>(),
3729 out.AsRegisterPairHigh<Register>(),
3730 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3731 break;
3732
Roland Levillain88cb1752014-10-20 16:36:47 +01003733 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003734 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003735 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003736 break;
3737
Roland Levillain88cb1752014-10-20 16:36:47 +01003738 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003739 DCHECK(in.IsFpuRegisterPair());
3740 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3741 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003742 break;
3743
3744 default:
3745 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3746 }
3747}
3748
Roland Levillaindff1f282014-11-05 14:15:05 +00003749void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003750 Primitive::Type result_type = conversion->GetResultType();
3751 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003752 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003753
Roland Levillain5b3ee562015-04-14 16:02:41 +01003754 // The float-to-long, double-to-long and long-to-float type conversions
3755 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003756 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003757 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3758 && result_type == Primitive::kPrimLong)
3759 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003760 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003761 : LocationSummary::kNoCall;
3762 LocationSummary* locations =
3763 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3764
David Brazdilb2bd1c52015-03-25 11:17:37 +00003765 // The Java language does not allow treating boolean as an integral type but
3766 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003767
Roland Levillaindff1f282014-11-05 14:15:05 +00003768 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003769 case Primitive::kPrimByte:
3770 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003771 case Primitive::kPrimLong:
3772 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003773 case Primitive::kPrimBoolean:
3774 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003775 case Primitive::kPrimShort:
3776 case Primitive::kPrimInt:
3777 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003778 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003779 locations->SetInAt(0, Location::RequiresRegister());
3780 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3781 break;
3782
3783 default:
3784 LOG(FATAL) << "Unexpected type conversion from " << input_type
3785 << " to " << result_type;
3786 }
3787 break;
3788
Roland Levillain01a8d712014-11-14 16:27:39 +00003789 case Primitive::kPrimShort:
3790 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003791 case Primitive::kPrimLong:
3792 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003793 case Primitive::kPrimBoolean:
3794 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003795 case Primitive::kPrimByte:
3796 case Primitive::kPrimInt:
3797 case Primitive::kPrimChar:
3798 // Processing a Dex `int-to-short' instruction.
3799 locations->SetInAt(0, Location::RequiresRegister());
3800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3801 break;
3802
3803 default:
3804 LOG(FATAL) << "Unexpected type conversion from " << input_type
3805 << " to " << result_type;
3806 }
3807 break;
3808
Roland Levillain946e1432014-11-11 17:35:19 +00003809 case Primitive::kPrimInt:
3810 switch (input_type) {
3811 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003812 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003813 locations->SetInAt(0, Location::Any());
3814 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3815 break;
3816
3817 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003818 // Processing a Dex `float-to-int' instruction.
3819 locations->SetInAt(0, Location::RequiresFpuRegister());
3820 locations->SetOut(Location::RequiresRegister());
3821 locations->AddTemp(Location::RequiresFpuRegister());
3822 break;
3823
Roland Levillain946e1432014-11-11 17:35:19 +00003824 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003825 // Processing a Dex `double-to-int' instruction.
3826 locations->SetInAt(0, Location::RequiresFpuRegister());
3827 locations->SetOut(Location::RequiresRegister());
3828 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003829 break;
3830
3831 default:
3832 LOG(FATAL) << "Unexpected type conversion from " << input_type
3833 << " to " << result_type;
3834 }
3835 break;
3836
Roland Levillaindff1f282014-11-05 14:15:05 +00003837 case Primitive::kPrimLong:
3838 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003839 case Primitive::kPrimBoolean:
3840 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003841 case Primitive::kPrimByte:
3842 case Primitive::kPrimShort:
3843 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003844 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003845 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003846 locations->SetInAt(0, Location::RequiresRegister());
3847 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3848 break;
3849
Roland Levillain624279f2014-12-04 11:54:28 +00003850 case Primitive::kPrimFloat: {
3851 // Processing a Dex `float-to-long' instruction.
3852 InvokeRuntimeCallingConvention calling_convention;
3853 locations->SetInAt(0, Location::FpuRegisterLocation(
3854 calling_convention.GetFpuRegisterAt(0)));
3855 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3856 break;
3857 }
3858
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003859 case Primitive::kPrimDouble: {
3860 // Processing a Dex `double-to-long' instruction.
3861 InvokeRuntimeCallingConvention calling_convention;
3862 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3863 calling_convention.GetFpuRegisterAt(0),
3864 calling_convention.GetFpuRegisterAt(1)));
3865 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003866 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003867 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003868
3869 default:
3870 LOG(FATAL) << "Unexpected type conversion from " << input_type
3871 << " to " << result_type;
3872 }
3873 break;
3874
Roland Levillain981e4542014-11-14 11:47:14 +00003875 case Primitive::kPrimChar:
3876 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003877 case Primitive::kPrimLong:
3878 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003879 case Primitive::kPrimBoolean:
3880 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003881 case Primitive::kPrimByte:
3882 case Primitive::kPrimShort:
3883 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003884 // Processing a Dex `int-to-char' instruction.
3885 locations->SetInAt(0, Location::RequiresRegister());
3886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3887 break;
3888
3889 default:
3890 LOG(FATAL) << "Unexpected type conversion from " << input_type
3891 << " to " << result_type;
3892 }
3893 break;
3894
Roland Levillaindff1f282014-11-05 14:15:05 +00003895 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003896 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003897 case Primitive::kPrimBoolean:
3898 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003899 case Primitive::kPrimByte:
3900 case Primitive::kPrimShort:
3901 case Primitive::kPrimInt:
3902 case Primitive::kPrimChar:
3903 // Processing a Dex `int-to-float' instruction.
3904 locations->SetInAt(0, Location::RequiresRegister());
3905 locations->SetOut(Location::RequiresFpuRegister());
3906 break;
3907
Roland Levillain5b3ee562015-04-14 16:02:41 +01003908 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003909 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003910 InvokeRuntimeCallingConvention calling_convention;
3911 locations->SetInAt(0, Location::RegisterPairLocation(
3912 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3913 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003914 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003915 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003916
Roland Levillaincff13742014-11-17 14:32:17 +00003917 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003918 // Processing a Dex `double-to-float' instruction.
3919 locations->SetInAt(0, Location::RequiresFpuRegister());
3920 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003921 break;
3922
3923 default:
3924 LOG(FATAL) << "Unexpected type conversion from " << input_type
3925 << " to " << result_type;
3926 };
3927 break;
3928
Roland Levillaindff1f282014-11-05 14:15:05 +00003929 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003930 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003931 case Primitive::kPrimBoolean:
3932 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003933 case Primitive::kPrimByte:
3934 case Primitive::kPrimShort:
3935 case Primitive::kPrimInt:
3936 case Primitive::kPrimChar:
3937 // Processing a Dex `int-to-double' instruction.
3938 locations->SetInAt(0, Location::RequiresRegister());
3939 locations->SetOut(Location::RequiresFpuRegister());
3940 break;
3941
3942 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003943 // Processing a Dex `long-to-double' instruction.
3944 locations->SetInAt(0, Location::RequiresRegister());
3945 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003946 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003947 locations->AddTemp(Location::RequiresFpuRegister());
3948 break;
3949
Roland Levillaincff13742014-11-17 14:32:17 +00003950 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003951 // Processing a Dex `float-to-double' instruction.
3952 locations->SetInAt(0, Location::RequiresFpuRegister());
3953 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003954 break;
3955
3956 default:
3957 LOG(FATAL) << "Unexpected type conversion from " << input_type
3958 << " to " << result_type;
3959 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003960 break;
3961
3962 default:
3963 LOG(FATAL) << "Unexpected type conversion from " << input_type
3964 << " to " << result_type;
3965 }
3966}
3967
3968void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3969 LocationSummary* locations = conversion->GetLocations();
3970 Location out = locations->Out();
3971 Location in = locations->InAt(0);
3972 Primitive::Type result_type = conversion->GetResultType();
3973 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003974 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003975 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003976 case Primitive::kPrimByte:
3977 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003978 case Primitive::kPrimLong:
3979 // Type conversion from long to byte is a result of code transformations.
3980 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3981 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003982 case Primitive::kPrimBoolean:
3983 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003984 case Primitive::kPrimShort:
3985 case Primitive::kPrimInt:
3986 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003987 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003988 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003989 break;
3990
3991 default:
3992 LOG(FATAL) << "Unexpected type conversion from " << input_type
3993 << " to " << result_type;
3994 }
3995 break;
3996
Roland Levillain01a8d712014-11-14 16:27:39 +00003997 case Primitive::kPrimShort:
3998 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003999 case Primitive::kPrimLong:
4000 // Type conversion from long to short is a result of code transformations.
4001 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
4002 break;
David Brazdil46e2a392015-03-16 17:31:52 +00004003 case Primitive::kPrimBoolean:
4004 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00004005 case Primitive::kPrimByte:
4006 case Primitive::kPrimInt:
4007 case Primitive::kPrimChar:
4008 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004009 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00004010 break;
4011
4012 default:
4013 LOG(FATAL) << "Unexpected type conversion from " << input_type
4014 << " to " << result_type;
4015 }
4016 break;
4017
Roland Levillain946e1432014-11-11 17:35:19 +00004018 case Primitive::kPrimInt:
4019 switch (input_type) {
4020 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00004021 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00004022 DCHECK(out.IsRegister());
4023 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004024 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00004025 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004026 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00004027 } else {
4028 DCHECK(in.IsConstant());
4029 DCHECK(in.GetConstant()->IsLongConstant());
4030 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004031 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00004032 }
4033 break;
4034
Roland Levillain3f8f9362014-12-02 17:45:01 +00004035 case Primitive::kPrimFloat: {
4036 // Processing a Dex `float-to-int' instruction.
4037 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01004038 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00004039 __ vmovrs(out.AsRegister<Register>(), temp);
4040 break;
4041 }
4042
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004043 case Primitive::kPrimDouble: {
4044 // Processing a Dex `double-to-int' instruction.
4045 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01004046 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004047 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00004048 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004049 }
Roland Levillain946e1432014-11-11 17:35:19 +00004050
4051 default:
4052 LOG(FATAL) << "Unexpected type conversion from " << input_type
4053 << " to " << result_type;
4054 }
4055 break;
4056
Roland Levillaindff1f282014-11-05 14:15:05 +00004057 case Primitive::kPrimLong:
4058 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004059 case Primitive::kPrimBoolean:
4060 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00004061 case Primitive::kPrimByte:
4062 case Primitive::kPrimShort:
4063 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00004064 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00004065 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00004066 DCHECK(out.IsRegisterPair());
4067 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004068 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00004069 // Sign extension.
4070 __ Asr(out.AsRegisterPairHigh<Register>(),
4071 out.AsRegisterPairLow<Register>(),
4072 31);
4073 break;
4074
4075 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00004076 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004077 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004078 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00004079 break;
4080
Roland Levillaindff1f282014-11-05 14:15:05 +00004081 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004082 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004083 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004084 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00004085 break;
4086
4087 default:
4088 LOG(FATAL) << "Unexpected type conversion from " << input_type
4089 << " to " << result_type;
4090 }
4091 break;
4092
Roland Levillain981e4542014-11-14 11:47:14 +00004093 case Primitive::kPrimChar:
4094 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00004095 case Primitive::kPrimLong:
4096 // Type conversion from long to char is a result of code transformations.
4097 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
4098 break;
David Brazdil46e2a392015-03-16 17:31:52 +00004099 case Primitive::kPrimBoolean:
4100 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00004101 case Primitive::kPrimByte:
4102 case Primitive::kPrimShort:
4103 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00004104 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004105 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00004106 break;
4107
4108 default:
4109 LOG(FATAL) << "Unexpected type conversion from " << input_type
4110 << " to " << result_type;
4111 }
4112 break;
4113
Roland Levillaindff1f282014-11-05 14:15:05 +00004114 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00004115 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004116 case Primitive::kPrimBoolean:
4117 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00004118 case Primitive::kPrimByte:
4119 case Primitive::kPrimShort:
4120 case Primitive::kPrimInt:
4121 case Primitive::kPrimChar: {
4122 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004123 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
4124 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00004125 break;
4126 }
4127
Roland Levillain5b3ee562015-04-14 16:02:41 +01004128 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00004129 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004130 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004131 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00004132 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00004133
Roland Levillaincff13742014-11-17 14:32:17 +00004134 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00004135 // Processing a Dex `double-to-float' instruction.
4136 __ vcvtsd(out.AsFpuRegister<SRegister>(),
4137 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00004138 break;
4139
4140 default:
4141 LOG(FATAL) << "Unexpected type conversion from " << input_type
4142 << " to " << result_type;
4143 };
4144 break;
4145
Roland Levillaindff1f282014-11-05 14:15:05 +00004146 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00004147 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004148 case Primitive::kPrimBoolean:
4149 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00004150 case Primitive::kPrimByte:
4151 case Primitive::kPrimShort:
4152 case Primitive::kPrimInt:
4153 case Primitive::kPrimChar: {
4154 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004155 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00004156 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4157 out.AsFpuRegisterPairLow<SRegister>());
4158 break;
4159 }
4160
Roland Levillain647b9ed2014-11-27 12:06:00 +00004161 case Primitive::kPrimLong: {
4162 // Processing a Dex `long-to-double' instruction.
4163 Register low = in.AsRegisterPairLow<Register>();
4164 Register high = in.AsRegisterPairHigh<Register>();
4165 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
4166 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01004167 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00004168 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01004169 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
4170 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00004171
Roland Levillain682393c2015-04-14 15:57:52 +01004172 // temp_d = int-to-double(high)
4173 __ vmovsr(temp_s, high);
4174 __ vcvtdi(temp_d, temp_s);
4175 // constant_d = k2Pow32EncodingForDouble
4176 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
4177 // out_d = unsigned-to-double(low)
4178 __ vmovsr(out_s, low);
4179 __ vcvtdu(out_d, out_s);
4180 // out_d += temp_d * constant_d
4181 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00004182 break;
4183 }
4184
Roland Levillaincff13742014-11-17 14:32:17 +00004185 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00004186 // Processing a Dex `float-to-double' instruction.
4187 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4188 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00004189 break;
4190
4191 default:
4192 LOG(FATAL) << "Unexpected type conversion from " << input_type
4193 << " to " << result_type;
4194 };
Roland Levillaindff1f282014-11-05 14:15:05 +00004195 break;
4196
4197 default:
4198 LOG(FATAL) << "Unexpected type conversion from " << input_type
4199 << " to " << result_type;
4200 }
4201}
4202
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004203void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004204 LocationSummary* locations =
4205 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004206 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004207 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004208 locations->SetInAt(0, Location::RequiresRegister());
4209 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004210 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4211 break;
4212 }
4213
4214 case Primitive::kPrimLong: {
4215 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01004216 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004218 break;
4219 }
4220
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004221 case Primitive::kPrimFloat:
4222 case Primitive::kPrimDouble: {
4223 locations->SetInAt(0, Location::RequiresFpuRegister());
4224 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004225 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004226 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004227 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004228
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004229 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004230 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004231 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004232}
4233
4234void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
4235 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004236 Location out = locations->Out();
4237 Location first = locations->InAt(0);
4238 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004239 switch (add->GetResultType()) {
4240 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004241 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004242 __ add(out.AsRegister<Register>(),
4243 first.AsRegister<Register>(),
4244 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004245 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004246 __ AddConstant(out.AsRegister<Register>(),
4247 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004248 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004249 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004250 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004251
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004252 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01004253 if (second.IsConstant()) {
4254 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4255 GenerateAddLongConst(out, first, value);
4256 } else {
4257 DCHECK(second.IsRegisterPair());
4258 __ adds(out.AsRegisterPairLow<Register>(),
4259 first.AsRegisterPairLow<Register>(),
4260 ShifterOperand(second.AsRegisterPairLow<Register>()));
4261 __ adc(out.AsRegisterPairHigh<Register>(),
4262 first.AsRegisterPairHigh<Register>(),
4263 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4264 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004265 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004266 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004267
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004268 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00004269 __ vadds(out.AsFpuRegister<SRegister>(),
4270 first.AsFpuRegister<SRegister>(),
4271 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004272 break;
4273
4274 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004275 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4276 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4277 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004278 break;
4279
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004280 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004281 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004282 }
4283}
4284
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004285void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004286 LocationSummary* locations =
4287 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004288 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004289 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004290 locations->SetInAt(0, Location::RequiresRegister());
4291 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004292 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4293 break;
4294 }
4295
4296 case Primitive::kPrimLong: {
4297 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01004298 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004300 break;
4301 }
Calin Juravle11351682014-10-23 15:38:15 +01004302 case Primitive::kPrimFloat:
4303 case Primitive::kPrimDouble: {
4304 locations->SetInAt(0, Location::RequiresFpuRegister());
4305 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004306 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004307 break;
Calin Juravle11351682014-10-23 15:38:15 +01004308 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004309 default:
Calin Juravle11351682014-10-23 15:38:15 +01004310 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004311 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004312}
4313
4314void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
4315 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01004316 Location out = locations->Out();
4317 Location first = locations->InAt(0);
4318 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004319 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004320 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01004321 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004322 __ sub(out.AsRegister<Register>(),
4323 first.AsRegister<Register>(),
4324 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004325 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004326 __ AddConstant(out.AsRegister<Register>(),
4327 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01004328 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004329 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004330 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004331 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004332
Calin Juravle11351682014-10-23 15:38:15 +01004333 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01004334 if (second.IsConstant()) {
4335 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4336 GenerateAddLongConst(out, first, -value);
4337 } else {
4338 DCHECK(second.IsRegisterPair());
4339 __ subs(out.AsRegisterPairLow<Register>(),
4340 first.AsRegisterPairLow<Register>(),
4341 ShifterOperand(second.AsRegisterPairLow<Register>()));
4342 __ sbc(out.AsRegisterPairHigh<Register>(),
4343 first.AsRegisterPairHigh<Register>(),
4344 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4345 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004346 break;
Calin Juravle11351682014-10-23 15:38:15 +01004347 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004348
Calin Juravle11351682014-10-23 15:38:15 +01004349 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004350 __ vsubs(out.AsFpuRegister<SRegister>(),
4351 first.AsFpuRegister<SRegister>(),
4352 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004353 break;
Calin Juravle11351682014-10-23 15:38:15 +01004354 }
4355
4356 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004357 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4358 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4359 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01004360 break;
4361 }
4362
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004363
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004364 default:
Calin Juravle11351682014-10-23 15:38:15 +01004365 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004366 }
4367}
4368
Calin Juravle34bacdf2014-10-07 20:23:36 +01004369void LocationsBuilderARM::VisitMul(HMul* mul) {
4370 LocationSummary* locations =
4371 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4372 switch (mul->GetResultType()) {
4373 case Primitive::kPrimInt:
4374 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004375 locations->SetInAt(0, Location::RequiresRegister());
4376 locations->SetInAt(1, Location::RequiresRegister());
4377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004378 break;
4379 }
4380
Calin Juravleb5bfa962014-10-21 18:02:24 +01004381 case Primitive::kPrimFloat:
4382 case Primitive::kPrimDouble: {
4383 locations->SetInAt(0, Location::RequiresFpuRegister());
4384 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004385 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004386 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004387 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004388
4389 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004390 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004391 }
4392}
4393
4394void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
4395 LocationSummary* locations = mul->GetLocations();
4396 Location out = locations->Out();
4397 Location first = locations->InAt(0);
4398 Location second = locations->InAt(1);
4399 switch (mul->GetResultType()) {
4400 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00004401 __ mul(out.AsRegister<Register>(),
4402 first.AsRegister<Register>(),
4403 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004404 break;
4405 }
4406 case Primitive::kPrimLong: {
4407 Register out_hi = out.AsRegisterPairHigh<Register>();
4408 Register out_lo = out.AsRegisterPairLow<Register>();
4409 Register in1_hi = first.AsRegisterPairHigh<Register>();
4410 Register in1_lo = first.AsRegisterPairLow<Register>();
4411 Register in2_hi = second.AsRegisterPairHigh<Register>();
4412 Register in2_lo = second.AsRegisterPairLow<Register>();
4413
4414 // Extra checks to protect caused by the existence of R1_R2.
4415 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4416 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
4417 DCHECK_NE(out_hi, in1_lo);
4418 DCHECK_NE(out_hi, in2_lo);
4419
4420 // input: in1 - 64 bits, in2 - 64 bits
4421 // output: out
4422 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4423 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4424 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4425
4426 // IP <- in1.lo * in2.hi
4427 __ mul(IP, in1_lo, in2_hi);
4428 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4429 __ mla(out_hi, in1_hi, in2_lo, IP);
4430 // out.lo <- (in1.lo * in2.lo)[31:0];
4431 __ umull(out_lo, IP, in1_lo, in2_lo);
4432 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
4433 __ add(out_hi, out_hi, ShifterOperand(IP));
4434 break;
4435 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01004436
4437 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004438 __ vmuls(out.AsFpuRegister<SRegister>(),
4439 first.AsFpuRegister<SRegister>(),
4440 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004441 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004442 }
4443
4444 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004445 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4446 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4447 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01004448 break;
4449 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004450
4451 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004452 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004453 }
4454}
4455
Zheng Xuc6667102015-05-15 16:08:45 +08004456void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4457 DCHECK(instruction->IsDiv() || instruction->IsRem());
4458 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4459
4460 LocationSummary* locations = instruction->GetLocations();
4461 Location second = locations->InAt(1);
4462 DCHECK(second.IsConstant());
4463
4464 Register out = locations->Out().AsRegister<Register>();
4465 Register dividend = locations->InAt(0).AsRegister<Register>();
4466 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4467 DCHECK(imm == 1 || imm == -1);
4468
4469 if (instruction->IsRem()) {
4470 __ LoadImmediate(out, 0);
4471 } else {
4472 if (imm == 1) {
4473 __ Mov(out, dividend);
4474 } else {
4475 __ rsb(out, dividend, ShifterOperand(0));
4476 }
4477 }
4478}
4479
4480void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4481 DCHECK(instruction->IsDiv() || instruction->IsRem());
4482 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4483
4484 LocationSummary* locations = instruction->GetLocations();
4485 Location second = locations->InAt(1);
4486 DCHECK(second.IsConstant());
4487
4488 Register out = locations->Out().AsRegister<Register>();
4489 Register dividend = locations->InAt(0).AsRegister<Register>();
4490 Register temp = locations->GetTemp(0).AsRegister<Register>();
4491 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004492 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004493 int ctz_imm = CTZ(abs_imm);
4494
4495 if (ctz_imm == 1) {
4496 __ Lsr(temp, dividend, 32 - ctz_imm);
4497 } else {
4498 __ Asr(temp, dividend, 31);
4499 __ Lsr(temp, temp, 32 - ctz_imm);
4500 }
4501 __ add(out, temp, ShifterOperand(dividend));
4502
4503 if (instruction->IsDiv()) {
4504 __ Asr(out, out, ctz_imm);
4505 if (imm < 0) {
4506 __ rsb(out, out, ShifterOperand(0));
4507 }
4508 } else {
4509 __ ubfx(out, out, 0, ctz_imm);
4510 __ sub(out, out, ShifterOperand(temp));
4511 }
4512}
4513
4514void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4515 DCHECK(instruction->IsDiv() || instruction->IsRem());
4516 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4517
4518 LocationSummary* locations = instruction->GetLocations();
4519 Location second = locations->InAt(1);
4520 DCHECK(second.IsConstant());
4521
4522 Register out = locations->Out().AsRegister<Register>();
4523 Register dividend = locations->InAt(0).AsRegister<Register>();
4524 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4525 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4526 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4527
4528 int64_t magic;
4529 int shift;
4530 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4531
4532 __ LoadImmediate(temp1, magic);
4533 __ smull(temp2, temp1, dividend, temp1);
4534
4535 if (imm > 0 && magic < 0) {
4536 __ add(temp1, temp1, ShifterOperand(dividend));
4537 } else if (imm < 0 && magic > 0) {
4538 __ sub(temp1, temp1, ShifterOperand(dividend));
4539 }
4540
4541 if (shift != 0) {
4542 __ Asr(temp1, temp1, shift);
4543 }
4544
4545 if (instruction->IsDiv()) {
4546 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4547 } else {
4548 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4549 // TODO: Strength reduction for mls.
4550 __ LoadImmediate(temp2, imm);
4551 __ mls(out, temp1, temp2, dividend);
4552 }
4553}
4554
4555void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4556 DCHECK(instruction->IsDiv() || instruction->IsRem());
4557 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4558
4559 LocationSummary* locations = instruction->GetLocations();
4560 Location second = locations->InAt(1);
4561 DCHECK(second.IsConstant());
4562
4563 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4564 if (imm == 0) {
4565 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4566 } else if (imm == 1 || imm == -1) {
4567 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004568 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004569 DivRemByPowerOfTwo(instruction);
4570 } else {
4571 DCHECK(imm <= -2 || imm >= 2);
4572 GenerateDivRemWithAnyConstant(instruction);
4573 }
4574}
4575
Calin Juravle7c4954d2014-10-28 16:57:40 +00004576void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004577 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4578 if (div->GetResultType() == Primitive::kPrimLong) {
4579 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004580 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004581 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4582 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004583 } else if (div->GetResultType() == Primitive::kPrimInt &&
4584 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4585 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004586 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004587 }
4588
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4590
Calin Juravle7c4954d2014-10-28 16:57:40 +00004591 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004592 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004593 if (div->InputAt(1)->IsConstant()) {
4594 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004595 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004597 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4598 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004599 // No temp register required.
4600 } else {
4601 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004602 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004603 locations->AddTemp(Location::RequiresRegister());
4604 }
4605 }
4606 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004607 locations->SetInAt(0, Location::RequiresRegister());
4608 locations->SetInAt(1, Location::RequiresRegister());
4609 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4610 } else {
4611 InvokeRuntimeCallingConvention calling_convention;
4612 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4613 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004614 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004615 // we only need the former.
4616 locations->SetOut(Location::RegisterLocation(R0));
4617 }
Calin Juravled0d48522014-11-04 16:40:20 +00004618 break;
4619 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004620 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004621 InvokeRuntimeCallingConvention calling_convention;
4622 locations->SetInAt(0, Location::RegisterPairLocation(
4623 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4624 locations->SetInAt(1, Location::RegisterPairLocation(
4625 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004626 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004627 break;
4628 }
4629 case Primitive::kPrimFloat:
4630 case Primitive::kPrimDouble: {
4631 locations->SetInAt(0, Location::RequiresFpuRegister());
4632 locations->SetInAt(1, Location::RequiresFpuRegister());
4633 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4634 break;
4635 }
4636
4637 default:
4638 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4639 }
4640}
4641
4642void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4643 LocationSummary* locations = div->GetLocations();
4644 Location out = locations->Out();
4645 Location first = locations->InAt(0);
4646 Location second = locations->InAt(1);
4647
4648 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004649 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004650 if (second.IsConstant()) {
4651 GenerateDivRemConstantIntegral(div);
4652 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004653 __ sdiv(out.AsRegister<Register>(),
4654 first.AsRegister<Register>(),
4655 second.AsRegister<Register>());
4656 } else {
4657 InvokeRuntimeCallingConvention calling_convention;
4658 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4659 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4660 DCHECK_EQ(R0, out.AsRegister<Register>());
4661
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004662 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004663 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004664 }
Calin Juravled0d48522014-11-04 16:40:20 +00004665 break;
4666 }
4667
Calin Juravle7c4954d2014-10-28 16:57:40 +00004668 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004669 InvokeRuntimeCallingConvention calling_convention;
4670 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4671 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4672 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4673 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4674 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004675 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004676
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004677 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004678 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004679 break;
4680 }
4681
4682 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004683 __ vdivs(out.AsFpuRegister<SRegister>(),
4684 first.AsFpuRegister<SRegister>(),
4685 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004686 break;
4687 }
4688
4689 case Primitive::kPrimDouble: {
4690 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4691 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4692 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4693 break;
4694 }
4695
4696 default:
4697 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4698 }
4699}
4700
Calin Juravlebacfec32014-11-14 15:54:36 +00004701void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004702 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004703
4704 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004705 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004706 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4707 // sdiv will be replaced by other instruction sequence.
4708 call_kind = LocationSummary::kNoCall;
4709 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4710 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004711 // Have hardware divide instruction for int, do it with three instructions.
4712 call_kind = LocationSummary::kNoCall;
4713 }
4714
Calin Juravlebacfec32014-11-14 15:54:36 +00004715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4716
Calin Juravled2ec87d2014-12-08 14:24:46 +00004717 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004718 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004719 if (rem->InputAt(1)->IsConstant()) {
4720 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004721 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004723 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4724 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004725 // No temp register required.
4726 } else {
4727 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004728 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004729 locations->AddTemp(Location::RequiresRegister());
4730 }
4731 }
4732 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004733 locations->SetInAt(0, Location::RequiresRegister());
4734 locations->SetInAt(1, Location::RequiresRegister());
4735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4736 locations->AddTemp(Location::RequiresRegister());
4737 } else {
4738 InvokeRuntimeCallingConvention calling_convention;
4739 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4740 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004741 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004742 // we only need the latter.
4743 locations->SetOut(Location::RegisterLocation(R1));
4744 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004745 break;
4746 }
4747 case Primitive::kPrimLong: {
4748 InvokeRuntimeCallingConvention calling_convention;
4749 locations->SetInAt(0, Location::RegisterPairLocation(
4750 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4751 locations->SetInAt(1, Location::RegisterPairLocation(
4752 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4753 // The runtime helper puts the output in R2,R3.
4754 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4755 break;
4756 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004757 case Primitive::kPrimFloat: {
4758 InvokeRuntimeCallingConvention calling_convention;
4759 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4760 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4761 locations->SetOut(Location::FpuRegisterLocation(S0));
4762 break;
4763 }
4764
Calin Juravlebacfec32014-11-14 15:54:36 +00004765 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004766 InvokeRuntimeCallingConvention calling_convention;
4767 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4768 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4769 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4770 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4771 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004772 break;
4773 }
4774
4775 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004776 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004777 }
4778}
4779
4780void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4781 LocationSummary* locations = rem->GetLocations();
4782 Location out = locations->Out();
4783 Location first = locations->InAt(0);
4784 Location second = locations->InAt(1);
4785
Calin Juravled2ec87d2014-12-08 14:24:46 +00004786 Primitive::Type type = rem->GetResultType();
4787 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004788 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004789 if (second.IsConstant()) {
4790 GenerateDivRemConstantIntegral(rem);
4791 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004792 Register reg1 = first.AsRegister<Register>();
4793 Register reg2 = second.AsRegister<Register>();
4794 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004795
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004796 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004797 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004798 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004799 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004800 } else {
4801 InvokeRuntimeCallingConvention calling_convention;
4802 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4803 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4804 DCHECK_EQ(R1, out.AsRegister<Register>());
4805
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004806 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004807 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004808 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004809 break;
4810 }
4811
4812 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004813 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004814 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004815 break;
4816 }
4817
Calin Juravled2ec87d2014-12-08 14:24:46 +00004818 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004819 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004820 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004821 break;
4822 }
4823
Calin Juravlebacfec32014-11-14 15:54:36 +00004824 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004825 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004826 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004827 break;
4828 }
4829
4830 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004831 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004832 }
4833}
4834
Calin Juravled0d48522014-11-04 16:40:20 +00004835void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004836 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004837 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004838}
4839
4840void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004841 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004842 codegen_->AddSlowPath(slow_path);
4843
4844 LocationSummary* locations = instruction->GetLocations();
4845 Location value = locations->InAt(0);
4846
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004847 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004848 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004849 case Primitive::kPrimByte:
4850 case Primitive::kPrimChar:
4851 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004852 case Primitive::kPrimInt: {
4853 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004854 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004855 } else {
4856 DCHECK(value.IsConstant()) << value;
4857 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4858 __ b(slow_path->GetEntryLabel());
4859 }
4860 }
4861 break;
4862 }
4863 case Primitive::kPrimLong: {
4864 if (value.IsRegisterPair()) {
4865 __ orrs(IP,
4866 value.AsRegisterPairLow<Register>(),
4867 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4868 __ b(slow_path->GetEntryLabel(), EQ);
4869 } else {
4870 DCHECK(value.IsConstant()) << value;
4871 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4872 __ b(slow_path->GetEntryLabel());
4873 }
4874 }
4875 break;
4876 default:
4877 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4878 }
4879 }
Calin Juravled0d48522014-11-04 16:40:20 +00004880}
4881
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004882void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4883 Register in = locations->InAt(0).AsRegister<Register>();
4884 Location rhs = locations->InAt(1);
4885 Register out = locations->Out().AsRegister<Register>();
4886
4887 if (rhs.IsConstant()) {
4888 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4889 // so map all rotations to a +ve. equivalent in that range.
4890 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4891 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4892 if (rot) {
4893 // Rotate, mapping left rotations to right equivalents if necessary.
4894 // (e.g. left by 2 bits == right by 30.)
4895 __ Ror(out, in, rot);
4896 } else if (out != in) {
4897 __ Mov(out, in);
4898 }
4899 } else {
4900 __ Ror(out, in, rhs.AsRegister<Register>());
4901 }
4902}
4903
4904// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4905// rotates by swapping input regs (effectively rotating by the first 32-bits of
4906// a larger rotation) or flipping direction (thus treating larger right/left
4907// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004908void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4909 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004910 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4911 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4912 Location rhs = locations->InAt(1);
4913 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4914 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4915
4916 if (rhs.IsConstant()) {
4917 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4918 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004919 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004920 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4921 // logic below to a simple pair of binary orr.
4922 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4923 if (rot >= kArmBitsPerWord) {
4924 rot -= kArmBitsPerWord;
4925 std::swap(in_reg_hi, in_reg_lo);
4926 }
4927 // Rotate, or mov to out for zero or word size rotations.
4928 if (rot != 0u) {
4929 __ Lsr(out_reg_hi, in_reg_hi, rot);
4930 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4931 __ Lsr(out_reg_lo, in_reg_lo, rot);
4932 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4933 } else {
4934 __ Mov(out_reg_lo, in_reg_lo);
4935 __ Mov(out_reg_hi, in_reg_hi);
4936 }
4937 } else {
4938 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4939 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4940 Label end;
4941 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004942 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004943
4944 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4945 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4946 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4947 __ b(&shift_by_32_plus_shift_right, CC);
4948
4949 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4950 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4951 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4952 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4953 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4954 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4955 __ Lsr(shift_left, in_reg_hi, shift_right);
4956 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004957 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004958
4959 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4960 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4961 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4962 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4963 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4964 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4965 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4966 __ Lsl(shift_right, in_reg_hi, shift_left);
4967 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4968
Anton Kirilov6f644202017-02-27 18:29:45 +00004969 if (end.IsLinked()) {
4970 __ Bind(&end);
4971 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004972 }
4973}
Roland Levillain22c49222016-03-18 14:04:28 +00004974
4975void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004976 LocationSummary* locations =
4977 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4978 switch (ror->GetResultType()) {
4979 case Primitive::kPrimInt: {
4980 locations->SetInAt(0, Location::RequiresRegister());
4981 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4982 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4983 break;
4984 }
4985 case Primitive::kPrimLong: {
4986 locations->SetInAt(0, Location::RequiresRegister());
4987 if (ror->InputAt(1)->IsConstant()) {
4988 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4989 } else {
4990 locations->SetInAt(1, Location::RequiresRegister());
4991 locations->AddTemp(Location::RequiresRegister());
4992 locations->AddTemp(Location::RequiresRegister());
4993 }
4994 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4995 break;
4996 }
4997 default:
4998 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4999 }
5000}
5001
Roland Levillain22c49222016-03-18 14:04:28 +00005002void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005003 LocationSummary* locations = ror->GetLocations();
5004 Primitive::Type type = ror->GetResultType();
5005 switch (type) {
5006 case Primitive::kPrimInt: {
5007 HandleIntegerRotate(locations);
5008 break;
5009 }
5010 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00005011 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005012 break;
5013 }
5014 default:
5015 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005016 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005017 }
5018}
5019
Calin Juravle9aec02f2014-11-18 23:06:35 +00005020void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
5021 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5022
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005023 LocationSummary* locations =
5024 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005025
5026 switch (op->GetResultType()) {
5027 case Primitive::kPrimInt: {
5028 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005029 if (op->InputAt(1)->IsConstant()) {
5030 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5032 } else {
5033 locations->SetInAt(1, Location::RequiresRegister());
5034 // Make the output overlap, as it will be used to hold the masked
5035 // second input.
5036 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5037 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005038 break;
5039 }
5040 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005041 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005042 if (op->InputAt(1)->IsConstant()) {
5043 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5044 // For simplicity, use kOutputOverlap even though we only require that low registers
5045 // don't clash with high registers which the register allocator currently guarantees.
5046 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5047 } else {
5048 locations->SetInAt(1, Location::RequiresRegister());
5049 locations->AddTemp(Location::RequiresRegister());
5050 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5051 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005052 break;
5053 }
5054 default:
5055 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5056 }
5057}
5058
5059void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
5060 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5061
5062 LocationSummary* locations = op->GetLocations();
5063 Location out = locations->Out();
5064 Location first = locations->InAt(0);
5065 Location second = locations->InAt(1);
5066
5067 Primitive::Type type = op->GetResultType();
5068 switch (type) {
5069 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005070 Register out_reg = out.AsRegister<Register>();
5071 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005072 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005073 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005074 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00005075 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005076 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005077 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005078 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005079 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005080 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005081 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005082 }
5083 } else {
5084 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00005085 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00005086 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00005087 __ Mov(out_reg, first_reg);
5088 } else if (op->IsShl()) {
5089 __ Lsl(out_reg, first_reg, shift_value);
5090 } else if (op->IsShr()) {
5091 __ Asr(out_reg, first_reg, shift_value);
5092 } else {
5093 __ Lsr(out_reg, first_reg, shift_value);
5094 }
5095 }
5096 break;
5097 }
5098 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005099 Register o_h = out.AsRegisterPairHigh<Register>();
5100 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005101
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005102 Register high = first.AsRegisterPairHigh<Register>();
5103 Register low = first.AsRegisterPairLow<Register>();
5104
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005105 if (second.IsRegister()) {
5106 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005107
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005108 Register second_reg = second.AsRegister<Register>();
5109
5110 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005111 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005112 // Shift the high part
5113 __ Lsl(o_h, high, o_l);
5114 // Shift the low part and `or` what overflew on the high part
5115 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
5116 __ Lsr(temp, low, temp);
5117 __ orr(o_h, o_h, ShifterOperand(temp));
5118 // If the shift is > 32 bits, override the high part
5119 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
5120 __ it(PL);
5121 __ Lsl(o_h, low, temp, PL);
5122 // Shift the low part
5123 __ Lsl(o_l, low, o_l);
5124 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005125 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005126 // Shift the low part
5127 __ Lsr(o_l, low, o_h);
5128 // Shift the high part and `or` what underflew on the low part
5129 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
5130 __ Lsl(temp, high, temp);
5131 __ orr(o_l, o_l, ShifterOperand(temp));
5132 // If the shift is > 32 bits, override the low part
5133 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
5134 __ it(PL);
5135 __ Asr(o_l, high, temp, PL);
5136 // Shift the high part
5137 __ Asr(o_h, high, o_h);
5138 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005139 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005140 // same as Shr except we use `Lsr`s and not `Asr`s
5141 __ Lsr(o_l, low, o_h);
5142 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
5143 __ Lsl(temp, high, temp);
5144 __ orr(o_l, o_l, ShifterOperand(temp));
5145 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
5146 __ it(PL);
5147 __ Lsr(o_l, high, temp, PL);
5148 __ Lsr(o_h, high, o_h);
5149 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005150 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005151 // Register allocator doesn't create partial overlap.
5152 DCHECK_NE(o_l, high);
5153 DCHECK_NE(o_h, low);
5154 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00005155 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005156 if (shift_value > 32) {
5157 if (op->IsShl()) {
5158 __ Lsl(o_h, low, shift_value - 32);
5159 __ LoadImmediate(o_l, 0);
5160 } else if (op->IsShr()) {
5161 __ Asr(o_l, high, shift_value - 32);
5162 __ Asr(o_h, high, 31);
5163 } else {
5164 __ Lsr(o_l, high, shift_value - 32);
5165 __ LoadImmediate(o_h, 0);
5166 }
5167 } else if (shift_value == 32) {
5168 if (op->IsShl()) {
5169 __ mov(o_h, ShifterOperand(low));
5170 __ LoadImmediate(o_l, 0);
5171 } else if (op->IsShr()) {
5172 __ mov(o_l, ShifterOperand(high));
5173 __ Asr(o_h, high, 31);
5174 } else {
5175 __ mov(o_l, ShifterOperand(high));
5176 __ LoadImmediate(o_h, 0);
5177 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00005178 } else if (shift_value == 1) {
5179 if (op->IsShl()) {
5180 __ Lsls(o_l, low, 1);
5181 __ adc(o_h, high, ShifterOperand(high));
5182 } else if (op->IsShr()) {
5183 __ Asrs(o_h, high, 1);
5184 __ Rrx(o_l, low);
5185 } else {
5186 __ Lsrs(o_h, high, 1);
5187 __ Rrx(o_l, low);
5188 }
5189 } else {
5190 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005191 if (op->IsShl()) {
5192 __ Lsl(o_h, high, shift_value);
5193 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
5194 __ Lsl(o_l, low, shift_value);
5195 } else if (op->IsShr()) {
5196 __ Lsr(o_l, low, shift_value);
5197 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
5198 __ Asr(o_h, high, shift_value);
5199 } else {
5200 __ Lsr(o_l, low, shift_value);
5201 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
5202 __ Lsr(o_h, high, shift_value);
5203 }
5204 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005205 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005206 break;
5207 }
5208 default:
5209 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005210 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005211 }
5212}
5213
5214void LocationsBuilderARM::VisitShl(HShl* shl) {
5215 HandleShift(shl);
5216}
5217
5218void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
5219 HandleShift(shl);
5220}
5221
5222void LocationsBuilderARM::VisitShr(HShr* shr) {
5223 HandleShift(shr);
5224}
5225
5226void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
5227 HandleShift(shr);
5228}
5229
5230void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
5231 HandleShift(ushr);
5232}
5233
5234void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
5235 HandleShift(ushr);
5236}
5237
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005238void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005239 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005240 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00005241 if (instruction->IsStringAlloc()) {
5242 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5243 } else {
5244 InvokeRuntimeCallingConvention calling_convention;
5245 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005246 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005247 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005248}
5249
5250void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005251 // Note: if heap poisoning is enabled, the entry point takes cares
5252 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005253 if (instruction->IsStringAlloc()) {
5254 // String is allocated through StringFactory. Call NewEmptyString entry point.
5255 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005256 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005257 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5258 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
5259 __ blx(LR);
5260 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5261 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005262 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005263 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005264 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005265}
5266
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005267void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
5268 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005270 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005271 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005272 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5273 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005274}
5275
5276void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005277 // Note: if heap poisoning is enabled, the entry point takes cares
5278 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00005279 QuickEntrypointEnum entrypoint =
5280 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5281 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005282 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00005283 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005284}
5285
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005286void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005287 LocationSummary* locations =
5288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005289 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5290 if (location.IsStackSlot()) {
5291 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5292 } else if (location.IsDoubleStackSlot()) {
5293 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005294 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005295 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005296}
5297
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005298void InstructionCodeGeneratorARM::VisitParameterValue(
5299 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01005300 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005301}
5302
5303void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
5304 LocationSummary* locations =
5305 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5306 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5307}
5308
5309void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5310 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005311}
5312
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005313void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005314 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005315 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005316 locations->SetInAt(0, Location::RequiresRegister());
5317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005318}
5319
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005320void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
5321 LocationSummary* locations = not_->GetLocations();
5322 Location out = locations->Out();
5323 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005324 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005325 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00005326 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005327 break;
5328
5329 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01005330 __ mvn(out.AsRegisterPairLow<Register>(),
5331 ShifterOperand(in.AsRegisterPairLow<Register>()));
5332 __ mvn(out.AsRegisterPairHigh<Register>(),
5333 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005334 break;
5335
5336 default:
5337 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5338 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005339}
5340
David Brazdil66d126e2015-04-03 16:02:44 +01005341void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
5342 LocationSummary* locations =
5343 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5344 locations->SetInAt(0, Location::RequiresRegister());
5345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5346}
5347
5348void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005349 LocationSummary* locations = bool_not->GetLocations();
5350 Location out = locations->Out();
5351 Location in = locations->InAt(0);
5352 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
5353}
5354
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005355void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005356 LocationSummary* locations =
5357 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005358 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005359 case Primitive::kPrimBoolean:
5360 case Primitive::kPrimByte:
5361 case Primitive::kPrimShort:
5362 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005363 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00005364 case Primitive::kPrimLong: {
5365 locations->SetInAt(0, Location::RequiresRegister());
5366 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005367 // Output overlaps because it is written before doing the low comparison.
5368 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00005369 break;
5370 }
5371 case Primitive::kPrimFloat:
5372 case Primitive::kPrimDouble: {
5373 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005374 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00005375 locations->SetOut(Location::RequiresRegister());
5376 break;
5377 }
5378 default:
5379 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5380 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005381}
5382
5383void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005384 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005385 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005386 Location left = locations->InAt(0);
5387 Location right = locations->InAt(1);
5388
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005389 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005390 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00005391 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00005392 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00005393 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005394 case Primitive::kPrimBoolean:
5395 case Primitive::kPrimByte:
5396 case Primitive::kPrimShort:
5397 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005398 case Primitive::kPrimInt: {
5399 __ LoadImmediate(out, 0);
5400 __ cmp(left.AsRegister<Register>(),
5401 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
5402 less_cond = LT;
5403 break;
5404 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005405 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005406 __ cmp(left.AsRegisterPairHigh<Register>(),
5407 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005408 __ b(&less, LT);
5409 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01005410 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00005411 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005412 __ cmp(left.AsRegisterPairLow<Register>(),
5413 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005414 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00005415 break;
5416 }
5417 case Primitive::kPrimFloat:
5418 case Primitive::kPrimDouble: {
5419 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005420 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00005421 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005422 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005423 break;
5424 }
5425 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005426 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00005427 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005428 }
Aart Bika19616e2016-02-01 18:57:58 -08005429
Anton Kirilov6f644202017-02-27 18:29:45 +00005430 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00005431 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00005432
5433 __ Bind(&greater);
5434 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005435 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00005436
5437 __ Bind(&less);
5438 __ LoadImmediate(out, -1);
5439
Anton Kirilov6f644202017-02-27 18:29:45 +00005440 if (done.IsLinked()) {
5441 __ Bind(&done);
5442 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005443}
5444
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005445void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005446 LocationSummary* locations =
5447 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005448 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005449 locations->SetInAt(i, Location::Any());
5450 }
5451 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005452}
5453
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005454void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005455 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005456}
5457
Roland Levillainc9285912015-12-18 10:38:42 +00005458void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5459 // TODO (ported from quick): revisit ARM barrier kinds.
5460 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005461 switch (kind) {
5462 case MemBarrierKind::kAnyStore:
5463 case MemBarrierKind::kLoadAny:
5464 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005465 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005466 break;
5467 }
5468 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005469 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005470 break;
5471 }
5472 default:
5473 LOG(FATAL) << "Unexpected memory barrier " << kind;
5474 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005475 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005476}
5477
5478void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5479 uint32_t offset,
5480 Register out_lo,
5481 Register out_hi) {
5482 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005483 // Ensure `out_lo` is different from `addr`, so that loading
5484 // `offset` into `out_lo` does not clutter `addr`.
5485 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005486 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005487 __ add(IP, addr, ShifterOperand(out_lo));
5488 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005489 }
5490 __ ldrexd(out_lo, out_hi, addr);
5491}
5492
5493void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5494 uint32_t offset,
5495 Register value_lo,
5496 Register value_hi,
5497 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005498 Register temp2,
5499 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005500 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005501 if (offset != 0) {
5502 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005503 __ add(IP, addr, ShifterOperand(temp1));
5504 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005505 }
5506 __ Bind(&fail);
5507 // We need a load followed by store. (The address used in a STREX instruction must
5508 // be the same as the address in the most recently executed LDREX instruction.)
5509 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005510 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005511 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005512 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005513}
5514
5515void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5516 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5517
Nicolas Geoffray39468442014-09-02 15:17:15 +01005518 LocationSummary* locations =
5519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005520 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005521
Calin Juravle52c48962014-12-16 17:02:57 +00005522 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005523 if (Primitive::IsFloatingPointType(field_type)) {
5524 locations->SetInAt(1, Location::RequiresFpuRegister());
5525 } else {
5526 locations->SetInAt(1, Location::RequiresRegister());
5527 }
5528
Calin Juravle52c48962014-12-16 17:02:57 +00005529 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005530 bool generate_volatile = field_info.IsVolatile()
5531 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005532 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005533 bool needs_write_barrier =
5534 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005535 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005536 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005537 if (needs_write_barrier) {
5538 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005539 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005540 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005541 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005542 // - registers need to be consecutive
5543 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005544 // We don't test for ARM yet, and the assertion makes sure that we
5545 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005546 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5547
5548 locations->AddTemp(Location::RequiresRegister());
5549 locations->AddTemp(Location::RequiresRegister());
5550 if (field_type == Primitive::kPrimDouble) {
5551 // For doubles we need two more registers to copy the value.
5552 locations->AddTemp(Location::RegisterLocation(R2));
5553 locations->AddTemp(Location::RegisterLocation(R3));
5554 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005555 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005556}
5557
Calin Juravle52c48962014-12-16 17:02:57 +00005558void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005559 const FieldInfo& field_info,
5560 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005561 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5562
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005563 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005564 Register base = locations->InAt(0).AsRegister<Register>();
5565 Location value = locations->InAt(1);
5566
5567 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005568 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005569 Primitive::Type field_type = field_info.GetFieldType();
5570 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005571 bool needs_write_barrier =
5572 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005573
5574 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005575 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005576 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005577
5578 switch (field_type) {
5579 case Primitive::kPrimBoolean:
5580 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005581 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005582 break;
5583 }
5584
5585 case Primitive::kPrimShort:
5586 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005587 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005588 break;
5589 }
5590
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005592 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005593 if (kPoisonHeapReferences && needs_write_barrier) {
5594 // Note that in the case where `value` is a null reference,
5595 // we do not enter this block, as a null reference does not
5596 // need poisoning.
5597 DCHECK_EQ(field_type, Primitive::kPrimNot);
5598 Register temp = locations->GetTemp(0).AsRegister<Register>();
5599 __ Mov(temp, value.AsRegister<Register>());
5600 __ PoisonHeapReference(temp);
5601 __ StoreToOffset(kStoreWord, temp, base, offset);
5602 } else {
5603 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5604 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005605 break;
5606 }
5607
5608 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005609 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005610 GenerateWideAtomicStore(base, offset,
5611 value.AsRegisterPairLow<Register>(),
5612 value.AsRegisterPairHigh<Register>(),
5613 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005614 locations->GetTemp(1).AsRegister<Register>(),
5615 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005616 } else {
5617 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005618 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005619 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005620 break;
5621 }
5622
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005623 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005624 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005625 break;
5626 }
5627
5628 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005629 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005630 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005631 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5632 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5633
5634 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5635
5636 GenerateWideAtomicStore(base, offset,
5637 value_reg_lo,
5638 value_reg_hi,
5639 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005640 locations->GetTemp(3).AsRegister<Register>(),
5641 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005642 } else {
5643 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005644 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005645 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005646 break;
5647 }
5648
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005649 case Primitive::kPrimVoid:
5650 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005651 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005652 }
Calin Juravle52c48962014-12-16 17:02:57 +00005653
Calin Juravle77520bc2015-01-12 18:45:46 +00005654 // Longs and doubles are handled in the switch.
5655 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5656 codegen_->MaybeRecordImplicitNullCheck(instruction);
5657 }
5658
5659 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5660 Register temp = locations->GetTemp(0).AsRegister<Register>();
5661 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005662 codegen_->MarkGCCard(
5663 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005664 }
5665
Calin Juravle52c48962014-12-16 17:02:57 +00005666 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005667 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005668 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005669}
5670
Calin Juravle52c48962014-12-16 17:02:57 +00005671void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5672 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005673
5674 bool object_field_get_with_read_barrier =
5675 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005676 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005677 new (GetGraph()->GetArena()) LocationSummary(instruction,
5678 object_field_get_with_read_barrier ?
5679 LocationSummary::kCallOnSlowPath :
5680 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005681 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005682 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005683 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005684 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005685
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005686 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005687 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005688 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005689 // The output overlaps in case of volatile long: we don't want the
5690 // code generated by GenerateWideAtomicLoad to overwrite the
5691 // object's location. Likewise, in the case of an object field get
5692 // with read barriers enabled, we do not want the load to overwrite
5693 // the object's location, as we need it to emit the read barrier.
5694 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5695 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005696
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005697 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5698 locations->SetOut(Location::RequiresFpuRegister());
5699 } else {
5700 locations->SetOut(Location::RequiresRegister(),
5701 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5702 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005703 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005704 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005705 // - registers need to be consecutive
5706 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005707 // We don't test for ARM yet, and the assertion makes sure that we
5708 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005709 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5710 locations->AddTemp(Location::RequiresRegister());
5711 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005712 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5713 // We need a temporary register for the read barrier marking slow
5714 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005715 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5716 !Runtime::Current()->UseJitCompilation()) {
5717 // If link-time thunks for the Baker read barrier are enabled, for AOT
5718 // loads we need a temporary only if the offset is too big.
5719 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5720 locations->AddTemp(Location::RequiresRegister());
5721 }
5722 // And we always need the reserved entrypoint register.
5723 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5724 } else {
5725 locations->AddTemp(Location::RequiresRegister());
5726 }
Calin Juravle52c48962014-12-16 17:02:57 +00005727 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005728}
5729
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005730Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5731 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5732 << input->GetType();
5733 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5734 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5735 return Location::ConstantLocation(input->AsConstant());
5736 } else {
5737 return Location::RequiresFpuRegister();
5738 }
5739}
5740
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005741Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5742 Opcode opcode) {
5743 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5744 if (constant->IsConstant() &&
5745 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5746 return Location::ConstantLocation(constant->AsConstant());
5747 }
5748 return Location::RequiresRegister();
5749}
5750
5751bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5752 Opcode opcode) {
5753 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5754 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005755 Opcode high_opcode = opcode;
5756 SetCc low_set_cc = kCcDontCare;
5757 switch (opcode) {
5758 case SUB:
5759 // Flip the operation to an ADD.
5760 value = -value;
5761 opcode = ADD;
5762 FALLTHROUGH_INTENDED;
5763 case ADD:
5764 if (Low32Bits(value) == 0u) {
5765 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5766 }
5767 high_opcode = ADC;
5768 low_set_cc = kCcSet;
5769 break;
5770 default:
5771 break;
5772 }
5773 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5774 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005775 } else {
5776 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5777 }
5778}
5779
Vladimir Marko59751a72016-08-05 14:37:27 +01005780bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5781 Opcode opcode,
5782 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005783 ShifterOperand so;
5784 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005785 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005786 return true;
5787 }
5788 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005789 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005790 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005791 case AND: neg_opcode = BIC; neg_value = ~value; break;
5792 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5793 case ADD: neg_opcode = SUB; neg_value = -value; break;
5794 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5795 case SUB: neg_opcode = ADD; neg_value = -value; break;
5796 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5797 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005798 default:
5799 return false;
5800 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005801
5802 if (assembler->ShifterOperandCanHold(kNoRegister,
5803 kNoRegister,
5804 neg_opcode,
5805 neg_value,
5806 set_cc,
5807 &so)) {
5808 return true;
5809 }
5810
5811 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005812}
5813
Calin Juravle52c48962014-12-16 17:02:57 +00005814void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5815 const FieldInfo& field_info) {
5816 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005817
Calin Juravle52c48962014-12-16 17:02:57 +00005818 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005819 Location base_loc = locations->InAt(0);
5820 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005821 Location out = locations->Out();
5822 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005823 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005824 Primitive::Type field_type = field_info.GetFieldType();
5825 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5826
5827 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005828 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005829 __ LoadFromOffset(kLoadUnsignedByte, 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::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005833 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005834 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005835
Roland Levillainc9285912015-12-18 10:38:42 +00005836 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005837 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005838 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005839
Roland Levillainc9285912015-12-18 10:38:42 +00005840 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005841 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005842 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005843
5844 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005845 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005846 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005847
5848 case Primitive::kPrimNot: {
5849 // /* HeapReference<Object> */ out = *(base + offset)
5850 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5851 Location temp_loc = locations->GetTemp(0);
5852 // Note that a potential implicit null check is handled in this
5853 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5854 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5855 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5856 if (is_volatile) {
5857 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5858 }
5859 } else {
5860 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5861 codegen_->MaybeRecordImplicitNullCheck(instruction);
5862 if (is_volatile) {
5863 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5864 }
5865 // If read barriers are enabled, emit read barriers other than
5866 // Baker's using a slow path (and also unpoison the loaded
5867 // reference, if heap poisoning is enabled).
5868 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5869 }
5870 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005871 }
5872
Roland Levillainc9285912015-12-18 10:38:42 +00005873 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005874 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005875 GenerateWideAtomicLoad(base, offset,
5876 out.AsRegisterPairLow<Register>(),
5877 out.AsRegisterPairHigh<Register>());
5878 } else {
5879 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5880 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005881 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005882
Roland Levillainc9285912015-12-18 10:38:42 +00005883 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005884 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005885 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005886
5887 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005888 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005889 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005890 Register lo = locations->GetTemp(0).AsRegister<Register>();
5891 Register hi = locations->GetTemp(1).AsRegister<Register>();
5892 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005893 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005894 __ vmovdrr(out_reg, lo, hi);
5895 } else {
5896 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005897 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005898 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005899 break;
5900 }
5901
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005902 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005903 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005904 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005905 }
Calin Juravle52c48962014-12-16 17:02:57 +00005906
Roland Levillainc9285912015-12-18 10:38:42 +00005907 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5908 // Potential implicit null checks, in the case of reference or
5909 // double fields, are handled in the previous switch statement.
5910 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005911 codegen_->MaybeRecordImplicitNullCheck(instruction);
5912 }
5913
Calin Juravle52c48962014-12-16 17:02:57 +00005914 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005915 if (field_type == Primitive::kPrimNot) {
5916 // Memory barriers, in the case of references, are also handled
5917 // in the previous switch statement.
5918 } else {
5919 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5920 }
Roland Levillain4d027112015-07-01 15:41:14 +01005921 }
Calin Juravle52c48962014-12-16 17:02:57 +00005922}
5923
5924void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5925 HandleFieldSet(instruction, instruction->GetFieldInfo());
5926}
5927
5928void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005929 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005930}
5931
5932void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5933 HandleFieldGet(instruction, instruction->GetFieldInfo());
5934}
5935
5936void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5937 HandleFieldGet(instruction, instruction->GetFieldInfo());
5938}
5939
5940void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5941 HandleFieldGet(instruction, instruction->GetFieldInfo());
5942}
5943
5944void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5945 HandleFieldGet(instruction, instruction->GetFieldInfo());
5946}
5947
5948void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5949 HandleFieldSet(instruction, instruction->GetFieldInfo());
5950}
5951
5952void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005953 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005954}
5955
Calin Juravlee460d1d2015-09-29 04:52:17 +01005956void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5957 HUnresolvedInstanceFieldGet* instruction) {
5958 FieldAccessCallingConventionARM calling_convention;
5959 codegen_->CreateUnresolvedFieldLocationSummary(
5960 instruction, instruction->GetFieldType(), calling_convention);
5961}
5962
5963void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5964 HUnresolvedInstanceFieldGet* instruction) {
5965 FieldAccessCallingConventionARM calling_convention;
5966 codegen_->GenerateUnresolvedFieldAccess(instruction,
5967 instruction->GetFieldType(),
5968 instruction->GetFieldIndex(),
5969 instruction->GetDexPc(),
5970 calling_convention);
5971}
5972
5973void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5974 HUnresolvedInstanceFieldSet* instruction) {
5975 FieldAccessCallingConventionARM calling_convention;
5976 codegen_->CreateUnresolvedFieldLocationSummary(
5977 instruction, instruction->GetFieldType(), calling_convention);
5978}
5979
5980void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5981 HUnresolvedInstanceFieldSet* instruction) {
5982 FieldAccessCallingConventionARM calling_convention;
5983 codegen_->GenerateUnresolvedFieldAccess(instruction,
5984 instruction->GetFieldType(),
5985 instruction->GetFieldIndex(),
5986 instruction->GetDexPc(),
5987 calling_convention);
5988}
5989
5990void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5991 HUnresolvedStaticFieldGet* instruction) {
5992 FieldAccessCallingConventionARM calling_convention;
5993 codegen_->CreateUnresolvedFieldLocationSummary(
5994 instruction, instruction->GetFieldType(), calling_convention);
5995}
5996
5997void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5998 HUnresolvedStaticFieldGet* instruction) {
5999 FieldAccessCallingConventionARM calling_convention;
6000 codegen_->GenerateUnresolvedFieldAccess(instruction,
6001 instruction->GetFieldType(),
6002 instruction->GetFieldIndex(),
6003 instruction->GetDexPc(),
6004 calling_convention);
6005}
6006
6007void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
6008 HUnresolvedStaticFieldSet* instruction) {
6009 FieldAccessCallingConventionARM calling_convention;
6010 codegen_->CreateUnresolvedFieldLocationSummary(
6011 instruction, instruction->GetFieldType(), calling_convention);
6012}
6013
6014void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
6015 HUnresolvedStaticFieldSet* instruction) {
6016 FieldAccessCallingConventionARM calling_convention;
6017 codegen_->GenerateUnresolvedFieldAccess(instruction,
6018 instruction->GetFieldType(),
6019 instruction->GetFieldIndex(),
6020 instruction->GetDexPc(),
6021 calling_convention);
6022}
6023
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006024void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006025 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6026 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006027}
6028
Calin Juravle2ae48182016-03-16 14:05:09 +00006029void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
6030 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006031 return;
6032 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006033 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006034
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006035 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006036 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006037}
6038
Calin Juravle2ae48182016-03-16 14:05:09 +00006039void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01006040 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006041 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006042
6043 LocationSummary* locations = instruction->GetLocations();
6044 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006045
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006046 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006047}
6048
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006049void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006050 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006051}
6052
Artem Serov6c916792016-07-11 14:02:34 +01006053static LoadOperandType GetLoadOperandType(Primitive::Type type) {
6054 switch (type) {
6055 case Primitive::kPrimNot:
6056 return kLoadWord;
6057 case Primitive::kPrimBoolean:
6058 return kLoadUnsignedByte;
6059 case Primitive::kPrimByte:
6060 return kLoadSignedByte;
6061 case Primitive::kPrimChar:
6062 return kLoadUnsignedHalfword;
6063 case Primitive::kPrimShort:
6064 return kLoadSignedHalfword;
6065 case Primitive::kPrimInt:
6066 return kLoadWord;
6067 case Primitive::kPrimLong:
6068 return kLoadWordPair;
6069 case Primitive::kPrimFloat:
6070 return kLoadSWord;
6071 case Primitive::kPrimDouble:
6072 return kLoadDWord;
6073 default:
6074 LOG(FATAL) << "Unreachable type " << type;
6075 UNREACHABLE();
6076 }
6077}
6078
6079static StoreOperandType GetStoreOperandType(Primitive::Type type) {
6080 switch (type) {
6081 case Primitive::kPrimNot:
6082 return kStoreWord;
6083 case Primitive::kPrimBoolean:
6084 case Primitive::kPrimByte:
6085 return kStoreByte;
6086 case Primitive::kPrimChar:
6087 case Primitive::kPrimShort:
6088 return kStoreHalfword;
6089 case Primitive::kPrimInt:
6090 return kStoreWord;
6091 case Primitive::kPrimLong:
6092 return kStoreWordPair;
6093 case Primitive::kPrimFloat:
6094 return kStoreSWord;
6095 case Primitive::kPrimDouble:
6096 return kStoreDWord;
6097 default:
6098 LOG(FATAL) << "Unreachable type " << type;
6099 UNREACHABLE();
6100 }
6101}
6102
6103void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
6104 Location out_loc,
6105 Register base,
6106 Register reg_offset,
6107 Condition cond) {
6108 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6109 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
6110
6111 switch (type) {
6112 case Primitive::kPrimByte:
6113 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
6114 break;
6115 case Primitive::kPrimBoolean:
6116 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
6117 break;
6118 case Primitive::kPrimShort:
6119 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
6120 break;
6121 case Primitive::kPrimChar:
6122 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
6123 break;
6124 case Primitive::kPrimNot:
6125 case Primitive::kPrimInt:
6126 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
6127 break;
6128 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
6129 case Primitive::kPrimLong:
6130 case Primitive::kPrimFloat:
6131 case Primitive::kPrimDouble:
6132 default:
6133 LOG(FATAL) << "Unreachable type " << type;
6134 UNREACHABLE();
6135 }
6136}
6137
6138void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
6139 Location loc,
6140 Register base,
6141 Register reg_offset,
6142 Condition cond) {
6143 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6144 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
6145
6146 switch (type) {
6147 case Primitive::kPrimByte:
6148 case Primitive::kPrimBoolean:
6149 __ strb(loc.AsRegister<Register>(), mem_address, cond);
6150 break;
6151 case Primitive::kPrimShort:
6152 case Primitive::kPrimChar:
6153 __ strh(loc.AsRegister<Register>(), mem_address, cond);
6154 break;
6155 case Primitive::kPrimNot:
6156 case Primitive::kPrimInt:
6157 __ str(loc.AsRegister<Register>(), mem_address, cond);
6158 break;
6159 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
6160 case Primitive::kPrimLong:
6161 case Primitive::kPrimFloat:
6162 case Primitive::kPrimDouble:
6163 default:
6164 LOG(FATAL) << "Unreachable type " << type;
6165 UNREACHABLE();
6166 }
6167}
6168
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006169void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006170 bool object_array_get_with_read_barrier =
6171 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006172 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00006173 new (GetGraph()->GetArena()) LocationSummary(instruction,
6174 object_array_get_with_read_barrier ?
6175 LocationSummary::kCallOnSlowPath :
6176 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006177 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006178 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006179 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006180 locations->SetInAt(0, Location::RequiresRegister());
6181 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006182 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6183 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6184 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00006185 // The output overlaps in the case of an object array get with
6186 // read barriers enabled: we do not want the move to overwrite the
6187 // array's location, as we need it to emit the read barrier.
6188 locations->SetOut(
6189 Location::RequiresRegister(),
6190 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006191 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006192 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6193 // We need a temporary register for the read barrier marking slow
6194 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
6195 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6196 !Runtime::Current()->UseJitCompilation() &&
6197 instruction->GetIndex()->IsConstant()) {
6198 // Array loads with constant index are treated as field loads.
6199 // If link-time thunks for the Baker read barrier are enabled, for AOT
6200 // constant index loads we need a temporary only if the offset is too big.
6201 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6202 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
6203 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
6204 if (offset >= kReferenceLoadMinFarOffset) {
6205 locations->AddTemp(Location::RequiresRegister());
6206 }
6207 // And we always need the reserved entrypoint register.
6208 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6209 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6210 !Runtime::Current()->UseJitCompilation() &&
6211 !instruction->GetIndex()->IsConstant()) {
6212 // We need a non-scratch temporary for the array data pointer.
6213 locations->AddTemp(Location::RequiresRegister());
6214 // And we always need the reserved entrypoint register.
6215 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6216 } else {
6217 locations->AddTemp(Location::RequiresRegister());
6218 }
6219 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6220 // Also need a temporary for String compression feature.
Roland Levillainc9285912015-12-18 10:38:42 +00006221 locations->AddTemp(Location::RequiresRegister());
6222 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006223}
6224
6225void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
6226 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006227 Location obj_loc = locations->InAt(0);
6228 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006229 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00006230 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006231 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00006232 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07006233 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6234 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01006235 HInstruction* array_instr = instruction->GetArray();
6236 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01006237
Roland Levillain4d027112015-07-01 15:41:14 +01006238 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01006239 case Primitive::kPrimBoolean:
6240 case Primitive::kPrimByte:
6241 case Primitive::kPrimShort:
6242 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00006243 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006244 Register length;
6245 if (maybe_compressed_char_at) {
6246 length = locations->GetTemp(0).AsRegister<Register>();
6247 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6248 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
6249 codegen_->MaybeRecordImplicitNullCheck(instruction);
6250 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006251 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006252 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07006253 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07006254 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006255 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006256 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6257 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6258 "Expecting 0=compressed, 1=uncompressed");
6259 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07006260 __ LoadFromOffset(kLoadUnsignedByte,
6261 out_loc.AsRegister<Register>(),
6262 obj,
6263 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006264 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07006265 __ Bind(&uncompressed_load);
6266 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
6267 out_loc.AsRegister<Register>(),
6268 obj,
6269 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006270 if (done.IsLinked()) {
6271 __ Bind(&done);
6272 }
jessicahandojo05765752016-09-09 19:01:32 -07006273 } else {
6274 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01006275
jessicahandojo05765752016-09-09 19:01:32 -07006276 LoadOperandType load_type = GetLoadOperandType(type);
6277 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
6278 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006279 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006280 Register temp = IP;
6281
6282 if (has_intermediate_address) {
6283 // We do not need to compute the intermediate address from the array: the
6284 // input instruction has done it already. See the comment in
6285 // `TryExtractArrayAccessAddress()`.
6286 if (kIsDebugBuild) {
6287 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6288 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6289 }
6290 temp = obj;
6291 } else {
6292 __ add(temp, obj, ShifterOperand(data_offset));
6293 }
jessicahandojo05765752016-09-09 19:01:32 -07006294 if (maybe_compressed_char_at) {
6295 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006296 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006297 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6298 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6299 "Expecting 0=compressed, 1=uncompressed");
6300 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07006301 __ ldrb(out_loc.AsRegister<Register>(),
6302 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006303 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07006304 __ Bind(&uncompressed_load);
6305 __ ldrh(out_loc.AsRegister<Register>(),
6306 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006307 if (done.IsLinked()) {
6308 __ Bind(&done);
6309 }
jessicahandojo05765752016-09-09 19:01:32 -07006310 } else {
6311 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
6312 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006313 }
6314 break;
6315 }
6316
Roland Levillainc9285912015-12-18 10:38:42 +00006317 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00006318 // The read barrier instrumentation of object ArrayGet
6319 // instructions does not support the HIntermediateAddress
6320 // instruction.
6321 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6322
Roland Levillainc9285912015-12-18 10:38:42 +00006323 static_assert(
6324 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6325 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006326 // /* HeapReference<Object> */ out =
6327 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6328 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6329 Location temp = locations->GetTemp(0);
6330 // Note that a potential implicit null check is handled in this
6331 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006332 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6333 if (index.IsConstant()) {
6334 // Array load with a constant index can be treated as a field load.
6335 data_offset += helpers::Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
6336 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6337 out_loc,
6338 obj,
6339 data_offset,
6340 locations->GetTemp(0),
6341 /* needs_null_check */ false);
6342 } else {
6343 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6344 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6345 }
Roland Levillainc9285912015-12-18 10:38:42 +00006346 } else {
6347 Register out = out_loc.AsRegister<Register>();
6348 if (index.IsConstant()) {
6349 size_t offset =
6350 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6351 __ LoadFromOffset(kLoadWord, out, obj, offset);
6352 codegen_->MaybeRecordImplicitNullCheck(instruction);
6353 // If read barriers are enabled, emit read barriers other than
6354 // Baker's using a slow path (and also unpoison the loaded
6355 // reference, if heap poisoning is enabled).
6356 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6357 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006358 Register temp = IP;
6359
6360 if (has_intermediate_address) {
6361 // We do not need to compute the intermediate address from the array: the
6362 // input instruction has done it already. See the comment in
6363 // `TryExtractArrayAccessAddress()`.
6364 if (kIsDebugBuild) {
6365 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6366 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6367 }
6368 temp = obj;
6369 } else {
6370 __ add(temp, obj, ShifterOperand(data_offset));
6371 }
6372 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01006373
Roland Levillainc9285912015-12-18 10:38:42 +00006374 codegen_->MaybeRecordImplicitNullCheck(instruction);
6375 // If read barriers are enabled, emit read barriers other than
6376 // Baker's using a slow path (and also unpoison the loaded
6377 // reference, if heap poisoning is enabled).
6378 codegen_->MaybeGenerateReadBarrierSlow(
6379 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6380 }
6381 }
6382 break;
6383 }
6384
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006385 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006386 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006387 size_t offset =
6388 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006389 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006390 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006391 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006392 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006393 }
6394 break;
6395 }
6396
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006397 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00006398 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006399 if (index.IsConstant()) {
6400 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006401 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006402 } else {
6403 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00006404 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006405 }
6406 break;
6407 }
6408
6409 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00006410 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006411 if (index.IsConstant()) {
6412 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006413 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006414 } else {
6415 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006416 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006417 }
6418 break;
6419 }
6420
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006421 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01006422 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006423 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006424 }
Roland Levillain4d027112015-07-01 15:41:14 +01006425
6426 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00006427 // Potential implicit null checks, in the case of reference
6428 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07006429 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00006430 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01006431 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006432}
6433
6434void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006435 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006436
6437 bool needs_write_barrier =
6438 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00006439 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006440
Nicolas Geoffray39468442014-09-02 15:17:15 +01006441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006442 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01006443 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00006444 LocationSummary::kCallOnSlowPath :
6445 LocationSummary::kNoCall);
6446
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006447 locations->SetInAt(0, Location::RequiresRegister());
6448 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6449 if (Primitive::IsFloatingPointType(value_type)) {
6450 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006451 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006452 locations->SetInAt(2, Location::RequiresRegister());
6453 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006454 if (needs_write_barrier) {
6455 // Temporary registers for the write barrier.
6456 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006457 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006458 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006459}
6460
6461void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
6462 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006463 Location array_loc = locations->InAt(0);
6464 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006465 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006466 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00006467 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006468 bool needs_write_barrier =
6469 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01006470 uint32_t data_offset =
6471 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6472 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01006473 HInstruction* array_instr = instruction->GetArray();
6474 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006475
6476 switch (value_type) {
6477 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01006478 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006479 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01006480 case Primitive::kPrimChar:
6481 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006482 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006483 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
6484 uint32_t full_offset =
6485 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6486 StoreOperandType store_type = GetStoreOperandType(value_type);
6487 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006488 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006489 Register temp = IP;
6490
6491 if (has_intermediate_address) {
6492 // We do not need to compute the intermediate address from the array: the
6493 // input instruction has done it already. See the comment in
6494 // `TryExtractArrayAccessAddress()`.
6495 if (kIsDebugBuild) {
6496 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6497 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
6498 }
6499 temp = array;
6500 } else {
6501 __ add(temp, array, ShifterOperand(data_offset));
6502 }
Artem Serov6c916792016-07-11 14:02:34 +01006503 codegen_->StoreToShiftedRegOffset(value_type,
6504 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01006505 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006506 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006507 }
6508 break;
6509 }
6510
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006511 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006512 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006513 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6514 // See the comment in instruction_simplifier_shared.cc.
6515 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006516
6517 if (instruction->InputAt(2)->IsNullConstant()) {
6518 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006519 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006520 size_t offset =
6521 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006522 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006523 } else {
6524 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006525 __ add(IP, array, ShifterOperand(data_offset));
6526 codegen_->StoreToShiftedRegOffset(value_type,
6527 value_loc,
6528 IP,
6529 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006530 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006531 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006532 DCHECK(!needs_write_barrier);
6533 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006534 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006535 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006536
6537 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006538 Location temp1_loc = locations->GetTemp(0);
6539 Register temp1 = temp1_loc.AsRegister<Register>();
6540 Location temp2_loc = locations->GetTemp(1);
6541 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006542 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6543 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6544 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6545 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006546 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006547 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006548
Roland Levillain3b359c72015-11-17 19:35:12 +00006549 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006550 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6551 codegen_->AddSlowPath(slow_path);
6552 if (instruction->GetValueCanBeNull()) {
6553 Label non_zero;
6554 __ CompareAndBranchIfNonZero(value, &non_zero);
6555 if (index.IsConstant()) {
6556 size_t offset =
6557 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6558 __ StoreToOffset(kStoreWord, value, array, offset);
6559 } else {
6560 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006561 __ add(IP, array, ShifterOperand(data_offset));
6562 codegen_->StoreToShiftedRegOffset(value_type,
6563 value_loc,
6564 IP,
6565 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006566 }
6567 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006568 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006569 __ Bind(&non_zero);
6570 }
6571
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006572 // Note that when read barriers are enabled, the type checks
6573 // are performed without read barriers. This is fine, even in
6574 // the case where a class object is in the from-space after
6575 // the flip, as a comparison involving such a type would not
6576 // produce a false positive; it may of course produce a false
6577 // negative, in which case we would take the ArraySet slow
6578 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006579
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006580 // /* HeapReference<Class> */ temp1 = array->klass_
6581 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6582 codegen_->MaybeRecordImplicitNullCheck(instruction);
6583 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006584
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006585 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6586 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6587 // /* HeapReference<Class> */ temp2 = value->klass_
6588 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6589 // If heap poisoning is enabled, no need to unpoison `temp1`
6590 // nor `temp2`, as we are comparing two poisoned references.
6591 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006592
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006593 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6594 Label do_put;
6595 __ b(&do_put, EQ);
6596 // If heap poisoning is enabled, the `temp1` reference has
6597 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006598 __ MaybeUnpoisonHeapReference(temp1);
6599
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006600 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6601 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6602 // If heap poisoning is enabled, no need to unpoison
6603 // `temp1`, as we are comparing against null below.
6604 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6605 __ Bind(&do_put);
6606 } else {
6607 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006608 }
6609 }
6610
Artem Serov6c916792016-07-11 14:02:34 +01006611 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006612 if (kPoisonHeapReferences) {
6613 // Note that in the case where `value` is a null reference,
6614 // we do not enter this block, as a null reference does not
6615 // need poisoning.
6616 DCHECK_EQ(value_type, Primitive::kPrimNot);
6617 __ Mov(temp1, value);
6618 __ PoisonHeapReference(temp1);
6619 source = temp1;
6620 }
6621
6622 if (index.IsConstant()) {
6623 size_t offset =
6624 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6625 __ StoreToOffset(kStoreWord, source, array, offset);
6626 } else {
6627 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006628
6629 __ add(IP, array, ShifterOperand(data_offset));
6630 codegen_->StoreToShiftedRegOffset(value_type,
6631 Location::RegisterLocation(source),
6632 IP,
6633 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006634 }
6635
Roland Levillain3b359c72015-11-17 19:35:12 +00006636 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006637 codegen_->MaybeRecordImplicitNullCheck(instruction);
6638 }
6639
6640 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6641
6642 if (done.IsLinked()) {
6643 __ Bind(&done);
6644 }
6645
6646 if (slow_path != nullptr) {
6647 __ Bind(slow_path->GetExitLabel());
6648 }
6649
6650 break;
6651 }
6652
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006653 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006654 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006655 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006656 size_t offset =
6657 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006658 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006659 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006660 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006661 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006662 }
6663 break;
6664 }
6665
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006666 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006667 Location value = locations->InAt(2);
6668 DCHECK(value.IsFpuRegister());
6669 if (index.IsConstant()) {
6670 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006671 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006672 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006673 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006674 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6675 }
6676 break;
6677 }
6678
6679 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006680 Location value = locations->InAt(2);
6681 DCHECK(value.IsFpuRegisterPair());
6682 if (index.IsConstant()) {
6683 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006684 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006685 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006686 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006687 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6688 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006689
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006690 break;
6691 }
6692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006693 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006694 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006695 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006696 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006697
Roland Levillain80e67092016-01-08 16:04:55 +00006698 // Objects are handled in the switch.
6699 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006700 codegen_->MaybeRecordImplicitNullCheck(instruction);
6701 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006702}
6703
6704void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006705 LocationSummary* locations =
6706 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006707 locations->SetInAt(0, Location::RequiresRegister());
6708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006709}
6710
6711void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6712 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006713 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006714 Register obj = locations->InAt(0).AsRegister<Register>();
6715 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006716 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006717 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006718 // Mask out compression flag from String's array length.
6719 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006720 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006721 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006722}
6723
Artem Serov328429f2016-07-06 16:23:04 +01006724void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006725 LocationSummary* locations =
6726 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6727
6728 locations->SetInAt(0, Location::RequiresRegister());
6729 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6730 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6731}
6732
6733void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6734 LocationSummary* locations = instruction->GetLocations();
6735 Location out = locations->Out();
6736 Location first = locations->InAt(0);
6737 Location second = locations->InAt(1);
6738
Artem Serov328429f2016-07-06 16:23:04 +01006739 if (second.IsRegister()) {
6740 __ add(out.AsRegister<Register>(),
6741 first.AsRegister<Register>(),
6742 ShifterOperand(second.AsRegister<Register>()));
6743 } else {
6744 __ AddConstant(out.AsRegister<Register>(),
6745 first.AsRegister<Register>(),
6746 second.GetConstant()->AsIntConstant()->GetValue());
6747 }
6748}
6749
Artem Serove1811ed2017-04-27 16:50:47 +01006750void LocationsBuilderARM::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
6751 LOG(FATAL) << "Unreachable " << instruction->GetId();
6752}
6753
6754void InstructionCodeGeneratorARM::VisitIntermediateAddressIndex(
6755 HIntermediateAddressIndex* instruction) {
6756 LOG(FATAL) << "Unreachable " << instruction->GetId();
6757}
6758
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006759void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006760 RegisterSet caller_saves = RegisterSet::Empty();
6761 InvokeRuntimeCallingConvention calling_convention;
6762 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6763 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6764 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006765
6766 HInstruction* index = instruction->InputAt(0);
6767 HInstruction* length = instruction->InputAt(1);
6768 // If both index and length are constants we can statically check the bounds. But if at least one
6769 // of them is not encodable ArmEncodableConstantOrRegister will create
6770 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6771 // locations.
6772 bool both_const = index->IsConstant() && length->IsConstant();
6773 locations->SetInAt(0, both_const
6774 ? Location::ConstantLocation(index->AsConstant())
6775 : ArmEncodableConstantOrRegister(index, CMP));
6776 locations->SetInAt(1, both_const
6777 ? Location::ConstantLocation(length->AsConstant())
6778 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006779}
6780
6781void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6782 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006783 Location index_loc = locations->InAt(0);
6784 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006785
Artem Serov2dd053d2017-03-08 14:54:06 +00006786 if (length_loc.IsConstant()) {
6787 int32_t length = helpers::Int32ConstantFrom(length_loc);
6788 if (index_loc.IsConstant()) {
6789 // BCE will remove the bounds check if we are guaranteed to pass.
6790 int32_t index = helpers::Int32ConstantFrom(index_loc);
6791 if (index < 0 || index >= length) {
6792 SlowPathCodeARM* slow_path =
6793 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6794 codegen_->AddSlowPath(slow_path);
6795 __ b(slow_path->GetEntryLabel());
6796 } else {
6797 // Some optimization after BCE may have generated this, and we should not
6798 // generate a bounds check if it is a valid range.
6799 }
6800 return;
6801 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006802
Artem Serov2dd053d2017-03-08 14:54:06 +00006803 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6804 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6805 codegen_->AddSlowPath(slow_path);
6806 __ b(slow_path->GetEntryLabel(), HS);
6807 } else {
6808 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6809 if (index_loc.IsConstant()) {
6810 int32_t index = helpers::Int32ConstantFrom(index_loc);
6811 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6812 } else {
6813 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6814 }
6815 codegen_->AddSlowPath(slow_path);
6816 __ b(slow_path->GetEntryLabel(), LS);
6817 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006818}
6819
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006820void CodeGeneratorARM::MarkGCCard(Register temp,
6821 Register card,
6822 Register object,
6823 Register value,
6824 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006825 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006826 if (can_be_null) {
6827 __ CompareAndBranchIfZero(value, &is_null);
6828 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006829 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006830 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6831 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006832 if (can_be_null) {
6833 __ Bind(&is_null);
6834 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006835}
6836
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006837void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006838 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006839}
6840
6841void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006842 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6843}
6844
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006845void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006846 LocationSummary* locations =
6847 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006848 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006849}
6850
6851void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006852 HBasicBlock* block = instruction->GetBlock();
6853 if (block->GetLoopInformation() != nullptr) {
6854 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6855 // The back edge will generate the suspend check.
6856 return;
6857 }
6858 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6859 // The goto will generate the suspend check.
6860 return;
6861 }
6862 GenerateSuspendCheck(instruction, nullptr);
6863}
6864
6865void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6866 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006867 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006868 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6869 if (slow_path == nullptr) {
6870 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6871 instruction->SetSlowPath(slow_path);
6872 codegen_->AddSlowPath(slow_path);
6873 if (successor != nullptr) {
6874 DCHECK(successor->IsLoopHeader());
6875 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6876 }
6877 } else {
6878 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6879 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006880
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006881 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006882 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006883 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006884 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006885 __ Bind(slow_path->GetReturnLabel());
6886 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006887 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006888 __ b(slow_path->GetEntryLabel());
6889 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006890}
6891
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006892ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6893 return codegen_->GetAssembler();
6894}
6895
6896void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006897 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006898 Location source = move->GetSource();
6899 Location destination = move->GetDestination();
6900
6901 if (source.IsRegister()) {
6902 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006903 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006904 } else if (destination.IsFpuRegister()) {
6905 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006906 } else {
6907 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006908 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006909 SP, destination.GetStackIndex());
6910 }
6911 } else if (source.IsStackSlot()) {
6912 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006913 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006914 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006915 } else if (destination.IsFpuRegister()) {
6916 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006917 } else {
6918 DCHECK(destination.IsStackSlot());
6919 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6920 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6921 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006922 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006923 if (destination.IsRegister()) {
6924 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6925 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006926 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006927 } else {
6928 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006929 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6930 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006931 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006932 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006933 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6934 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006935 } else if (destination.IsRegisterPair()) {
6936 DCHECK(ExpectedPairLayout(destination));
6937 __ LoadFromOffset(
6938 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6939 } else {
6940 DCHECK(destination.IsFpuRegisterPair()) << destination;
6941 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6942 SP,
6943 source.GetStackIndex());
6944 }
6945 } else if (source.IsRegisterPair()) {
6946 if (destination.IsRegisterPair()) {
6947 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6948 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006949 } else if (destination.IsFpuRegisterPair()) {
6950 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6951 source.AsRegisterPairLow<Register>(),
6952 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006953 } else {
6954 DCHECK(destination.IsDoubleStackSlot()) << destination;
6955 DCHECK(ExpectedPairLayout(source));
6956 __ StoreToOffset(
6957 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6958 }
6959 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006960 if (destination.IsRegisterPair()) {
6961 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6962 destination.AsRegisterPairHigh<Register>(),
6963 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6964 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006965 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6966 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6967 } else {
6968 DCHECK(destination.IsDoubleStackSlot()) << destination;
6969 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6970 SP,
6971 destination.GetStackIndex());
6972 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006973 } else {
6974 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006975 HConstant* constant = source.GetConstant();
6976 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6977 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006978 if (destination.IsRegister()) {
6979 __ LoadImmediate(destination.AsRegister<Register>(), value);
6980 } else {
6981 DCHECK(destination.IsStackSlot());
6982 __ LoadImmediate(IP, value);
6983 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6984 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006985 } else if (constant->IsLongConstant()) {
6986 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006987 if (destination.IsRegisterPair()) {
6988 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6989 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006990 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006991 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006992 __ LoadImmediate(IP, Low32Bits(value));
6993 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6994 __ LoadImmediate(IP, High32Bits(value));
6995 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6996 }
6997 } else if (constant->IsDoubleConstant()) {
6998 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006999 if (destination.IsFpuRegisterPair()) {
7000 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00007001 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007002 DCHECK(destination.IsDoubleStackSlot()) << destination;
7003 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00007004 __ LoadImmediate(IP, Low32Bits(int_value));
7005 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
7006 __ LoadImmediate(IP, High32Bits(int_value));
7007 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
7008 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007009 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00007010 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007011 float value = constant->AsFloatConstant()->GetValue();
7012 if (destination.IsFpuRegister()) {
7013 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
7014 } else {
7015 DCHECK(destination.IsStackSlot());
7016 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
7017 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
7018 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01007019 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007020 }
7021}
7022
7023void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
7024 __ Mov(IP, reg);
7025 __ LoadFromOffset(kLoadWord, reg, SP, mem);
7026 __ StoreToOffset(kStoreWord, IP, SP, mem);
7027}
7028
7029void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
7030 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
7031 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7032 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
7033 SP, mem1 + stack_offset);
7034 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
7035 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
7036 SP, mem2 + stack_offset);
7037 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
7038}
7039
7040void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007041 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007042 Location source = move->GetSource();
7043 Location destination = move->GetDestination();
7044
7045 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007046 DCHECK_NE(source.AsRegister<Register>(), IP);
7047 DCHECK_NE(destination.AsRegister<Register>(), IP);
7048 __ Mov(IP, source.AsRegister<Register>());
7049 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
7050 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007051 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007052 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007053 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007054 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007055 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
7056 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007057 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007058 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007059 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007060 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007061 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007062 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007063 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007064 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007065 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
7066 destination.AsRegisterPairHigh<Register>(),
7067 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007068 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007069 Register low_reg = source.IsRegisterPair()
7070 ? source.AsRegisterPairLow<Register>()
7071 : destination.AsRegisterPairLow<Register>();
7072 int mem = source.IsRegisterPair()
7073 ? destination.GetStackIndex()
7074 : source.GetStackIndex();
7075 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007076 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007077 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007078 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007079 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007080 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
7081 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007082 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007083 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007084 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007085 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
7086 DRegister reg = source.IsFpuRegisterPair()
7087 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
7088 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
7089 int mem = source.IsFpuRegisterPair()
7090 ? destination.GetStackIndex()
7091 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007092 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007093 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007094 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007095 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
7096 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
7097 : destination.AsFpuRegister<SRegister>();
7098 int mem = source.IsFpuRegister()
7099 ? destination.GetStackIndex()
7100 : source.GetStackIndex();
7101
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007102 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007103 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007104 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007105 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007106 Exchange(source.GetStackIndex(), destination.GetStackIndex());
7107 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007108 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007109 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007110 }
7111}
7112
7113void ParallelMoveResolverARM::SpillScratch(int reg) {
7114 __ Push(static_cast<Register>(reg));
7115}
7116
7117void ParallelMoveResolverARM::RestoreScratch(int reg) {
7118 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007119}
7120
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007121HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
7122 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007123 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007124 case HLoadClass::LoadKind::kInvalid:
7125 LOG(FATAL) << "UNREACHABLE";
7126 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007127 case HLoadClass::LoadKind::kReferrersClass:
7128 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007129 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007130 case HLoadClass::LoadKind::kBssEntry:
7131 DCHECK(!Runtime::Current()->UseJitCompilation());
7132 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007133 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007134 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007135 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007136 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007137 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007138 break;
7139 }
7140 return desired_class_load_kind;
7141}
7142
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007143void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007144 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007145 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007146 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007147 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007148 cls,
7149 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007150 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007151 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007152 return;
7153 }
Vladimir Marko41559982017-01-06 14:04:23 +00007154 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007155
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007156 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7157 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007158 ? LocationSummary::kCallOnSlowPath
7159 : LocationSummary::kNoCall;
7160 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007161 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007162 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007163 }
7164
Vladimir Marko41559982017-01-06 14:04:23 +00007165 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007166 locations->SetInAt(0, Location::RequiresRegister());
7167 }
7168 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007169 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7170 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7171 // Rely on the type resolution or initialization and marking to save everything we need.
7172 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7173 // to the custom calling convention) or by marking, so we request a different temp.
7174 locations->AddTemp(Location::RequiresRegister());
7175 RegisterSet caller_saves = RegisterSet::Empty();
7176 InvokeRuntimeCallingConvention calling_convention;
7177 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7178 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7179 // that the the kPrimNot result register is the same as the first argument register.
7180 locations->SetCustomSlowPathCallerSaves(caller_saves);
7181 } else {
7182 // For non-Baker read barrier we have a temp-clobbering call.
7183 }
7184 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007185 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7186 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7187 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7188 !Runtime::Current()->UseJitCompilation())) {
7189 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
7190 }
7191 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007192}
7193
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007194// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7195// move.
7196void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007197 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007198 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007199 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007200 return;
7201 }
Vladimir Marko41559982017-01-06 14:04:23 +00007202 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01007203
Vladimir Marko41559982017-01-06 14:04:23 +00007204 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007205 Location out_loc = locations->Out();
7206 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007207
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007208 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7209 ? kWithoutReadBarrier
7210 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007211 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007212 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007213 case HLoadClass::LoadKind::kReferrersClass: {
7214 DCHECK(!cls->CanCallRuntime());
7215 DCHECK(!cls->MustGenerateClinitCheck());
7216 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7217 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007218 GenerateGcRootFieldLoad(cls,
7219 out_loc,
7220 current_method,
7221 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007222 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007223 break;
7224 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007225 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007226 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007227 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007228 CodeGeneratorARM::PcRelativePatchInfo* labels =
7229 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7230 __ BindTrackedLabel(&labels->movw_label);
7231 __ movw(out, /* placeholder */ 0u);
7232 __ BindTrackedLabel(&labels->movt_label);
7233 __ movt(out, /* placeholder */ 0u);
7234 __ BindTrackedLabel(&labels->add_pc_label);
7235 __ add(out, out, ShifterOperand(PC));
7236 break;
7237 }
7238 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007239 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007240 uint32_t address = dchecked_integral_cast<uint32_t>(
7241 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7242 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007243 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7244 break;
7245 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007246 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007247 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7248 ? locations->GetTemp(0).AsRegister<Register>()
7249 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007250 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007251 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007252 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007253 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007254 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007255 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007256 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007257 __ add(temp, temp, ShifterOperand(PC));
7258 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007259 generate_null_check = true;
7260 break;
7261 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007262 case HLoadClass::LoadKind::kJitTableAddress: {
7263 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7264 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007265 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007266 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007267 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007268 break;
7269 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007270 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007271 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007272 LOG(FATAL) << "UNREACHABLE";
7273 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007274 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007275
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007276 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7277 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01007278 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007279 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7280 codegen_->AddSlowPath(slow_path);
7281 if (generate_null_check) {
7282 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7283 }
7284 if (cls->MustGenerateClinitCheck()) {
7285 GenerateClassInitializationCheck(slow_path, out);
7286 } else {
7287 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007288 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007289 }
7290}
7291
7292void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
7293 LocationSummary* locations =
7294 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7295 locations->SetInAt(0, Location::RequiresRegister());
7296 if (check->HasUses()) {
7297 locations->SetOut(Location::SameAsFirstInput());
7298 }
7299}
7300
7301void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007302 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007303 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007304 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007305 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007306 GenerateClassInitializationCheck(slow_path,
7307 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007308}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007309
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007310void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01007311 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007312 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
7313 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
7314 __ b(slow_path->GetEntryLabel(), LT);
7315 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7316 // properly. Therefore, we do a memory fence.
7317 __ dmb(ISH);
7318 __ Bind(slow_path->GetExitLabel());
7319}
7320
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007321HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
7322 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007323 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007324 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007325 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01007326 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007327 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007328 case HLoadString::LoadKind::kJitTableAddress:
7329 DCHECK(Runtime::Current()->UseJitCompilation());
7330 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007331 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007332 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007333 break;
7334 }
7335 return desired_string_load_kind;
7336}
7337
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007338void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007339 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007340 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007341 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007342 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007343 locations->SetOut(Location::RegisterLocation(R0));
7344 } else {
7345 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007346 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7347 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007348 // Rely on the pResolveString and marking to save everything we need, including temps.
7349 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7350 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007351 locations->AddTemp(Location::RequiresRegister());
7352 RegisterSet caller_saves = RegisterSet::Empty();
7353 InvokeRuntimeCallingConvention calling_convention;
7354 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7355 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7356 // that the the kPrimNot result register is the same as the first argument register.
7357 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007358 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7359 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
7360 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007361 } else {
7362 // For non-Baker read barrier we have a temp-clobbering call.
7363 }
7364 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007365 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007366}
7367
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007368// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7369// move.
7370void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007371 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007372 Location out_loc = locations->Out();
7373 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007374 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00007375
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007376 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007377 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007378 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007379 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007380 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007381 __ BindTrackedLabel(&labels->movw_label);
7382 __ movw(out, /* placeholder */ 0u);
7383 __ BindTrackedLabel(&labels->movt_label);
7384 __ movt(out, /* placeholder */ 0u);
7385 __ BindTrackedLabel(&labels->add_pc_label);
7386 __ add(out, out, ShifterOperand(PC));
7387 return; // No dex cache slow path.
7388 }
7389 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007390 uint32_t address = dchecked_integral_cast<uint32_t>(
7391 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7392 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007393 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7394 return; // No dex cache slow path.
7395 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007396 case HLoadString::LoadKind::kBssEntry: {
7397 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007398 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7399 ? locations->GetTemp(0).AsRegister<Register>()
7400 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007401 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007402 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007403 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007404 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007405 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007406 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007407 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007408 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007409 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007410 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
7411 codegen_->AddSlowPath(slow_path);
7412 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7413 __ Bind(slow_path->GetExitLabel());
7414 return;
7415 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007416 case HLoadString::LoadKind::kJitTableAddress: {
7417 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007418 load->GetStringIndex(),
7419 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007420 // /* GcRoot<mirror::String> */ out = *out
7421 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7422 return;
7423 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007424 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007425 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007426 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007427
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007428 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007429 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007430 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007431 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007432 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007433 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7434 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007435}
7436
David Brazdilcb1c0552015-08-04 16:22:25 +01007437static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007438 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01007439}
7440
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007441void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
7442 LocationSummary* locations =
7443 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7444 locations->SetOut(Location::RequiresRegister());
7445}
7446
7447void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007448 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01007449 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7450}
7451
7452void LocationsBuilderARM::VisitClearException(HClearException* clear) {
7453 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7454}
7455
7456void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007457 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01007458 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007459}
7460
7461void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
7462 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007464 InvokeRuntimeCallingConvention calling_convention;
7465 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7466}
7467
7468void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007469 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007470 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007471}
7472
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007473// Temp is used for read barrier.
7474static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7475 if (kEmitCompilerReadBarrier &&
7476 (kUseBakerReadBarrier ||
7477 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7478 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7479 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7480 return 1;
7481 }
7482 return 0;
7483}
7484
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007485// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007486// interface pointer, one for loading the current interface.
7487// The other checks have one temp for loading the object's class.
7488static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7489 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7490 return 3;
7491 }
7492 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00007493}
7494
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007495void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007496 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00007497 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007498 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00007499 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007500 case TypeCheckKind::kExactCheck:
7501 case TypeCheckKind::kAbstractClassCheck:
7502 case TypeCheckKind::kClassHierarchyCheck:
7503 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007504 call_kind =
7505 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007506 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007507 break;
7508 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007509 case TypeCheckKind::kUnresolvedCheck:
7510 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007511 call_kind = LocationSummary::kCallOnSlowPath;
7512 break;
7513 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007514
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007516 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007517 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007518 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007519 locations->SetInAt(0, Location::RequiresRegister());
7520 locations->SetInAt(1, Location::RequiresRegister());
7521 // The "out" register is used as a temporary, so it overlaps with the inputs.
7522 // Note that TypeCheckSlowPathARM uses this register too.
7523 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007524 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007525 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7526 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7527 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007528}
7529
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007530void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007531 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007532 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007533 Location obj_loc = locations->InAt(0);
7534 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007535 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007536 Location out_loc = locations->Out();
7537 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007538 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7539 DCHECK_LE(num_temps, 1u);
7540 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007541 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007542 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7543 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7544 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007545 Label done;
7546 Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007547 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007548
7549 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007550 // avoid null check if we know obj is not null.
7551 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007552 DCHECK_NE(out, obj);
7553 __ LoadImmediate(out, 0);
7554 __ CompareAndBranchIfZero(obj, final_label);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007555 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007556
Roland Levillainc9285912015-12-18 10:38:42 +00007557 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007558 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007559 // /* HeapReference<Class> */ out = obj->klass_
7560 GenerateReferenceLoadTwoRegisters(instruction,
7561 out_loc,
7562 obj_loc,
7563 class_offset,
7564 maybe_temp_loc,
7565 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007566 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007567 __ cmp(out, ShifterOperand(cls));
7568 // We speculatively set the result to false without changing the condition
7569 // flags, which allows us to avoid some branching later.
7570 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7571
7572 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7573 // we check that the output is in a low register, so that a 16-bit MOV
7574 // encoding can be used.
7575 if (ArmAssembler::IsLowRegister(out)) {
7576 __ it(EQ);
7577 __ mov(out, ShifterOperand(1), EQ);
7578 } else {
7579 __ b(final_label, NE);
7580 __ LoadImmediate(out, 1);
7581 }
7582
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007583 break;
7584 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007585
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007586 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007587 // /* HeapReference<Class> */ out = obj->klass_
7588 GenerateReferenceLoadTwoRegisters(instruction,
7589 out_loc,
7590 obj_loc,
7591 class_offset,
7592 maybe_temp_loc,
7593 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007594 // If the class is abstract, we eagerly fetch the super class of the
7595 // object to avoid doing a comparison we know will fail.
7596 Label loop;
7597 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007598 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007599 GenerateReferenceLoadOneRegister(instruction,
7600 out_loc,
7601 super_offset,
7602 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007603 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007604 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007605 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007606 __ cmp(out, ShifterOperand(cls));
7607 __ b(&loop, NE);
7608 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007609 break;
7610 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007611
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007612 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007613 // /* HeapReference<Class> */ out = obj->klass_
7614 GenerateReferenceLoadTwoRegisters(instruction,
7615 out_loc,
7616 obj_loc,
7617 class_offset,
7618 maybe_temp_loc,
7619 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007620 // Walk over the class hierarchy to find a match.
7621 Label loop, success;
7622 __ Bind(&loop);
7623 __ cmp(out, ShifterOperand(cls));
7624 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007625 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007626 GenerateReferenceLoadOneRegister(instruction,
7627 out_loc,
7628 super_offset,
7629 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007630 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007631 // This is essentially a null check, but it sets the condition flags to the
7632 // proper value for the code that follows the loop, i.e. not `EQ`.
7633 __ cmp(out, ShifterOperand(1));
7634 __ b(&loop, HS);
7635
7636 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7637 // we check that the output is in a low register, so that a 16-bit MOV
7638 // encoding can be used.
7639 if (ArmAssembler::IsLowRegister(out)) {
7640 // If `out` is null, we use it for the result, and the condition flags
7641 // have already been set to `NE`, so the IT block that comes afterwards
7642 // (and which handles the successful case) turns into a NOP (instead of
7643 // overwriting `out`).
7644 __ Bind(&success);
7645 // There is only one branch to the `success` label (which is bound to this
7646 // IT block), and it has the same condition, `EQ`, so in that case the MOV
7647 // is executed.
7648 __ it(EQ);
7649 __ mov(out, ShifterOperand(1), EQ);
7650 } else {
7651 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007652 __ b(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007653 __ Bind(&success);
7654 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007655 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007656
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007657 break;
7658 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007659
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007660 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007661 // /* HeapReference<Class> */ out = obj->klass_
7662 GenerateReferenceLoadTwoRegisters(instruction,
7663 out_loc,
7664 obj_loc,
7665 class_offset,
7666 maybe_temp_loc,
7667 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007668 // Do an exact check.
7669 Label exact_check;
7670 __ cmp(out, ShifterOperand(cls));
7671 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007672 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007673 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007674 GenerateReferenceLoadOneRegister(instruction,
7675 out_loc,
7676 component_offset,
7677 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007678 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007679 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007680 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007681 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7682 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007683 __ cmp(out, ShifterOperand(0));
7684 // We speculatively set the result to false without changing the condition
7685 // flags, which allows us to avoid some branching later.
7686 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7687
7688 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7689 // we check that the output is in a low register, so that a 16-bit MOV
7690 // encoding can be used.
7691 if (ArmAssembler::IsLowRegister(out)) {
7692 __ Bind(&exact_check);
7693 __ it(EQ);
7694 __ mov(out, ShifterOperand(1), EQ);
7695 } else {
7696 __ b(final_label, NE);
7697 __ Bind(&exact_check);
7698 __ LoadImmediate(out, 1);
7699 }
7700
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007701 break;
7702 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007703
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007704 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007705 // No read barrier since the slow path will retry upon failure.
7706 // /* HeapReference<Class> */ out = obj->klass_
7707 GenerateReferenceLoadTwoRegisters(instruction,
7708 out_loc,
7709 obj_loc,
7710 class_offset,
7711 maybe_temp_loc,
7712 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007713 __ cmp(out, ShifterOperand(cls));
7714 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007715 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7716 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007717 codegen_->AddSlowPath(slow_path);
7718 __ b(slow_path->GetEntryLabel(), NE);
7719 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007720 break;
7721 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007722
Calin Juravle98893e12015-10-02 21:05:03 +01007723 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007724 case TypeCheckKind::kInterfaceCheck: {
7725 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007726 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007727 // cases.
7728 //
7729 // We cannot directly call the InstanceofNonTrivial runtime
7730 // entry point without resorting to a type checking slow path
7731 // here (i.e. by calling InvokeRuntime directly), as it would
7732 // require to assign fixed registers for the inputs of this
7733 // HInstanceOf instruction (following the runtime calling
7734 // convention), which might be cluttered by the potential first
7735 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007736 //
7737 // TODO: Introduce a new runtime entry point taking the object
7738 // to test (instead of its class) as argument, and let it deal
7739 // with the read barrier issues. This will let us refactor this
7740 // case of the `switch` code as it was previously (with a direct
7741 // call to the runtime not using a type checking slow path).
7742 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007743 DCHECK(locations->OnlyCallsOnSlowPath());
7744 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7745 /* is_fatal */ false);
7746 codegen_->AddSlowPath(slow_path);
7747 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007748 break;
7749 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007750 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007751
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007752 if (done.IsLinked()) {
7753 __ Bind(&done);
7754 }
7755
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007756 if (slow_path != nullptr) {
7757 __ Bind(slow_path->GetExitLabel());
7758 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007759}
7760
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007761void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007762 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7763 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7764
Roland Levillain3b359c72015-11-17 19:35:12 +00007765 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7766 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007767 case TypeCheckKind::kExactCheck:
7768 case TypeCheckKind::kAbstractClassCheck:
7769 case TypeCheckKind::kClassHierarchyCheck:
7770 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007771 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7772 LocationSummary::kCallOnSlowPath :
7773 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007774 break;
7775 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007776 case TypeCheckKind::kUnresolvedCheck:
7777 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007778 call_kind = LocationSummary::kCallOnSlowPath;
7779 break;
7780 }
7781
Roland Levillain3b359c72015-11-17 19:35:12 +00007782 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7783 locations->SetInAt(0, Location::RequiresRegister());
7784 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007785 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007786}
7787
7788void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007789 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007790 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007791 Location obj_loc = locations->InAt(0);
7792 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007793 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007794 Location temp_loc = locations->GetTemp(0);
7795 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007796 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7797 DCHECK_LE(num_temps, 3u);
7798 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7799 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7800 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7801 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7802 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7803 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7804 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7805 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7806 const uint32_t object_array_data_offset =
7807 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007808
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007809 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7810 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7811 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007812 bool is_type_check_slow_path_fatal = false;
7813 if (!kEmitCompilerReadBarrier) {
7814 is_type_check_slow_path_fatal =
7815 (type_check_kind == TypeCheckKind::kExactCheck ||
7816 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7817 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7818 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7819 !instruction->CanThrowIntoCatchBlock();
7820 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007821 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007822 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7823 is_type_check_slow_path_fatal);
7824 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007825
7826 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007827 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007828 // Avoid null check if we know obj is not null.
7829 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007830 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007831 }
7832
Roland Levillain3b359c72015-11-17 19:35:12 +00007833 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007834 case TypeCheckKind::kExactCheck:
7835 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007836 // /* HeapReference<Class> */ temp = obj->klass_
7837 GenerateReferenceLoadTwoRegisters(instruction,
7838 temp_loc,
7839 obj_loc,
7840 class_offset,
7841 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007842 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007843
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007844 __ cmp(temp, ShifterOperand(cls));
7845 // Jump to slow path for throwing the exception or doing a
7846 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007847 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007848 break;
7849 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007850
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007851 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007852 // /* HeapReference<Class> */ temp = obj->klass_
7853 GenerateReferenceLoadTwoRegisters(instruction,
7854 temp_loc,
7855 obj_loc,
7856 class_offset,
7857 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007858 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007859
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007860 // If the class is abstract, we eagerly fetch the super class of the
7861 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007862 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007863 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007864 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007865 GenerateReferenceLoadOneRegister(instruction,
7866 temp_loc,
7867 super_offset,
7868 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007869 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007870
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007871 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7872 // exception.
7873 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007874
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007875 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007876 __ cmp(temp, ShifterOperand(cls));
7877 __ b(&loop, NE);
7878 break;
7879 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007880
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007881 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007882 // /* HeapReference<Class> */ temp = obj->klass_
7883 GenerateReferenceLoadTwoRegisters(instruction,
7884 temp_loc,
7885 obj_loc,
7886 class_offset,
7887 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007888 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007889
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007890 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007891 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007892 __ Bind(&loop);
7893 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007894 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007895
Roland Levillain3b359c72015-11-17 19:35:12 +00007896 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007897 GenerateReferenceLoadOneRegister(instruction,
7898 temp_loc,
7899 super_offset,
7900 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007901 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007902
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007903 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7904 // exception.
7905 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7906 // Otherwise, jump to the beginning of the loop.
7907 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007908 break;
7909 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007910
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007911 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007912 // /* HeapReference<Class> */ temp = obj->klass_
7913 GenerateReferenceLoadTwoRegisters(instruction,
7914 temp_loc,
7915 obj_loc,
7916 class_offset,
7917 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007918 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007919
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007920 // Do an exact check.
7921 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007922 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007923
7924 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007925 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007926 GenerateReferenceLoadOneRegister(instruction,
7927 temp_loc,
7928 component_offset,
7929 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007930 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007931 // If the component type is null, jump to the slow path to throw the exception.
7932 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7933 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7934 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007935 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007936 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007937 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007938 break;
7939 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007940
Calin Juravle98893e12015-10-02 21:05:03 +01007941 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007942 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007943 // We cannot directly call the CheckCast runtime entry point
7944 // without resorting to a type checking slow path here (i.e. by
7945 // calling InvokeRuntime directly), as it would require to
7946 // assign fixed registers for the inputs of this HInstanceOf
7947 // instruction (following the runtime calling convention), which
7948 // might be cluttered by the potential first read barrier
7949 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007950
Roland Levillain3b359c72015-11-17 19:35:12 +00007951 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007952 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007953
7954 case TypeCheckKind::kInterfaceCheck: {
7955 // Avoid read barriers to improve performance of the fast path. We can not get false
7956 // positives by doing this.
7957 // /* HeapReference<Class> */ temp = obj->klass_
7958 GenerateReferenceLoadTwoRegisters(instruction,
7959 temp_loc,
7960 obj_loc,
7961 class_offset,
7962 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007963 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007964
7965 // /* HeapReference<Class> */ temp = temp->iftable_
7966 GenerateReferenceLoadTwoRegisters(instruction,
7967 temp_loc,
7968 temp_loc,
7969 iftable_offset,
7970 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007971 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007972 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007973 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007974 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007975 Label start_loop;
7976 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007977 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7978 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007979 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7980 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007981 // Go to next interface.
7982 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7983 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7984 maybe_temp2_loc.AsRegister<Register>(),
7985 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007986 // Compare the classes and continue the loop if they do not match.
7987 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7988 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007989 break;
7990 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007991 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007992
7993 if (done.IsLinked()) {
7994 __ Bind(&done);
7995 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007996
Roland Levillain3b359c72015-11-17 19:35:12 +00007997 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007998}
7999
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008000void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
8001 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008003 InvokeRuntimeCallingConvention calling_convention;
8004 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8005}
8006
8007void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01008008 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8009 instruction,
8010 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008011 if (instruction->IsEnter()) {
8012 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8013 } else {
8014 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8015 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008016}
8017
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008018void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
8019void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
8020void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008021
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008022void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008023 LocationSummary* locations =
8024 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8025 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8026 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008027 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008028 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008029 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00008030 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008031}
8032
8033void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
8034 HandleBitwiseOperation(instruction);
8035}
8036
8037void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
8038 HandleBitwiseOperation(instruction);
8039}
8040
8041void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
8042 HandleBitwiseOperation(instruction);
8043}
8044
Artem Serov7fc63502016-02-09 17:15:29 +00008045
8046void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8047 LocationSummary* locations =
8048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8049 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8050 || instruction->GetResultType() == Primitive::kPrimLong);
8051
8052 locations->SetInAt(0, Location::RequiresRegister());
8053 locations->SetInAt(1, Location::RequiresRegister());
8054 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8055}
8056
8057void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8058 LocationSummary* locations = instruction->GetLocations();
8059 Location first = locations->InAt(0);
8060 Location second = locations->InAt(1);
8061 Location out = locations->Out();
8062
8063 if (instruction->GetResultType() == Primitive::kPrimInt) {
8064 Register first_reg = first.AsRegister<Register>();
8065 ShifterOperand second_reg(second.AsRegister<Register>());
8066 Register out_reg = out.AsRegister<Register>();
8067
8068 switch (instruction->GetOpKind()) {
8069 case HInstruction::kAnd:
8070 __ bic(out_reg, first_reg, second_reg);
8071 break;
8072 case HInstruction::kOr:
8073 __ orn(out_reg, first_reg, second_reg);
8074 break;
8075 // There is no EON on arm.
8076 case HInstruction::kXor:
8077 default:
8078 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8079 UNREACHABLE();
8080 }
8081 return;
8082
8083 } else {
8084 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8085 Register first_low = first.AsRegisterPairLow<Register>();
8086 Register first_high = first.AsRegisterPairHigh<Register>();
8087 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
8088 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
8089 Register out_low = out.AsRegisterPairLow<Register>();
8090 Register out_high = out.AsRegisterPairHigh<Register>();
8091
8092 switch (instruction->GetOpKind()) {
8093 case HInstruction::kAnd:
8094 __ bic(out_low, first_low, second_low);
8095 __ bic(out_high, first_high, second_high);
8096 break;
8097 case HInstruction::kOr:
8098 __ orn(out_low, first_low, second_low);
8099 __ orn(out_high, first_high, second_high);
8100 break;
8101 // There is no EON on arm.
8102 case HInstruction::kXor:
8103 default:
8104 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8105 UNREACHABLE();
8106 }
8107 }
8108}
8109
Anton Kirilov74234da2017-01-13 14:42:47 +00008110void LocationsBuilderARM::VisitDataProcWithShifterOp(
8111 HDataProcWithShifterOp* instruction) {
8112 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
8113 instruction->GetType() == Primitive::kPrimLong);
8114 LocationSummary* locations =
8115 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8116 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
8117 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8118
8119 locations->SetInAt(0, Location::RequiresRegister());
8120 locations->SetInAt(1, Location::RequiresRegister());
8121 locations->SetOut(Location::RequiresRegister(),
8122 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8123}
8124
8125void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
8126 HDataProcWithShifterOp* instruction) {
8127 const LocationSummary* const locations = instruction->GetLocations();
8128 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8129 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8130 const Location left = locations->InAt(0);
8131 const Location right = locations->InAt(1);
8132 const Location out = locations->Out();
8133
8134 if (instruction->GetType() == Primitive::kPrimInt) {
8135 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
8136
8137 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
8138 ? right.AsRegisterPairLow<Register>()
8139 : right.AsRegister<Register>();
8140
8141 GenerateDataProcInstruction(kind,
8142 out.AsRegister<Register>(),
8143 left.AsRegister<Register>(),
8144 ShifterOperand(second,
8145 ShiftFromOpKind(op_kind),
8146 instruction->GetShiftAmount()),
8147 codegen_);
8148 } else {
8149 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
8150
8151 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8152 const Register second = right.AsRegister<Register>();
8153
8154 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
8155 GenerateDataProc(kind,
8156 out,
8157 left,
8158 ShifterOperand(second),
8159 ShifterOperand(second, ASR, 31),
8160 codegen_);
8161 } else {
8162 GenerateLongDataProc(instruction, codegen_);
8163 }
8164 }
8165}
8166
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008167void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
8168 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8169 if (value == 0xffffffffu) {
8170 if (out != first) {
8171 __ mov(out, ShifterOperand(first));
8172 }
8173 return;
8174 }
8175 if (value == 0u) {
8176 __ mov(out, ShifterOperand(0));
8177 return;
8178 }
8179 ShifterOperand so;
8180 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
8181 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008182 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008183 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008184 } else {
8185 DCHECK(IsPowerOfTwo(value + 1));
8186 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008187 }
8188}
8189
8190void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
8191 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8192 if (value == 0u) {
8193 if (out != first) {
8194 __ mov(out, ShifterOperand(first));
8195 }
8196 return;
8197 }
8198 if (value == 0xffffffffu) {
8199 __ mvn(out, ShifterOperand(0));
8200 return;
8201 }
8202 ShifterOperand so;
8203 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
8204 __ orr(out, first, so);
8205 } else {
8206 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
8207 __ orn(out, first, ShifterOperand(~value));
8208 }
8209}
8210
8211void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
8212 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8213 if (value == 0u) {
8214 if (out != first) {
8215 __ mov(out, ShifterOperand(first));
8216 }
8217 return;
8218 }
8219 __ eor(out, first, ShifterOperand(value));
8220}
8221
Vladimir Marko59751a72016-08-05 14:37:27 +01008222void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
8223 Location first,
8224 uint64_t value) {
8225 Register out_low = out.AsRegisterPairLow<Register>();
8226 Register out_high = out.AsRegisterPairHigh<Register>();
8227 Register first_low = first.AsRegisterPairLow<Register>();
8228 Register first_high = first.AsRegisterPairHigh<Register>();
8229 uint32_t value_low = Low32Bits(value);
8230 uint32_t value_high = High32Bits(value);
8231 if (value_low == 0u) {
8232 if (out_low != first_low) {
8233 __ mov(out_low, ShifterOperand(first_low));
8234 }
8235 __ AddConstant(out_high, first_high, value_high);
8236 return;
8237 }
8238 __ AddConstantSetFlags(out_low, first_low, value_low);
8239 ShifterOperand so;
8240 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
8241 __ adc(out_high, first_high, so);
8242 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
8243 __ sbc(out_high, first_high, so);
8244 } else {
8245 LOG(FATAL) << "Unexpected constant " << value_high;
8246 UNREACHABLE();
8247 }
8248}
8249
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008250void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
8251 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008252 Location first = locations->InAt(0);
8253 Location second = locations->InAt(1);
8254 Location out = locations->Out();
8255
8256 if (second.IsConstant()) {
8257 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8258 uint32_t value_low = Low32Bits(value);
8259 if (instruction->GetResultType() == Primitive::kPrimInt) {
8260 Register first_reg = first.AsRegister<Register>();
8261 Register out_reg = out.AsRegister<Register>();
8262 if (instruction->IsAnd()) {
8263 GenerateAndConst(out_reg, first_reg, value_low);
8264 } else if (instruction->IsOr()) {
8265 GenerateOrrConst(out_reg, first_reg, value_low);
8266 } else {
8267 DCHECK(instruction->IsXor());
8268 GenerateEorConst(out_reg, first_reg, value_low);
8269 }
8270 } else {
8271 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8272 uint32_t value_high = High32Bits(value);
8273 Register first_low = first.AsRegisterPairLow<Register>();
8274 Register first_high = first.AsRegisterPairHigh<Register>();
8275 Register out_low = out.AsRegisterPairLow<Register>();
8276 Register out_high = out.AsRegisterPairHigh<Register>();
8277 if (instruction->IsAnd()) {
8278 GenerateAndConst(out_low, first_low, value_low);
8279 GenerateAndConst(out_high, first_high, value_high);
8280 } else if (instruction->IsOr()) {
8281 GenerateOrrConst(out_low, first_low, value_low);
8282 GenerateOrrConst(out_high, first_high, value_high);
8283 } else {
8284 DCHECK(instruction->IsXor());
8285 GenerateEorConst(out_low, first_low, value_low);
8286 GenerateEorConst(out_high, first_high, value_high);
8287 }
8288 }
8289 return;
8290 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008291
8292 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008293 Register first_reg = first.AsRegister<Register>();
8294 ShifterOperand second_reg(second.AsRegister<Register>());
8295 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008296 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008297 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008298 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008299 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008300 } else {
8301 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008302 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008303 }
8304 } else {
8305 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008306 Register first_low = first.AsRegisterPairLow<Register>();
8307 Register first_high = first.AsRegisterPairHigh<Register>();
8308 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
8309 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
8310 Register out_low = out.AsRegisterPairLow<Register>();
8311 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008312 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008313 __ and_(out_low, first_low, second_low);
8314 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008315 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008316 __ orr(out_low, first_low, second_low);
8317 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008318 } else {
8319 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008320 __ eor(out_low, first_low, second_low);
8321 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008322 }
8323 }
8324}
8325
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008326void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
8327 HInstruction* instruction,
8328 Location out,
8329 uint32_t offset,
8330 Location maybe_temp,
8331 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008332 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008333 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08008334 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008335 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00008336 if (kUseBakerReadBarrier) {
8337 // Load with fast path based Baker's read barrier.
8338 // /* HeapReference<Object> */ out = *(out + offset)
8339 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008340 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00008341 } else {
8342 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008343 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00008344 // in the following move operation, as we will need it for the
8345 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008346 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008347 // /* HeapReference<Object> */ out = *(out + offset)
8348 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008349 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00008350 }
8351 } else {
8352 // Plain load with no read barrier.
8353 // /* HeapReference<Object> */ out = *(out + offset)
8354 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8355 __ MaybeUnpoisonHeapReference(out_reg);
8356 }
8357}
8358
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008359void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
8360 HInstruction* instruction,
8361 Location out,
8362 Location obj,
8363 uint32_t offset,
8364 Location maybe_temp,
8365 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008366 Register out_reg = out.AsRegister<Register>();
8367 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008368 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08008369 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008370 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008371 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00008372 // Load with fast path based Baker's read barrier.
8373 // /* HeapReference<Object> */ out = *(obj + offset)
8374 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008375 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00008376 } else {
8377 // Load with slow path based read barrier.
8378 // /* HeapReference<Object> */ out = *(obj + offset)
8379 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8380 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8381 }
8382 } else {
8383 // Plain load with no read barrier.
8384 // /* HeapReference<Object> */ out = *(obj + offset)
8385 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8386 __ MaybeUnpoisonHeapReference(out_reg);
8387 }
8388}
8389
8390void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
8391 Location root,
8392 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008393 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008394 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008395 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008396 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008397 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008398 if (kUseBakerReadBarrier) {
8399 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008400 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008401 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8402 !Runtime::Current()->UseJitCompilation()) {
8403 // Note that we do not actually check the value of `GetIsGcMarking()`
8404 // to decide whether to mark the loaded GC root or not. Instead, we
8405 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8406 // barrier mark introspection entrypoint. If `temp` is null, it means
8407 // that `GetIsGcMarking()` is false, and vice versa.
8408 //
8409 // We use link-time generated thunks for the slow path. That thunk
8410 // checks the reference and jumps to the entrypoint if needed.
8411 //
8412 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8413 // lr = &return_address;
8414 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8415 // if (temp != nullptr) {
8416 // goto gc_root_thunk<root_reg>(lr)
8417 // }
8418 // return_address:
Roland Levillainc9285912015-12-18 10:38:42 +00008419
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008420 CheckLastTempIsBakerCcEntrypointRegister(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008421 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008422 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008423 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008424 Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008425
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008426 // entrypoint_reg =
8427 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8428 DCHECK_EQ(IP, 12);
8429 const int32_t entry_point_offset =
8430 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8431 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008432
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008433 Label return_address;
8434 __ AdrCode(LR, &return_address);
8435 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008436 // Currently the offset is always within range. If that changes,
8437 // we shall have to split the load the same way as for fields.
8438 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008439 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8440 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8441 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008442 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8443 EmitPlaceholderBne(codegen_, bne_label);
8444 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008445 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8446 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8447 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008448 } else {
8449 // Note that we do not actually check the value of
8450 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8451 // root or not. Instead, we load into `temp` the read barrier
8452 // mark entry point corresponding to register `root`. If `temp`
8453 // is null, it means that `GetIsGcMarking()` is false, and vice
8454 // versa.
8455 //
8456 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8457 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8458 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8459 // // Slow path.
8460 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8461 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008462
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008463 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8464 Location temp = Location::RegisterLocation(LR);
8465 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
8466 instruction, root, /* entrypoint */ temp);
8467 codegen_->AddSlowPath(slow_path);
8468
8469 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8470 const int32_t entry_point_offset =
8471 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8472 // Loading the entrypoint does not require a load acquire since it is only changed when
8473 // threads are suspended or running a checkpoint.
8474 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
8475
8476 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8477 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8478 static_assert(
8479 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8480 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8481 "have different sizes.");
8482 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8483 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8484 "have different sizes.");
8485
8486 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8487 // checking GetIsGcMarking.
8488 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
8489 __ Bind(slow_path->GetExitLabel());
8490 }
Roland Levillainc9285912015-12-18 10:38:42 +00008491 } else {
8492 // GC root loaded through a slow path for read barriers other
8493 // than Baker's.
8494 // /* GcRoot<mirror::Object>* */ root = obj + offset
8495 __ AddConstant(root_reg, obj, offset);
8496 // /* mirror::Object* */ root = root->Read()
8497 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8498 }
8499 } else {
8500 // Plain GC root load with no read barrier.
8501 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8502 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8503 // Note that GC roots are not affected by heap poisoning, thus we
8504 // do not have to unpoison `root_reg` here.
8505 }
8506}
8507
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008508void CodeGeneratorARM::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8509 DCHECK(kEmitCompilerReadBarrier);
8510 DCHECK(kUseBakerReadBarrier);
8511 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8512 if (!Runtime::Current()->UseJitCompilation()) {
8513 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
8514 }
8515 }
8516}
8517
Roland Levillainc9285912015-12-18 10:38:42 +00008518void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8519 Location ref,
8520 Register obj,
8521 uint32_t offset,
8522 Location temp,
8523 bool needs_null_check) {
8524 DCHECK(kEmitCompilerReadBarrier);
8525 DCHECK(kUseBakerReadBarrier);
8526
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008527 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8528 !Runtime::Current()->UseJitCompilation()) {
8529 // Note that we do not actually check the value of `GetIsGcMarking()`
8530 // to decide whether to mark the loaded reference or not. Instead, we
8531 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8532 // barrier mark introspection entrypoint. If `temp` is null, it means
8533 // that `GetIsGcMarking()` is false, and vice versa.
8534 //
8535 // We use link-time generated thunks for the slow path. That thunk checks
8536 // the holder and jumps to the entrypoint if needed. If the holder is not
8537 // gray, it creates a fake dependency and returns to the LDR instruction.
8538 //
8539 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8540 // lr = &gray_return_address;
8541 // if (temp != nullptr) {
8542 // goto field_thunk<holder_reg, base_reg>(lr)
8543 // }
8544 // not_gray_return_address:
8545 // // Original reference load. If the offset is too large to fit
8546 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008547 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008548 // gray_return_address:
8549
8550 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008551 Register ref_reg = ref.AsRegister<Register>();
8552 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008553 Register base = obj;
8554 if (offset >= kReferenceLoadMinFarOffset) {
8555 base = temp.AsRegister<Register>();
8556 DCHECK_NE(base, kBakerCcEntrypointRegister);
8557 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8558 __ AddConstant(base, obj, offset & ~(kReferenceLoadMinFarOffset - 1u));
8559 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008560 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8561 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8562 // increase the overall code size when taking the generated thunks into account.
8563 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008564 }
8565 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8566 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008567 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(base, obj, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008568 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8569
8570 // entrypoint_reg =
8571 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8572 DCHECK_EQ(IP, 12);
8573 const int32_t entry_point_offset =
8574 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8575 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8576
8577 Label return_address;
8578 __ AdrCode(LR, &return_address);
8579 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008580 EmitPlaceholderBne(this, bne_label);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008581 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008582 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8583 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8584 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008585 __ LoadFromOffset(kLoadWord, ref_reg, base, offset);
8586 if (needs_null_check) {
8587 MaybeRecordImplicitNullCheck(instruction);
8588 }
8589 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8590 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008591 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8592 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8593 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008594 return;
8595 }
8596
Roland Levillainc9285912015-12-18 10:38:42 +00008597 // /* HeapReference<Object> */ ref = *(obj + offset)
8598 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01008599 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00008600 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008601 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008602}
8603
8604void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8605 Location ref,
8606 Register obj,
8607 uint32_t data_offset,
8608 Location index,
8609 Location temp,
8610 bool needs_null_check) {
8611 DCHECK(kEmitCompilerReadBarrier);
8612 DCHECK(kUseBakerReadBarrier);
8613
Roland Levillainbfea3352016-06-23 13:48:47 +01008614 static_assert(
8615 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8616 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008617 ScaleFactor scale_factor = TIMES_4;
8618
8619 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8620 !Runtime::Current()->UseJitCompilation()) {
8621 // Note that we do not actually check the value of `GetIsGcMarking()`
8622 // to decide whether to mark the loaded reference or not. Instead, we
8623 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8624 // barrier mark introspection entrypoint. If `temp` is null, it means
8625 // that `GetIsGcMarking()` is false, and vice versa.
8626 //
8627 // We use link-time generated thunks for the slow path. That thunk checks
8628 // the holder and jumps to the entrypoint if needed. If the holder is not
8629 // gray, it creates a fake dependency and returns to the LDR instruction.
8630 //
8631 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8632 // lr = &gray_return_address;
8633 // if (temp != nullptr) {
8634 // goto field_thunk<holder_reg, base_reg>(lr)
8635 // }
8636 // not_gray_return_address:
8637 // // Original reference load. If the offset is too large to fit
8638 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008639 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008640 // gray_return_address:
8641
8642 DCHECK(index.IsValid());
8643 Register index_reg = index.AsRegister<Register>();
8644 Register ref_reg = ref.AsRegister<Register>();
8645 Register data_reg = temp.AsRegister<Register>();
8646 DCHECK_NE(data_reg, kBakerCcEntrypointRegister);
8647
8648 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8649 uint32_t custom_data =
8650 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg);
8651 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8652
8653 // entrypoint_reg =
8654 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8655 DCHECK_EQ(IP, 12);
8656 const int32_t entry_point_offset =
8657 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8658 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8659 __ AddConstant(data_reg, obj, data_offset);
8660
8661 Label return_address;
8662 __ AdrCode(LR, &return_address);
8663 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008664 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008665 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8666 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008667 __ ldr(ref_reg, Address(data_reg, index_reg, LSL, scale_factor));
8668 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8669 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8670 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008671 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8672 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008673 return;
8674 }
8675
Roland Levillainc9285912015-12-18 10:38:42 +00008676 // /* HeapReference<Object> */ ref =
8677 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8678 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008679 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008680}
8681
8682void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8683 Location ref,
8684 Register obj,
8685 uint32_t offset,
8686 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01008687 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00008688 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008689 bool needs_null_check) {
Roland Levillainc9285912015-12-18 10:38:42 +00008690 DCHECK(kEmitCompilerReadBarrier);
8691 DCHECK(kUseBakerReadBarrier);
8692
Roland Levillain54f869e2017-03-06 13:54:11 +00008693 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8694 // whether we need to enter the slow path to mark the reference.
8695 // Then, in the slow path, check the gray bit in the lock word of
8696 // the reference's holder (`obj`) to decide whether to mark `ref` or
8697 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00008698 //
Roland Levillainba650a42017-03-06 13:52:32 +00008699 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008700 // instead, we load into `temp2` the read barrier mark entry point
8701 // corresponding to register `ref`. If `temp2` is null, it means
8702 // that `GetIsGcMarking()` is false, and vice versa.
8703 //
8704 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8705 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8706 // // Slow path.
8707 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8708 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8709 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8710 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8711 // if (is_gray) {
8712 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8713 // }
8714 // } else {
8715 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8716 // }
8717
8718 Register temp_reg = temp.AsRegister<Register>();
8719
8720 // Slow path marking the object `ref` when the GC is marking. The
8721 // entrypoint will already be loaded in `temp2`.
8722 Location temp2 = Location::RegisterLocation(LR);
8723 SlowPathCodeARM* slow_path =
8724 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8725 instruction,
8726 ref,
8727 obj,
8728 offset,
8729 index,
8730 scale_factor,
8731 needs_null_check,
8732 temp_reg,
8733 /* entrypoint */ temp2);
8734 AddSlowPath(slow_path);
8735
8736 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8737 const int32_t entry_point_offset =
8738 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8739 // Loading the entrypoint does not require a load acquire since it is only changed when
8740 // threads are suspended or running a checkpoint.
8741 __ LoadFromOffset(kLoadWord, temp2.AsRegister<Register>(), TR, entry_point_offset);
8742 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8743 // checking GetIsGcMarking.
8744 __ CompareAndBranchIfNonZero(temp2.AsRegister<Register>(), slow_path->GetEntryLabel());
8745 // Fast path: the GC is not marking: just load the reference.
8746 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8747 __ Bind(slow_path->GetExitLabel());
8748}
8749
8750void CodeGeneratorARM::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8751 Location ref,
8752 Register obj,
8753 Location field_offset,
8754 Location temp,
8755 bool needs_null_check,
8756 Register temp2) {
8757 DCHECK(kEmitCompilerReadBarrier);
8758 DCHECK(kUseBakerReadBarrier);
8759
8760 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8761 // whether we need to enter the slow path to update the reference
8762 // field within `obj`. Then, in the slow path, check the gray bit
8763 // in the lock word of the reference's holder (`obj`) to decide
8764 // whether to mark `ref` and update the field or not.
8765 //
8766 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008767 // instead, we load into `temp3` the read barrier mark entry point
8768 // corresponding to register `ref`. If `temp3` is null, it means
8769 // that `GetIsGcMarking()` is false, and vice versa.
8770 //
8771 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008772 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8773 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008774 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8775 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8776 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8777 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8778 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008779 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008780 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008781 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008782 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008783 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008784
Roland Levillain35345a52017-02-27 14:32:08 +00008785 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008786
Roland Levillainff487002017-03-07 16:50:01 +00008787 // Slow path updating the object reference at address `obj +
8788 // field_offset` when the GC is marking. The entrypoint will already
8789 // be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008790 Location temp3 = Location::RegisterLocation(LR);
Roland Levillainff487002017-03-07 16:50:01 +00008791 SlowPathCodeARM* slow_path =
8792 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8793 instruction,
8794 ref,
8795 obj,
8796 /* offset */ 0u,
8797 /* index */ field_offset,
8798 /* scale_factor */ ScaleFactor::TIMES_1,
8799 needs_null_check,
8800 temp_reg,
8801 temp2,
8802 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008803 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008804
Roland Levillainba650a42017-03-06 13:52:32 +00008805 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8806 const int32_t entry_point_offset =
8807 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8808 // Loading the entrypoint does not require a load acquire since it is only changed when
8809 // threads are suspended or running a checkpoint.
8810 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008811 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8812 // checking GetIsGcMarking.
8813 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008814 // Fast path: the GC is not marking: nothing to do (the field is
8815 // up-to-date, and we don't need to load the reference).
Roland Levillainba650a42017-03-06 13:52:32 +00008816 __ Bind(slow_path->GetExitLabel());
8817}
Roland Levillain35345a52017-02-27 14:32:08 +00008818
Roland Levillainba650a42017-03-06 13:52:32 +00008819void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8820 Location ref,
8821 Register obj,
8822 uint32_t offset,
8823 Location index,
8824 ScaleFactor scale_factor,
8825 bool needs_null_check) {
8826 Register ref_reg = ref.AsRegister<Register>();
8827
Roland Levillainc9285912015-12-18 10:38:42 +00008828 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008829 // Load types involving an "index": ArrayGet,
8830 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8831 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008832 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008833 if (index.IsConstant()) {
8834 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008835 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008836 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8837 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008838 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008839 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8840 // intrinsics, which use a register pair as index ("long
8841 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008842 Register index_reg = index.IsRegisterPair()
8843 ? index.AsRegisterPairLow<Register>()
8844 : index.AsRegister<Register>();
8845 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008846 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8847 }
8848 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008849 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008850 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8851 }
8852
Roland Levillainba650a42017-03-06 13:52:32 +00008853 if (needs_null_check) {
8854 MaybeRecordImplicitNullCheck(instruction);
8855 }
8856
Roland Levillainc9285912015-12-18 10:38:42 +00008857 // Object* ref = ref_addr->AsMirrorPtr()
8858 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008859}
8860
8861void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8862 Location out,
8863 Location ref,
8864 Location obj,
8865 uint32_t offset,
8866 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008867 DCHECK(kEmitCompilerReadBarrier);
8868
Roland Levillainc9285912015-12-18 10:38:42 +00008869 // Insert a slow path based read barrier *after* the reference load.
8870 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008871 // If heap poisoning is enabled, the unpoisoning of the loaded
8872 // reference will be carried out by the runtime within the slow
8873 // path.
8874 //
8875 // Note that `ref` currently does not get unpoisoned (when heap
8876 // poisoning is enabled), which is alright as the `ref` argument is
8877 // not used by the artReadBarrierSlow entry point.
8878 //
8879 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008880 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008881 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8882 AddSlowPath(slow_path);
8883
Roland Levillain3b359c72015-11-17 19:35:12 +00008884 __ b(slow_path->GetEntryLabel());
8885 __ Bind(slow_path->GetExitLabel());
8886}
8887
Roland Levillainc9285912015-12-18 10:38:42 +00008888void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8889 Location out,
8890 Location ref,
8891 Location obj,
8892 uint32_t offset,
8893 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008894 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008895 // Baker's read barriers shall be handled by the fast path
8896 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8897 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008898 // If heap poisoning is enabled, unpoisoning will be taken care of
8899 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008900 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008901 } else if (kPoisonHeapReferences) {
8902 __ UnpoisonHeapReference(out.AsRegister<Register>());
8903 }
8904}
8905
Roland Levillainc9285912015-12-18 10:38:42 +00008906void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8907 Location out,
8908 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008909 DCHECK(kEmitCompilerReadBarrier);
8910
Roland Levillainc9285912015-12-18 10:38:42 +00008911 // Insert a slow path based read barrier *after* the GC root load.
8912 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008913 // Note that GC roots are not affected by heap poisoning, so we do
8914 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008915 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008916 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8917 AddSlowPath(slow_path);
8918
Roland Levillain3b359c72015-11-17 19:35:12 +00008919 __ b(slow_path->GetEntryLabel());
8920 __ Bind(slow_path->GetExitLabel());
8921}
8922
Vladimir Markodc151b22015-10-15 18:02:30 +01008923HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8924 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008925 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008926 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008927}
8928
Vladimir Markob4536b72015-11-24 13:45:23 +00008929Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8930 Register temp) {
8931 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8932 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8933 if (!invoke->GetLocations()->Intrinsified()) {
8934 return location.AsRegister<Register>();
8935 }
8936 // For intrinsics we allow any location, so it may be on the stack.
8937 if (!location.IsRegister()) {
8938 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8939 return temp;
8940 }
8941 // For register locations, check if the register was saved. If so, get it from the stack.
8942 // Note: There is a chance that the register was saved but not overwritten, so we could
8943 // save one load. However, since this is just an intrinsic slow path we prefer this
8944 // simple and more robust approach rather that trying to determine if that's the case.
8945 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Markod254f5c2017-06-02 15:18:36 +00008946 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
8947 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008948 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8949 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8950 return temp;
8951 }
8952 return location.AsRegister<Register>();
8953}
8954
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008955void CodeGeneratorARM::GenerateStaticOrDirectCall(
8956 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00008957 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8958 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008959 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8960 uint32_t offset =
8961 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008962 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008963 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008964 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008965 }
Vladimir Marko58155012015-08-19 12:49:41 +00008966 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008967 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008968 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008969 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8970 DCHECK(GetCompilerOptions().IsBootImage());
8971 Register temp_reg = temp.AsRegister<Register>();
8972 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8973 __ BindTrackedLabel(&labels->movw_label);
8974 __ movw(temp_reg, /* placeholder */ 0u);
8975 __ BindTrackedLabel(&labels->movt_label);
8976 __ movt(temp_reg, /* placeholder */ 0u);
8977 __ BindTrackedLabel(&labels->add_pc_label);
8978 __ add(temp_reg, temp_reg, ShifterOperand(PC));
8979 break;
8980 }
Vladimir Marko58155012015-08-19 12:49:41 +00008981 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8982 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8983 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00008984 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8985 HArmDexCacheArraysBase* base =
8986 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8987 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
8988 temp.AsRegister<Register>());
8989 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8990 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
8991 break;
8992 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008993 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8994 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8995 return; // No code pointer retrieval; the runtime performs the call directly.
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01008996 }
Vladimir Marko58155012015-08-19 12:49:41 +00008997 }
8998
8999 switch (invoke->GetCodePtrLocation()) {
9000 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
9001 __ bl(GetFrameEntryLabel());
9002 break;
Vladimir Marko58155012015-08-19 12:49:41 +00009003 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9004 // LR = callee_method->entry_point_from_quick_compiled_code_
9005 __ LoadFromOffset(
9006 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07009007 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00009008 // LR()
9009 __ blx(LR);
9010 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08009011 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009012 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08009013
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08009014 DCHECK(!IsLeafMethod());
9015}
9016
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009017void CodeGeneratorARM::GenerateVirtualCall(
9018 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009019 Register temp = temp_location.AsRegister<Register>();
9020 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9021 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00009022
9023 // Use the calling convention instead of the location of the receiver, as
9024 // intrinsics may have put the receiver in a different register. In the intrinsics
9025 // slow path, the arguments have been moved to the right place, so here we are
9026 // guaranteed that the receiver is the first register of the calling convention.
9027 InvokeDexCallingConvention calling_convention;
9028 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009029 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00009030 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00009031 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009032 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00009033 // Instead of simply (possibly) unpoisoning `temp` here, we should
9034 // emit a read barrier for the previous class reference load.
9035 // However this is not required in practice, as this is an
9036 // intermediate/temporary reference and because the current
9037 // concurrent copying collector keeps the from-space memory
9038 // intact/accessible until the end of the marking phase (the
9039 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009040 __ MaybeUnpoisonHeapReference(temp);
9041 // temp = temp->GetMethodAt(method_offset);
9042 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07009043 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009044 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
9045 // LR = temp->GetEntryPoint();
9046 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
9047 // LR();
9048 __ blx(LR);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009049 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009050}
9051
Vladimir Marko65979462017-05-19 17:25:12 +01009052CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeMethodPatch(
9053 MethodReference target_method) {
9054 return NewPcRelativePatch(*target_method.dex_file,
9055 target_method.dex_method_index,
9056 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009057}
9058
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01009059CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08009060 const DexFile& dex_file, dex::TypeIndex type_index) {
9061 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01009062}
9063
Vladimir Marko1998cd02017-01-13 13:02:58 +00009064CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
9065 const DexFile& dex_file, dex::TypeIndex type_index) {
9066 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9067}
9068
Vladimir Marko65979462017-05-19 17:25:12 +01009069CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
9070 const DexFile& dex_file, dex::StringIndex string_index) {
9071 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9072}
9073
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009074CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
9075 const DexFile& dex_file, uint32_t element_offset) {
9076 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
9077}
9078
9079CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
9080 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9081 patches->emplace_back(dex_file, offset_or_index);
9082 return &patches->back();
9083}
9084
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009085Label* CodeGeneratorARM::NewBakerReadBarrierPatch(uint32_t custom_data) {
9086 baker_read_barrier_patches_.emplace_back(custom_data);
9087 return &baker_read_barrier_patches_.back().label;
9088}
9089
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009090Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009091 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009092}
9093
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009094Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009095 dex::StringIndex string_index,
9096 Handle<mirror::String> handle) {
9097 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
9098 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009099 return jit_string_patches_.GetOrCreate(
9100 StringReference(&dex_file, string_index),
9101 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
9102}
9103
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009104Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
9105 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009106 Handle<mirror::Class> handle) {
9107 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
9108 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009109 return jit_class_patches_.GetOrCreate(
9110 TypeReference(&dex_file, type_index),
9111 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
9112}
9113
Vladimir Markoaad75c62016-10-03 08:46:48 +00009114template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
9115inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
9116 const ArenaDeque<PcRelativePatchInfo>& infos,
9117 ArenaVector<LinkerPatch>* linker_patches) {
9118 for (const PcRelativePatchInfo& info : infos) {
9119 const DexFile& dex_file = info.target_dex_file;
9120 size_t offset_or_index = info.offset_or_index;
9121 DCHECK(info.add_pc_label.IsBound());
9122 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
9123 // Add MOVW patch.
9124 DCHECK(info.movw_label.IsBound());
9125 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
9126 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9127 // Add MOVT patch.
9128 DCHECK(info.movt_label.IsBound());
9129 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
9130 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9131 }
9132}
9133
Vladimir Marko58155012015-08-19 12:49:41 +00009134void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
9135 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00009136 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00009137 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009138 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00009139 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009140 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009141 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009142 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00009143 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00009144 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
9145 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009146 if (GetCompilerOptions().IsBootImage()) {
9147 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00009148 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00009149 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
9150 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00009151 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
9152 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009153 } else {
9154 DCHECK(pc_relative_method_patches_.empty());
9155 DCHECK(pc_relative_type_patches_.empty());
9156 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
9157 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009158 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009159 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
9160 linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009161 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
9162 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.Position(),
9163 info.custom_data));
9164 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009165 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009166}
9167
9168Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
9169 return map->GetOrCreate(
9170 value,
9171 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00009172}
9173
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03009174void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9175 LocationSummary* locations =
9176 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
9177 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9178 Location::RequiresRegister());
9179 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9180 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9182}
9183
9184void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9185 LocationSummary* locations = instr->GetLocations();
9186 Register res = locations->Out().AsRegister<Register>();
9187 Register accumulator =
9188 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
9189 Register mul_left =
9190 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
9191 Register mul_right =
9192 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
9193
9194 if (instr->GetOpKind() == HInstruction::kAdd) {
9195 __ mla(res, mul_left, mul_right, accumulator);
9196 } else {
9197 __ mls(res, mul_left, mul_right, accumulator);
9198 }
9199}
9200
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01009201void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00009202 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00009203 LOG(FATAL) << "Unreachable";
9204}
9205
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01009206void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00009207 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00009208 LOG(FATAL) << "Unreachable";
9209}
9210
Mark Mendellfe57faa2015-09-18 09:26:15 -04009211// Simple implementation of packed switch - generate cascaded compare/jumps.
9212void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9213 LocationSummary* locations =
9214 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9215 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009216 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009217 codegen_->GetAssembler()->IsThumb()) {
9218 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9219 if (switch_instr->GetStartValue() != 0) {
9220 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9221 }
9222 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04009223}
9224
9225void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9226 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009227 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04009228 LocationSummary* locations = switch_instr->GetLocations();
9229 Register value_reg = locations->InAt(0).AsRegister<Register>();
9230 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9231
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009232 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009233 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009234 Register temp_reg = IP;
9235 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9236 // the immediate, because IP is used as the destination register. For the other
9237 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9238 // and they can be encoded in the instruction without making use of IP register.
9239 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
9240
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009241 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009242 // Jump to successors[0] if value == lower_bound.
9243 __ b(codegen_->GetLabelOf(successors[0]), EQ);
9244 int32_t last_index = 0;
9245 for (; num_entries - last_index > 2; last_index += 2) {
9246 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
9247 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9248 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
9249 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9250 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
9251 }
9252 if (num_entries - last_index == 2) {
9253 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00009254 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009255 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009256 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04009257
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009258 // And the default for any other value.
9259 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9260 __ b(codegen_->GetLabelOf(default_block));
9261 }
9262 } else {
9263 // Create a table lookup.
9264 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
9265
9266 // Materialize a pointer to the switch table
9267 std::vector<Label*> labels(num_entries);
9268 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9269 for (uint32_t i = 0; i < num_entries; i++) {
9270 labels[i] = codegen_->GetLabelOf(successors[i]);
9271 }
9272 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
9273
9274 // Remove the bias.
9275 Register key_reg;
9276 if (lower_bound != 0) {
9277 key_reg = locations->GetTemp(1).AsRegister<Register>();
9278 __ AddConstant(key_reg, value_reg, -lower_bound);
9279 } else {
9280 key_reg = value_reg;
9281 }
9282
9283 // Check whether the value is in the table, jump to default block if not.
9284 __ CmpConstant(key_reg, num_entries - 1);
9285 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
9286
9287 // Load the displacement from the table.
9288 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
9289
9290 // Dispatch is a direct add to the PC (for Thumb2).
9291 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04009292 }
9293}
9294
Vladimir Markob4536b72015-11-24 13:45:23 +00009295void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9296 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
9297 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00009298}
9299
9300void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9301 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009302 CodeGeneratorARM::PcRelativePatchInfo* labels =
9303 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00009304 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009305 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00009306 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009307 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00009308 __ BindTrackedLabel(&labels->add_pc_label);
9309 __ add(base_reg, base_reg, ShifterOperand(PC));
9310}
9311
Andreas Gampe85b62f22015-09-09 13:15:38 -07009312void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
9313 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00009314 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07009315 return;
9316 }
9317
9318 DCHECK_NE(type, Primitive::kPrimVoid);
9319
9320 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
9321 if (return_loc.Equals(trg)) {
9322 return;
9323 }
9324
9325 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9326 // with the last branch.
9327 if (type == Primitive::kPrimLong) {
9328 HParallelMove parallel_move(GetGraph()->GetArena());
9329 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
9330 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
9331 GetMoveResolver()->EmitNativeCode(&parallel_move);
9332 } else if (type == Primitive::kPrimDouble) {
9333 HParallelMove parallel_move(GetGraph()->GetArena());
9334 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
9335 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
9336 GetMoveResolver()->EmitNativeCode(&parallel_move);
9337 } else {
9338 // Let the parallel move resolver take care of all of this.
9339 HParallelMove parallel_move(GetGraph()->GetArena());
9340 parallel_move.AddMove(return_loc, trg, type, nullptr);
9341 GetMoveResolver()->EmitNativeCode(&parallel_move);
9342 }
9343}
9344
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009345void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
9346 LocationSummary* locations =
9347 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9348 locations->SetInAt(0, Location::RequiresRegister());
9349 locations->SetOut(Location::RequiresRegister());
9350}
9351
9352void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
9353 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009354 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009355 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009356 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009357 __ LoadFromOffset(kLoadWord,
9358 locations->Out().AsRegister<Register>(),
9359 locations->InAt(0).AsRegister<Register>(),
9360 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009361 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009362 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009363 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009364 __ LoadFromOffset(kLoadWord,
9365 locations->Out().AsRegister<Register>(),
9366 locations->InAt(0).AsRegister<Register>(),
9367 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9368 __ LoadFromOffset(kLoadWord,
9369 locations->Out().AsRegister<Register>(),
9370 locations->Out().AsRegister<Register>(),
9371 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009372 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009373}
9374
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009375static void PatchJitRootUse(uint8_t* code,
9376 const uint8_t* roots_data,
9377 Literal* literal,
9378 uint64_t index_in_table) {
9379 DCHECK(literal->GetLabel()->IsBound());
9380 uint32_t literal_offset = literal->GetLabel()->Position();
9381 uintptr_t address =
9382 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9383 uint8_t* data = code + literal_offset;
9384 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9385}
9386
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009387void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9388 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009389 const StringReference& string_reference = entry.first;
9390 Literal* table_entry_literal = entry.second;
9391 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009392 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009393 uint64_t index_in_table = it->second;
9394 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009395 }
9396 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009397 const TypeReference& type_reference = entry.first;
9398 Literal* table_entry_literal = entry.second;
9399 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009400 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009401 uint64_t index_in_table = it->second;
9402 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009403 }
9404}
9405
Roland Levillain4d027112015-07-01 15:41:14 +01009406#undef __
9407#undef QUICK_ENTRY_POINT
9408
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009409} // namespace arm
9410} // namespace art