blob: 3c4a3e8e188ead0bc8276d401864d10edf2ab254 [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) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004254 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4255 ? LocationSummary::kCallOnSlowPath
4256 : LocationSummary::kNoCall;
4257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004258 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004259 if (instruction->HasUses()) {
4260 locations->SetOut(Location::SameAsFirstInput());
4261 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004262}
4263
Calin Juravle2ae48182016-03-16 14:05:09 +00004264void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4265 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004266 return;
4267 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004268 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004269
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004270 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004271 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004272}
4273
Calin Juravle2ae48182016-03-16 14:05:09 +00004274void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004275 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004276 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277
4278 LocationSummary* locations = instruction->GetLocations();
4279 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004281 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004282}
4283
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004284void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004285 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004286}
4287
Artem Serov6c916792016-07-11 14:02:34 +01004288static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4289 switch (type) {
4290 case Primitive::kPrimNot:
4291 return kLoadWord;
4292 case Primitive::kPrimBoolean:
4293 return kLoadUnsignedByte;
4294 case Primitive::kPrimByte:
4295 return kLoadSignedByte;
4296 case Primitive::kPrimChar:
4297 return kLoadUnsignedHalfword;
4298 case Primitive::kPrimShort:
4299 return kLoadSignedHalfword;
4300 case Primitive::kPrimInt:
4301 return kLoadWord;
4302 case Primitive::kPrimLong:
4303 return kLoadWordPair;
4304 case Primitive::kPrimFloat:
4305 return kLoadSWord;
4306 case Primitive::kPrimDouble:
4307 return kLoadDWord;
4308 default:
4309 LOG(FATAL) << "Unreachable type " << type;
4310 UNREACHABLE();
4311 }
4312}
4313
4314static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4315 switch (type) {
4316 case Primitive::kPrimNot:
4317 return kStoreWord;
4318 case Primitive::kPrimBoolean:
4319 case Primitive::kPrimByte:
4320 return kStoreByte;
4321 case Primitive::kPrimChar:
4322 case Primitive::kPrimShort:
4323 return kStoreHalfword;
4324 case Primitive::kPrimInt:
4325 return kStoreWord;
4326 case Primitive::kPrimLong:
4327 return kStoreWordPair;
4328 case Primitive::kPrimFloat:
4329 return kStoreSWord;
4330 case Primitive::kPrimDouble:
4331 return kStoreDWord;
4332 default:
4333 LOG(FATAL) << "Unreachable type " << type;
4334 UNREACHABLE();
4335 }
4336}
4337
4338void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4339 Location out_loc,
4340 Register base,
4341 Register reg_offset,
4342 Condition cond) {
4343 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4344 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4345
4346 switch (type) {
4347 case Primitive::kPrimByte:
4348 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4349 break;
4350 case Primitive::kPrimBoolean:
4351 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4352 break;
4353 case Primitive::kPrimShort:
4354 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4355 break;
4356 case Primitive::kPrimChar:
4357 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4358 break;
4359 case Primitive::kPrimNot:
4360 case Primitive::kPrimInt:
4361 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4362 break;
4363 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4364 case Primitive::kPrimLong:
4365 case Primitive::kPrimFloat:
4366 case Primitive::kPrimDouble:
4367 default:
4368 LOG(FATAL) << "Unreachable type " << type;
4369 UNREACHABLE();
4370 }
4371}
4372
4373void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4374 Location loc,
4375 Register base,
4376 Register reg_offset,
4377 Condition cond) {
4378 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4379 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4380
4381 switch (type) {
4382 case Primitive::kPrimByte:
4383 case Primitive::kPrimBoolean:
4384 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4385 break;
4386 case Primitive::kPrimShort:
4387 case Primitive::kPrimChar:
4388 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4389 break;
4390 case Primitive::kPrimNot:
4391 case Primitive::kPrimInt:
4392 __ str(loc.AsRegister<Register>(), mem_address, cond);
4393 break;
4394 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4395 case Primitive::kPrimLong:
4396 case Primitive::kPrimFloat:
4397 case Primitive::kPrimDouble:
4398 default:
4399 LOG(FATAL) << "Unreachable type " << type;
4400 UNREACHABLE();
4401 }
4402}
4403
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004404void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004405 bool object_array_get_with_read_barrier =
4406 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004407 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004408 new (GetGraph()->GetArena()) LocationSummary(instruction,
4409 object_array_get_with_read_barrier ?
4410 LocationSummary::kCallOnSlowPath :
4411 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004412 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4413 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4414 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004415 locations->SetInAt(0, Location::RequiresRegister());
4416 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004417 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4418 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4419 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004420 // The output overlaps in the case of an object array get with
4421 // read barriers enabled: we do not want the move to overwrite the
4422 // array's location, as we need it to emit the read barrier.
4423 locations->SetOut(
4424 Location::RequiresRegister(),
4425 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004426 }
Roland Levillainc9285912015-12-18 10:38:42 +00004427 // We need a temporary register for the read barrier marking slow
4428 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4429 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4430 locations->AddTemp(Location::RequiresRegister());
4431 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432}
4433
4434void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4435 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004436 Location obj_loc = locations->InAt(0);
4437 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004438 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004439 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004440 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004441 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004442 HInstruction* array_instr = instruction->GetArray();
4443 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004444 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4445 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004446
Roland Levillain4d027112015-07-01 15:41:14 +01004447 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004448 case Primitive::kPrimBoolean:
4449 case Primitive::kPrimByte:
4450 case Primitive::kPrimShort:
4451 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004452 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004453 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004454 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4455 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4456
4457 LoadOperandType load_type = GetLoadOperandType(type);
4458 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004459 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004460 Register temp = IP;
4461
4462 if (has_intermediate_address) {
4463 // We do not need to compute the intermediate address from the array: the
4464 // input instruction has done it already. See the comment in
4465 // `TryExtractArrayAccessAddress()`.
4466 if (kIsDebugBuild) {
4467 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4468 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4469 }
4470 temp = obj;
4471 } else {
4472 __ add(temp, obj, ShifterOperand(data_offset));
4473 }
4474 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004475 }
4476 break;
4477 }
4478
Roland Levillainc9285912015-12-18 10:38:42 +00004479 case Primitive::kPrimNot: {
4480 static_assert(
4481 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4482 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004483 // /* HeapReference<Object> */ out =
4484 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4485 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4486 Location temp = locations->GetTemp(0);
4487 // Note that a potential implicit null check is handled in this
4488 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4489 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4490 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4491 } else {
4492 Register out = out_loc.AsRegister<Register>();
4493 if (index.IsConstant()) {
4494 size_t offset =
4495 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4496 __ LoadFromOffset(kLoadWord, out, obj, offset);
4497 codegen_->MaybeRecordImplicitNullCheck(instruction);
4498 // If read barriers are enabled, emit read barriers other than
4499 // Baker's using a slow path (and also unpoison the loaded
4500 // reference, if heap poisoning is enabled).
4501 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4502 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004503 Register temp = IP;
4504
4505 if (has_intermediate_address) {
4506 // We do not need to compute the intermediate address from the array: the
4507 // input instruction has done it already. See the comment in
4508 // `TryExtractArrayAccessAddress()`.
4509 if (kIsDebugBuild) {
4510 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4511 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4512 }
4513 temp = obj;
4514 } else {
4515 __ add(temp, obj, ShifterOperand(data_offset));
4516 }
4517 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004518
Roland Levillainc9285912015-12-18 10:38:42 +00004519 codegen_->MaybeRecordImplicitNullCheck(instruction);
4520 // If read barriers are enabled, emit read barriers other than
4521 // Baker's using a slow path (and also unpoison the loaded
4522 // reference, if heap poisoning is enabled).
4523 codegen_->MaybeGenerateReadBarrierSlow(
4524 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4525 }
4526 }
4527 break;
4528 }
4529
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004530 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004531 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004532 size_t offset =
4533 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004534 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004536 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004537 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 }
4539 break;
4540 }
4541
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004542 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004543 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004544 if (index.IsConstant()) {
4545 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004546 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004547 } else {
4548 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004549 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004550 }
4551 break;
4552 }
4553
4554 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004555 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004556 if (index.IsConstant()) {
4557 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004558 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004559 } else {
4560 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004561 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004562 }
4563 break;
4564 }
4565
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004566 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004567 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004568 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569 }
Roland Levillain4d027112015-07-01 15:41:14 +01004570
4571 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004572 // Potential implicit null checks, in the case of reference
4573 // arrays, are handled in the previous switch statement.
4574 } else {
4575 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004576 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577}
4578
4579void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004580 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004581
4582 bool needs_write_barrier =
4583 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004584 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004585
Nicolas Geoffray39468442014-09-02 15:17:15 +01004586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004587 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004588 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004589 LocationSummary::kCallOnSlowPath :
4590 LocationSummary::kNoCall);
4591
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004592 locations->SetInAt(0, Location::RequiresRegister());
4593 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4594 if (Primitive::IsFloatingPointType(value_type)) {
4595 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004596 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004597 locations->SetInAt(2, Location::RequiresRegister());
4598 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004599 if (needs_write_barrier) {
4600 // Temporary registers for the write barrier.
4601 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004602 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604}
4605
4606void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4607 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004608 Location array_loc = locations->InAt(0);
4609 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004611 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004612 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004613 bool needs_write_barrier =
4614 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004615 uint32_t data_offset =
4616 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4617 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004618 HInstruction* array_instr = instruction->GetArray();
4619 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004620 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4621 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622
4623 switch (value_type) {
4624 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004625 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004627 case Primitive::kPrimChar:
4628 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004630 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4631 uint32_t full_offset =
4632 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4633 StoreOperandType store_type = GetStoreOperandType(value_type);
4634 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004636 Register temp = IP;
4637
4638 if (has_intermediate_address) {
4639 // We do not need to compute the intermediate address from the array: the
4640 // input instruction has done it already. See the comment in
4641 // `TryExtractArrayAccessAddress()`.
4642 if (kIsDebugBuild) {
4643 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4644 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4645 }
4646 temp = array;
4647 } else {
4648 __ add(temp, array, ShifterOperand(data_offset));
4649 }
Artem Serov6c916792016-07-11 14:02:34 +01004650 codegen_->StoreToShiftedRegOffset(value_type,
4651 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004652 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004653 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004654 }
4655 break;
4656 }
4657
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004659 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004660 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4661 // See the comment in instruction_simplifier_shared.cc.
4662 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004663
4664 if (instruction->InputAt(2)->IsNullConstant()) {
4665 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004666 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004667 size_t offset =
4668 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004669 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004670 } else {
4671 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004672 __ add(IP, array, ShifterOperand(data_offset));
4673 codegen_->StoreToShiftedRegOffset(value_type,
4674 value_loc,
4675 IP,
4676 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004677 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004678 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004679 DCHECK(!needs_write_barrier);
4680 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004681 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004682 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004683
4684 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01004685 Location temp1_loc = locations->GetTemp(0);
4686 Register temp1 = temp1_loc.AsRegister<Register>();
4687 Location temp2_loc = locations->GetTemp(1);
4688 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004689 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4690 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4691 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4692 Label done;
4693 SlowPathCode* slow_path = nullptr;
4694
Roland Levillain3b359c72015-11-17 19:35:12 +00004695 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004696 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4697 codegen_->AddSlowPath(slow_path);
4698 if (instruction->GetValueCanBeNull()) {
4699 Label non_zero;
4700 __ CompareAndBranchIfNonZero(value, &non_zero);
4701 if (index.IsConstant()) {
4702 size_t offset =
4703 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4704 __ StoreToOffset(kStoreWord, value, array, offset);
4705 } else {
4706 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004707 __ add(IP, array, ShifterOperand(data_offset));
4708 codegen_->StoreToShiftedRegOffset(value_type,
4709 value_loc,
4710 IP,
4711 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004712 }
4713 codegen_->MaybeRecordImplicitNullCheck(instruction);
4714 __ b(&done);
4715 __ Bind(&non_zero);
4716 }
4717
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004718 // Note that when read barriers are enabled, the type checks
4719 // are performed without read barriers. This is fine, even in
4720 // the case where a class object is in the from-space after
4721 // the flip, as a comparison involving such a type would not
4722 // produce a false positive; it may of course produce a false
4723 // negative, in which case we would take the ArraySet slow
4724 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004725
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004726 // /* HeapReference<Class> */ temp1 = array->klass_
4727 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4728 codegen_->MaybeRecordImplicitNullCheck(instruction);
4729 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01004730
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004731 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4732 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4733 // /* HeapReference<Class> */ temp2 = value->klass_
4734 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4735 // If heap poisoning is enabled, no need to unpoison `temp1`
4736 // nor `temp2`, as we are comparing two poisoned references.
4737 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01004738
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004739 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4740 Label do_put;
4741 __ b(&do_put, EQ);
4742 // If heap poisoning is enabled, the `temp1` reference has
4743 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00004744 __ MaybeUnpoisonHeapReference(temp1);
4745
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004746 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4747 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4748 // If heap poisoning is enabled, no need to unpoison
4749 // `temp1`, as we are comparing against null below.
4750 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4751 __ Bind(&do_put);
4752 } else {
4753 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004754 }
4755 }
4756
Artem Serov6c916792016-07-11 14:02:34 +01004757 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004758 if (kPoisonHeapReferences) {
4759 // Note that in the case where `value` is a null reference,
4760 // we do not enter this block, as a null reference does not
4761 // need poisoning.
4762 DCHECK_EQ(value_type, Primitive::kPrimNot);
4763 __ Mov(temp1, value);
4764 __ PoisonHeapReference(temp1);
4765 source = temp1;
4766 }
4767
4768 if (index.IsConstant()) {
4769 size_t offset =
4770 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4771 __ StoreToOffset(kStoreWord, source, array, offset);
4772 } else {
4773 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004774
4775 __ add(IP, array, ShifterOperand(data_offset));
4776 codegen_->StoreToShiftedRegOffset(value_type,
4777 Location::RegisterLocation(source),
4778 IP,
4779 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004780 }
4781
Roland Levillain3b359c72015-11-17 19:35:12 +00004782 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004783 codegen_->MaybeRecordImplicitNullCheck(instruction);
4784 }
4785
4786 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4787
4788 if (done.IsLinked()) {
4789 __ Bind(&done);
4790 }
4791
4792 if (slow_path != nullptr) {
4793 __ Bind(slow_path->GetExitLabel());
4794 }
4795
4796 break;
4797 }
4798
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004799 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004800 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004802 size_t offset =
4803 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004804 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004805 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004806 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004807 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004808 }
4809 break;
4810 }
4811
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004812 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004813 Location value = locations->InAt(2);
4814 DCHECK(value.IsFpuRegister());
4815 if (index.IsConstant()) {
4816 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004817 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004818 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004819 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004820 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4821 }
4822 break;
4823 }
4824
4825 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004826 Location value = locations->InAt(2);
4827 DCHECK(value.IsFpuRegisterPair());
4828 if (index.IsConstant()) {
4829 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004830 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004831 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004833 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4834 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004835
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004836 break;
4837 }
4838
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004839 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004840 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004841 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004843
Roland Levillain80e67092016-01-08 16:04:55 +00004844 // Objects are handled in the switch.
4845 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004846 codegen_->MaybeRecordImplicitNullCheck(instruction);
4847 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848}
4849
4850void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004851 LocationSummary* locations =
4852 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004853 locations->SetInAt(0, Location::RequiresRegister());
4854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004855}
4856
4857void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4858 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004859 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004860 Register obj = locations->InAt(0).AsRegister<Register>();
4861 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004863 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864}
4865
Artem Serov328429f2016-07-06 16:23:04 +01004866void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00004867 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4868 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01004869 LocationSummary* locations =
4870 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4871
4872 locations->SetInAt(0, Location::RequiresRegister());
4873 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4874 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4875}
4876
4877void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4878 LocationSummary* locations = instruction->GetLocations();
4879 Location out = locations->Out();
4880 Location first = locations->InAt(0);
4881 Location second = locations->InAt(1);
4882
Roland Levillain4a3aa572016-08-15 13:17:06 +00004883 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4884 DCHECK(!kEmitCompilerReadBarrier);
4885
Artem Serov328429f2016-07-06 16:23:04 +01004886 if (second.IsRegister()) {
4887 __ add(out.AsRegister<Register>(),
4888 first.AsRegister<Register>(),
4889 ShifterOperand(second.AsRegister<Register>()));
4890 } else {
4891 __ AddConstant(out.AsRegister<Register>(),
4892 first.AsRegister<Register>(),
4893 second.GetConstant()->AsIntConstant()->GetValue());
4894 }
4895}
4896
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004897void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004898 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4899 ? LocationSummary::kCallOnSlowPath
4900 : LocationSummary::kNoCall;
4901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004902 locations->SetInAt(0, Location::RequiresRegister());
4903 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004904 if (instruction->HasUses()) {
4905 locations->SetOut(Location::SameAsFirstInput());
4906 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004907}
4908
4909void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4910 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004911 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004912 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004913 codegen_->AddSlowPath(slow_path);
4914
Roland Levillain271ab9c2014-11-27 15:23:57 +00004915 Register index = locations->InAt(0).AsRegister<Register>();
4916 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004917
4918 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004919 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004920}
4921
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004922void CodeGeneratorARM::MarkGCCard(Register temp,
4923 Register card,
4924 Register object,
4925 Register value,
4926 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004927 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004928 if (can_be_null) {
4929 __ CompareAndBranchIfZero(value, &is_null);
4930 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004931 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004932 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4933 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004934 if (can_be_null) {
4935 __ Bind(&is_null);
4936 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004937}
4938
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004939void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004940 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004941}
4942
4943void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004944 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4945}
4946
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004947void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004948 LocationSummary* locations =
4949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4950 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004951}
4952
4953void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004954 HBasicBlock* block = instruction->GetBlock();
4955 if (block->GetLoopInformation() != nullptr) {
4956 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4957 // The back edge will generate the suspend check.
4958 return;
4959 }
4960 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4961 // The goto will generate the suspend check.
4962 return;
4963 }
4964 GenerateSuspendCheck(instruction, nullptr);
4965}
4966
4967void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4968 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004969 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004970 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4971 if (slow_path == nullptr) {
4972 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4973 instruction->SetSlowPath(slow_path);
4974 codegen_->AddSlowPath(slow_path);
4975 if (successor != nullptr) {
4976 DCHECK(successor->IsLoopHeader());
4977 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4978 }
4979 } else {
4980 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4981 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004982
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004983 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004984 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004985 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004986 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004987 __ Bind(slow_path->GetReturnLabel());
4988 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004989 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004990 __ b(slow_path->GetEntryLabel());
4991 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004992}
4993
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004994ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4995 return codegen_->GetAssembler();
4996}
4997
4998void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004999 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005000 Location source = move->GetSource();
5001 Location destination = move->GetDestination();
5002
5003 if (source.IsRegister()) {
5004 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005005 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005006 } else if (destination.IsFpuRegister()) {
5007 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005008 } else {
5009 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005010 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005011 SP, destination.GetStackIndex());
5012 }
5013 } else if (source.IsStackSlot()) {
5014 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005015 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005016 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005017 } else if (destination.IsFpuRegister()) {
5018 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005019 } else {
5020 DCHECK(destination.IsStackSlot());
5021 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5022 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5023 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005024 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005025 if (destination.IsRegister()) {
5026 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5027 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005028 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005029 } else {
5030 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005031 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5032 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005033 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005034 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005035 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5036 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005037 } else if (destination.IsRegisterPair()) {
5038 DCHECK(ExpectedPairLayout(destination));
5039 __ LoadFromOffset(
5040 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5041 } else {
5042 DCHECK(destination.IsFpuRegisterPair()) << destination;
5043 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5044 SP,
5045 source.GetStackIndex());
5046 }
5047 } else if (source.IsRegisterPair()) {
5048 if (destination.IsRegisterPair()) {
5049 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5050 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005051 } else if (destination.IsFpuRegisterPair()) {
5052 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5053 source.AsRegisterPairLow<Register>(),
5054 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005055 } else {
5056 DCHECK(destination.IsDoubleStackSlot()) << destination;
5057 DCHECK(ExpectedPairLayout(source));
5058 __ StoreToOffset(
5059 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5060 }
5061 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005062 if (destination.IsRegisterPair()) {
5063 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5064 destination.AsRegisterPairHigh<Register>(),
5065 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5066 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005067 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5068 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5069 } else {
5070 DCHECK(destination.IsDoubleStackSlot()) << destination;
5071 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5072 SP,
5073 destination.GetStackIndex());
5074 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005075 } else {
5076 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005077 HConstant* constant = source.GetConstant();
5078 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5079 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005080 if (destination.IsRegister()) {
5081 __ LoadImmediate(destination.AsRegister<Register>(), value);
5082 } else {
5083 DCHECK(destination.IsStackSlot());
5084 __ LoadImmediate(IP, value);
5085 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5086 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005087 } else if (constant->IsLongConstant()) {
5088 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005089 if (destination.IsRegisterPair()) {
5090 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5091 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005092 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005093 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005094 __ LoadImmediate(IP, Low32Bits(value));
5095 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5096 __ LoadImmediate(IP, High32Bits(value));
5097 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5098 }
5099 } else if (constant->IsDoubleConstant()) {
5100 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005101 if (destination.IsFpuRegisterPair()) {
5102 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005103 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005104 DCHECK(destination.IsDoubleStackSlot()) << destination;
5105 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005106 __ LoadImmediate(IP, Low32Bits(int_value));
5107 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5108 __ LoadImmediate(IP, High32Bits(int_value));
5109 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5110 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005111 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005112 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005113 float value = constant->AsFloatConstant()->GetValue();
5114 if (destination.IsFpuRegister()) {
5115 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5116 } else {
5117 DCHECK(destination.IsStackSlot());
5118 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5119 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5120 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005121 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005122 }
5123}
5124
5125void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5126 __ Mov(IP, reg);
5127 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5128 __ StoreToOffset(kStoreWord, IP, SP, mem);
5129}
5130
5131void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5132 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5133 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5134 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5135 SP, mem1 + stack_offset);
5136 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5137 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5138 SP, mem2 + stack_offset);
5139 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5140}
5141
5142void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005143 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005144 Location source = move->GetSource();
5145 Location destination = move->GetDestination();
5146
5147 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005148 DCHECK_NE(source.AsRegister<Register>(), IP);
5149 DCHECK_NE(destination.AsRegister<Register>(), IP);
5150 __ Mov(IP, source.AsRegister<Register>());
5151 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5152 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005153 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005154 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005155 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005156 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005157 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5158 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005159 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005160 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005161 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005162 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005163 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005164 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005165 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005166 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005167 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5168 destination.AsRegisterPairHigh<Register>(),
5169 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005170 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005171 Register low_reg = source.IsRegisterPair()
5172 ? source.AsRegisterPairLow<Register>()
5173 : destination.AsRegisterPairLow<Register>();
5174 int mem = source.IsRegisterPair()
5175 ? destination.GetStackIndex()
5176 : source.GetStackIndex();
5177 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005178 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005179 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005180 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005181 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005182 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5183 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005184 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005185 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005186 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005187 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5188 DRegister reg = source.IsFpuRegisterPair()
5189 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5190 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5191 int mem = source.IsFpuRegisterPair()
5192 ? destination.GetStackIndex()
5193 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005194 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005195 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005196 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005197 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5198 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5199 : destination.AsFpuRegister<SRegister>();
5200 int mem = source.IsFpuRegister()
5201 ? destination.GetStackIndex()
5202 : source.GetStackIndex();
5203
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005204 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005205 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005206 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005207 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005208 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5209 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005210 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005211 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005212 }
5213}
5214
5215void ParallelMoveResolverARM::SpillScratch(int reg) {
5216 __ Push(static_cast<Register>(reg));
5217}
5218
5219void ParallelMoveResolverARM::RestoreScratch(int reg) {
5220 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005221}
5222
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005223HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5224 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005225 switch (desired_class_load_kind) {
5226 case HLoadClass::LoadKind::kReferrersClass:
5227 break;
5228 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5229 DCHECK(!GetCompilerOptions().GetCompilePic());
5230 break;
5231 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5232 DCHECK(GetCompilerOptions().GetCompilePic());
5233 break;
5234 case HLoadClass::LoadKind::kBootImageAddress:
5235 break;
5236 case HLoadClass::LoadKind::kDexCacheAddress:
5237 DCHECK(Runtime::Current()->UseJitCompilation());
5238 break;
5239 case HLoadClass::LoadKind::kDexCachePcRelative:
5240 DCHECK(!Runtime::Current()->UseJitCompilation());
5241 // We disable pc-relative load when there is an irreducible loop, as the optimization
5242 // is incompatible with it.
5243 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5244 // with irreducible loops.
5245 if (GetGraph()->HasIrreducibleLoops()) {
5246 return HLoadClass::LoadKind::kDexCacheViaMethod;
5247 }
5248 break;
5249 case HLoadClass::LoadKind::kDexCacheViaMethod:
5250 break;
5251 }
5252 return desired_class_load_kind;
5253}
5254
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005255void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005256 if (cls->NeedsAccessCheck()) {
5257 InvokeRuntimeCallingConvention calling_convention;
5258 CodeGenerator::CreateLoadClassLocationSummary(
5259 cls,
5260 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5261 Location::RegisterLocation(R0),
5262 /* code_generator_supports_read_barrier */ true);
5263 return;
5264 }
5265
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005266 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5267 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005268 ? LocationSummary::kCallOnSlowPath
5269 : LocationSummary::kNoCall;
5270 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005271 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005272 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5273 }
5274
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005275 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5276 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5277 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5278 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5279 locations->SetInAt(0, Location::RequiresRegister());
5280 }
5281 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005282}
5283
5284void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005285 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005286 if (cls->NeedsAccessCheck()) {
5287 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005288 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005289 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005290 return;
5291 }
5292
Roland Levillain3b359c72015-11-17 19:35:12 +00005293 Location out_loc = locations->Out();
5294 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005295
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005296 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005297 bool generate_null_check = false;
5298 switch (cls->GetLoadKind()) {
5299 case HLoadClass::LoadKind::kReferrersClass: {
5300 DCHECK(!cls->CanCallRuntime());
5301 DCHECK(!cls->MustGenerateClinitCheck());
5302 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5303 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005304 GenerateGcRootFieldLoad(cls,
5305 out_loc,
5306 current_method,
5307 ArtMethod::DeclaringClassOffset().Int32Value(),
5308 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005309 break;
5310 }
5311 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005312 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005313 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5314 cls->GetTypeIndex()));
5315 break;
5316 }
5317 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005318 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005319 CodeGeneratorARM::PcRelativePatchInfo* labels =
5320 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5321 __ BindTrackedLabel(&labels->movw_label);
5322 __ movw(out, /* placeholder */ 0u);
5323 __ BindTrackedLabel(&labels->movt_label);
5324 __ movt(out, /* placeholder */ 0u);
5325 __ BindTrackedLabel(&labels->add_pc_label);
5326 __ add(out, out, ShifterOperand(PC));
5327 break;
5328 }
5329 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005330 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005331 DCHECK_NE(cls->GetAddress(), 0u);
5332 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5333 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5334 break;
5335 }
5336 case HLoadClass::LoadKind::kDexCacheAddress: {
5337 DCHECK_NE(cls->GetAddress(), 0u);
5338 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5339 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5340 // a 128B range. To try and reduce the number of literals if we load multiple types,
5341 // simply split the dex cache address to a 128B aligned base loaded from a literal
5342 // and the remaining offset embedded in the load.
5343 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5344 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5345 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5346 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5347 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5348 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5349 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005350 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005351 generate_null_check = !cls->IsInDexCache();
5352 break;
5353 }
5354 case HLoadClass::LoadKind::kDexCachePcRelative: {
5355 Register base_reg = locations->InAt(0).AsRegister<Register>();
5356 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5357 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5358 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005359 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005360 generate_null_check = !cls->IsInDexCache();
5361 break;
5362 }
5363 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5364 // /* GcRoot<mirror::Class>[] */ out =
5365 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5366 Register current_method = locations->InAt(0).AsRegister<Register>();
5367 __ LoadFromOffset(kLoadWord,
5368 out,
5369 current_method,
5370 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5371 // /* GcRoot<mirror::Class> */ out = out[type_index]
5372 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005373 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005374 generate_null_check = !cls->IsInDexCache();
5375 }
5376 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005377
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005378 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5379 DCHECK(cls->CanCallRuntime());
5380 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5381 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5382 codegen_->AddSlowPath(slow_path);
5383 if (generate_null_check) {
5384 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5385 }
5386 if (cls->MustGenerateClinitCheck()) {
5387 GenerateClassInitializationCheck(slow_path, out);
5388 } else {
5389 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005390 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005391 }
5392}
5393
5394void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5395 LocationSummary* locations =
5396 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5397 locations->SetInAt(0, Location::RequiresRegister());
5398 if (check->HasUses()) {
5399 locations->SetOut(Location::SameAsFirstInput());
5400 }
5401}
5402
5403void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005404 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005405 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005406 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005407 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005408 GenerateClassInitializationCheck(slow_path,
5409 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005410}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005411
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005412void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005413 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005414 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5415 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5416 __ b(slow_path->GetEntryLabel(), LT);
5417 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5418 // properly. Therefore, we do a memory fence.
5419 __ dmb(ISH);
5420 __ Bind(slow_path->GetExitLabel());
5421}
5422
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005423HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5424 HLoadString::LoadKind desired_string_load_kind) {
5425 if (kEmitCompilerReadBarrier) {
5426 switch (desired_string_load_kind) {
5427 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5428 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5429 case HLoadString::LoadKind::kBootImageAddress:
5430 // TODO: Implement for read barrier.
5431 return HLoadString::LoadKind::kDexCacheViaMethod;
5432 default:
5433 break;
5434 }
5435 }
5436 switch (desired_string_load_kind) {
5437 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5438 DCHECK(!GetCompilerOptions().GetCompilePic());
5439 break;
5440 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5441 DCHECK(GetCompilerOptions().GetCompilePic());
5442 break;
5443 case HLoadString::LoadKind::kBootImageAddress:
5444 break;
5445 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005446 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005447 break;
5448 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005449 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005450 // We disable pc-relative load when there is an irreducible loop, as the optimization
5451 // is incompatible with it.
5452 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5453 // with irreducible loops.
5454 if (GetGraph()->HasIrreducibleLoops()) {
5455 return HLoadString::LoadKind::kDexCacheViaMethod;
5456 }
5457 break;
5458 case HLoadString::LoadKind::kDexCacheViaMethod:
5459 break;
5460 }
5461 return desired_string_load_kind;
5462}
5463
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005464void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005465 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5466 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005467 : LocationSummary::kNoCall;
5468 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005469
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005470 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005471 DCHECK(load_kind != HLoadString::LoadKind::kDexCachePcRelative) << "Not supported";
5472 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005473 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005474 locations->SetOut(Location::RegisterLocation(R0));
5475 } else {
5476 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005478}
5479
5480void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005481 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005482 Location out_loc = locations->Out();
5483 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005484 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005485
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005486 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005487 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5488 DCHECK(!kEmitCompilerReadBarrier);
5489 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5490 load->GetStringIndex()));
5491 return; // No dex cache slow path.
5492 }
5493 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5494 DCHECK(!kEmitCompilerReadBarrier);
5495 CodeGeneratorARM::PcRelativePatchInfo* labels =
5496 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5497 __ BindTrackedLabel(&labels->movw_label);
5498 __ movw(out, /* placeholder */ 0u);
5499 __ BindTrackedLabel(&labels->movt_label);
5500 __ movt(out, /* placeholder */ 0u);
5501 __ BindTrackedLabel(&labels->add_pc_label);
5502 __ add(out, out, ShifterOperand(PC));
5503 return; // No dex cache slow path.
5504 }
5505 case HLoadString::LoadKind::kBootImageAddress: {
5506 DCHECK(!kEmitCompilerReadBarrier);
5507 DCHECK_NE(load->GetAddress(), 0u);
5508 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5509 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5510 return; // No dex cache slow path.
5511 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005512 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005513 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005514 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005515
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005516 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5517 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5518 InvokeRuntimeCallingConvention calling_convention;
5519 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5520 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5521 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005522}
5523
David Brazdilcb1c0552015-08-04 16:22:25 +01005524static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005525 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005526}
5527
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005528void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5529 LocationSummary* locations =
5530 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5531 locations->SetOut(Location::RequiresRegister());
5532}
5533
5534void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005535 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005536 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5537}
5538
5539void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5540 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5541}
5542
5543void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005544 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005545 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005546}
5547
5548void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5549 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005550 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005551 InvokeRuntimeCallingConvention calling_convention;
5552 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5553}
5554
5555void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005556 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005557 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005558}
5559
Roland Levillainc9285912015-12-18 10:38:42 +00005560static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5561 return kEmitCompilerReadBarrier &&
5562 (kUseBakerReadBarrier ||
5563 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5564 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5565 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5566}
5567
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005568void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005569 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005570 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005571 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005572 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005573 case TypeCheckKind::kExactCheck:
5574 case TypeCheckKind::kAbstractClassCheck:
5575 case TypeCheckKind::kClassHierarchyCheck:
5576 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005577 call_kind =
5578 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005579 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005580 break;
5581 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005582 case TypeCheckKind::kUnresolvedCheck:
5583 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005584 call_kind = LocationSummary::kCallOnSlowPath;
5585 break;
5586 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005587
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005588 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005589 if (baker_read_barrier_slow_path) {
5590 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5591 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005592 locations->SetInAt(0, Location::RequiresRegister());
5593 locations->SetInAt(1, Location::RequiresRegister());
5594 // The "out" register is used as a temporary, so it overlaps with the inputs.
5595 // Note that TypeCheckSlowPathARM uses this register too.
5596 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5597 // When read barriers are enabled, we need a temporary register for
5598 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005599 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005600 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005602}
5603
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005604void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005605 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005606 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005607 Location obj_loc = locations->InAt(0);
5608 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005609 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005610 Location out_loc = locations->Out();
5611 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005612 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005613 locations->GetTemp(0) :
5614 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005615 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005616 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5617 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5618 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005619 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005620 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005621
5622 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005623 // avoid null check if we know obj is not null.
5624 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005625 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005626 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005627
Roland Levillain3b359c72015-11-17 19:35:12 +00005628 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005629 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005630
Roland Levillainc9285912015-12-18 10:38:42 +00005631 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005632 case TypeCheckKind::kExactCheck: {
5633 __ cmp(out, ShifterOperand(cls));
5634 // Classes must be equal for the instanceof to succeed.
5635 __ b(&zero, NE);
5636 __ LoadImmediate(out, 1);
5637 __ b(&done);
5638 break;
5639 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005640
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 case TypeCheckKind::kAbstractClassCheck: {
5642 // If the class is abstract, we eagerly fetch the super class of the
5643 // object to avoid doing a comparison we know will fail.
5644 Label loop;
5645 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005646 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005647 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005648 // If `out` is null, we use it for the result, and jump to `done`.
5649 __ CompareAndBranchIfZero(out, &done);
5650 __ cmp(out, ShifterOperand(cls));
5651 __ b(&loop, NE);
5652 __ LoadImmediate(out, 1);
5653 if (zero.IsLinked()) {
5654 __ b(&done);
5655 }
5656 break;
5657 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005658
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005659 case TypeCheckKind::kClassHierarchyCheck: {
5660 // Walk over the class hierarchy to find a match.
5661 Label loop, success;
5662 __ Bind(&loop);
5663 __ cmp(out, ShifterOperand(cls));
5664 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005665 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005666 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005667 __ CompareAndBranchIfNonZero(out, &loop);
5668 // If `out` is null, we use it for the result, and jump to `done`.
5669 __ b(&done);
5670 __ Bind(&success);
5671 __ LoadImmediate(out, 1);
5672 if (zero.IsLinked()) {
5673 __ b(&done);
5674 }
5675 break;
5676 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005677
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005678 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005679 // Do an exact check.
5680 Label exact_check;
5681 __ cmp(out, ShifterOperand(cls));
5682 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005683 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005684 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005685 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005686 // If `out` is null, we use it for the result, and jump to `done`.
5687 __ CompareAndBranchIfZero(out, &done);
5688 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5689 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5690 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005691 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005692 __ LoadImmediate(out, 1);
5693 __ b(&done);
5694 break;
5695 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005696
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005697 case TypeCheckKind::kArrayCheck: {
5698 __ cmp(out, ShifterOperand(cls));
5699 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005700 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5701 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005702 codegen_->AddSlowPath(slow_path);
5703 __ b(slow_path->GetEntryLabel(), NE);
5704 __ LoadImmediate(out, 1);
5705 if (zero.IsLinked()) {
5706 __ b(&done);
5707 }
5708 break;
5709 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005710
Calin Juravle98893e12015-10-02 21:05:03 +01005711 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005712 case TypeCheckKind::kInterfaceCheck: {
5713 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005714 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005715 // cases.
5716 //
5717 // We cannot directly call the InstanceofNonTrivial runtime
5718 // entry point without resorting to a type checking slow path
5719 // here (i.e. by calling InvokeRuntime directly), as it would
5720 // require to assign fixed registers for the inputs of this
5721 // HInstanceOf instruction (following the runtime calling
5722 // convention), which might be cluttered by the potential first
5723 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005724 //
5725 // TODO: Introduce a new runtime entry point taking the object
5726 // to test (instead of its class) as argument, and let it deal
5727 // with the read barrier issues. This will let us refactor this
5728 // case of the `switch` code as it was previously (with a direct
5729 // call to the runtime not using a type checking slow path).
5730 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005731 DCHECK(locations->OnlyCallsOnSlowPath());
5732 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5733 /* is_fatal */ false);
5734 codegen_->AddSlowPath(slow_path);
5735 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005736 if (zero.IsLinked()) {
5737 __ b(&done);
5738 }
5739 break;
5740 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005741 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005742
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005743 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005744 __ Bind(&zero);
5745 __ LoadImmediate(out, 0);
5746 }
5747
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005748 if (done.IsLinked()) {
5749 __ Bind(&done);
5750 }
5751
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005752 if (slow_path != nullptr) {
5753 __ Bind(slow_path->GetExitLabel());
5754 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005755}
5756
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005757void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005758 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5759 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5760
Roland Levillain3b359c72015-11-17 19:35:12 +00005761 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005762 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005763 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 case TypeCheckKind::kExactCheck:
5765 case TypeCheckKind::kAbstractClassCheck:
5766 case TypeCheckKind::kClassHierarchyCheck:
5767 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005768 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5769 LocationSummary::kCallOnSlowPath :
5770 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01005771 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005772 break;
5773 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005774 case TypeCheckKind::kUnresolvedCheck:
5775 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005776 call_kind = LocationSummary::kCallOnSlowPath;
5777 break;
5778 }
5779
Roland Levillain3b359c72015-11-17 19:35:12 +00005780 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005781 if (baker_read_barrier_slow_path) {
5782 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5783 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005784 locations->SetInAt(0, Location::RequiresRegister());
5785 locations->SetInAt(1, Location::RequiresRegister());
5786 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5787 locations->AddTemp(Location::RequiresRegister());
5788 // When read barriers are enabled, we need an additional temporary
5789 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005790 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005791 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005792 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005793}
5794
5795void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005796 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005797 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005798 Location obj_loc = locations->InAt(0);
5799 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005800 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005801 Location temp_loc = locations->GetTemp(0);
5802 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005803 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005804 locations->GetTemp(1) :
5805 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005806 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5808 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5809 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005810
Roland Levillain3b359c72015-11-17 19:35:12 +00005811 bool is_type_check_slow_path_fatal =
5812 (type_check_kind == TypeCheckKind::kExactCheck ||
5813 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5814 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5815 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5816 !instruction->CanThrowIntoCatchBlock();
5817 SlowPathCode* type_check_slow_path =
5818 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5819 is_type_check_slow_path_fatal);
5820 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005821
5822 Label done;
5823 // Avoid null check if we know obj is not null.
5824 if (instruction->MustDoNullCheck()) {
5825 __ CompareAndBranchIfZero(obj, &done);
5826 }
5827
Roland Levillain3b359c72015-11-17 19:35:12 +00005828 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005829 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005830
Roland Levillain3b359c72015-11-17 19:35:12 +00005831 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005832 case TypeCheckKind::kExactCheck:
5833 case TypeCheckKind::kArrayCheck: {
5834 __ cmp(temp, ShifterOperand(cls));
5835 // Jump to slow path for throwing the exception or doing a
5836 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005837 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 break;
5839 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005840
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 case TypeCheckKind::kAbstractClassCheck: {
5842 // If the class is abstract, we eagerly fetch the super class of the
5843 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005844 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005845 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005846 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005847 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005848
5849 // If the class reference currently in `temp` is not null, jump
5850 // to the `compare_classes` label to compare it with the checked
5851 // class.
5852 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5853 // Otherwise, jump to the slow path to throw the exception.
5854 //
5855 // But before, move back the object's class into `temp` before
5856 // going into the slow path, as it has been overwritten in the
5857 // meantime.
5858 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005859 GenerateReferenceLoadTwoRegisters(
5860 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005861 __ b(type_check_slow_path->GetEntryLabel());
5862
5863 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005864 __ cmp(temp, ShifterOperand(cls));
5865 __ b(&loop, NE);
5866 break;
5867 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005868
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005869 case TypeCheckKind::kClassHierarchyCheck: {
5870 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005871 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005872 __ Bind(&loop);
5873 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005874 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005875
Roland Levillain3b359c72015-11-17 19:35:12 +00005876 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005877 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005878
5879 // If the class reference currently in `temp` is not null, jump
5880 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005881 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005882 // Otherwise, jump to the slow path to throw the exception.
5883 //
5884 // But before, move back the object's class into `temp` before
5885 // going into the slow path, as it has been overwritten in the
5886 // meantime.
5887 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005888 GenerateReferenceLoadTwoRegisters(
5889 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005890 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891 break;
5892 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005893
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005894 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005895 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005896 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005897 __ cmp(temp, ShifterOperand(cls));
5898 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005899
5900 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005901 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005902 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005903
5904 // If the component type is not null (i.e. the object is indeed
5905 // an array), jump to label `check_non_primitive_component_type`
5906 // to further check that this component type is not a primitive
5907 // type.
5908 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5909 // Otherwise, jump to the slow path to throw the exception.
5910 //
5911 // But before, move back the object's class into `temp` before
5912 // going into the slow path, as it has been overwritten in the
5913 // meantime.
5914 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005915 GenerateReferenceLoadTwoRegisters(
5916 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005917 __ b(type_check_slow_path->GetEntryLabel());
5918
5919 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005920 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005921 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5922 __ CompareAndBranchIfZero(temp, &done);
5923 // Same comment as above regarding `temp` and the slow path.
5924 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005925 GenerateReferenceLoadTwoRegisters(
5926 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005927 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005928 break;
5929 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005930
Calin Juravle98893e12015-10-02 21:05:03 +01005931 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005933 // We always go into the type check slow path for the unresolved
5934 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005935 //
5936 // We cannot directly call the CheckCast runtime entry point
5937 // without resorting to a type checking slow path here (i.e. by
5938 // calling InvokeRuntime directly), as it would require to
5939 // assign fixed registers for the inputs of this HInstanceOf
5940 // instruction (following the runtime calling convention), which
5941 // might be cluttered by the potential first read barrier
5942 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005943 //
5944 // TODO: Introduce a new runtime entry point taking the object
5945 // to test (instead of its class) as argument, and let it deal
5946 // with the read barrier issues. This will let us refactor this
5947 // case of the `switch` code as it was previously (with a direct
5948 // call to the runtime not using a type checking slow path).
5949 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005950 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005951 break;
5952 }
5953 __ Bind(&done);
5954
Roland Levillain3b359c72015-11-17 19:35:12 +00005955 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005956}
5957
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005958void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5959 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005960 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005961 InvokeRuntimeCallingConvention calling_convention;
5962 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5963}
5964
5965void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005966 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
5967 instruction,
5968 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005969 if (instruction->IsEnter()) {
5970 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5971 } else {
5972 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5973 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005974}
5975
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005976void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5977void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5978void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005979
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005980void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005981 LocationSummary* locations =
5982 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5983 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5984 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005985 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005986 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005987 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005988 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005989}
5990
5991void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5992 HandleBitwiseOperation(instruction);
5993}
5994
5995void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5996 HandleBitwiseOperation(instruction);
5997}
5998
5999void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6000 HandleBitwiseOperation(instruction);
6001}
6002
Artem Serov7fc63502016-02-09 17:15:29 +00006003
6004void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6005 LocationSummary* locations =
6006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6007 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6008 || instruction->GetResultType() == Primitive::kPrimLong);
6009
6010 locations->SetInAt(0, Location::RequiresRegister());
6011 locations->SetInAt(1, Location::RequiresRegister());
6012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6013}
6014
6015void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6016 LocationSummary* locations = instruction->GetLocations();
6017 Location first = locations->InAt(0);
6018 Location second = locations->InAt(1);
6019 Location out = locations->Out();
6020
6021 if (instruction->GetResultType() == Primitive::kPrimInt) {
6022 Register first_reg = first.AsRegister<Register>();
6023 ShifterOperand second_reg(second.AsRegister<Register>());
6024 Register out_reg = out.AsRegister<Register>();
6025
6026 switch (instruction->GetOpKind()) {
6027 case HInstruction::kAnd:
6028 __ bic(out_reg, first_reg, second_reg);
6029 break;
6030 case HInstruction::kOr:
6031 __ orn(out_reg, first_reg, second_reg);
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 return;
6040
6041 } else {
6042 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6043 Register first_low = first.AsRegisterPairLow<Register>();
6044 Register first_high = first.AsRegisterPairHigh<Register>();
6045 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6046 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6047 Register out_low = out.AsRegisterPairLow<Register>();
6048 Register out_high = out.AsRegisterPairHigh<Register>();
6049
6050 switch (instruction->GetOpKind()) {
6051 case HInstruction::kAnd:
6052 __ bic(out_low, first_low, second_low);
6053 __ bic(out_high, first_high, second_high);
6054 break;
6055 case HInstruction::kOr:
6056 __ orn(out_low, first_low, second_low);
6057 __ orn(out_high, first_high, second_high);
6058 break;
6059 // There is no EON on arm.
6060 case HInstruction::kXor:
6061 default:
6062 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6063 UNREACHABLE();
6064 }
6065 }
6066}
6067
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006068void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6069 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6070 if (value == 0xffffffffu) {
6071 if (out != first) {
6072 __ mov(out, ShifterOperand(first));
6073 }
6074 return;
6075 }
6076 if (value == 0u) {
6077 __ mov(out, ShifterOperand(0));
6078 return;
6079 }
6080 ShifterOperand so;
6081 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6082 __ and_(out, first, so);
6083 } else {
6084 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6085 __ bic(out, first, ShifterOperand(~value));
6086 }
6087}
6088
6089void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6090 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6091 if (value == 0u) {
6092 if (out != first) {
6093 __ mov(out, ShifterOperand(first));
6094 }
6095 return;
6096 }
6097 if (value == 0xffffffffu) {
6098 __ mvn(out, ShifterOperand(0));
6099 return;
6100 }
6101 ShifterOperand so;
6102 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6103 __ orr(out, first, so);
6104 } else {
6105 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6106 __ orn(out, first, ShifterOperand(~value));
6107 }
6108}
6109
6110void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6111 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6112 if (value == 0u) {
6113 if (out != first) {
6114 __ mov(out, ShifterOperand(first));
6115 }
6116 return;
6117 }
6118 __ eor(out, first, ShifterOperand(value));
6119}
6120
Vladimir Marko59751a72016-08-05 14:37:27 +01006121void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6122 Location first,
6123 uint64_t value) {
6124 Register out_low = out.AsRegisterPairLow<Register>();
6125 Register out_high = out.AsRegisterPairHigh<Register>();
6126 Register first_low = first.AsRegisterPairLow<Register>();
6127 Register first_high = first.AsRegisterPairHigh<Register>();
6128 uint32_t value_low = Low32Bits(value);
6129 uint32_t value_high = High32Bits(value);
6130 if (value_low == 0u) {
6131 if (out_low != first_low) {
6132 __ mov(out_low, ShifterOperand(first_low));
6133 }
6134 __ AddConstant(out_high, first_high, value_high);
6135 return;
6136 }
6137 __ AddConstantSetFlags(out_low, first_low, value_low);
6138 ShifterOperand so;
6139 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6140 __ adc(out_high, first_high, so);
6141 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6142 __ sbc(out_high, first_high, so);
6143 } else {
6144 LOG(FATAL) << "Unexpected constant " << value_high;
6145 UNREACHABLE();
6146 }
6147}
6148
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006149void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6150 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006151 Location first = locations->InAt(0);
6152 Location second = locations->InAt(1);
6153 Location out = locations->Out();
6154
6155 if (second.IsConstant()) {
6156 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6157 uint32_t value_low = Low32Bits(value);
6158 if (instruction->GetResultType() == Primitive::kPrimInt) {
6159 Register first_reg = first.AsRegister<Register>();
6160 Register out_reg = out.AsRegister<Register>();
6161 if (instruction->IsAnd()) {
6162 GenerateAndConst(out_reg, first_reg, value_low);
6163 } else if (instruction->IsOr()) {
6164 GenerateOrrConst(out_reg, first_reg, value_low);
6165 } else {
6166 DCHECK(instruction->IsXor());
6167 GenerateEorConst(out_reg, first_reg, value_low);
6168 }
6169 } else {
6170 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6171 uint32_t value_high = High32Bits(value);
6172 Register first_low = first.AsRegisterPairLow<Register>();
6173 Register first_high = first.AsRegisterPairHigh<Register>();
6174 Register out_low = out.AsRegisterPairLow<Register>();
6175 Register out_high = out.AsRegisterPairHigh<Register>();
6176 if (instruction->IsAnd()) {
6177 GenerateAndConst(out_low, first_low, value_low);
6178 GenerateAndConst(out_high, first_high, value_high);
6179 } else if (instruction->IsOr()) {
6180 GenerateOrrConst(out_low, first_low, value_low);
6181 GenerateOrrConst(out_high, first_high, value_high);
6182 } else {
6183 DCHECK(instruction->IsXor());
6184 GenerateEorConst(out_low, first_low, value_low);
6185 GenerateEorConst(out_high, first_high, value_high);
6186 }
6187 }
6188 return;
6189 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006190
6191 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006192 Register first_reg = first.AsRegister<Register>();
6193 ShifterOperand second_reg(second.AsRegister<Register>());
6194 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006195 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006196 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006197 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006198 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006199 } else {
6200 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006201 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006202 }
6203 } else {
6204 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006205 Register first_low = first.AsRegisterPairLow<Register>();
6206 Register first_high = first.AsRegisterPairHigh<Register>();
6207 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6208 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6209 Register out_low = out.AsRegisterPairLow<Register>();
6210 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006211 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006212 __ and_(out_low, first_low, second_low);
6213 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006214 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006215 __ orr(out_low, first_low, second_low);
6216 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006217 } else {
6218 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006219 __ eor(out_low, first_low, second_low);
6220 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006221 }
6222 }
6223}
6224
Roland Levillainc9285912015-12-18 10:38:42 +00006225void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6226 Location out,
6227 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006228 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006229 Register out_reg = out.AsRegister<Register>();
6230 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006231 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006232 if (kUseBakerReadBarrier) {
6233 // Load with fast path based Baker's read barrier.
6234 // /* HeapReference<Object> */ out = *(out + offset)
6235 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006236 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006237 } else {
6238 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006239 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006240 // in the following move operation, as we will need it for the
6241 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006242 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006243 // /* HeapReference<Object> */ out = *(out + offset)
6244 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006245 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006246 }
6247 } else {
6248 // Plain load with no read barrier.
6249 // /* HeapReference<Object> */ out = *(out + offset)
6250 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6251 __ MaybeUnpoisonHeapReference(out_reg);
6252 }
6253}
6254
6255void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6256 Location out,
6257 Location obj,
6258 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006259 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006260 Register out_reg = out.AsRegister<Register>();
6261 Register obj_reg = obj.AsRegister<Register>();
6262 if (kEmitCompilerReadBarrier) {
6263 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006264 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006265 // Load with fast path based Baker's read barrier.
6266 // /* HeapReference<Object> */ out = *(obj + offset)
6267 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006268 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006269 } else {
6270 // Load with slow path based read barrier.
6271 // /* HeapReference<Object> */ out = *(obj + offset)
6272 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6273 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6274 }
6275 } else {
6276 // Plain load with no read barrier.
6277 // /* HeapReference<Object> */ out = *(obj + offset)
6278 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6279 __ MaybeUnpoisonHeapReference(out_reg);
6280 }
6281}
6282
6283void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6284 Location root,
6285 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006286 uint32_t offset,
6287 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006288 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006289 if (requires_read_barrier) {
6290 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006291 if (kUseBakerReadBarrier) {
6292 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6293 // Baker's read barrier are used:
6294 //
6295 // root = obj.field;
6296 // if (Thread::Current()->GetIsGcMarking()) {
6297 // root = ReadBarrier::Mark(root)
6298 // }
6299
6300 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6301 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6302 static_assert(
6303 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6304 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6305 "have different sizes.");
6306 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6307 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6308 "have different sizes.");
6309
Vladimir Marko953437b2016-08-24 08:30:46 +00006310 // Slow path marking the GC root `root`.
Roland Levillainc9285912015-12-18 10:38:42 +00006311 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006312 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006313 codegen_->AddSlowPath(slow_path);
6314
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006315 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006316 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006317 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006318 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6319 __ Bind(slow_path->GetExitLabel());
6320 } else {
6321 // GC root loaded through a slow path for read barriers other
6322 // than Baker's.
6323 // /* GcRoot<mirror::Object>* */ root = obj + offset
6324 __ AddConstant(root_reg, obj, offset);
6325 // /* mirror::Object* */ root = root->Read()
6326 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6327 }
6328 } else {
6329 // Plain GC root load with no read barrier.
6330 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6331 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6332 // Note that GC roots are not affected by heap poisoning, thus we
6333 // do not have to unpoison `root_reg` here.
6334 }
6335}
6336
6337void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6338 Location ref,
6339 Register obj,
6340 uint32_t offset,
6341 Location temp,
6342 bool needs_null_check) {
6343 DCHECK(kEmitCompilerReadBarrier);
6344 DCHECK(kUseBakerReadBarrier);
6345
6346 // /* HeapReference<Object> */ ref = *(obj + offset)
6347 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006348 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006349 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006350 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006351}
6352
6353void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6354 Location ref,
6355 Register obj,
6356 uint32_t data_offset,
6357 Location index,
6358 Location temp,
6359 bool needs_null_check) {
6360 DCHECK(kEmitCompilerReadBarrier);
6361 DCHECK(kUseBakerReadBarrier);
6362
Roland Levillainbfea3352016-06-23 13:48:47 +01006363 static_assert(
6364 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6365 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006366 // /* HeapReference<Object> */ ref =
6367 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006368 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006369 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006370 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006371}
6372
6373void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6374 Location ref,
6375 Register obj,
6376 uint32_t offset,
6377 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006378 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006379 Location temp,
6380 bool needs_null_check) {
6381 DCHECK(kEmitCompilerReadBarrier);
6382 DCHECK(kUseBakerReadBarrier);
6383
6384 // In slow path based read barriers, the read barrier call is
6385 // inserted after the original load. However, in fast path based
6386 // Baker's read barriers, we need to perform the load of
6387 // mirror::Object::monitor_ *before* the original reference load.
6388 // This load-load ordering is required by the read barrier.
6389 // The fast path/slow path (for Baker's algorithm) should look like:
6390 //
6391 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6392 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6393 // HeapReference<Object> ref = *src; // Original reference load.
6394 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6395 // if (is_gray) {
6396 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6397 // }
6398 //
6399 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006400 // slightly more complex as it performs additional checks that we do
6401 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006402
6403 Register ref_reg = ref.AsRegister<Register>();
6404 Register temp_reg = temp.AsRegister<Register>();
6405 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6406
6407 // /* int32_t */ monitor = obj->monitor_
6408 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6409 if (needs_null_check) {
6410 MaybeRecordImplicitNullCheck(instruction);
6411 }
6412 // /* LockWord */ lock_word = LockWord(monitor)
6413 static_assert(sizeof(LockWord) == sizeof(int32_t),
6414 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006415
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006416 // Introduce a dependency on the lock_word including the rb_state,
6417 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006418 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006419 // `obj` is unchanged by this operation, but its value now depends
6420 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006421 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006422
6423 // The actual reference load.
6424 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006425 // Load types involving an "index": ArrayGet and
6426 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6427 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006428 if (index.IsConstant()) {
6429 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006430 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006431 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6432 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006433 // Handle the special case of the
6434 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6435 // a register pair as index ("long offset"), of which only the low
6436 // part contains data.
6437 Register index_reg = index.IsRegisterPair()
6438 ? index.AsRegisterPairLow<Register>()
6439 : index.AsRegister<Register>();
6440 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006441 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6442 }
6443 } else {
6444 // /* HeapReference<Object> */ ref = *(obj + offset)
6445 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6446 }
6447
6448 // Object* ref = ref_addr->AsMirrorPtr()
6449 __ MaybeUnpoisonHeapReference(ref_reg);
6450
Vladimir Marko953437b2016-08-24 08:30:46 +00006451 // Slow path marking the object `ref` when it is gray.
Roland Levillainc9285912015-12-18 10:38:42 +00006452 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006453 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006454 AddSlowPath(slow_path);
6455
6456 // if (rb_state == ReadBarrier::gray_ptr_)
6457 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006458 // Given the numeric representation, it's enough to check the low bit of the
6459 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6460 // which can be a 16-bit instruction unlike the TST immediate.
6461 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6462 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6463 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6464 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6465 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006466 __ Bind(slow_path->GetExitLabel());
6467}
6468
6469void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6470 Location out,
6471 Location ref,
6472 Location obj,
6473 uint32_t offset,
6474 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006475 DCHECK(kEmitCompilerReadBarrier);
6476
Roland Levillainc9285912015-12-18 10:38:42 +00006477 // Insert a slow path based read barrier *after* the reference load.
6478 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006479 // If heap poisoning is enabled, the unpoisoning of the loaded
6480 // reference will be carried out by the runtime within the slow
6481 // path.
6482 //
6483 // Note that `ref` currently does not get unpoisoned (when heap
6484 // poisoning is enabled), which is alright as the `ref` argument is
6485 // not used by the artReadBarrierSlow entry point.
6486 //
6487 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6488 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6489 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6490 AddSlowPath(slow_path);
6491
Roland Levillain3b359c72015-11-17 19:35:12 +00006492 __ b(slow_path->GetEntryLabel());
6493 __ Bind(slow_path->GetExitLabel());
6494}
6495
Roland Levillainc9285912015-12-18 10:38:42 +00006496void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6497 Location out,
6498 Location ref,
6499 Location obj,
6500 uint32_t offset,
6501 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006502 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006503 // Baker's read barriers shall be handled by the fast path
6504 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6505 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006506 // If heap poisoning is enabled, unpoisoning will be taken care of
6507 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006508 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006509 } else if (kPoisonHeapReferences) {
6510 __ UnpoisonHeapReference(out.AsRegister<Register>());
6511 }
6512}
6513
Roland Levillainc9285912015-12-18 10:38:42 +00006514void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6515 Location out,
6516 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006517 DCHECK(kEmitCompilerReadBarrier);
6518
Roland Levillainc9285912015-12-18 10:38:42 +00006519 // Insert a slow path based read barrier *after* the GC root load.
6520 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006521 // Note that GC roots are not affected by heap poisoning, so we do
6522 // not need to do anything special for this here.
6523 SlowPathCode* slow_path =
6524 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6525 AddSlowPath(slow_path);
6526
Roland Levillain3b359c72015-11-17 19:35:12 +00006527 __ b(slow_path->GetEntryLabel());
6528 __ Bind(slow_path->GetExitLabel());
6529}
6530
Vladimir Markodc151b22015-10-15 18:02:30 +01006531HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6532 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6533 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006534 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6535 // We disable pc-relative load when there is an irreducible loop, as the optimization
6536 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006537 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6538 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006539 if (GetGraph()->HasIrreducibleLoops() &&
6540 (dispatch_info.method_load_kind ==
6541 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6542 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6543 }
6544
6545 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006546 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6547 if (&outer_dex_file != target_method.dex_file) {
6548 // Calls across dex files are more likely to exceed the available BL range,
6549 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6550 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6551 (desired_dispatch_info.method_load_kind ==
6552 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6553 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6554 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6555 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006556 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006557 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006558 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006559 0u
6560 };
6561 }
6562 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006563 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006564}
6565
Vladimir Markob4536b72015-11-24 13:45:23 +00006566Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6567 Register temp) {
6568 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6569 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6570 if (!invoke->GetLocations()->Intrinsified()) {
6571 return location.AsRegister<Register>();
6572 }
6573 // For intrinsics we allow any location, so it may be on the stack.
6574 if (!location.IsRegister()) {
6575 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6576 return temp;
6577 }
6578 // For register locations, check if the register was saved. If so, get it from the stack.
6579 // Note: There is a chance that the register was saved but not overwritten, so we could
6580 // save one load. However, since this is just an intrinsic slow path we prefer this
6581 // simple and more robust approach rather that trying to determine if that's the case.
6582 SlowPathCode* slow_path = GetCurrentSlowPath();
6583 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6584 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6585 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6586 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6587 return temp;
6588 }
6589 return location.AsRegister<Register>();
6590}
6591
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006592void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006593 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006594 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006595 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6596 // LR = code address from literal pool with link-time patch.
6597 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006598 break;
6599 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6600 // LR = invoke->GetDirectCodePtr();
6601 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006602 break;
6603 default:
6604 break;
6605 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006606
Vladimir Marko58155012015-08-19 12:49:41 +00006607 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6608 switch (invoke->GetMethodLoadKind()) {
6609 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6610 // temp = thread->string_init_entrypoint
6611 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6612 break;
6613 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006614 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006615 break;
6616 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6617 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6618 break;
6619 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6620 __ LoadLiteral(temp.AsRegister<Register>(),
6621 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6622 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006623 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6624 HArmDexCacheArraysBase* base =
6625 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6626 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6627 temp.AsRegister<Register>());
6628 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6629 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6630 break;
6631 }
Vladimir Marko58155012015-08-19 12:49:41 +00006632 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006633 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006634 Register method_reg;
6635 Register reg = temp.AsRegister<Register>();
6636 if (current_method.IsRegister()) {
6637 method_reg = current_method.AsRegister<Register>();
6638 } else {
6639 DCHECK(invoke->GetLocations()->Intrinsified());
6640 DCHECK(!current_method.IsValid());
6641 method_reg = reg;
6642 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6643 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006644 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6645 __ LoadFromOffset(kLoadWord,
6646 reg,
6647 method_reg,
6648 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006649 // temp = temp[index_in_cache];
6650 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6651 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006652 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6653 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006654 }
Vladimir Marko58155012015-08-19 12:49:41 +00006655 }
6656
6657 switch (invoke->GetCodePtrLocation()) {
6658 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6659 __ bl(GetFrameEntryLabel());
6660 break;
6661 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006662 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006663 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006664 // Arbitrarily branch to the BL itself, override at link time.
6665 __ bl(&relative_call_patches_.back().label);
6666 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006667 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6668 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6669 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006670 // LR()
6671 __ blx(LR);
6672 break;
6673 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6674 // LR = callee_method->entry_point_from_quick_compiled_code_
6675 __ LoadFromOffset(
6676 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006677 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006678 // LR()
6679 __ blx(LR);
6680 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006681 }
6682
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006683 DCHECK(!IsLeafMethod());
6684}
6685
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006686void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6687 Register temp = temp_location.AsRegister<Register>();
6688 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6689 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006690
6691 // Use the calling convention instead of the location of the receiver, as
6692 // intrinsics may have put the receiver in a different register. In the intrinsics
6693 // slow path, the arguments have been moved to the right place, so here we are
6694 // guaranteed that the receiver is the first register of the calling convention.
6695 InvokeDexCallingConvention calling_convention;
6696 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006697 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006698 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006699 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006700 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006701 // Instead of simply (possibly) unpoisoning `temp` here, we should
6702 // emit a read barrier for the previous class reference load.
6703 // However this is not required in practice, as this is an
6704 // intermediate/temporary reference and because the current
6705 // concurrent copying collector keeps the from-space memory
6706 // intact/accessible until the end of the marking phase (the
6707 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006708 __ MaybeUnpoisonHeapReference(temp);
6709 // temp = temp->GetMethodAt(method_offset);
6710 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006711 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006712 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6713 // LR = temp->GetEntryPoint();
6714 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6715 // LR();
6716 __ blx(LR);
6717}
6718
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006719CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6720 const DexFile& dex_file, uint32_t string_index) {
6721 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6722}
6723
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006724CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6725 const DexFile& dex_file, uint32_t type_index) {
6726 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6727}
6728
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006729CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6730 const DexFile& dex_file, uint32_t element_offset) {
6731 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6732}
6733
6734CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6735 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6736 patches->emplace_back(dex_file, offset_or_index);
6737 return &patches->back();
6738}
6739
6740Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6741 uint32_t string_index) {
6742 return boot_image_string_patches_.GetOrCreate(
6743 StringReference(&dex_file, string_index),
6744 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6745}
6746
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006747Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6748 uint32_t type_index) {
6749 return boot_image_type_patches_.GetOrCreate(
6750 TypeReference(&dex_file, type_index),
6751 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6752}
6753
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006754Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6755 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6756 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6757 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6758}
6759
6760Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6761 return DeduplicateUint32Literal(address, &uint32_literals_);
6762}
6763
Vladimir Marko58155012015-08-19 12:49:41 +00006764void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6765 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006766 size_t size =
6767 method_patches_.size() +
6768 call_patches_.size() +
6769 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006770 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6771 boot_image_string_patches_.size() +
6772 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006773 boot_image_type_patches_.size() +
6774 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006775 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006776 linker_patches->reserve(size);
6777 for (const auto& entry : method_patches_) {
6778 const MethodReference& target_method = entry.first;
6779 Literal* literal = entry.second;
6780 DCHECK(literal->GetLabel()->IsBound());
6781 uint32_t literal_offset = literal->GetLabel()->Position();
6782 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6783 target_method.dex_file,
6784 target_method.dex_method_index));
6785 }
6786 for (const auto& entry : call_patches_) {
6787 const MethodReference& target_method = entry.first;
6788 Literal* literal = entry.second;
6789 DCHECK(literal->GetLabel()->IsBound());
6790 uint32_t literal_offset = literal->GetLabel()->Position();
6791 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6792 target_method.dex_file,
6793 target_method.dex_method_index));
6794 }
6795 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6796 uint32_t literal_offset = info.label.Position();
6797 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6798 info.target_method.dex_file,
6799 info.target_method.dex_method_index));
6800 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006801 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6802 const DexFile& dex_file = info.target_dex_file;
6803 size_t base_element_offset = info.offset_or_index;
6804 DCHECK(info.add_pc_label.IsBound());
6805 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006806 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006807 DCHECK(info.movw_label.IsBound());
6808 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006809 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6810 &dex_file,
6811 add_pc_offset,
6812 base_element_offset));
6813 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006814 DCHECK(info.movt_label.IsBound());
6815 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006816 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6817 &dex_file,
6818 add_pc_offset,
6819 base_element_offset));
6820 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006821 for (const auto& entry : boot_image_string_patches_) {
6822 const StringReference& target_string = entry.first;
6823 Literal* literal = entry.second;
6824 DCHECK(literal->GetLabel()->IsBound());
6825 uint32_t literal_offset = literal->GetLabel()->Position();
6826 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6827 target_string.dex_file,
6828 target_string.string_index));
6829 }
6830 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6831 const DexFile& dex_file = info.target_dex_file;
6832 uint32_t string_index = info.offset_or_index;
6833 DCHECK(info.add_pc_label.IsBound());
6834 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6835 // Add MOVW patch.
6836 DCHECK(info.movw_label.IsBound());
6837 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6838 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6839 &dex_file,
6840 add_pc_offset,
6841 string_index));
6842 // Add MOVT patch.
6843 DCHECK(info.movt_label.IsBound());
6844 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6845 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6846 &dex_file,
6847 add_pc_offset,
6848 string_index));
6849 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006850 for (const auto& entry : boot_image_type_patches_) {
6851 const TypeReference& target_type = entry.first;
6852 Literal* literal = entry.second;
6853 DCHECK(literal->GetLabel()->IsBound());
6854 uint32_t literal_offset = literal->GetLabel()->Position();
6855 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6856 target_type.dex_file,
6857 target_type.type_index));
6858 }
6859 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6860 const DexFile& dex_file = info.target_dex_file;
6861 uint32_t type_index = info.offset_or_index;
6862 DCHECK(info.add_pc_label.IsBound());
6863 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6864 // Add MOVW patch.
6865 DCHECK(info.movw_label.IsBound());
6866 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6867 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6868 &dex_file,
6869 add_pc_offset,
6870 type_index));
6871 // Add MOVT patch.
6872 DCHECK(info.movt_label.IsBound());
6873 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6874 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6875 &dex_file,
6876 add_pc_offset,
6877 type_index));
6878 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006879 for (const auto& entry : boot_image_address_patches_) {
6880 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6881 Literal* literal = entry.second;
6882 DCHECK(literal->GetLabel()->IsBound());
6883 uint32_t literal_offset = literal->GetLabel()->Position();
6884 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6885 }
6886}
6887
6888Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6889 return map->GetOrCreate(
6890 value,
6891 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006892}
6893
6894Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6895 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006896 return map->GetOrCreate(
6897 target_method,
6898 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006899}
6900
6901Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6902 return DeduplicateMethodLiteral(target_method, &method_patches_);
6903}
6904
6905Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6906 return DeduplicateMethodLiteral(target_method, &call_patches_);
6907}
6908
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006909void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6910 LocationSummary* locations =
6911 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6912 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6913 Location::RequiresRegister());
6914 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6915 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6917}
6918
6919void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6920 LocationSummary* locations = instr->GetLocations();
6921 Register res = locations->Out().AsRegister<Register>();
6922 Register accumulator =
6923 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6924 Register mul_left =
6925 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6926 Register mul_right =
6927 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6928
6929 if (instr->GetOpKind() == HInstruction::kAdd) {
6930 __ mla(res, mul_left, mul_right, accumulator);
6931 } else {
6932 __ mls(res, mul_left, mul_right, accumulator);
6933 }
6934}
6935
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006936void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006937 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006938 LOG(FATAL) << "Unreachable";
6939}
6940
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006941void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006942 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006943 LOG(FATAL) << "Unreachable";
6944}
6945
Mark Mendellfe57faa2015-09-18 09:26:15 -04006946// Simple implementation of packed switch - generate cascaded compare/jumps.
6947void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6948 LocationSummary* locations =
6949 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6950 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006951 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006952 codegen_->GetAssembler()->IsThumb()) {
6953 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6954 if (switch_instr->GetStartValue() != 0) {
6955 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6956 }
6957 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006958}
6959
6960void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6961 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006962 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006963 LocationSummary* locations = switch_instr->GetLocations();
6964 Register value_reg = locations->InAt(0).AsRegister<Register>();
6965 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6966
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006967 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006968 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006969 Register temp_reg = IP;
6970 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6971 // the immediate, because IP is used as the destination register. For the other
6972 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6973 // and they can be encoded in the instruction without making use of IP register.
6974 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6975
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006976 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006977 // Jump to successors[0] if value == lower_bound.
6978 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6979 int32_t last_index = 0;
6980 for (; num_entries - last_index > 2; last_index += 2) {
6981 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6982 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6983 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6984 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6985 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6986 }
6987 if (num_entries - last_index == 2) {
6988 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006989 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006990 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006991 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006992
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006993 // And the default for any other value.
6994 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6995 __ b(codegen_->GetLabelOf(default_block));
6996 }
6997 } else {
6998 // Create a table lookup.
6999 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7000
7001 // Materialize a pointer to the switch table
7002 std::vector<Label*> labels(num_entries);
7003 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7004 for (uint32_t i = 0; i < num_entries; i++) {
7005 labels[i] = codegen_->GetLabelOf(successors[i]);
7006 }
7007 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7008
7009 // Remove the bias.
7010 Register key_reg;
7011 if (lower_bound != 0) {
7012 key_reg = locations->GetTemp(1).AsRegister<Register>();
7013 __ AddConstant(key_reg, value_reg, -lower_bound);
7014 } else {
7015 key_reg = value_reg;
7016 }
7017
7018 // Check whether the value is in the table, jump to default block if not.
7019 __ CmpConstant(key_reg, num_entries - 1);
7020 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7021
7022 // Load the displacement from the table.
7023 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7024
7025 // Dispatch is a direct add to the PC (for Thumb2).
7026 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007027 }
7028}
7029
Vladimir Markob4536b72015-11-24 13:45:23 +00007030void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7031 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7032 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007033}
7034
7035void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7036 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007037 CodeGeneratorARM::PcRelativePatchInfo* labels =
7038 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007039 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007040 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007041 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007042 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007043 __ BindTrackedLabel(&labels->add_pc_label);
7044 __ add(base_reg, base_reg, ShifterOperand(PC));
7045}
7046
Andreas Gampe85b62f22015-09-09 13:15:38 -07007047void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7048 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007049 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007050 return;
7051 }
7052
7053 DCHECK_NE(type, Primitive::kPrimVoid);
7054
7055 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7056 if (return_loc.Equals(trg)) {
7057 return;
7058 }
7059
7060 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7061 // with the last branch.
7062 if (type == Primitive::kPrimLong) {
7063 HParallelMove parallel_move(GetGraph()->GetArena());
7064 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7065 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7066 GetMoveResolver()->EmitNativeCode(&parallel_move);
7067 } else if (type == Primitive::kPrimDouble) {
7068 HParallelMove parallel_move(GetGraph()->GetArena());
7069 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7070 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7071 GetMoveResolver()->EmitNativeCode(&parallel_move);
7072 } else {
7073 // Let the parallel move resolver take care of all of this.
7074 HParallelMove parallel_move(GetGraph()->GetArena());
7075 parallel_move.AddMove(return_loc, trg, type, nullptr);
7076 GetMoveResolver()->EmitNativeCode(&parallel_move);
7077 }
7078}
7079
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007080void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7081 LocationSummary* locations =
7082 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7083 locations->SetInAt(0, Location::RequiresRegister());
7084 locations->SetOut(Location::RequiresRegister());
7085}
7086
7087void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7088 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007089 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007090 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007091 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007092 __ LoadFromOffset(kLoadWord,
7093 locations->Out().AsRegister<Register>(),
7094 locations->InAt(0).AsRegister<Register>(),
7095 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007096 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007097 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007098 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007099 __ LoadFromOffset(kLoadWord,
7100 locations->Out().AsRegister<Register>(),
7101 locations->InAt(0).AsRegister<Register>(),
7102 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7103 __ LoadFromOffset(kLoadWord,
7104 locations->Out().AsRegister<Register>(),
7105 locations->Out().AsRegister<Register>(),
7106 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007107 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007108}
7109
Roland Levillain4d027112015-07-01 15:41:14 +01007110#undef __
7111#undef QUICK_ENTRY_POINT
7112
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007113} // namespace arm
7114} // namespace art