blob: 40c2b9c1ec6a8be3316c730c756879cd99554a1a [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
Andreas Gampe85b62f22015-09-09 13:15:38 -070066class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000068 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069
Alexandre Rames67555f72014-11-18 10:55:16 +000070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010071 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000073 if (instruction_->CanThrowIntoCatchBlock()) {
74 // Live registers will be restored in the catch block if caught.
75 SaveLiveRegisters(codegen, instruction_->GetLocations());
76 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +010077 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
78 instruction_,
79 instruction_->GetDexPc(),
80 this);
Roland Levillain888d0672015-11-23 18:53:50 +000081 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 }
83
Alexandre Rames8158f282015-08-07 10:26:17 +010084 bool IsFatal() const OVERRIDE { return true; }
85
Alexandre Rames9931f312015-06-19 14:47:01 +010086 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
87
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
90};
91
Andreas Gampe85b62f22015-09-09 13:15:38 -070092class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000093 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000094 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000095
Alexandre Rames67555f72014-11-18 10:55:16 +000096 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000097 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
98 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000099 if (instruction_->CanThrowIntoCatchBlock()) {
100 // Live registers will be restored in the catch block if caught.
101 SaveLiveRegisters(codegen, instruction_->GetLocations());
102 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100103 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000104 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000105 }
106
Alexandre Rames8158f282015-08-07 10:26:17 +0100107 bool IsFatal() const OVERRIDE { return true; }
108
Alexandre Rames9931f312015-06-19 14:47:01 +0100109 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
110
Calin Juravled0d48522014-11-04 16:40:20 +0000111 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000112 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
113};
114
Andreas Gampe85b62f22015-09-09 13:15:38 -0700115class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000117 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000118 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames67555f72014-11-18 10:55:16 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100123 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000124 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100125 if (successor_ == nullptr) {
126 __ b(GetReturnLabel());
127 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000130 }
131
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132 Label* GetReturnLabel() {
133 DCHECK(successor_ == nullptr);
134 return &return_label_;
135 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100137 HBasicBlock* GetSuccessor() const {
138 return successor_;
139 }
140
Alexandre Rames9931f312015-06-19 14:47:01 +0100141 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
142
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 // If not null, the block to branch to after the suspend check.
145 HBasicBlock* const successor_;
146
147 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 Label return_label_;
149
150 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
151};
152
Andreas Gampe85b62f22015-09-09 13:15:38 -0700153class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100155 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000156 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157
Alexandre Rames67555f72014-11-18 10:55:16 +0000158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100159 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100160 LocationSummary* locations = instruction_->GetLocations();
161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000163 if (instruction_->CanThrowIntoCatchBlock()) {
164 // Live registers will be restored in the catch block if caught.
165 SaveLiveRegisters(codegen, instruction_->GetLocations());
166 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000167 // We're moving two locations to locations that could overlap, so we need a parallel
168 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100169 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000170 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100171 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000172 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100173 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100174 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100175 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
176 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100177 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
178 ? kQuickThrowStringBounds
179 : kQuickThrowArrayBounds;
180 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100181 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000182 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 }
184
Alexandre Rames8158f282015-08-07 10:26:17 +0100185 bool IsFatal() const OVERRIDE { return true; }
186
Alexandre Rames9931f312015-06-19 14:47:01 +0100187 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
188
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
191};
192
Andreas Gampe85b62f22015-09-09 13:15:38 -0700193class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100194 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000195 LoadClassSlowPathARM(HLoadClass* cls,
196 HInstruction* at,
197 uint32_t dex_pc,
198 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000199 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000200 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
201 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100202
Alexandre Rames67555f72014-11-18 10:55:16 +0000203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 LocationSummary* locations = at_->GetLocations();
205
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100206 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
207 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000208 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100212 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
213 : kQuickInitializeType;
214 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000215 if (do_clinit_) {
216 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
217 } else {
218 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
219 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220
221 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000222 Location out = locations->Out();
223 if (out.IsValid()) {
224 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000225 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
226 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 __ b(GetExitLabel());
229 }
230
Alexandre Rames9931f312015-06-19 14:47:01 +0100231 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234 // The class this slow path will load.
235 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100236
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 // The instruction where this slow path is happening.
238 // (Might be the load class or an initialization check).
239 HInstruction* const at_;
240
241 // The dex PC of `at_`.
242 const uint32_t dex_pc_;
243
244 // Whether to initialize the class.
245 const bool do_clinit_;
246
247 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248};
249
Andreas Gampe85b62f22015-09-09 13:15:38 -0700250class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000252 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000253 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254
Alexandre Rames67555f72014-11-18 10:55:16 +0000255 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000256 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100257 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
258 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000259 DCHECK(instruction_->IsCheckCast()
260 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000261
262 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
263 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000265 if (!is_fatal_) {
266 SaveLiveRegisters(codegen, locations);
267 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000268
269 // We're moving two locations to locations that could overlap, so we need a parallel
270 // move resolver.
271 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000272 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100273 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000274 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100275 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100276 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100277 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
278 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100281 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100282 instruction_,
283 instruction_->GetDexPc(),
284 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000285 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700286 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
288 } else {
289 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100290 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000291 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000294 if (!is_fatal_) {
295 RestoreLiveRegisters(codegen, locations);
296 __ b(GetExitLabel());
297 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298 }
299
Alexandre Rames9931f312015-06-19 14:47:01 +0100300 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
301
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302 bool IsFatal() const OVERRIDE { return is_fatal_; }
303
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000305 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
307 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
308};
309
Andreas Gampe85b62f22015-09-09 13:15:38 -0700310class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700311 public:
Aart Bik42249c32016-01-07 15:33:50 -0800312 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000313 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700314
315 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800316 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700317 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100318 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000319 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700320 }
321
Alexandre Rames9931f312015-06-19 14:47:01 +0100322 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
323
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700324 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700325 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
326};
327
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100328class ArraySetSlowPathARM : public SlowPathCode {
329 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000330 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100331
332 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
333 LocationSummary* locations = instruction_->GetLocations();
334 __ Bind(GetEntryLabel());
335 SaveLiveRegisters(codegen, locations);
336
337 InvokeRuntimeCallingConvention calling_convention;
338 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
339 parallel_move.AddMove(
340 locations->InAt(0),
341 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
342 Primitive::kPrimNot,
343 nullptr);
344 parallel_move.AddMove(
345 locations->InAt(1),
346 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
347 Primitive::kPrimInt,
348 nullptr);
349 parallel_move.AddMove(
350 locations->InAt(2),
351 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
352 Primitive::kPrimNot,
353 nullptr);
354 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
355
356 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100357 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000358 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100359 RestoreLiveRegisters(codegen, locations);
360 __ b(GetExitLabel());
361 }
362
363 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
364
365 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100366 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
367};
368
Roland Levillainc9285912015-12-18 10:38:42 +0000369// Slow path marking an object during a read barrier.
370class ReadBarrierMarkSlowPathARM : public SlowPathCode {
371 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100372 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
373 : SlowPathCode(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000374 DCHECK(kEmitCompilerReadBarrier);
375 }
376
377 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
380 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100381 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000382 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100383 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000384 DCHECK(instruction_->IsInstanceFieldGet() ||
385 instruction_->IsStaticFieldGet() ||
386 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100387 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000388 instruction_->IsLoadClass() ||
389 instruction_->IsLoadString() ||
390 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100391 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100392 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
393 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000394 << "Unexpected instruction in read barrier marking slow path: "
395 << instruction_->DebugName();
396
397 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100398 // No need to save live registers; it's taken care of by the
399 // entrypoint. Also, there is no need to update the stack mask,
400 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000401 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100402 DCHECK_NE(reg, SP);
403 DCHECK_NE(reg, LR);
404 DCHECK_NE(reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100405 // IP is used internally by the ReadBarrierMarkRegX entry point
406 // as a temporary, it cannot be the entry point's input/output.
407 DCHECK_NE(reg, IP);
Roland Levillain02b75802016-07-13 11:54:35 +0100408 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
409 // "Compact" slow path, saving two moves.
410 //
411 // Instead of using the standard runtime calling convention (input
412 // and output in R0):
413 //
414 // R0 <- obj
415 // R0 <- ReadBarrierMark(R0)
416 // obj <- R0
417 //
418 // we just use rX (the register holding `obj`) as input and output
419 // of a dedicated entrypoint:
420 //
421 // rX <- ReadBarrierMarkRegX(rX)
422 //
423 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700424 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100425 // This runtime call does not require a stack map.
426 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000427 __ b(GetExitLabel());
428 }
429
430 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000431 const Location obj_;
432
433 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
434};
435
Roland Levillain3b359c72015-11-17 19:35:12 +0000436// Slow path generating a read barrier for a heap reference.
437class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
438 public:
439 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
440 Location out,
441 Location ref,
442 Location obj,
443 uint32_t offset,
444 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000445 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000446 out_(out),
447 ref_(ref),
448 obj_(obj),
449 offset_(offset),
450 index_(index) {
451 DCHECK(kEmitCompilerReadBarrier);
452 // If `obj` is equal to `out` or `ref`, it means the initial object
453 // has been overwritten by (or after) the heap object reference load
454 // to be instrumented, e.g.:
455 //
456 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000457 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000458 //
459 // In that case, we have lost the information about the original
460 // object, and the emitted read barrier cannot work properly.
461 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
462 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
463 }
464
465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
467 LocationSummary* locations = instruction_->GetLocations();
468 Register reg_out = out_.AsRegister<Register>();
469 DCHECK(locations->CanCall());
470 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100471 DCHECK(instruction_->IsInstanceFieldGet() ||
472 instruction_->IsStaticFieldGet() ||
473 instruction_->IsArrayGet() ||
474 instruction_->IsInstanceOf() ||
475 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100476 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000477 << "Unexpected instruction in read barrier for heap reference slow path: "
478 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000479
480 __ Bind(GetEntryLabel());
481 SaveLiveRegisters(codegen, locations);
482
483 // We may have to change the index's value, but as `index_` is a
484 // constant member (like other "inputs" of this slow path),
485 // introduce a copy of it, `index`.
486 Location index = index_;
487 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100488 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000489 if (instruction_->IsArrayGet()) {
490 // Compute the actual memory offset and store it in `index`.
491 Register index_reg = index_.AsRegister<Register>();
492 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
493 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
494 // We are about to change the value of `index_reg` (see the
495 // calls to art::arm::Thumb2Assembler::Lsl and
496 // art::arm::Thumb2Assembler::AddConstant below), but it has
497 // not been saved by the previous call to
498 // art::SlowPathCode::SaveLiveRegisters, as it is a
499 // callee-save register --
500 // art::SlowPathCode::SaveLiveRegisters does not consider
501 // callee-save registers, as it has been designed with the
502 // assumption that callee-save registers are supposed to be
503 // handled by the called function. So, as a callee-save
504 // register, `index_reg` _would_ eventually be saved onto
505 // the stack, but it would be too late: we would have
506 // changed its value earlier. Therefore, we manually save
507 // it here into another freely available register,
508 // `free_reg`, chosen of course among the caller-save
509 // registers (as a callee-save `free_reg` register would
510 // exhibit the same problem).
511 //
512 // Note we could have requested a temporary register from
513 // the register allocator instead; but we prefer not to, as
514 // this is a slow path, and we know we can find a
515 // caller-save register that is available.
516 Register free_reg = FindAvailableCallerSaveRegister(codegen);
517 __ Mov(free_reg, index_reg);
518 index_reg = free_reg;
519 index = Location::RegisterLocation(index_reg);
520 } else {
521 // The initial register stored in `index_` has already been
522 // saved in the call to art::SlowPathCode::SaveLiveRegisters
523 // (as it is not a callee-save register), so we can freely
524 // use it.
525 }
526 // Shifting the index value contained in `index_reg` by the scale
527 // factor (2) cannot overflow in practice, as the runtime is
528 // unable to allocate object arrays with a size larger than
529 // 2^26 - 1 (that is, 2^28 - 4 bytes).
530 __ Lsl(index_reg, index_reg, TIMES_4);
531 static_assert(
532 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
533 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
534 __ AddConstant(index_reg, index_reg, offset_);
535 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100536 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
537 // intrinsics, `index_` is not shifted by a scale factor of 2
538 // (as in the case of ArrayGet), as it is actually an offset
539 // to an object field within an object.
540 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000541 DCHECK(instruction_->GetLocations()->Intrinsified());
542 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
543 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
544 << instruction_->AsInvoke()->GetIntrinsic();
545 DCHECK_EQ(offset_, 0U);
546 DCHECK(index_.IsRegisterPair());
547 // UnsafeGet's offset location is a register pair, the low
548 // part contains the correct offset.
549 index = index_.ToLow();
550 }
551 }
552
553 // We're moving two or three locations to locations that could
554 // overlap, so we need a parallel move resolver.
555 InvokeRuntimeCallingConvention calling_convention;
556 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
557 parallel_move.AddMove(ref_,
558 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
559 Primitive::kPrimNot,
560 nullptr);
561 parallel_move.AddMove(obj_,
562 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
563 Primitive::kPrimNot,
564 nullptr);
565 if (index.IsValid()) {
566 parallel_move.AddMove(index,
567 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
568 Primitive::kPrimInt,
569 nullptr);
570 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
571 } else {
572 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
573 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
574 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100575 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000576 CheckEntrypointTypes<
577 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
578 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
579
580 RestoreLiveRegisters(codegen, locations);
581 __ b(GetExitLabel());
582 }
583
584 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
585
586 private:
587 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
588 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
589 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
590 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
591 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
592 return static_cast<Register>(i);
593 }
594 }
595 // We shall never fail to find a free caller-save register, as
596 // there are more than two core caller-save registers on ARM
597 // (meaning it is possible to find one which is different from
598 // `ref` and `obj`).
599 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
600 LOG(FATAL) << "Could not find a free caller-save register";
601 UNREACHABLE();
602 }
603
Roland Levillain3b359c72015-11-17 19:35:12 +0000604 const Location out_;
605 const Location ref_;
606 const Location obj_;
607 const uint32_t offset_;
608 // An additional location containing an index to an array.
609 // Only used for HArrayGet and the UnsafeGetObject &
610 // UnsafeGetObjectVolatile intrinsics.
611 const Location index_;
612
613 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
614};
615
616// Slow path generating a read barrier for a GC root.
617class ReadBarrierForRootSlowPathARM : public SlowPathCode {
618 public:
619 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000620 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000621 DCHECK(kEmitCompilerReadBarrier);
622 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000623
624 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
625 LocationSummary* locations = instruction_->GetLocations();
626 Register reg_out = out_.AsRegister<Register>();
627 DCHECK(locations->CanCall());
628 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000629 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
630 << "Unexpected instruction in read barrier for GC root slow path: "
631 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000632
633 __ Bind(GetEntryLabel());
634 SaveLiveRegisters(codegen, locations);
635
636 InvokeRuntimeCallingConvention calling_convention;
637 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
638 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100639 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +0000640 instruction_,
641 instruction_->GetDexPc(),
642 this);
643 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
644 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
645
646 RestoreLiveRegisters(codegen, locations);
647 __ b(GetExitLabel());
648 }
649
650 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
651
652 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000653 const Location out_;
654 const Location root_;
655
656 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
657};
658
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000659#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100660// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
661#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700662
Aart Bike9f37602015-10-09 11:15:55 -0700663inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700664 switch (cond) {
665 case kCondEQ: return EQ;
666 case kCondNE: return NE;
667 case kCondLT: return LT;
668 case kCondLE: return LE;
669 case kCondGT: return GT;
670 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700671 case kCondB: return LO;
672 case kCondBE: return LS;
673 case kCondA: return HI;
674 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700675 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100676 LOG(FATAL) << "Unreachable";
677 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700678}
679
Aart Bike9f37602015-10-09 11:15:55 -0700680// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100681inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700682 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100683 case kCondEQ: return EQ;
684 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700685 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100686 case kCondLT: return LO;
687 case kCondLE: return LS;
688 case kCondGT: return HI;
689 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700690 // Unsigned remain unchanged.
691 case kCondB: return LO;
692 case kCondBE: return LS;
693 case kCondA: return HI;
694 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700695 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100696 LOG(FATAL) << "Unreachable";
697 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700698}
699
Vladimir Markod6e069b2016-01-18 11:11:01 +0000700inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
701 // The ARM condition codes can express all the necessary branches, see the
702 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
703 // There is no dex instruction or HIR that would need the missing conditions
704 // "equal or unordered" or "not equal".
705 switch (cond) {
706 case kCondEQ: return EQ;
707 case kCondNE: return NE /* unordered */;
708 case kCondLT: return gt_bias ? CC : LT /* unordered */;
709 case kCondLE: return gt_bias ? LS : LE /* unordered */;
710 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
711 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
712 default:
713 LOG(FATAL) << "UNREACHABLE";
714 UNREACHABLE();
715 }
716}
717
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100718void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100719 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100720}
721
722void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100723 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100724}
725
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100726size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
727 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
728 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100729}
730
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100731size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
732 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
733 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100734}
735
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000736size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
737 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
738 return kArmWordSize;
739}
740
741size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
742 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
743 return kArmWordSize;
744}
745
Calin Juravle34166012014-12-19 17:22:29 +0000746CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000747 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100748 const CompilerOptions& compiler_options,
749 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000750 : CodeGenerator(graph,
751 kNumberOfCoreRegisters,
752 kNumberOfSRegisters,
753 kNumberOfRegisterPairs,
754 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
755 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000756 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
757 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100758 compiler_options,
759 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100760 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100761 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100762 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100763 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100764 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000765 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000766 uint32_literals_(std::less<uint32_t>(),
767 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100768 method_patches_(MethodReferenceComparator(),
769 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
770 call_patches_(MethodReferenceComparator(),
771 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000772 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000773 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
774 boot_image_string_patches_(StringReferenceValueComparator(),
775 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
776 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100777 boot_image_type_patches_(TypeReferenceValueComparator(),
778 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
779 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000780 boot_image_address_patches_(std::less<uint32_t>(),
781 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700782 // Always save the LR register to mimic Quick.
783 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100784}
785
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000786void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
787 // Ensure that we fix up branches and literal loads and emit the literal pool.
788 __ FinalizeCode();
789
790 // Adjust native pc offsets in stack maps.
791 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
792 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
793 uint32_t new_position = __ GetAdjustedPosition(old_position);
794 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
795 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100796 // Adjust pc offsets for the disassembly information.
797 if (disasm_info_ != nullptr) {
798 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
799 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
800 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
801 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
802 it.second.start = __ GetAdjustedPosition(it.second.start);
803 it.second.end = __ GetAdjustedPosition(it.second.end);
804 }
805 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
806 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
807 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
808 }
809 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000810
811 CodeGenerator::Finalize(allocator);
812}
813
David Brazdil58282f42016-01-14 12:45:10 +0000814void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100815 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100816 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100817
818 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100819 blocked_core_registers_[SP] = true;
820 blocked_core_registers_[LR] = true;
821 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100822
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100823 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100824 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100825
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100826 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100827 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100828
David Brazdil58282f42016-01-14 12:45:10 +0000829 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100830 // Stubs do not save callee-save floating point registers. If the graph
831 // is debuggable, we need to deal with these registers differently. For
832 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000833 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
834 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
835 }
836 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100837
838 UpdateBlockedPairRegisters();
839}
840
841void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
842 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
843 ArmManagedRegister current =
844 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
845 if (blocked_core_registers_[current.AsRegisterPairLow()]
846 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
847 blocked_register_pairs_[i] = true;
848 }
849 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100850}
851
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100852InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800853 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100854 assembler_(codegen->GetAssembler()),
855 codegen_(codegen) {}
856
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000857void CodeGeneratorARM::ComputeSpillMask() {
858 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
859 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000860 // There is no easy instruction to restore just the PC on thumb2. We spill and
861 // restore another arbitrary register.
862 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000863 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
864 // We use vpush and vpop for saving and restoring floating point registers, which take
865 // a SRegister and the number of registers to save/restore after that SRegister. We
866 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
867 // but in the range.
868 if (fpu_spill_mask_ != 0) {
869 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
870 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
871 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
872 fpu_spill_mask_ |= (1 << i);
873 }
874 }
875}
876
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100877static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100878 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100879}
880
881static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100882 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100883}
884
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000885void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000886 bool skip_overflow_check =
887 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000888 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000889 __ Bind(&frame_entry_label_);
890
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000891 if (HasEmptyFrame()) {
892 return;
893 }
894
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100895 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000896 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
897 __ LoadFromOffset(kLoadWord, IP, IP, 0);
898 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100899 }
900
Andreas Gampe501fd632015-09-10 16:11:06 -0700901 __ PushList(core_spill_mask_);
902 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
903 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000904 if (fpu_spill_mask_ != 0) {
905 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
906 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100907 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100908 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000909 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100910 int adjust = GetFrameSize() - FrameEntrySpillSize();
911 __ AddConstant(SP, -adjust);
912 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100913 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000914}
915
916void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000917 if (HasEmptyFrame()) {
918 __ bx(LR);
919 return;
920 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100921 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100922 int adjust = GetFrameSize() - FrameEntrySpillSize();
923 __ AddConstant(SP, adjust);
924 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000925 if (fpu_spill_mask_ != 0) {
926 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
927 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -0700928 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100929 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000930 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700931 // Pop LR into PC to return.
932 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
933 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
934 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100935 __ cfi().RestoreState();
936 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000937}
938
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100939void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700940 Label* label = GetLabelOf(block);
941 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000942}
943
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100944Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100945 switch (type) {
946 case Primitive::kPrimBoolean:
947 case Primitive::kPrimByte:
948 case Primitive::kPrimChar:
949 case Primitive::kPrimShort:
950 case Primitive::kPrimInt:
951 case Primitive::kPrimNot: {
952 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000953 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100954 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100955 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100956 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000957 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100958 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100959 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100960
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000961 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100962 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000963 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100964 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000965 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100966 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000967 if (calling_convention.GetRegisterAt(index) == R1) {
968 // Skip R1, and use R2_R3 instead.
969 gp_index_++;
970 index++;
971 }
972 }
973 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
974 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000975 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100976
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000977 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000978 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000980 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
981 }
982 }
983
984 case Primitive::kPrimFloat: {
985 uint32_t stack_index = stack_index_++;
986 if (float_index_ % 2 == 0) {
987 float_index_ = std::max(double_index_, float_index_);
988 }
989 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
990 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
991 } else {
992 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
993 }
994 }
995
996 case Primitive::kPrimDouble: {
997 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
998 uint32_t stack_index = stack_index_;
999 stack_index_ += 2;
1000 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1001 uint32_t index = double_index_;
1002 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001003 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001004 calling_convention.GetFpuRegisterAt(index),
1005 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001006 DCHECK(ExpectedPairLayout(result));
1007 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001008 } else {
1009 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010 }
1011 }
1012
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013 case Primitive::kPrimVoid:
1014 LOG(FATAL) << "Unexpected parameter type " << type;
1015 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001016 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001017 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001018}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001019
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001020Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001021 switch (type) {
1022 case Primitive::kPrimBoolean:
1023 case Primitive::kPrimByte:
1024 case Primitive::kPrimChar:
1025 case Primitive::kPrimShort:
1026 case Primitive::kPrimInt:
1027 case Primitive::kPrimNot: {
1028 return Location::RegisterLocation(R0);
1029 }
1030
1031 case Primitive::kPrimFloat: {
1032 return Location::FpuRegisterLocation(S0);
1033 }
1034
1035 case Primitive::kPrimLong: {
1036 return Location::RegisterPairLocation(R0, R1);
1037 }
1038
1039 case Primitive::kPrimDouble: {
1040 return Location::FpuRegisterPairLocation(S0, S1);
1041 }
1042
1043 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001044 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001045 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001046
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001047 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001048}
1049
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001050Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1051 return Location::RegisterLocation(kMethodRegisterArgument);
1052}
1053
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054void CodeGeneratorARM::Move32(Location destination, Location source) {
1055 if (source.Equals(destination)) {
1056 return;
1057 }
1058 if (destination.IsRegister()) {
1059 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001060 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001062 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001064 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001066 } else if (destination.IsFpuRegister()) {
1067 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001068 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001069 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001070 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001071 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001072 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001074 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001075 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001077 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001079 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001081 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001082 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1083 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 }
1085 }
1086}
1087
1088void CodeGeneratorARM::Move64(Location destination, Location source) {
1089 if (source.Equals(destination)) {
1090 return;
1091 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 if (destination.IsRegisterPair()) {
1093 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001094 EmitParallelMoves(
1095 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1096 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001097 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001098 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001099 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1100 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001102 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001103 } else if (source.IsFpuRegisterPair()) {
1104 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1105 destination.AsRegisterPairHigh<Register>(),
1106 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 } else {
1108 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001109 DCHECK(ExpectedPairLayout(destination));
1110 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1111 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001113 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001114 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001115 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1116 SP,
1117 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001118 } else if (source.IsRegisterPair()) {
1119 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1120 source.AsRegisterPairLow<Register>(),
1121 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001123 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001124 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125 } else {
1126 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001127 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001128 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001129 if (source.AsRegisterPairLow<Register>() == R1) {
1130 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001131 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1132 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001133 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001134 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 SP, destination.GetStackIndex());
1136 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001137 } else if (source.IsFpuRegisterPair()) {
1138 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1139 SP,
1140 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001141 } else {
1142 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001143 EmitParallelMoves(
1144 Location::StackSlot(source.GetStackIndex()),
1145 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001146 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001147 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001148 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1149 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150 }
1151 }
1152}
1153
Calin Juravle175dc732015-08-25 15:42:32 +01001154void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1155 DCHECK(location.IsRegister());
1156 __ LoadImmediate(location.AsRegister<Register>(), value);
1157}
1158
Calin Juravlee460d1d2015-09-29 04:52:17 +01001159void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001160 HParallelMove move(GetGraph()->GetArena());
1161 move.AddMove(src, dst, dst_type, nullptr);
1162 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001163}
1164
1165void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1166 if (location.IsRegister()) {
1167 locations->AddTemp(location);
1168 } else if (location.IsRegisterPair()) {
1169 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1170 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1171 } else {
1172 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1173 }
1174}
1175
Calin Juravle175dc732015-08-25 15:42:32 +01001176void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1177 HInstruction* instruction,
1178 uint32_t dex_pc,
1179 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001180 ValidateInvokeRuntime(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001181 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001182 if (EntrypointRequiresStackMap(entrypoint)) {
1183 RecordPcInfo(instruction, dex_pc, slow_path);
1184 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001185}
1186
Roland Levillaindec8f632016-07-22 17:10:06 +01001187void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1188 HInstruction* instruction,
1189 SlowPathCode* slow_path) {
1190 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001191 GenerateInvokeRuntime(entry_point_offset);
1192}
1193
1194void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001195 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1196 __ blx(LR);
1197}
1198
David Brazdilfc6a86a2015-06-26 10:33:45 +00001199void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001200 DCHECK(!successor->IsExitBlock());
1201
1202 HBasicBlock* block = got->GetBlock();
1203 HInstruction* previous = got->GetPrevious();
1204
1205 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001206 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001207 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1208 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1209 return;
1210 }
1211
1212 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1213 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1214 }
1215 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001216 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001217 }
1218}
1219
David Brazdilfc6a86a2015-06-26 10:33:45 +00001220void LocationsBuilderARM::VisitGoto(HGoto* got) {
1221 got->SetLocations(nullptr);
1222}
1223
1224void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1225 HandleGoto(got, got->GetSuccessor());
1226}
1227
1228void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1229 try_boundary->SetLocations(nullptr);
1230}
1231
1232void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1233 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1234 if (!successor->IsExitBlock()) {
1235 HandleGoto(try_boundary, successor);
1236 }
1237}
1238
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001239void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001240 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001241}
1242
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001243void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001244}
1245
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001246void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1247 Primitive::Type type = instruction->InputAt(0)->GetType();
1248 Location lhs_loc = instruction->GetLocations()->InAt(0);
1249 Location rhs_loc = instruction->GetLocations()->InAt(1);
1250 if (rhs_loc.IsConstant()) {
1251 // 0.0 is the only immediate that can be encoded directly in
1252 // a VCMP instruction.
1253 //
1254 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1255 // specify that in a floating-point comparison, positive zero
1256 // and negative zero are considered equal, so we can use the
1257 // literal 0.0 for both cases here.
1258 //
1259 // Note however that some methods (Float.equal, Float.compare,
1260 // Float.compareTo, Double.equal, Double.compare,
1261 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1262 // StrictMath.min) consider 0.0 to be (strictly) greater than
1263 // -0.0. So if we ever translate calls to these methods into a
1264 // HCompare instruction, we must handle the -0.0 case with
1265 // care here.
1266 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1267 if (type == Primitive::kPrimFloat) {
1268 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1269 } else {
1270 DCHECK_EQ(type, Primitive::kPrimDouble);
1271 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1272 }
1273 } else {
1274 if (type == Primitive::kPrimFloat) {
1275 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1276 } else {
1277 DCHECK_EQ(type, Primitive::kPrimDouble);
1278 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1279 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1280 }
1281 }
1282}
1283
Roland Levillain4fa13f62015-07-06 18:11:54 +01001284void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1285 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001286 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001287 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001288 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001289}
1290
1291void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1292 Label* true_label,
1293 Label* false_label) {
1294 LocationSummary* locations = cond->GetLocations();
1295 Location left = locations->InAt(0);
1296 Location right = locations->InAt(1);
1297 IfCondition if_cond = cond->GetCondition();
1298
1299 Register left_high = left.AsRegisterPairHigh<Register>();
1300 Register left_low = left.AsRegisterPairLow<Register>();
1301 IfCondition true_high_cond = if_cond;
1302 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001303 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001304
1305 // Set the conditions for the test, remembering that == needs to be
1306 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001307 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001308 switch (if_cond) {
1309 case kCondEQ:
1310 case kCondNE:
1311 // Nothing to do.
1312 break;
1313 case kCondLT:
1314 false_high_cond = kCondGT;
1315 break;
1316 case kCondLE:
1317 true_high_cond = kCondLT;
1318 break;
1319 case kCondGT:
1320 false_high_cond = kCondLT;
1321 break;
1322 case kCondGE:
1323 true_high_cond = kCondGT;
1324 break;
Aart Bike9f37602015-10-09 11:15:55 -07001325 case kCondB:
1326 false_high_cond = kCondA;
1327 break;
1328 case kCondBE:
1329 true_high_cond = kCondB;
1330 break;
1331 case kCondA:
1332 false_high_cond = kCondB;
1333 break;
1334 case kCondAE:
1335 true_high_cond = kCondA;
1336 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001337 }
1338 if (right.IsConstant()) {
1339 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1340 int32_t val_low = Low32Bits(value);
1341 int32_t val_high = High32Bits(value);
1342
Vladimir Markoac6ac102015-12-17 12:14:00 +00001343 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001344 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001345 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001346 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001347 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001348 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001349 __ b(true_label, ARMCondition(true_high_cond));
1350 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001351 }
1352 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001353 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 } else {
1355 Register right_high = right.AsRegisterPairHigh<Register>();
1356 Register right_low = right.AsRegisterPairLow<Register>();
1357
1358 __ cmp(left_high, ShifterOperand(right_high));
1359 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001360 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001361 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001362 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001363 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001364 __ b(true_label, ARMCondition(true_high_cond));
1365 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001366 }
1367 // Must be equal high, so compare the lows.
1368 __ cmp(left_low, ShifterOperand(right_low));
1369 }
1370 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001371 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001372 __ b(true_label, final_condition);
1373}
1374
David Brazdil0debae72015-11-12 18:37:00 +00001375void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1376 Label* true_target_in,
1377 Label* false_target_in) {
1378 // Generated branching requires both targets to be explicit. If either of the
1379 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1380 Label fallthrough_target;
1381 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1382 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1383
Roland Levillain4fa13f62015-07-06 18:11:54 +01001384 Primitive::Type type = condition->InputAt(0)->GetType();
1385 switch (type) {
1386 case Primitive::kPrimLong:
1387 GenerateLongComparesAndJumps(condition, true_target, false_target);
1388 break;
1389 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001390 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001391 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001392 GenerateFPJumps(condition, true_target, false_target);
1393 break;
1394 default:
1395 LOG(FATAL) << "Unexpected compare type " << type;
1396 }
1397
David Brazdil0debae72015-11-12 18:37:00 +00001398 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 __ b(false_target);
1400 }
David Brazdil0debae72015-11-12 18:37:00 +00001401
1402 if (fallthrough_target.IsLinked()) {
1403 __ Bind(&fallthrough_target);
1404 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001405}
1406
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001407void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001408 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001409 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001410 Label* false_target) {
1411 HInstruction* cond = instruction->InputAt(condition_input_index);
1412
1413 if (true_target == nullptr && false_target == nullptr) {
1414 // Nothing to do. The code always falls through.
1415 return;
1416 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001417 // Constant condition, statically compared against "true" (integer value 1).
1418 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001419 if (true_target != nullptr) {
1420 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001421 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001422 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001423 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001424 if (false_target != nullptr) {
1425 __ b(false_target);
1426 }
1427 }
1428 return;
1429 }
1430
1431 // The following code generates these patterns:
1432 // (1) true_target == nullptr && false_target != nullptr
1433 // - opposite condition true => branch to false_target
1434 // (2) true_target != nullptr && false_target == nullptr
1435 // - condition true => branch to true_target
1436 // (3) true_target != nullptr && false_target != nullptr
1437 // - condition true => branch to true_target
1438 // - branch to false_target
1439 if (IsBooleanValueOrMaterializedCondition(cond)) {
1440 // Condition has been materialized, compare the output to 0.
1441 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1442 DCHECK(cond_val.IsRegister());
1443 if (true_target == nullptr) {
1444 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1445 } else {
1446 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001447 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001448 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001449 // Condition has not been materialized. Use its inputs as the comparison and
1450 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001451 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001452
1453 // If this is a long or FP comparison that has been folded into
1454 // the HCondition, generate the comparison directly.
1455 Primitive::Type type = condition->InputAt(0)->GetType();
1456 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1457 GenerateCompareTestAndBranch(condition, true_target, false_target);
1458 return;
1459 }
1460
1461 LocationSummary* locations = cond->GetLocations();
1462 DCHECK(locations->InAt(0).IsRegister());
1463 Register left = locations->InAt(0).AsRegister<Register>();
1464 Location right = locations->InAt(1);
1465 if (right.IsRegister()) {
1466 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001467 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001468 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001469 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001470 }
1471 if (true_target == nullptr) {
1472 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1473 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001474 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001475 }
Dave Allison20dfc792014-06-16 20:44:29 -07001476 }
David Brazdil0debae72015-11-12 18:37:00 +00001477
1478 // If neither branch falls through (case 3), the conditional branch to `true_target`
1479 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1480 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001481 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001482 }
1483}
1484
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001485void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001486 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1487 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001488 locations->SetInAt(0, Location::RequiresRegister());
1489 }
1490}
1491
1492void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001493 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1494 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1495 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1496 nullptr : codegen_->GetLabelOf(true_successor);
1497 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1498 nullptr : codegen_->GetLabelOf(false_successor);
1499 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001500}
1501
1502void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1503 LocationSummary* locations = new (GetGraph()->GetArena())
1504 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01001505 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001506 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001507 locations->SetInAt(0, Location::RequiresRegister());
1508 }
1509}
1510
1511void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001512 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001513 GenerateTestAndBranch(deoptimize,
1514 /* condition_input_index */ 0,
1515 slow_path->GetEntryLabel(),
1516 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001517}
Dave Allison20dfc792014-06-16 20:44:29 -07001518
David Brazdil74eb1b22015-12-14 11:44:01 +00001519void LocationsBuilderARM::VisitSelect(HSelect* select) {
1520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1521 if (Primitive::IsFloatingPointType(select->GetType())) {
1522 locations->SetInAt(0, Location::RequiresFpuRegister());
1523 locations->SetInAt(1, Location::RequiresFpuRegister());
1524 } else {
1525 locations->SetInAt(0, Location::RequiresRegister());
1526 locations->SetInAt(1, Location::RequiresRegister());
1527 }
1528 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1529 locations->SetInAt(2, Location::RequiresRegister());
1530 }
1531 locations->SetOut(Location::SameAsFirstInput());
1532}
1533
1534void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1535 LocationSummary* locations = select->GetLocations();
1536 Label false_target;
1537 GenerateTestAndBranch(select,
1538 /* condition_input_index */ 2,
1539 /* true_target */ nullptr,
1540 &false_target);
1541 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1542 __ Bind(&false_target);
1543}
1544
David Srbecky0cf44932015-12-09 14:09:59 +00001545void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1546 new (GetGraph()->GetArena()) LocationSummary(info);
1547}
1548
David Srbeckyd28f4a02016-03-14 17:14:24 +00001549void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1550 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001551}
1552
1553void CodeGeneratorARM::GenerateNop() {
1554 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001555}
1556
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001557void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001558 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001559 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001560 // Handle the long/FP comparisons made in instruction simplification.
1561 switch (cond->InputAt(0)->GetType()) {
1562 case Primitive::kPrimLong:
1563 locations->SetInAt(0, Location::RequiresRegister());
1564 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001565 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001566 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1567 }
1568 break;
1569
1570 case Primitive::kPrimFloat:
1571 case Primitive::kPrimDouble:
1572 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001573 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001574 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1576 }
1577 break;
1578
1579 default:
1580 locations->SetInAt(0, Location::RequiresRegister());
1581 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001582 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1584 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001585 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001586}
1587
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001588void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001589 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001590 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001591 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001592
1593 LocationSummary* locations = cond->GetLocations();
1594 Location left = locations->InAt(0);
1595 Location right = locations->InAt(1);
1596 Register out = locations->Out().AsRegister<Register>();
1597 Label true_label, false_label;
1598
1599 switch (cond->InputAt(0)->GetType()) {
1600 default: {
1601 // Integer case.
1602 if (right.IsRegister()) {
1603 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1604 } else {
1605 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001606 __ CmpConstant(left.AsRegister<Register>(),
1607 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001608 }
Aart Bike9f37602015-10-09 11:15:55 -07001609 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001610 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001611 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001612 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001613 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001614 return;
1615 }
1616 case Primitive::kPrimLong:
1617 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1618 break;
1619 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001620 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001621 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001622 GenerateFPJumps(cond, &true_label, &false_label);
1623 break;
1624 }
1625
1626 // Convert the jumps into the result.
1627 Label done_label;
1628
1629 // False case: result = 0.
1630 __ Bind(&false_label);
1631 __ LoadImmediate(out, 0);
1632 __ b(&done_label);
1633
1634 // True case: result = 1.
1635 __ Bind(&true_label);
1636 __ LoadImmediate(out, 1);
1637 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001638}
1639
1640void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001641 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001642}
1643
1644void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001645 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001646}
1647
1648void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001649 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001650}
1651
1652void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001653 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001654}
1655
1656void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001657 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001658}
1659
1660void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001661 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001662}
1663
1664void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001665 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001666}
1667
1668void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001669 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001670}
1671
1672void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001673 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001674}
1675
1676void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001677 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001678}
1679
1680void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001681 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001682}
1683
1684void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001685 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001686}
1687
Aart Bike9f37602015-10-09 11:15:55 -07001688void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001689 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001690}
1691
1692void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001693 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001694}
1695
1696void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001697 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001698}
1699
1700void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001701 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001702}
1703
1704void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001705 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001706}
1707
1708void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001709 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001710}
1711
1712void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001713 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001714}
1715
1716void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001717 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001718}
1719
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001720void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001721 LocationSummary* locations =
1722 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001723 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001724}
1725
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001726void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001727 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001728}
1729
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001730void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1731 LocationSummary* locations =
1732 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1733 locations->SetOut(Location::ConstantLocation(constant));
1734}
1735
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001736void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001737 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001738}
1739
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001740void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001741 LocationSummary* locations =
1742 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001743 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001744}
1745
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001746void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001747 // Will be generated at use site.
1748}
1749
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001750void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1751 LocationSummary* locations =
1752 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1753 locations->SetOut(Location::ConstantLocation(constant));
1754}
1755
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001756void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001757 // Will be generated at use site.
1758}
1759
1760void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1761 LocationSummary* locations =
1762 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1763 locations->SetOut(Location::ConstantLocation(constant));
1764}
1765
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001766void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001767 // Will be generated at use site.
1768}
1769
Calin Juravle27df7582015-04-17 19:12:31 +01001770void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1771 memory_barrier->SetLocations(nullptr);
1772}
1773
1774void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001775 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001776}
1777
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001779 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001780}
1781
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001782void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001784}
1785
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001786void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001787 LocationSummary* locations =
1788 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001789 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001790}
1791
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001792void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001793 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001794}
1795
Calin Juravle175dc732015-08-25 15:42:32 +01001796void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1797 // The trampoline uses the same calling convention as dex calling conventions,
1798 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1799 // the method_idx.
1800 HandleInvoke(invoke);
1801}
1802
1803void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1804 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1805}
1806
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001807void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001808 // Explicit clinit checks triggered by static invokes must have been pruned by
1809 // art::PrepareForRegisterAllocation.
1810 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001811
Vladimir Marko68c981f2016-08-26 13:13:33 +01001812 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001813 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001814 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1815 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1816 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001817 return;
1818 }
1819
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001820 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001821
1822 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1823 if (invoke->HasPcRelativeDexCache()) {
1824 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1825 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001826}
1827
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001828static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1829 if (invoke->GetLocations()->Intrinsified()) {
1830 IntrinsicCodeGeneratorARM intrinsic(codegen);
1831 intrinsic.Dispatch(invoke);
1832 return true;
1833 }
1834 return false;
1835}
1836
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001837void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001838 // Explicit clinit checks triggered by static invokes must have been pruned by
1839 // art::PrepareForRegisterAllocation.
1840 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001841
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001842 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1843 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001844 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001845
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001846 LocationSummary* locations = invoke->GetLocations();
1847 codegen_->GenerateStaticOrDirectCall(
1848 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001849 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001850}
1851
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001852void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001853 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001854 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001855}
1856
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001857void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01001858 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001859 if (intrinsic.TryDispatch(invoke)) {
1860 return;
1861 }
1862
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001863 HandleInvoke(invoke);
1864}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001865
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001866void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001867 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1868 return;
1869 }
1870
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001871 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001872 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001873 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001874}
1875
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001876void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1877 HandleInvoke(invoke);
1878 // Add the hidden argument.
1879 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1880}
1881
1882void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1883 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001884 LocationSummary* locations = invoke->GetLocations();
1885 Register temp = locations->GetTemp(0).AsRegister<Register>();
1886 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001887 Location receiver = locations->InAt(0);
1888 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1889
Roland Levillain3b359c72015-11-17 19:35:12 +00001890 // Set the hidden argument. This is safe to do this here, as R12
1891 // won't be modified thereafter, before the `blx` (call) instruction.
1892 DCHECK_EQ(R12, hidden_reg);
1893 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001894
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001895 if (receiver.IsStackSlot()) {
1896 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001897 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001898 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1899 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001900 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001901 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001902 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001903 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001904 // Instead of simply (possibly) unpoisoning `temp` here, we should
1905 // emit a read barrier for the previous class reference load.
1906 // However this is not required in practice, as this is an
1907 // intermediate/temporary reference and because the current
1908 // concurrent copying collector keeps the from-space memory
1909 // intact/accessible until the end of the marking phase (the
1910 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001911 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001912 __ LoadFromOffset(kLoadWord, temp, temp,
1913 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1914 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00001915 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001916 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001917 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001918 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07001919 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001920 // LR = temp->GetEntryPoint();
1921 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1922 // LR();
1923 __ blx(LR);
1924 DCHECK(!codegen_->IsLeafMethod());
1925 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1926}
1927
Roland Levillain88cb1752014-10-20 16:36:47 +01001928void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1929 LocationSummary* locations =
1930 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1931 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001932 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001933 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001934 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1935 break;
1936 }
1937 case Primitive::kPrimLong: {
1938 locations->SetInAt(0, Location::RequiresRegister());
1939 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001940 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001941 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001942
Roland Levillain88cb1752014-10-20 16:36:47 +01001943 case Primitive::kPrimFloat:
1944 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001945 locations->SetInAt(0, Location::RequiresFpuRegister());
1946 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001947 break;
1948
1949 default:
1950 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1951 }
1952}
1953
1954void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1955 LocationSummary* locations = neg->GetLocations();
1956 Location out = locations->Out();
1957 Location in = locations->InAt(0);
1958 switch (neg->GetResultType()) {
1959 case Primitive::kPrimInt:
1960 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001961 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001962 break;
1963
1964 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001965 DCHECK(in.IsRegisterPair());
1966 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1967 __ rsbs(out.AsRegisterPairLow<Register>(),
1968 in.AsRegisterPairLow<Register>(),
1969 ShifterOperand(0));
1970 // We cannot emit an RSC (Reverse Subtract with Carry)
1971 // instruction here, as it does not exist in the Thumb-2
1972 // instruction set. We use the following approach
1973 // using SBC and SUB instead.
1974 //
1975 // out.hi = -C
1976 __ sbc(out.AsRegisterPairHigh<Register>(),
1977 out.AsRegisterPairHigh<Register>(),
1978 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1979 // out.hi = out.hi - in.hi
1980 __ sub(out.AsRegisterPairHigh<Register>(),
1981 out.AsRegisterPairHigh<Register>(),
1982 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1983 break;
1984
Roland Levillain88cb1752014-10-20 16:36:47 +01001985 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001986 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001987 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001988 break;
1989
Roland Levillain88cb1752014-10-20 16:36:47 +01001990 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001991 DCHECK(in.IsFpuRegisterPair());
1992 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1993 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001994 break;
1995
1996 default:
1997 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1998 }
1999}
2000
Roland Levillaindff1f282014-11-05 14:15:05 +00002001void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002002 Primitive::Type result_type = conversion->GetResultType();
2003 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002004 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002005
Roland Levillain5b3ee562015-04-14 16:02:41 +01002006 // The float-to-long, double-to-long and long-to-float type conversions
2007 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002008 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002009 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2010 && result_type == Primitive::kPrimLong)
2011 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002012 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002013 : LocationSummary::kNoCall;
2014 LocationSummary* locations =
2015 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2016
David Brazdilb2bd1c52015-03-25 11:17:37 +00002017 // The Java language does not allow treating boolean as an integral type but
2018 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002019
Roland Levillaindff1f282014-11-05 14:15:05 +00002020 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002021 case Primitive::kPrimByte:
2022 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002023 case Primitive::kPrimLong:
2024 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002025 case Primitive::kPrimBoolean:
2026 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002027 case Primitive::kPrimShort:
2028 case Primitive::kPrimInt:
2029 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002030 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002031 locations->SetInAt(0, Location::RequiresRegister());
2032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2033 break;
2034
2035 default:
2036 LOG(FATAL) << "Unexpected type conversion from " << input_type
2037 << " to " << result_type;
2038 }
2039 break;
2040
Roland Levillain01a8d712014-11-14 16:27:39 +00002041 case Primitive::kPrimShort:
2042 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002043 case Primitive::kPrimLong:
2044 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002045 case Primitive::kPrimBoolean:
2046 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002047 case Primitive::kPrimByte:
2048 case Primitive::kPrimInt:
2049 case Primitive::kPrimChar:
2050 // Processing a Dex `int-to-short' instruction.
2051 locations->SetInAt(0, Location::RequiresRegister());
2052 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2053 break;
2054
2055 default:
2056 LOG(FATAL) << "Unexpected type conversion from " << input_type
2057 << " to " << result_type;
2058 }
2059 break;
2060
Roland Levillain946e1432014-11-11 17:35:19 +00002061 case Primitive::kPrimInt:
2062 switch (input_type) {
2063 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002064 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002065 locations->SetInAt(0, Location::Any());
2066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2067 break;
2068
2069 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002070 // Processing a Dex `float-to-int' instruction.
2071 locations->SetInAt(0, Location::RequiresFpuRegister());
2072 locations->SetOut(Location::RequiresRegister());
2073 locations->AddTemp(Location::RequiresFpuRegister());
2074 break;
2075
Roland Levillain946e1432014-11-11 17:35:19 +00002076 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002077 // Processing a Dex `double-to-int' instruction.
2078 locations->SetInAt(0, Location::RequiresFpuRegister());
2079 locations->SetOut(Location::RequiresRegister());
2080 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002081 break;
2082
2083 default:
2084 LOG(FATAL) << "Unexpected type conversion from " << input_type
2085 << " to " << result_type;
2086 }
2087 break;
2088
Roland Levillaindff1f282014-11-05 14:15:05 +00002089 case Primitive::kPrimLong:
2090 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002091 case Primitive::kPrimBoolean:
2092 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002093 case Primitive::kPrimByte:
2094 case Primitive::kPrimShort:
2095 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002096 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002097 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002098 locations->SetInAt(0, Location::RequiresRegister());
2099 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2100 break;
2101
Roland Levillain624279f2014-12-04 11:54:28 +00002102 case Primitive::kPrimFloat: {
2103 // Processing a Dex `float-to-long' instruction.
2104 InvokeRuntimeCallingConvention calling_convention;
2105 locations->SetInAt(0, Location::FpuRegisterLocation(
2106 calling_convention.GetFpuRegisterAt(0)));
2107 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2108 break;
2109 }
2110
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002111 case Primitive::kPrimDouble: {
2112 // Processing a Dex `double-to-long' instruction.
2113 InvokeRuntimeCallingConvention calling_convention;
2114 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2115 calling_convention.GetFpuRegisterAt(0),
2116 calling_convention.GetFpuRegisterAt(1)));
2117 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002118 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002119 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002120
2121 default:
2122 LOG(FATAL) << "Unexpected type conversion from " << input_type
2123 << " to " << result_type;
2124 }
2125 break;
2126
Roland Levillain981e4542014-11-14 11:47:14 +00002127 case Primitive::kPrimChar:
2128 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002129 case Primitive::kPrimLong:
2130 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002131 case Primitive::kPrimBoolean:
2132 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002133 case Primitive::kPrimByte:
2134 case Primitive::kPrimShort:
2135 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002136 // Processing a Dex `int-to-char' instruction.
2137 locations->SetInAt(0, Location::RequiresRegister());
2138 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2139 break;
2140
2141 default:
2142 LOG(FATAL) << "Unexpected type conversion from " << input_type
2143 << " to " << result_type;
2144 }
2145 break;
2146
Roland Levillaindff1f282014-11-05 14:15:05 +00002147 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002148 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002149 case Primitive::kPrimBoolean:
2150 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002151 case Primitive::kPrimByte:
2152 case Primitive::kPrimShort:
2153 case Primitive::kPrimInt:
2154 case Primitive::kPrimChar:
2155 // Processing a Dex `int-to-float' instruction.
2156 locations->SetInAt(0, Location::RequiresRegister());
2157 locations->SetOut(Location::RequiresFpuRegister());
2158 break;
2159
Roland Levillain5b3ee562015-04-14 16:02:41 +01002160 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002161 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002162 InvokeRuntimeCallingConvention calling_convention;
2163 locations->SetInAt(0, Location::RegisterPairLocation(
2164 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2165 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002166 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002167 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002168
Roland Levillaincff13742014-11-17 14:32:17 +00002169 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002170 // Processing a Dex `double-to-float' instruction.
2171 locations->SetInAt(0, Location::RequiresFpuRegister());
2172 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002173 break;
2174
2175 default:
2176 LOG(FATAL) << "Unexpected type conversion from " << input_type
2177 << " to " << result_type;
2178 };
2179 break;
2180
Roland Levillaindff1f282014-11-05 14:15:05 +00002181 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002182 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002183 case Primitive::kPrimBoolean:
2184 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002185 case Primitive::kPrimByte:
2186 case Primitive::kPrimShort:
2187 case Primitive::kPrimInt:
2188 case Primitive::kPrimChar:
2189 // Processing a Dex `int-to-double' instruction.
2190 locations->SetInAt(0, Location::RequiresRegister());
2191 locations->SetOut(Location::RequiresFpuRegister());
2192 break;
2193
2194 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002195 // Processing a Dex `long-to-double' instruction.
2196 locations->SetInAt(0, Location::RequiresRegister());
2197 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002198 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002199 locations->AddTemp(Location::RequiresFpuRegister());
2200 break;
2201
Roland Levillaincff13742014-11-17 14:32:17 +00002202 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002203 // Processing a Dex `float-to-double' instruction.
2204 locations->SetInAt(0, Location::RequiresFpuRegister());
2205 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002206 break;
2207
2208 default:
2209 LOG(FATAL) << "Unexpected type conversion from " << input_type
2210 << " to " << result_type;
2211 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002212 break;
2213
2214 default:
2215 LOG(FATAL) << "Unexpected type conversion from " << input_type
2216 << " to " << result_type;
2217 }
2218}
2219
2220void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2221 LocationSummary* locations = conversion->GetLocations();
2222 Location out = locations->Out();
2223 Location in = locations->InAt(0);
2224 Primitive::Type result_type = conversion->GetResultType();
2225 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002226 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002227 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002228 case Primitive::kPrimByte:
2229 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002230 case Primitive::kPrimLong:
2231 // Type conversion from long to byte is a result of code transformations.
2232 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2233 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002234 case Primitive::kPrimBoolean:
2235 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002236 case Primitive::kPrimShort:
2237 case Primitive::kPrimInt:
2238 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002239 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002240 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002241 break;
2242
2243 default:
2244 LOG(FATAL) << "Unexpected type conversion from " << input_type
2245 << " to " << result_type;
2246 }
2247 break;
2248
Roland Levillain01a8d712014-11-14 16:27:39 +00002249 case Primitive::kPrimShort:
2250 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002251 case Primitive::kPrimLong:
2252 // Type conversion from long to short is a result of code transformations.
2253 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2254 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002255 case Primitive::kPrimBoolean:
2256 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002257 case Primitive::kPrimByte:
2258 case Primitive::kPrimInt:
2259 case Primitive::kPrimChar:
2260 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002262 break;
2263
2264 default:
2265 LOG(FATAL) << "Unexpected type conversion from " << input_type
2266 << " to " << result_type;
2267 }
2268 break;
2269
Roland Levillain946e1432014-11-11 17:35:19 +00002270 case Primitive::kPrimInt:
2271 switch (input_type) {
2272 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002273 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002274 DCHECK(out.IsRegister());
2275 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002276 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002277 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002278 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002279 } else {
2280 DCHECK(in.IsConstant());
2281 DCHECK(in.GetConstant()->IsLongConstant());
2282 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002283 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002284 }
2285 break;
2286
Roland Levillain3f8f9362014-12-02 17:45:01 +00002287 case Primitive::kPrimFloat: {
2288 // Processing a Dex `float-to-int' instruction.
2289 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002290 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002291 __ vmovrs(out.AsRegister<Register>(), temp);
2292 break;
2293 }
2294
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002295 case Primitive::kPrimDouble: {
2296 // Processing a Dex `double-to-int' instruction.
2297 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002298 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002299 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002300 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002301 }
Roland Levillain946e1432014-11-11 17:35:19 +00002302
2303 default:
2304 LOG(FATAL) << "Unexpected type conversion from " << input_type
2305 << " to " << result_type;
2306 }
2307 break;
2308
Roland Levillaindff1f282014-11-05 14:15:05 +00002309 case Primitive::kPrimLong:
2310 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002311 case Primitive::kPrimBoolean:
2312 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002313 case Primitive::kPrimByte:
2314 case Primitive::kPrimShort:
2315 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002316 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002317 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002318 DCHECK(out.IsRegisterPair());
2319 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002320 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 // Sign extension.
2322 __ Asr(out.AsRegisterPairHigh<Register>(),
2323 out.AsRegisterPairLow<Register>(),
2324 31);
2325 break;
2326
2327 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002328 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002329 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002330 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002331 break;
2332
Roland Levillaindff1f282014-11-05 14:15:05 +00002333 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002334 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002335 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002336 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002337 break;
2338
2339 default:
2340 LOG(FATAL) << "Unexpected type conversion from " << input_type
2341 << " to " << result_type;
2342 }
2343 break;
2344
Roland Levillain981e4542014-11-14 11:47:14 +00002345 case Primitive::kPrimChar:
2346 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002347 case Primitive::kPrimLong:
2348 // Type conversion from long to char is a result of code transformations.
2349 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2350 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002351 case Primitive::kPrimBoolean:
2352 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002353 case Primitive::kPrimByte:
2354 case Primitive::kPrimShort:
2355 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002356 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002358 break;
2359
2360 default:
2361 LOG(FATAL) << "Unexpected type conversion from " << input_type
2362 << " to " << result_type;
2363 }
2364 break;
2365
Roland Levillaindff1f282014-11-05 14:15:05 +00002366 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002367 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002368 case Primitive::kPrimBoolean:
2369 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002370 case Primitive::kPrimByte:
2371 case Primitive::kPrimShort:
2372 case Primitive::kPrimInt:
2373 case Primitive::kPrimChar: {
2374 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2376 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002377 break;
2378 }
2379
Roland Levillain5b3ee562015-04-14 16:02:41 +01002380 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002381 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002382 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002383 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002384 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002385
Roland Levillaincff13742014-11-17 14:32:17 +00002386 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002387 // Processing a Dex `double-to-float' instruction.
2388 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2389 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002390 break;
2391
2392 default:
2393 LOG(FATAL) << "Unexpected type conversion from " << input_type
2394 << " to " << result_type;
2395 };
2396 break;
2397
Roland Levillaindff1f282014-11-05 14:15:05 +00002398 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002399 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002400 case Primitive::kPrimBoolean:
2401 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002402 case Primitive::kPrimByte:
2403 case Primitive::kPrimShort:
2404 case Primitive::kPrimInt:
2405 case Primitive::kPrimChar: {
2406 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002407 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002408 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2409 out.AsFpuRegisterPairLow<SRegister>());
2410 break;
2411 }
2412
Roland Levillain647b9ed2014-11-27 12:06:00 +00002413 case Primitive::kPrimLong: {
2414 // Processing a Dex `long-to-double' instruction.
2415 Register low = in.AsRegisterPairLow<Register>();
2416 Register high = in.AsRegisterPairHigh<Register>();
2417 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2418 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002419 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002420 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002421 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2422 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002423
Roland Levillain682393c2015-04-14 15:57:52 +01002424 // temp_d = int-to-double(high)
2425 __ vmovsr(temp_s, high);
2426 __ vcvtdi(temp_d, temp_s);
2427 // constant_d = k2Pow32EncodingForDouble
2428 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2429 // out_d = unsigned-to-double(low)
2430 __ vmovsr(out_s, low);
2431 __ vcvtdu(out_d, out_s);
2432 // out_d += temp_d * constant_d
2433 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002434 break;
2435 }
2436
Roland Levillaincff13742014-11-17 14:32:17 +00002437 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002438 // Processing a Dex `float-to-double' instruction.
2439 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2440 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002441 break;
2442
2443 default:
2444 LOG(FATAL) << "Unexpected type conversion from " << input_type
2445 << " to " << result_type;
2446 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002447 break;
2448
2449 default:
2450 LOG(FATAL) << "Unexpected type conversion from " << input_type
2451 << " to " << result_type;
2452 }
2453}
2454
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002455void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002456 LocationSummary* locations =
2457 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002458 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002459 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002460 locations->SetInAt(0, Location::RequiresRegister());
2461 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2463 break;
2464 }
2465
2466 case Primitive::kPrimLong: {
2467 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002468 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002469 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002470 break;
2471 }
2472
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002473 case Primitive::kPrimFloat:
2474 case Primitive::kPrimDouble: {
2475 locations->SetInAt(0, Location::RequiresFpuRegister());
2476 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002477 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002478 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002479 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002480
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002481 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002482 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002483 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002484}
2485
2486void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2487 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002488 Location out = locations->Out();
2489 Location first = locations->InAt(0);
2490 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002491 switch (add->GetResultType()) {
2492 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002493 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002494 __ add(out.AsRegister<Register>(),
2495 first.AsRegister<Register>(),
2496 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002497 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002498 __ AddConstant(out.AsRegister<Register>(),
2499 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002500 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002501 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002502 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002503
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002504 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002505 if (second.IsConstant()) {
2506 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2507 GenerateAddLongConst(out, first, value);
2508 } else {
2509 DCHECK(second.IsRegisterPair());
2510 __ adds(out.AsRegisterPairLow<Register>(),
2511 first.AsRegisterPairLow<Register>(),
2512 ShifterOperand(second.AsRegisterPairLow<Register>()));
2513 __ adc(out.AsRegisterPairHigh<Register>(),
2514 first.AsRegisterPairHigh<Register>(),
2515 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2516 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002517 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002518 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002519
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002520 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002521 __ vadds(out.AsFpuRegister<SRegister>(),
2522 first.AsFpuRegister<SRegister>(),
2523 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002524 break;
2525
2526 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002527 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2528 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2529 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002530 break;
2531
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002532 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002533 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002534 }
2535}
2536
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002537void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002538 LocationSummary* locations =
2539 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002540 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002541 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2545 break;
2546 }
2547
2548 case Primitive::kPrimLong: {
2549 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002550 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002552 break;
2553 }
Calin Juravle11351682014-10-23 15:38:15 +01002554 case Primitive::kPrimFloat:
2555 case Primitive::kPrimDouble: {
2556 locations->SetInAt(0, Location::RequiresFpuRegister());
2557 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002558 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002559 break;
Calin Juravle11351682014-10-23 15:38:15 +01002560 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002561 default:
Calin Juravle11351682014-10-23 15:38:15 +01002562 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002563 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002564}
2565
2566void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2567 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002568 Location out = locations->Out();
2569 Location first = locations->InAt(0);
2570 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002571 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002572 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002573 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002574 __ sub(out.AsRegister<Register>(),
2575 first.AsRegister<Register>(),
2576 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002577 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002578 __ AddConstant(out.AsRegister<Register>(),
2579 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002580 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002581 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002582 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002583 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002584
Calin Juravle11351682014-10-23 15:38:15 +01002585 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002586 if (second.IsConstant()) {
2587 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2588 GenerateAddLongConst(out, first, -value);
2589 } else {
2590 DCHECK(second.IsRegisterPair());
2591 __ subs(out.AsRegisterPairLow<Register>(),
2592 first.AsRegisterPairLow<Register>(),
2593 ShifterOperand(second.AsRegisterPairLow<Register>()));
2594 __ sbc(out.AsRegisterPairHigh<Register>(),
2595 first.AsRegisterPairHigh<Register>(),
2596 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2597 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002598 break;
Calin Juravle11351682014-10-23 15:38:15 +01002599 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002600
Calin Juravle11351682014-10-23 15:38:15 +01002601 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002602 __ vsubs(out.AsFpuRegister<SRegister>(),
2603 first.AsFpuRegister<SRegister>(),
2604 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002605 break;
Calin Juravle11351682014-10-23 15:38:15 +01002606 }
2607
2608 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002609 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2610 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2611 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002612 break;
2613 }
2614
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002615
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002616 default:
Calin Juravle11351682014-10-23 15:38:15 +01002617 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002618 }
2619}
2620
Calin Juravle34bacdf2014-10-07 20:23:36 +01002621void LocationsBuilderARM::VisitMul(HMul* mul) {
2622 LocationSummary* locations =
2623 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2624 switch (mul->GetResultType()) {
2625 case Primitive::kPrimInt:
2626 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002627 locations->SetInAt(0, Location::RequiresRegister());
2628 locations->SetInAt(1, Location::RequiresRegister());
2629 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002630 break;
2631 }
2632
Calin Juravleb5bfa962014-10-21 18:02:24 +01002633 case Primitive::kPrimFloat:
2634 case Primitive::kPrimDouble: {
2635 locations->SetInAt(0, Location::RequiresFpuRegister());
2636 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002637 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002638 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002639 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002640
2641 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002642 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002643 }
2644}
2645
2646void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2647 LocationSummary* locations = mul->GetLocations();
2648 Location out = locations->Out();
2649 Location first = locations->InAt(0);
2650 Location second = locations->InAt(1);
2651 switch (mul->GetResultType()) {
2652 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002653 __ mul(out.AsRegister<Register>(),
2654 first.AsRegister<Register>(),
2655 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002656 break;
2657 }
2658 case Primitive::kPrimLong: {
2659 Register out_hi = out.AsRegisterPairHigh<Register>();
2660 Register out_lo = out.AsRegisterPairLow<Register>();
2661 Register in1_hi = first.AsRegisterPairHigh<Register>();
2662 Register in1_lo = first.AsRegisterPairLow<Register>();
2663 Register in2_hi = second.AsRegisterPairHigh<Register>();
2664 Register in2_lo = second.AsRegisterPairLow<Register>();
2665
2666 // Extra checks to protect caused by the existence of R1_R2.
2667 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2668 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2669 DCHECK_NE(out_hi, in1_lo);
2670 DCHECK_NE(out_hi, in2_lo);
2671
2672 // input: in1 - 64 bits, in2 - 64 bits
2673 // output: out
2674 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2675 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2676 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2677
2678 // IP <- in1.lo * in2.hi
2679 __ mul(IP, in1_lo, in2_hi);
2680 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2681 __ mla(out_hi, in1_hi, in2_lo, IP);
2682 // out.lo <- (in1.lo * in2.lo)[31:0];
2683 __ umull(out_lo, IP, in1_lo, in2_lo);
2684 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2685 __ add(out_hi, out_hi, ShifterOperand(IP));
2686 break;
2687 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002688
2689 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002690 __ vmuls(out.AsFpuRegister<SRegister>(),
2691 first.AsFpuRegister<SRegister>(),
2692 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002693 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002694 }
2695
2696 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002697 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2698 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2699 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002700 break;
2701 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002702
2703 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002704 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002705 }
2706}
2707
Zheng Xuc6667102015-05-15 16:08:45 +08002708void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2709 DCHECK(instruction->IsDiv() || instruction->IsRem());
2710 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2711
2712 LocationSummary* locations = instruction->GetLocations();
2713 Location second = locations->InAt(1);
2714 DCHECK(second.IsConstant());
2715
2716 Register out = locations->Out().AsRegister<Register>();
2717 Register dividend = locations->InAt(0).AsRegister<Register>();
2718 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2719 DCHECK(imm == 1 || imm == -1);
2720
2721 if (instruction->IsRem()) {
2722 __ LoadImmediate(out, 0);
2723 } else {
2724 if (imm == 1) {
2725 __ Mov(out, dividend);
2726 } else {
2727 __ rsb(out, dividend, ShifterOperand(0));
2728 }
2729 }
2730}
2731
2732void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2733 DCHECK(instruction->IsDiv() || instruction->IsRem());
2734 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2735
2736 LocationSummary* locations = instruction->GetLocations();
2737 Location second = locations->InAt(1);
2738 DCHECK(second.IsConstant());
2739
2740 Register out = locations->Out().AsRegister<Register>();
2741 Register dividend = locations->InAt(0).AsRegister<Register>();
2742 Register temp = locations->GetTemp(0).AsRegister<Register>();
2743 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002744 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002745 int ctz_imm = CTZ(abs_imm);
2746
2747 if (ctz_imm == 1) {
2748 __ Lsr(temp, dividend, 32 - ctz_imm);
2749 } else {
2750 __ Asr(temp, dividend, 31);
2751 __ Lsr(temp, temp, 32 - ctz_imm);
2752 }
2753 __ add(out, temp, ShifterOperand(dividend));
2754
2755 if (instruction->IsDiv()) {
2756 __ Asr(out, out, ctz_imm);
2757 if (imm < 0) {
2758 __ rsb(out, out, ShifterOperand(0));
2759 }
2760 } else {
2761 __ ubfx(out, out, 0, ctz_imm);
2762 __ sub(out, out, ShifterOperand(temp));
2763 }
2764}
2765
2766void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2767 DCHECK(instruction->IsDiv() || instruction->IsRem());
2768 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2769
2770 LocationSummary* locations = instruction->GetLocations();
2771 Location second = locations->InAt(1);
2772 DCHECK(second.IsConstant());
2773
2774 Register out = locations->Out().AsRegister<Register>();
2775 Register dividend = locations->InAt(0).AsRegister<Register>();
2776 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2777 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2778 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2779
2780 int64_t magic;
2781 int shift;
2782 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2783
2784 __ LoadImmediate(temp1, magic);
2785 __ smull(temp2, temp1, dividend, temp1);
2786
2787 if (imm > 0 && magic < 0) {
2788 __ add(temp1, temp1, ShifterOperand(dividend));
2789 } else if (imm < 0 && magic > 0) {
2790 __ sub(temp1, temp1, ShifterOperand(dividend));
2791 }
2792
2793 if (shift != 0) {
2794 __ Asr(temp1, temp1, shift);
2795 }
2796
2797 if (instruction->IsDiv()) {
2798 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2799 } else {
2800 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2801 // TODO: Strength reduction for mls.
2802 __ LoadImmediate(temp2, imm);
2803 __ mls(out, temp1, temp2, dividend);
2804 }
2805}
2806
2807void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2808 DCHECK(instruction->IsDiv() || instruction->IsRem());
2809 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2810
2811 LocationSummary* locations = instruction->GetLocations();
2812 Location second = locations->InAt(1);
2813 DCHECK(second.IsConstant());
2814
2815 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2816 if (imm == 0) {
2817 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2818 } else if (imm == 1 || imm == -1) {
2819 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002820 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002821 DivRemByPowerOfTwo(instruction);
2822 } else {
2823 DCHECK(imm <= -2 || imm >= 2);
2824 GenerateDivRemWithAnyConstant(instruction);
2825 }
2826}
2827
Calin Juravle7c4954d2014-10-28 16:57:40 +00002828void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002829 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2830 if (div->GetResultType() == Primitive::kPrimLong) {
2831 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002832 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002833 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2834 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002835 } else if (div->GetResultType() == Primitive::kPrimInt &&
2836 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2837 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002838 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002839 }
2840
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2842
Calin Juravle7c4954d2014-10-28 16:57:40 +00002843 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002844 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002845 if (div->InputAt(1)->IsConstant()) {
2846 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002847 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002848 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002849 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2850 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002851 // No temp register required.
2852 } else {
2853 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002854 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002855 locations->AddTemp(Location::RequiresRegister());
2856 }
2857 }
2858 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002859 locations->SetInAt(0, Location::RequiresRegister());
2860 locations->SetInAt(1, Location::RequiresRegister());
2861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2862 } else {
2863 InvokeRuntimeCallingConvention calling_convention;
2864 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2865 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2866 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2867 // we only need the former.
2868 locations->SetOut(Location::RegisterLocation(R0));
2869 }
Calin Juravled0d48522014-11-04 16:40:20 +00002870 break;
2871 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002872 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002873 InvokeRuntimeCallingConvention calling_convention;
2874 locations->SetInAt(0, Location::RegisterPairLocation(
2875 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2876 locations->SetInAt(1, Location::RegisterPairLocation(
2877 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002878 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002879 break;
2880 }
2881 case Primitive::kPrimFloat:
2882 case Primitive::kPrimDouble: {
2883 locations->SetInAt(0, Location::RequiresFpuRegister());
2884 locations->SetInAt(1, Location::RequiresFpuRegister());
2885 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2886 break;
2887 }
2888
2889 default:
2890 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2891 }
2892}
2893
2894void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2895 LocationSummary* locations = div->GetLocations();
2896 Location out = locations->Out();
2897 Location first = locations->InAt(0);
2898 Location second = locations->InAt(1);
2899
2900 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002901 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002902 if (second.IsConstant()) {
2903 GenerateDivRemConstantIntegral(div);
2904 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002905 __ sdiv(out.AsRegister<Register>(),
2906 first.AsRegister<Register>(),
2907 second.AsRegister<Register>());
2908 } else {
2909 InvokeRuntimeCallingConvention calling_convention;
2910 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2911 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2912 DCHECK_EQ(R0, out.AsRegister<Register>());
2913
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002914 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002915 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002916 }
Calin Juravled0d48522014-11-04 16:40:20 +00002917 break;
2918 }
2919
Calin Juravle7c4954d2014-10-28 16:57:40 +00002920 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002921 InvokeRuntimeCallingConvention calling_convention;
2922 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2923 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2924 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2925 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2926 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002927 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002928
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002929 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002930 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002931 break;
2932 }
2933
2934 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002935 __ vdivs(out.AsFpuRegister<SRegister>(),
2936 first.AsFpuRegister<SRegister>(),
2937 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002938 break;
2939 }
2940
2941 case Primitive::kPrimDouble: {
2942 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2943 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2944 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2945 break;
2946 }
2947
2948 default:
2949 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2950 }
2951}
2952
Calin Juravlebacfec32014-11-14 15:54:36 +00002953void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002954 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002955
2956 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002957 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002958 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2959 // sdiv will be replaced by other instruction sequence.
2960 call_kind = LocationSummary::kNoCall;
2961 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2962 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002963 // Have hardware divide instruction for int, do it with three instructions.
2964 call_kind = LocationSummary::kNoCall;
2965 }
2966
Calin Juravlebacfec32014-11-14 15:54:36 +00002967 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2968
Calin Juravled2ec87d2014-12-08 14:24:46 +00002969 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002970 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002971 if (rem->InputAt(1)->IsConstant()) {
2972 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002973 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002975 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
2976 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002977 // No temp register required.
2978 } else {
2979 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002980 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002981 locations->AddTemp(Location::RequiresRegister());
2982 }
2983 }
2984 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002985 locations->SetInAt(0, Location::RequiresRegister());
2986 locations->SetInAt(1, Location::RequiresRegister());
2987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2988 locations->AddTemp(Location::RequiresRegister());
2989 } else {
2990 InvokeRuntimeCallingConvention calling_convention;
2991 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2992 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2993 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2994 // we only need the latter.
2995 locations->SetOut(Location::RegisterLocation(R1));
2996 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002997 break;
2998 }
2999 case Primitive::kPrimLong: {
3000 InvokeRuntimeCallingConvention calling_convention;
3001 locations->SetInAt(0, Location::RegisterPairLocation(
3002 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3003 locations->SetInAt(1, Location::RegisterPairLocation(
3004 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3005 // The runtime helper puts the output in R2,R3.
3006 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3007 break;
3008 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003009 case Primitive::kPrimFloat: {
3010 InvokeRuntimeCallingConvention calling_convention;
3011 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3012 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3013 locations->SetOut(Location::FpuRegisterLocation(S0));
3014 break;
3015 }
3016
Calin Juravlebacfec32014-11-14 15:54:36 +00003017 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003018 InvokeRuntimeCallingConvention calling_convention;
3019 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3020 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3021 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3022 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3023 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003024 break;
3025 }
3026
3027 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003028 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003029 }
3030}
3031
3032void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3033 LocationSummary* locations = rem->GetLocations();
3034 Location out = locations->Out();
3035 Location first = locations->InAt(0);
3036 Location second = locations->InAt(1);
3037
Calin Juravled2ec87d2014-12-08 14:24:46 +00003038 Primitive::Type type = rem->GetResultType();
3039 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003040 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003041 if (second.IsConstant()) {
3042 GenerateDivRemConstantIntegral(rem);
3043 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003044 Register reg1 = first.AsRegister<Register>();
3045 Register reg2 = second.AsRegister<Register>();
3046 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003047
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003048 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003049 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003050 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003051 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003052 } else {
3053 InvokeRuntimeCallingConvention calling_convention;
3054 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3055 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3056 DCHECK_EQ(R1, out.AsRegister<Register>());
3057
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003058 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003059 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003060 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003061 break;
3062 }
3063
3064 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003065 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003066 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003067 break;
3068 }
3069
Calin Juravled2ec87d2014-12-08 14:24:46 +00003070 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003071 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003072 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003073 break;
3074 }
3075
Calin Juravlebacfec32014-11-14 15:54:36 +00003076 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003077 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003078 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003079 break;
3080 }
3081
3082 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003083 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003084 }
3085}
3086
Calin Juravled0d48522014-11-04 16:40:20 +00003087void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003088 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3089 ? LocationSummary::kCallOnSlowPath
3090 : LocationSummary::kNoCall;
3091 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003092 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003093 if (instruction->HasUses()) {
3094 locations->SetOut(Location::SameAsFirstInput());
3095 }
3096}
3097
3098void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003099 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003100 codegen_->AddSlowPath(slow_path);
3101
3102 LocationSummary* locations = instruction->GetLocations();
3103 Location value = locations->InAt(0);
3104
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003105 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003106 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003107 case Primitive::kPrimByte:
3108 case Primitive::kPrimChar:
3109 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003110 case Primitive::kPrimInt: {
3111 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003112 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003113 } else {
3114 DCHECK(value.IsConstant()) << value;
3115 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3116 __ b(slow_path->GetEntryLabel());
3117 }
3118 }
3119 break;
3120 }
3121 case Primitive::kPrimLong: {
3122 if (value.IsRegisterPair()) {
3123 __ orrs(IP,
3124 value.AsRegisterPairLow<Register>(),
3125 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3126 __ b(slow_path->GetEntryLabel(), EQ);
3127 } else {
3128 DCHECK(value.IsConstant()) << value;
3129 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3130 __ b(slow_path->GetEntryLabel());
3131 }
3132 }
3133 break;
3134 default:
3135 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3136 }
3137 }
Calin Juravled0d48522014-11-04 16:40:20 +00003138}
3139
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003140void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3141 Register in = locations->InAt(0).AsRegister<Register>();
3142 Location rhs = locations->InAt(1);
3143 Register out = locations->Out().AsRegister<Register>();
3144
3145 if (rhs.IsConstant()) {
3146 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3147 // so map all rotations to a +ve. equivalent in that range.
3148 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3149 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3150 if (rot) {
3151 // Rotate, mapping left rotations to right equivalents if necessary.
3152 // (e.g. left by 2 bits == right by 30.)
3153 __ Ror(out, in, rot);
3154 } else if (out != in) {
3155 __ Mov(out, in);
3156 }
3157 } else {
3158 __ Ror(out, in, rhs.AsRegister<Register>());
3159 }
3160}
3161
3162// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3163// rotates by swapping input regs (effectively rotating by the first 32-bits of
3164// a larger rotation) or flipping direction (thus treating larger right/left
3165// rotations as sub-word sized rotations in the other direction) as appropriate.
3166void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3167 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3168 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3169 Location rhs = locations->InAt(1);
3170 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3171 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3172
3173 if (rhs.IsConstant()) {
3174 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3175 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003176 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003177 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3178 // logic below to a simple pair of binary orr.
3179 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3180 if (rot >= kArmBitsPerWord) {
3181 rot -= kArmBitsPerWord;
3182 std::swap(in_reg_hi, in_reg_lo);
3183 }
3184 // Rotate, or mov to out for zero or word size rotations.
3185 if (rot != 0u) {
3186 __ Lsr(out_reg_hi, in_reg_hi, rot);
3187 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3188 __ Lsr(out_reg_lo, in_reg_lo, rot);
3189 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3190 } else {
3191 __ Mov(out_reg_lo, in_reg_lo);
3192 __ Mov(out_reg_hi, in_reg_hi);
3193 }
3194 } else {
3195 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3196 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3197 Label end;
3198 Label shift_by_32_plus_shift_right;
3199
3200 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3201 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3202 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3203 __ b(&shift_by_32_plus_shift_right, CC);
3204
3205 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3206 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3207 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3208 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3209 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3210 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3211 __ Lsr(shift_left, in_reg_hi, shift_right);
3212 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3213 __ b(&end);
3214
3215 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3216 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3217 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3218 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3219 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3220 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3221 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3222 __ Lsl(shift_right, in_reg_hi, shift_left);
3223 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3224
3225 __ Bind(&end);
3226 }
3227}
Roland Levillain22c49222016-03-18 14:04:28 +00003228
3229void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003230 LocationSummary* locations =
3231 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3232 switch (ror->GetResultType()) {
3233 case Primitive::kPrimInt: {
3234 locations->SetInAt(0, Location::RequiresRegister());
3235 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3237 break;
3238 }
3239 case Primitive::kPrimLong: {
3240 locations->SetInAt(0, Location::RequiresRegister());
3241 if (ror->InputAt(1)->IsConstant()) {
3242 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3243 } else {
3244 locations->SetInAt(1, Location::RequiresRegister());
3245 locations->AddTemp(Location::RequiresRegister());
3246 locations->AddTemp(Location::RequiresRegister());
3247 }
3248 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3249 break;
3250 }
3251 default:
3252 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3253 }
3254}
3255
Roland Levillain22c49222016-03-18 14:04:28 +00003256void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003257 LocationSummary* locations = ror->GetLocations();
3258 Primitive::Type type = ror->GetResultType();
3259 switch (type) {
3260 case Primitive::kPrimInt: {
3261 HandleIntegerRotate(locations);
3262 break;
3263 }
3264 case Primitive::kPrimLong: {
3265 HandleLongRotate(locations);
3266 break;
3267 }
3268 default:
3269 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003270 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003271 }
3272}
3273
Calin Juravle9aec02f2014-11-18 23:06:35 +00003274void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3275 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3276
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003277 LocationSummary* locations =
3278 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003279
3280 switch (op->GetResultType()) {
3281 case Primitive::kPrimInt: {
3282 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003283 if (op->InputAt(1)->IsConstant()) {
3284 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3286 } else {
3287 locations->SetInAt(1, Location::RequiresRegister());
3288 // Make the output overlap, as it will be used to hold the masked
3289 // second input.
3290 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3291 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003292 break;
3293 }
3294 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003295 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003296 if (op->InputAt(1)->IsConstant()) {
3297 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3298 // For simplicity, use kOutputOverlap even though we only require that low registers
3299 // don't clash with high registers which the register allocator currently guarantees.
3300 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3301 } else {
3302 locations->SetInAt(1, Location::RequiresRegister());
3303 locations->AddTemp(Location::RequiresRegister());
3304 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3305 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003306 break;
3307 }
3308 default:
3309 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3310 }
3311}
3312
3313void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3314 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3315
3316 LocationSummary* locations = op->GetLocations();
3317 Location out = locations->Out();
3318 Location first = locations->InAt(0);
3319 Location second = locations->InAt(1);
3320
3321 Primitive::Type type = op->GetResultType();
3322 switch (type) {
3323 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 Register out_reg = out.AsRegister<Register>();
3325 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003326 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003327 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003328 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003329 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003330 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003331 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003332 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003333 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003334 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003335 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003336 }
3337 } else {
3338 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003339 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003340 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003341 __ Mov(out_reg, first_reg);
3342 } else if (op->IsShl()) {
3343 __ Lsl(out_reg, first_reg, shift_value);
3344 } else if (op->IsShr()) {
3345 __ Asr(out_reg, first_reg, shift_value);
3346 } else {
3347 __ Lsr(out_reg, first_reg, shift_value);
3348 }
3349 }
3350 break;
3351 }
3352 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003353 Register o_h = out.AsRegisterPairHigh<Register>();
3354 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003355
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003356 Register high = first.AsRegisterPairHigh<Register>();
3357 Register low = first.AsRegisterPairLow<Register>();
3358
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003359 if (second.IsRegister()) {
3360 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003361
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003362 Register second_reg = second.AsRegister<Register>();
3363
3364 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003365 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003366 // Shift the high part
3367 __ Lsl(o_h, high, o_l);
3368 // Shift the low part and `or` what overflew on the high part
3369 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3370 __ Lsr(temp, low, temp);
3371 __ orr(o_h, o_h, ShifterOperand(temp));
3372 // If the shift is > 32 bits, override the high part
3373 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3374 __ it(PL);
3375 __ Lsl(o_h, low, temp, PL);
3376 // Shift the low part
3377 __ Lsl(o_l, low, o_l);
3378 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003379 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003380 // Shift the low part
3381 __ Lsr(o_l, low, o_h);
3382 // Shift the high part and `or` what underflew on the low part
3383 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3384 __ Lsl(temp, high, temp);
3385 __ orr(o_l, o_l, ShifterOperand(temp));
3386 // If the shift is > 32 bits, override the low part
3387 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3388 __ it(PL);
3389 __ Asr(o_l, high, temp, PL);
3390 // Shift the high part
3391 __ Asr(o_h, high, o_h);
3392 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003393 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003394 // same as Shr except we use `Lsr`s and not `Asr`s
3395 __ Lsr(o_l, low, o_h);
3396 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3397 __ Lsl(temp, high, temp);
3398 __ orr(o_l, o_l, ShifterOperand(temp));
3399 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3400 __ it(PL);
3401 __ Lsr(o_l, high, temp, PL);
3402 __ Lsr(o_h, high, o_h);
3403 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003404 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003405 // Register allocator doesn't create partial overlap.
3406 DCHECK_NE(o_l, high);
3407 DCHECK_NE(o_h, low);
3408 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003409 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003410 if (shift_value > 32) {
3411 if (op->IsShl()) {
3412 __ Lsl(o_h, low, shift_value - 32);
3413 __ LoadImmediate(o_l, 0);
3414 } else if (op->IsShr()) {
3415 __ Asr(o_l, high, shift_value - 32);
3416 __ Asr(o_h, high, 31);
3417 } else {
3418 __ Lsr(o_l, high, shift_value - 32);
3419 __ LoadImmediate(o_h, 0);
3420 }
3421 } else if (shift_value == 32) {
3422 if (op->IsShl()) {
3423 __ mov(o_h, ShifterOperand(low));
3424 __ LoadImmediate(o_l, 0);
3425 } else if (op->IsShr()) {
3426 __ mov(o_l, ShifterOperand(high));
3427 __ Asr(o_h, high, 31);
3428 } else {
3429 __ mov(o_l, ShifterOperand(high));
3430 __ LoadImmediate(o_h, 0);
3431 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003432 } else if (shift_value == 1) {
3433 if (op->IsShl()) {
3434 __ Lsls(o_l, low, 1);
3435 __ adc(o_h, high, ShifterOperand(high));
3436 } else if (op->IsShr()) {
3437 __ Asrs(o_h, high, 1);
3438 __ Rrx(o_l, low);
3439 } else {
3440 __ Lsrs(o_h, high, 1);
3441 __ Rrx(o_l, low);
3442 }
3443 } else {
3444 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003445 if (op->IsShl()) {
3446 __ Lsl(o_h, high, shift_value);
3447 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3448 __ Lsl(o_l, low, shift_value);
3449 } else if (op->IsShr()) {
3450 __ Lsr(o_l, low, shift_value);
3451 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3452 __ Asr(o_h, high, shift_value);
3453 } else {
3454 __ Lsr(o_l, low, shift_value);
3455 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3456 __ Lsr(o_h, high, shift_value);
3457 }
3458 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003459 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003460 break;
3461 }
3462 default:
3463 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003464 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003465 }
3466}
3467
3468void LocationsBuilderARM::VisitShl(HShl* shl) {
3469 HandleShift(shl);
3470}
3471
3472void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3473 HandleShift(shl);
3474}
3475
3476void LocationsBuilderARM::VisitShr(HShr* shr) {
3477 HandleShift(shr);
3478}
3479
3480void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3481 HandleShift(shr);
3482}
3483
3484void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3485 HandleShift(ushr);
3486}
3487
3488void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3489 HandleShift(ushr);
3490}
3491
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003492void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003493 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003495 if (instruction->IsStringAlloc()) {
3496 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3497 } else {
3498 InvokeRuntimeCallingConvention calling_convention;
3499 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3500 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3501 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003502 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003503}
3504
3505void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003506 // Note: if heap poisoning is enabled, the entry point takes cares
3507 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003508 if (instruction->IsStringAlloc()) {
3509 // String is allocated through StringFactory. Call NewEmptyString entry point.
3510 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003511 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003512 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3513 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3514 __ blx(LR);
3515 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3516 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003517 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003518 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3519 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003520}
3521
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003522void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3523 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003525 InvokeRuntimeCallingConvention calling_convention;
3526 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003527 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003528 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003529 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003530}
3531
3532void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3533 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003534 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003535 // Note: if heap poisoning is enabled, the entry point takes cares
3536 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003537 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003538 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003539}
3540
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003541void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003542 LocationSummary* locations =
3543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003544 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3545 if (location.IsStackSlot()) {
3546 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3547 } else if (location.IsDoubleStackSlot()) {
3548 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003549 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003550 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003551}
3552
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003553void InstructionCodeGeneratorARM::VisitParameterValue(
3554 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003555 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003556}
3557
3558void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3561 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3562}
3563
3564void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3565 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003566}
3567
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003568void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003569 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003570 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003571 locations->SetInAt(0, Location::RequiresRegister());
3572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003573}
3574
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003575void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3576 LocationSummary* locations = not_->GetLocations();
3577 Location out = locations->Out();
3578 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003579 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003580 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003581 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003582 break;
3583
3584 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003585 __ mvn(out.AsRegisterPairLow<Register>(),
3586 ShifterOperand(in.AsRegisterPairLow<Register>()));
3587 __ mvn(out.AsRegisterPairHigh<Register>(),
3588 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003589 break;
3590
3591 default:
3592 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3593 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003594}
3595
David Brazdil66d126e2015-04-03 16:02:44 +01003596void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3599 locations->SetInAt(0, Location::RequiresRegister());
3600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3601}
3602
3603void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003604 LocationSummary* locations = bool_not->GetLocations();
3605 Location out = locations->Out();
3606 Location in = locations->InAt(0);
3607 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3608}
3609
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003610void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003611 LocationSummary* locations =
3612 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003613 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003614 case Primitive::kPrimBoolean:
3615 case Primitive::kPrimByte:
3616 case Primitive::kPrimShort:
3617 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003618 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003619 case Primitive::kPrimLong: {
3620 locations->SetInAt(0, Location::RequiresRegister());
3621 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003622 // Output overlaps because it is written before doing the low comparison.
3623 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003624 break;
3625 }
3626 case Primitive::kPrimFloat:
3627 case Primitive::kPrimDouble: {
3628 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003629 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003630 locations->SetOut(Location::RequiresRegister());
3631 break;
3632 }
3633 default:
3634 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3635 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003636}
3637
3638void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003639 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003641 Location left = locations->InAt(0);
3642 Location right = locations->InAt(1);
3643
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003644 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003645 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003646 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003647 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003648 case Primitive::kPrimBoolean:
3649 case Primitive::kPrimByte:
3650 case Primitive::kPrimShort:
3651 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003652 case Primitive::kPrimInt: {
3653 __ LoadImmediate(out, 0);
3654 __ cmp(left.AsRegister<Register>(),
3655 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3656 less_cond = LT;
3657 break;
3658 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003659 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003660 __ cmp(left.AsRegisterPairHigh<Register>(),
3661 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003662 __ b(&less, LT);
3663 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003664 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003665 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003666 __ cmp(left.AsRegisterPairLow<Register>(),
3667 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003668 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003669 break;
3670 }
3671 case Primitive::kPrimFloat:
3672 case Primitive::kPrimDouble: {
3673 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003674 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003675 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003676 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003677 break;
3678 }
3679 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003680 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003681 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003682 }
Aart Bika19616e2016-02-01 18:57:58 -08003683
Calin Juravleddb7df22014-11-25 20:56:51 +00003684 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003685 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003686
3687 __ Bind(&greater);
3688 __ LoadImmediate(out, 1);
3689 __ b(&done);
3690
3691 __ Bind(&less);
3692 __ LoadImmediate(out, -1);
3693
3694 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003695}
3696
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003697void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003698 LocationSummary* locations =
3699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003700 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003701 locations->SetInAt(i, Location::Any());
3702 }
3703 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003704}
3705
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003706void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003707 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003708}
3709
Roland Levillainc9285912015-12-18 10:38:42 +00003710void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3711 // TODO (ported from quick): revisit ARM barrier kinds.
3712 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003713 switch (kind) {
3714 case MemBarrierKind::kAnyStore:
3715 case MemBarrierKind::kLoadAny:
3716 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003717 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003718 break;
3719 }
3720 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003721 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003722 break;
3723 }
3724 default:
3725 LOG(FATAL) << "Unexpected memory barrier " << kind;
3726 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003727 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003728}
3729
3730void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3731 uint32_t offset,
3732 Register out_lo,
3733 Register out_hi) {
3734 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003735 // Ensure `out_lo` is different from `addr`, so that loading
3736 // `offset` into `out_lo` does not clutter `addr`.
3737 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003738 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003739 __ add(IP, addr, ShifterOperand(out_lo));
3740 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003741 }
3742 __ ldrexd(out_lo, out_hi, addr);
3743}
3744
3745void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3746 uint32_t offset,
3747 Register value_lo,
3748 Register value_hi,
3749 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003750 Register temp2,
3751 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003752 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003753 if (offset != 0) {
3754 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003755 __ add(IP, addr, ShifterOperand(temp1));
3756 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003757 }
3758 __ Bind(&fail);
3759 // We need a load followed by store. (The address used in a STREX instruction must
3760 // be the same as the address in the most recently executed LDREX instruction.)
3761 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003762 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003763 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003764 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003765}
3766
3767void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3768 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3769
Nicolas Geoffray39468442014-09-02 15:17:15 +01003770 LocationSummary* locations =
3771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003772 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003773
Calin Juravle52c48962014-12-16 17:02:57 +00003774 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003775 if (Primitive::IsFloatingPointType(field_type)) {
3776 locations->SetInAt(1, Location::RequiresFpuRegister());
3777 } else {
3778 locations->SetInAt(1, Location::RequiresRegister());
3779 }
3780
Calin Juravle52c48962014-12-16 17:02:57 +00003781 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003782 bool generate_volatile = field_info.IsVolatile()
3783 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003784 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003785 bool needs_write_barrier =
3786 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003787 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003788 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003789 if (needs_write_barrier) {
3790 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003791 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003792 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003793 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003794 // - registers need to be consecutive
3795 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003796 // We don't test for ARM yet, and the assertion makes sure that we
3797 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003798 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3799
3800 locations->AddTemp(Location::RequiresRegister());
3801 locations->AddTemp(Location::RequiresRegister());
3802 if (field_type == Primitive::kPrimDouble) {
3803 // For doubles we need two more registers to copy the value.
3804 locations->AddTemp(Location::RegisterLocation(R2));
3805 locations->AddTemp(Location::RegisterLocation(R3));
3806 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003807 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003808}
3809
Calin Juravle52c48962014-12-16 17:02:57 +00003810void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003811 const FieldInfo& field_info,
3812 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003813 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3814
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003815 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003816 Register base = locations->InAt(0).AsRegister<Register>();
3817 Location value = locations->InAt(1);
3818
3819 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003820 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003821 Primitive::Type field_type = field_info.GetFieldType();
3822 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003823 bool needs_write_barrier =
3824 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003825
3826 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003827 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003828 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003829
3830 switch (field_type) {
3831 case Primitive::kPrimBoolean:
3832 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003833 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003834 break;
3835 }
3836
3837 case Primitive::kPrimShort:
3838 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003839 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003840 break;
3841 }
3842
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003843 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003844 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003845 if (kPoisonHeapReferences && needs_write_barrier) {
3846 // Note that in the case where `value` is a null reference,
3847 // we do not enter this block, as a null reference does not
3848 // need poisoning.
3849 DCHECK_EQ(field_type, Primitive::kPrimNot);
3850 Register temp = locations->GetTemp(0).AsRegister<Register>();
3851 __ Mov(temp, value.AsRegister<Register>());
3852 __ PoisonHeapReference(temp);
3853 __ StoreToOffset(kStoreWord, temp, base, offset);
3854 } else {
3855 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3856 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003857 break;
3858 }
3859
3860 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003861 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003862 GenerateWideAtomicStore(base, offset,
3863 value.AsRegisterPairLow<Register>(),
3864 value.AsRegisterPairHigh<Register>(),
3865 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003866 locations->GetTemp(1).AsRegister<Register>(),
3867 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003868 } else {
3869 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003870 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003871 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003872 break;
3873 }
3874
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003875 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003876 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003877 break;
3878 }
3879
3880 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003881 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003882 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003883 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3884 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3885
3886 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3887
3888 GenerateWideAtomicStore(base, offset,
3889 value_reg_lo,
3890 value_reg_hi,
3891 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003892 locations->GetTemp(3).AsRegister<Register>(),
3893 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003894 } else {
3895 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003896 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003897 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003898 break;
3899 }
3900
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003901 case Primitive::kPrimVoid:
3902 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003903 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003904 }
Calin Juravle52c48962014-12-16 17:02:57 +00003905
Calin Juravle77520bc2015-01-12 18:45:46 +00003906 // Longs and doubles are handled in the switch.
3907 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3908 codegen_->MaybeRecordImplicitNullCheck(instruction);
3909 }
3910
3911 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3912 Register temp = locations->GetTemp(0).AsRegister<Register>();
3913 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003914 codegen_->MarkGCCard(
3915 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003916 }
3917
Calin Juravle52c48962014-12-16 17:02:57 +00003918 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003919 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003920 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003921}
3922
Calin Juravle52c48962014-12-16 17:02:57 +00003923void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3924 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003925
3926 bool object_field_get_with_read_barrier =
3927 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003928 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003929 new (GetGraph()->GetArena()) LocationSummary(instruction,
3930 object_field_get_with_read_barrier ?
3931 LocationSummary::kCallOnSlowPath :
3932 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01003933 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3934 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
3935 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003936 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003937
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003938 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003939 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003940 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003941 // The output overlaps in case of volatile long: we don't want the
3942 // code generated by GenerateWideAtomicLoad to overwrite the
3943 // object's location. Likewise, in the case of an object field get
3944 // with read barriers enabled, we do not want the load to overwrite
3945 // the object's location, as we need it to emit the read barrier.
3946 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3947 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003948
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003949 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3950 locations->SetOut(Location::RequiresFpuRegister());
3951 } else {
3952 locations->SetOut(Location::RequiresRegister(),
3953 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3954 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003955 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00003956 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003957 // - registers need to be consecutive
3958 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003959 // We don't test for ARM yet, and the assertion makes sure that we
3960 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003961 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3962 locations->AddTemp(Location::RequiresRegister());
3963 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00003964 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3965 // We need a temporary register for the read barrier marking slow
3966 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3967 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003968 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003969}
3970
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003971Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
3972 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
3973 << input->GetType();
3974 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
3975 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
3976 return Location::ConstantLocation(input->AsConstant());
3977 } else {
3978 return Location::RequiresFpuRegister();
3979 }
3980}
3981
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003982Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3983 Opcode opcode) {
3984 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3985 if (constant->IsConstant() &&
3986 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3987 return Location::ConstantLocation(constant->AsConstant());
3988 }
3989 return Location::RequiresRegister();
3990}
3991
3992bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3993 Opcode opcode) {
3994 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3995 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01003996 Opcode high_opcode = opcode;
3997 SetCc low_set_cc = kCcDontCare;
3998 switch (opcode) {
3999 case SUB:
4000 // Flip the operation to an ADD.
4001 value = -value;
4002 opcode = ADD;
4003 FALLTHROUGH_INTENDED;
4004 case ADD:
4005 if (Low32Bits(value) == 0u) {
4006 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4007 }
4008 high_opcode = ADC;
4009 low_set_cc = kCcSet;
4010 break;
4011 default:
4012 break;
4013 }
4014 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4015 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004016 } else {
4017 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4018 }
4019}
4020
Vladimir Marko59751a72016-08-05 14:37:27 +01004021bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4022 Opcode opcode,
4023 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004024 ShifterOperand so;
4025 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004026 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004027 return true;
4028 }
4029 Opcode neg_opcode = kNoOperand;
4030 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004031 case AND: neg_opcode = BIC; value = ~value; break;
4032 case ORR: neg_opcode = ORN; value = ~value; break;
4033 case ADD: neg_opcode = SUB; value = -value; break;
4034 case ADC: neg_opcode = SBC; value = ~value; break;
4035 case SUB: neg_opcode = ADD; value = -value; break;
4036 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004037 default:
4038 return false;
4039 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004040 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004041}
4042
Calin Juravle52c48962014-12-16 17:02:57 +00004043void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4044 const FieldInfo& field_info) {
4045 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004046
Calin Juravle52c48962014-12-16 17:02:57 +00004047 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004048 Location base_loc = locations->InAt(0);
4049 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004050 Location out = locations->Out();
4051 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004052 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004053 Primitive::Type field_type = field_info.GetFieldType();
4054 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4055
4056 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004057 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004058 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004059 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004060
Roland Levillainc9285912015-12-18 10:38:42 +00004061 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004062 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004063 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004064
Roland Levillainc9285912015-12-18 10:38:42 +00004065 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004066 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004067 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004068
Roland Levillainc9285912015-12-18 10:38:42 +00004069 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004070 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004071 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004072
4073 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004074 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004075 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004076
4077 case Primitive::kPrimNot: {
4078 // /* HeapReference<Object> */ out = *(base + offset)
4079 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4080 Location temp_loc = locations->GetTemp(0);
4081 // Note that a potential implicit null check is handled in this
4082 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4083 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4084 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4085 if (is_volatile) {
4086 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4087 }
4088 } else {
4089 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4090 codegen_->MaybeRecordImplicitNullCheck(instruction);
4091 if (is_volatile) {
4092 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4093 }
4094 // If read barriers are enabled, emit read barriers other than
4095 // Baker's using a slow path (and also unpoison the loaded
4096 // reference, if heap poisoning is enabled).
4097 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4098 }
4099 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004100 }
4101
Roland Levillainc9285912015-12-18 10:38:42 +00004102 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004103 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004104 GenerateWideAtomicLoad(base, offset,
4105 out.AsRegisterPairLow<Register>(),
4106 out.AsRegisterPairHigh<Register>());
4107 } else {
4108 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4109 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004110 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111
Roland Levillainc9285912015-12-18 10:38:42 +00004112 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004113 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004114 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004115
4116 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004117 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004118 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004119 Register lo = locations->GetTemp(0).AsRegister<Register>();
4120 Register hi = locations->GetTemp(1).AsRegister<Register>();
4121 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004122 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004123 __ vmovdrr(out_reg, lo, hi);
4124 } else {
4125 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004126 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004127 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004128 break;
4129 }
4130
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004131 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004132 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004133 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004134 }
Calin Juravle52c48962014-12-16 17:02:57 +00004135
Roland Levillainc9285912015-12-18 10:38:42 +00004136 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4137 // Potential implicit null checks, in the case of reference or
4138 // double fields, are handled in the previous switch statement.
4139 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004140 codegen_->MaybeRecordImplicitNullCheck(instruction);
4141 }
4142
Calin Juravle52c48962014-12-16 17:02:57 +00004143 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004144 if (field_type == Primitive::kPrimNot) {
4145 // Memory barriers, in the case of references, are also handled
4146 // in the previous switch statement.
4147 } else {
4148 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4149 }
Roland Levillain4d027112015-07-01 15:41:14 +01004150 }
Calin Juravle52c48962014-12-16 17:02:57 +00004151}
4152
4153void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4154 HandleFieldSet(instruction, instruction->GetFieldInfo());
4155}
4156
4157void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004158 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004159}
4160
4161void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4162 HandleFieldGet(instruction, instruction->GetFieldInfo());
4163}
4164
4165void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4166 HandleFieldGet(instruction, instruction->GetFieldInfo());
4167}
4168
4169void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4170 HandleFieldGet(instruction, instruction->GetFieldInfo());
4171}
4172
4173void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4174 HandleFieldGet(instruction, instruction->GetFieldInfo());
4175}
4176
4177void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4178 HandleFieldSet(instruction, instruction->GetFieldInfo());
4179}
4180
4181void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004182 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004183}
4184
Calin Juravlee460d1d2015-09-29 04:52:17 +01004185void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4186 HUnresolvedInstanceFieldGet* instruction) {
4187 FieldAccessCallingConventionARM calling_convention;
4188 codegen_->CreateUnresolvedFieldLocationSummary(
4189 instruction, instruction->GetFieldType(), calling_convention);
4190}
4191
4192void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4193 HUnresolvedInstanceFieldGet* instruction) {
4194 FieldAccessCallingConventionARM calling_convention;
4195 codegen_->GenerateUnresolvedFieldAccess(instruction,
4196 instruction->GetFieldType(),
4197 instruction->GetFieldIndex(),
4198 instruction->GetDexPc(),
4199 calling_convention);
4200}
4201
4202void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4203 HUnresolvedInstanceFieldSet* instruction) {
4204 FieldAccessCallingConventionARM calling_convention;
4205 codegen_->CreateUnresolvedFieldLocationSummary(
4206 instruction, instruction->GetFieldType(), calling_convention);
4207}
4208
4209void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4210 HUnresolvedInstanceFieldSet* instruction) {
4211 FieldAccessCallingConventionARM calling_convention;
4212 codegen_->GenerateUnresolvedFieldAccess(instruction,
4213 instruction->GetFieldType(),
4214 instruction->GetFieldIndex(),
4215 instruction->GetDexPc(),
4216 calling_convention);
4217}
4218
4219void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4220 HUnresolvedStaticFieldGet* instruction) {
4221 FieldAccessCallingConventionARM calling_convention;
4222 codegen_->CreateUnresolvedFieldLocationSummary(
4223 instruction, instruction->GetFieldType(), calling_convention);
4224}
4225
4226void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4227 HUnresolvedStaticFieldGet* instruction) {
4228 FieldAccessCallingConventionARM calling_convention;
4229 codegen_->GenerateUnresolvedFieldAccess(instruction,
4230 instruction->GetFieldType(),
4231 instruction->GetFieldIndex(),
4232 instruction->GetDexPc(),
4233 calling_convention);
4234}
4235
4236void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4237 HUnresolvedStaticFieldSet* instruction) {
4238 FieldAccessCallingConventionARM calling_convention;
4239 codegen_->CreateUnresolvedFieldLocationSummary(
4240 instruction, instruction->GetFieldType(), calling_convention);
4241}
4242
4243void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4244 HUnresolvedStaticFieldSet* instruction) {
4245 FieldAccessCallingConventionARM calling_convention;
4246 codegen_->GenerateUnresolvedFieldAccess(instruction,
4247 instruction->GetFieldType(),
4248 instruction->GetFieldIndex(),
4249 instruction->GetDexPc(),
4250 calling_convention);
4251}
4252
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00004254 codegen_->CreateNullCheckLocations(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004255}
4256
Calin Juravle2ae48182016-03-16 14:05:09 +00004257void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4258 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004259 return;
4260 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004261 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004262
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004263 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004264 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004265}
4266
Calin Juravle2ae48182016-03-16 14:05:09 +00004267void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004268 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004269 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004270
4271 LocationSummary* locations = instruction->GetLocations();
4272 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004273
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004274 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275}
4276
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004277void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004278 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004279}
4280
Artem Serov6c916792016-07-11 14:02:34 +01004281static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4282 switch (type) {
4283 case Primitive::kPrimNot:
4284 return kLoadWord;
4285 case Primitive::kPrimBoolean:
4286 return kLoadUnsignedByte;
4287 case Primitive::kPrimByte:
4288 return kLoadSignedByte;
4289 case Primitive::kPrimChar:
4290 return kLoadUnsignedHalfword;
4291 case Primitive::kPrimShort:
4292 return kLoadSignedHalfword;
4293 case Primitive::kPrimInt:
4294 return kLoadWord;
4295 case Primitive::kPrimLong:
4296 return kLoadWordPair;
4297 case Primitive::kPrimFloat:
4298 return kLoadSWord;
4299 case Primitive::kPrimDouble:
4300 return kLoadDWord;
4301 default:
4302 LOG(FATAL) << "Unreachable type " << type;
4303 UNREACHABLE();
4304 }
4305}
4306
4307static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4308 switch (type) {
4309 case Primitive::kPrimNot:
4310 return kStoreWord;
4311 case Primitive::kPrimBoolean:
4312 case Primitive::kPrimByte:
4313 return kStoreByte;
4314 case Primitive::kPrimChar:
4315 case Primitive::kPrimShort:
4316 return kStoreHalfword;
4317 case Primitive::kPrimInt:
4318 return kStoreWord;
4319 case Primitive::kPrimLong:
4320 return kStoreWordPair;
4321 case Primitive::kPrimFloat:
4322 return kStoreSWord;
4323 case Primitive::kPrimDouble:
4324 return kStoreDWord;
4325 default:
4326 LOG(FATAL) << "Unreachable type " << type;
4327 UNREACHABLE();
4328 }
4329}
4330
4331void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4332 Location out_loc,
4333 Register base,
4334 Register reg_offset,
4335 Condition cond) {
4336 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4337 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4338
4339 switch (type) {
4340 case Primitive::kPrimByte:
4341 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4342 break;
4343 case Primitive::kPrimBoolean:
4344 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4345 break;
4346 case Primitive::kPrimShort:
4347 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4348 break;
4349 case Primitive::kPrimChar:
4350 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4351 break;
4352 case Primitive::kPrimNot:
4353 case Primitive::kPrimInt:
4354 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4355 break;
4356 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4357 case Primitive::kPrimLong:
4358 case Primitive::kPrimFloat:
4359 case Primitive::kPrimDouble:
4360 default:
4361 LOG(FATAL) << "Unreachable type " << type;
4362 UNREACHABLE();
4363 }
4364}
4365
4366void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4367 Location loc,
4368 Register base,
4369 Register reg_offset,
4370 Condition cond) {
4371 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4372 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4373
4374 switch (type) {
4375 case Primitive::kPrimByte:
4376 case Primitive::kPrimBoolean:
4377 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4378 break;
4379 case Primitive::kPrimShort:
4380 case Primitive::kPrimChar:
4381 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4382 break;
4383 case Primitive::kPrimNot:
4384 case Primitive::kPrimInt:
4385 __ str(loc.AsRegister<Register>(), mem_address, cond);
4386 break;
4387 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4388 case Primitive::kPrimLong:
4389 case Primitive::kPrimFloat:
4390 case Primitive::kPrimDouble:
4391 default:
4392 LOG(FATAL) << "Unreachable type " << type;
4393 UNREACHABLE();
4394 }
4395}
4396
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004397void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004398 bool object_array_get_with_read_barrier =
4399 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004400 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004401 new (GetGraph()->GetArena()) LocationSummary(instruction,
4402 object_array_get_with_read_barrier ?
4403 LocationSummary::kCallOnSlowPath :
4404 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004405 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4406 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4407 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004408 locations->SetInAt(0, Location::RequiresRegister());
4409 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004410 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4411 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4412 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004413 // The output overlaps in the case of an object array get with
4414 // read barriers enabled: we do not want the move to overwrite the
4415 // array's location, as we need it to emit the read barrier.
4416 locations->SetOut(
4417 Location::RequiresRegister(),
4418 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004419 }
Roland Levillainc9285912015-12-18 10:38:42 +00004420 // We need a temporary register for the read barrier marking slow
4421 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4422 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4423 locations->AddTemp(Location::RequiresRegister());
4424 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425}
4426
4427void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4428 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004429 Location obj_loc = locations->InAt(0);
4430 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004432 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004433 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004434 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004435 HInstruction* array_instr = instruction->GetArray();
4436 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004437 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4438 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004439
Roland Levillain4d027112015-07-01 15:41:14 +01004440 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004441 case Primitive::kPrimBoolean:
4442 case Primitive::kPrimByte:
4443 case Primitive::kPrimShort:
4444 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004445 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004446 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004447 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4448 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4449
4450 LoadOperandType load_type = GetLoadOperandType(type);
4451 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004452 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004453 Register temp = IP;
4454
4455 if (has_intermediate_address) {
4456 // We do not need to compute the intermediate address from the array: the
4457 // input instruction has done it already. See the comment in
4458 // `TryExtractArrayAccessAddress()`.
4459 if (kIsDebugBuild) {
4460 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4461 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4462 }
4463 temp = obj;
4464 } else {
4465 __ add(temp, obj, ShifterOperand(data_offset));
4466 }
4467 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004468 }
4469 break;
4470 }
4471
Roland Levillainc9285912015-12-18 10:38:42 +00004472 case Primitive::kPrimNot: {
4473 static_assert(
4474 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4475 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004476 // /* HeapReference<Object> */ out =
4477 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4478 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4479 Location temp = locations->GetTemp(0);
4480 // Note that a potential implicit null check is handled in this
4481 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4482 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4483 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4484 } else {
4485 Register out = out_loc.AsRegister<Register>();
4486 if (index.IsConstant()) {
4487 size_t offset =
4488 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4489 __ LoadFromOffset(kLoadWord, out, obj, offset);
4490 codegen_->MaybeRecordImplicitNullCheck(instruction);
4491 // If read barriers are enabled, emit read barriers other than
4492 // Baker's using a slow path (and also unpoison the loaded
4493 // reference, if heap poisoning is enabled).
4494 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4495 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004496 Register temp = IP;
4497
4498 if (has_intermediate_address) {
4499 // We do not need to compute the intermediate address from the array: the
4500 // input instruction has done it already. See the comment in
4501 // `TryExtractArrayAccessAddress()`.
4502 if (kIsDebugBuild) {
4503 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4504 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4505 }
4506 temp = obj;
4507 } else {
4508 __ add(temp, obj, ShifterOperand(data_offset));
4509 }
4510 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004511
Roland Levillainc9285912015-12-18 10:38:42 +00004512 codegen_->MaybeRecordImplicitNullCheck(instruction);
4513 // If read barriers are enabled, emit read barriers other than
4514 // Baker's using a slow path (and also unpoison the loaded
4515 // reference, if heap poisoning is enabled).
4516 codegen_->MaybeGenerateReadBarrierSlow(
4517 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4518 }
4519 }
4520 break;
4521 }
4522
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004524 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004525 size_t offset =
4526 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004527 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004528 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004529 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004530 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004531 }
4532 break;
4533 }
4534
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004535 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004536 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004537 if (index.IsConstant()) {
4538 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004539 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004540 } else {
4541 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004542 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004543 }
4544 break;
4545 }
4546
4547 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004548 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004549 if (index.IsConstant()) {
4550 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004551 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004552 } else {
4553 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004554 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004555 }
4556 break;
4557 }
4558
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004560 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004561 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004562 }
Roland Levillain4d027112015-07-01 15:41:14 +01004563
4564 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004565 // Potential implicit null checks, in the case of reference
4566 // arrays, are handled in the previous switch statement.
4567 } else {
4568 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004569 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004570}
4571
4572void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004573 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004574
4575 bool needs_write_barrier =
4576 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004577 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004578
Nicolas Geoffray39468442014-09-02 15:17:15 +01004579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004580 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004581 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004582 LocationSummary::kCallOnSlowPath :
4583 LocationSummary::kNoCall);
4584
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004585 locations->SetInAt(0, Location::RequiresRegister());
4586 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4587 if (Primitive::IsFloatingPointType(value_type)) {
4588 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004590 locations->SetInAt(2, Location::RequiresRegister());
4591 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004592 if (needs_write_barrier) {
4593 // Temporary registers for the write barrier.
4594 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004595 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004596 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597}
4598
4599void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4600 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004601 Location array_loc = locations->InAt(0);
4602 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004603 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004604 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004605 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004606 bool needs_write_barrier =
4607 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004608 uint32_t data_offset =
4609 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4610 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004611 HInstruction* array_instr = instruction->GetArray();
4612 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004613 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4614 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004615
4616 switch (value_type) {
4617 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004618 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004619 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004620 case Primitive::kPrimChar:
4621 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004623 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4624 uint32_t full_offset =
4625 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4626 StoreOperandType store_type = GetStoreOperandType(value_type);
4627 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004628 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004629 Register temp = IP;
4630
4631 if (has_intermediate_address) {
4632 // We do not need to compute the intermediate address from the array: the
4633 // input instruction has done it already. See the comment in
4634 // `TryExtractArrayAccessAddress()`.
4635 if (kIsDebugBuild) {
4636 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4637 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4638 }
4639 temp = array;
4640 } else {
4641 __ add(temp, array, ShifterOperand(data_offset));
4642 }
Artem Serov6c916792016-07-11 14:02:34 +01004643 codegen_->StoreToShiftedRegOffset(value_type,
4644 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004645 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004646 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004647 }
4648 break;
4649 }
4650
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004652 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004653 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4654 // See the comment in instruction_simplifier_shared.cc.
4655 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004656
4657 if (instruction->InputAt(2)->IsNullConstant()) {
4658 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004659 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004660 size_t offset =
4661 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004662 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004663 } else {
4664 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004665 __ add(IP, array, ShifterOperand(data_offset));
4666 codegen_->StoreToShiftedRegOffset(value_type,
4667 value_loc,
4668 IP,
4669 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004670 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004671 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004672 DCHECK(!needs_write_barrier);
4673 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004674 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004675 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004676
4677 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01004678 Location temp1_loc = locations->GetTemp(0);
4679 Register temp1 = temp1_loc.AsRegister<Register>();
4680 Location temp2_loc = locations->GetTemp(1);
4681 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004682 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4683 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4684 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4685 Label done;
4686 SlowPathCode* slow_path = nullptr;
4687
Roland Levillain3b359c72015-11-17 19:35:12 +00004688 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004689 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4690 codegen_->AddSlowPath(slow_path);
4691 if (instruction->GetValueCanBeNull()) {
4692 Label non_zero;
4693 __ CompareAndBranchIfNonZero(value, &non_zero);
4694 if (index.IsConstant()) {
4695 size_t offset =
4696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4697 __ StoreToOffset(kStoreWord, value, array, offset);
4698 } else {
4699 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004700 __ add(IP, array, ShifterOperand(data_offset));
4701 codegen_->StoreToShiftedRegOffset(value_type,
4702 value_loc,
4703 IP,
4704 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004705 }
4706 codegen_->MaybeRecordImplicitNullCheck(instruction);
4707 __ b(&done);
4708 __ Bind(&non_zero);
4709 }
4710
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004711 // Note that when read barriers are enabled, the type checks
4712 // are performed without read barriers. This is fine, even in
4713 // the case where a class object is in the from-space after
4714 // the flip, as a comparison involving such a type would not
4715 // produce a false positive; it may of course produce a false
4716 // negative, in which case we would take the ArraySet slow
4717 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004718
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004719 // /* HeapReference<Class> */ temp1 = array->klass_
4720 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4721 codegen_->MaybeRecordImplicitNullCheck(instruction);
4722 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01004723
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004724 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4725 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4726 // /* HeapReference<Class> */ temp2 = value->klass_
4727 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4728 // If heap poisoning is enabled, no need to unpoison `temp1`
4729 // nor `temp2`, as we are comparing two poisoned references.
4730 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01004731
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004732 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4733 Label do_put;
4734 __ b(&do_put, EQ);
4735 // If heap poisoning is enabled, the `temp1` reference has
4736 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00004737 __ MaybeUnpoisonHeapReference(temp1);
4738
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004739 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4740 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4741 // If heap poisoning is enabled, no need to unpoison
4742 // `temp1`, as we are comparing against null below.
4743 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4744 __ Bind(&do_put);
4745 } else {
4746 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004747 }
4748 }
4749
Artem Serov6c916792016-07-11 14:02:34 +01004750 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004751 if (kPoisonHeapReferences) {
4752 // Note that in the case where `value` is a null reference,
4753 // we do not enter this block, as a null reference does not
4754 // need poisoning.
4755 DCHECK_EQ(value_type, Primitive::kPrimNot);
4756 __ Mov(temp1, value);
4757 __ PoisonHeapReference(temp1);
4758 source = temp1;
4759 }
4760
4761 if (index.IsConstant()) {
4762 size_t offset =
4763 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4764 __ StoreToOffset(kStoreWord, source, array, offset);
4765 } else {
4766 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004767
4768 __ add(IP, array, ShifterOperand(data_offset));
4769 codegen_->StoreToShiftedRegOffset(value_type,
4770 Location::RegisterLocation(source),
4771 IP,
4772 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 }
4774
Roland Levillain3b359c72015-11-17 19:35:12 +00004775 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004776 codegen_->MaybeRecordImplicitNullCheck(instruction);
4777 }
4778
4779 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4780
4781 if (done.IsLinked()) {
4782 __ Bind(&done);
4783 }
4784
4785 if (slow_path != nullptr) {
4786 __ Bind(slow_path->GetExitLabel());
4787 }
4788
4789 break;
4790 }
4791
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004792 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004793 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004794 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004795 size_t offset =
4796 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004797 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004800 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 }
4802 break;
4803 }
4804
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004805 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004806 Location value = locations->InAt(2);
4807 DCHECK(value.IsFpuRegister());
4808 if (index.IsConstant()) {
4809 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004810 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004811 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004812 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004813 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4814 }
4815 break;
4816 }
4817
4818 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004819 Location value = locations->InAt(2);
4820 DCHECK(value.IsFpuRegisterPair());
4821 if (index.IsConstant()) {
4822 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004823 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004824 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004825 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004826 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4827 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004828
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004829 break;
4830 }
4831
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004832 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004833 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004834 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004835 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004836
Roland Levillain80e67092016-01-08 16:04:55 +00004837 // Objects are handled in the switch.
4838 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004839 codegen_->MaybeRecordImplicitNullCheck(instruction);
4840 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841}
4842
4843void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004844 LocationSummary* locations =
4845 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004846 locations->SetInAt(0, Location::RequiresRegister());
4847 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848}
4849
4850void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4851 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004852 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004853 Register obj = locations->InAt(0).AsRegister<Register>();
4854 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004855 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004856 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857}
4858
Artem Serov328429f2016-07-06 16:23:04 +01004859void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00004860 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4861 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01004862 LocationSummary* locations =
4863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4864
4865 locations->SetInAt(0, Location::RequiresRegister());
4866 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4867 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4868}
4869
4870void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4871 LocationSummary* locations = instruction->GetLocations();
4872 Location out = locations->Out();
4873 Location first = locations->InAt(0);
4874 Location second = locations->InAt(1);
4875
Roland Levillain4a3aa572016-08-15 13:17:06 +00004876 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4877 DCHECK(!kEmitCompilerReadBarrier);
4878
Artem Serov328429f2016-07-06 16:23:04 +01004879 if (second.IsRegister()) {
4880 __ add(out.AsRegister<Register>(),
4881 first.AsRegister<Register>(),
4882 ShifterOperand(second.AsRegister<Register>()));
4883 } else {
4884 __ AddConstant(out.AsRegister<Register>(),
4885 first.AsRegister<Register>(),
4886 second.GetConstant()->AsIntConstant()->GetValue());
4887 }
4888}
4889
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004891 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4892 ? LocationSummary::kCallOnSlowPath
4893 : LocationSummary::kNoCall;
4894 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004895 locations->SetInAt(0, Location::RequiresRegister());
4896 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004897 if (instruction->HasUses()) {
4898 locations->SetOut(Location::SameAsFirstInput());
4899 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900}
4901
4902void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4903 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004904 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004905 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004906 codegen_->AddSlowPath(slow_path);
4907
Roland Levillain271ab9c2014-11-27 15:23:57 +00004908 Register index = locations->InAt(0).AsRegister<Register>();
4909 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910
4911 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004912 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004913}
4914
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004915void CodeGeneratorARM::MarkGCCard(Register temp,
4916 Register card,
4917 Register object,
4918 Register value,
4919 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004920 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004921 if (can_be_null) {
4922 __ CompareAndBranchIfZero(value, &is_null);
4923 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004924 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4926 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004927 if (can_be_null) {
4928 __ Bind(&is_null);
4929 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004930}
4931
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004932void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004933 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004934}
4935
4936void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004937 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4938}
4939
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004940void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004941 LocationSummary* locations =
4942 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4943 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004944}
4945
4946void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004947 HBasicBlock* block = instruction->GetBlock();
4948 if (block->GetLoopInformation() != nullptr) {
4949 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4950 // The back edge will generate the suspend check.
4951 return;
4952 }
4953 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4954 // The goto will generate the suspend check.
4955 return;
4956 }
4957 GenerateSuspendCheck(instruction, nullptr);
4958}
4959
4960void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4961 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004962 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004963 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4964 if (slow_path == nullptr) {
4965 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4966 instruction->SetSlowPath(slow_path);
4967 codegen_->AddSlowPath(slow_path);
4968 if (successor != nullptr) {
4969 DCHECK(successor->IsLoopHeader());
4970 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4971 }
4972 } else {
4973 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4974 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004975
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004976 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004977 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004978 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004979 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004980 __ Bind(slow_path->GetReturnLabel());
4981 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004982 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004983 __ b(slow_path->GetEntryLabel());
4984 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004985}
4986
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004987ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4988 return codegen_->GetAssembler();
4989}
4990
4991void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004992 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004993 Location source = move->GetSource();
4994 Location destination = move->GetDestination();
4995
4996 if (source.IsRegister()) {
4997 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004998 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004999 } else if (destination.IsFpuRegister()) {
5000 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005001 } else {
5002 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005003 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005004 SP, destination.GetStackIndex());
5005 }
5006 } else if (source.IsStackSlot()) {
5007 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005008 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005009 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005010 } else if (destination.IsFpuRegister()) {
5011 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005012 } else {
5013 DCHECK(destination.IsStackSlot());
5014 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5015 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5016 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005017 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005018 if (destination.IsRegister()) {
5019 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5020 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005021 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005022 } else {
5023 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005024 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5025 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005026 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005027 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005028 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5029 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005030 } else if (destination.IsRegisterPair()) {
5031 DCHECK(ExpectedPairLayout(destination));
5032 __ LoadFromOffset(
5033 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5034 } else {
5035 DCHECK(destination.IsFpuRegisterPair()) << destination;
5036 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5037 SP,
5038 source.GetStackIndex());
5039 }
5040 } else if (source.IsRegisterPair()) {
5041 if (destination.IsRegisterPair()) {
5042 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5043 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005044 } else if (destination.IsFpuRegisterPair()) {
5045 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5046 source.AsRegisterPairLow<Register>(),
5047 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005048 } else {
5049 DCHECK(destination.IsDoubleStackSlot()) << destination;
5050 DCHECK(ExpectedPairLayout(source));
5051 __ StoreToOffset(
5052 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5053 }
5054 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005055 if (destination.IsRegisterPair()) {
5056 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5057 destination.AsRegisterPairHigh<Register>(),
5058 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5059 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005060 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5061 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5062 } else {
5063 DCHECK(destination.IsDoubleStackSlot()) << destination;
5064 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5065 SP,
5066 destination.GetStackIndex());
5067 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005068 } else {
5069 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005070 HConstant* constant = source.GetConstant();
5071 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5072 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005073 if (destination.IsRegister()) {
5074 __ LoadImmediate(destination.AsRegister<Register>(), value);
5075 } else {
5076 DCHECK(destination.IsStackSlot());
5077 __ LoadImmediate(IP, value);
5078 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5079 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005080 } else if (constant->IsLongConstant()) {
5081 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005082 if (destination.IsRegisterPair()) {
5083 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5084 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005085 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005086 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005087 __ LoadImmediate(IP, Low32Bits(value));
5088 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5089 __ LoadImmediate(IP, High32Bits(value));
5090 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5091 }
5092 } else if (constant->IsDoubleConstant()) {
5093 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005094 if (destination.IsFpuRegisterPair()) {
5095 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005096 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005097 DCHECK(destination.IsDoubleStackSlot()) << destination;
5098 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005099 __ LoadImmediate(IP, Low32Bits(int_value));
5100 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5101 __ LoadImmediate(IP, High32Bits(int_value));
5102 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5103 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005104 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005105 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005106 float value = constant->AsFloatConstant()->GetValue();
5107 if (destination.IsFpuRegister()) {
5108 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5109 } else {
5110 DCHECK(destination.IsStackSlot());
5111 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5112 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5113 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005114 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005115 }
5116}
5117
5118void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5119 __ Mov(IP, reg);
5120 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5121 __ StoreToOffset(kStoreWord, IP, SP, mem);
5122}
5123
5124void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5125 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5126 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5127 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5128 SP, mem1 + stack_offset);
5129 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5130 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5131 SP, mem2 + stack_offset);
5132 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5133}
5134
5135void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005136 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005137 Location source = move->GetSource();
5138 Location destination = move->GetDestination();
5139
5140 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005141 DCHECK_NE(source.AsRegister<Register>(), IP);
5142 DCHECK_NE(destination.AsRegister<Register>(), IP);
5143 __ Mov(IP, source.AsRegister<Register>());
5144 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5145 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005146 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005147 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005148 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005149 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005150 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5151 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005152 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005153 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005154 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005155 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005156 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005157 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005158 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005159 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005160 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5161 destination.AsRegisterPairHigh<Register>(),
5162 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005163 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005164 Register low_reg = source.IsRegisterPair()
5165 ? source.AsRegisterPairLow<Register>()
5166 : destination.AsRegisterPairLow<Register>();
5167 int mem = source.IsRegisterPair()
5168 ? destination.GetStackIndex()
5169 : source.GetStackIndex();
5170 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005171 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005172 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005173 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005174 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005175 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5176 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005177 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005178 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005179 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005180 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5181 DRegister reg = source.IsFpuRegisterPair()
5182 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5183 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5184 int mem = source.IsFpuRegisterPair()
5185 ? destination.GetStackIndex()
5186 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005187 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005188 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005189 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005190 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5191 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5192 : destination.AsFpuRegister<SRegister>();
5193 int mem = source.IsFpuRegister()
5194 ? destination.GetStackIndex()
5195 : source.GetStackIndex();
5196
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005197 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005198 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005199 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005200 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005201 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5202 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005203 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005204 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005205 }
5206}
5207
5208void ParallelMoveResolverARM::SpillScratch(int reg) {
5209 __ Push(static_cast<Register>(reg));
5210}
5211
5212void ParallelMoveResolverARM::RestoreScratch(int reg) {
5213 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005214}
5215
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005216HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5217 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005218 switch (desired_class_load_kind) {
5219 case HLoadClass::LoadKind::kReferrersClass:
5220 break;
5221 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5222 DCHECK(!GetCompilerOptions().GetCompilePic());
5223 break;
5224 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5225 DCHECK(GetCompilerOptions().GetCompilePic());
5226 break;
5227 case HLoadClass::LoadKind::kBootImageAddress:
5228 break;
5229 case HLoadClass::LoadKind::kDexCacheAddress:
5230 DCHECK(Runtime::Current()->UseJitCompilation());
5231 break;
5232 case HLoadClass::LoadKind::kDexCachePcRelative:
5233 DCHECK(!Runtime::Current()->UseJitCompilation());
5234 // We disable pc-relative load when there is an irreducible loop, as the optimization
5235 // is incompatible with it.
5236 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5237 // with irreducible loops.
5238 if (GetGraph()->HasIrreducibleLoops()) {
5239 return HLoadClass::LoadKind::kDexCacheViaMethod;
5240 }
5241 break;
5242 case HLoadClass::LoadKind::kDexCacheViaMethod:
5243 break;
5244 }
5245 return desired_class_load_kind;
5246}
5247
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005248void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005249 if (cls->NeedsAccessCheck()) {
5250 InvokeRuntimeCallingConvention calling_convention;
5251 CodeGenerator::CreateLoadClassLocationSummary(
5252 cls,
5253 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5254 Location::RegisterLocation(R0),
5255 /* code_generator_supports_read_barrier */ true);
5256 return;
5257 }
5258
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005259 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5260 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005261 ? LocationSummary::kCallOnSlowPath
5262 : LocationSummary::kNoCall;
5263 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005264 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005265 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5266 }
5267
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005268 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5269 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5270 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5271 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5272 locations->SetInAt(0, Location::RequiresRegister());
5273 }
5274 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005275}
5276
5277void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005278 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005279 if (cls->NeedsAccessCheck()) {
5280 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005281 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005282 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005283 return;
5284 }
5285
Roland Levillain3b359c72015-11-17 19:35:12 +00005286 Location out_loc = locations->Out();
5287 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005288
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005289 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005290 bool generate_null_check = false;
5291 switch (cls->GetLoadKind()) {
5292 case HLoadClass::LoadKind::kReferrersClass: {
5293 DCHECK(!cls->CanCallRuntime());
5294 DCHECK(!cls->MustGenerateClinitCheck());
5295 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5296 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005297 GenerateGcRootFieldLoad(cls,
5298 out_loc,
5299 current_method,
5300 ArtMethod::DeclaringClassOffset().Int32Value(),
5301 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005302 break;
5303 }
5304 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005305 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005306 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5307 cls->GetTypeIndex()));
5308 break;
5309 }
5310 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005311 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005312 CodeGeneratorARM::PcRelativePatchInfo* labels =
5313 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5314 __ BindTrackedLabel(&labels->movw_label);
5315 __ movw(out, /* placeholder */ 0u);
5316 __ BindTrackedLabel(&labels->movt_label);
5317 __ movt(out, /* placeholder */ 0u);
5318 __ BindTrackedLabel(&labels->add_pc_label);
5319 __ add(out, out, ShifterOperand(PC));
5320 break;
5321 }
5322 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005323 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005324 DCHECK_NE(cls->GetAddress(), 0u);
5325 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5326 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5327 break;
5328 }
5329 case HLoadClass::LoadKind::kDexCacheAddress: {
5330 DCHECK_NE(cls->GetAddress(), 0u);
5331 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5332 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5333 // a 128B range. To try and reduce the number of literals if we load multiple types,
5334 // simply split the dex cache address to a 128B aligned base loaded from a literal
5335 // and the remaining offset embedded in the load.
5336 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5337 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5338 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5339 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5340 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5341 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5342 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005343 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005344 generate_null_check = !cls->IsInDexCache();
5345 break;
5346 }
5347 case HLoadClass::LoadKind::kDexCachePcRelative: {
5348 Register base_reg = locations->InAt(0).AsRegister<Register>();
5349 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5350 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5351 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005352 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005353 generate_null_check = !cls->IsInDexCache();
5354 break;
5355 }
5356 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5357 // /* GcRoot<mirror::Class>[] */ out =
5358 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5359 Register current_method = locations->InAt(0).AsRegister<Register>();
5360 __ LoadFromOffset(kLoadWord,
5361 out,
5362 current_method,
5363 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5364 // /* GcRoot<mirror::Class> */ out = out[type_index]
5365 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005366 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005367 generate_null_check = !cls->IsInDexCache();
5368 }
5369 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005370
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005371 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5372 DCHECK(cls->CanCallRuntime());
5373 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5374 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5375 codegen_->AddSlowPath(slow_path);
5376 if (generate_null_check) {
5377 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5378 }
5379 if (cls->MustGenerateClinitCheck()) {
5380 GenerateClassInitializationCheck(slow_path, out);
5381 } else {
5382 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005383 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005384 }
5385}
5386
5387void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5388 LocationSummary* locations =
5389 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5390 locations->SetInAt(0, Location::RequiresRegister());
5391 if (check->HasUses()) {
5392 locations->SetOut(Location::SameAsFirstInput());
5393 }
5394}
5395
5396void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005397 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005398 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005399 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005400 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005401 GenerateClassInitializationCheck(slow_path,
5402 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005403}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005404
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005405void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005406 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005407 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5408 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5409 __ b(slow_path->GetEntryLabel(), LT);
5410 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5411 // properly. Therefore, we do a memory fence.
5412 __ dmb(ISH);
5413 __ Bind(slow_path->GetExitLabel());
5414}
5415
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005416HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5417 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005418 switch (desired_string_load_kind) {
5419 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5420 DCHECK(!GetCompilerOptions().GetCompilePic());
5421 break;
5422 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5423 DCHECK(GetCompilerOptions().GetCompilePic());
5424 break;
5425 case HLoadString::LoadKind::kBootImageAddress:
5426 break;
5427 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005428 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005429 break;
5430 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005431 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005432 // We disable pc-relative load when there is an irreducible loop, as the optimization
5433 // is incompatible with it.
5434 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5435 // with irreducible loops.
5436 if (GetGraph()->HasIrreducibleLoops()) {
5437 return HLoadString::LoadKind::kDexCacheViaMethod;
5438 }
5439 break;
5440 case HLoadString::LoadKind::kDexCacheViaMethod:
5441 break;
5442 }
5443 return desired_string_load_kind;
5444}
5445
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005446void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005447 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5448 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005449 : LocationSummary::kNoCall;
5450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005451
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005452 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005453 DCHECK(load_kind != HLoadString::LoadKind::kDexCachePcRelative) << "Not supported";
5454 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005455 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005456 locations->SetOut(Location::RegisterLocation(R0));
5457 } else {
5458 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005459 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005460}
5461
5462void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005463 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005464 Location out_loc = locations->Out();
5465 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005466 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005467
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005468 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005469 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005470 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5471 load->GetStringIndex()));
5472 return; // No dex cache slow path.
5473 }
5474 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005475 CodeGeneratorARM::PcRelativePatchInfo* labels =
5476 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5477 __ BindTrackedLabel(&labels->movw_label);
5478 __ movw(out, /* placeholder */ 0u);
5479 __ BindTrackedLabel(&labels->movt_label);
5480 __ movt(out, /* placeholder */ 0u);
5481 __ BindTrackedLabel(&labels->add_pc_label);
5482 __ add(out, out, ShifterOperand(PC));
5483 return; // No dex cache slow path.
5484 }
5485 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005486 DCHECK_NE(load->GetAddress(), 0u);
5487 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5488 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5489 return; // No dex cache slow path.
5490 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005491 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005492 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005493 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005494
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005495 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5496 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5497 InvokeRuntimeCallingConvention calling_convention;
5498 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5499 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5500 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005501}
5502
David Brazdilcb1c0552015-08-04 16:22:25 +01005503static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005504 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005505}
5506
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005507void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5508 LocationSummary* locations =
5509 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5510 locations->SetOut(Location::RequiresRegister());
5511}
5512
5513void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005514 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005515 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5516}
5517
5518void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5519 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5520}
5521
5522void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005523 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005524 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005525}
5526
5527void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5528 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005529 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005530 InvokeRuntimeCallingConvention calling_convention;
5531 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5532}
5533
5534void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005535 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005536 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005537}
5538
Roland Levillainc9285912015-12-18 10:38:42 +00005539static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5540 return kEmitCompilerReadBarrier &&
5541 (kUseBakerReadBarrier ||
5542 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5543 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5544 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5545}
5546
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005547void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005548 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005549 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005550 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005551 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005552 case TypeCheckKind::kExactCheck:
5553 case TypeCheckKind::kAbstractClassCheck:
5554 case TypeCheckKind::kClassHierarchyCheck:
5555 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005556 call_kind =
5557 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005558 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005559 break;
5560 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005561 case TypeCheckKind::kUnresolvedCheck:
5562 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005563 call_kind = LocationSummary::kCallOnSlowPath;
5564 break;
5565 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005566
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005568 if (baker_read_barrier_slow_path) {
5569 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5570 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005571 locations->SetInAt(0, Location::RequiresRegister());
5572 locations->SetInAt(1, Location::RequiresRegister());
5573 // The "out" register is used as a temporary, so it overlaps with the inputs.
5574 // Note that TypeCheckSlowPathARM uses this register too.
5575 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5576 // When read barriers are enabled, we need a temporary register for
5577 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005578 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005579 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005580 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005581}
5582
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005583void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005584 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005585 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005586 Location obj_loc = locations->InAt(0);
5587 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005588 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005589 Location out_loc = locations->Out();
5590 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005591 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005592 locations->GetTemp(0) :
5593 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005594 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005595 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5596 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5597 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005598 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005599 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005600
5601 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005602 // avoid null check if we know obj is not null.
5603 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005604 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005605 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005606
Roland Levillain3b359c72015-11-17 19:35:12 +00005607 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005608 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005609
Roland Levillainc9285912015-12-18 10:38:42 +00005610 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005611 case TypeCheckKind::kExactCheck: {
5612 __ cmp(out, ShifterOperand(cls));
5613 // Classes must be equal for the instanceof to succeed.
5614 __ b(&zero, NE);
5615 __ LoadImmediate(out, 1);
5616 __ b(&done);
5617 break;
5618 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005619
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005620 case TypeCheckKind::kAbstractClassCheck: {
5621 // If the class is abstract, we eagerly fetch the super class of the
5622 // object to avoid doing a comparison we know will fail.
5623 Label loop;
5624 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005625 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005626 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005627 // If `out` is null, we use it for the result, and jump to `done`.
5628 __ CompareAndBranchIfZero(out, &done);
5629 __ cmp(out, ShifterOperand(cls));
5630 __ b(&loop, NE);
5631 __ LoadImmediate(out, 1);
5632 if (zero.IsLinked()) {
5633 __ b(&done);
5634 }
5635 break;
5636 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005637
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005638 case TypeCheckKind::kClassHierarchyCheck: {
5639 // Walk over the class hierarchy to find a match.
5640 Label loop, success;
5641 __ Bind(&loop);
5642 __ cmp(out, ShifterOperand(cls));
5643 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005644 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005645 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005646 __ CompareAndBranchIfNonZero(out, &loop);
5647 // If `out` is null, we use it for the result, and jump to `done`.
5648 __ b(&done);
5649 __ Bind(&success);
5650 __ LoadImmediate(out, 1);
5651 if (zero.IsLinked()) {
5652 __ b(&done);
5653 }
5654 break;
5655 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005656
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005657 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005658 // Do an exact check.
5659 Label exact_check;
5660 __ cmp(out, ShifterOperand(cls));
5661 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005662 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005663 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005664 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005665 // If `out` is null, we use it for the result, and jump to `done`.
5666 __ CompareAndBranchIfZero(out, &done);
5667 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5668 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5669 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005670 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 __ LoadImmediate(out, 1);
5672 __ b(&done);
5673 break;
5674 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005675
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676 case TypeCheckKind::kArrayCheck: {
5677 __ cmp(out, ShifterOperand(cls));
5678 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005679 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5680 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 codegen_->AddSlowPath(slow_path);
5682 __ b(slow_path->GetEntryLabel(), NE);
5683 __ LoadImmediate(out, 1);
5684 if (zero.IsLinked()) {
5685 __ b(&done);
5686 }
5687 break;
5688 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005689
Calin Juravle98893e12015-10-02 21:05:03 +01005690 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005691 case TypeCheckKind::kInterfaceCheck: {
5692 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005693 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005694 // cases.
5695 //
5696 // We cannot directly call the InstanceofNonTrivial runtime
5697 // entry point without resorting to a type checking slow path
5698 // here (i.e. by calling InvokeRuntime directly), as it would
5699 // require to assign fixed registers for the inputs of this
5700 // HInstanceOf instruction (following the runtime calling
5701 // convention), which might be cluttered by the potential first
5702 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005703 //
5704 // TODO: Introduce a new runtime entry point taking the object
5705 // to test (instead of its class) as argument, and let it deal
5706 // with the read barrier issues. This will let us refactor this
5707 // case of the `switch` code as it was previously (with a direct
5708 // call to the runtime not using a type checking slow path).
5709 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005710 DCHECK(locations->OnlyCallsOnSlowPath());
5711 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5712 /* is_fatal */ false);
5713 codegen_->AddSlowPath(slow_path);
5714 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005715 if (zero.IsLinked()) {
5716 __ b(&done);
5717 }
5718 break;
5719 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005720 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005721
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005722 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005723 __ Bind(&zero);
5724 __ LoadImmediate(out, 0);
5725 }
5726
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 if (done.IsLinked()) {
5728 __ Bind(&done);
5729 }
5730
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005731 if (slow_path != nullptr) {
5732 __ Bind(slow_path->GetExitLabel());
5733 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005734}
5735
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005736void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005737 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5738 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5739
Roland Levillain3b359c72015-11-17 19:35:12 +00005740 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5741 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 case TypeCheckKind::kExactCheck:
5743 case TypeCheckKind::kAbstractClassCheck:
5744 case TypeCheckKind::kClassHierarchyCheck:
5745 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005746 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5747 LocationSummary::kCallOnSlowPath :
5748 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005749 break;
5750 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005751 case TypeCheckKind::kUnresolvedCheck:
5752 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005753 call_kind = LocationSummary::kCallOnSlowPath;
5754 break;
5755 }
5756
Roland Levillain3b359c72015-11-17 19:35:12 +00005757 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5758 locations->SetInAt(0, Location::RequiresRegister());
5759 locations->SetInAt(1, Location::RequiresRegister());
5760 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5761 locations->AddTemp(Location::RequiresRegister());
5762 // When read barriers are enabled, we need an additional temporary
5763 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005764 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005766 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005767}
5768
5769void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005770 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005771 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005772 Location obj_loc = locations->InAt(0);
5773 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005774 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005775 Location temp_loc = locations->GetTemp(0);
5776 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005777 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005778 locations->GetTemp(1) :
5779 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005780 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5782 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5783 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005784
Roland Levillain3b359c72015-11-17 19:35:12 +00005785 bool is_type_check_slow_path_fatal =
5786 (type_check_kind == TypeCheckKind::kExactCheck ||
5787 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5788 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5789 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5790 !instruction->CanThrowIntoCatchBlock();
5791 SlowPathCode* type_check_slow_path =
5792 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5793 is_type_check_slow_path_fatal);
5794 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795
5796 Label done;
5797 // Avoid null check if we know obj is not null.
5798 if (instruction->MustDoNullCheck()) {
5799 __ CompareAndBranchIfZero(obj, &done);
5800 }
5801
Roland Levillain3b359c72015-11-17 19:35:12 +00005802 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005803 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005804
Roland Levillain3b359c72015-11-17 19:35:12 +00005805 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 case TypeCheckKind::kExactCheck:
5807 case TypeCheckKind::kArrayCheck: {
5808 __ cmp(temp, ShifterOperand(cls));
5809 // Jump to slow path for throwing the exception or doing a
5810 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005811 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 break;
5813 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005814
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005815 case TypeCheckKind::kAbstractClassCheck: {
5816 // If the class is abstract, we eagerly fetch the super class of the
5817 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005818 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005820 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005821 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005822
5823 // If the class reference currently in `temp` is not null, jump
5824 // to the `compare_classes` label to compare it with the checked
5825 // class.
5826 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5827 // Otherwise, jump to the slow path to throw the exception.
5828 //
5829 // But before, move back the object's class into `temp` before
5830 // going into the slow path, as it has been overwritten in the
5831 // meantime.
5832 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005833 GenerateReferenceLoadTwoRegisters(
5834 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005835 __ b(type_check_slow_path->GetEntryLabel());
5836
5837 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 __ cmp(temp, ShifterOperand(cls));
5839 __ b(&loop, NE);
5840 break;
5841 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005842
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005843 case TypeCheckKind::kClassHierarchyCheck: {
5844 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005845 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005846 __ Bind(&loop);
5847 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005848 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005849
Roland Levillain3b359c72015-11-17 19:35:12 +00005850 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005851 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005852
5853 // If the class reference currently in `temp` is not null, jump
5854 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005855 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005856 // Otherwise, jump to the slow path to throw the exception.
5857 //
5858 // But before, move back the object's class into `temp` before
5859 // going into the slow path, as it has been overwritten in the
5860 // meantime.
5861 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005862 GenerateReferenceLoadTwoRegisters(
5863 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005864 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865 break;
5866 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005867
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005868 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005869 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005870 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005871 __ cmp(temp, ShifterOperand(cls));
5872 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005873
5874 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005875 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005876 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005877
5878 // If the component type is not null (i.e. the object is indeed
5879 // an array), jump to label `check_non_primitive_component_type`
5880 // to further check that this component type is not a primitive
5881 // type.
5882 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5883 // Otherwise, jump to the slow path to throw the exception.
5884 //
5885 // But before, move back the object's class into `temp` before
5886 // going into the slow path, as it has been overwritten in the
5887 // meantime.
5888 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005889 GenerateReferenceLoadTwoRegisters(
5890 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005891 __ b(type_check_slow_path->GetEntryLabel());
5892
5893 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005894 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005895 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5896 __ CompareAndBranchIfZero(temp, &done);
5897 // Same comment as above regarding `temp` and the slow path.
5898 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005899 GenerateReferenceLoadTwoRegisters(
5900 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005901 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005902 break;
5903 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005904
Calin Juravle98893e12015-10-02 21:05:03 +01005905 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005907 // We always go into the type check slow path for the unresolved
5908 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005909 //
5910 // We cannot directly call the CheckCast runtime entry point
5911 // without resorting to a type checking slow path here (i.e. by
5912 // calling InvokeRuntime directly), as it would require to
5913 // assign fixed registers for the inputs of this HInstanceOf
5914 // instruction (following the runtime calling convention), which
5915 // might be cluttered by the potential first read barrier
5916 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005917 //
5918 // TODO: Introduce a new runtime entry point taking the object
5919 // to test (instead of its class) as argument, and let it deal
5920 // with the read barrier issues. This will let us refactor this
5921 // case of the `switch` code as it was previously (with a direct
5922 // call to the runtime not using a type checking slow path).
5923 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005924 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 break;
5926 }
5927 __ Bind(&done);
5928
Roland Levillain3b359c72015-11-17 19:35:12 +00005929 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005930}
5931
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005932void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5933 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005935 InvokeRuntimeCallingConvention calling_convention;
5936 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5937}
5938
5939void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005940 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
5941 instruction,
5942 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005943 if (instruction->IsEnter()) {
5944 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5945 } else {
5946 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5947 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005948}
5949
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005950void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5951void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5952void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005953
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005954void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005955 LocationSummary* locations =
5956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5957 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5958 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005959 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005960 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005961 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005963}
5964
5965void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5966 HandleBitwiseOperation(instruction);
5967}
5968
5969void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5970 HandleBitwiseOperation(instruction);
5971}
5972
5973void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5974 HandleBitwiseOperation(instruction);
5975}
5976
Artem Serov7fc63502016-02-09 17:15:29 +00005977
5978void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5979 LocationSummary* locations =
5980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5981 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5982 || instruction->GetResultType() == Primitive::kPrimLong);
5983
5984 locations->SetInAt(0, Location::RequiresRegister());
5985 locations->SetInAt(1, Location::RequiresRegister());
5986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5987}
5988
5989void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5990 LocationSummary* locations = instruction->GetLocations();
5991 Location first = locations->InAt(0);
5992 Location second = locations->InAt(1);
5993 Location out = locations->Out();
5994
5995 if (instruction->GetResultType() == Primitive::kPrimInt) {
5996 Register first_reg = first.AsRegister<Register>();
5997 ShifterOperand second_reg(second.AsRegister<Register>());
5998 Register out_reg = out.AsRegister<Register>();
5999
6000 switch (instruction->GetOpKind()) {
6001 case HInstruction::kAnd:
6002 __ bic(out_reg, first_reg, second_reg);
6003 break;
6004 case HInstruction::kOr:
6005 __ orn(out_reg, first_reg, second_reg);
6006 break;
6007 // There is no EON on arm.
6008 case HInstruction::kXor:
6009 default:
6010 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6011 UNREACHABLE();
6012 }
6013 return;
6014
6015 } else {
6016 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6017 Register first_low = first.AsRegisterPairLow<Register>();
6018 Register first_high = first.AsRegisterPairHigh<Register>();
6019 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6020 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6021 Register out_low = out.AsRegisterPairLow<Register>();
6022 Register out_high = out.AsRegisterPairHigh<Register>();
6023
6024 switch (instruction->GetOpKind()) {
6025 case HInstruction::kAnd:
6026 __ bic(out_low, first_low, second_low);
6027 __ bic(out_high, first_high, second_high);
6028 break;
6029 case HInstruction::kOr:
6030 __ orn(out_low, first_low, second_low);
6031 __ orn(out_high, first_high, second_high);
6032 break;
6033 // There is no EON on arm.
6034 case HInstruction::kXor:
6035 default:
6036 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6037 UNREACHABLE();
6038 }
6039 }
6040}
6041
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006042void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6043 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6044 if (value == 0xffffffffu) {
6045 if (out != first) {
6046 __ mov(out, ShifterOperand(first));
6047 }
6048 return;
6049 }
6050 if (value == 0u) {
6051 __ mov(out, ShifterOperand(0));
6052 return;
6053 }
6054 ShifterOperand so;
6055 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6056 __ and_(out, first, so);
6057 } else {
6058 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6059 __ bic(out, first, ShifterOperand(~value));
6060 }
6061}
6062
6063void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6064 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6065 if (value == 0u) {
6066 if (out != first) {
6067 __ mov(out, ShifterOperand(first));
6068 }
6069 return;
6070 }
6071 if (value == 0xffffffffu) {
6072 __ mvn(out, ShifterOperand(0));
6073 return;
6074 }
6075 ShifterOperand so;
6076 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6077 __ orr(out, first, so);
6078 } else {
6079 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6080 __ orn(out, first, ShifterOperand(~value));
6081 }
6082}
6083
6084void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6085 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6086 if (value == 0u) {
6087 if (out != first) {
6088 __ mov(out, ShifterOperand(first));
6089 }
6090 return;
6091 }
6092 __ eor(out, first, ShifterOperand(value));
6093}
6094
Vladimir Marko59751a72016-08-05 14:37:27 +01006095void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6096 Location first,
6097 uint64_t value) {
6098 Register out_low = out.AsRegisterPairLow<Register>();
6099 Register out_high = out.AsRegisterPairHigh<Register>();
6100 Register first_low = first.AsRegisterPairLow<Register>();
6101 Register first_high = first.AsRegisterPairHigh<Register>();
6102 uint32_t value_low = Low32Bits(value);
6103 uint32_t value_high = High32Bits(value);
6104 if (value_low == 0u) {
6105 if (out_low != first_low) {
6106 __ mov(out_low, ShifterOperand(first_low));
6107 }
6108 __ AddConstant(out_high, first_high, value_high);
6109 return;
6110 }
6111 __ AddConstantSetFlags(out_low, first_low, value_low);
6112 ShifterOperand so;
6113 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6114 __ adc(out_high, first_high, so);
6115 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6116 __ sbc(out_high, first_high, so);
6117 } else {
6118 LOG(FATAL) << "Unexpected constant " << value_high;
6119 UNREACHABLE();
6120 }
6121}
6122
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006123void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6124 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006125 Location first = locations->InAt(0);
6126 Location second = locations->InAt(1);
6127 Location out = locations->Out();
6128
6129 if (second.IsConstant()) {
6130 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6131 uint32_t value_low = Low32Bits(value);
6132 if (instruction->GetResultType() == Primitive::kPrimInt) {
6133 Register first_reg = first.AsRegister<Register>();
6134 Register out_reg = out.AsRegister<Register>();
6135 if (instruction->IsAnd()) {
6136 GenerateAndConst(out_reg, first_reg, value_low);
6137 } else if (instruction->IsOr()) {
6138 GenerateOrrConst(out_reg, first_reg, value_low);
6139 } else {
6140 DCHECK(instruction->IsXor());
6141 GenerateEorConst(out_reg, first_reg, value_low);
6142 }
6143 } else {
6144 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6145 uint32_t value_high = High32Bits(value);
6146 Register first_low = first.AsRegisterPairLow<Register>();
6147 Register first_high = first.AsRegisterPairHigh<Register>();
6148 Register out_low = out.AsRegisterPairLow<Register>();
6149 Register out_high = out.AsRegisterPairHigh<Register>();
6150 if (instruction->IsAnd()) {
6151 GenerateAndConst(out_low, first_low, value_low);
6152 GenerateAndConst(out_high, first_high, value_high);
6153 } else if (instruction->IsOr()) {
6154 GenerateOrrConst(out_low, first_low, value_low);
6155 GenerateOrrConst(out_high, first_high, value_high);
6156 } else {
6157 DCHECK(instruction->IsXor());
6158 GenerateEorConst(out_low, first_low, value_low);
6159 GenerateEorConst(out_high, first_high, value_high);
6160 }
6161 }
6162 return;
6163 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006164
6165 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006166 Register first_reg = first.AsRegister<Register>();
6167 ShifterOperand second_reg(second.AsRegister<Register>());
6168 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006169 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006170 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006171 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006172 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006173 } else {
6174 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006175 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006176 }
6177 } else {
6178 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006179 Register first_low = first.AsRegisterPairLow<Register>();
6180 Register first_high = first.AsRegisterPairHigh<Register>();
6181 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6182 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6183 Register out_low = out.AsRegisterPairLow<Register>();
6184 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006185 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006186 __ and_(out_low, first_low, second_low);
6187 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006188 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006189 __ orr(out_low, first_low, second_low);
6190 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006191 } else {
6192 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006193 __ eor(out_low, first_low, second_low);
6194 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006195 }
6196 }
6197}
6198
Roland Levillainc9285912015-12-18 10:38:42 +00006199void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6200 Location out,
6201 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006202 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006203 Register out_reg = out.AsRegister<Register>();
6204 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006205 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006206 if (kUseBakerReadBarrier) {
6207 // Load with fast path based Baker's read barrier.
6208 // /* HeapReference<Object> */ out = *(out + offset)
6209 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006210 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006211 } else {
6212 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006213 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006214 // in the following move operation, as we will need it for the
6215 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006216 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006217 // /* HeapReference<Object> */ out = *(out + offset)
6218 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006219 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006220 }
6221 } else {
6222 // Plain load with no read barrier.
6223 // /* HeapReference<Object> */ out = *(out + offset)
6224 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6225 __ MaybeUnpoisonHeapReference(out_reg);
6226 }
6227}
6228
6229void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6230 Location out,
6231 Location obj,
6232 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006233 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006234 Register out_reg = out.AsRegister<Register>();
6235 Register obj_reg = obj.AsRegister<Register>();
6236 if (kEmitCompilerReadBarrier) {
6237 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006238 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006239 // Load with fast path based Baker's read barrier.
6240 // /* HeapReference<Object> */ out = *(obj + offset)
6241 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006242 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006243 } else {
6244 // Load with slow path based read barrier.
6245 // /* HeapReference<Object> */ out = *(obj + offset)
6246 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6247 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6248 }
6249 } else {
6250 // Plain load with no read barrier.
6251 // /* HeapReference<Object> */ out = *(obj + offset)
6252 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6253 __ MaybeUnpoisonHeapReference(out_reg);
6254 }
6255}
6256
6257void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6258 Location root,
6259 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006260 uint32_t offset,
6261 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006262 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006263 if (requires_read_barrier) {
6264 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006265 if (kUseBakerReadBarrier) {
6266 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6267 // Baker's read barrier are used:
6268 //
6269 // root = obj.field;
6270 // if (Thread::Current()->GetIsGcMarking()) {
6271 // root = ReadBarrier::Mark(root)
6272 // }
6273
6274 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6275 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6276 static_assert(
6277 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6278 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6279 "have different sizes.");
6280 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6281 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6282 "have different sizes.");
6283
Vladimir Marko953437b2016-08-24 08:30:46 +00006284 // Slow path marking the GC root `root`.
Roland Levillainc9285912015-12-18 10:38:42 +00006285 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006286 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006287 codegen_->AddSlowPath(slow_path);
6288
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006289 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006290 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006291 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006292 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6293 __ Bind(slow_path->GetExitLabel());
6294 } else {
6295 // GC root loaded through a slow path for read barriers other
6296 // than Baker's.
6297 // /* GcRoot<mirror::Object>* */ root = obj + offset
6298 __ AddConstant(root_reg, obj, offset);
6299 // /* mirror::Object* */ root = root->Read()
6300 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6301 }
6302 } else {
6303 // Plain GC root load with no read barrier.
6304 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6305 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6306 // Note that GC roots are not affected by heap poisoning, thus we
6307 // do not have to unpoison `root_reg` here.
6308 }
6309}
6310
6311void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6312 Location ref,
6313 Register obj,
6314 uint32_t offset,
6315 Location temp,
6316 bool needs_null_check) {
6317 DCHECK(kEmitCompilerReadBarrier);
6318 DCHECK(kUseBakerReadBarrier);
6319
6320 // /* HeapReference<Object> */ ref = *(obj + offset)
6321 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006322 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006323 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006324 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006325}
6326
6327void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6328 Location ref,
6329 Register obj,
6330 uint32_t data_offset,
6331 Location index,
6332 Location temp,
6333 bool needs_null_check) {
6334 DCHECK(kEmitCompilerReadBarrier);
6335 DCHECK(kUseBakerReadBarrier);
6336
Roland Levillainbfea3352016-06-23 13:48:47 +01006337 static_assert(
6338 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6339 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006340 // /* HeapReference<Object> */ ref =
6341 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006342 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006343 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006344 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006345}
6346
6347void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6348 Location ref,
6349 Register obj,
6350 uint32_t offset,
6351 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006352 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006353 Location temp,
6354 bool needs_null_check) {
6355 DCHECK(kEmitCompilerReadBarrier);
6356 DCHECK(kUseBakerReadBarrier);
6357
6358 // In slow path based read barriers, the read barrier call is
6359 // inserted after the original load. However, in fast path based
6360 // Baker's read barriers, we need to perform the load of
6361 // mirror::Object::monitor_ *before* the original reference load.
6362 // This load-load ordering is required by the read barrier.
6363 // The fast path/slow path (for Baker's algorithm) should look like:
6364 //
6365 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6366 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6367 // HeapReference<Object> ref = *src; // Original reference load.
6368 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6369 // if (is_gray) {
6370 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6371 // }
6372 //
6373 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006374 // slightly more complex as it performs additional checks that we do
6375 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006376
6377 Register ref_reg = ref.AsRegister<Register>();
6378 Register temp_reg = temp.AsRegister<Register>();
6379 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6380
6381 // /* int32_t */ monitor = obj->monitor_
6382 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6383 if (needs_null_check) {
6384 MaybeRecordImplicitNullCheck(instruction);
6385 }
6386 // /* LockWord */ lock_word = LockWord(monitor)
6387 static_assert(sizeof(LockWord) == sizeof(int32_t),
6388 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006389
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006390 // Introduce a dependency on the lock_word including the rb_state,
6391 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006392 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006393 // `obj` is unchanged by this operation, but its value now depends
6394 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006395 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006396
6397 // The actual reference load.
6398 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006399 // Load types involving an "index": ArrayGet and
6400 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6401 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006402 if (index.IsConstant()) {
6403 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006404 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006405 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6406 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006407 // Handle the special case of the
6408 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6409 // a register pair as index ("long offset"), of which only the low
6410 // part contains data.
6411 Register index_reg = index.IsRegisterPair()
6412 ? index.AsRegisterPairLow<Register>()
6413 : index.AsRegister<Register>();
6414 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006415 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6416 }
6417 } else {
6418 // /* HeapReference<Object> */ ref = *(obj + offset)
6419 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6420 }
6421
6422 // Object* ref = ref_addr->AsMirrorPtr()
6423 __ MaybeUnpoisonHeapReference(ref_reg);
6424
Vladimir Marko953437b2016-08-24 08:30:46 +00006425 // Slow path marking the object `ref` when it is gray.
Roland Levillainc9285912015-12-18 10:38:42 +00006426 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006427 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006428 AddSlowPath(slow_path);
6429
6430 // if (rb_state == ReadBarrier::gray_ptr_)
6431 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006432 // Given the numeric representation, it's enough to check the low bit of the
6433 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6434 // which can be a 16-bit instruction unlike the TST immediate.
6435 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6436 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6437 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6438 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6439 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006440 __ Bind(slow_path->GetExitLabel());
6441}
6442
6443void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6444 Location out,
6445 Location ref,
6446 Location obj,
6447 uint32_t offset,
6448 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006449 DCHECK(kEmitCompilerReadBarrier);
6450
Roland Levillainc9285912015-12-18 10:38:42 +00006451 // Insert a slow path based read barrier *after* the reference load.
6452 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006453 // If heap poisoning is enabled, the unpoisoning of the loaded
6454 // reference will be carried out by the runtime within the slow
6455 // path.
6456 //
6457 // Note that `ref` currently does not get unpoisoned (when heap
6458 // poisoning is enabled), which is alright as the `ref` argument is
6459 // not used by the artReadBarrierSlow entry point.
6460 //
6461 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6462 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6463 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6464 AddSlowPath(slow_path);
6465
Roland Levillain3b359c72015-11-17 19:35:12 +00006466 __ b(slow_path->GetEntryLabel());
6467 __ Bind(slow_path->GetExitLabel());
6468}
6469
Roland Levillainc9285912015-12-18 10:38:42 +00006470void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6471 Location out,
6472 Location ref,
6473 Location obj,
6474 uint32_t offset,
6475 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006476 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006477 // Baker's read barriers shall be handled by the fast path
6478 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6479 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006480 // If heap poisoning is enabled, unpoisoning will be taken care of
6481 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006482 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006483 } else if (kPoisonHeapReferences) {
6484 __ UnpoisonHeapReference(out.AsRegister<Register>());
6485 }
6486}
6487
Roland Levillainc9285912015-12-18 10:38:42 +00006488void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6489 Location out,
6490 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006491 DCHECK(kEmitCompilerReadBarrier);
6492
Roland Levillainc9285912015-12-18 10:38:42 +00006493 // Insert a slow path based read barrier *after* the GC root load.
6494 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006495 // Note that GC roots are not affected by heap poisoning, so we do
6496 // not need to do anything special for this here.
6497 SlowPathCode* slow_path =
6498 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6499 AddSlowPath(slow_path);
6500
Roland Levillain3b359c72015-11-17 19:35:12 +00006501 __ b(slow_path->GetEntryLabel());
6502 __ Bind(slow_path->GetExitLabel());
6503}
6504
Vladimir Markodc151b22015-10-15 18:02:30 +01006505HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6506 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6507 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006508 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6509 // We disable pc-relative load when there is an irreducible loop, as the optimization
6510 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006511 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6512 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006513 if (GetGraph()->HasIrreducibleLoops() &&
6514 (dispatch_info.method_load_kind ==
6515 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6516 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6517 }
6518
6519 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006520 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6521 if (&outer_dex_file != target_method.dex_file) {
6522 // Calls across dex files are more likely to exceed the available BL range,
6523 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6524 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6525 (desired_dispatch_info.method_load_kind ==
6526 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6527 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6528 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6529 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006530 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006531 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006532 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006533 0u
6534 };
6535 }
6536 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006537 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006538}
6539
Vladimir Markob4536b72015-11-24 13:45:23 +00006540Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6541 Register temp) {
6542 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6543 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6544 if (!invoke->GetLocations()->Intrinsified()) {
6545 return location.AsRegister<Register>();
6546 }
6547 // For intrinsics we allow any location, so it may be on the stack.
6548 if (!location.IsRegister()) {
6549 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6550 return temp;
6551 }
6552 // For register locations, check if the register was saved. If so, get it from the stack.
6553 // Note: There is a chance that the register was saved but not overwritten, so we could
6554 // save one load. However, since this is just an intrinsic slow path we prefer this
6555 // simple and more robust approach rather that trying to determine if that's the case.
6556 SlowPathCode* slow_path = GetCurrentSlowPath();
6557 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6558 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6559 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6560 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6561 return temp;
6562 }
6563 return location.AsRegister<Register>();
6564}
6565
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006566void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006567 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006568 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006569 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6570 // LR = code address from literal pool with link-time patch.
6571 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006572 break;
6573 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6574 // LR = invoke->GetDirectCodePtr();
6575 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006576 break;
6577 default:
6578 break;
6579 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006580
Vladimir Marko58155012015-08-19 12:49:41 +00006581 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6582 switch (invoke->GetMethodLoadKind()) {
6583 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6584 // temp = thread->string_init_entrypoint
6585 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6586 break;
6587 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006588 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006589 break;
6590 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6591 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6592 break;
6593 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6594 __ LoadLiteral(temp.AsRegister<Register>(),
6595 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6596 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006597 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6598 HArmDexCacheArraysBase* base =
6599 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6600 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6601 temp.AsRegister<Register>());
6602 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6603 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6604 break;
6605 }
Vladimir Marko58155012015-08-19 12:49:41 +00006606 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006607 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006608 Register method_reg;
6609 Register reg = temp.AsRegister<Register>();
6610 if (current_method.IsRegister()) {
6611 method_reg = current_method.AsRegister<Register>();
6612 } else {
6613 DCHECK(invoke->GetLocations()->Intrinsified());
6614 DCHECK(!current_method.IsValid());
6615 method_reg = reg;
6616 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6617 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006618 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6619 __ LoadFromOffset(kLoadWord,
6620 reg,
6621 method_reg,
6622 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006623 // temp = temp[index_in_cache];
6624 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6625 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006626 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6627 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006628 }
Vladimir Marko58155012015-08-19 12:49:41 +00006629 }
6630
6631 switch (invoke->GetCodePtrLocation()) {
6632 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6633 __ bl(GetFrameEntryLabel());
6634 break;
6635 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006636 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006637 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006638 // Arbitrarily branch to the BL itself, override at link time.
6639 __ bl(&relative_call_patches_.back().label);
6640 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006641 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6642 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6643 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006644 // LR()
6645 __ blx(LR);
6646 break;
6647 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6648 // LR = callee_method->entry_point_from_quick_compiled_code_
6649 __ LoadFromOffset(
6650 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006651 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006652 // LR()
6653 __ blx(LR);
6654 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006655 }
6656
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006657 DCHECK(!IsLeafMethod());
6658}
6659
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006660void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6661 Register temp = temp_location.AsRegister<Register>();
6662 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6663 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006664
6665 // Use the calling convention instead of the location of the receiver, as
6666 // intrinsics may have put the receiver in a different register. In the intrinsics
6667 // slow path, the arguments have been moved to the right place, so here we are
6668 // guaranteed that the receiver is the first register of the calling convention.
6669 InvokeDexCallingConvention calling_convention;
6670 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006671 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006672 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006673 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006674 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006675 // Instead of simply (possibly) unpoisoning `temp` here, we should
6676 // emit a read barrier for the previous class reference load.
6677 // However this is not required in practice, as this is an
6678 // intermediate/temporary reference and because the current
6679 // concurrent copying collector keeps the from-space memory
6680 // intact/accessible until the end of the marking phase (the
6681 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006682 __ MaybeUnpoisonHeapReference(temp);
6683 // temp = temp->GetMethodAt(method_offset);
6684 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006685 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006686 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6687 // LR = temp->GetEntryPoint();
6688 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6689 // LR();
6690 __ blx(LR);
6691}
6692
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006693CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6694 const DexFile& dex_file, uint32_t string_index) {
6695 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6696}
6697
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006698CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6699 const DexFile& dex_file, uint32_t type_index) {
6700 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6701}
6702
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006703CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6704 const DexFile& dex_file, uint32_t element_offset) {
6705 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6706}
6707
6708CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6709 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6710 patches->emplace_back(dex_file, offset_or_index);
6711 return &patches->back();
6712}
6713
6714Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6715 uint32_t string_index) {
6716 return boot_image_string_patches_.GetOrCreate(
6717 StringReference(&dex_file, string_index),
6718 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6719}
6720
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006721Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6722 uint32_t type_index) {
6723 return boot_image_type_patches_.GetOrCreate(
6724 TypeReference(&dex_file, type_index),
6725 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6726}
6727
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006728Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6729 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6730 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6731 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6732}
6733
6734Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6735 return DeduplicateUint32Literal(address, &uint32_literals_);
6736}
6737
Vladimir Marko58155012015-08-19 12:49:41 +00006738void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6739 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006740 size_t size =
6741 method_patches_.size() +
6742 call_patches_.size() +
6743 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006744 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6745 boot_image_string_patches_.size() +
6746 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006747 boot_image_type_patches_.size() +
6748 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006749 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006750 linker_patches->reserve(size);
6751 for (const auto& entry : method_patches_) {
6752 const MethodReference& target_method = entry.first;
6753 Literal* literal = entry.second;
6754 DCHECK(literal->GetLabel()->IsBound());
6755 uint32_t literal_offset = literal->GetLabel()->Position();
6756 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6757 target_method.dex_file,
6758 target_method.dex_method_index));
6759 }
6760 for (const auto& entry : call_patches_) {
6761 const MethodReference& target_method = entry.first;
6762 Literal* literal = entry.second;
6763 DCHECK(literal->GetLabel()->IsBound());
6764 uint32_t literal_offset = literal->GetLabel()->Position();
6765 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6766 target_method.dex_file,
6767 target_method.dex_method_index));
6768 }
6769 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6770 uint32_t literal_offset = info.label.Position();
6771 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6772 info.target_method.dex_file,
6773 info.target_method.dex_method_index));
6774 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006775 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6776 const DexFile& dex_file = info.target_dex_file;
6777 size_t base_element_offset = info.offset_or_index;
6778 DCHECK(info.add_pc_label.IsBound());
6779 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006780 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006781 DCHECK(info.movw_label.IsBound());
6782 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006783 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6784 &dex_file,
6785 add_pc_offset,
6786 base_element_offset));
6787 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006788 DCHECK(info.movt_label.IsBound());
6789 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006790 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6791 &dex_file,
6792 add_pc_offset,
6793 base_element_offset));
6794 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006795 for (const auto& entry : boot_image_string_patches_) {
6796 const StringReference& target_string = entry.first;
6797 Literal* literal = entry.second;
6798 DCHECK(literal->GetLabel()->IsBound());
6799 uint32_t literal_offset = literal->GetLabel()->Position();
6800 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6801 target_string.dex_file,
6802 target_string.string_index));
6803 }
6804 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6805 const DexFile& dex_file = info.target_dex_file;
6806 uint32_t string_index = info.offset_or_index;
6807 DCHECK(info.add_pc_label.IsBound());
6808 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6809 // Add MOVW patch.
6810 DCHECK(info.movw_label.IsBound());
6811 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6812 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6813 &dex_file,
6814 add_pc_offset,
6815 string_index));
6816 // Add MOVT patch.
6817 DCHECK(info.movt_label.IsBound());
6818 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6819 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6820 &dex_file,
6821 add_pc_offset,
6822 string_index));
6823 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006824 for (const auto& entry : boot_image_type_patches_) {
6825 const TypeReference& target_type = entry.first;
6826 Literal* literal = entry.second;
6827 DCHECK(literal->GetLabel()->IsBound());
6828 uint32_t literal_offset = literal->GetLabel()->Position();
6829 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6830 target_type.dex_file,
6831 target_type.type_index));
6832 }
6833 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6834 const DexFile& dex_file = info.target_dex_file;
6835 uint32_t type_index = info.offset_or_index;
6836 DCHECK(info.add_pc_label.IsBound());
6837 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6838 // Add MOVW patch.
6839 DCHECK(info.movw_label.IsBound());
6840 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6841 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6842 &dex_file,
6843 add_pc_offset,
6844 type_index));
6845 // Add MOVT patch.
6846 DCHECK(info.movt_label.IsBound());
6847 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6848 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6849 &dex_file,
6850 add_pc_offset,
6851 type_index));
6852 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006853 for (const auto& entry : boot_image_address_patches_) {
6854 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6855 Literal* literal = entry.second;
6856 DCHECK(literal->GetLabel()->IsBound());
6857 uint32_t literal_offset = literal->GetLabel()->Position();
6858 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6859 }
6860}
6861
6862Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6863 return map->GetOrCreate(
6864 value,
6865 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006866}
6867
6868Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6869 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006870 return map->GetOrCreate(
6871 target_method,
6872 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006873}
6874
6875Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6876 return DeduplicateMethodLiteral(target_method, &method_patches_);
6877}
6878
6879Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6880 return DeduplicateMethodLiteral(target_method, &call_patches_);
6881}
6882
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006883void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6884 LocationSummary* locations =
6885 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6886 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6887 Location::RequiresRegister());
6888 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6889 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6890 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6891}
6892
6893void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6894 LocationSummary* locations = instr->GetLocations();
6895 Register res = locations->Out().AsRegister<Register>();
6896 Register accumulator =
6897 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6898 Register mul_left =
6899 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6900 Register mul_right =
6901 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6902
6903 if (instr->GetOpKind() == HInstruction::kAdd) {
6904 __ mla(res, mul_left, mul_right, accumulator);
6905 } else {
6906 __ mls(res, mul_left, mul_right, accumulator);
6907 }
6908}
6909
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006910void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006911 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006912 LOG(FATAL) << "Unreachable";
6913}
6914
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006915void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006916 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006917 LOG(FATAL) << "Unreachable";
6918}
6919
Mark Mendellfe57faa2015-09-18 09:26:15 -04006920// Simple implementation of packed switch - generate cascaded compare/jumps.
6921void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6922 LocationSummary* locations =
6923 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6924 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006925 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006926 codegen_->GetAssembler()->IsThumb()) {
6927 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6928 if (switch_instr->GetStartValue() != 0) {
6929 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6930 }
6931 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006932}
6933
6934void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6935 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006936 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006937 LocationSummary* locations = switch_instr->GetLocations();
6938 Register value_reg = locations->InAt(0).AsRegister<Register>();
6939 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6940
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006941 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006942 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006943 Register temp_reg = IP;
6944 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6945 // the immediate, because IP is used as the destination register. For the other
6946 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6947 // and they can be encoded in the instruction without making use of IP register.
6948 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6949
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006950 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006951 // Jump to successors[0] if value == lower_bound.
6952 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6953 int32_t last_index = 0;
6954 for (; num_entries - last_index > 2; last_index += 2) {
6955 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6956 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6957 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6958 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6959 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6960 }
6961 if (num_entries - last_index == 2) {
6962 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006963 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006964 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006965 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006966
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006967 // And the default for any other value.
6968 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6969 __ b(codegen_->GetLabelOf(default_block));
6970 }
6971 } else {
6972 // Create a table lookup.
6973 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6974
6975 // Materialize a pointer to the switch table
6976 std::vector<Label*> labels(num_entries);
6977 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6978 for (uint32_t i = 0; i < num_entries; i++) {
6979 labels[i] = codegen_->GetLabelOf(successors[i]);
6980 }
6981 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6982
6983 // Remove the bias.
6984 Register key_reg;
6985 if (lower_bound != 0) {
6986 key_reg = locations->GetTemp(1).AsRegister<Register>();
6987 __ AddConstant(key_reg, value_reg, -lower_bound);
6988 } else {
6989 key_reg = value_reg;
6990 }
6991
6992 // Check whether the value is in the table, jump to default block if not.
6993 __ CmpConstant(key_reg, num_entries - 1);
6994 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6995
6996 // Load the displacement from the table.
6997 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6998
6999 // Dispatch is a direct add to the PC (for Thumb2).
7000 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007001 }
7002}
7003
Vladimir Markob4536b72015-11-24 13:45:23 +00007004void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7006 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007007}
7008
7009void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7010 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007011 CodeGeneratorARM::PcRelativePatchInfo* labels =
7012 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007013 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007014 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007015 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007016 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007017 __ BindTrackedLabel(&labels->add_pc_label);
7018 __ add(base_reg, base_reg, ShifterOperand(PC));
7019}
7020
Andreas Gampe85b62f22015-09-09 13:15:38 -07007021void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7022 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007023 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007024 return;
7025 }
7026
7027 DCHECK_NE(type, Primitive::kPrimVoid);
7028
7029 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7030 if (return_loc.Equals(trg)) {
7031 return;
7032 }
7033
7034 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7035 // with the last branch.
7036 if (type == Primitive::kPrimLong) {
7037 HParallelMove parallel_move(GetGraph()->GetArena());
7038 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7039 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7040 GetMoveResolver()->EmitNativeCode(&parallel_move);
7041 } else if (type == Primitive::kPrimDouble) {
7042 HParallelMove parallel_move(GetGraph()->GetArena());
7043 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7044 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7045 GetMoveResolver()->EmitNativeCode(&parallel_move);
7046 } else {
7047 // Let the parallel move resolver take care of all of this.
7048 HParallelMove parallel_move(GetGraph()->GetArena());
7049 parallel_move.AddMove(return_loc, trg, type, nullptr);
7050 GetMoveResolver()->EmitNativeCode(&parallel_move);
7051 }
7052}
7053
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007054void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7055 LocationSummary* locations =
7056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7057 locations->SetInAt(0, Location::RequiresRegister());
7058 locations->SetOut(Location::RequiresRegister());
7059}
7060
7061void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7062 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007063 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007064 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007065 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007066 __ LoadFromOffset(kLoadWord,
7067 locations->Out().AsRegister<Register>(),
7068 locations->InAt(0).AsRegister<Register>(),
7069 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007070 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007071 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007072 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007073 __ LoadFromOffset(kLoadWord,
7074 locations->Out().AsRegister<Register>(),
7075 locations->InAt(0).AsRegister<Register>(),
7076 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7077 __ LoadFromOffset(kLoadWord,
7078 locations->Out().AsRegister<Register>(),
7079 locations->Out().AsRegister<Register>(),
7080 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007081 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007082}
7083
Roland Levillain4d027112015-07-01 15:41:14 +01007084#undef __
7085#undef QUICK_ENTRY_POINT
7086
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007087} // namespace arm
7088} // namespace art