blob: 48190e0eb3179f7827f50e1996de2b82b3b32c59 [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
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain7cbd27f2016-08-11 23:53:33 +010062// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Artem Serovf4d6aee2016-07-11 10:41:45 +010066static constexpr int kRegListThreshold = 4;
67
68void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
69 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
70 size_t orig_offset = stack_offset;
71
72 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
73 for (uint32_t i : LowToHighBits(core_spills)) {
74 // If the register holds an object, update the stack mask.
75 if (locations->RegisterContainsObject(i)) {
76 locations->SetStackBit(stack_offset / kVRegSize);
77 }
78 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
79 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
80 saved_core_stack_offsets_[i] = stack_offset;
81 stack_offset += kArmWordSize;
82 }
83
84 int reg_num = POPCOUNT(core_spills);
85 if (reg_num != 0) {
86 if (reg_num > kRegListThreshold) {
87 __ StoreList(RegList(core_spills), orig_offset);
88 } else {
89 stack_offset = orig_offset;
90 for (uint32_t i : LowToHighBits(core_spills)) {
91 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
92 }
93 }
94 }
95
96 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
97 for (size_t i : LowToHighBits(fp_spills)) {
98 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
99 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
100 saved_fpu_stack_offsets_[i] = stack_offset;
101 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
102 }
103}
104
105void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
106 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
107 size_t orig_offset = stack_offset;
108
109 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
110 for (uint32_t i : LowToHighBits(core_spills)) {
111 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
112 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
113 stack_offset += kArmWordSize;
114 }
115
116 int reg_num = POPCOUNT(core_spills);
117 if (reg_num != 0) {
118 if (reg_num > kRegListThreshold) {
119 __ LoadList(RegList(core_spills), orig_offset);
120 } else {
121 stack_offset = orig_offset;
122 for (uint32_t i : LowToHighBits(core_spills)) {
123 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
124 }
125 }
126 }
127
128 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
129 for (size_t i : LowToHighBits(fp_spills)) {
130 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
131 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
132 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
133 }
134}
135
136class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100137 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100138 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100139
Alexandre Rames67555f72014-11-18 10:55:16 +0000140 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100141 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100142 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000143 if (instruction_->CanThrowIntoCatchBlock()) {
144 // Live registers will be restored in the catch block if caught.
145 SaveLiveRegisters(codegen, instruction_->GetLocations());
146 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100147 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
148 instruction_,
149 instruction_->GetDexPc(),
150 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000151 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100152 }
153
Alexandre Rames8158f282015-08-07 10:26:17 +0100154 bool IsFatal() const OVERRIDE { return true; }
155
Alexandre Rames9931f312015-06-19 14:47:01 +0100156 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
157
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100158 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100159 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
160};
161
Artem Serovf4d6aee2016-07-11 10:41:45 +0100162class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000163 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100164 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000165
Alexandre Rames67555f72014-11-18 10:55:16 +0000166 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000167 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
168 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000169 if (instruction_->CanThrowIntoCatchBlock()) {
170 // Live registers will be restored in the catch block if caught.
171 SaveLiveRegisters(codegen, instruction_->GetLocations());
172 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100173 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000174 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000175 }
176
Alexandre Rames8158f282015-08-07 10:26:17 +0100177 bool IsFatal() const OVERRIDE { return true; }
178
Alexandre Rames9931f312015-06-19 14:47:01 +0100179 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
180
Calin Juravled0d48522014-11-04 16:40:20 +0000181 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000182 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
183};
184
Artem Serovf4d6aee2016-07-11 10:41:45 +0100185class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000187 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100188 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189
Alexandre Rames67555f72014-11-18 10:55:16 +0000190 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100191 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100193 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000194 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100195 if (successor_ == nullptr) {
196 __ b(GetReturnLabel());
197 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100198 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100199 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000200 }
201
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 Label* GetReturnLabel() {
203 DCHECK(successor_ == nullptr);
204 return &return_label_;
205 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000206
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100207 HBasicBlock* GetSuccessor() const {
208 return successor_;
209 }
210
Alexandre Rames9931f312015-06-19 14:47:01 +0100211 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
212
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000213 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100214 // If not null, the block to branch to after the suspend check.
215 HBasicBlock* const successor_;
216
217 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000218 Label return_label_;
219
220 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
221};
222
Artem Serovf4d6aee2016-07-11 10:41:45 +0100223class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100224 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100225 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100226 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100227
Alexandre Rames67555f72014-11-18 10:55:16 +0000228 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100229 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100230 LocationSummary* locations = instruction_->GetLocations();
231
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100232 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000233 if (instruction_->CanThrowIntoCatchBlock()) {
234 // Live registers will be restored in the catch block if caught.
235 SaveLiveRegisters(codegen, instruction_->GetLocations());
236 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000237 // We're moving two locations to locations that could overlap, so we need a parallel
238 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100239 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000240 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100241 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000242 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100243 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100244 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100245 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
246 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100247 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
248 ? kQuickThrowStringBounds
249 : kQuickThrowArrayBounds;
250 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100251 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000252 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100253 }
254
Alexandre Rames8158f282015-08-07 10:26:17 +0100255 bool IsFatal() const OVERRIDE { return true; }
256
Alexandre Rames9931f312015-06-19 14:47:01 +0100257 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
258
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100259 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100260 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
261};
262
Artem Serovf4d6aee2016-07-11 10:41:45 +0100263class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100264 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265 LoadClassSlowPathARM(HLoadClass* cls,
266 HInstruction* at,
267 uint32_t dex_pc,
268 bool do_clinit)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100269 : SlowPathCodeARM(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
271 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100272
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 LocationSummary* locations = at_->GetLocations();
275
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100276 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
277 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000278 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100279
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100280 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000281 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100282 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
283 : kQuickInitializeType;
284 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000285 if (do_clinit_) {
286 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
287 } else {
288 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
289 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000290
291 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 Location out = locations->Out();
293 if (out.IsValid()) {
294 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000295 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
296 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000297 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100298 __ b(GetExitLabel());
299 }
300
Alexandre Rames9931f312015-06-19 14:47:01 +0100301 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
302
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100303 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000304 // The class this slow path will load.
305 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100306
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000307 // The instruction where this slow path is happening.
308 // (Might be the load class or an initialization check).
309 HInstruction* const at_;
310
311 // The dex PC of `at_`.
312 const uint32_t dex_pc_;
313
314 // Whether to initialize the class.
315 const bool do_clinit_;
316
317 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100318};
319
Artem Serovf4d6aee2016-07-11 10:41:45 +0100320class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100323 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Alexandre Rames67555f72014-11-18 10:55:16 +0000325 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100327 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
328 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 DCHECK(instruction_->IsCheckCast()
330 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
332 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
333 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335 if (!is_fatal_) {
336 SaveLiveRegisters(codegen, locations);
337 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338
339 // We're moving two locations to locations that could overlap, so we need a parallel
340 // move resolver.
341 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000342 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100343 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000344 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100345 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100347 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
348 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000349
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000350 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100351 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100352 instruction_,
353 instruction_->GetDexPc(),
354 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000355 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700356 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000357 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
358 } else {
359 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100360 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000361 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000364 if (!is_fatal_) {
365 RestoreLiveRegisters(codegen, locations);
366 __ b(GetExitLabel());
367 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000368 }
369
Alexandre Rames9931f312015-06-19 14:47:01 +0100370 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
371
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000372 bool IsFatal() const OVERRIDE { return is_fatal_; }
373
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000374 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000375 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000376
377 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
378};
379
Artem Serovf4d6aee2016-07-11 10:41:45 +0100380class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 public:
Aart Bik42249c32016-01-07 15:33:50 -0800382 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100383 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800386 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700387 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100388 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000389 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 }
391
Alexandre Rames9931f312015-06-19 14:47:01 +0100392 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
393
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
396};
397
Artem Serovf4d6aee2016-07-11 10:41:45 +0100398class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100399 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100400 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100401
402 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
403 LocationSummary* locations = instruction_->GetLocations();
404 __ Bind(GetEntryLabel());
405 SaveLiveRegisters(codegen, locations);
406
407 InvokeRuntimeCallingConvention calling_convention;
408 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
409 parallel_move.AddMove(
410 locations->InAt(0),
411 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
412 Primitive::kPrimNot,
413 nullptr);
414 parallel_move.AddMove(
415 locations->InAt(1),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
417 Primitive::kPrimInt,
418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(2),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
422 Primitive::kPrimNot,
423 nullptr);
424 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
425
426 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100427 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000428 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100429 RestoreLiveRegisters(codegen, locations);
430 __ b(GetExitLabel());
431 }
432
433 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
434
435 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100436 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
437};
438
Roland Levillainc9285912015-12-18 10:38:42 +0000439// Slow path marking an object during a read barrier.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100440class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000441 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100442 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100443 : SlowPathCodeARM(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000444 DCHECK(kEmitCompilerReadBarrier);
445 }
446
447 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
448
449 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
450 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100451 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000452 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100453 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000454 DCHECK(instruction_->IsInstanceFieldGet() ||
455 instruction_->IsStaticFieldGet() ||
456 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100457 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000458 instruction_->IsLoadClass() ||
459 instruction_->IsLoadString() ||
460 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100461 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100462 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
463 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000464 << "Unexpected instruction in read barrier marking slow path: "
465 << instruction_->DebugName();
466
467 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100468 // No need to save live registers; it's taken care of by the
469 // entrypoint. Also, there is no need to update the stack mask,
470 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000471 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100472 DCHECK_NE(reg, SP);
473 DCHECK_NE(reg, LR);
474 DCHECK_NE(reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100475 // IP is used internally by the ReadBarrierMarkRegX entry point
476 // as a temporary, it cannot be the entry point's input/output.
477 DCHECK_NE(reg, IP);
Roland Levillain02b75802016-07-13 11:54:35 +0100478 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
479 // "Compact" slow path, saving two moves.
480 //
481 // Instead of using the standard runtime calling convention (input
482 // and output in R0):
483 //
484 // R0 <- obj
485 // R0 <- ReadBarrierMark(R0)
486 // obj <- R0
487 //
488 // we just use rX (the register holding `obj`) as input and output
489 // of a dedicated entrypoint:
490 //
491 // rX <- ReadBarrierMarkRegX(rX)
492 //
493 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700494 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100495 // This runtime call does not require a stack map.
496 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000497 __ b(GetExitLabel());
498 }
499
500 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000501 const Location obj_;
502
503 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
504};
505
Roland Levillain3b359c72015-11-17 19:35:12 +0000506// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100507class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000508 public:
509 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
510 Location out,
511 Location ref,
512 Location obj,
513 uint32_t offset,
514 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100515 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000516 out_(out),
517 ref_(ref),
518 obj_(obj),
519 offset_(offset),
520 index_(index) {
521 DCHECK(kEmitCompilerReadBarrier);
522 // If `obj` is equal to `out` or `ref`, it means the initial object
523 // has been overwritten by (or after) the heap object reference load
524 // to be instrumented, e.g.:
525 //
526 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000527 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000528 //
529 // In that case, we have lost the information about the original
530 // object, and the emitted read barrier cannot work properly.
531 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
532 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
533 }
534
535 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
536 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
537 LocationSummary* locations = instruction_->GetLocations();
538 Register reg_out = out_.AsRegister<Register>();
539 DCHECK(locations->CanCall());
540 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100541 DCHECK(instruction_->IsInstanceFieldGet() ||
542 instruction_->IsStaticFieldGet() ||
543 instruction_->IsArrayGet() ||
544 instruction_->IsInstanceOf() ||
545 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100546 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000547 << "Unexpected instruction in read barrier for heap reference slow path: "
548 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000549
550 __ Bind(GetEntryLabel());
551 SaveLiveRegisters(codegen, locations);
552
553 // We may have to change the index's value, but as `index_` is a
554 // constant member (like other "inputs" of this slow path),
555 // introduce a copy of it, `index`.
556 Location index = index_;
557 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100558 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000559 if (instruction_->IsArrayGet()) {
560 // Compute the actual memory offset and store it in `index`.
561 Register index_reg = index_.AsRegister<Register>();
562 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
563 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
564 // We are about to change the value of `index_reg` (see the
565 // calls to art::arm::Thumb2Assembler::Lsl and
566 // art::arm::Thumb2Assembler::AddConstant below), but it has
567 // not been saved by the previous call to
568 // art::SlowPathCode::SaveLiveRegisters, as it is a
569 // callee-save register --
570 // art::SlowPathCode::SaveLiveRegisters does not consider
571 // callee-save registers, as it has been designed with the
572 // assumption that callee-save registers are supposed to be
573 // handled by the called function. So, as a callee-save
574 // register, `index_reg` _would_ eventually be saved onto
575 // the stack, but it would be too late: we would have
576 // changed its value earlier. Therefore, we manually save
577 // it here into another freely available register,
578 // `free_reg`, chosen of course among the caller-save
579 // registers (as a callee-save `free_reg` register would
580 // exhibit the same problem).
581 //
582 // Note we could have requested a temporary register from
583 // the register allocator instead; but we prefer not to, as
584 // this is a slow path, and we know we can find a
585 // caller-save register that is available.
586 Register free_reg = FindAvailableCallerSaveRegister(codegen);
587 __ Mov(free_reg, index_reg);
588 index_reg = free_reg;
589 index = Location::RegisterLocation(index_reg);
590 } else {
591 // The initial register stored in `index_` has already been
592 // saved in the call to art::SlowPathCode::SaveLiveRegisters
593 // (as it is not a callee-save register), so we can freely
594 // use it.
595 }
596 // Shifting the index value contained in `index_reg` by the scale
597 // factor (2) cannot overflow in practice, as the runtime is
598 // unable to allocate object arrays with a size larger than
599 // 2^26 - 1 (that is, 2^28 - 4 bytes).
600 __ Lsl(index_reg, index_reg, TIMES_4);
601 static_assert(
602 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
603 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
604 __ AddConstant(index_reg, index_reg, offset_);
605 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100606 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
607 // intrinsics, `index_` is not shifted by a scale factor of 2
608 // (as in the case of ArrayGet), as it is actually an offset
609 // to an object field within an object.
610 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000611 DCHECK(instruction_->GetLocations()->Intrinsified());
612 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
613 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
614 << instruction_->AsInvoke()->GetIntrinsic();
615 DCHECK_EQ(offset_, 0U);
616 DCHECK(index_.IsRegisterPair());
617 // UnsafeGet's offset location is a register pair, the low
618 // part contains the correct offset.
619 index = index_.ToLow();
620 }
621 }
622
623 // We're moving two or three locations to locations that could
624 // overlap, so we need a parallel move resolver.
625 InvokeRuntimeCallingConvention calling_convention;
626 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
627 parallel_move.AddMove(ref_,
628 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
629 Primitive::kPrimNot,
630 nullptr);
631 parallel_move.AddMove(obj_,
632 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
633 Primitive::kPrimNot,
634 nullptr);
635 if (index.IsValid()) {
636 parallel_move.AddMove(index,
637 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
638 Primitive::kPrimInt,
639 nullptr);
640 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
641 } else {
642 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
643 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
644 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100645 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000646 CheckEntrypointTypes<
647 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
648 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
649
650 RestoreLiveRegisters(codegen, locations);
651 __ b(GetExitLabel());
652 }
653
654 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
655
656 private:
657 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
658 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
659 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
660 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
661 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
662 return static_cast<Register>(i);
663 }
664 }
665 // We shall never fail to find a free caller-save register, as
666 // there are more than two core caller-save registers on ARM
667 // (meaning it is possible to find one which is different from
668 // `ref` and `obj`).
669 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
670 LOG(FATAL) << "Could not find a free caller-save register";
671 UNREACHABLE();
672 }
673
Roland Levillain3b359c72015-11-17 19:35:12 +0000674 const Location out_;
675 const Location ref_;
676 const Location obj_;
677 const uint32_t offset_;
678 // An additional location containing an index to an array.
679 // Only used for HArrayGet and the UnsafeGetObject &
680 // UnsafeGetObjectVolatile intrinsics.
681 const Location index_;
682
683 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
684};
685
686// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100687class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000688 public:
689 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100690 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000691 DCHECK(kEmitCompilerReadBarrier);
692 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000693
694 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
695 LocationSummary* locations = instruction_->GetLocations();
696 Register reg_out = out_.AsRegister<Register>();
697 DCHECK(locations->CanCall());
698 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000699 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
700 << "Unexpected instruction in read barrier for GC root slow path: "
701 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000702
703 __ Bind(GetEntryLabel());
704 SaveLiveRegisters(codegen, locations);
705
706 InvokeRuntimeCallingConvention calling_convention;
707 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
708 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100709 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +0000710 instruction_,
711 instruction_->GetDexPc(),
712 this);
713 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
714 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
715
716 RestoreLiveRegisters(codegen, locations);
717 __ b(GetExitLabel());
718 }
719
720 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
721
722 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000723 const Location out_;
724 const Location root_;
725
726 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
727};
728
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000729#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100730// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
731#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700732
Aart Bike9f37602015-10-09 11:15:55 -0700733inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700734 switch (cond) {
735 case kCondEQ: return EQ;
736 case kCondNE: return NE;
737 case kCondLT: return LT;
738 case kCondLE: return LE;
739 case kCondGT: return GT;
740 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700741 case kCondB: return LO;
742 case kCondBE: return LS;
743 case kCondA: return HI;
744 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700745 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100746 LOG(FATAL) << "Unreachable";
747 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700748}
749
Aart Bike9f37602015-10-09 11:15:55 -0700750// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100751inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700752 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100753 case kCondEQ: return EQ;
754 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700755 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100756 case kCondLT: return LO;
757 case kCondLE: return LS;
758 case kCondGT: return HI;
759 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700760 // Unsigned remain unchanged.
761 case kCondB: return LO;
762 case kCondBE: return LS;
763 case kCondA: return HI;
764 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700765 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100766 LOG(FATAL) << "Unreachable";
767 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700768}
769
Vladimir Markod6e069b2016-01-18 11:11:01 +0000770inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
771 // The ARM condition codes can express all the necessary branches, see the
772 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
773 // There is no dex instruction or HIR that would need the missing conditions
774 // "equal or unordered" or "not equal".
775 switch (cond) {
776 case kCondEQ: return EQ;
777 case kCondNE: return NE /* unordered */;
778 case kCondLT: return gt_bias ? CC : LT /* unordered */;
779 case kCondLE: return gt_bias ? LS : LE /* unordered */;
780 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
781 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
782 default:
783 LOG(FATAL) << "UNREACHABLE";
784 UNREACHABLE();
785 }
786}
787
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100788void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100789 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100790}
791
792void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100793 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100794}
795
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100796size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
797 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
798 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100799}
800
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100801size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
802 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
803 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100804}
805
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000806size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
807 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
808 return kArmWordSize;
809}
810
811size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
812 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
813 return kArmWordSize;
814}
815
Calin Juravle34166012014-12-19 17:22:29 +0000816CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000817 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100818 const CompilerOptions& compiler_options,
819 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000820 : CodeGenerator(graph,
821 kNumberOfCoreRegisters,
822 kNumberOfSRegisters,
823 kNumberOfRegisterPairs,
824 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
825 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000826 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
827 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100828 compiler_options,
829 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100830 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100831 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100832 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100833 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100834 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000835 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000836 uint32_literals_(std::less<uint32_t>(),
837 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100838 method_patches_(MethodReferenceComparator(),
839 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
840 call_patches_(MethodReferenceComparator(),
841 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000842 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000843 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
844 boot_image_string_patches_(StringReferenceValueComparator(),
845 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
846 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100847 boot_image_type_patches_(TypeReferenceValueComparator(),
848 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
849 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000850 boot_image_address_patches_(std::less<uint32_t>(),
851 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700852 // Always save the LR register to mimic Quick.
853 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100854}
855
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000856void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
857 // Ensure that we fix up branches and literal loads and emit the literal pool.
858 __ FinalizeCode();
859
860 // Adjust native pc offsets in stack maps.
861 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
862 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
863 uint32_t new_position = __ GetAdjustedPosition(old_position);
864 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
865 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100866 // Adjust pc offsets for the disassembly information.
867 if (disasm_info_ != nullptr) {
868 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
869 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
870 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
871 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
872 it.second.start = __ GetAdjustedPosition(it.second.start);
873 it.second.end = __ GetAdjustedPosition(it.second.end);
874 }
875 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
876 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
877 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
878 }
879 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000880
881 CodeGenerator::Finalize(allocator);
882}
883
David Brazdil58282f42016-01-14 12:45:10 +0000884void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100885 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100886 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100887
888 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100889 blocked_core_registers_[SP] = true;
890 blocked_core_registers_[LR] = true;
891 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100892
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100893 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100894 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100895
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100896 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100897 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100898
David Brazdil58282f42016-01-14 12:45:10 +0000899 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100900 // Stubs do not save callee-save floating point registers. If the graph
901 // is debuggable, we need to deal with these registers differently. For
902 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000903 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
904 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
905 }
906 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100907
908 UpdateBlockedPairRegisters();
909}
910
911void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
912 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
913 ArmManagedRegister current =
914 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
915 if (blocked_core_registers_[current.AsRegisterPairLow()]
916 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
917 blocked_register_pairs_[i] = true;
918 }
919 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100920}
921
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100922InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800923 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100924 assembler_(codegen->GetAssembler()),
925 codegen_(codegen) {}
926
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000927void CodeGeneratorARM::ComputeSpillMask() {
928 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
929 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000930 // There is no easy instruction to restore just the PC on thumb2. We spill and
931 // restore another arbitrary register.
932 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000933 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
934 // We use vpush and vpop for saving and restoring floating point registers, which take
935 // a SRegister and the number of registers to save/restore after that SRegister. We
936 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
937 // but in the range.
938 if (fpu_spill_mask_ != 0) {
939 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
940 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
941 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
942 fpu_spill_mask_ |= (1 << i);
943 }
944 }
945}
946
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100947static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100948 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100949}
950
951static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100952 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100953}
954
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000955void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000956 bool skip_overflow_check =
957 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000958 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000959 __ Bind(&frame_entry_label_);
960
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000961 if (HasEmptyFrame()) {
962 return;
963 }
964
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100965 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000966 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
967 __ LoadFromOffset(kLoadWord, IP, IP, 0);
968 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100969 }
970
Andreas Gampe501fd632015-09-10 16:11:06 -0700971 __ PushList(core_spill_mask_);
972 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
973 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000974 if (fpu_spill_mask_ != 0) {
975 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
976 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100977 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100978 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000979 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100980 int adjust = GetFrameSize() - FrameEntrySpillSize();
981 __ AddConstant(SP, -adjust);
982 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100983 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000984}
985
986void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000987 if (HasEmptyFrame()) {
988 __ bx(LR);
989 return;
990 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100991 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100992 int adjust = GetFrameSize() - FrameEntrySpillSize();
993 __ AddConstant(SP, adjust);
994 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000995 if (fpu_spill_mask_ != 0) {
996 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
997 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -0700998 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100999 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001000 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001001 // Pop LR into PC to return.
1002 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1003 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1004 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001005 __ cfi().RestoreState();
1006 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001007}
1008
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001009void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001010 Label* label = GetLabelOf(block);
1011 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001012}
1013
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001014Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001015 switch (type) {
1016 case Primitive::kPrimBoolean:
1017 case Primitive::kPrimByte:
1018 case Primitive::kPrimChar:
1019 case Primitive::kPrimShort:
1020 case Primitive::kPrimInt:
1021 case Primitive::kPrimNot: {
1022 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001023 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001024 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001025 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001026 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001027 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001028 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001029 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001030
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001031 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001032 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001033 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001034 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001035 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001036 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001037 if (calling_convention.GetRegisterAt(index) == R1) {
1038 // Skip R1, and use R2_R3 instead.
1039 gp_index_++;
1040 index++;
1041 }
1042 }
1043 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1044 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001045 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001046
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001047 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001048 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001049 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001050 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1051 }
1052 }
1053
1054 case Primitive::kPrimFloat: {
1055 uint32_t stack_index = stack_index_++;
1056 if (float_index_ % 2 == 0) {
1057 float_index_ = std::max(double_index_, float_index_);
1058 }
1059 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1060 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1061 } else {
1062 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1063 }
1064 }
1065
1066 case Primitive::kPrimDouble: {
1067 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1068 uint32_t stack_index = stack_index_;
1069 stack_index_ += 2;
1070 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1071 uint32_t index = double_index_;
1072 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001073 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001074 calling_convention.GetFpuRegisterAt(index),
1075 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001076 DCHECK(ExpectedPairLayout(result));
1077 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001078 } else {
1079 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001080 }
1081 }
1082
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001083 case Primitive::kPrimVoid:
1084 LOG(FATAL) << "Unexpected parameter type " << type;
1085 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001086 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001087 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001088}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001089
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001090Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001091 switch (type) {
1092 case Primitive::kPrimBoolean:
1093 case Primitive::kPrimByte:
1094 case Primitive::kPrimChar:
1095 case Primitive::kPrimShort:
1096 case Primitive::kPrimInt:
1097 case Primitive::kPrimNot: {
1098 return Location::RegisterLocation(R0);
1099 }
1100
1101 case Primitive::kPrimFloat: {
1102 return Location::FpuRegisterLocation(S0);
1103 }
1104
1105 case Primitive::kPrimLong: {
1106 return Location::RegisterPairLocation(R0, R1);
1107 }
1108
1109 case Primitive::kPrimDouble: {
1110 return Location::FpuRegisterPairLocation(S0, S1);
1111 }
1112
1113 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001114 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001115 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001116
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001117 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001118}
1119
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001120Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1121 return Location::RegisterLocation(kMethodRegisterArgument);
1122}
1123
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124void CodeGeneratorARM::Move32(Location destination, Location source) {
1125 if (source.Equals(destination)) {
1126 return;
1127 }
1128 if (destination.IsRegister()) {
1129 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001130 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001131 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001132 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001133 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001134 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001136 } else if (destination.IsFpuRegister()) {
1137 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001138 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001139 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001140 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001141 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001142 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001143 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001144 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001145 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001147 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001149 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001151 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001152 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1153 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 }
1155 }
1156}
1157
1158void CodeGeneratorARM::Move64(Location destination, Location source) {
1159 if (source.Equals(destination)) {
1160 return;
1161 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001162 if (destination.IsRegisterPair()) {
1163 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001164 EmitParallelMoves(
1165 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1166 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001167 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001168 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001169 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1170 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001172 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001173 } else if (source.IsFpuRegisterPair()) {
1174 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1175 destination.AsRegisterPairHigh<Register>(),
1176 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 } else {
1178 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001179 DCHECK(ExpectedPairLayout(destination));
1180 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1181 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001183 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001185 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1186 SP,
1187 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001188 } else if (source.IsRegisterPair()) {
1189 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1190 source.AsRegisterPairLow<Register>(),
1191 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001192 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001193 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001194 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 } else {
1196 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001197 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001198 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001199 if (source.AsRegisterPairLow<Register>() == R1) {
1200 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001201 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1202 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001203 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001204 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 SP, destination.GetStackIndex());
1206 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001207 } else if (source.IsFpuRegisterPair()) {
1208 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1209 SP,
1210 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 } else {
1212 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001213 EmitParallelMoves(
1214 Location::StackSlot(source.GetStackIndex()),
1215 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001216 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001217 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001218 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1219 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 }
1221 }
1222}
1223
Calin Juravle175dc732015-08-25 15:42:32 +01001224void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1225 DCHECK(location.IsRegister());
1226 __ LoadImmediate(location.AsRegister<Register>(), value);
1227}
1228
Calin Juravlee460d1d2015-09-29 04:52:17 +01001229void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001230 HParallelMove move(GetGraph()->GetArena());
1231 move.AddMove(src, dst, dst_type, nullptr);
1232 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233}
1234
1235void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1236 if (location.IsRegister()) {
1237 locations->AddTemp(location);
1238 } else if (location.IsRegisterPair()) {
1239 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1240 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1241 } else {
1242 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1243 }
1244}
1245
Calin Juravle175dc732015-08-25 15:42:32 +01001246void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1247 HInstruction* instruction,
1248 uint32_t dex_pc,
1249 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001250 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001251 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001252 if (EntrypointRequiresStackMap(entrypoint)) {
1253 RecordPcInfo(instruction, dex_pc, slow_path);
1254 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001255}
1256
Roland Levillaindec8f632016-07-22 17:10:06 +01001257void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1258 HInstruction* instruction,
1259 SlowPathCode* slow_path) {
1260 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001261 GenerateInvokeRuntime(entry_point_offset);
1262}
1263
1264void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001265 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1266 __ blx(LR);
1267}
1268
David Brazdilfc6a86a2015-06-26 10:33:45 +00001269void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001270 DCHECK(!successor->IsExitBlock());
1271
1272 HBasicBlock* block = got->GetBlock();
1273 HInstruction* previous = got->GetPrevious();
1274
1275 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001276 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001277 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1278 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1279 return;
1280 }
1281
1282 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1283 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1284 }
1285 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001286 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001287 }
1288}
1289
David Brazdilfc6a86a2015-06-26 10:33:45 +00001290void LocationsBuilderARM::VisitGoto(HGoto* got) {
1291 got->SetLocations(nullptr);
1292}
1293
1294void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1295 HandleGoto(got, got->GetSuccessor());
1296}
1297
1298void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1299 try_boundary->SetLocations(nullptr);
1300}
1301
1302void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1303 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1304 if (!successor->IsExitBlock()) {
1305 HandleGoto(try_boundary, successor);
1306 }
1307}
1308
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001309void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001310 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001311}
1312
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001313void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001314}
1315
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001316void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1317 Primitive::Type type = instruction->InputAt(0)->GetType();
1318 Location lhs_loc = instruction->GetLocations()->InAt(0);
1319 Location rhs_loc = instruction->GetLocations()->InAt(1);
1320 if (rhs_loc.IsConstant()) {
1321 // 0.0 is the only immediate that can be encoded directly in
1322 // a VCMP instruction.
1323 //
1324 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1325 // specify that in a floating-point comparison, positive zero
1326 // and negative zero are considered equal, so we can use the
1327 // literal 0.0 for both cases here.
1328 //
1329 // Note however that some methods (Float.equal, Float.compare,
1330 // Float.compareTo, Double.equal, Double.compare,
1331 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1332 // StrictMath.min) consider 0.0 to be (strictly) greater than
1333 // -0.0. So if we ever translate calls to these methods into a
1334 // HCompare instruction, we must handle the -0.0 case with
1335 // care here.
1336 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1337 if (type == Primitive::kPrimFloat) {
1338 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1339 } else {
1340 DCHECK_EQ(type, Primitive::kPrimDouble);
1341 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1342 }
1343 } else {
1344 if (type == Primitive::kPrimFloat) {
1345 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1346 } else {
1347 DCHECK_EQ(type, Primitive::kPrimDouble);
1348 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1349 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1350 }
1351 }
1352}
1353
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1355 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001356 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001357 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001358 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001359}
1360
1361void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1362 Label* true_label,
1363 Label* false_label) {
1364 LocationSummary* locations = cond->GetLocations();
1365 Location left = locations->InAt(0);
1366 Location right = locations->InAt(1);
1367 IfCondition if_cond = cond->GetCondition();
1368
1369 Register left_high = left.AsRegisterPairHigh<Register>();
1370 Register left_low = left.AsRegisterPairLow<Register>();
1371 IfCondition true_high_cond = if_cond;
1372 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001373 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001374
1375 // Set the conditions for the test, remembering that == needs to be
1376 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001377 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001378 switch (if_cond) {
1379 case kCondEQ:
1380 case kCondNE:
1381 // Nothing to do.
1382 break;
1383 case kCondLT:
1384 false_high_cond = kCondGT;
1385 break;
1386 case kCondLE:
1387 true_high_cond = kCondLT;
1388 break;
1389 case kCondGT:
1390 false_high_cond = kCondLT;
1391 break;
1392 case kCondGE:
1393 true_high_cond = kCondGT;
1394 break;
Aart Bike9f37602015-10-09 11:15:55 -07001395 case kCondB:
1396 false_high_cond = kCondA;
1397 break;
1398 case kCondBE:
1399 true_high_cond = kCondB;
1400 break;
1401 case kCondA:
1402 false_high_cond = kCondB;
1403 break;
1404 case kCondAE:
1405 true_high_cond = kCondA;
1406 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 }
1408 if (right.IsConstant()) {
1409 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1410 int32_t val_low = Low32Bits(value);
1411 int32_t val_high = High32Bits(value);
1412
Vladimir Markoac6ac102015-12-17 12:14:00 +00001413 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001414 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001415 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001416 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001417 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001418 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001419 __ b(true_label, ARMCondition(true_high_cond));
1420 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001421 }
1422 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001423 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001424 } else {
1425 Register right_high = right.AsRegisterPairHigh<Register>();
1426 Register right_low = right.AsRegisterPairLow<Register>();
1427
1428 __ cmp(left_high, ShifterOperand(right_high));
1429 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001430 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001431 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001432 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001433 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001434 __ b(true_label, ARMCondition(true_high_cond));
1435 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001436 }
1437 // Must be equal high, so compare the lows.
1438 __ cmp(left_low, ShifterOperand(right_low));
1439 }
1440 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001441 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001442 __ b(true_label, final_condition);
1443}
1444
David Brazdil0debae72015-11-12 18:37:00 +00001445void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1446 Label* true_target_in,
1447 Label* false_target_in) {
1448 // Generated branching requires both targets to be explicit. If either of the
1449 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1450 Label fallthrough_target;
1451 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1452 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1453
Roland Levillain4fa13f62015-07-06 18:11:54 +01001454 Primitive::Type type = condition->InputAt(0)->GetType();
1455 switch (type) {
1456 case Primitive::kPrimLong:
1457 GenerateLongComparesAndJumps(condition, true_target, false_target);
1458 break;
1459 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001460 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001461 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001462 GenerateFPJumps(condition, true_target, false_target);
1463 break;
1464 default:
1465 LOG(FATAL) << "Unexpected compare type " << type;
1466 }
1467
David Brazdil0debae72015-11-12 18:37:00 +00001468 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001469 __ b(false_target);
1470 }
David Brazdil0debae72015-11-12 18:37:00 +00001471
1472 if (fallthrough_target.IsLinked()) {
1473 __ Bind(&fallthrough_target);
1474 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001475}
1476
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001477void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001478 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001479 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001480 Label* false_target) {
1481 HInstruction* cond = instruction->InputAt(condition_input_index);
1482
1483 if (true_target == nullptr && false_target == nullptr) {
1484 // Nothing to do. The code always falls through.
1485 return;
1486 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001487 // Constant condition, statically compared against "true" (integer value 1).
1488 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001489 if (true_target != nullptr) {
1490 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001491 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001492 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001493 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001494 if (false_target != nullptr) {
1495 __ b(false_target);
1496 }
1497 }
1498 return;
1499 }
1500
1501 // The following code generates these patterns:
1502 // (1) true_target == nullptr && false_target != nullptr
1503 // - opposite condition true => branch to false_target
1504 // (2) true_target != nullptr && false_target == nullptr
1505 // - condition true => branch to true_target
1506 // (3) true_target != nullptr && false_target != nullptr
1507 // - condition true => branch to true_target
1508 // - branch to false_target
1509 if (IsBooleanValueOrMaterializedCondition(cond)) {
1510 // Condition has been materialized, compare the output to 0.
1511 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1512 DCHECK(cond_val.IsRegister());
1513 if (true_target == nullptr) {
1514 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1515 } else {
1516 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001517 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001518 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001519 // Condition has not been materialized. Use its inputs as the comparison and
1520 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001521 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001522
1523 // If this is a long or FP comparison that has been folded into
1524 // the HCondition, generate the comparison directly.
1525 Primitive::Type type = condition->InputAt(0)->GetType();
1526 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1527 GenerateCompareTestAndBranch(condition, true_target, false_target);
1528 return;
1529 }
1530
1531 LocationSummary* locations = cond->GetLocations();
1532 DCHECK(locations->InAt(0).IsRegister());
1533 Register left = locations->InAt(0).AsRegister<Register>();
1534 Location right = locations->InAt(1);
1535 if (right.IsRegister()) {
1536 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001537 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001538 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001539 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001540 }
1541 if (true_target == nullptr) {
1542 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1543 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001544 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001545 }
Dave Allison20dfc792014-06-16 20:44:29 -07001546 }
David Brazdil0debae72015-11-12 18:37:00 +00001547
1548 // If neither branch falls through (case 3), the conditional branch to `true_target`
1549 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1550 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001551 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001552 }
1553}
1554
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001555void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1557 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001558 locations->SetInAt(0, Location::RequiresRegister());
1559 }
1560}
1561
1562void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001563 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1564 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1565 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1566 nullptr : codegen_->GetLabelOf(true_successor);
1567 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1568 nullptr : codegen_->GetLabelOf(false_successor);
1569 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001570}
1571
1572void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1573 LocationSummary* locations = new (GetGraph()->GetArena())
1574 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01001575 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001576 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001577 locations->SetInAt(0, Location::RequiresRegister());
1578 }
1579}
1580
1581void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001582 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001583 GenerateTestAndBranch(deoptimize,
1584 /* condition_input_index */ 0,
1585 slow_path->GetEntryLabel(),
1586 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587}
Dave Allison20dfc792014-06-16 20:44:29 -07001588
David Brazdil74eb1b22015-12-14 11:44:01 +00001589void LocationsBuilderARM::VisitSelect(HSelect* select) {
1590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1591 if (Primitive::IsFloatingPointType(select->GetType())) {
1592 locations->SetInAt(0, Location::RequiresFpuRegister());
1593 locations->SetInAt(1, Location::RequiresFpuRegister());
1594 } else {
1595 locations->SetInAt(0, Location::RequiresRegister());
1596 locations->SetInAt(1, Location::RequiresRegister());
1597 }
1598 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1599 locations->SetInAt(2, Location::RequiresRegister());
1600 }
1601 locations->SetOut(Location::SameAsFirstInput());
1602}
1603
1604void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1605 LocationSummary* locations = select->GetLocations();
1606 Label false_target;
1607 GenerateTestAndBranch(select,
1608 /* condition_input_index */ 2,
1609 /* true_target */ nullptr,
1610 &false_target);
1611 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1612 __ Bind(&false_target);
1613}
1614
David Srbecky0cf44932015-12-09 14:09:59 +00001615void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1616 new (GetGraph()->GetArena()) LocationSummary(info);
1617}
1618
David Srbeckyd28f4a02016-03-14 17:14:24 +00001619void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1620 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001621}
1622
1623void CodeGeneratorARM::GenerateNop() {
1624 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001625}
1626
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001627void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001628 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001629 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001630 // Handle the long/FP comparisons made in instruction simplification.
1631 switch (cond->InputAt(0)->GetType()) {
1632 case Primitive::kPrimLong:
1633 locations->SetInAt(0, Location::RequiresRegister());
1634 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001635 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001636 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1637 }
1638 break;
1639
1640 case Primitive::kPrimFloat:
1641 case Primitive::kPrimDouble:
1642 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001643 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001644 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1646 }
1647 break;
1648
1649 default:
1650 locations->SetInAt(0, Location::RequiresRegister());
1651 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001652 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1654 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001655 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001656}
1657
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001658void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001659 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001660 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001661 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001662
1663 LocationSummary* locations = cond->GetLocations();
1664 Location left = locations->InAt(0);
1665 Location right = locations->InAt(1);
1666 Register out = locations->Out().AsRegister<Register>();
1667 Label true_label, false_label;
1668
1669 switch (cond->InputAt(0)->GetType()) {
1670 default: {
1671 // Integer case.
1672 if (right.IsRegister()) {
1673 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1674 } else {
1675 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001676 __ CmpConstant(left.AsRegister<Register>(),
1677 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001678 }
Aart Bike9f37602015-10-09 11:15:55 -07001679 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001680 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001681 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001682 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001683 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001684 return;
1685 }
1686 case Primitive::kPrimLong:
1687 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1688 break;
1689 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001690 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001691 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001692 GenerateFPJumps(cond, &true_label, &false_label);
1693 break;
1694 }
1695
1696 // Convert the jumps into the result.
1697 Label done_label;
1698
1699 // False case: result = 0.
1700 __ Bind(&false_label);
1701 __ LoadImmediate(out, 0);
1702 __ b(&done_label);
1703
1704 // True case: result = 1.
1705 __ Bind(&true_label);
1706 __ LoadImmediate(out, 1);
1707 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001708}
1709
1710void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001711 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001712}
1713
1714void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001715 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001716}
1717
1718void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001719 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001720}
1721
1722void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001723 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001724}
1725
1726void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001727 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001728}
1729
1730void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001731 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001732}
1733
1734void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001735 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001736}
1737
1738void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001739 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001740}
1741
1742void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001743 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001744}
1745
1746void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001747 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001748}
1749
1750void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001751 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001752}
1753
1754void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001755 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001756}
1757
Aart Bike9f37602015-10-09 11:15:55 -07001758void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001759 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001760}
1761
1762void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001764}
1765
1766void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001768}
1769
1770void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001771 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001772}
1773
1774void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001776}
1777
1778void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001780}
1781
1782void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001784}
1785
1786void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001788}
1789
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001790void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001791 LocationSummary* locations =
1792 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001793 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001794}
1795
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001796void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001797 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001798}
1799
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001800void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1801 LocationSummary* locations =
1802 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1803 locations->SetOut(Location::ConstantLocation(constant));
1804}
1805
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001806void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001807 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001808}
1809
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001810void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001811 LocationSummary* locations =
1812 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001813 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814}
1815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001816void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001817 // Will be generated at use site.
1818}
1819
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001820void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1821 LocationSummary* locations =
1822 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1823 locations->SetOut(Location::ConstantLocation(constant));
1824}
1825
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001826void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001827 // Will be generated at use site.
1828}
1829
1830void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1831 LocationSummary* locations =
1832 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1833 locations->SetOut(Location::ConstantLocation(constant));
1834}
1835
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001836void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001837 // Will be generated at use site.
1838}
1839
Calin Juravle27df7582015-04-17 19:12:31 +01001840void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1841 memory_barrier->SetLocations(nullptr);
1842}
1843
1844void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001845 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001846}
1847
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001849 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001850}
1851
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001852void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001853 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001854}
1855
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001857 LocationSummary* locations =
1858 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001859 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860}
1861
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001862void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001863 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864}
1865
Calin Juravle175dc732015-08-25 15:42:32 +01001866void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1867 // The trampoline uses the same calling convention as dex calling conventions,
1868 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1869 // the method_idx.
1870 HandleInvoke(invoke);
1871}
1872
1873void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1874 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1875}
1876
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001877void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001878 // Explicit clinit checks triggered by static invokes must have been pruned by
1879 // art::PrepareForRegisterAllocation.
1880 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001881
Vladimir Marko68c981f2016-08-26 13:13:33 +01001882 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001883 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001884 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1885 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1886 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001887 return;
1888 }
1889
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001890 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001891
1892 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1893 if (invoke->HasPcRelativeDexCache()) {
1894 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1895 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001896}
1897
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001898static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1899 if (invoke->GetLocations()->Intrinsified()) {
1900 IntrinsicCodeGeneratorARM intrinsic(codegen);
1901 intrinsic.Dispatch(invoke);
1902 return true;
1903 }
1904 return false;
1905}
1906
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001907void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001908 // Explicit clinit checks triggered by static invokes must have been pruned by
1909 // art::PrepareForRegisterAllocation.
1910 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001911
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001912 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1913 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001914 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001915
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001916 LocationSummary* locations = invoke->GetLocations();
1917 codegen_->GenerateStaticOrDirectCall(
1918 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001919 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001920}
1921
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001922void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001923 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001924 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001925}
1926
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001927void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01001928 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001929 if (intrinsic.TryDispatch(invoke)) {
1930 return;
1931 }
1932
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001933 HandleInvoke(invoke);
1934}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001935
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001936void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001937 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1938 return;
1939 }
1940
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001941 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001942 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001943 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001944}
1945
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001946void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1947 HandleInvoke(invoke);
1948 // Add the hidden argument.
1949 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1950}
1951
1952void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1953 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001954 LocationSummary* locations = invoke->GetLocations();
1955 Register temp = locations->GetTemp(0).AsRegister<Register>();
1956 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001957 Location receiver = locations->InAt(0);
1958 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1959
Roland Levillain3b359c72015-11-17 19:35:12 +00001960 // Set the hidden argument. This is safe to do this here, as R12
1961 // won't be modified thereafter, before the `blx` (call) instruction.
1962 DCHECK_EQ(R12, hidden_reg);
1963 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001964
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001965 if (receiver.IsStackSlot()) {
1966 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001967 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001968 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1969 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001970 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001971 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001972 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001973 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001974 // Instead of simply (possibly) unpoisoning `temp` here, we should
1975 // emit a read barrier for the previous class reference load.
1976 // However this is not required in practice, as this is an
1977 // intermediate/temporary reference and because the current
1978 // concurrent copying collector keeps the from-space memory
1979 // intact/accessible until the end of the marking phase (the
1980 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001981 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001982 __ LoadFromOffset(kLoadWord, temp, temp,
1983 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1984 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00001985 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001986 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001987 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001988 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07001989 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001990 // LR = temp->GetEntryPoint();
1991 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1992 // LR();
1993 __ blx(LR);
1994 DCHECK(!codegen_->IsLeafMethod());
1995 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1996}
1997
Roland Levillain88cb1752014-10-20 16:36:47 +01001998void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2001 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002002 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002003 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002004 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2005 break;
2006 }
2007 case Primitive::kPrimLong: {
2008 locations->SetInAt(0, Location::RequiresRegister());
2009 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002010 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002011 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002012
Roland Levillain88cb1752014-10-20 16:36:47 +01002013 case Primitive::kPrimFloat:
2014 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002015 locations->SetInAt(0, Location::RequiresFpuRegister());
2016 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002017 break;
2018
2019 default:
2020 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2021 }
2022}
2023
2024void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2025 LocationSummary* locations = neg->GetLocations();
2026 Location out = locations->Out();
2027 Location in = locations->InAt(0);
2028 switch (neg->GetResultType()) {
2029 case Primitive::kPrimInt:
2030 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002031 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002032 break;
2033
2034 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002035 DCHECK(in.IsRegisterPair());
2036 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2037 __ rsbs(out.AsRegisterPairLow<Register>(),
2038 in.AsRegisterPairLow<Register>(),
2039 ShifterOperand(0));
2040 // We cannot emit an RSC (Reverse Subtract with Carry)
2041 // instruction here, as it does not exist in the Thumb-2
2042 // instruction set. We use the following approach
2043 // using SBC and SUB instead.
2044 //
2045 // out.hi = -C
2046 __ sbc(out.AsRegisterPairHigh<Register>(),
2047 out.AsRegisterPairHigh<Register>(),
2048 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2049 // out.hi = out.hi - in.hi
2050 __ sub(out.AsRegisterPairHigh<Register>(),
2051 out.AsRegisterPairHigh<Register>(),
2052 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2053 break;
2054
Roland Levillain88cb1752014-10-20 16:36:47 +01002055 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002056 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002057 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002058 break;
2059
Roland Levillain88cb1752014-10-20 16:36:47 +01002060 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002061 DCHECK(in.IsFpuRegisterPair());
2062 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2063 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002064 break;
2065
2066 default:
2067 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2068 }
2069}
2070
Roland Levillaindff1f282014-11-05 14:15:05 +00002071void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002072 Primitive::Type result_type = conversion->GetResultType();
2073 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002074 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002075
Roland Levillain5b3ee562015-04-14 16:02:41 +01002076 // The float-to-long, double-to-long and long-to-float type conversions
2077 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002078 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002079 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2080 && result_type == Primitive::kPrimLong)
2081 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002082 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002083 : LocationSummary::kNoCall;
2084 LocationSummary* locations =
2085 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2086
David Brazdilb2bd1c52015-03-25 11:17:37 +00002087 // The Java language does not allow treating boolean as an integral type but
2088 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002089
Roland Levillaindff1f282014-11-05 14:15:05 +00002090 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002091 case Primitive::kPrimByte:
2092 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002093 case Primitive::kPrimLong:
2094 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002095 case Primitive::kPrimBoolean:
2096 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002097 case Primitive::kPrimShort:
2098 case Primitive::kPrimInt:
2099 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002100 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002101 locations->SetInAt(0, Location::RequiresRegister());
2102 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2103 break;
2104
2105 default:
2106 LOG(FATAL) << "Unexpected type conversion from " << input_type
2107 << " to " << result_type;
2108 }
2109 break;
2110
Roland Levillain01a8d712014-11-14 16:27:39 +00002111 case Primitive::kPrimShort:
2112 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002113 case Primitive::kPrimLong:
2114 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002115 case Primitive::kPrimBoolean:
2116 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002117 case Primitive::kPrimByte:
2118 case Primitive::kPrimInt:
2119 case Primitive::kPrimChar:
2120 // Processing a Dex `int-to-short' instruction.
2121 locations->SetInAt(0, Location::RequiresRegister());
2122 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2123 break;
2124
2125 default:
2126 LOG(FATAL) << "Unexpected type conversion from " << input_type
2127 << " to " << result_type;
2128 }
2129 break;
2130
Roland Levillain946e1432014-11-11 17:35:19 +00002131 case Primitive::kPrimInt:
2132 switch (input_type) {
2133 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002134 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002135 locations->SetInAt(0, Location::Any());
2136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2137 break;
2138
2139 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002140 // Processing a Dex `float-to-int' instruction.
2141 locations->SetInAt(0, Location::RequiresFpuRegister());
2142 locations->SetOut(Location::RequiresRegister());
2143 locations->AddTemp(Location::RequiresFpuRegister());
2144 break;
2145
Roland Levillain946e1432014-11-11 17:35:19 +00002146 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002147 // Processing a Dex `double-to-int' instruction.
2148 locations->SetInAt(0, Location::RequiresFpuRegister());
2149 locations->SetOut(Location::RequiresRegister());
2150 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002151 break;
2152
2153 default:
2154 LOG(FATAL) << "Unexpected type conversion from " << input_type
2155 << " to " << result_type;
2156 }
2157 break;
2158
Roland Levillaindff1f282014-11-05 14:15:05 +00002159 case Primitive::kPrimLong:
2160 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002161 case Primitive::kPrimBoolean:
2162 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002163 case Primitive::kPrimByte:
2164 case Primitive::kPrimShort:
2165 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002166 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002167 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002168 locations->SetInAt(0, Location::RequiresRegister());
2169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2170 break;
2171
Roland Levillain624279f2014-12-04 11:54:28 +00002172 case Primitive::kPrimFloat: {
2173 // Processing a Dex `float-to-long' instruction.
2174 InvokeRuntimeCallingConvention calling_convention;
2175 locations->SetInAt(0, Location::FpuRegisterLocation(
2176 calling_convention.GetFpuRegisterAt(0)));
2177 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2178 break;
2179 }
2180
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002181 case Primitive::kPrimDouble: {
2182 // Processing a Dex `double-to-long' instruction.
2183 InvokeRuntimeCallingConvention calling_convention;
2184 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2185 calling_convention.GetFpuRegisterAt(0),
2186 calling_convention.GetFpuRegisterAt(1)));
2187 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002188 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002189 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002190
2191 default:
2192 LOG(FATAL) << "Unexpected type conversion from " << input_type
2193 << " to " << result_type;
2194 }
2195 break;
2196
Roland Levillain981e4542014-11-14 11:47:14 +00002197 case Primitive::kPrimChar:
2198 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002199 case Primitive::kPrimLong:
2200 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002201 case Primitive::kPrimBoolean:
2202 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002203 case Primitive::kPrimByte:
2204 case Primitive::kPrimShort:
2205 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002206 // Processing a Dex `int-to-char' instruction.
2207 locations->SetInAt(0, Location::RequiresRegister());
2208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2209 break;
2210
2211 default:
2212 LOG(FATAL) << "Unexpected type conversion from " << input_type
2213 << " to " << result_type;
2214 }
2215 break;
2216
Roland Levillaindff1f282014-11-05 14:15:05 +00002217 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002218 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002219 case Primitive::kPrimBoolean:
2220 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002221 case Primitive::kPrimByte:
2222 case Primitive::kPrimShort:
2223 case Primitive::kPrimInt:
2224 case Primitive::kPrimChar:
2225 // Processing a Dex `int-to-float' instruction.
2226 locations->SetInAt(0, Location::RequiresRegister());
2227 locations->SetOut(Location::RequiresFpuRegister());
2228 break;
2229
Roland Levillain5b3ee562015-04-14 16:02:41 +01002230 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002231 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002232 InvokeRuntimeCallingConvention calling_convention;
2233 locations->SetInAt(0, Location::RegisterPairLocation(
2234 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2235 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002236 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002237 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002238
Roland Levillaincff13742014-11-17 14:32:17 +00002239 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002240 // Processing a Dex `double-to-float' instruction.
2241 locations->SetInAt(0, Location::RequiresFpuRegister());
2242 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002243 break;
2244
2245 default:
2246 LOG(FATAL) << "Unexpected type conversion from " << input_type
2247 << " to " << result_type;
2248 };
2249 break;
2250
Roland Levillaindff1f282014-11-05 14:15:05 +00002251 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002252 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002253 case Primitive::kPrimBoolean:
2254 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002255 case Primitive::kPrimByte:
2256 case Primitive::kPrimShort:
2257 case Primitive::kPrimInt:
2258 case Primitive::kPrimChar:
2259 // Processing a Dex `int-to-double' instruction.
2260 locations->SetInAt(0, Location::RequiresRegister());
2261 locations->SetOut(Location::RequiresFpuRegister());
2262 break;
2263
2264 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002265 // Processing a Dex `long-to-double' instruction.
2266 locations->SetInAt(0, Location::RequiresRegister());
2267 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002268 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002269 locations->AddTemp(Location::RequiresFpuRegister());
2270 break;
2271
Roland Levillaincff13742014-11-17 14:32:17 +00002272 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002273 // Processing a Dex `float-to-double' instruction.
2274 locations->SetInAt(0, Location::RequiresFpuRegister());
2275 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002276 break;
2277
2278 default:
2279 LOG(FATAL) << "Unexpected type conversion from " << input_type
2280 << " to " << result_type;
2281 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002282 break;
2283
2284 default:
2285 LOG(FATAL) << "Unexpected type conversion from " << input_type
2286 << " to " << result_type;
2287 }
2288}
2289
2290void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2291 LocationSummary* locations = conversion->GetLocations();
2292 Location out = locations->Out();
2293 Location in = locations->InAt(0);
2294 Primitive::Type result_type = conversion->GetResultType();
2295 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002296 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002297 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002298 case Primitive::kPrimByte:
2299 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002300 case Primitive::kPrimLong:
2301 // Type conversion from long to byte is a result of code transformations.
2302 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2303 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002304 case Primitive::kPrimBoolean:
2305 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002306 case Primitive::kPrimShort:
2307 case Primitive::kPrimInt:
2308 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002309 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002310 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002311 break;
2312
2313 default:
2314 LOG(FATAL) << "Unexpected type conversion from " << input_type
2315 << " to " << result_type;
2316 }
2317 break;
2318
Roland Levillain01a8d712014-11-14 16:27:39 +00002319 case Primitive::kPrimShort:
2320 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002321 case Primitive::kPrimLong:
2322 // Type conversion from long to short is a result of code transformations.
2323 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2324 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002325 case Primitive::kPrimBoolean:
2326 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002327 case Primitive::kPrimByte:
2328 case Primitive::kPrimInt:
2329 case Primitive::kPrimChar:
2330 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002332 break;
2333
2334 default:
2335 LOG(FATAL) << "Unexpected type conversion from " << input_type
2336 << " to " << result_type;
2337 }
2338 break;
2339
Roland Levillain946e1432014-11-11 17:35:19 +00002340 case Primitive::kPrimInt:
2341 switch (input_type) {
2342 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002343 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002344 DCHECK(out.IsRegister());
2345 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002346 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002347 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002348 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002349 } else {
2350 DCHECK(in.IsConstant());
2351 DCHECK(in.GetConstant()->IsLongConstant());
2352 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002353 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002354 }
2355 break;
2356
Roland Levillain3f8f9362014-12-02 17:45:01 +00002357 case Primitive::kPrimFloat: {
2358 // Processing a Dex `float-to-int' instruction.
2359 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002360 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002361 __ vmovrs(out.AsRegister<Register>(), temp);
2362 break;
2363 }
2364
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002365 case Primitive::kPrimDouble: {
2366 // Processing a Dex `double-to-int' instruction.
2367 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002368 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002369 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002370 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002371 }
Roland Levillain946e1432014-11-11 17:35:19 +00002372
2373 default:
2374 LOG(FATAL) << "Unexpected type conversion from " << input_type
2375 << " to " << result_type;
2376 }
2377 break;
2378
Roland Levillaindff1f282014-11-05 14:15:05 +00002379 case Primitive::kPrimLong:
2380 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002381 case Primitive::kPrimBoolean:
2382 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002383 case Primitive::kPrimByte:
2384 case Primitive::kPrimShort:
2385 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002386 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002387 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002388 DCHECK(out.IsRegisterPair());
2389 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002390 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002391 // Sign extension.
2392 __ Asr(out.AsRegisterPairHigh<Register>(),
2393 out.AsRegisterPairLow<Register>(),
2394 31);
2395 break;
2396
2397 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002398 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002399 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002400 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002401 break;
2402
Roland Levillaindff1f282014-11-05 14:15:05 +00002403 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002404 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002405 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002406 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002407 break;
2408
2409 default:
2410 LOG(FATAL) << "Unexpected type conversion from " << input_type
2411 << " to " << result_type;
2412 }
2413 break;
2414
Roland Levillain981e4542014-11-14 11:47:14 +00002415 case Primitive::kPrimChar:
2416 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002417 case Primitive::kPrimLong:
2418 // Type conversion from long to char is a result of code transformations.
2419 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2420 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002421 case Primitive::kPrimBoolean:
2422 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002423 case Primitive::kPrimByte:
2424 case Primitive::kPrimShort:
2425 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002426 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002427 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002428 break;
2429
2430 default:
2431 LOG(FATAL) << "Unexpected type conversion from " << input_type
2432 << " to " << result_type;
2433 }
2434 break;
2435
Roland Levillaindff1f282014-11-05 14:15:05 +00002436 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002437 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002438 case Primitive::kPrimBoolean:
2439 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002440 case Primitive::kPrimByte:
2441 case Primitive::kPrimShort:
2442 case Primitive::kPrimInt:
2443 case Primitive::kPrimChar: {
2444 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002445 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2446 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002447 break;
2448 }
2449
Roland Levillain5b3ee562015-04-14 16:02:41 +01002450 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002451 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002452 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002453 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002454 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002455
Roland Levillaincff13742014-11-17 14:32:17 +00002456 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002457 // Processing a Dex `double-to-float' instruction.
2458 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2459 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002460 break;
2461
2462 default:
2463 LOG(FATAL) << "Unexpected type conversion from " << input_type
2464 << " to " << result_type;
2465 };
2466 break;
2467
Roland Levillaindff1f282014-11-05 14:15:05 +00002468 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002469 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002470 case Primitive::kPrimBoolean:
2471 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002472 case Primitive::kPrimByte:
2473 case Primitive::kPrimShort:
2474 case Primitive::kPrimInt:
2475 case Primitive::kPrimChar: {
2476 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002477 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002478 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2479 out.AsFpuRegisterPairLow<SRegister>());
2480 break;
2481 }
2482
Roland Levillain647b9ed2014-11-27 12:06:00 +00002483 case Primitive::kPrimLong: {
2484 // Processing a Dex `long-to-double' instruction.
2485 Register low = in.AsRegisterPairLow<Register>();
2486 Register high = in.AsRegisterPairHigh<Register>();
2487 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2488 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002489 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002490 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002491 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2492 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002493
Roland Levillain682393c2015-04-14 15:57:52 +01002494 // temp_d = int-to-double(high)
2495 __ vmovsr(temp_s, high);
2496 __ vcvtdi(temp_d, temp_s);
2497 // constant_d = k2Pow32EncodingForDouble
2498 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2499 // out_d = unsigned-to-double(low)
2500 __ vmovsr(out_s, low);
2501 __ vcvtdu(out_d, out_s);
2502 // out_d += temp_d * constant_d
2503 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002504 break;
2505 }
2506
Roland Levillaincff13742014-11-17 14:32:17 +00002507 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002508 // Processing a Dex `float-to-double' instruction.
2509 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2510 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002511 break;
2512
2513 default:
2514 LOG(FATAL) << "Unexpected type conversion from " << input_type
2515 << " to " << result_type;
2516 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002517 break;
2518
2519 default:
2520 LOG(FATAL) << "Unexpected type conversion from " << input_type
2521 << " to " << result_type;
2522 }
2523}
2524
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002525void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002526 LocationSummary* locations =
2527 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002528 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002529 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002530 locations->SetInAt(0, Location::RequiresRegister());
2531 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2533 break;
2534 }
2535
2536 case Primitive::kPrimLong: {
2537 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002538 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002539 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002540 break;
2541 }
2542
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002543 case Primitive::kPrimFloat:
2544 case Primitive::kPrimDouble: {
2545 locations->SetInAt(0, Location::RequiresFpuRegister());
2546 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002547 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002548 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002549 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002550
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002551 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002552 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002553 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002554}
2555
2556void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2557 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002558 Location out = locations->Out();
2559 Location first = locations->InAt(0);
2560 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002561 switch (add->GetResultType()) {
2562 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002563 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002564 __ add(out.AsRegister<Register>(),
2565 first.AsRegister<Register>(),
2566 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002567 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002568 __ AddConstant(out.AsRegister<Register>(),
2569 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002570 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002571 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002572 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002573
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002574 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002575 if (second.IsConstant()) {
2576 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2577 GenerateAddLongConst(out, first, value);
2578 } else {
2579 DCHECK(second.IsRegisterPair());
2580 __ adds(out.AsRegisterPairLow<Register>(),
2581 first.AsRegisterPairLow<Register>(),
2582 ShifterOperand(second.AsRegisterPairLow<Register>()));
2583 __ adc(out.AsRegisterPairHigh<Register>(),
2584 first.AsRegisterPairHigh<Register>(),
2585 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2586 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002587 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002588 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002589
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002590 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002591 __ vadds(out.AsFpuRegister<SRegister>(),
2592 first.AsFpuRegister<SRegister>(),
2593 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002594 break;
2595
2596 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002597 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2598 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2599 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002600 break;
2601
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002602 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002603 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002604 }
2605}
2606
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002607void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002608 LocationSummary* locations =
2609 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002610 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002611 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002612 locations->SetInAt(0, Location::RequiresRegister());
2613 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002614 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2615 break;
2616 }
2617
2618 case Primitive::kPrimLong: {
2619 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002620 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002621 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002622 break;
2623 }
Calin Juravle11351682014-10-23 15:38:15 +01002624 case Primitive::kPrimFloat:
2625 case Primitive::kPrimDouble: {
2626 locations->SetInAt(0, Location::RequiresFpuRegister());
2627 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002628 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002629 break;
Calin Juravle11351682014-10-23 15:38:15 +01002630 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002631 default:
Calin Juravle11351682014-10-23 15:38:15 +01002632 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002633 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002634}
2635
2636void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2637 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002638 Location out = locations->Out();
2639 Location first = locations->InAt(0);
2640 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002641 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002642 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002643 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002644 __ sub(out.AsRegister<Register>(),
2645 first.AsRegister<Register>(),
2646 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002647 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ AddConstant(out.AsRegister<Register>(),
2649 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002650 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002651 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002652 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002653 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002654
Calin Juravle11351682014-10-23 15:38:15 +01002655 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002656 if (second.IsConstant()) {
2657 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2658 GenerateAddLongConst(out, first, -value);
2659 } else {
2660 DCHECK(second.IsRegisterPair());
2661 __ subs(out.AsRegisterPairLow<Register>(),
2662 first.AsRegisterPairLow<Register>(),
2663 ShifterOperand(second.AsRegisterPairLow<Register>()));
2664 __ sbc(out.AsRegisterPairHigh<Register>(),
2665 first.AsRegisterPairHigh<Register>(),
2666 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2667 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002668 break;
Calin Juravle11351682014-10-23 15:38:15 +01002669 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002670
Calin Juravle11351682014-10-23 15:38:15 +01002671 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002672 __ vsubs(out.AsFpuRegister<SRegister>(),
2673 first.AsFpuRegister<SRegister>(),
2674 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002675 break;
Calin Juravle11351682014-10-23 15:38:15 +01002676 }
2677
2678 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002679 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2680 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2681 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002682 break;
2683 }
2684
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002685
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002686 default:
Calin Juravle11351682014-10-23 15:38:15 +01002687 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002688 }
2689}
2690
Calin Juravle34bacdf2014-10-07 20:23:36 +01002691void LocationsBuilderARM::VisitMul(HMul* mul) {
2692 LocationSummary* locations =
2693 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2694 switch (mul->GetResultType()) {
2695 case Primitive::kPrimInt:
2696 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002697 locations->SetInAt(0, Location::RequiresRegister());
2698 locations->SetInAt(1, Location::RequiresRegister());
2699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002700 break;
2701 }
2702
Calin Juravleb5bfa962014-10-21 18:02:24 +01002703 case Primitive::kPrimFloat:
2704 case Primitive::kPrimDouble: {
2705 locations->SetInAt(0, Location::RequiresFpuRegister());
2706 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002707 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002708 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002709 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002710
2711 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002712 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002713 }
2714}
2715
2716void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2717 LocationSummary* locations = mul->GetLocations();
2718 Location out = locations->Out();
2719 Location first = locations->InAt(0);
2720 Location second = locations->InAt(1);
2721 switch (mul->GetResultType()) {
2722 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002723 __ mul(out.AsRegister<Register>(),
2724 first.AsRegister<Register>(),
2725 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002726 break;
2727 }
2728 case Primitive::kPrimLong: {
2729 Register out_hi = out.AsRegisterPairHigh<Register>();
2730 Register out_lo = out.AsRegisterPairLow<Register>();
2731 Register in1_hi = first.AsRegisterPairHigh<Register>();
2732 Register in1_lo = first.AsRegisterPairLow<Register>();
2733 Register in2_hi = second.AsRegisterPairHigh<Register>();
2734 Register in2_lo = second.AsRegisterPairLow<Register>();
2735
2736 // Extra checks to protect caused by the existence of R1_R2.
2737 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2738 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2739 DCHECK_NE(out_hi, in1_lo);
2740 DCHECK_NE(out_hi, in2_lo);
2741
2742 // input: in1 - 64 bits, in2 - 64 bits
2743 // output: out
2744 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2745 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2746 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2747
2748 // IP <- in1.lo * in2.hi
2749 __ mul(IP, in1_lo, in2_hi);
2750 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2751 __ mla(out_hi, in1_hi, in2_lo, IP);
2752 // out.lo <- (in1.lo * in2.lo)[31:0];
2753 __ umull(out_lo, IP, in1_lo, in2_lo);
2754 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2755 __ add(out_hi, out_hi, ShifterOperand(IP));
2756 break;
2757 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002758
2759 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002760 __ vmuls(out.AsFpuRegister<SRegister>(),
2761 first.AsFpuRegister<SRegister>(),
2762 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002763 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002764 }
2765
2766 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002767 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2768 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2769 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002770 break;
2771 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002772
2773 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002774 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002775 }
2776}
2777
Zheng Xuc6667102015-05-15 16:08:45 +08002778void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2779 DCHECK(instruction->IsDiv() || instruction->IsRem());
2780 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2781
2782 LocationSummary* locations = instruction->GetLocations();
2783 Location second = locations->InAt(1);
2784 DCHECK(second.IsConstant());
2785
2786 Register out = locations->Out().AsRegister<Register>();
2787 Register dividend = locations->InAt(0).AsRegister<Register>();
2788 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2789 DCHECK(imm == 1 || imm == -1);
2790
2791 if (instruction->IsRem()) {
2792 __ LoadImmediate(out, 0);
2793 } else {
2794 if (imm == 1) {
2795 __ Mov(out, dividend);
2796 } else {
2797 __ rsb(out, dividend, ShifterOperand(0));
2798 }
2799 }
2800}
2801
2802void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2803 DCHECK(instruction->IsDiv() || instruction->IsRem());
2804 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2805
2806 LocationSummary* locations = instruction->GetLocations();
2807 Location second = locations->InAt(1);
2808 DCHECK(second.IsConstant());
2809
2810 Register out = locations->Out().AsRegister<Register>();
2811 Register dividend = locations->InAt(0).AsRegister<Register>();
2812 Register temp = locations->GetTemp(0).AsRegister<Register>();
2813 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002814 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002815 int ctz_imm = CTZ(abs_imm);
2816
2817 if (ctz_imm == 1) {
2818 __ Lsr(temp, dividend, 32 - ctz_imm);
2819 } else {
2820 __ Asr(temp, dividend, 31);
2821 __ Lsr(temp, temp, 32 - ctz_imm);
2822 }
2823 __ add(out, temp, ShifterOperand(dividend));
2824
2825 if (instruction->IsDiv()) {
2826 __ Asr(out, out, ctz_imm);
2827 if (imm < 0) {
2828 __ rsb(out, out, ShifterOperand(0));
2829 }
2830 } else {
2831 __ ubfx(out, out, 0, ctz_imm);
2832 __ sub(out, out, ShifterOperand(temp));
2833 }
2834}
2835
2836void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2837 DCHECK(instruction->IsDiv() || instruction->IsRem());
2838 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2839
2840 LocationSummary* locations = instruction->GetLocations();
2841 Location second = locations->InAt(1);
2842 DCHECK(second.IsConstant());
2843
2844 Register out = locations->Out().AsRegister<Register>();
2845 Register dividend = locations->InAt(0).AsRegister<Register>();
2846 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2847 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2848 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2849
2850 int64_t magic;
2851 int shift;
2852 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2853
2854 __ LoadImmediate(temp1, magic);
2855 __ smull(temp2, temp1, dividend, temp1);
2856
2857 if (imm > 0 && magic < 0) {
2858 __ add(temp1, temp1, ShifterOperand(dividend));
2859 } else if (imm < 0 && magic > 0) {
2860 __ sub(temp1, temp1, ShifterOperand(dividend));
2861 }
2862
2863 if (shift != 0) {
2864 __ Asr(temp1, temp1, shift);
2865 }
2866
2867 if (instruction->IsDiv()) {
2868 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2869 } else {
2870 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2871 // TODO: Strength reduction for mls.
2872 __ LoadImmediate(temp2, imm);
2873 __ mls(out, temp1, temp2, dividend);
2874 }
2875}
2876
2877void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2878 DCHECK(instruction->IsDiv() || instruction->IsRem());
2879 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2880
2881 LocationSummary* locations = instruction->GetLocations();
2882 Location second = locations->InAt(1);
2883 DCHECK(second.IsConstant());
2884
2885 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2886 if (imm == 0) {
2887 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2888 } else if (imm == 1 || imm == -1) {
2889 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002890 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002891 DivRemByPowerOfTwo(instruction);
2892 } else {
2893 DCHECK(imm <= -2 || imm >= 2);
2894 GenerateDivRemWithAnyConstant(instruction);
2895 }
2896}
2897
Calin Juravle7c4954d2014-10-28 16:57:40 +00002898void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002899 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2900 if (div->GetResultType() == Primitive::kPrimLong) {
2901 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002902 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002903 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2904 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002905 } else if (div->GetResultType() == Primitive::kPrimInt &&
2906 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2907 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002908 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002909 }
2910
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002911 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2912
Calin Juravle7c4954d2014-10-28 16:57:40 +00002913 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002914 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002915 if (div->InputAt(1)->IsConstant()) {
2916 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002917 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002919 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2920 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002921 // No temp register required.
2922 } else {
2923 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002924 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002925 locations->AddTemp(Location::RequiresRegister());
2926 }
2927 }
2928 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002929 locations->SetInAt(0, Location::RequiresRegister());
2930 locations->SetInAt(1, Location::RequiresRegister());
2931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2932 } else {
2933 InvokeRuntimeCallingConvention calling_convention;
2934 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2935 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2936 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2937 // we only need the former.
2938 locations->SetOut(Location::RegisterLocation(R0));
2939 }
Calin Juravled0d48522014-11-04 16:40:20 +00002940 break;
2941 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002942 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002943 InvokeRuntimeCallingConvention calling_convention;
2944 locations->SetInAt(0, Location::RegisterPairLocation(
2945 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2946 locations->SetInAt(1, Location::RegisterPairLocation(
2947 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002948 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002949 break;
2950 }
2951 case Primitive::kPrimFloat:
2952 case Primitive::kPrimDouble: {
2953 locations->SetInAt(0, Location::RequiresFpuRegister());
2954 locations->SetInAt(1, Location::RequiresFpuRegister());
2955 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2956 break;
2957 }
2958
2959 default:
2960 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2961 }
2962}
2963
2964void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2965 LocationSummary* locations = div->GetLocations();
2966 Location out = locations->Out();
2967 Location first = locations->InAt(0);
2968 Location second = locations->InAt(1);
2969
2970 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002971 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002972 if (second.IsConstant()) {
2973 GenerateDivRemConstantIntegral(div);
2974 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002975 __ sdiv(out.AsRegister<Register>(),
2976 first.AsRegister<Register>(),
2977 second.AsRegister<Register>());
2978 } else {
2979 InvokeRuntimeCallingConvention calling_convention;
2980 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2981 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2982 DCHECK_EQ(R0, out.AsRegister<Register>());
2983
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002984 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002985 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002986 }
Calin Juravled0d48522014-11-04 16:40:20 +00002987 break;
2988 }
2989
Calin Juravle7c4954d2014-10-28 16:57:40 +00002990 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002991 InvokeRuntimeCallingConvention calling_convention;
2992 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2993 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2994 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2995 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2996 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002997 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002998
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002999 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003000 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003001 break;
3002 }
3003
3004 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003005 __ vdivs(out.AsFpuRegister<SRegister>(),
3006 first.AsFpuRegister<SRegister>(),
3007 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003008 break;
3009 }
3010
3011 case Primitive::kPrimDouble: {
3012 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3013 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3014 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3015 break;
3016 }
3017
3018 default:
3019 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3020 }
3021}
3022
Calin Juravlebacfec32014-11-14 15:54:36 +00003023void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003024 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003025
3026 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003027 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003028 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3029 // sdiv will be replaced by other instruction sequence.
3030 call_kind = LocationSummary::kNoCall;
3031 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3032 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003033 // Have hardware divide instruction for int, do it with three instructions.
3034 call_kind = LocationSummary::kNoCall;
3035 }
3036
Calin Juravlebacfec32014-11-14 15:54:36 +00003037 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3038
Calin Juravled2ec87d2014-12-08 14:24:46 +00003039 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003040 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003041 if (rem->InputAt(1)->IsConstant()) {
3042 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003043 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003045 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3046 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003047 // No temp register required.
3048 } else {
3049 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003050 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003051 locations->AddTemp(Location::RequiresRegister());
3052 }
3053 }
3054 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003055 locations->SetInAt(0, Location::RequiresRegister());
3056 locations->SetInAt(1, Location::RequiresRegister());
3057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3058 locations->AddTemp(Location::RequiresRegister());
3059 } else {
3060 InvokeRuntimeCallingConvention calling_convention;
3061 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3062 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3063 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3064 // we only need the latter.
3065 locations->SetOut(Location::RegisterLocation(R1));
3066 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003067 break;
3068 }
3069 case Primitive::kPrimLong: {
3070 InvokeRuntimeCallingConvention calling_convention;
3071 locations->SetInAt(0, Location::RegisterPairLocation(
3072 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3073 locations->SetInAt(1, Location::RegisterPairLocation(
3074 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3075 // The runtime helper puts the output in R2,R3.
3076 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3077 break;
3078 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003079 case Primitive::kPrimFloat: {
3080 InvokeRuntimeCallingConvention calling_convention;
3081 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3082 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3083 locations->SetOut(Location::FpuRegisterLocation(S0));
3084 break;
3085 }
3086
Calin Juravlebacfec32014-11-14 15:54:36 +00003087 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003088 InvokeRuntimeCallingConvention calling_convention;
3089 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3090 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3091 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3092 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3093 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003094 break;
3095 }
3096
3097 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003098 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003099 }
3100}
3101
3102void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3103 LocationSummary* locations = rem->GetLocations();
3104 Location out = locations->Out();
3105 Location first = locations->InAt(0);
3106 Location second = locations->InAt(1);
3107
Calin Juravled2ec87d2014-12-08 14:24:46 +00003108 Primitive::Type type = rem->GetResultType();
3109 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003110 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003111 if (second.IsConstant()) {
3112 GenerateDivRemConstantIntegral(rem);
3113 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003114 Register reg1 = first.AsRegister<Register>();
3115 Register reg2 = second.AsRegister<Register>();
3116 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003117
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003118 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003119 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003120 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003121 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003122 } else {
3123 InvokeRuntimeCallingConvention calling_convention;
3124 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3125 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3126 DCHECK_EQ(R1, out.AsRegister<Register>());
3127
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003128 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003129 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003130 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003131 break;
3132 }
3133
3134 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003135 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003136 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003137 break;
3138 }
3139
Calin Juravled2ec87d2014-12-08 14:24:46 +00003140 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003141 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003142 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003143 break;
3144 }
3145
Calin Juravlebacfec32014-11-14 15:54:36 +00003146 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003147 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003148 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003149 break;
3150 }
3151
3152 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003153 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003154 }
3155}
3156
Calin Juravled0d48522014-11-04 16:40:20 +00003157void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003158 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3159 ? LocationSummary::kCallOnSlowPath
3160 : LocationSummary::kNoCall;
3161 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003162 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003163 if (instruction->HasUses()) {
3164 locations->SetOut(Location::SameAsFirstInput());
3165 }
3166}
3167
3168void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003169 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003170 codegen_->AddSlowPath(slow_path);
3171
3172 LocationSummary* locations = instruction->GetLocations();
3173 Location value = locations->InAt(0);
3174
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003175 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003176 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003177 case Primitive::kPrimByte:
3178 case Primitive::kPrimChar:
3179 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003180 case Primitive::kPrimInt: {
3181 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003182 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003183 } else {
3184 DCHECK(value.IsConstant()) << value;
3185 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3186 __ b(slow_path->GetEntryLabel());
3187 }
3188 }
3189 break;
3190 }
3191 case Primitive::kPrimLong: {
3192 if (value.IsRegisterPair()) {
3193 __ orrs(IP,
3194 value.AsRegisterPairLow<Register>(),
3195 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3196 __ b(slow_path->GetEntryLabel(), EQ);
3197 } else {
3198 DCHECK(value.IsConstant()) << value;
3199 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3200 __ b(slow_path->GetEntryLabel());
3201 }
3202 }
3203 break;
3204 default:
3205 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3206 }
3207 }
Calin Juravled0d48522014-11-04 16:40:20 +00003208}
3209
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003210void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3211 Register in = locations->InAt(0).AsRegister<Register>();
3212 Location rhs = locations->InAt(1);
3213 Register out = locations->Out().AsRegister<Register>();
3214
3215 if (rhs.IsConstant()) {
3216 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3217 // so map all rotations to a +ve. equivalent in that range.
3218 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3219 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3220 if (rot) {
3221 // Rotate, mapping left rotations to right equivalents if necessary.
3222 // (e.g. left by 2 bits == right by 30.)
3223 __ Ror(out, in, rot);
3224 } else if (out != in) {
3225 __ Mov(out, in);
3226 }
3227 } else {
3228 __ Ror(out, in, rhs.AsRegister<Register>());
3229 }
3230}
3231
3232// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3233// rotates by swapping input regs (effectively rotating by the first 32-bits of
3234// a larger rotation) or flipping direction (thus treating larger right/left
3235// rotations as sub-word sized rotations in the other direction) as appropriate.
3236void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3237 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3238 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3239 Location rhs = locations->InAt(1);
3240 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3241 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3242
3243 if (rhs.IsConstant()) {
3244 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3245 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003246 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003247 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3248 // logic below to a simple pair of binary orr.
3249 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3250 if (rot >= kArmBitsPerWord) {
3251 rot -= kArmBitsPerWord;
3252 std::swap(in_reg_hi, in_reg_lo);
3253 }
3254 // Rotate, or mov to out for zero or word size rotations.
3255 if (rot != 0u) {
3256 __ Lsr(out_reg_hi, in_reg_hi, rot);
3257 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3258 __ Lsr(out_reg_lo, in_reg_lo, rot);
3259 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3260 } else {
3261 __ Mov(out_reg_lo, in_reg_lo);
3262 __ Mov(out_reg_hi, in_reg_hi);
3263 }
3264 } else {
3265 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3266 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3267 Label end;
3268 Label shift_by_32_plus_shift_right;
3269
3270 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3271 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3272 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3273 __ b(&shift_by_32_plus_shift_right, CC);
3274
3275 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3276 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3277 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3278 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3279 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3280 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3281 __ Lsr(shift_left, in_reg_hi, shift_right);
3282 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3283 __ b(&end);
3284
3285 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3286 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3287 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3288 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3289 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3290 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3291 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3292 __ Lsl(shift_right, in_reg_hi, shift_left);
3293 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3294
3295 __ Bind(&end);
3296 }
3297}
Roland Levillain22c49222016-03-18 14:04:28 +00003298
3299void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003300 LocationSummary* locations =
3301 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3302 switch (ror->GetResultType()) {
3303 case Primitive::kPrimInt: {
3304 locations->SetInAt(0, Location::RequiresRegister());
3305 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3307 break;
3308 }
3309 case Primitive::kPrimLong: {
3310 locations->SetInAt(0, Location::RequiresRegister());
3311 if (ror->InputAt(1)->IsConstant()) {
3312 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3313 } else {
3314 locations->SetInAt(1, Location::RequiresRegister());
3315 locations->AddTemp(Location::RequiresRegister());
3316 locations->AddTemp(Location::RequiresRegister());
3317 }
3318 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3319 break;
3320 }
3321 default:
3322 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3323 }
3324}
3325
Roland Levillain22c49222016-03-18 14:04:28 +00003326void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003327 LocationSummary* locations = ror->GetLocations();
3328 Primitive::Type type = ror->GetResultType();
3329 switch (type) {
3330 case Primitive::kPrimInt: {
3331 HandleIntegerRotate(locations);
3332 break;
3333 }
3334 case Primitive::kPrimLong: {
3335 HandleLongRotate(locations);
3336 break;
3337 }
3338 default:
3339 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003340 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003341 }
3342}
3343
Calin Juravle9aec02f2014-11-18 23:06:35 +00003344void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3345 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3346
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003347 LocationSummary* locations =
3348 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003349
3350 switch (op->GetResultType()) {
3351 case Primitive::kPrimInt: {
3352 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003353 if (op->InputAt(1)->IsConstant()) {
3354 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3356 } else {
3357 locations->SetInAt(1, Location::RequiresRegister());
3358 // Make the output overlap, as it will be used to hold the masked
3359 // second input.
3360 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3361 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003362 break;
3363 }
3364 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003365 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003366 if (op->InputAt(1)->IsConstant()) {
3367 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3368 // For simplicity, use kOutputOverlap even though we only require that low registers
3369 // don't clash with high registers which the register allocator currently guarantees.
3370 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3371 } else {
3372 locations->SetInAt(1, Location::RequiresRegister());
3373 locations->AddTemp(Location::RequiresRegister());
3374 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3375 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003376 break;
3377 }
3378 default:
3379 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3380 }
3381}
3382
3383void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3384 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3385
3386 LocationSummary* locations = op->GetLocations();
3387 Location out = locations->Out();
3388 Location first = locations->InAt(0);
3389 Location second = locations->InAt(1);
3390
3391 Primitive::Type type = op->GetResultType();
3392 switch (type) {
3393 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003394 Register out_reg = out.AsRegister<Register>();
3395 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003396 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003397 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003398 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003399 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003400 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003401 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003402 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003403 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003404 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003405 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003406 }
3407 } else {
3408 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003409 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003410 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003411 __ Mov(out_reg, first_reg);
3412 } else if (op->IsShl()) {
3413 __ Lsl(out_reg, first_reg, shift_value);
3414 } else if (op->IsShr()) {
3415 __ Asr(out_reg, first_reg, shift_value);
3416 } else {
3417 __ Lsr(out_reg, first_reg, shift_value);
3418 }
3419 }
3420 break;
3421 }
3422 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003423 Register o_h = out.AsRegisterPairHigh<Register>();
3424 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003425
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003426 Register high = first.AsRegisterPairHigh<Register>();
3427 Register low = first.AsRegisterPairLow<Register>();
3428
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003429 if (second.IsRegister()) {
3430 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003431
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003432 Register second_reg = second.AsRegister<Register>();
3433
3434 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003435 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003436 // Shift the high part
3437 __ Lsl(o_h, high, o_l);
3438 // Shift the low part and `or` what overflew on the high part
3439 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3440 __ Lsr(temp, low, temp);
3441 __ orr(o_h, o_h, ShifterOperand(temp));
3442 // If the shift is > 32 bits, override the high part
3443 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3444 __ it(PL);
3445 __ Lsl(o_h, low, temp, PL);
3446 // Shift the low part
3447 __ Lsl(o_l, low, o_l);
3448 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003449 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003450 // Shift the low part
3451 __ Lsr(o_l, low, o_h);
3452 // Shift the high part and `or` what underflew on the low part
3453 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3454 __ Lsl(temp, high, temp);
3455 __ orr(o_l, o_l, ShifterOperand(temp));
3456 // If the shift is > 32 bits, override the low part
3457 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3458 __ it(PL);
3459 __ Asr(o_l, high, temp, PL);
3460 // Shift the high part
3461 __ Asr(o_h, high, o_h);
3462 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003463 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003464 // same as Shr except we use `Lsr`s and not `Asr`s
3465 __ Lsr(o_l, low, o_h);
3466 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3467 __ Lsl(temp, high, temp);
3468 __ orr(o_l, o_l, ShifterOperand(temp));
3469 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3470 __ it(PL);
3471 __ Lsr(o_l, high, temp, PL);
3472 __ Lsr(o_h, high, o_h);
3473 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003474 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003475 // Register allocator doesn't create partial overlap.
3476 DCHECK_NE(o_l, high);
3477 DCHECK_NE(o_h, low);
3478 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003479 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003480 if (shift_value > 32) {
3481 if (op->IsShl()) {
3482 __ Lsl(o_h, low, shift_value - 32);
3483 __ LoadImmediate(o_l, 0);
3484 } else if (op->IsShr()) {
3485 __ Asr(o_l, high, shift_value - 32);
3486 __ Asr(o_h, high, 31);
3487 } else {
3488 __ Lsr(o_l, high, shift_value - 32);
3489 __ LoadImmediate(o_h, 0);
3490 }
3491 } else if (shift_value == 32) {
3492 if (op->IsShl()) {
3493 __ mov(o_h, ShifterOperand(low));
3494 __ LoadImmediate(o_l, 0);
3495 } else if (op->IsShr()) {
3496 __ mov(o_l, ShifterOperand(high));
3497 __ Asr(o_h, high, 31);
3498 } else {
3499 __ mov(o_l, ShifterOperand(high));
3500 __ LoadImmediate(o_h, 0);
3501 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003502 } else if (shift_value == 1) {
3503 if (op->IsShl()) {
3504 __ Lsls(o_l, low, 1);
3505 __ adc(o_h, high, ShifterOperand(high));
3506 } else if (op->IsShr()) {
3507 __ Asrs(o_h, high, 1);
3508 __ Rrx(o_l, low);
3509 } else {
3510 __ Lsrs(o_h, high, 1);
3511 __ Rrx(o_l, low);
3512 }
3513 } else {
3514 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003515 if (op->IsShl()) {
3516 __ Lsl(o_h, high, shift_value);
3517 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3518 __ Lsl(o_l, low, shift_value);
3519 } else if (op->IsShr()) {
3520 __ Lsr(o_l, low, shift_value);
3521 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3522 __ Asr(o_h, high, shift_value);
3523 } else {
3524 __ Lsr(o_l, low, shift_value);
3525 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3526 __ Lsr(o_h, high, shift_value);
3527 }
3528 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003529 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003530 break;
3531 }
3532 default:
3533 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003534 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003535 }
3536}
3537
3538void LocationsBuilderARM::VisitShl(HShl* shl) {
3539 HandleShift(shl);
3540}
3541
3542void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3543 HandleShift(shl);
3544}
3545
3546void LocationsBuilderARM::VisitShr(HShr* shr) {
3547 HandleShift(shr);
3548}
3549
3550void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3551 HandleShift(shr);
3552}
3553
3554void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3555 HandleShift(ushr);
3556}
3557
3558void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3559 HandleShift(ushr);
3560}
3561
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003562void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003563 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003565 if (instruction->IsStringAlloc()) {
3566 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3567 } else {
3568 InvokeRuntimeCallingConvention calling_convention;
3569 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3570 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3571 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003572 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003573}
3574
3575void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003576 // Note: if heap poisoning is enabled, the entry point takes cares
3577 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003578 if (instruction->IsStringAlloc()) {
3579 // String is allocated through StringFactory. Call NewEmptyString entry point.
3580 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003581 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003582 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3583 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3584 __ blx(LR);
3585 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3586 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003587 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003588 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3589 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003590}
3591
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003592void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3593 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003594 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003595 InvokeRuntimeCallingConvention calling_convention;
3596 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003597 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003598 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003599 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003600}
3601
3602void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3603 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003604 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003605 // Note: if heap poisoning is enabled, the entry point takes cares
3606 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003607 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003608 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003609}
3610
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003611void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003612 LocationSummary* locations =
3613 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003614 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3615 if (location.IsStackSlot()) {
3616 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3617 } else if (location.IsDoubleStackSlot()) {
3618 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003619 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003620 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003621}
3622
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003623void InstructionCodeGeneratorARM::VisitParameterValue(
3624 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003625 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003626}
3627
3628void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3629 LocationSummary* locations =
3630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3631 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3632}
3633
3634void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3635 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003636}
3637
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003638void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003639 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003640 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003641 locations->SetInAt(0, Location::RequiresRegister());
3642 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003643}
3644
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003645void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3646 LocationSummary* locations = not_->GetLocations();
3647 Location out = locations->Out();
3648 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003649 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003650 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003651 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003652 break;
3653
3654 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003655 __ mvn(out.AsRegisterPairLow<Register>(),
3656 ShifterOperand(in.AsRegisterPairLow<Register>()));
3657 __ mvn(out.AsRegisterPairHigh<Register>(),
3658 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003659 break;
3660
3661 default:
3662 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3663 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003664}
3665
David Brazdil66d126e2015-04-03 16:02:44 +01003666void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3667 LocationSummary* locations =
3668 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3669 locations->SetInAt(0, Location::RequiresRegister());
3670 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3671}
3672
3673void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003674 LocationSummary* locations = bool_not->GetLocations();
3675 Location out = locations->Out();
3676 Location in = locations->InAt(0);
3677 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3678}
3679
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003680void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003681 LocationSummary* locations =
3682 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003683 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003684 case Primitive::kPrimBoolean:
3685 case Primitive::kPrimByte:
3686 case Primitive::kPrimShort:
3687 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003688 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003689 case Primitive::kPrimLong: {
3690 locations->SetInAt(0, Location::RequiresRegister());
3691 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003692 // Output overlaps because it is written before doing the low comparison.
3693 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003694 break;
3695 }
3696 case Primitive::kPrimFloat:
3697 case Primitive::kPrimDouble: {
3698 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003699 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003700 locations->SetOut(Location::RequiresRegister());
3701 break;
3702 }
3703 default:
3704 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3705 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003706}
3707
3708void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003709 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003710 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003711 Location left = locations->InAt(0);
3712 Location right = locations->InAt(1);
3713
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003714 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003715 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003716 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003717 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003718 case Primitive::kPrimBoolean:
3719 case Primitive::kPrimByte:
3720 case Primitive::kPrimShort:
3721 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003722 case Primitive::kPrimInt: {
3723 __ LoadImmediate(out, 0);
3724 __ cmp(left.AsRegister<Register>(),
3725 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3726 less_cond = LT;
3727 break;
3728 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003729 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003730 __ cmp(left.AsRegisterPairHigh<Register>(),
3731 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003732 __ b(&less, LT);
3733 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003734 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003735 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003736 __ cmp(left.AsRegisterPairLow<Register>(),
3737 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003738 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003739 break;
3740 }
3741 case Primitive::kPrimFloat:
3742 case Primitive::kPrimDouble: {
3743 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003744 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003745 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003746 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003747 break;
3748 }
3749 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003750 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003751 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003752 }
Aart Bika19616e2016-02-01 18:57:58 -08003753
Calin Juravleddb7df22014-11-25 20:56:51 +00003754 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003755 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003756
3757 __ Bind(&greater);
3758 __ LoadImmediate(out, 1);
3759 __ b(&done);
3760
3761 __ Bind(&less);
3762 __ LoadImmediate(out, -1);
3763
3764 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003765}
3766
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003767void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003768 LocationSummary* locations =
3769 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003770 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003771 locations->SetInAt(i, Location::Any());
3772 }
3773 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003774}
3775
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003776void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003777 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003778}
3779
Roland Levillainc9285912015-12-18 10:38:42 +00003780void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3781 // TODO (ported from quick): revisit ARM barrier kinds.
3782 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003783 switch (kind) {
3784 case MemBarrierKind::kAnyStore:
3785 case MemBarrierKind::kLoadAny:
3786 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003787 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003788 break;
3789 }
3790 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003791 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003792 break;
3793 }
3794 default:
3795 LOG(FATAL) << "Unexpected memory barrier " << kind;
3796 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003797 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003798}
3799
3800void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3801 uint32_t offset,
3802 Register out_lo,
3803 Register out_hi) {
3804 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003805 // Ensure `out_lo` is different from `addr`, so that loading
3806 // `offset` into `out_lo` does not clutter `addr`.
3807 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003808 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003809 __ add(IP, addr, ShifterOperand(out_lo));
3810 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003811 }
3812 __ ldrexd(out_lo, out_hi, addr);
3813}
3814
3815void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3816 uint32_t offset,
3817 Register value_lo,
3818 Register value_hi,
3819 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003820 Register temp2,
3821 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003822 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003823 if (offset != 0) {
3824 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003825 __ add(IP, addr, ShifterOperand(temp1));
3826 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003827 }
3828 __ Bind(&fail);
3829 // We need a load followed by store. (The address used in a STREX instruction must
3830 // be the same as the address in the most recently executed LDREX instruction.)
3831 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003832 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003833 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003834 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003835}
3836
3837void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3838 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3839
Nicolas Geoffray39468442014-09-02 15:17:15 +01003840 LocationSummary* locations =
3841 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003842 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003843
Calin Juravle52c48962014-12-16 17:02:57 +00003844 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003845 if (Primitive::IsFloatingPointType(field_type)) {
3846 locations->SetInAt(1, Location::RequiresFpuRegister());
3847 } else {
3848 locations->SetInAt(1, Location::RequiresRegister());
3849 }
3850
Calin Juravle52c48962014-12-16 17:02:57 +00003851 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003852 bool generate_volatile = field_info.IsVolatile()
3853 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003854 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003855 bool needs_write_barrier =
3856 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003857 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003858 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003859 if (needs_write_barrier) {
3860 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003861 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003862 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003863 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003864 // - registers need to be consecutive
3865 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003866 // We don't test for ARM yet, and the assertion makes sure that we
3867 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003868 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3869
3870 locations->AddTemp(Location::RequiresRegister());
3871 locations->AddTemp(Location::RequiresRegister());
3872 if (field_type == Primitive::kPrimDouble) {
3873 // For doubles we need two more registers to copy the value.
3874 locations->AddTemp(Location::RegisterLocation(R2));
3875 locations->AddTemp(Location::RegisterLocation(R3));
3876 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003877 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003878}
3879
Calin Juravle52c48962014-12-16 17:02:57 +00003880void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003881 const FieldInfo& field_info,
3882 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003883 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3884
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003885 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003886 Register base = locations->InAt(0).AsRegister<Register>();
3887 Location value = locations->InAt(1);
3888
3889 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003890 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003891 Primitive::Type field_type = field_info.GetFieldType();
3892 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003893 bool needs_write_barrier =
3894 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003895
3896 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003897 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003898 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003899
3900 switch (field_type) {
3901 case Primitive::kPrimBoolean:
3902 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003903 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003904 break;
3905 }
3906
3907 case Primitive::kPrimShort:
3908 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003909 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003910 break;
3911 }
3912
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003914 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003915 if (kPoisonHeapReferences && needs_write_barrier) {
3916 // Note that in the case where `value` is a null reference,
3917 // we do not enter this block, as a null reference does not
3918 // need poisoning.
3919 DCHECK_EQ(field_type, Primitive::kPrimNot);
3920 Register temp = locations->GetTemp(0).AsRegister<Register>();
3921 __ Mov(temp, value.AsRegister<Register>());
3922 __ PoisonHeapReference(temp);
3923 __ StoreToOffset(kStoreWord, temp, base, offset);
3924 } else {
3925 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3926 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003927 break;
3928 }
3929
3930 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003931 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003932 GenerateWideAtomicStore(base, offset,
3933 value.AsRegisterPairLow<Register>(),
3934 value.AsRegisterPairHigh<Register>(),
3935 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003936 locations->GetTemp(1).AsRegister<Register>(),
3937 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003938 } else {
3939 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003940 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003941 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003942 break;
3943 }
3944
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003945 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003946 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003947 break;
3948 }
3949
3950 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003951 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003952 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003953 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3954 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3955
3956 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3957
3958 GenerateWideAtomicStore(base, offset,
3959 value_reg_lo,
3960 value_reg_hi,
3961 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003962 locations->GetTemp(3).AsRegister<Register>(),
3963 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003964 } else {
3965 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003966 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003967 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003968 break;
3969 }
3970
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971 case Primitive::kPrimVoid:
3972 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003973 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003974 }
Calin Juravle52c48962014-12-16 17:02:57 +00003975
Calin Juravle77520bc2015-01-12 18:45:46 +00003976 // Longs and doubles are handled in the switch.
3977 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3978 codegen_->MaybeRecordImplicitNullCheck(instruction);
3979 }
3980
3981 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3982 Register temp = locations->GetTemp(0).AsRegister<Register>();
3983 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003984 codegen_->MarkGCCard(
3985 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003986 }
3987
Calin Juravle52c48962014-12-16 17:02:57 +00003988 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003989 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003990 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003991}
3992
Calin Juravle52c48962014-12-16 17:02:57 +00003993void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3994 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003995
3996 bool object_field_get_with_read_barrier =
3997 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003998 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003999 new (GetGraph()->GetArena()) LocationSummary(instruction,
4000 object_field_get_with_read_barrier ?
4001 LocationSummary::kCallOnSlowPath :
4002 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004003 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4004 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4005 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004006 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004007
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004008 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004009 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004010 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004011 // The output overlaps in case of volatile long: we don't want the
4012 // code generated by GenerateWideAtomicLoad to overwrite the
4013 // object's location. Likewise, in the case of an object field get
4014 // with read barriers enabled, we do not want the load to overwrite
4015 // the object's location, as we need it to emit the read barrier.
4016 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4017 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004018
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004019 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4020 locations->SetOut(Location::RequiresFpuRegister());
4021 } else {
4022 locations->SetOut(Location::RequiresRegister(),
4023 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4024 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004025 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004026 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004027 // - registers need to be consecutive
4028 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004029 // We don't test for ARM yet, and the assertion makes sure that we
4030 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004031 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4032 locations->AddTemp(Location::RequiresRegister());
4033 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004034 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4035 // We need a temporary register for the read barrier marking slow
4036 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4037 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004038 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004039}
4040
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004041Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4042 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4043 << input->GetType();
4044 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4045 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4046 return Location::ConstantLocation(input->AsConstant());
4047 } else {
4048 return Location::RequiresFpuRegister();
4049 }
4050}
4051
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004052Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4053 Opcode opcode) {
4054 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4055 if (constant->IsConstant() &&
4056 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4057 return Location::ConstantLocation(constant->AsConstant());
4058 }
4059 return Location::RequiresRegister();
4060}
4061
4062bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4063 Opcode opcode) {
4064 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4065 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004066 Opcode high_opcode = opcode;
4067 SetCc low_set_cc = kCcDontCare;
4068 switch (opcode) {
4069 case SUB:
4070 // Flip the operation to an ADD.
4071 value = -value;
4072 opcode = ADD;
4073 FALLTHROUGH_INTENDED;
4074 case ADD:
4075 if (Low32Bits(value) == 0u) {
4076 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4077 }
4078 high_opcode = ADC;
4079 low_set_cc = kCcSet;
4080 break;
4081 default:
4082 break;
4083 }
4084 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4085 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004086 } else {
4087 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4088 }
4089}
4090
Vladimir Marko59751a72016-08-05 14:37:27 +01004091bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4092 Opcode opcode,
4093 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004094 ShifterOperand so;
4095 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004096 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004097 return true;
4098 }
4099 Opcode neg_opcode = kNoOperand;
4100 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004101 case AND: neg_opcode = BIC; value = ~value; break;
4102 case ORR: neg_opcode = ORN; value = ~value; break;
4103 case ADD: neg_opcode = SUB; value = -value; break;
4104 case ADC: neg_opcode = SBC; value = ~value; break;
4105 case SUB: neg_opcode = ADD; value = -value; break;
4106 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004107 default:
4108 return false;
4109 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004110 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004111}
4112
Calin Juravle52c48962014-12-16 17:02:57 +00004113void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4114 const FieldInfo& field_info) {
4115 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004116
Calin Juravle52c48962014-12-16 17:02:57 +00004117 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004118 Location base_loc = locations->InAt(0);
4119 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004120 Location out = locations->Out();
4121 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004122 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004123 Primitive::Type field_type = field_info.GetFieldType();
4124 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4125
4126 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004127 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004128 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004129 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004130
Roland Levillainc9285912015-12-18 10:38:42 +00004131 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004132 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004133 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004134
Roland Levillainc9285912015-12-18 10:38:42 +00004135 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004136 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004137 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004138
Roland Levillainc9285912015-12-18 10:38:42 +00004139 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004140 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004141 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004142
4143 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004144 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004145 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004146
4147 case Primitive::kPrimNot: {
4148 // /* HeapReference<Object> */ out = *(base + offset)
4149 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4150 Location temp_loc = locations->GetTemp(0);
4151 // Note that a potential implicit null check is handled in this
4152 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4153 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4154 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4155 if (is_volatile) {
4156 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4157 }
4158 } else {
4159 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4160 codegen_->MaybeRecordImplicitNullCheck(instruction);
4161 if (is_volatile) {
4162 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4163 }
4164 // If read barriers are enabled, emit read barriers other than
4165 // Baker's using a slow path (and also unpoison the loaded
4166 // reference, if heap poisoning is enabled).
4167 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4168 }
4169 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004170 }
4171
Roland Levillainc9285912015-12-18 10:38:42 +00004172 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004173 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004174 GenerateWideAtomicLoad(base, offset,
4175 out.AsRegisterPairLow<Register>(),
4176 out.AsRegisterPairHigh<Register>());
4177 } else {
4178 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4179 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004180 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004181
Roland Levillainc9285912015-12-18 10:38:42 +00004182 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004183 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004184 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004185
4186 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004187 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004188 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004189 Register lo = locations->GetTemp(0).AsRegister<Register>();
4190 Register hi = locations->GetTemp(1).AsRegister<Register>();
4191 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004192 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004193 __ vmovdrr(out_reg, lo, hi);
4194 } else {
4195 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004196 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004197 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004198 break;
4199 }
4200
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004201 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004202 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004203 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004204 }
Calin Juravle52c48962014-12-16 17:02:57 +00004205
Roland Levillainc9285912015-12-18 10:38:42 +00004206 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4207 // Potential implicit null checks, in the case of reference or
4208 // double fields, are handled in the previous switch statement.
4209 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004210 codegen_->MaybeRecordImplicitNullCheck(instruction);
4211 }
4212
Calin Juravle52c48962014-12-16 17:02:57 +00004213 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004214 if (field_type == Primitive::kPrimNot) {
4215 // Memory barriers, in the case of references, are also handled
4216 // in the previous switch statement.
4217 } else {
4218 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4219 }
Roland Levillain4d027112015-07-01 15:41:14 +01004220 }
Calin Juravle52c48962014-12-16 17:02:57 +00004221}
4222
4223void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4224 HandleFieldSet(instruction, instruction->GetFieldInfo());
4225}
4226
4227void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004228 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004229}
4230
4231void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4232 HandleFieldGet(instruction, instruction->GetFieldInfo());
4233}
4234
4235void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4236 HandleFieldGet(instruction, instruction->GetFieldInfo());
4237}
4238
4239void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4240 HandleFieldGet(instruction, instruction->GetFieldInfo());
4241}
4242
4243void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4244 HandleFieldGet(instruction, instruction->GetFieldInfo());
4245}
4246
4247void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4248 HandleFieldSet(instruction, instruction->GetFieldInfo());
4249}
4250
4251void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004252 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253}
4254
Calin Juravlee460d1d2015-09-29 04:52:17 +01004255void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4256 HUnresolvedInstanceFieldGet* instruction) {
4257 FieldAccessCallingConventionARM calling_convention;
4258 codegen_->CreateUnresolvedFieldLocationSummary(
4259 instruction, instruction->GetFieldType(), calling_convention);
4260}
4261
4262void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4263 HUnresolvedInstanceFieldGet* instruction) {
4264 FieldAccessCallingConventionARM calling_convention;
4265 codegen_->GenerateUnresolvedFieldAccess(instruction,
4266 instruction->GetFieldType(),
4267 instruction->GetFieldIndex(),
4268 instruction->GetDexPc(),
4269 calling_convention);
4270}
4271
4272void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4273 HUnresolvedInstanceFieldSet* instruction) {
4274 FieldAccessCallingConventionARM calling_convention;
4275 codegen_->CreateUnresolvedFieldLocationSummary(
4276 instruction, instruction->GetFieldType(), calling_convention);
4277}
4278
4279void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4280 HUnresolvedInstanceFieldSet* instruction) {
4281 FieldAccessCallingConventionARM calling_convention;
4282 codegen_->GenerateUnresolvedFieldAccess(instruction,
4283 instruction->GetFieldType(),
4284 instruction->GetFieldIndex(),
4285 instruction->GetDexPc(),
4286 calling_convention);
4287}
4288
4289void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4290 HUnresolvedStaticFieldGet* instruction) {
4291 FieldAccessCallingConventionARM calling_convention;
4292 codegen_->CreateUnresolvedFieldLocationSummary(
4293 instruction, instruction->GetFieldType(), calling_convention);
4294}
4295
4296void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4297 HUnresolvedStaticFieldGet* instruction) {
4298 FieldAccessCallingConventionARM calling_convention;
4299 codegen_->GenerateUnresolvedFieldAccess(instruction,
4300 instruction->GetFieldType(),
4301 instruction->GetFieldIndex(),
4302 instruction->GetDexPc(),
4303 calling_convention);
4304}
4305
4306void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4307 HUnresolvedStaticFieldSet* instruction) {
4308 FieldAccessCallingConventionARM calling_convention;
4309 codegen_->CreateUnresolvedFieldLocationSummary(
4310 instruction, instruction->GetFieldType(), calling_convention);
4311}
4312
4313void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4314 HUnresolvedStaticFieldSet* instruction) {
4315 FieldAccessCallingConventionARM calling_convention;
4316 codegen_->GenerateUnresolvedFieldAccess(instruction,
4317 instruction->GetFieldType(),
4318 instruction->GetFieldIndex(),
4319 instruction->GetDexPc(),
4320 calling_convention);
4321}
4322
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00004324 codegen_->CreateNullCheckLocations(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004325}
4326
Calin Juravle2ae48182016-03-16 14:05:09 +00004327void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4328 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004329 return;
4330 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004331 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004332
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004333 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004334 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004335}
4336
Calin Juravle2ae48182016-03-16 14:05:09 +00004337void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004338 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004339 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004340
4341 LocationSummary* locations = instruction->GetLocations();
4342 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004343
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004344 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004345}
4346
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004347void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004348 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004349}
4350
Artem Serov6c916792016-07-11 14:02:34 +01004351static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4352 switch (type) {
4353 case Primitive::kPrimNot:
4354 return kLoadWord;
4355 case Primitive::kPrimBoolean:
4356 return kLoadUnsignedByte;
4357 case Primitive::kPrimByte:
4358 return kLoadSignedByte;
4359 case Primitive::kPrimChar:
4360 return kLoadUnsignedHalfword;
4361 case Primitive::kPrimShort:
4362 return kLoadSignedHalfword;
4363 case Primitive::kPrimInt:
4364 return kLoadWord;
4365 case Primitive::kPrimLong:
4366 return kLoadWordPair;
4367 case Primitive::kPrimFloat:
4368 return kLoadSWord;
4369 case Primitive::kPrimDouble:
4370 return kLoadDWord;
4371 default:
4372 LOG(FATAL) << "Unreachable type " << type;
4373 UNREACHABLE();
4374 }
4375}
4376
4377static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4378 switch (type) {
4379 case Primitive::kPrimNot:
4380 return kStoreWord;
4381 case Primitive::kPrimBoolean:
4382 case Primitive::kPrimByte:
4383 return kStoreByte;
4384 case Primitive::kPrimChar:
4385 case Primitive::kPrimShort:
4386 return kStoreHalfword;
4387 case Primitive::kPrimInt:
4388 return kStoreWord;
4389 case Primitive::kPrimLong:
4390 return kStoreWordPair;
4391 case Primitive::kPrimFloat:
4392 return kStoreSWord;
4393 case Primitive::kPrimDouble:
4394 return kStoreDWord;
4395 default:
4396 LOG(FATAL) << "Unreachable type " << type;
4397 UNREACHABLE();
4398 }
4399}
4400
4401void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4402 Location out_loc,
4403 Register base,
4404 Register reg_offset,
4405 Condition cond) {
4406 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4407 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4408
4409 switch (type) {
4410 case Primitive::kPrimByte:
4411 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4412 break;
4413 case Primitive::kPrimBoolean:
4414 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4415 break;
4416 case Primitive::kPrimShort:
4417 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4418 break;
4419 case Primitive::kPrimChar:
4420 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4421 break;
4422 case Primitive::kPrimNot:
4423 case Primitive::kPrimInt:
4424 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4425 break;
4426 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4427 case Primitive::kPrimLong:
4428 case Primitive::kPrimFloat:
4429 case Primitive::kPrimDouble:
4430 default:
4431 LOG(FATAL) << "Unreachable type " << type;
4432 UNREACHABLE();
4433 }
4434}
4435
4436void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4437 Location loc,
4438 Register base,
4439 Register reg_offset,
4440 Condition cond) {
4441 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4442 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4443
4444 switch (type) {
4445 case Primitive::kPrimByte:
4446 case Primitive::kPrimBoolean:
4447 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4448 break;
4449 case Primitive::kPrimShort:
4450 case Primitive::kPrimChar:
4451 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4452 break;
4453 case Primitive::kPrimNot:
4454 case Primitive::kPrimInt:
4455 __ str(loc.AsRegister<Register>(), mem_address, cond);
4456 break;
4457 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4458 case Primitive::kPrimLong:
4459 case Primitive::kPrimFloat:
4460 case Primitive::kPrimDouble:
4461 default:
4462 LOG(FATAL) << "Unreachable type " << type;
4463 UNREACHABLE();
4464 }
4465}
4466
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004467void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004468 bool object_array_get_with_read_barrier =
4469 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004470 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004471 new (GetGraph()->GetArena()) LocationSummary(instruction,
4472 object_array_get_with_read_barrier ?
4473 LocationSummary::kCallOnSlowPath :
4474 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004475 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4476 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4477 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004478 locations->SetInAt(0, Location::RequiresRegister());
4479 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004480 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4481 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4482 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004483 // The output overlaps in the case of an object array get with
4484 // read barriers enabled: we do not want the move to overwrite the
4485 // array's location, as we need it to emit the read barrier.
4486 locations->SetOut(
4487 Location::RequiresRegister(),
4488 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004489 }
Roland Levillainc9285912015-12-18 10:38:42 +00004490 // We need a temporary register for the read barrier marking slow
4491 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4492 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4493 locations->AddTemp(Location::RequiresRegister());
4494 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004495}
4496
4497void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4498 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004499 Location obj_loc = locations->InAt(0);
4500 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004501 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004502 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004503 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004504 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004505 HInstruction* array_instr = instruction->GetArray();
4506 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004507 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4508 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004509
Roland Levillain4d027112015-07-01 15:41:14 +01004510 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004511 case Primitive::kPrimBoolean:
4512 case Primitive::kPrimByte:
4513 case Primitive::kPrimShort:
4514 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004515 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004516 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004517 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4518 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4519
4520 LoadOperandType load_type = GetLoadOperandType(type);
4521 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004522 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004523 Register temp = IP;
4524
4525 if (has_intermediate_address) {
4526 // We do not need to compute the intermediate address from the array: the
4527 // input instruction has done it already. See the comment in
4528 // `TryExtractArrayAccessAddress()`.
4529 if (kIsDebugBuild) {
4530 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4531 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4532 }
4533 temp = obj;
4534 } else {
4535 __ add(temp, obj, ShifterOperand(data_offset));
4536 }
4537 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 }
4539 break;
4540 }
4541
Roland Levillainc9285912015-12-18 10:38:42 +00004542 case Primitive::kPrimNot: {
4543 static_assert(
4544 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4545 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004546 // /* HeapReference<Object> */ out =
4547 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4548 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4549 Location temp = locations->GetTemp(0);
4550 // Note that a potential implicit null check is handled in this
4551 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4552 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4553 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4554 } else {
4555 Register out = out_loc.AsRegister<Register>();
4556 if (index.IsConstant()) {
4557 size_t offset =
4558 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4559 __ LoadFromOffset(kLoadWord, out, obj, offset);
4560 codegen_->MaybeRecordImplicitNullCheck(instruction);
4561 // If read barriers are enabled, emit read barriers other than
4562 // Baker's using a slow path (and also unpoison the loaded
4563 // reference, if heap poisoning is enabled).
4564 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4565 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004566 Register temp = IP;
4567
4568 if (has_intermediate_address) {
4569 // We do not need to compute the intermediate address from the array: the
4570 // input instruction has done it already. See the comment in
4571 // `TryExtractArrayAccessAddress()`.
4572 if (kIsDebugBuild) {
4573 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4574 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4575 }
4576 temp = obj;
4577 } else {
4578 __ add(temp, obj, ShifterOperand(data_offset));
4579 }
4580 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004581
Roland Levillainc9285912015-12-18 10:38:42 +00004582 codegen_->MaybeRecordImplicitNullCheck(instruction);
4583 // If read barriers are enabled, emit read barriers other than
4584 // Baker's using a slow path (and also unpoison the loaded
4585 // reference, if heap poisoning is enabled).
4586 codegen_->MaybeGenerateReadBarrierSlow(
4587 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4588 }
4589 }
4590 break;
4591 }
4592
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004593 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004595 size_t offset =
4596 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004597 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004599 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004600 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 }
4602 break;
4603 }
4604
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004605 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004606 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004607 if (index.IsConstant()) {
4608 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004609 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004610 } else {
4611 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004612 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004613 }
4614 break;
4615 }
4616
4617 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004618 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004619 if (index.IsConstant()) {
4620 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004621 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004622 } else {
4623 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004624 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004625 }
4626 break;
4627 }
4628
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004630 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004631 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 }
Roland Levillain4d027112015-07-01 15:41:14 +01004633
4634 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004635 // Potential implicit null checks, in the case of reference
4636 // arrays, are handled in the previous switch statement.
4637 } else {
4638 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004639 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640}
4641
4642void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004643 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004644
4645 bool needs_write_barrier =
4646 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004647 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004648
Nicolas Geoffray39468442014-09-02 15:17:15 +01004649 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004650 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004651 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004652 LocationSummary::kCallOnSlowPath :
4653 LocationSummary::kNoCall);
4654
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004655 locations->SetInAt(0, Location::RequiresRegister());
4656 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4657 if (Primitive::IsFloatingPointType(value_type)) {
4658 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004659 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004660 locations->SetInAt(2, Location::RequiresRegister());
4661 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004662 if (needs_write_barrier) {
4663 // Temporary registers for the write barrier.
4664 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004665 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004666 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004667}
4668
4669void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4670 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004671 Location array_loc = locations->InAt(0);
4672 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004673 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004674 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004675 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004676 bool needs_write_barrier =
4677 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004678 uint32_t data_offset =
4679 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4680 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004681 HInstruction* array_instr = instruction->GetArray();
4682 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004683 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4684 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004685
4686 switch (value_type) {
4687 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004688 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004690 case Primitive::kPrimChar:
4691 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004692 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004693 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4694 uint32_t full_offset =
4695 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4696 StoreOperandType store_type = GetStoreOperandType(value_type);
4697 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004699 Register temp = IP;
4700
4701 if (has_intermediate_address) {
4702 // We do not need to compute the intermediate address from the array: the
4703 // input instruction has done it already. See the comment in
4704 // `TryExtractArrayAccessAddress()`.
4705 if (kIsDebugBuild) {
4706 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4707 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4708 }
4709 temp = array;
4710 } else {
4711 __ add(temp, array, ShifterOperand(data_offset));
4712 }
Artem Serov6c916792016-07-11 14:02:34 +01004713 codegen_->StoreToShiftedRegOffset(value_type,
4714 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004715 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004716 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004717 }
4718 break;
4719 }
4720
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004721 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004722 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004723 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4724 // See the comment in instruction_simplifier_shared.cc.
4725 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004726
4727 if (instruction->InputAt(2)->IsNullConstant()) {
4728 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004729 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004730 size_t offset =
4731 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004732 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004733 } else {
4734 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004735 __ add(IP, array, ShifterOperand(data_offset));
4736 codegen_->StoreToShiftedRegOffset(value_type,
4737 value_loc,
4738 IP,
4739 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004740 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004741 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004742 DCHECK(!needs_write_barrier);
4743 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004744 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004745 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004746
4747 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01004748 Location temp1_loc = locations->GetTemp(0);
4749 Register temp1 = temp1_loc.AsRegister<Register>();
4750 Location temp2_loc = locations->GetTemp(1);
4751 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004752 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4753 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4754 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4755 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01004756 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004757
Roland Levillain3b359c72015-11-17 19:35:12 +00004758 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4760 codegen_->AddSlowPath(slow_path);
4761 if (instruction->GetValueCanBeNull()) {
4762 Label non_zero;
4763 __ CompareAndBranchIfNonZero(value, &non_zero);
4764 if (index.IsConstant()) {
4765 size_t offset =
4766 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4767 __ StoreToOffset(kStoreWord, value, array, offset);
4768 } else {
4769 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004770 __ add(IP, array, ShifterOperand(data_offset));
4771 codegen_->StoreToShiftedRegOffset(value_type,
4772 value_loc,
4773 IP,
4774 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004775 }
4776 codegen_->MaybeRecordImplicitNullCheck(instruction);
4777 __ b(&done);
4778 __ Bind(&non_zero);
4779 }
4780
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004781 // Note that when read barriers are enabled, the type checks
4782 // are performed without read barriers. This is fine, even in
4783 // the case where a class object is in the from-space after
4784 // the flip, as a comparison involving such a type would not
4785 // produce a false positive; it may of course produce a false
4786 // negative, in which case we would take the ArraySet slow
4787 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004788
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004789 // /* HeapReference<Class> */ temp1 = array->klass_
4790 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4791 codegen_->MaybeRecordImplicitNullCheck(instruction);
4792 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01004793
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004794 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4795 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4796 // /* HeapReference<Class> */ temp2 = value->klass_
4797 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4798 // If heap poisoning is enabled, no need to unpoison `temp1`
4799 // nor `temp2`, as we are comparing two poisoned references.
4800 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01004801
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004802 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4803 Label do_put;
4804 __ b(&do_put, EQ);
4805 // If heap poisoning is enabled, the `temp1` reference has
4806 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00004807 __ MaybeUnpoisonHeapReference(temp1);
4808
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004809 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4810 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4811 // If heap poisoning is enabled, no need to unpoison
4812 // `temp1`, as we are comparing against null below.
4813 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4814 __ Bind(&do_put);
4815 } else {
4816 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004817 }
4818 }
4819
Artem Serov6c916792016-07-11 14:02:34 +01004820 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004821 if (kPoisonHeapReferences) {
4822 // Note that in the case where `value` is a null reference,
4823 // we do not enter this block, as a null reference does not
4824 // need poisoning.
4825 DCHECK_EQ(value_type, Primitive::kPrimNot);
4826 __ Mov(temp1, value);
4827 __ PoisonHeapReference(temp1);
4828 source = temp1;
4829 }
4830
4831 if (index.IsConstant()) {
4832 size_t offset =
4833 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4834 __ StoreToOffset(kStoreWord, source, array, offset);
4835 } else {
4836 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004837
4838 __ add(IP, array, ShifterOperand(data_offset));
4839 codegen_->StoreToShiftedRegOffset(value_type,
4840 Location::RegisterLocation(source),
4841 IP,
4842 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004843 }
4844
Roland Levillain3b359c72015-11-17 19:35:12 +00004845 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 codegen_->MaybeRecordImplicitNullCheck(instruction);
4847 }
4848
4849 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4850
4851 if (done.IsLinked()) {
4852 __ Bind(&done);
4853 }
4854
4855 if (slow_path != nullptr) {
4856 __ Bind(slow_path->GetExitLabel());
4857 }
4858
4859 break;
4860 }
4861
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004863 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004865 size_t offset =
4866 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004867 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004868 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004869 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004870 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004871 }
4872 break;
4873 }
4874
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004875 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004876 Location value = locations->InAt(2);
4877 DCHECK(value.IsFpuRegister());
4878 if (index.IsConstant()) {
4879 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004880 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004881 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004882 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004883 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4884 }
4885 break;
4886 }
4887
4888 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004889 Location value = locations->InAt(2);
4890 DCHECK(value.IsFpuRegisterPair());
4891 if (index.IsConstant()) {
4892 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004893 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004894 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004895 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004896 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4897 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004898
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004899 break;
4900 }
4901
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004902 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004903 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004904 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004906
Roland Levillain80e67092016-01-08 16:04:55 +00004907 // Objects are handled in the switch.
4908 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004909 codegen_->MaybeRecordImplicitNullCheck(instruction);
4910 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004911}
4912
4913void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004914 LocationSummary* locations =
4915 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004916 locations->SetInAt(0, Location::RequiresRegister());
4917 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004918}
4919
4920void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4921 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004922 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004923 Register obj = locations->InAt(0).AsRegister<Register>();
4924 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004926 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004927}
4928
Artem Serov328429f2016-07-06 16:23:04 +01004929void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00004930 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4931 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01004932 LocationSummary* locations =
4933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4934
4935 locations->SetInAt(0, Location::RequiresRegister());
4936 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4937 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4938}
4939
4940void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4941 LocationSummary* locations = instruction->GetLocations();
4942 Location out = locations->Out();
4943 Location first = locations->InAt(0);
4944 Location second = locations->InAt(1);
4945
Roland Levillain4a3aa572016-08-15 13:17:06 +00004946 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4947 DCHECK(!kEmitCompilerReadBarrier);
4948
Artem Serov328429f2016-07-06 16:23:04 +01004949 if (second.IsRegister()) {
4950 __ add(out.AsRegister<Register>(),
4951 first.AsRegister<Register>(),
4952 ShifterOperand(second.AsRegister<Register>()));
4953 } else {
4954 __ AddConstant(out.AsRegister<Register>(),
4955 first.AsRegister<Register>(),
4956 second.GetConstant()->AsIntConstant()->GetValue());
4957 }
4958}
4959
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004960void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004961 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4962 ? LocationSummary::kCallOnSlowPath
4963 : LocationSummary::kNoCall;
4964 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004965 locations->SetInAt(0, Location::RequiresRegister());
4966 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004967 if (instruction->HasUses()) {
4968 locations->SetOut(Location::SameAsFirstInput());
4969 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004970}
4971
4972void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4973 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01004974 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004975 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004976 codegen_->AddSlowPath(slow_path);
4977
Roland Levillain271ab9c2014-11-27 15:23:57 +00004978 Register index = locations->InAt(0).AsRegister<Register>();
4979 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004980
4981 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004982 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004983}
4984
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004985void CodeGeneratorARM::MarkGCCard(Register temp,
4986 Register card,
4987 Register object,
4988 Register value,
4989 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004990 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004991 if (can_be_null) {
4992 __ CompareAndBranchIfZero(value, &is_null);
4993 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004994 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4996 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004997 if (can_be_null) {
4998 __ Bind(&is_null);
4999 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005000}
5001
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005002void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005003 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005004}
5005
5006void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005007 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5008}
5009
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005010void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005011 LocationSummary* locations =
5012 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5013 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005014}
5015
5016void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005017 HBasicBlock* block = instruction->GetBlock();
5018 if (block->GetLoopInformation() != nullptr) {
5019 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5020 // The back edge will generate the suspend check.
5021 return;
5022 }
5023 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5024 // The goto will generate the suspend check.
5025 return;
5026 }
5027 GenerateSuspendCheck(instruction, nullptr);
5028}
5029
5030void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5031 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005032 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005033 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5034 if (slow_path == nullptr) {
5035 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5036 instruction->SetSlowPath(slow_path);
5037 codegen_->AddSlowPath(slow_path);
5038 if (successor != nullptr) {
5039 DCHECK(successor->IsLoopHeader());
5040 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5041 }
5042 } else {
5043 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5044 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005045
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005046 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005047 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005048 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005049 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005050 __ Bind(slow_path->GetReturnLabel());
5051 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005052 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005053 __ b(slow_path->GetEntryLabel());
5054 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005055}
5056
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005057ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5058 return codegen_->GetAssembler();
5059}
5060
5061void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005062 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005063 Location source = move->GetSource();
5064 Location destination = move->GetDestination();
5065
5066 if (source.IsRegister()) {
5067 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005068 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005069 } else if (destination.IsFpuRegister()) {
5070 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005071 } else {
5072 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005073 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005074 SP, destination.GetStackIndex());
5075 }
5076 } else if (source.IsStackSlot()) {
5077 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005078 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005079 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005080 } else if (destination.IsFpuRegister()) {
5081 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005082 } else {
5083 DCHECK(destination.IsStackSlot());
5084 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5085 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5086 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005087 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005088 if (destination.IsRegister()) {
5089 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5090 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005091 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005092 } else {
5093 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005094 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5095 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005096 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005097 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005098 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5099 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005100 } else if (destination.IsRegisterPair()) {
5101 DCHECK(ExpectedPairLayout(destination));
5102 __ LoadFromOffset(
5103 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5104 } else {
5105 DCHECK(destination.IsFpuRegisterPair()) << destination;
5106 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5107 SP,
5108 source.GetStackIndex());
5109 }
5110 } else if (source.IsRegisterPair()) {
5111 if (destination.IsRegisterPair()) {
5112 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5113 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005114 } else if (destination.IsFpuRegisterPair()) {
5115 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5116 source.AsRegisterPairLow<Register>(),
5117 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005118 } else {
5119 DCHECK(destination.IsDoubleStackSlot()) << destination;
5120 DCHECK(ExpectedPairLayout(source));
5121 __ StoreToOffset(
5122 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5123 }
5124 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005125 if (destination.IsRegisterPair()) {
5126 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5127 destination.AsRegisterPairHigh<Register>(),
5128 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5129 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005130 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5131 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5132 } else {
5133 DCHECK(destination.IsDoubleStackSlot()) << destination;
5134 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5135 SP,
5136 destination.GetStackIndex());
5137 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005138 } else {
5139 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005140 HConstant* constant = source.GetConstant();
5141 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5142 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005143 if (destination.IsRegister()) {
5144 __ LoadImmediate(destination.AsRegister<Register>(), value);
5145 } else {
5146 DCHECK(destination.IsStackSlot());
5147 __ LoadImmediate(IP, value);
5148 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5149 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005150 } else if (constant->IsLongConstant()) {
5151 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005152 if (destination.IsRegisterPair()) {
5153 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5154 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005155 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005156 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005157 __ LoadImmediate(IP, Low32Bits(value));
5158 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5159 __ LoadImmediate(IP, High32Bits(value));
5160 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5161 }
5162 } else if (constant->IsDoubleConstant()) {
5163 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005164 if (destination.IsFpuRegisterPair()) {
5165 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005166 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005167 DCHECK(destination.IsDoubleStackSlot()) << destination;
5168 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005169 __ LoadImmediate(IP, Low32Bits(int_value));
5170 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5171 __ LoadImmediate(IP, High32Bits(int_value));
5172 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5173 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005174 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005175 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005176 float value = constant->AsFloatConstant()->GetValue();
5177 if (destination.IsFpuRegister()) {
5178 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5179 } else {
5180 DCHECK(destination.IsStackSlot());
5181 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5182 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5183 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005184 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005185 }
5186}
5187
5188void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5189 __ Mov(IP, reg);
5190 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5191 __ StoreToOffset(kStoreWord, IP, SP, mem);
5192}
5193
5194void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5195 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5196 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5197 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5198 SP, mem1 + stack_offset);
5199 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5200 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5201 SP, mem2 + stack_offset);
5202 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5203}
5204
5205void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005206 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005207 Location source = move->GetSource();
5208 Location destination = move->GetDestination();
5209
5210 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005211 DCHECK_NE(source.AsRegister<Register>(), IP);
5212 DCHECK_NE(destination.AsRegister<Register>(), IP);
5213 __ Mov(IP, source.AsRegister<Register>());
5214 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5215 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005216 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005217 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005218 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005219 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005220 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5221 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005222 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005223 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005224 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005225 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005226 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005227 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005228 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005229 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005230 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5231 destination.AsRegisterPairHigh<Register>(),
5232 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005233 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005234 Register low_reg = source.IsRegisterPair()
5235 ? source.AsRegisterPairLow<Register>()
5236 : destination.AsRegisterPairLow<Register>();
5237 int mem = source.IsRegisterPair()
5238 ? destination.GetStackIndex()
5239 : source.GetStackIndex();
5240 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005241 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005242 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005243 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005244 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005245 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5246 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005247 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005248 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005249 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005250 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5251 DRegister reg = source.IsFpuRegisterPair()
5252 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5253 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5254 int mem = source.IsFpuRegisterPair()
5255 ? destination.GetStackIndex()
5256 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005257 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005258 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005259 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005260 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5261 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5262 : destination.AsFpuRegister<SRegister>();
5263 int mem = source.IsFpuRegister()
5264 ? destination.GetStackIndex()
5265 : source.GetStackIndex();
5266
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005267 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005268 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005269 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005270 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005271 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5272 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005273 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005274 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005275 }
5276}
5277
5278void ParallelMoveResolverARM::SpillScratch(int reg) {
5279 __ Push(static_cast<Register>(reg));
5280}
5281
5282void ParallelMoveResolverARM::RestoreScratch(int reg) {
5283 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005284}
5285
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005286HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5287 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005288 switch (desired_class_load_kind) {
5289 case HLoadClass::LoadKind::kReferrersClass:
5290 break;
5291 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5292 DCHECK(!GetCompilerOptions().GetCompilePic());
5293 break;
5294 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5295 DCHECK(GetCompilerOptions().GetCompilePic());
5296 break;
5297 case HLoadClass::LoadKind::kBootImageAddress:
5298 break;
5299 case HLoadClass::LoadKind::kDexCacheAddress:
5300 DCHECK(Runtime::Current()->UseJitCompilation());
5301 break;
5302 case HLoadClass::LoadKind::kDexCachePcRelative:
5303 DCHECK(!Runtime::Current()->UseJitCompilation());
5304 // We disable pc-relative load when there is an irreducible loop, as the optimization
5305 // is incompatible with it.
5306 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5307 // with irreducible loops.
5308 if (GetGraph()->HasIrreducibleLoops()) {
5309 return HLoadClass::LoadKind::kDexCacheViaMethod;
5310 }
5311 break;
5312 case HLoadClass::LoadKind::kDexCacheViaMethod:
5313 break;
5314 }
5315 return desired_class_load_kind;
5316}
5317
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005318void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005319 if (cls->NeedsAccessCheck()) {
5320 InvokeRuntimeCallingConvention calling_convention;
5321 CodeGenerator::CreateLoadClassLocationSummary(
5322 cls,
5323 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5324 Location::RegisterLocation(R0),
5325 /* code_generator_supports_read_barrier */ true);
5326 return;
5327 }
5328
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005329 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5330 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005331 ? LocationSummary::kCallOnSlowPath
5332 : LocationSummary::kNoCall;
5333 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005334 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005335 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5336 }
5337
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005338 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5339 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5340 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5341 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5342 locations->SetInAt(0, Location::RequiresRegister());
5343 }
5344 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005345}
5346
5347void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005348 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005349 if (cls->NeedsAccessCheck()) {
5350 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005351 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005352 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005353 return;
5354 }
5355
Roland Levillain3b359c72015-11-17 19:35:12 +00005356 Location out_loc = locations->Out();
5357 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005358
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005359 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005360 bool generate_null_check = false;
5361 switch (cls->GetLoadKind()) {
5362 case HLoadClass::LoadKind::kReferrersClass: {
5363 DCHECK(!cls->CanCallRuntime());
5364 DCHECK(!cls->MustGenerateClinitCheck());
5365 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5366 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005367 GenerateGcRootFieldLoad(cls,
5368 out_loc,
5369 current_method,
5370 ArtMethod::DeclaringClassOffset().Int32Value(),
5371 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005372 break;
5373 }
5374 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005375 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005376 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5377 cls->GetTypeIndex()));
5378 break;
5379 }
5380 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005381 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005382 CodeGeneratorARM::PcRelativePatchInfo* labels =
5383 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5384 __ BindTrackedLabel(&labels->movw_label);
5385 __ movw(out, /* placeholder */ 0u);
5386 __ BindTrackedLabel(&labels->movt_label);
5387 __ movt(out, /* placeholder */ 0u);
5388 __ BindTrackedLabel(&labels->add_pc_label);
5389 __ add(out, out, ShifterOperand(PC));
5390 break;
5391 }
5392 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005393 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005394 DCHECK_NE(cls->GetAddress(), 0u);
5395 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5396 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5397 break;
5398 }
5399 case HLoadClass::LoadKind::kDexCacheAddress: {
5400 DCHECK_NE(cls->GetAddress(), 0u);
5401 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5402 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5403 // a 128B range. To try and reduce the number of literals if we load multiple types,
5404 // simply split the dex cache address to a 128B aligned base loaded from a literal
5405 // and the remaining offset embedded in the load.
5406 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5407 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5408 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5409 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5410 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5411 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5412 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005413 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005414 generate_null_check = !cls->IsInDexCache();
5415 break;
5416 }
5417 case HLoadClass::LoadKind::kDexCachePcRelative: {
5418 Register base_reg = locations->InAt(0).AsRegister<Register>();
5419 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5420 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5421 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005422 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005423 generate_null_check = !cls->IsInDexCache();
5424 break;
5425 }
5426 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5427 // /* GcRoot<mirror::Class>[] */ out =
5428 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5429 Register current_method = locations->InAt(0).AsRegister<Register>();
5430 __ LoadFromOffset(kLoadWord,
5431 out,
5432 current_method,
5433 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5434 // /* GcRoot<mirror::Class> */ out = out[type_index]
5435 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005436 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005437 generate_null_check = !cls->IsInDexCache();
5438 }
5439 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005440
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005441 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5442 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005443 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005444 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5445 codegen_->AddSlowPath(slow_path);
5446 if (generate_null_check) {
5447 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5448 }
5449 if (cls->MustGenerateClinitCheck()) {
5450 GenerateClassInitializationCheck(slow_path, out);
5451 } else {
5452 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005453 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005454 }
5455}
5456
5457void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5458 LocationSummary* locations =
5459 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5460 locations->SetInAt(0, Location::RequiresRegister());
5461 if (check->HasUses()) {
5462 locations->SetOut(Location::SameAsFirstInput());
5463 }
5464}
5465
5466void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005467 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005468 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005469 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005470 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005471 GenerateClassInitializationCheck(slow_path,
5472 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005473}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005474
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005475void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005476 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005477 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5478 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5479 __ b(slow_path->GetEntryLabel(), LT);
5480 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5481 // properly. Therefore, we do a memory fence.
5482 __ dmb(ISH);
5483 __ Bind(slow_path->GetExitLabel());
5484}
5485
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005486HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5487 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005488 switch (desired_string_load_kind) {
5489 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5490 DCHECK(!GetCompilerOptions().GetCompilePic());
5491 break;
5492 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5493 DCHECK(GetCompilerOptions().GetCompilePic());
5494 break;
5495 case HLoadString::LoadKind::kBootImageAddress:
5496 break;
5497 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005498 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005499 break;
5500 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005501 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005502 // We disable pc-relative load when there is an irreducible loop, as the optimization
5503 // is incompatible with it.
5504 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5505 // with irreducible loops.
5506 if (GetGraph()->HasIrreducibleLoops()) {
5507 return HLoadString::LoadKind::kDexCacheViaMethod;
5508 }
5509 break;
5510 case HLoadString::LoadKind::kDexCacheViaMethod:
5511 break;
5512 }
5513 return desired_string_load_kind;
5514}
5515
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005516void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005517 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5518 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005519 : LocationSummary::kNoCall;
5520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005521
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005522 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005523 DCHECK(load_kind != HLoadString::LoadKind::kDexCachePcRelative) << "Not supported";
5524 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005525 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005526 locations->SetOut(Location::RegisterLocation(R0));
5527 } else {
5528 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005529 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005530}
5531
5532void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005533 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005534 Location out_loc = locations->Out();
5535 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005536 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005537
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005538 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005539 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005540 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5541 load->GetStringIndex()));
5542 return; // No dex cache slow path.
5543 }
5544 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005545 CodeGeneratorARM::PcRelativePatchInfo* labels =
5546 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5547 __ BindTrackedLabel(&labels->movw_label);
5548 __ movw(out, /* placeholder */ 0u);
5549 __ BindTrackedLabel(&labels->movt_label);
5550 __ movt(out, /* placeholder */ 0u);
5551 __ BindTrackedLabel(&labels->add_pc_label);
5552 __ add(out, out, ShifterOperand(PC));
5553 return; // No dex cache slow path.
5554 }
5555 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005556 DCHECK_NE(load->GetAddress(), 0u);
5557 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5558 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5559 return; // No dex cache slow path.
5560 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005561 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005562 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005563 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005564
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005565 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5566 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5567 InvokeRuntimeCallingConvention calling_convention;
5568 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5569 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5570 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005571}
5572
David Brazdilcb1c0552015-08-04 16:22:25 +01005573static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005574 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005575}
5576
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005577void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5578 LocationSummary* locations =
5579 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5580 locations->SetOut(Location::RequiresRegister());
5581}
5582
5583void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005584 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005585 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5586}
5587
5588void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5589 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5590}
5591
5592void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005593 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005594 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005595}
5596
5597void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5598 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005599 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005600 InvokeRuntimeCallingConvention calling_convention;
5601 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5602}
5603
5604void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005605 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005606 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005607}
5608
Roland Levillainc9285912015-12-18 10:38:42 +00005609static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5610 return kEmitCompilerReadBarrier &&
5611 (kUseBakerReadBarrier ||
5612 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5613 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5614 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5615}
5616
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005617void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005618 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005619 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005620 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005621 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005622 case TypeCheckKind::kExactCheck:
5623 case TypeCheckKind::kAbstractClassCheck:
5624 case TypeCheckKind::kClassHierarchyCheck:
5625 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005626 call_kind =
5627 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005628 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005629 break;
5630 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005631 case TypeCheckKind::kUnresolvedCheck:
5632 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005633 call_kind = LocationSummary::kCallOnSlowPath;
5634 break;
5635 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005636
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005637 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005638 if (baker_read_barrier_slow_path) {
5639 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5640 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005641 locations->SetInAt(0, Location::RequiresRegister());
5642 locations->SetInAt(1, Location::RequiresRegister());
5643 // The "out" register is used as a temporary, so it overlaps with the inputs.
5644 // Note that TypeCheckSlowPathARM uses this register too.
5645 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5646 // When read barriers are enabled, we need a temporary register for
5647 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005648 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005649 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005650 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005651}
5652
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005653void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005654 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005655 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005656 Location obj_loc = locations->InAt(0);
5657 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005658 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005659 Location out_loc = locations->Out();
5660 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005661 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005662 locations->GetTemp(0) :
5663 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005664 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005665 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5666 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5667 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005668 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005669 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005670
5671 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005672 // avoid null check if we know obj is not null.
5673 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005674 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005675 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676
Roland Levillain3b359c72015-11-17 19:35:12 +00005677 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005678 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005679
Roland Levillainc9285912015-12-18 10:38:42 +00005680 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 case TypeCheckKind::kExactCheck: {
5682 __ cmp(out, ShifterOperand(cls));
5683 // Classes must be equal for the instanceof to succeed.
5684 __ b(&zero, NE);
5685 __ LoadImmediate(out, 1);
5686 __ b(&done);
5687 break;
5688 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005689
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005690 case TypeCheckKind::kAbstractClassCheck: {
5691 // If the class is abstract, we eagerly fetch the super class of the
5692 // object to avoid doing a comparison we know will fail.
5693 Label loop;
5694 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005695 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005696 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005697 // If `out` is null, we use it for the result, and jump to `done`.
5698 __ CompareAndBranchIfZero(out, &done);
5699 __ cmp(out, ShifterOperand(cls));
5700 __ b(&loop, NE);
5701 __ LoadImmediate(out, 1);
5702 if (zero.IsLinked()) {
5703 __ b(&done);
5704 }
5705 break;
5706 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005707
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005708 case TypeCheckKind::kClassHierarchyCheck: {
5709 // Walk over the class hierarchy to find a match.
5710 Label loop, success;
5711 __ Bind(&loop);
5712 __ cmp(out, ShifterOperand(cls));
5713 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005714 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005715 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005716 __ CompareAndBranchIfNonZero(out, &loop);
5717 // If `out` is null, we use it for the result, and jump to `done`.
5718 __ b(&done);
5719 __ Bind(&success);
5720 __ LoadImmediate(out, 1);
5721 if (zero.IsLinked()) {
5722 __ b(&done);
5723 }
5724 break;
5725 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005726
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005728 // Do an exact check.
5729 Label exact_check;
5730 __ cmp(out, ShifterOperand(cls));
5731 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005732 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005733 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005734 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005735 // If `out` is null, we use it for the result, and jump to `done`.
5736 __ CompareAndBranchIfZero(out, &done);
5737 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5738 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5739 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005740 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005741 __ LoadImmediate(out, 1);
5742 __ b(&done);
5743 break;
5744 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005745
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005746 case TypeCheckKind::kArrayCheck: {
5747 __ cmp(out, ShifterOperand(cls));
5748 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005749 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5750 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005751 codegen_->AddSlowPath(slow_path);
5752 __ b(slow_path->GetEntryLabel(), NE);
5753 __ LoadImmediate(out, 1);
5754 if (zero.IsLinked()) {
5755 __ b(&done);
5756 }
5757 break;
5758 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005759
Calin Juravle98893e12015-10-02 21:05:03 +01005760 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005761 case TypeCheckKind::kInterfaceCheck: {
5762 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005763 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005764 // cases.
5765 //
5766 // We cannot directly call the InstanceofNonTrivial runtime
5767 // entry point without resorting to a type checking slow path
5768 // here (i.e. by calling InvokeRuntime directly), as it would
5769 // require to assign fixed registers for the inputs of this
5770 // HInstanceOf instruction (following the runtime calling
5771 // convention), which might be cluttered by the potential first
5772 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005773 //
5774 // TODO: Introduce a new runtime entry point taking the object
5775 // to test (instead of its class) as argument, and let it deal
5776 // with the read barrier issues. This will let us refactor this
5777 // case of the `switch` code as it was previously (with a direct
5778 // call to the runtime not using a type checking slow path).
5779 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005780 DCHECK(locations->OnlyCallsOnSlowPath());
5781 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5782 /* is_fatal */ false);
5783 codegen_->AddSlowPath(slow_path);
5784 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005785 if (zero.IsLinked()) {
5786 __ b(&done);
5787 }
5788 break;
5789 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005790 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005791
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005792 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005793 __ Bind(&zero);
5794 __ LoadImmediate(out, 0);
5795 }
5796
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 if (done.IsLinked()) {
5798 __ Bind(&done);
5799 }
5800
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005801 if (slow_path != nullptr) {
5802 __ Bind(slow_path->GetExitLabel());
5803 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005804}
5805
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005806void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5808 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5809
Roland Levillain3b359c72015-11-17 19:35:12 +00005810 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5811 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 case TypeCheckKind::kExactCheck:
5813 case TypeCheckKind::kAbstractClassCheck:
5814 case TypeCheckKind::kClassHierarchyCheck:
5815 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005816 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5817 LocationSummary::kCallOnSlowPath :
5818 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 break;
5820 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005821 case TypeCheckKind::kUnresolvedCheck:
5822 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005823 call_kind = LocationSummary::kCallOnSlowPath;
5824 break;
5825 }
5826
Roland Levillain3b359c72015-11-17 19:35:12 +00005827 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5828 locations->SetInAt(0, Location::RequiresRegister());
5829 locations->SetInAt(1, Location::RequiresRegister());
5830 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5831 locations->AddTemp(Location::RequiresRegister());
5832 // When read barriers are enabled, we need an additional temporary
5833 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005834 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005835 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005837}
5838
5839void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005840 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005841 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005842 Location obj_loc = locations->InAt(0);
5843 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005844 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005845 Location temp_loc = locations->GetTemp(0);
5846 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005847 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005848 locations->GetTemp(1) :
5849 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005850 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5852 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5853 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005854
Roland Levillain3b359c72015-11-17 19:35:12 +00005855 bool is_type_check_slow_path_fatal =
5856 (type_check_kind == TypeCheckKind::kExactCheck ||
5857 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5858 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5859 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5860 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005861 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00005862 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5863 is_type_check_slow_path_fatal);
5864 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865
5866 Label done;
5867 // Avoid null check if we know obj is not null.
5868 if (instruction->MustDoNullCheck()) {
5869 __ CompareAndBranchIfZero(obj, &done);
5870 }
5871
Roland Levillain3b359c72015-11-17 19:35:12 +00005872 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005873 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005874
Roland Levillain3b359c72015-11-17 19:35:12 +00005875 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 case TypeCheckKind::kExactCheck:
5877 case TypeCheckKind::kArrayCheck: {
5878 __ cmp(temp, ShifterOperand(cls));
5879 // Jump to slow path for throwing the exception or doing a
5880 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005881 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 break;
5883 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005884
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 case TypeCheckKind::kAbstractClassCheck: {
5886 // If the class is abstract, we eagerly fetch the super class of the
5887 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005888 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005890 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005891 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005892
5893 // If the class reference currently in `temp` is not null, jump
5894 // to the `compare_classes` label to compare it with the checked
5895 // class.
5896 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5897 // Otherwise, jump to the slow path to throw the exception.
5898 //
5899 // But before, move back the object's class into `temp` before
5900 // going into the slow path, as it has been overwritten in the
5901 // meantime.
5902 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005903 GenerateReferenceLoadTwoRegisters(
5904 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005905 __ b(type_check_slow_path->GetEntryLabel());
5906
5907 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908 __ cmp(temp, ShifterOperand(cls));
5909 __ b(&loop, NE);
5910 break;
5911 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005912
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005913 case TypeCheckKind::kClassHierarchyCheck: {
5914 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005915 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 __ Bind(&loop);
5917 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005918 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005919
Roland Levillain3b359c72015-11-17 19:35:12 +00005920 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005921 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005922
5923 // If the class reference currently in `temp` is not null, jump
5924 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005926 // Otherwise, jump to the slow path to throw the exception.
5927 //
5928 // But before, move back the object's class into `temp` before
5929 // going into the slow path, as it has been overwritten in the
5930 // meantime.
5931 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005932 GenerateReferenceLoadTwoRegisters(
5933 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005934 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005935 break;
5936 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005937
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005939 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005940 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005941 __ cmp(temp, ShifterOperand(cls));
5942 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005943
5944 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005945 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005946 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005947
5948 // If the component type is not null (i.e. the object is indeed
5949 // an array), jump to label `check_non_primitive_component_type`
5950 // to further check that this component type is not a primitive
5951 // type.
5952 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5953 // Otherwise, jump to the slow path to throw the exception.
5954 //
5955 // But before, move back the object's class into `temp` before
5956 // going into the slow path, as it has been overwritten in the
5957 // meantime.
5958 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005959 GenerateReferenceLoadTwoRegisters(
5960 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005961 __ b(type_check_slow_path->GetEntryLabel());
5962
5963 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005964 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005965 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5966 __ CompareAndBranchIfZero(temp, &done);
5967 // Same comment as above regarding `temp` and the slow path.
5968 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005969 GenerateReferenceLoadTwoRegisters(
5970 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005971 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 break;
5973 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005974
Calin Juravle98893e12015-10-02 21:05:03 +01005975 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005976 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005977 // We always go into the type check slow path for the unresolved
5978 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005979 //
5980 // We cannot directly call the CheckCast runtime entry point
5981 // without resorting to a type checking slow path here (i.e. by
5982 // calling InvokeRuntime directly), as it would require to
5983 // assign fixed registers for the inputs of this HInstanceOf
5984 // instruction (following the runtime calling convention), which
5985 // might be cluttered by the potential first read barrier
5986 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005987 //
5988 // TODO: Introduce a new runtime entry point taking the object
5989 // to test (instead of its class) as argument, and let it deal
5990 // with the read barrier issues. This will let us refactor this
5991 // case of the `switch` code as it was previously (with a direct
5992 // call to the runtime not using a type checking slow path).
5993 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005994 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005995 break;
5996 }
5997 __ Bind(&done);
5998
Roland Levillain3b359c72015-11-17 19:35:12 +00005999 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006000}
6001
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006002void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6003 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006005 InvokeRuntimeCallingConvention calling_convention;
6006 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6007}
6008
6009void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006010 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6011 instruction,
6012 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006013 if (instruction->IsEnter()) {
6014 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6015 } else {
6016 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6017 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006018}
6019
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006020void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6021void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6022void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006023
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006024void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006025 LocationSummary* locations =
6026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6027 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6028 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006029 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006030 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006031 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006033}
6034
6035void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6036 HandleBitwiseOperation(instruction);
6037}
6038
6039void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6040 HandleBitwiseOperation(instruction);
6041}
6042
6043void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6044 HandleBitwiseOperation(instruction);
6045}
6046
Artem Serov7fc63502016-02-09 17:15:29 +00006047
6048void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6049 LocationSummary* locations =
6050 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6051 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6052 || instruction->GetResultType() == Primitive::kPrimLong);
6053
6054 locations->SetInAt(0, Location::RequiresRegister());
6055 locations->SetInAt(1, Location::RequiresRegister());
6056 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6057}
6058
6059void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6060 LocationSummary* locations = instruction->GetLocations();
6061 Location first = locations->InAt(0);
6062 Location second = locations->InAt(1);
6063 Location out = locations->Out();
6064
6065 if (instruction->GetResultType() == Primitive::kPrimInt) {
6066 Register first_reg = first.AsRegister<Register>();
6067 ShifterOperand second_reg(second.AsRegister<Register>());
6068 Register out_reg = out.AsRegister<Register>();
6069
6070 switch (instruction->GetOpKind()) {
6071 case HInstruction::kAnd:
6072 __ bic(out_reg, first_reg, second_reg);
6073 break;
6074 case HInstruction::kOr:
6075 __ orn(out_reg, first_reg, second_reg);
6076 break;
6077 // There is no EON on arm.
6078 case HInstruction::kXor:
6079 default:
6080 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6081 UNREACHABLE();
6082 }
6083 return;
6084
6085 } else {
6086 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6087 Register first_low = first.AsRegisterPairLow<Register>();
6088 Register first_high = first.AsRegisterPairHigh<Register>();
6089 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6090 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6091 Register out_low = out.AsRegisterPairLow<Register>();
6092 Register out_high = out.AsRegisterPairHigh<Register>();
6093
6094 switch (instruction->GetOpKind()) {
6095 case HInstruction::kAnd:
6096 __ bic(out_low, first_low, second_low);
6097 __ bic(out_high, first_high, second_high);
6098 break;
6099 case HInstruction::kOr:
6100 __ orn(out_low, first_low, second_low);
6101 __ orn(out_high, first_high, second_high);
6102 break;
6103 // There is no EON on arm.
6104 case HInstruction::kXor:
6105 default:
6106 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6107 UNREACHABLE();
6108 }
6109 }
6110}
6111
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006112void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6113 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6114 if (value == 0xffffffffu) {
6115 if (out != first) {
6116 __ mov(out, ShifterOperand(first));
6117 }
6118 return;
6119 }
6120 if (value == 0u) {
6121 __ mov(out, ShifterOperand(0));
6122 return;
6123 }
6124 ShifterOperand so;
6125 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6126 __ and_(out, first, so);
6127 } else {
6128 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6129 __ bic(out, first, ShifterOperand(~value));
6130 }
6131}
6132
6133void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6134 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6135 if (value == 0u) {
6136 if (out != first) {
6137 __ mov(out, ShifterOperand(first));
6138 }
6139 return;
6140 }
6141 if (value == 0xffffffffu) {
6142 __ mvn(out, ShifterOperand(0));
6143 return;
6144 }
6145 ShifterOperand so;
6146 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6147 __ orr(out, first, so);
6148 } else {
6149 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6150 __ orn(out, first, ShifterOperand(~value));
6151 }
6152}
6153
6154void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6155 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6156 if (value == 0u) {
6157 if (out != first) {
6158 __ mov(out, ShifterOperand(first));
6159 }
6160 return;
6161 }
6162 __ eor(out, first, ShifterOperand(value));
6163}
6164
Vladimir Marko59751a72016-08-05 14:37:27 +01006165void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6166 Location first,
6167 uint64_t value) {
6168 Register out_low = out.AsRegisterPairLow<Register>();
6169 Register out_high = out.AsRegisterPairHigh<Register>();
6170 Register first_low = first.AsRegisterPairLow<Register>();
6171 Register first_high = first.AsRegisterPairHigh<Register>();
6172 uint32_t value_low = Low32Bits(value);
6173 uint32_t value_high = High32Bits(value);
6174 if (value_low == 0u) {
6175 if (out_low != first_low) {
6176 __ mov(out_low, ShifterOperand(first_low));
6177 }
6178 __ AddConstant(out_high, first_high, value_high);
6179 return;
6180 }
6181 __ AddConstantSetFlags(out_low, first_low, value_low);
6182 ShifterOperand so;
6183 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6184 __ adc(out_high, first_high, so);
6185 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6186 __ sbc(out_high, first_high, so);
6187 } else {
6188 LOG(FATAL) << "Unexpected constant " << value_high;
6189 UNREACHABLE();
6190 }
6191}
6192
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006193void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6194 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006195 Location first = locations->InAt(0);
6196 Location second = locations->InAt(1);
6197 Location out = locations->Out();
6198
6199 if (second.IsConstant()) {
6200 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6201 uint32_t value_low = Low32Bits(value);
6202 if (instruction->GetResultType() == Primitive::kPrimInt) {
6203 Register first_reg = first.AsRegister<Register>();
6204 Register out_reg = out.AsRegister<Register>();
6205 if (instruction->IsAnd()) {
6206 GenerateAndConst(out_reg, first_reg, value_low);
6207 } else if (instruction->IsOr()) {
6208 GenerateOrrConst(out_reg, first_reg, value_low);
6209 } else {
6210 DCHECK(instruction->IsXor());
6211 GenerateEorConst(out_reg, first_reg, value_low);
6212 }
6213 } else {
6214 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6215 uint32_t value_high = High32Bits(value);
6216 Register first_low = first.AsRegisterPairLow<Register>();
6217 Register first_high = first.AsRegisterPairHigh<Register>();
6218 Register out_low = out.AsRegisterPairLow<Register>();
6219 Register out_high = out.AsRegisterPairHigh<Register>();
6220 if (instruction->IsAnd()) {
6221 GenerateAndConst(out_low, first_low, value_low);
6222 GenerateAndConst(out_high, first_high, value_high);
6223 } else if (instruction->IsOr()) {
6224 GenerateOrrConst(out_low, first_low, value_low);
6225 GenerateOrrConst(out_high, first_high, value_high);
6226 } else {
6227 DCHECK(instruction->IsXor());
6228 GenerateEorConst(out_low, first_low, value_low);
6229 GenerateEorConst(out_high, first_high, value_high);
6230 }
6231 }
6232 return;
6233 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006234
6235 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006236 Register first_reg = first.AsRegister<Register>();
6237 ShifterOperand second_reg(second.AsRegister<Register>());
6238 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006239 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006240 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006241 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006242 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006243 } else {
6244 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006245 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006246 }
6247 } else {
6248 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006249 Register first_low = first.AsRegisterPairLow<Register>();
6250 Register first_high = first.AsRegisterPairHigh<Register>();
6251 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6252 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6253 Register out_low = out.AsRegisterPairLow<Register>();
6254 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006255 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006256 __ and_(out_low, first_low, second_low);
6257 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006258 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006259 __ orr(out_low, first_low, second_low);
6260 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006261 } else {
6262 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006263 __ eor(out_low, first_low, second_low);
6264 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006265 }
6266 }
6267}
6268
Roland Levillainc9285912015-12-18 10:38:42 +00006269void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6270 Location out,
6271 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006272 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006273 Register out_reg = out.AsRegister<Register>();
6274 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006275 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006276 if (kUseBakerReadBarrier) {
6277 // Load with fast path based Baker's read barrier.
6278 // /* HeapReference<Object> */ out = *(out + offset)
6279 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006280 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006281 } else {
6282 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006283 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006284 // in the following move operation, as we will need it for the
6285 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006286 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006287 // /* HeapReference<Object> */ out = *(out + offset)
6288 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006289 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006290 }
6291 } else {
6292 // Plain load with no read barrier.
6293 // /* HeapReference<Object> */ out = *(out + offset)
6294 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6295 __ MaybeUnpoisonHeapReference(out_reg);
6296 }
6297}
6298
6299void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6300 Location out,
6301 Location obj,
6302 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006303 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006304 Register out_reg = out.AsRegister<Register>();
6305 Register obj_reg = obj.AsRegister<Register>();
6306 if (kEmitCompilerReadBarrier) {
6307 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006308 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006309 // Load with fast path based Baker's read barrier.
6310 // /* HeapReference<Object> */ out = *(obj + offset)
6311 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006312 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006313 } else {
6314 // Load with slow path based read barrier.
6315 // /* HeapReference<Object> */ out = *(obj + offset)
6316 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6317 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6318 }
6319 } else {
6320 // Plain load with no read barrier.
6321 // /* HeapReference<Object> */ out = *(obj + offset)
6322 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6323 __ MaybeUnpoisonHeapReference(out_reg);
6324 }
6325}
6326
6327void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6328 Location root,
6329 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006330 uint32_t offset,
6331 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006332 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006333 if (requires_read_barrier) {
6334 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006335 if (kUseBakerReadBarrier) {
6336 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6337 // Baker's read barrier are used:
6338 //
6339 // root = obj.field;
6340 // if (Thread::Current()->GetIsGcMarking()) {
6341 // root = ReadBarrier::Mark(root)
6342 // }
6343
6344 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6345 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6346 static_assert(
6347 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6348 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6349 "have different sizes.");
6350 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6351 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6352 "have different sizes.");
6353
Vladimir Marko953437b2016-08-24 08:30:46 +00006354 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006355 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006356 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006357 codegen_->AddSlowPath(slow_path);
6358
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006359 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006360 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006361 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006362 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6363 __ Bind(slow_path->GetExitLabel());
6364 } else {
6365 // GC root loaded through a slow path for read barriers other
6366 // than Baker's.
6367 // /* GcRoot<mirror::Object>* */ root = obj + offset
6368 __ AddConstant(root_reg, obj, offset);
6369 // /* mirror::Object* */ root = root->Read()
6370 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6371 }
6372 } else {
6373 // Plain GC root load with no read barrier.
6374 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6375 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6376 // Note that GC roots are not affected by heap poisoning, thus we
6377 // do not have to unpoison `root_reg` here.
6378 }
6379}
6380
6381void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6382 Location ref,
6383 Register obj,
6384 uint32_t offset,
6385 Location temp,
6386 bool needs_null_check) {
6387 DCHECK(kEmitCompilerReadBarrier);
6388 DCHECK(kUseBakerReadBarrier);
6389
6390 // /* HeapReference<Object> */ ref = *(obj + offset)
6391 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006392 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006393 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006394 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006395}
6396
6397void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6398 Location ref,
6399 Register obj,
6400 uint32_t data_offset,
6401 Location index,
6402 Location temp,
6403 bool needs_null_check) {
6404 DCHECK(kEmitCompilerReadBarrier);
6405 DCHECK(kUseBakerReadBarrier);
6406
Roland Levillainbfea3352016-06-23 13:48:47 +01006407 static_assert(
6408 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6409 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006410 // /* HeapReference<Object> */ ref =
6411 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006412 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006413 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006414 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006415}
6416
6417void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6418 Location ref,
6419 Register obj,
6420 uint32_t offset,
6421 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006422 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006423 Location temp,
6424 bool needs_null_check) {
6425 DCHECK(kEmitCompilerReadBarrier);
6426 DCHECK(kUseBakerReadBarrier);
6427
6428 // In slow path based read barriers, the read barrier call is
6429 // inserted after the original load. However, in fast path based
6430 // Baker's read barriers, we need to perform the load of
6431 // mirror::Object::monitor_ *before* the original reference load.
6432 // This load-load ordering is required by the read barrier.
6433 // The fast path/slow path (for Baker's algorithm) should look like:
6434 //
6435 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6436 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6437 // HeapReference<Object> ref = *src; // Original reference load.
6438 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6439 // if (is_gray) {
6440 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6441 // }
6442 //
6443 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006444 // slightly more complex as it performs additional checks that we do
6445 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006446
6447 Register ref_reg = ref.AsRegister<Register>();
6448 Register temp_reg = temp.AsRegister<Register>();
6449 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6450
6451 // /* int32_t */ monitor = obj->monitor_
6452 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6453 if (needs_null_check) {
6454 MaybeRecordImplicitNullCheck(instruction);
6455 }
6456 // /* LockWord */ lock_word = LockWord(monitor)
6457 static_assert(sizeof(LockWord) == sizeof(int32_t),
6458 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006459
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006460 // Introduce a dependency on the lock_word including the rb_state,
6461 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006462 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006463 // `obj` is unchanged by this operation, but its value now depends
6464 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006465 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006466
6467 // The actual reference load.
6468 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006469 // Load types involving an "index": ArrayGet and
6470 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6471 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006472 if (index.IsConstant()) {
6473 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006474 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006475 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6476 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006477 // Handle the special case of the
6478 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6479 // a register pair as index ("long offset"), of which only the low
6480 // part contains data.
6481 Register index_reg = index.IsRegisterPair()
6482 ? index.AsRegisterPairLow<Register>()
6483 : index.AsRegister<Register>();
6484 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006485 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6486 }
6487 } else {
6488 // /* HeapReference<Object> */ ref = *(obj + offset)
6489 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6490 }
6491
6492 // Object* ref = ref_addr->AsMirrorPtr()
6493 __ MaybeUnpoisonHeapReference(ref_reg);
6494
Vladimir Marko953437b2016-08-24 08:30:46 +00006495 // Slow path marking the object `ref` when it is gray.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006496 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006497 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006498 AddSlowPath(slow_path);
6499
6500 // if (rb_state == ReadBarrier::gray_ptr_)
6501 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006502 // Given the numeric representation, it's enough to check the low bit of the
6503 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6504 // which can be a 16-bit instruction unlike the TST immediate.
6505 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6506 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6507 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6508 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6509 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006510 __ Bind(slow_path->GetExitLabel());
6511}
6512
6513void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6514 Location out,
6515 Location ref,
6516 Location obj,
6517 uint32_t offset,
6518 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006519 DCHECK(kEmitCompilerReadBarrier);
6520
Roland Levillainc9285912015-12-18 10:38:42 +00006521 // Insert a slow path based read barrier *after* the reference load.
6522 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006523 // If heap poisoning is enabled, the unpoisoning of the loaded
6524 // reference will be carried out by the runtime within the slow
6525 // path.
6526 //
6527 // Note that `ref` currently does not get unpoisoned (when heap
6528 // poisoning is enabled), which is alright as the `ref` argument is
6529 // not used by the artReadBarrierSlow entry point.
6530 //
6531 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006532 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00006533 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6534 AddSlowPath(slow_path);
6535
Roland Levillain3b359c72015-11-17 19:35:12 +00006536 __ b(slow_path->GetEntryLabel());
6537 __ Bind(slow_path->GetExitLabel());
6538}
6539
Roland Levillainc9285912015-12-18 10:38:42 +00006540void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6541 Location out,
6542 Location ref,
6543 Location obj,
6544 uint32_t offset,
6545 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006546 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006547 // Baker's read barriers shall be handled by the fast path
6548 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6549 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006550 // If heap poisoning is enabled, unpoisoning will be taken care of
6551 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006552 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006553 } else if (kPoisonHeapReferences) {
6554 __ UnpoisonHeapReference(out.AsRegister<Register>());
6555 }
6556}
6557
Roland Levillainc9285912015-12-18 10:38:42 +00006558void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6559 Location out,
6560 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006561 DCHECK(kEmitCompilerReadBarrier);
6562
Roland Levillainc9285912015-12-18 10:38:42 +00006563 // Insert a slow path based read barrier *after* the GC root load.
6564 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006565 // Note that GC roots are not affected by heap poisoning, so we do
6566 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006567 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006568 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6569 AddSlowPath(slow_path);
6570
Roland Levillain3b359c72015-11-17 19:35:12 +00006571 __ b(slow_path->GetEntryLabel());
6572 __ Bind(slow_path->GetExitLabel());
6573}
6574
Vladimir Markodc151b22015-10-15 18:02:30 +01006575HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6576 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6577 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006578 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6579 // We disable pc-relative load when there is an irreducible loop, as the optimization
6580 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006581 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6582 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006583 if (GetGraph()->HasIrreducibleLoops() &&
6584 (dispatch_info.method_load_kind ==
6585 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6586 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6587 }
6588
6589 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006590 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6591 if (&outer_dex_file != target_method.dex_file) {
6592 // Calls across dex files are more likely to exceed the available BL range,
6593 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6594 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6595 (desired_dispatch_info.method_load_kind ==
6596 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6597 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6598 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6599 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006600 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006601 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006602 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006603 0u
6604 };
6605 }
6606 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006607 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006608}
6609
Vladimir Markob4536b72015-11-24 13:45:23 +00006610Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6611 Register temp) {
6612 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6613 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6614 if (!invoke->GetLocations()->Intrinsified()) {
6615 return location.AsRegister<Register>();
6616 }
6617 // For intrinsics we allow any location, so it may be on the stack.
6618 if (!location.IsRegister()) {
6619 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6620 return temp;
6621 }
6622 // For register locations, check if the register was saved. If so, get it from the stack.
6623 // Note: There is a chance that the register was saved but not overwritten, so we could
6624 // save one load. However, since this is just an intrinsic slow path we prefer this
6625 // simple and more robust approach rather that trying to determine if that's the case.
6626 SlowPathCode* slow_path = GetCurrentSlowPath();
6627 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6628 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6629 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6630 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6631 return temp;
6632 }
6633 return location.AsRegister<Register>();
6634}
6635
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006636void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006637 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006638 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006639 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6640 // LR = code address from literal pool with link-time patch.
6641 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006642 break;
6643 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6644 // LR = invoke->GetDirectCodePtr();
6645 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006646 break;
6647 default:
6648 break;
6649 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006650
Vladimir Marko58155012015-08-19 12:49:41 +00006651 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6652 switch (invoke->GetMethodLoadKind()) {
6653 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6654 // temp = thread->string_init_entrypoint
6655 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6656 break;
6657 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006658 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006659 break;
6660 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6661 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6662 break;
6663 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6664 __ LoadLiteral(temp.AsRegister<Register>(),
6665 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6666 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006667 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6668 HArmDexCacheArraysBase* base =
6669 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6670 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6671 temp.AsRegister<Register>());
6672 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6673 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6674 break;
6675 }
Vladimir Marko58155012015-08-19 12:49:41 +00006676 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006677 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006678 Register method_reg;
6679 Register reg = temp.AsRegister<Register>();
6680 if (current_method.IsRegister()) {
6681 method_reg = current_method.AsRegister<Register>();
6682 } else {
6683 DCHECK(invoke->GetLocations()->Intrinsified());
6684 DCHECK(!current_method.IsValid());
6685 method_reg = reg;
6686 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6687 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006688 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6689 __ LoadFromOffset(kLoadWord,
6690 reg,
6691 method_reg,
6692 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006693 // temp = temp[index_in_cache];
6694 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6695 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006696 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6697 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006698 }
Vladimir Marko58155012015-08-19 12:49:41 +00006699 }
6700
6701 switch (invoke->GetCodePtrLocation()) {
6702 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6703 __ bl(GetFrameEntryLabel());
6704 break;
6705 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006706 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006707 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006708 // Arbitrarily branch to the BL itself, override at link time.
6709 __ bl(&relative_call_patches_.back().label);
6710 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006711 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6712 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6713 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006714 // LR()
6715 __ blx(LR);
6716 break;
6717 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6718 // LR = callee_method->entry_point_from_quick_compiled_code_
6719 __ LoadFromOffset(
6720 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006721 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006722 // LR()
6723 __ blx(LR);
6724 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006725 }
6726
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006727 DCHECK(!IsLeafMethod());
6728}
6729
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006730void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6731 Register temp = temp_location.AsRegister<Register>();
6732 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6733 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006734
6735 // Use the calling convention instead of the location of the receiver, as
6736 // intrinsics may have put the receiver in a different register. In the intrinsics
6737 // slow path, the arguments have been moved to the right place, so here we are
6738 // guaranteed that the receiver is the first register of the calling convention.
6739 InvokeDexCallingConvention calling_convention;
6740 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006741 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006742 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006743 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006744 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006745 // Instead of simply (possibly) unpoisoning `temp` here, we should
6746 // emit a read barrier for the previous class reference load.
6747 // However this is not required in practice, as this is an
6748 // intermediate/temporary reference and because the current
6749 // concurrent copying collector keeps the from-space memory
6750 // intact/accessible until the end of the marking phase (the
6751 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006752 __ MaybeUnpoisonHeapReference(temp);
6753 // temp = temp->GetMethodAt(method_offset);
6754 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006755 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006756 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6757 // LR = temp->GetEntryPoint();
6758 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6759 // LR();
6760 __ blx(LR);
6761}
6762
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006763CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6764 const DexFile& dex_file, uint32_t string_index) {
6765 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6766}
6767
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006768CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6769 const DexFile& dex_file, uint32_t type_index) {
6770 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6771}
6772
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006773CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6774 const DexFile& dex_file, uint32_t element_offset) {
6775 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6776}
6777
6778CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6779 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6780 patches->emplace_back(dex_file, offset_or_index);
6781 return &patches->back();
6782}
6783
6784Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6785 uint32_t string_index) {
6786 return boot_image_string_patches_.GetOrCreate(
6787 StringReference(&dex_file, string_index),
6788 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6789}
6790
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006791Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6792 uint32_t type_index) {
6793 return boot_image_type_patches_.GetOrCreate(
6794 TypeReference(&dex_file, type_index),
6795 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6796}
6797
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006798Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6799 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6800 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6801 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6802}
6803
6804Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6805 return DeduplicateUint32Literal(address, &uint32_literals_);
6806}
6807
Vladimir Marko58155012015-08-19 12:49:41 +00006808void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6809 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006810 size_t size =
6811 method_patches_.size() +
6812 call_patches_.size() +
6813 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006814 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6815 boot_image_string_patches_.size() +
6816 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006817 boot_image_type_patches_.size() +
6818 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006819 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006820 linker_patches->reserve(size);
6821 for (const auto& entry : method_patches_) {
6822 const MethodReference& target_method = entry.first;
6823 Literal* literal = entry.second;
6824 DCHECK(literal->GetLabel()->IsBound());
6825 uint32_t literal_offset = literal->GetLabel()->Position();
6826 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6827 target_method.dex_file,
6828 target_method.dex_method_index));
6829 }
6830 for (const auto& entry : call_patches_) {
6831 const MethodReference& target_method = entry.first;
6832 Literal* literal = entry.second;
6833 DCHECK(literal->GetLabel()->IsBound());
6834 uint32_t literal_offset = literal->GetLabel()->Position();
6835 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6836 target_method.dex_file,
6837 target_method.dex_method_index));
6838 }
6839 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6840 uint32_t literal_offset = info.label.Position();
6841 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6842 info.target_method.dex_file,
6843 info.target_method.dex_method_index));
6844 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006845 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6846 const DexFile& dex_file = info.target_dex_file;
6847 size_t base_element_offset = info.offset_or_index;
6848 DCHECK(info.add_pc_label.IsBound());
6849 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006850 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006851 DCHECK(info.movw_label.IsBound());
6852 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006853 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6854 &dex_file,
6855 add_pc_offset,
6856 base_element_offset));
6857 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006858 DCHECK(info.movt_label.IsBound());
6859 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006860 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6861 &dex_file,
6862 add_pc_offset,
6863 base_element_offset));
6864 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006865 for (const auto& entry : boot_image_string_patches_) {
6866 const StringReference& target_string = entry.first;
6867 Literal* literal = entry.second;
6868 DCHECK(literal->GetLabel()->IsBound());
6869 uint32_t literal_offset = literal->GetLabel()->Position();
6870 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6871 target_string.dex_file,
6872 target_string.string_index));
6873 }
6874 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6875 const DexFile& dex_file = info.target_dex_file;
6876 uint32_t string_index = info.offset_or_index;
6877 DCHECK(info.add_pc_label.IsBound());
6878 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6879 // Add MOVW patch.
6880 DCHECK(info.movw_label.IsBound());
6881 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6882 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6883 &dex_file,
6884 add_pc_offset,
6885 string_index));
6886 // Add MOVT patch.
6887 DCHECK(info.movt_label.IsBound());
6888 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6889 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6890 &dex_file,
6891 add_pc_offset,
6892 string_index));
6893 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006894 for (const auto& entry : boot_image_type_patches_) {
6895 const TypeReference& target_type = entry.first;
6896 Literal* literal = entry.second;
6897 DCHECK(literal->GetLabel()->IsBound());
6898 uint32_t literal_offset = literal->GetLabel()->Position();
6899 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6900 target_type.dex_file,
6901 target_type.type_index));
6902 }
6903 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6904 const DexFile& dex_file = info.target_dex_file;
6905 uint32_t type_index = info.offset_or_index;
6906 DCHECK(info.add_pc_label.IsBound());
6907 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6908 // Add MOVW patch.
6909 DCHECK(info.movw_label.IsBound());
6910 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6911 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6912 &dex_file,
6913 add_pc_offset,
6914 type_index));
6915 // Add MOVT patch.
6916 DCHECK(info.movt_label.IsBound());
6917 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6918 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6919 &dex_file,
6920 add_pc_offset,
6921 type_index));
6922 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006923 for (const auto& entry : boot_image_address_patches_) {
6924 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6925 Literal* literal = entry.second;
6926 DCHECK(literal->GetLabel()->IsBound());
6927 uint32_t literal_offset = literal->GetLabel()->Position();
6928 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6929 }
6930}
6931
6932Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6933 return map->GetOrCreate(
6934 value,
6935 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006936}
6937
6938Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6939 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006940 return map->GetOrCreate(
6941 target_method,
6942 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006943}
6944
6945Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6946 return DeduplicateMethodLiteral(target_method, &method_patches_);
6947}
6948
6949Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6950 return DeduplicateMethodLiteral(target_method, &call_patches_);
6951}
6952
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006953void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6954 LocationSummary* locations =
6955 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6956 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6957 Location::RequiresRegister());
6958 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6959 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6961}
6962
6963void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6964 LocationSummary* locations = instr->GetLocations();
6965 Register res = locations->Out().AsRegister<Register>();
6966 Register accumulator =
6967 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6968 Register mul_left =
6969 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6970 Register mul_right =
6971 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6972
6973 if (instr->GetOpKind() == HInstruction::kAdd) {
6974 __ mla(res, mul_left, mul_right, accumulator);
6975 } else {
6976 __ mls(res, mul_left, mul_right, accumulator);
6977 }
6978}
6979
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006980void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006981 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006982 LOG(FATAL) << "Unreachable";
6983}
6984
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006985void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006986 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006987 LOG(FATAL) << "Unreachable";
6988}
6989
Mark Mendellfe57faa2015-09-18 09:26:15 -04006990// Simple implementation of packed switch - generate cascaded compare/jumps.
6991void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6992 LocationSummary* locations =
6993 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6994 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006995 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006996 codegen_->GetAssembler()->IsThumb()) {
6997 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6998 if (switch_instr->GetStartValue() != 0) {
6999 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7000 }
7001 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007002}
7003
7004void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7005 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007006 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007007 LocationSummary* locations = switch_instr->GetLocations();
7008 Register value_reg = locations->InAt(0).AsRegister<Register>();
7009 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7010
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007011 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007012 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007013 Register temp_reg = IP;
7014 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7015 // the immediate, because IP is used as the destination register. For the other
7016 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7017 // and they can be encoded in the instruction without making use of IP register.
7018 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7019
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007020 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007021 // Jump to successors[0] if value == lower_bound.
7022 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7023 int32_t last_index = 0;
7024 for (; num_entries - last_index > 2; last_index += 2) {
7025 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7026 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7027 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7028 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7029 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7030 }
7031 if (num_entries - last_index == 2) {
7032 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007033 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007034 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007035 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007036
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007037 // And the default for any other value.
7038 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7039 __ b(codegen_->GetLabelOf(default_block));
7040 }
7041 } else {
7042 // Create a table lookup.
7043 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7044
7045 // Materialize a pointer to the switch table
7046 std::vector<Label*> labels(num_entries);
7047 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7048 for (uint32_t i = 0; i < num_entries; i++) {
7049 labels[i] = codegen_->GetLabelOf(successors[i]);
7050 }
7051 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7052
7053 // Remove the bias.
7054 Register key_reg;
7055 if (lower_bound != 0) {
7056 key_reg = locations->GetTemp(1).AsRegister<Register>();
7057 __ AddConstant(key_reg, value_reg, -lower_bound);
7058 } else {
7059 key_reg = value_reg;
7060 }
7061
7062 // Check whether the value is in the table, jump to default block if not.
7063 __ CmpConstant(key_reg, num_entries - 1);
7064 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7065
7066 // Load the displacement from the table.
7067 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7068
7069 // Dispatch is a direct add to the PC (for Thumb2).
7070 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007071 }
7072}
7073
Vladimir Markob4536b72015-11-24 13:45:23 +00007074void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7076 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007077}
7078
7079void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7080 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007081 CodeGeneratorARM::PcRelativePatchInfo* labels =
7082 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007083 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007084 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007085 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007086 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007087 __ BindTrackedLabel(&labels->add_pc_label);
7088 __ add(base_reg, base_reg, ShifterOperand(PC));
7089}
7090
Andreas Gampe85b62f22015-09-09 13:15:38 -07007091void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7092 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007093 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007094 return;
7095 }
7096
7097 DCHECK_NE(type, Primitive::kPrimVoid);
7098
7099 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7100 if (return_loc.Equals(trg)) {
7101 return;
7102 }
7103
7104 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7105 // with the last branch.
7106 if (type == Primitive::kPrimLong) {
7107 HParallelMove parallel_move(GetGraph()->GetArena());
7108 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7109 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7110 GetMoveResolver()->EmitNativeCode(&parallel_move);
7111 } else if (type == Primitive::kPrimDouble) {
7112 HParallelMove parallel_move(GetGraph()->GetArena());
7113 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7114 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7115 GetMoveResolver()->EmitNativeCode(&parallel_move);
7116 } else {
7117 // Let the parallel move resolver take care of all of this.
7118 HParallelMove parallel_move(GetGraph()->GetArena());
7119 parallel_move.AddMove(return_loc, trg, type, nullptr);
7120 GetMoveResolver()->EmitNativeCode(&parallel_move);
7121 }
7122}
7123
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007124void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7125 LocationSummary* locations =
7126 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7127 locations->SetInAt(0, Location::RequiresRegister());
7128 locations->SetOut(Location::RequiresRegister());
7129}
7130
7131void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7132 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007133 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007134 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007135 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007136 __ LoadFromOffset(kLoadWord,
7137 locations->Out().AsRegister<Register>(),
7138 locations->InAt(0).AsRegister<Register>(),
7139 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007140 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007141 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007142 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007143 __ LoadFromOffset(kLoadWord,
7144 locations->Out().AsRegister<Register>(),
7145 locations->InAt(0).AsRegister<Register>(),
7146 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7147 __ LoadFromOffset(kLoadWord,
7148 locations->Out().AsRegister<Register>(),
7149 locations->Out().AsRegister<Register>(),
7150 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007151 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007152}
7153
Roland Levillain4d027112015-07-01 15:41:14 +01007154#undef __
7155#undef QUICK_ENTRY_POINT
7156
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007157} // namespace arm
7158} // namespace art