blob: 5301a6bb3e250182756a3d56208563f0f54c763b [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.
Roland Levillain16d9f942016-08-25 17:27:56 +01004601 // These registers may be used for Baker read barriers too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004602 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004603 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605}
4606
4607void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4608 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004609 Location array_loc = locations->InAt(0);
4610 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004612 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004613 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004614 bool needs_write_barrier =
4615 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004616 uint32_t data_offset =
4617 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4618 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004619 HInstruction* array_instr = instruction->GetArray();
4620 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004621 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4622 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004623
4624 switch (value_type) {
4625 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004626 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004628 case Primitive::kPrimChar:
4629 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004630 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004631 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4632 uint32_t full_offset =
4633 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4634 StoreOperandType store_type = GetStoreOperandType(value_type);
4635 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004636 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004637 Register temp = IP;
4638
4639 if (has_intermediate_address) {
4640 // We do not need to compute the intermediate address from the array: the
4641 // input instruction has done it already. See the comment in
4642 // `TryExtractArrayAccessAddress()`.
4643 if (kIsDebugBuild) {
4644 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4645 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4646 }
4647 temp = array;
4648 } else {
4649 __ add(temp, array, ShifterOperand(data_offset));
4650 }
Artem Serov6c916792016-07-11 14:02:34 +01004651 codegen_->StoreToShiftedRegOffset(value_type,
4652 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004653 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004654 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004655 }
4656 break;
4657 }
4658
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004659 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004660 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004661 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4662 // See the comment in instruction_simplifier_shared.cc.
4663 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004664
4665 if (instruction->InputAt(2)->IsNullConstant()) {
4666 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004667 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004668 size_t offset =
4669 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004670 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004671 } else {
4672 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004673 __ add(IP, array, ShifterOperand(data_offset));
4674 codegen_->StoreToShiftedRegOffset(value_type,
4675 value_loc,
4676 IP,
4677 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004678 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004679 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004680 DCHECK(!needs_write_barrier);
4681 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004682 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004683 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004684
4685 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01004686 Location temp1_loc = locations->GetTemp(0);
4687 Register temp1 = temp1_loc.AsRegister<Register>();
4688 Location temp2_loc = locations->GetTemp(1);
4689 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004690 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4691 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4692 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4693 Label done;
4694 SlowPathCode* slow_path = nullptr;
4695
Roland Levillain3b359c72015-11-17 19:35:12 +00004696 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004697 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4698 codegen_->AddSlowPath(slow_path);
4699 if (instruction->GetValueCanBeNull()) {
4700 Label non_zero;
4701 __ CompareAndBranchIfNonZero(value, &non_zero);
4702 if (index.IsConstant()) {
4703 size_t offset =
4704 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4705 __ StoreToOffset(kStoreWord, value, array, offset);
4706 } else {
4707 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004708 __ add(IP, array, ShifterOperand(data_offset));
4709 codegen_->StoreToShiftedRegOffset(value_type,
4710 value_loc,
4711 IP,
4712 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004713 }
4714 codegen_->MaybeRecordImplicitNullCheck(instruction);
4715 __ b(&done);
4716 __ Bind(&non_zero);
4717 }
4718
Roland Levillain3b359c72015-11-17 19:35:12 +00004719 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01004720 if (!kUseBakerReadBarrier) {
4721 // When (non-Baker) read barriers are enabled, the type
4722 // checking instrumentation requires two read barriers
4723 // generated by CodeGeneratorARM::GenerateReadBarrierSlow:
4724 //
4725 // __ Mov(temp2, temp1);
4726 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4727 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4728 // codegen_->GenerateReadBarrierSlow(
4729 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4730 //
4731 // // /* HeapReference<Class> */ temp2 = value->klass_
4732 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4733 // codegen_->GenerateReadBarrierSlow(
4734 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4735 //
4736 // __ cmp(temp1, ShifterOperand(temp2));
4737 //
4738 // However, the second read barrier may trash `temp`, as it
4739 // is a temporary register, and as such would not be saved
4740 // along with live registers before calling the runtime (nor
4741 // restored afterwards). So in this case, we bail out and
4742 // delegate the work to the array set slow path.
4743 //
4744 // TODO: Extend the register allocator to support a new
4745 // "(locally) live temp" location so as to avoid always
4746 // going into the slow path when read barriers are enabled?
4747 //
4748 // There is no such problem with Baker read barriers (see below).
4749 __ b(slow_path->GetEntryLabel());
4750 } else {
4751 Register temp3 = IP;
4752 Location temp3_loc = Location::RegisterLocation(temp3);
4753
4754 // Note: `temp3` (scratch register IP) cannot be used as
4755 // `ref` argument of GenerateFieldLoadWithBakerReadBarrier
4756 // calls below (see ReadBarrierMarkSlowPathARM for more
4757 // details).
4758
4759 // /* HeapReference<Class> */ temp1 = array->klass_
4760 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4761 temp1_loc,
4762 array,
4763 class_offset,
4764 temp3_loc,
4765 /* needs_null_check */ true);
4766
4767 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4768 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4769 temp1_loc,
4770 temp1,
4771 component_offset,
4772 temp3_loc,
4773 /* needs_null_check */ false);
4774 // Register `temp1` is not trashed by the read barrier
4775 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
4776 // as that method produces a call to a ReadBarrierMarkRegX
4777 // entry point, which saves all potentially live registers,
4778 // including temporaries such a `temp1`.
4779 // /* HeapReference<Class> */ temp2 = value->klass_
4780 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4781 temp2_loc,
4782 value,
4783 class_offset,
4784 temp3_loc,
4785 /* needs_null_check */ false);
4786 // If heap poisoning is enabled, `temp1` and `temp2` have
4787 // been unpoisoned by the the previous calls to
4788 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4789 __ cmp(temp1, ShifterOperand(temp2));
4790
4791 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4792 Label do_put;
4793 __ b(&do_put, EQ);
4794 // We do not need to emit a read barrier for the
4795 // following heap reference load, as `temp1` is only used
4796 // in a comparison with null below, and this reference
4797 // is not kept afterwards.
4798 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4799 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4800 // If heap poisoning is enabled, no need to unpoison
4801 // `temp`, as we are comparing against null below.
4802 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4803 __ Bind(&do_put);
4804 } else {
4805 __ b(slow_path->GetEntryLabel(), NE);
4806 }
4807 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01004809 // Non read barrier code.
4810
Roland Levillain3b359c72015-11-17 19:35:12 +00004811 // /* HeapReference<Class> */ temp1 = array->klass_
4812 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4813 codegen_->MaybeRecordImplicitNullCheck(instruction);
4814 __ MaybeUnpoisonHeapReference(temp1);
4815
4816 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4817 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4818 // /* HeapReference<Class> */ temp2 = value->klass_
4819 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4820 // If heap poisoning is enabled, no need to unpoison `temp1`
4821 // nor `temp2`, as we are comparing two poisoned references.
4822 __ cmp(temp1, ShifterOperand(temp2));
4823
4824 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4825 Label do_put;
4826 __ b(&do_put, EQ);
4827 // If heap poisoning is enabled, the `temp1` reference has
4828 // not been unpoisoned yet; unpoison it now.
4829 __ MaybeUnpoisonHeapReference(temp1);
4830
4831 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4832 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4833 // If heap poisoning is enabled, no need to unpoison
4834 // `temp1`, as we are comparing against null below.
4835 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4836 __ Bind(&do_put);
4837 } else {
4838 __ b(slow_path->GetEntryLabel(), NE);
4839 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004840 }
4841 }
4842
Artem Serov6c916792016-07-11 14:02:34 +01004843 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004844 if (kPoisonHeapReferences) {
4845 // Note that in the case where `value` is a null reference,
4846 // we do not enter this block, as a null reference does not
4847 // need poisoning.
4848 DCHECK_EQ(value_type, Primitive::kPrimNot);
4849 __ Mov(temp1, value);
4850 __ PoisonHeapReference(temp1);
4851 source = temp1;
4852 }
4853
4854 if (index.IsConstant()) {
4855 size_t offset =
4856 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4857 __ StoreToOffset(kStoreWord, source, array, offset);
4858 } else {
4859 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004860
4861 __ add(IP, array, ShifterOperand(data_offset));
4862 codegen_->StoreToShiftedRegOffset(value_type,
4863 Location::RegisterLocation(source),
4864 IP,
4865 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004866 }
4867
Roland Levillain3b359c72015-11-17 19:35:12 +00004868 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004869 codegen_->MaybeRecordImplicitNullCheck(instruction);
4870 }
4871
4872 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4873
4874 if (done.IsLinked()) {
4875 __ Bind(&done);
4876 }
4877
4878 if (slow_path != nullptr) {
4879 __ Bind(slow_path->GetExitLabel());
4880 }
4881
4882 break;
4883 }
4884
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004885 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004886 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004888 size_t offset =
4889 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004890 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004891 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004892 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004893 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 }
4895 break;
4896 }
4897
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004898 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004899 Location value = locations->InAt(2);
4900 DCHECK(value.IsFpuRegister());
4901 if (index.IsConstant()) {
4902 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004903 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004904 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004905 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004906 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4907 }
4908 break;
4909 }
4910
4911 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004912 Location value = locations->InAt(2);
4913 DCHECK(value.IsFpuRegisterPair());
4914 if (index.IsConstant()) {
4915 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004916 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004917 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004918 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004919 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4920 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004921
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004922 break;
4923 }
4924
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004926 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004927 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004929
Roland Levillain80e67092016-01-08 16:04:55 +00004930 // Objects are handled in the switch.
4931 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004932 codegen_->MaybeRecordImplicitNullCheck(instruction);
4933 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934}
4935
4936void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004937 LocationSummary* locations =
4938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004939 locations->SetInAt(0, Location::RequiresRegister());
4940 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004941}
4942
4943void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4944 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004945 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004946 Register obj = locations->InAt(0).AsRegister<Register>();
4947 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004948 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004949 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004950}
4951
Artem Serov328429f2016-07-06 16:23:04 +01004952void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00004953 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4954 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01004955 LocationSummary* locations =
4956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4957
4958 locations->SetInAt(0, Location::RequiresRegister());
4959 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4961}
4962
4963void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4964 LocationSummary* locations = instruction->GetLocations();
4965 Location out = locations->Out();
4966 Location first = locations->InAt(0);
4967 Location second = locations->InAt(1);
4968
Roland Levillain4a3aa572016-08-15 13:17:06 +00004969 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4970 DCHECK(!kEmitCompilerReadBarrier);
4971
Artem Serov328429f2016-07-06 16:23:04 +01004972 if (second.IsRegister()) {
4973 __ add(out.AsRegister<Register>(),
4974 first.AsRegister<Register>(),
4975 ShifterOperand(second.AsRegister<Register>()));
4976 } else {
4977 __ AddConstant(out.AsRegister<Register>(),
4978 first.AsRegister<Register>(),
4979 second.GetConstant()->AsIntConstant()->GetValue());
4980 }
4981}
4982
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004983void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004984 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4985 ? LocationSummary::kCallOnSlowPath
4986 : LocationSummary::kNoCall;
4987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004988 locations->SetInAt(0, Location::RequiresRegister());
4989 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004990 if (instruction->HasUses()) {
4991 locations->SetOut(Location::SameAsFirstInput());
4992 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004993}
4994
4995void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4996 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004997 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004998 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999 codegen_->AddSlowPath(slow_path);
5000
Roland Levillain271ab9c2014-11-27 15:23:57 +00005001 Register index = locations->InAt(0).AsRegister<Register>();
5002 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005003
5004 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005005 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005006}
5007
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005008void CodeGeneratorARM::MarkGCCard(Register temp,
5009 Register card,
5010 Register object,
5011 Register value,
5012 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005013 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005014 if (can_be_null) {
5015 __ CompareAndBranchIfZero(value, &is_null);
5016 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005017 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005018 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5019 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005020 if (can_be_null) {
5021 __ Bind(&is_null);
5022 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023}
5024
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005025void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005026 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005027}
5028
5029void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005030 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5031}
5032
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005033void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005034 LocationSummary* locations =
5035 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5036 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005037}
5038
5039void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005040 HBasicBlock* block = instruction->GetBlock();
5041 if (block->GetLoopInformation() != nullptr) {
5042 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5043 // The back edge will generate the suspend check.
5044 return;
5045 }
5046 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5047 // The goto will generate the suspend check.
5048 return;
5049 }
5050 GenerateSuspendCheck(instruction, nullptr);
5051}
5052
5053void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5054 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005055 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005056 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5057 if (slow_path == nullptr) {
5058 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5059 instruction->SetSlowPath(slow_path);
5060 codegen_->AddSlowPath(slow_path);
5061 if (successor != nullptr) {
5062 DCHECK(successor->IsLoopHeader());
5063 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5064 }
5065 } else {
5066 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5067 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005068
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005069 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005070 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005071 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005072 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005073 __ Bind(slow_path->GetReturnLabel());
5074 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005075 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005076 __ b(slow_path->GetEntryLabel());
5077 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005078}
5079
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005080ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5081 return codegen_->GetAssembler();
5082}
5083
5084void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005085 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005086 Location source = move->GetSource();
5087 Location destination = move->GetDestination();
5088
5089 if (source.IsRegister()) {
5090 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005091 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005092 } else if (destination.IsFpuRegister()) {
5093 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005094 } else {
5095 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005096 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005097 SP, destination.GetStackIndex());
5098 }
5099 } else if (source.IsStackSlot()) {
5100 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005101 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005102 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005103 } else if (destination.IsFpuRegister()) {
5104 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005105 } else {
5106 DCHECK(destination.IsStackSlot());
5107 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5108 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5109 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005110 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005111 if (destination.IsRegister()) {
5112 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5113 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005114 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005115 } else {
5116 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005117 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5118 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005119 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005120 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005121 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5122 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005123 } else if (destination.IsRegisterPair()) {
5124 DCHECK(ExpectedPairLayout(destination));
5125 __ LoadFromOffset(
5126 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5127 } else {
5128 DCHECK(destination.IsFpuRegisterPair()) << destination;
5129 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5130 SP,
5131 source.GetStackIndex());
5132 }
5133 } else if (source.IsRegisterPair()) {
5134 if (destination.IsRegisterPair()) {
5135 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5136 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005137 } else if (destination.IsFpuRegisterPair()) {
5138 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5139 source.AsRegisterPairLow<Register>(),
5140 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005141 } else {
5142 DCHECK(destination.IsDoubleStackSlot()) << destination;
5143 DCHECK(ExpectedPairLayout(source));
5144 __ StoreToOffset(
5145 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5146 }
5147 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005148 if (destination.IsRegisterPair()) {
5149 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5150 destination.AsRegisterPairHigh<Register>(),
5151 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5152 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005153 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5154 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5155 } else {
5156 DCHECK(destination.IsDoubleStackSlot()) << destination;
5157 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5158 SP,
5159 destination.GetStackIndex());
5160 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005161 } else {
5162 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005163 HConstant* constant = source.GetConstant();
5164 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5165 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005166 if (destination.IsRegister()) {
5167 __ LoadImmediate(destination.AsRegister<Register>(), value);
5168 } else {
5169 DCHECK(destination.IsStackSlot());
5170 __ LoadImmediate(IP, value);
5171 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5172 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005173 } else if (constant->IsLongConstant()) {
5174 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005175 if (destination.IsRegisterPair()) {
5176 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5177 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005178 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005179 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005180 __ LoadImmediate(IP, Low32Bits(value));
5181 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5182 __ LoadImmediate(IP, High32Bits(value));
5183 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5184 }
5185 } else if (constant->IsDoubleConstant()) {
5186 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005187 if (destination.IsFpuRegisterPair()) {
5188 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005189 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005190 DCHECK(destination.IsDoubleStackSlot()) << destination;
5191 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005192 __ LoadImmediate(IP, Low32Bits(int_value));
5193 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5194 __ LoadImmediate(IP, High32Bits(int_value));
5195 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5196 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005197 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005198 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005199 float value = constant->AsFloatConstant()->GetValue();
5200 if (destination.IsFpuRegister()) {
5201 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5202 } else {
5203 DCHECK(destination.IsStackSlot());
5204 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5205 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5206 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005207 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005208 }
5209}
5210
5211void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5212 __ Mov(IP, reg);
5213 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5214 __ StoreToOffset(kStoreWord, IP, SP, mem);
5215}
5216
5217void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5218 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5219 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5220 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5221 SP, mem1 + stack_offset);
5222 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5223 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5224 SP, mem2 + stack_offset);
5225 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5226}
5227
5228void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005229 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005230 Location source = move->GetSource();
5231 Location destination = move->GetDestination();
5232
5233 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005234 DCHECK_NE(source.AsRegister<Register>(), IP);
5235 DCHECK_NE(destination.AsRegister<Register>(), IP);
5236 __ Mov(IP, source.AsRegister<Register>());
5237 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5238 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005239 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005240 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005241 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005242 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005243 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5244 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005245 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005246 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005247 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005248 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005249 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005250 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005251 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005252 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005253 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5254 destination.AsRegisterPairHigh<Register>(),
5255 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005256 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005257 Register low_reg = source.IsRegisterPair()
5258 ? source.AsRegisterPairLow<Register>()
5259 : destination.AsRegisterPairLow<Register>();
5260 int mem = source.IsRegisterPair()
5261 ? destination.GetStackIndex()
5262 : source.GetStackIndex();
5263 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005264 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005265 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005266 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005267 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005268 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5269 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005270 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005271 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005272 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005273 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5274 DRegister reg = source.IsFpuRegisterPair()
5275 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5276 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5277 int mem = source.IsFpuRegisterPair()
5278 ? destination.GetStackIndex()
5279 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005280 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005281 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005282 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005283 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5284 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5285 : destination.AsFpuRegister<SRegister>();
5286 int mem = source.IsFpuRegister()
5287 ? destination.GetStackIndex()
5288 : source.GetStackIndex();
5289
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005290 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005291 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005292 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005293 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005294 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5295 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005296 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005297 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005298 }
5299}
5300
5301void ParallelMoveResolverARM::SpillScratch(int reg) {
5302 __ Push(static_cast<Register>(reg));
5303}
5304
5305void ParallelMoveResolverARM::RestoreScratch(int reg) {
5306 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005307}
5308
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005309HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5310 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005311 switch (desired_class_load_kind) {
5312 case HLoadClass::LoadKind::kReferrersClass:
5313 break;
5314 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5315 DCHECK(!GetCompilerOptions().GetCompilePic());
5316 break;
5317 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5318 DCHECK(GetCompilerOptions().GetCompilePic());
5319 break;
5320 case HLoadClass::LoadKind::kBootImageAddress:
5321 break;
5322 case HLoadClass::LoadKind::kDexCacheAddress:
5323 DCHECK(Runtime::Current()->UseJitCompilation());
5324 break;
5325 case HLoadClass::LoadKind::kDexCachePcRelative:
5326 DCHECK(!Runtime::Current()->UseJitCompilation());
5327 // We disable pc-relative load when there is an irreducible loop, as the optimization
5328 // is incompatible with it.
5329 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5330 // with irreducible loops.
5331 if (GetGraph()->HasIrreducibleLoops()) {
5332 return HLoadClass::LoadKind::kDexCacheViaMethod;
5333 }
5334 break;
5335 case HLoadClass::LoadKind::kDexCacheViaMethod:
5336 break;
5337 }
5338 return desired_class_load_kind;
5339}
5340
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005341void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005342 if (cls->NeedsAccessCheck()) {
5343 InvokeRuntimeCallingConvention calling_convention;
5344 CodeGenerator::CreateLoadClassLocationSummary(
5345 cls,
5346 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5347 Location::RegisterLocation(R0),
5348 /* code_generator_supports_read_barrier */ true);
5349 return;
5350 }
5351
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005352 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5353 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005354 ? LocationSummary::kCallOnSlowPath
5355 : LocationSummary::kNoCall;
5356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005357 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005358 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5359 }
5360
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005361 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5362 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5363 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5364 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5365 locations->SetInAt(0, Location::RequiresRegister());
5366 }
5367 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005368}
5369
5370void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005371 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005372 if (cls->NeedsAccessCheck()) {
5373 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005374 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005375 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005376 return;
5377 }
5378
Roland Levillain3b359c72015-11-17 19:35:12 +00005379 Location out_loc = locations->Out();
5380 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005381
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005382 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005383 bool generate_null_check = false;
5384 switch (cls->GetLoadKind()) {
5385 case HLoadClass::LoadKind::kReferrersClass: {
5386 DCHECK(!cls->CanCallRuntime());
5387 DCHECK(!cls->MustGenerateClinitCheck());
5388 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5389 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005390 GenerateGcRootFieldLoad(cls,
5391 out_loc,
5392 current_method,
5393 ArtMethod::DeclaringClassOffset().Int32Value(),
5394 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005395 break;
5396 }
5397 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005398 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005399 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5400 cls->GetTypeIndex()));
5401 break;
5402 }
5403 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005404 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005405 CodeGeneratorARM::PcRelativePatchInfo* labels =
5406 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5407 __ BindTrackedLabel(&labels->movw_label);
5408 __ movw(out, /* placeholder */ 0u);
5409 __ BindTrackedLabel(&labels->movt_label);
5410 __ movt(out, /* placeholder */ 0u);
5411 __ BindTrackedLabel(&labels->add_pc_label);
5412 __ add(out, out, ShifterOperand(PC));
5413 break;
5414 }
5415 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005416 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005417 DCHECK_NE(cls->GetAddress(), 0u);
5418 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5419 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5420 break;
5421 }
5422 case HLoadClass::LoadKind::kDexCacheAddress: {
5423 DCHECK_NE(cls->GetAddress(), 0u);
5424 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5425 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5426 // a 128B range. To try and reduce the number of literals if we load multiple types,
5427 // simply split the dex cache address to a 128B aligned base loaded from a literal
5428 // and the remaining offset embedded in the load.
5429 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5430 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5431 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5432 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5433 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5434 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5435 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005436 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005437 generate_null_check = !cls->IsInDexCache();
5438 break;
5439 }
5440 case HLoadClass::LoadKind::kDexCachePcRelative: {
5441 Register base_reg = locations->InAt(0).AsRegister<Register>();
5442 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5443 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5444 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005445 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005446 generate_null_check = !cls->IsInDexCache();
5447 break;
5448 }
5449 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5450 // /* GcRoot<mirror::Class>[] */ out =
5451 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5452 Register current_method = locations->InAt(0).AsRegister<Register>();
5453 __ LoadFromOffset(kLoadWord,
5454 out,
5455 current_method,
5456 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5457 // /* GcRoot<mirror::Class> */ out = out[type_index]
5458 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005459 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005460 generate_null_check = !cls->IsInDexCache();
5461 }
5462 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005463
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005464 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5465 DCHECK(cls->CanCallRuntime());
5466 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5467 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5468 codegen_->AddSlowPath(slow_path);
5469 if (generate_null_check) {
5470 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5471 }
5472 if (cls->MustGenerateClinitCheck()) {
5473 GenerateClassInitializationCheck(slow_path, out);
5474 } else {
5475 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005476 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005477 }
5478}
5479
5480void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5481 LocationSummary* locations =
5482 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5483 locations->SetInAt(0, Location::RequiresRegister());
5484 if (check->HasUses()) {
5485 locations->SetOut(Location::SameAsFirstInput());
5486 }
5487}
5488
5489void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005490 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005491 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005492 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005493 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005494 GenerateClassInitializationCheck(slow_path,
5495 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005496}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005497
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005498void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005499 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005500 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5501 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5502 __ b(slow_path->GetEntryLabel(), LT);
5503 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5504 // properly. Therefore, we do a memory fence.
5505 __ dmb(ISH);
5506 __ Bind(slow_path->GetExitLabel());
5507}
5508
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005509HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5510 HLoadString::LoadKind desired_string_load_kind) {
5511 if (kEmitCompilerReadBarrier) {
5512 switch (desired_string_load_kind) {
5513 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5514 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5515 case HLoadString::LoadKind::kBootImageAddress:
5516 // TODO: Implement for read barrier.
5517 return HLoadString::LoadKind::kDexCacheViaMethod;
5518 default:
5519 break;
5520 }
5521 }
5522 switch (desired_string_load_kind) {
5523 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5524 DCHECK(!GetCompilerOptions().GetCompilePic());
5525 break;
5526 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5527 DCHECK(GetCompilerOptions().GetCompilePic());
5528 break;
5529 case HLoadString::LoadKind::kBootImageAddress:
5530 break;
5531 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005532 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005533 break;
5534 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005535 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005536 // We disable pc-relative load when there is an irreducible loop, as the optimization
5537 // is incompatible with it.
5538 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5539 // with irreducible loops.
5540 if (GetGraph()->HasIrreducibleLoops()) {
5541 return HLoadString::LoadKind::kDexCacheViaMethod;
5542 }
5543 break;
5544 case HLoadString::LoadKind::kDexCacheViaMethod:
5545 break;
5546 }
5547 return desired_string_load_kind;
5548}
5549
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005550void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005551 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5552 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005553 : LocationSummary::kNoCall;
5554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005555
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005556 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005557 DCHECK(load_kind != HLoadString::LoadKind::kDexCachePcRelative) << "Not supported";
5558 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005559 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005560 locations->SetOut(Location::RegisterLocation(R0));
5561 } else {
5562 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005563 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005564}
5565
5566void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005567 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005568 Location out_loc = locations->Out();
5569 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005570 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005571
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005572 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005573 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5574 DCHECK(!kEmitCompilerReadBarrier);
5575 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5576 load->GetStringIndex()));
5577 return; // No dex cache slow path.
5578 }
5579 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5580 DCHECK(!kEmitCompilerReadBarrier);
5581 CodeGeneratorARM::PcRelativePatchInfo* labels =
5582 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5583 __ BindTrackedLabel(&labels->movw_label);
5584 __ movw(out, /* placeholder */ 0u);
5585 __ BindTrackedLabel(&labels->movt_label);
5586 __ movt(out, /* placeholder */ 0u);
5587 __ BindTrackedLabel(&labels->add_pc_label);
5588 __ add(out, out, ShifterOperand(PC));
5589 return; // No dex cache slow path.
5590 }
5591 case HLoadString::LoadKind::kBootImageAddress: {
5592 DCHECK(!kEmitCompilerReadBarrier);
5593 DCHECK_NE(load->GetAddress(), 0u);
5594 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5595 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5596 return; // No dex cache slow path.
5597 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005598 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005599 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005600 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005601
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005602 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5603 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5604 InvokeRuntimeCallingConvention calling_convention;
5605 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5606 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5607 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005608}
5609
David Brazdilcb1c0552015-08-04 16:22:25 +01005610static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005611 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005612}
5613
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005614void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5615 LocationSummary* locations =
5616 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5617 locations->SetOut(Location::RequiresRegister());
5618}
5619
5620void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005621 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005622 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5623}
5624
5625void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5626 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5627}
5628
5629void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005630 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005631 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005632}
5633
5634void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5635 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005636 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005637 InvokeRuntimeCallingConvention calling_convention;
5638 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5639}
5640
5641void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005642 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005643 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005644}
5645
Roland Levillainc9285912015-12-18 10:38:42 +00005646static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5647 return kEmitCompilerReadBarrier &&
5648 (kUseBakerReadBarrier ||
5649 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5650 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5651 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5652}
5653
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005654void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005655 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005656 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005657 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005658 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005659 case TypeCheckKind::kExactCheck:
5660 case TypeCheckKind::kAbstractClassCheck:
5661 case TypeCheckKind::kClassHierarchyCheck:
5662 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005663 call_kind =
5664 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005665 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005666 break;
5667 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005668 case TypeCheckKind::kUnresolvedCheck:
5669 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005670 call_kind = LocationSummary::kCallOnSlowPath;
5671 break;
5672 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005673
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005675 if (baker_read_barrier_slow_path) {
5676 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5677 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005678 locations->SetInAt(0, Location::RequiresRegister());
5679 locations->SetInAt(1, Location::RequiresRegister());
5680 // The "out" register is used as a temporary, so it overlaps with the inputs.
5681 // Note that TypeCheckSlowPathARM uses this register too.
5682 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5683 // When read barriers are enabled, we need a temporary register for
5684 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005685 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005686 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005687 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005688}
5689
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005690void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005691 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005692 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005693 Location obj_loc = locations->InAt(0);
5694 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005695 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005696 Location out_loc = locations->Out();
5697 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005698 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005699 locations->GetTemp(0) :
5700 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005701 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005702 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5703 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5704 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005705 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005706 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005707
5708 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005709 // avoid null check if we know obj is not null.
5710 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005711 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005712 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713
Roland Levillain3b359c72015-11-17 19:35:12 +00005714 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005715 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005716
Roland Levillainc9285912015-12-18 10:38:42 +00005717 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005718 case TypeCheckKind::kExactCheck: {
5719 __ cmp(out, ShifterOperand(cls));
5720 // Classes must be equal for the instanceof to succeed.
5721 __ b(&zero, NE);
5722 __ LoadImmediate(out, 1);
5723 __ b(&done);
5724 break;
5725 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005726
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 case TypeCheckKind::kAbstractClassCheck: {
5728 // If the class is abstract, we eagerly fetch the super class of the
5729 // object to avoid doing a comparison we know will fail.
5730 Label loop;
5731 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005732 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005733 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005734 // If `out` is null, we use it for the result, and jump to `done`.
5735 __ CompareAndBranchIfZero(out, &done);
5736 __ cmp(out, ShifterOperand(cls));
5737 __ b(&loop, NE);
5738 __ LoadImmediate(out, 1);
5739 if (zero.IsLinked()) {
5740 __ b(&done);
5741 }
5742 break;
5743 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005744
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005745 case TypeCheckKind::kClassHierarchyCheck: {
5746 // Walk over the class hierarchy to find a match.
5747 Label loop, success;
5748 __ Bind(&loop);
5749 __ cmp(out, ShifterOperand(cls));
5750 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005751 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005752 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005753 __ CompareAndBranchIfNonZero(out, &loop);
5754 // If `out` is null, we use it for the result, and jump to `done`.
5755 __ b(&done);
5756 __ Bind(&success);
5757 __ LoadImmediate(out, 1);
5758 if (zero.IsLinked()) {
5759 __ b(&done);
5760 }
5761 break;
5762 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005763
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005765 // Do an exact check.
5766 Label exact_check;
5767 __ cmp(out, ShifterOperand(cls));
5768 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005769 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005770 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005771 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005772 // If `out` is null, we use it for the result, and jump to `done`.
5773 __ CompareAndBranchIfZero(out, &done);
5774 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5775 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5776 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005777 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005778 __ LoadImmediate(out, 1);
5779 __ b(&done);
5780 break;
5781 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005782
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005783 case TypeCheckKind::kArrayCheck: {
5784 __ cmp(out, ShifterOperand(cls));
5785 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005786 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5787 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005788 codegen_->AddSlowPath(slow_path);
5789 __ b(slow_path->GetEntryLabel(), NE);
5790 __ LoadImmediate(out, 1);
5791 if (zero.IsLinked()) {
5792 __ b(&done);
5793 }
5794 break;
5795 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005796
Calin Juravle98893e12015-10-02 21:05:03 +01005797 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005798 case TypeCheckKind::kInterfaceCheck: {
5799 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005800 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005801 // cases.
5802 //
5803 // We cannot directly call the InstanceofNonTrivial runtime
5804 // entry point without resorting to a type checking slow path
5805 // here (i.e. by calling InvokeRuntime directly), as it would
5806 // require to assign fixed registers for the inputs of this
5807 // HInstanceOf instruction (following the runtime calling
5808 // convention), which might be cluttered by the potential first
5809 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005810 //
5811 // TODO: Introduce a new runtime entry point taking the object
5812 // to test (instead of its class) as argument, and let it deal
5813 // with the read barrier issues. This will let us refactor this
5814 // case of the `switch` code as it was previously (with a direct
5815 // call to the runtime not using a type checking slow path).
5816 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005817 DCHECK(locations->OnlyCallsOnSlowPath());
5818 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5819 /* is_fatal */ false);
5820 codegen_->AddSlowPath(slow_path);
5821 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005822 if (zero.IsLinked()) {
5823 __ b(&done);
5824 }
5825 break;
5826 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005827 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005828
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005830 __ Bind(&zero);
5831 __ LoadImmediate(out, 0);
5832 }
5833
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005834 if (done.IsLinked()) {
5835 __ Bind(&done);
5836 }
5837
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005838 if (slow_path != nullptr) {
5839 __ Bind(slow_path->GetExitLabel());
5840 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005841}
5842
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005843void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5845 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5846
Roland Levillain3b359c72015-11-17 19:35:12 +00005847 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005848 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005849 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005850 case TypeCheckKind::kExactCheck:
5851 case TypeCheckKind::kAbstractClassCheck:
5852 case TypeCheckKind::kClassHierarchyCheck:
5853 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005854 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5855 LocationSummary::kCallOnSlowPath :
5856 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Vladimir Marko70e97462016-08-09 11:04:26 +01005857 baker_read_barrier_slow_path = kUseBakerReadBarrier && !throws_into_catch;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005858 break;
5859 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005860 case TypeCheckKind::kUnresolvedCheck:
5861 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 call_kind = LocationSummary::kCallOnSlowPath;
5863 break;
5864 }
5865
Roland Levillain3b359c72015-11-17 19:35:12 +00005866 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005867 if (baker_read_barrier_slow_path) {
5868 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5869 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005870 locations->SetInAt(0, Location::RequiresRegister());
5871 locations->SetInAt(1, Location::RequiresRegister());
5872 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5873 locations->AddTemp(Location::RequiresRegister());
5874 // When read barriers are enabled, we need an additional temporary
5875 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005876 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005877 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005878 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005879}
5880
5881void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005882 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005883 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005884 Location obj_loc = locations->InAt(0);
5885 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005886 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005887 Location temp_loc = locations->GetTemp(0);
5888 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005889 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005890 locations->GetTemp(1) :
5891 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005892 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005893 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5894 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5895 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005896
Roland Levillain3b359c72015-11-17 19:35:12 +00005897 bool is_type_check_slow_path_fatal =
5898 (type_check_kind == TypeCheckKind::kExactCheck ||
5899 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5900 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5901 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5902 !instruction->CanThrowIntoCatchBlock();
5903 SlowPathCode* type_check_slow_path =
5904 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5905 is_type_check_slow_path_fatal);
5906 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005907
5908 Label done;
5909 // Avoid null check if we know obj is not null.
5910 if (instruction->MustDoNullCheck()) {
5911 __ CompareAndBranchIfZero(obj, &done);
5912 }
5913
Roland Levillain3b359c72015-11-17 19:35:12 +00005914 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005915 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916
Roland Levillain3b359c72015-11-17 19:35:12 +00005917 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005918 case TypeCheckKind::kExactCheck:
5919 case TypeCheckKind::kArrayCheck: {
5920 __ cmp(temp, ShifterOperand(cls));
5921 // Jump to slow path for throwing the exception or doing a
5922 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005923 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005924 break;
5925 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005926
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005927 case TypeCheckKind::kAbstractClassCheck: {
5928 // If the class is abstract, we eagerly fetch the super class of the
5929 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005930 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005932 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005933 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005934
5935 // If the class reference currently in `temp` is not null, jump
5936 // to the `compare_classes` label to compare it with the checked
5937 // class.
5938 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5939 // Otherwise, jump to the slow path to throw the exception.
5940 //
5941 // But before, move back the object's class into `temp` before
5942 // going into the slow path, as it has been overwritten in the
5943 // meantime.
5944 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005945 GenerateReferenceLoadTwoRegisters(
5946 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005947 __ b(type_check_slow_path->GetEntryLabel());
5948
5949 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 __ cmp(temp, ShifterOperand(cls));
5951 __ b(&loop, NE);
5952 break;
5953 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005954
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005955 case TypeCheckKind::kClassHierarchyCheck: {
5956 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005957 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005958 __ Bind(&loop);
5959 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005960 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005961
Roland Levillain3b359c72015-11-17 19:35:12 +00005962 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005963 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005964
5965 // If the class reference currently in `temp` is not null, jump
5966 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005967 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005968 // Otherwise, jump to the slow path to throw the exception.
5969 //
5970 // But before, move back the object's class into `temp` before
5971 // going into the slow path, as it has been overwritten in the
5972 // meantime.
5973 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005974 GenerateReferenceLoadTwoRegisters(
5975 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005976 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 break;
5978 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005979
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005981 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005982 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005983 __ cmp(temp, ShifterOperand(cls));
5984 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005985
5986 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005987 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005988 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005989
5990 // If the component type is not null (i.e. the object is indeed
5991 // an array), jump to label `check_non_primitive_component_type`
5992 // to further check that this component type is not a primitive
5993 // type.
5994 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5995 // Otherwise, jump to the slow path to throw the exception.
5996 //
5997 // But before, move back the object's class into `temp` before
5998 // going into the slow path, as it has been overwritten in the
5999 // meantime.
6000 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006001 GenerateReferenceLoadTwoRegisters(
6002 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006003 __ b(type_check_slow_path->GetEntryLabel());
6004
6005 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006007 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6008 __ CompareAndBranchIfZero(temp, &done);
6009 // Same comment as above regarding `temp` and the slow path.
6010 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006011 GenerateReferenceLoadTwoRegisters(
6012 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006013 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006014 break;
6015 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006016
Calin Juravle98893e12015-10-02 21:05:03 +01006017 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006018 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006019 // We always go into the type check slow path for the unresolved
6020 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006021 //
6022 // We cannot directly call the CheckCast runtime entry point
6023 // without resorting to a type checking slow path here (i.e. by
6024 // calling InvokeRuntime directly), as it would require to
6025 // assign fixed registers for the inputs of this HInstanceOf
6026 // instruction (following the runtime calling convention), which
6027 // might be cluttered by the potential first read barrier
6028 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006029 //
6030 // TODO: Introduce a new runtime entry point taking the object
6031 // to test (instead of its class) as argument, and let it deal
6032 // with the read barrier issues. This will let us refactor this
6033 // case of the `switch` code as it was previously (with a direct
6034 // call to the runtime not using a type checking slow path).
6035 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006036 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006037 break;
6038 }
6039 __ Bind(&done);
6040
Roland Levillain3b359c72015-11-17 19:35:12 +00006041 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006042}
6043
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006044void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6045 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006046 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006047 InvokeRuntimeCallingConvention calling_convention;
6048 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6049}
6050
6051void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006052 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6053 instruction,
6054 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006055 if (instruction->IsEnter()) {
6056 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6057 } else {
6058 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6059 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006060}
6061
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006062void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6063void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6064void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006065
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006066void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006067 LocationSummary* locations =
6068 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6069 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6070 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006071 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006072 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006073 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006075}
6076
6077void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6078 HandleBitwiseOperation(instruction);
6079}
6080
6081void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6082 HandleBitwiseOperation(instruction);
6083}
6084
6085void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6086 HandleBitwiseOperation(instruction);
6087}
6088
Artem Serov7fc63502016-02-09 17:15:29 +00006089
6090void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6091 LocationSummary* locations =
6092 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6093 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6094 || instruction->GetResultType() == Primitive::kPrimLong);
6095
6096 locations->SetInAt(0, Location::RequiresRegister());
6097 locations->SetInAt(1, Location::RequiresRegister());
6098 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6099}
6100
6101void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6102 LocationSummary* locations = instruction->GetLocations();
6103 Location first = locations->InAt(0);
6104 Location second = locations->InAt(1);
6105 Location out = locations->Out();
6106
6107 if (instruction->GetResultType() == Primitive::kPrimInt) {
6108 Register first_reg = first.AsRegister<Register>();
6109 ShifterOperand second_reg(second.AsRegister<Register>());
6110 Register out_reg = out.AsRegister<Register>();
6111
6112 switch (instruction->GetOpKind()) {
6113 case HInstruction::kAnd:
6114 __ bic(out_reg, first_reg, second_reg);
6115 break;
6116 case HInstruction::kOr:
6117 __ orn(out_reg, first_reg, second_reg);
6118 break;
6119 // There is no EON on arm.
6120 case HInstruction::kXor:
6121 default:
6122 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6123 UNREACHABLE();
6124 }
6125 return;
6126
6127 } else {
6128 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6129 Register first_low = first.AsRegisterPairLow<Register>();
6130 Register first_high = first.AsRegisterPairHigh<Register>();
6131 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6132 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6133 Register out_low = out.AsRegisterPairLow<Register>();
6134 Register out_high = out.AsRegisterPairHigh<Register>();
6135
6136 switch (instruction->GetOpKind()) {
6137 case HInstruction::kAnd:
6138 __ bic(out_low, first_low, second_low);
6139 __ bic(out_high, first_high, second_high);
6140 break;
6141 case HInstruction::kOr:
6142 __ orn(out_low, first_low, second_low);
6143 __ orn(out_high, first_high, second_high);
6144 break;
6145 // There is no EON on arm.
6146 case HInstruction::kXor:
6147 default:
6148 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6149 UNREACHABLE();
6150 }
6151 }
6152}
6153
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006154void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6155 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6156 if (value == 0xffffffffu) {
6157 if (out != first) {
6158 __ mov(out, ShifterOperand(first));
6159 }
6160 return;
6161 }
6162 if (value == 0u) {
6163 __ mov(out, ShifterOperand(0));
6164 return;
6165 }
6166 ShifterOperand so;
6167 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6168 __ and_(out, first, so);
6169 } else {
6170 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6171 __ bic(out, first, ShifterOperand(~value));
6172 }
6173}
6174
6175void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6176 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6177 if (value == 0u) {
6178 if (out != first) {
6179 __ mov(out, ShifterOperand(first));
6180 }
6181 return;
6182 }
6183 if (value == 0xffffffffu) {
6184 __ mvn(out, ShifterOperand(0));
6185 return;
6186 }
6187 ShifterOperand so;
6188 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6189 __ orr(out, first, so);
6190 } else {
6191 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6192 __ orn(out, first, ShifterOperand(~value));
6193 }
6194}
6195
6196void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6197 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6198 if (value == 0u) {
6199 if (out != first) {
6200 __ mov(out, ShifterOperand(first));
6201 }
6202 return;
6203 }
6204 __ eor(out, first, ShifterOperand(value));
6205}
6206
Vladimir Marko59751a72016-08-05 14:37:27 +01006207void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6208 Location first,
6209 uint64_t value) {
6210 Register out_low = out.AsRegisterPairLow<Register>();
6211 Register out_high = out.AsRegisterPairHigh<Register>();
6212 Register first_low = first.AsRegisterPairLow<Register>();
6213 Register first_high = first.AsRegisterPairHigh<Register>();
6214 uint32_t value_low = Low32Bits(value);
6215 uint32_t value_high = High32Bits(value);
6216 if (value_low == 0u) {
6217 if (out_low != first_low) {
6218 __ mov(out_low, ShifterOperand(first_low));
6219 }
6220 __ AddConstant(out_high, first_high, value_high);
6221 return;
6222 }
6223 __ AddConstantSetFlags(out_low, first_low, value_low);
6224 ShifterOperand so;
6225 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6226 __ adc(out_high, first_high, so);
6227 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6228 __ sbc(out_high, first_high, so);
6229 } else {
6230 LOG(FATAL) << "Unexpected constant " << value_high;
6231 UNREACHABLE();
6232 }
6233}
6234
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006235void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6236 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006237 Location first = locations->InAt(0);
6238 Location second = locations->InAt(1);
6239 Location out = locations->Out();
6240
6241 if (second.IsConstant()) {
6242 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6243 uint32_t value_low = Low32Bits(value);
6244 if (instruction->GetResultType() == Primitive::kPrimInt) {
6245 Register first_reg = first.AsRegister<Register>();
6246 Register out_reg = out.AsRegister<Register>();
6247 if (instruction->IsAnd()) {
6248 GenerateAndConst(out_reg, first_reg, value_low);
6249 } else if (instruction->IsOr()) {
6250 GenerateOrrConst(out_reg, first_reg, value_low);
6251 } else {
6252 DCHECK(instruction->IsXor());
6253 GenerateEorConst(out_reg, first_reg, value_low);
6254 }
6255 } else {
6256 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6257 uint32_t value_high = High32Bits(value);
6258 Register first_low = first.AsRegisterPairLow<Register>();
6259 Register first_high = first.AsRegisterPairHigh<Register>();
6260 Register out_low = out.AsRegisterPairLow<Register>();
6261 Register out_high = out.AsRegisterPairHigh<Register>();
6262 if (instruction->IsAnd()) {
6263 GenerateAndConst(out_low, first_low, value_low);
6264 GenerateAndConst(out_high, first_high, value_high);
6265 } else if (instruction->IsOr()) {
6266 GenerateOrrConst(out_low, first_low, value_low);
6267 GenerateOrrConst(out_high, first_high, value_high);
6268 } else {
6269 DCHECK(instruction->IsXor());
6270 GenerateEorConst(out_low, first_low, value_low);
6271 GenerateEorConst(out_high, first_high, value_high);
6272 }
6273 }
6274 return;
6275 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006276
6277 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006278 Register first_reg = first.AsRegister<Register>();
6279 ShifterOperand second_reg(second.AsRegister<Register>());
6280 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006281 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006282 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006283 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006284 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006285 } else {
6286 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006287 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006288 }
6289 } else {
6290 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006291 Register first_low = first.AsRegisterPairLow<Register>();
6292 Register first_high = first.AsRegisterPairHigh<Register>();
6293 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6294 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6295 Register out_low = out.AsRegisterPairLow<Register>();
6296 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006297 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006298 __ and_(out_low, first_low, second_low);
6299 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006300 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006301 __ orr(out_low, first_low, second_low);
6302 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006303 } else {
6304 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006305 __ eor(out_low, first_low, second_low);
6306 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006307 }
6308 }
6309}
6310
Roland Levillainc9285912015-12-18 10:38:42 +00006311void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6312 Location out,
6313 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006314 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006315 Register out_reg = out.AsRegister<Register>();
6316 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006317 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006318 if (kUseBakerReadBarrier) {
6319 // Load with fast path based Baker's read barrier.
6320 // /* HeapReference<Object> */ out = *(out + offset)
6321 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006322 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006323 } else {
6324 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006325 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006326 // in the following move operation, as we will need it for the
6327 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006328 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006329 // /* HeapReference<Object> */ out = *(out + offset)
6330 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006331 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006332 }
6333 } else {
6334 // Plain load with no read barrier.
6335 // /* HeapReference<Object> */ out = *(out + offset)
6336 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6337 __ MaybeUnpoisonHeapReference(out_reg);
6338 }
6339}
6340
6341void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6342 Location out,
6343 Location obj,
6344 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006345 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006346 Register out_reg = out.AsRegister<Register>();
6347 Register obj_reg = obj.AsRegister<Register>();
6348 if (kEmitCompilerReadBarrier) {
6349 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006350 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006351 // Load with fast path based Baker's read barrier.
6352 // /* HeapReference<Object> */ out = *(obj + offset)
6353 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006354 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006355 } else {
6356 // Load with slow path based read barrier.
6357 // /* HeapReference<Object> */ out = *(obj + offset)
6358 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6359 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6360 }
6361 } else {
6362 // Plain load with no read barrier.
6363 // /* HeapReference<Object> */ out = *(obj + offset)
6364 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6365 __ MaybeUnpoisonHeapReference(out_reg);
6366 }
6367}
6368
6369void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6370 Location root,
6371 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006372 uint32_t offset,
6373 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006374 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006375 if (requires_read_barrier) {
6376 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006377 if (kUseBakerReadBarrier) {
6378 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6379 // Baker's read barrier are used:
6380 //
6381 // root = obj.field;
6382 // if (Thread::Current()->GetIsGcMarking()) {
6383 // root = ReadBarrier::Mark(root)
6384 // }
6385
6386 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6387 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6388 static_assert(
6389 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6390 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6391 "have different sizes.");
6392 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6393 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6394 "have different sizes.");
6395
Vladimir Marko953437b2016-08-24 08:30:46 +00006396 // Slow path marking the GC root `root`.
Roland Levillainc9285912015-12-18 10:38:42 +00006397 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006398 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006399 codegen_->AddSlowPath(slow_path);
6400
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006401 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006402 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006403 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006404 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6405 __ Bind(slow_path->GetExitLabel());
6406 } else {
6407 // GC root loaded through a slow path for read barriers other
6408 // than Baker's.
6409 // /* GcRoot<mirror::Object>* */ root = obj + offset
6410 __ AddConstant(root_reg, obj, offset);
6411 // /* mirror::Object* */ root = root->Read()
6412 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6413 }
6414 } else {
6415 // Plain GC root load with no read barrier.
6416 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6417 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6418 // Note that GC roots are not affected by heap poisoning, thus we
6419 // do not have to unpoison `root_reg` here.
6420 }
6421}
6422
6423void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6424 Location ref,
6425 Register obj,
6426 uint32_t offset,
6427 Location temp,
6428 bool needs_null_check) {
6429 DCHECK(kEmitCompilerReadBarrier);
6430 DCHECK(kUseBakerReadBarrier);
6431
6432 // /* HeapReference<Object> */ ref = *(obj + offset)
6433 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006434 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006435 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006436 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006437}
6438
6439void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6440 Location ref,
6441 Register obj,
6442 uint32_t data_offset,
6443 Location index,
6444 Location temp,
6445 bool needs_null_check) {
6446 DCHECK(kEmitCompilerReadBarrier);
6447 DCHECK(kUseBakerReadBarrier);
6448
Roland Levillainbfea3352016-06-23 13:48:47 +01006449 static_assert(
6450 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6451 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006452 // /* HeapReference<Object> */ ref =
6453 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006454 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006455 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006456 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006457}
6458
6459void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6460 Location ref,
6461 Register obj,
6462 uint32_t offset,
6463 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006464 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006465 Location temp,
6466 bool needs_null_check) {
6467 DCHECK(kEmitCompilerReadBarrier);
6468 DCHECK(kUseBakerReadBarrier);
6469
6470 // In slow path based read barriers, the read barrier call is
6471 // inserted after the original load. However, in fast path based
6472 // Baker's read barriers, we need to perform the load of
6473 // mirror::Object::monitor_ *before* the original reference load.
6474 // This load-load ordering is required by the read barrier.
6475 // The fast path/slow path (for Baker's algorithm) should look like:
6476 //
6477 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6478 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6479 // HeapReference<Object> ref = *src; // Original reference load.
6480 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6481 // if (is_gray) {
6482 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6483 // }
6484 //
6485 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006486 // slightly more complex as it performs additional checks that we do
6487 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006488
6489 Register ref_reg = ref.AsRegister<Register>();
6490 Register temp_reg = temp.AsRegister<Register>();
6491 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6492
6493 // /* int32_t */ monitor = obj->monitor_
6494 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6495 if (needs_null_check) {
6496 MaybeRecordImplicitNullCheck(instruction);
6497 }
6498 // /* LockWord */ lock_word = LockWord(monitor)
6499 static_assert(sizeof(LockWord) == sizeof(int32_t),
6500 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006501
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006502 // Introduce a dependency on the lock_word including the rb_state,
6503 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006504 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006505 // `obj` is unchanged by this operation, but its value now depends
6506 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006507 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006508
6509 // The actual reference load.
6510 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006511 // Load types involving an "index": ArrayGet and
6512 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6513 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006514 if (index.IsConstant()) {
6515 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006516 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006517 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6518 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006519 // Handle the special case of the
6520 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6521 // a register pair as index ("long offset"), of which only the low
6522 // part contains data.
6523 Register index_reg = index.IsRegisterPair()
6524 ? index.AsRegisterPairLow<Register>()
6525 : index.AsRegister<Register>();
6526 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006527 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6528 }
6529 } else {
6530 // /* HeapReference<Object> */ ref = *(obj + offset)
6531 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6532 }
6533
6534 // Object* ref = ref_addr->AsMirrorPtr()
6535 __ MaybeUnpoisonHeapReference(ref_reg);
6536
Vladimir Marko953437b2016-08-24 08:30:46 +00006537 // Slow path marking the object `ref` when it is gray.
Roland Levillainc9285912015-12-18 10:38:42 +00006538 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006539 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006540 AddSlowPath(slow_path);
6541
6542 // if (rb_state == ReadBarrier::gray_ptr_)
6543 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006544 // Given the numeric representation, it's enough to check the low bit of the
6545 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6546 // which can be a 16-bit instruction unlike the TST immediate.
6547 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6548 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6549 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6550 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6551 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006552 __ Bind(slow_path->GetExitLabel());
6553}
6554
6555void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6556 Location out,
6557 Location ref,
6558 Location obj,
6559 uint32_t offset,
6560 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006561 DCHECK(kEmitCompilerReadBarrier);
6562
Roland Levillainc9285912015-12-18 10:38:42 +00006563 // Insert a slow path based read barrier *after* the reference load.
6564 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006565 // If heap poisoning is enabled, the unpoisoning of the loaded
6566 // reference will be carried out by the runtime within the slow
6567 // path.
6568 //
6569 // Note that `ref` currently does not get unpoisoned (when heap
6570 // poisoning is enabled), which is alright as the `ref` argument is
6571 // not used by the artReadBarrierSlow entry point.
6572 //
6573 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6574 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6575 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6576 AddSlowPath(slow_path);
6577
Roland Levillain3b359c72015-11-17 19:35:12 +00006578 __ b(slow_path->GetEntryLabel());
6579 __ Bind(slow_path->GetExitLabel());
6580}
6581
Roland Levillainc9285912015-12-18 10:38:42 +00006582void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6583 Location out,
6584 Location ref,
6585 Location obj,
6586 uint32_t offset,
6587 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006588 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006589 // Baker's read barriers shall be handled by the fast path
6590 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6591 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006592 // If heap poisoning is enabled, unpoisoning will be taken care of
6593 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006594 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006595 } else if (kPoisonHeapReferences) {
6596 __ UnpoisonHeapReference(out.AsRegister<Register>());
6597 }
6598}
6599
Roland Levillainc9285912015-12-18 10:38:42 +00006600void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6601 Location out,
6602 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006603 DCHECK(kEmitCompilerReadBarrier);
6604
Roland Levillainc9285912015-12-18 10:38:42 +00006605 // Insert a slow path based read barrier *after* the GC root load.
6606 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006607 // Note that GC roots are not affected by heap poisoning, so we do
6608 // not need to do anything special for this here.
6609 SlowPathCode* slow_path =
6610 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6611 AddSlowPath(slow_path);
6612
Roland Levillain3b359c72015-11-17 19:35:12 +00006613 __ b(slow_path->GetEntryLabel());
6614 __ Bind(slow_path->GetExitLabel());
6615}
6616
Vladimir Markodc151b22015-10-15 18:02:30 +01006617HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6618 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6619 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006620 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6621 // We disable pc-relative load when there is an irreducible loop, as the optimization
6622 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006623 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6624 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006625 if (GetGraph()->HasIrreducibleLoops() &&
6626 (dispatch_info.method_load_kind ==
6627 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6628 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6629 }
6630
6631 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006632 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6633 if (&outer_dex_file != target_method.dex_file) {
6634 // Calls across dex files are more likely to exceed the available BL range,
6635 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6636 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6637 (desired_dispatch_info.method_load_kind ==
6638 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6639 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6640 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6641 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006642 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006643 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006644 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006645 0u
6646 };
6647 }
6648 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006649 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006650}
6651
Vladimir Markob4536b72015-11-24 13:45:23 +00006652Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6653 Register temp) {
6654 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6655 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6656 if (!invoke->GetLocations()->Intrinsified()) {
6657 return location.AsRegister<Register>();
6658 }
6659 // For intrinsics we allow any location, so it may be on the stack.
6660 if (!location.IsRegister()) {
6661 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6662 return temp;
6663 }
6664 // For register locations, check if the register was saved. If so, get it from the stack.
6665 // Note: There is a chance that the register was saved but not overwritten, so we could
6666 // save one load. However, since this is just an intrinsic slow path we prefer this
6667 // simple and more robust approach rather that trying to determine if that's the case.
6668 SlowPathCode* slow_path = GetCurrentSlowPath();
6669 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6670 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6671 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6672 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6673 return temp;
6674 }
6675 return location.AsRegister<Register>();
6676}
6677
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006678void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006679 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006680 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006681 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6682 // LR = code address from literal pool with link-time patch.
6683 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006684 break;
6685 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6686 // LR = invoke->GetDirectCodePtr();
6687 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006688 break;
6689 default:
6690 break;
6691 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006692
Vladimir Marko58155012015-08-19 12:49:41 +00006693 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6694 switch (invoke->GetMethodLoadKind()) {
6695 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6696 // temp = thread->string_init_entrypoint
6697 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6698 break;
6699 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006700 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006701 break;
6702 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6703 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6704 break;
6705 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6706 __ LoadLiteral(temp.AsRegister<Register>(),
6707 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6708 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006709 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6710 HArmDexCacheArraysBase* base =
6711 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6712 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6713 temp.AsRegister<Register>());
6714 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6715 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6716 break;
6717 }
Vladimir Marko58155012015-08-19 12:49:41 +00006718 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006719 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006720 Register method_reg;
6721 Register reg = temp.AsRegister<Register>();
6722 if (current_method.IsRegister()) {
6723 method_reg = current_method.AsRegister<Register>();
6724 } else {
6725 DCHECK(invoke->GetLocations()->Intrinsified());
6726 DCHECK(!current_method.IsValid());
6727 method_reg = reg;
6728 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6729 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006730 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6731 __ LoadFromOffset(kLoadWord,
6732 reg,
6733 method_reg,
6734 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006735 // temp = temp[index_in_cache];
6736 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6737 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006738 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6739 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006740 }
Vladimir Marko58155012015-08-19 12:49:41 +00006741 }
6742
6743 switch (invoke->GetCodePtrLocation()) {
6744 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6745 __ bl(GetFrameEntryLabel());
6746 break;
6747 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006748 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006749 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006750 // Arbitrarily branch to the BL itself, override at link time.
6751 __ bl(&relative_call_patches_.back().label);
6752 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006753 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6754 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6755 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006756 // LR()
6757 __ blx(LR);
6758 break;
6759 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6760 // LR = callee_method->entry_point_from_quick_compiled_code_
6761 __ LoadFromOffset(
6762 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006763 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006764 // LR()
6765 __ blx(LR);
6766 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006767 }
6768
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006769 DCHECK(!IsLeafMethod());
6770}
6771
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006772void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6773 Register temp = temp_location.AsRegister<Register>();
6774 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6775 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006776
6777 // Use the calling convention instead of the location of the receiver, as
6778 // intrinsics may have put the receiver in a different register. In the intrinsics
6779 // slow path, the arguments have been moved to the right place, so here we are
6780 // guaranteed that the receiver is the first register of the calling convention.
6781 InvokeDexCallingConvention calling_convention;
6782 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006783 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006784 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006785 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006786 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006787 // Instead of simply (possibly) unpoisoning `temp` here, we should
6788 // emit a read barrier for the previous class reference load.
6789 // However this is not required in practice, as this is an
6790 // intermediate/temporary reference and because the current
6791 // concurrent copying collector keeps the from-space memory
6792 // intact/accessible until the end of the marking phase (the
6793 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006794 __ MaybeUnpoisonHeapReference(temp);
6795 // temp = temp->GetMethodAt(method_offset);
6796 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006797 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006798 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6799 // LR = temp->GetEntryPoint();
6800 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6801 // LR();
6802 __ blx(LR);
6803}
6804
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006805CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6806 const DexFile& dex_file, uint32_t string_index) {
6807 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6808}
6809
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006810CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6811 const DexFile& dex_file, uint32_t type_index) {
6812 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6813}
6814
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006815CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6816 const DexFile& dex_file, uint32_t element_offset) {
6817 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6818}
6819
6820CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6821 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6822 patches->emplace_back(dex_file, offset_or_index);
6823 return &patches->back();
6824}
6825
6826Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6827 uint32_t string_index) {
6828 return boot_image_string_patches_.GetOrCreate(
6829 StringReference(&dex_file, string_index),
6830 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6831}
6832
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006833Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6834 uint32_t type_index) {
6835 return boot_image_type_patches_.GetOrCreate(
6836 TypeReference(&dex_file, type_index),
6837 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6838}
6839
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006840Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6841 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6842 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6843 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6844}
6845
6846Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6847 return DeduplicateUint32Literal(address, &uint32_literals_);
6848}
6849
Vladimir Marko58155012015-08-19 12:49:41 +00006850void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6851 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006852 size_t size =
6853 method_patches_.size() +
6854 call_patches_.size() +
6855 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006856 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6857 boot_image_string_patches_.size() +
6858 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006859 boot_image_type_patches_.size() +
6860 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006861 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006862 linker_patches->reserve(size);
6863 for (const auto& entry : method_patches_) {
6864 const MethodReference& target_method = entry.first;
6865 Literal* literal = entry.second;
6866 DCHECK(literal->GetLabel()->IsBound());
6867 uint32_t literal_offset = literal->GetLabel()->Position();
6868 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6869 target_method.dex_file,
6870 target_method.dex_method_index));
6871 }
6872 for (const auto& entry : call_patches_) {
6873 const MethodReference& target_method = entry.first;
6874 Literal* literal = entry.second;
6875 DCHECK(literal->GetLabel()->IsBound());
6876 uint32_t literal_offset = literal->GetLabel()->Position();
6877 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6878 target_method.dex_file,
6879 target_method.dex_method_index));
6880 }
6881 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6882 uint32_t literal_offset = info.label.Position();
6883 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6884 info.target_method.dex_file,
6885 info.target_method.dex_method_index));
6886 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006887 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6888 const DexFile& dex_file = info.target_dex_file;
6889 size_t base_element_offset = info.offset_or_index;
6890 DCHECK(info.add_pc_label.IsBound());
6891 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006892 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006893 DCHECK(info.movw_label.IsBound());
6894 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006895 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6896 &dex_file,
6897 add_pc_offset,
6898 base_element_offset));
6899 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006900 DCHECK(info.movt_label.IsBound());
6901 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006902 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6903 &dex_file,
6904 add_pc_offset,
6905 base_element_offset));
6906 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006907 for (const auto& entry : boot_image_string_patches_) {
6908 const StringReference& target_string = entry.first;
6909 Literal* literal = entry.second;
6910 DCHECK(literal->GetLabel()->IsBound());
6911 uint32_t literal_offset = literal->GetLabel()->Position();
6912 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6913 target_string.dex_file,
6914 target_string.string_index));
6915 }
6916 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6917 const DexFile& dex_file = info.target_dex_file;
6918 uint32_t string_index = info.offset_or_index;
6919 DCHECK(info.add_pc_label.IsBound());
6920 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6921 // Add MOVW patch.
6922 DCHECK(info.movw_label.IsBound());
6923 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6924 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6925 &dex_file,
6926 add_pc_offset,
6927 string_index));
6928 // Add MOVT patch.
6929 DCHECK(info.movt_label.IsBound());
6930 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6931 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6932 &dex_file,
6933 add_pc_offset,
6934 string_index));
6935 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006936 for (const auto& entry : boot_image_type_patches_) {
6937 const TypeReference& target_type = entry.first;
6938 Literal* literal = entry.second;
6939 DCHECK(literal->GetLabel()->IsBound());
6940 uint32_t literal_offset = literal->GetLabel()->Position();
6941 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6942 target_type.dex_file,
6943 target_type.type_index));
6944 }
6945 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6946 const DexFile& dex_file = info.target_dex_file;
6947 uint32_t type_index = info.offset_or_index;
6948 DCHECK(info.add_pc_label.IsBound());
6949 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6950 // Add MOVW patch.
6951 DCHECK(info.movw_label.IsBound());
6952 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6953 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6954 &dex_file,
6955 add_pc_offset,
6956 type_index));
6957 // Add MOVT patch.
6958 DCHECK(info.movt_label.IsBound());
6959 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6960 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6961 &dex_file,
6962 add_pc_offset,
6963 type_index));
6964 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006965 for (const auto& entry : boot_image_address_patches_) {
6966 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6967 Literal* literal = entry.second;
6968 DCHECK(literal->GetLabel()->IsBound());
6969 uint32_t literal_offset = literal->GetLabel()->Position();
6970 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6971 }
6972}
6973
6974Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6975 return map->GetOrCreate(
6976 value,
6977 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006978}
6979
6980Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6981 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006982 return map->GetOrCreate(
6983 target_method,
6984 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006985}
6986
6987Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6988 return DeduplicateMethodLiteral(target_method, &method_patches_);
6989}
6990
6991Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6992 return DeduplicateMethodLiteral(target_method, &call_patches_);
6993}
6994
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006995void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6996 LocationSummary* locations =
6997 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6998 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6999 Location::RequiresRegister());
7000 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7001 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7002 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7003}
7004
7005void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7006 LocationSummary* locations = instr->GetLocations();
7007 Register res = locations->Out().AsRegister<Register>();
7008 Register accumulator =
7009 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7010 Register mul_left =
7011 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7012 Register mul_right =
7013 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7014
7015 if (instr->GetOpKind() == HInstruction::kAdd) {
7016 __ mla(res, mul_left, mul_right, accumulator);
7017 } else {
7018 __ mls(res, mul_left, mul_right, accumulator);
7019 }
7020}
7021
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007022void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007023 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007024 LOG(FATAL) << "Unreachable";
7025}
7026
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007027void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007028 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007029 LOG(FATAL) << "Unreachable";
7030}
7031
Mark Mendellfe57faa2015-09-18 09:26:15 -04007032// Simple implementation of packed switch - generate cascaded compare/jumps.
7033void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7034 LocationSummary* locations =
7035 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7036 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007037 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007038 codegen_->GetAssembler()->IsThumb()) {
7039 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7040 if (switch_instr->GetStartValue() != 0) {
7041 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7042 }
7043 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007044}
7045
7046void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7047 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007048 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007049 LocationSummary* locations = switch_instr->GetLocations();
7050 Register value_reg = locations->InAt(0).AsRegister<Register>();
7051 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7052
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007053 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007054 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007055 Register temp_reg = IP;
7056 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7057 // the immediate, because IP is used as the destination register. For the other
7058 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7059 // and they can be encoded in the instruction without making use of IP register.
7060 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7061
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007062 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007063 // Jump to successors[0] if value == lower_bound.
7064 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7065 int32_t last_index = 0;
7066 for (; num_entries - last_index > 2; last_index += 2) {
7067 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7068 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7069 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7070 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7071 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7072 }
7073 if (num_entries - last_index == 2) {
7074 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007075 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007076 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007077 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007078
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007079 // And the default for any other value.
7080 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7081 __ b(codegen_->GetLabelOf(default_block));
7082 }
7083 } else {
7084 // Create a table lookup.
7085 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7086
7087 // Materialize a pointer to the switch table
7088 std::vector<Label*> labels(num_entries);
7089 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7090 for (uint32_t i = 0; i < num_entries; i++) {
7091 labels[i] = codegen_->GetLabelOf(successors[i]);
7092 }
7093 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7094
7095 // Remove the bias.
7096 Register key_reg;
7097 if (lower_bound != 0) {
7098 key_reg = locations->GetTemp(1).AsRegister<Register>();
7099 __ AddConstant(key_reg, value_reg, -lower_bound);
7100 } else {
7101 key_reg = value_reg;
7102 }
7103
7104 // Check whether the value is in the table, jump to default block if not.
7105 __ CmpConstant(key_reg, num_entries - 1);
7106 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7107
7108 // Load the displacement from the table.
7109 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7110
7111 // Dispatch is a direct add to the PC (for Thumb2).
7112 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007113 }
7114}
7115
Vladimir Markob4536b72015-11-24 13:45:23 +00007116void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7118 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007119}
7120
7121void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7122 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007123 CodeGeneratorARM::PcRelativePatchInfo* labels =
7124 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007125 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007126 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007127 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007128 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007129 __ BindTrackedLabel(&labels->add_pc_label);
7130 __ add(base_reg, base_reg, ShifterOperand(PC));
7131}
7132
Andreas Gampe85b62f22015-09-09 13:15:38 -07007133void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7134 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007135 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007136 return;
7137 }
7138
7139 DCHECK_NE(type, Primitive::kPrimVoid);
7140
7141 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7142 if (return_loc.Equals(trg)) {
7143 return;
7144 }
7145
7146 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7147 // with the last branch.
7148 if (type == Primitive::kPrimLong) {
7149 HParallelMove parallel_move(GetGraph()->GetArena());
7150 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7151 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7152 GetMoveResolver()->EmitNativeCode(&parallel_move);
7153 } else if (type == Primitive::kPrimDouble) {
7154 HParallelMove parallel_move(GetGraph()->GetArena());
7155 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7156 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7157 GetMoveResolver()->EmitNativeCode(&parallel_move);
7158 } else {
7159 // Let the parallel move resolver take care of all of this.
7160 HParallelMove parallel_move(GetGraph()->GetArena());
7161 parallel_move.AddMove(return_loc, trg, type, nullptr);
7162 GetMoveResolver()->EmitNativeCode(&parallel_move);
7163 }
7164}
7165
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007166void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7167 LocationSummary* locations =
7168 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7169 locations->SetInAt(0, Location::RequiresRegister());
7170 locations->SetOut(Location::RequiresRegister());
7171}
7172
7173void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7174 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007175 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007176 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007177 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007178 __ LoadFromOffset(kLoadWord,
7179 locations->Out().AsRegister<Register>(),
7180 locations->InAt(0).AsRegister<Register>(),
7181 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007182 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007183 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007184 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007185 __ LoadFromOffset(kLoadWord,
7186 locations->Out().AsRegister<Register>(),
7187 locations->InAt(0).AsRegister<Register>(),
7188 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7189 __ LoadFromOffset(kLoadWord,
7190 locations->Out().AsRegister<Register>(),
7191 locations->Out().AsRegister<Register>(),
7192 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007193 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007194}
7195
Roland Levillain4d027112015-07-01 15:41:14 +01007196#undef __
7197#undef QUICK_ENTRY_POINT
7198
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007199} // namespace arm
7200} // namespace art