blob: e60b781b78a63576579e5f4de7ef3c4e9d9bd6a3 [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 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010077 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000078 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000079 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 }
81
Alexandre Rames8158f282015-08-07 10:26:17 +010082 bool IsFatal() const OVERRIDE { return true; }
83
Alexandre Rames9931f312015-06-19 14:47:01 +010084 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
85
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
88};
89
Andreas Gampe85b62f22015-09-09 13:15:38 -070090class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000091 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000092 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000093
Alexandre Rames67555f72014-11-18 10:55:16 +000094 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000095 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
96 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000097 if (instruction_->CanThrowIntoCatchBlock()) {
98 // Live registers will be restored in the catch block if caught.
99 SaveLiveRegisters(codegen, instruction_->GetLocations());
100 }
Calin Juravled0d48522014-11-04 16:40:20 +0000101 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000102 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000103 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000104 }
105
Alexandre Rames8158f282015-08-07 10:26:17 +0100106 bool IsFatal() const OVERRIDE { return true; }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000111 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
112};
113
Andreas Gampe85b62f22015-09-09 13:15:38 -0700114class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000115 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000116 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000117 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118
Alexandre Rames67555f72014-11-18 10:55:16 +0000119 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100120 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100122 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000123 QUICK_ENTRY_POINT(pTestSuspend), 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);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100177 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
178 ? QUICK_ENTRY_POINT(pThrowStringBounds)
179 : QUICK_ENTRY_POINT(pThrowArrayBounds);
180 arm_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
181 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());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212 int32_t entry_point_offset = do_clinit_
213 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
214 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000215 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000216 if (do_clinit_) {
217 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
218 } else {
219 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
220 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221
222 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000223 Location out = locations->Out();
224 if (out.IsValid()) {
225 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
227 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000228 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100229 __ b(GetExitLabel());
230 }
231
Alexandre Rames9931f312015-06-19 14:47:01 +0100232 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
233
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000235 // The class this slow path will load.
236 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100237
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 // The instruction where this slow path is happening.
239 // (Might be the load class or an initialization check).
240 HInstruction* const at_;
241
242 // The dex PC of `at_`.
243 const uint32_t dex_pc_;
244
245 // Whether to initialize the class.
246 const bool do_clinit_;
247
248 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100249};
250
Andreas Gampe85b62f22015-09-09 13:15:38 -0700251class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000252 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000253 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254
Alexandre Rames67555f72014-11-18 10:55:16 +0000255 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256 LocationSummary* locations = instruction_->GetLocations();
257 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
258
259 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
260 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000261 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000262
263 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000264 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
265 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000266 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000267 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000268 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000269 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
270
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000272 __ b(GetExitLabel());
273 }
274
Alexandre Rames9931f312015-06-19 14:47:01 +0100275 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
276
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000277 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000278 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
279};
280
Andreas Gampe85b62f22015-09-09 13:15:38 -0700281class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000282 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000283 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100288 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
289 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000290 DCHECK(instruction_->IsCheckCast()
291 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
293 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
294 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000295
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000296 if (!is_fatal_) {
297 SaveLiveRegisters(codegen, locations);
298 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 // We're moving two locations to locations that could overlap, so we need a parallel
301 // move resolver.
302 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000303 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100304 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
309 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100312 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
313 instruction_,
314 instruction_->GetDexPc(),
315 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000316 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700317 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
319 } else {
320 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100321 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
322 instruction_,
323 instruction_->GetDexPc(),
324 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000325 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328 if (!is_fatal_) {
329 RestoreLiveRegisters(codegen, locations);
330 __ b(GetExitLabel());
331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332 }
333
Alexandre Rames9931f312015-06-19 14:47:01 +0100334 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
335
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000336 bool IsFatal() const OVERRIDE { return is_fatal_; }
337
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000339 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
341 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
342};
343
Andreas Gampe85b62f22015-09-09 13:15:38 -0700344class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700345 public:
Aart Bik42249c32016-01-07 15:33:50 -0800346 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000347 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800350 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800353 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000357 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358 }
359
Alexandre Rames9931f312015-06-19 14:47:01 +0100360 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
361
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700362 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700363 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
364};
365
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100366class ArraySetSlowPathARM : public SlowPathCode {
367 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000368 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100369
370 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
371 LocationSummary* locations = instruction_->GetLocations();
372 __ Bind(GetEntryLabel());
373 SaveLiveRegisters(codegen, locations);
374
375 InvokeRuntimeCallingConvention calling_convention;
376 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
377 parallel_move.AddMove(
378 locations->InAt(0),
379 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
380 Primitive::kPrimNot,
381 nullptr);
382 parallel_move.AddMove(
383 locations->InAt(1),
384 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
385 Primitive::kPrimInt,
386 nullptr);
387 parallel_move.AddMove(
388 locations->InAt(2),
389 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
390 Primitive::kPrimNot,
391 nullptr);
392 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
393
394 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
395 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
396 instruction_,
397 instruction_->GetDexPc(),
398 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000399 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100400 RestoreLiveRegisters(codegen, locations);
401 __ b(GetExitLabel());
402 }
403
404 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
405
406 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100407 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
408};
409
Roland Levillainc9285912015-12-18 10:38:42 +0000410// Slow path marking an object during a read barrier.
411class ReadBarrierMarkSlowPathARM : public SlowPathCode {
412 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100413 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
414 : SlowPathCode(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000415 DCHECK(kEmitCompilerReadBarrier);
416 }
417
418 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
419
420 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
421 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100422 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000423 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100424 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000425 DCHECK(instruction_->IsInstanceFieldGet() ||
426 instruction_->IsStaticFieldGet() ||
427 instruction_->IsArrayGet() ||
428 instruction_->IsLoadClass() ||
429 instruction_->IsLoadString() ||
430 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100431 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100432 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
433 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000434 << "Unexpected instruction in read barrier marking slow path: "
435 << instruction_->DebugName();
436
437 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100438 // No need to save live registers; it's taken care of by the
439 // entrypoint. Also, there is no need to update the stack mask,
440 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000441 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100442 DCHECK_NE(reg, SP);
443 DCHECK_NE(reg, LR);
444 DCHECK_NE(reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100445 // IP is used internally by the ReadBarrierMarkRegX entry point
446 // as a temporary, it cannot be the entry point's input/output.
447 DCHECK_NE(reg, IP);
Roland Levillain02b75802016-07-13 11:54:35 +0100448 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
449 // "Compact" slow path, saving two moves.
450 //
451 // Instead of using the standard runtime calling convention (input
452 // and output in R0):
453 //
454 // R0 <- obj
455 // R0 <- ReadBarrierMark(R0)
456 // obj <- R0
457 //
458 // we just use rX (the register holding `obj`) as input and output
459 // of a dedicated entrypoint:
460 //
461 // rX <- ReadBarrierMarkRegX(rX)
462 //
463 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700464 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100465 // This runtime call does not require a stack map.
466 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000467 __ b(GetExitLabel());
468 }
469
470 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000471 const Location obj_;
472
473 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
474};
475
Roland Levillain3b359c72015-11-17 19:35:12 +0000476// Slow path generating a read barrier for a heap reference.
477class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
478 public:
479 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
480 Location out,
481 Location ref,
482 Location obj,
483 uint32_t offset,
484 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000485 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000486 out_(out),
487 ref_(ref),
488 obj_(obj),
489 offset_(offset),
490 index_(index) {
491 DCHECK(kEmitCompilerReadBarrier);
492 // If `obj` is equal to `out` or `ref`, it means the initial object
493 // has been overwritten by (or after) the heap object reference load
494 // to be instrumented, e.g.:
495 //
496 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000497 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000498 //
499 // In that case, we have lost the information about the original
500 // object, and the emitted read barrier cannot work properly.
501 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
502 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
503 }
504
505 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
506 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
507 LocationSummary* locations = instruction_->GetLocations();
508 Register reg_out = out_.AsRegister<Register>();
509 DCHECK(locations->CanCall());
510 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100511 DCHECK(instruction_->IsInstanceFieldGet() ||
512 instruction_->IsStaticFieldGet() ||
513 instruction_->IsArrayGet() ||
514 instruction_->IsInstanceOf() ||
515 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100516 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000517 << "Unexpected instruction in read barrier for heap reference slow path: "
518 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000519
520 __ Bind(GetEntryLabel());
521 SaveLiveRegisters(codegen, locations);
522
523 // We may have to change the index's value, but as `index_` is a
524 // constant member (like other "inputs" of this slow path),
525 // introduce a copy of it, `index`.
526 Location index = index_;
527 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100528 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000529 if (instruction_->IsArrayGet()) {
530 // Compute the actual memory offset and store it in `index`.
531 Register index_reg = index_.AsRegister<Register>();
532 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
533 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
534 // We are about to change the value of `index_reg` (see the
535 // calls to art::arm::Thumb2Assembler::Lsl and
536 // art::arm::Thumb2Assembler::AddConstant below), but it has
537 // not been saved by the previous call to
538 // art::SlowPathCode::SaveLiveRegisters, as it is a
539 // callee-save register --
540 // art::SlowPathCode::SaveLiveRegisters does not consider
541 // callee-save registers, as it has been designed with the
542 // assumption that callee-save registers are supposed to be
543 // handled by the called function. So, as a callee-save
544 // register, `index_reg` _would_ eventually be saved onto
545 // the stack, but it would be too late: we would have
546 // changed its value earlier. Therefore, we manually save
547 // it here into another freely available register,
548 // `free_reg`, chosen of course among the caller-save
549 // registers (as a callee-save `free_reg` register would
550 // exhibit the same problem).
551 //
552 // Note we could have requested a temporary register from
553 // the register allocator instead; but we prefer not to, as
554 // this is a slow path, and we know we can find a
555 // caller-save register that is available.
556 Register free_reg = FindAvailableCallerSaveRegister(codegen);
557 __ Mov(free_reg, index_reg);
558 index_reg = free_reg;
559 index = Location::RegisterLocation(index_reg);
560 } else {
561 // The initial register stored in `index_` has already been
562 // saved in the call to art::SlowPathCode::SaveLiveRegisters
563 // (as it is not a callee-save register), so we can freely
564 // use it.
565 }
566 // Shifting the index value contained in `index_reg` by the scale
567 // factor (2) cannot overflow in practice, as the runtime is
568 // unable to allocate object arrays with a size larger than
569 // 2^26 - 1 (that is, 2^28 - 4 bytes).
570 __ Lsl(index_reg, index_reg, TIMES_4);
571 static_assert(
572 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
573 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
574 __ AddConstant(index_reg, index_reg, offset_);
575 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100576 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
577 // intrinsics, `index_` is not shifted by a scale factor of 2
578 // (as in the case of ArrayGet), as it is actually an offset
579 // to an object field within an object.
580 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000581 DCHECK(instruction_->GetLocations()->Intrinsified());
582 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
583 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
584 << instruction_->AsInvoke()->GetIntrinsic();
585 DCHECK_EQ(offset_, 0U);
586 DCHECK(index_.IsRegisterPair());
587 // UnsafeGet's offset location is a register pair, the low
588 // part contains the correct offset.
589 index = index_.ToLow();
590 }
591 }
592
593 // We're moving two or three locations to locations that could
594 // overlap, so we need a parallel move resolver.
595 InvokeRuntimeCallingConvention calling_convention;
596 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
597 parallel_move.AddMove(ref_,
598 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
599 Primitive::kPrimNot,
600 nullptr);
601 parallel_move.AddMove(obj_,
602 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
603 Primitive::kPrimNot,
604 nullptr);
605 if (index.IsValid()) {
606 parallel_move.AddMove(index,
607 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
608 Primitive::kPrimInt,
609 nullptr);
610 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
611 } else {
612 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
613 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
614 }
615 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
616 instruction_,
617 instruction_->GetDexPc(),
618 this);
619 CheckEntrypointTypes<
620 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
621 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
622
623 RestoreLiveRegisters(codegen, locations);
624 __ b(GetExitLabel());
625 }
626
627 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
628
629 private:
630 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
631 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
632 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
633 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
634 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
635 return static_cast<Register>(i);
636 }
637 }
638 // We shall never fail to find a free caller-save register, as
639 // there are more than two core caller-save registers on ARM
640 // (meaning it is possible to find one which is different from
641 // `ref` and `obj`).
642 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
643 LOG(FATAL) << "Could not find a free caller-save register";
644 UNREACHABLE();
645 }
646
Roland Levillain3b359c72015-11-17 19:35:12 +0000647 const Location out_;
648 const Location ref_;
649 const Location obj_;
650 const uint32_t offset_;
651 // An additional location containing an index to an array.
652 // Only used for HArrayGet and the UnsafeGetObject &
653 // UnsafeGetObjectVolatile intrinsics.
654 const Location index_;
655
656 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
657};
658
659// Slow path generating a read barrier for a GC root.
660class ReadBarrierForRootSlowPathARM : public SlowPathCode {
661 public:
662 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000663 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000664 DCHECK(kEmitCompilerReadBarrier);
665 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000666
667 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
668 LocationSummary* locations = instruction_->GetLocations();
669 Register reg_out = out_.AsRegister<Register>();
670 DCHECK(locations->CanCall());
671 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000672 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
673 << "Unexpected instruction in read barrier for GC root slow path: "
674 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000675
676 __ Bind(GetEntryLabel());
677 SaveLiveRegisters(codegen, locations);
678
679 InvokeRuntimeCallingConvention calling_convention;
680 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
681 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
682 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
683 instruction_,
684 instruction_->GetDexPc(),
685 this);
686 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
687 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
688
689 RestoreLiveRegisters(codegen, locations);
690 __ b(GetExitLabel());
691 }
692
693 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
694
695 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000696 const Location out_;
697 const Location root_;
698
699 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
700};
701
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000702#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100703// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
704#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700705
Aart Bike9f37602015-10-09 11:15:55 -0700706inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700707 switch (cond) {
708 case kCondEQ: return EQ;
709 case kCondNE: return NE;
710 case kCondLT: return LT;
711 case kCondLE: return LE;
712 case kCondGT: return GT;
713 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700714 case kCondB: return LO;
715 case kCondBE: return LS;
716 case kCondA: return HI;
717 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700718 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100719 LOG(FATAL) << "Unreachable";
720 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700721}
722
Aart Bike9f37602015-10-09 11:15:55 -0700723// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100724inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700725 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100726 case kCondEQ: return EQ;
727 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700728 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100729 case kCondLT: return LO;
730 case kCondLE: return LS;
731 case kCondGT: return HI;
732 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700733 // Unsigned remain unchanged.
734 case kCondB: return LO;
735 case kCondBE: return LS;
736 case kCondA: return HI;
737 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700738 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100739 LOG(FATAL) << "Unreachable";
740 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700741}
742
Vladimir Markod6e069b2016-01-18 11:11:01 +0000743inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
744 // The ARM condition codes can express all the necessary branches, see the
745 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
746 // There is no dex instruction or HIR that would need the missing conditions
747 // "equal or unordered" or "not equal".
748 switch (cond) {
749 case kCondEQ: return EQ;
750 case kCondNE: return NE /* unordered */;
751 case kCondLT: return gt_bias ? CC : LT /* unordered */;
752 case kCondLE: return gt_bias ? LS : LE /* unordered */;
753 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
754 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
755 default:
756 LOG(FATAL) << "UNREACHABLE";
757 UNREACHABLE();
758 }
759}
760
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100761void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100762 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100763}
764
765void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100766 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100767}
768
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100769size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
770 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
771 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100772}
773
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100774size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
775 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
776 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100777}
778
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000779size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
780 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
781 return kArmWordSize;
782}
783
784size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
785 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
786 return kArmWordSize;
787}
788
Calin Juravle34166012014-12-19 17:22:29 +0000789CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000790 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100791 const CompilerOptions& compiler_options,
792 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000793 : CodeGenerator(graph,
794 kNumberOfCoreRegisters,
795 kNumberOfSRegisters,
796 kNumberOfRegisterPairs,
797 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
798 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000799 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
800 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100801 compiler_options,
802 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100803 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100804 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100805 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100806 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100807 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000808 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000809 uint32_literals_(std::less<uint32_t>(),
810 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100811 method_patches_(MethodReferenceComparator(),
812 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
813 call_patches_(MethodReferenceComparator(),
814 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000815 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000816 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
817 boot_image_string_patches_(StringReferenceValueComparator(),
818 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
819 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100820 boot_image_type_patches_(TypeReferenceValueComparator(),
821 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
822 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000823 boot_image_address_patches_(std::less<uint32_t>(),
824 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700825 // Always save the LR register to mimic Quick.
826 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100827}
828
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000829void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
830 // Ensure that we fix up branches and literal loads and emit the literal pool.
831 __ FinalizeCode();
832
833 // Adjust native pc offsets in stack maps.
834 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
835 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
836 uint32_t new_position = __ GetAdjustedPosition(old_position);
837 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
838 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100839 // Adjust pc offsets for the disassembly information.
840 if (disasm_info_ != nullptr) {
841 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
842 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
843 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
844 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
845 it.second.start = __ GetAdjustedPosition(it.second.start);
846 it.second.end = __ GetAdjustedPosition(it.second.end);
847 }
848 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
849 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
850 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
851 }
852 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000853
854 CodeGenerator::Finalize(allocator);
855}
856
David Brazdil58282f42016-01-14 12:45:10 +0000857void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100858 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100859 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100860
861 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100862 blocked_core_registers_[SP] = true;
863 blocked_core_registers_[LR] = true;
864 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100865
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100867 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100869 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100870 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100871
David Brazdil58282f42016-01-14 12:45:10 +0000872 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100873 // Stubs do not save callee-save floating point registers. If the graph
874 // is debuggable, we need to deal with these registers differently. For
875 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000876 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
877 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
878 }
879 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100880
881 UpdateBlockedPairRegisters();
882}
883
884void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
885 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
886 ArmManagedRegister current =
887 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
888 if (blocked_core_registers_[current.AsRegisterPairLow()]
889 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
890 blocked_register_pairs_[i] = true;
891 }
892 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100893}
894
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100895InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800896 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100897 assembler_(codegen->GetAssembler()),
898 codegen_(codegen) {}
899
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000900void CodeGeneratorARM::ComputeSpillMask() {
901 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
902 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000903 // There is no easy instruction to restore just the PC on thumb2. We spill and
904 // restore another arbitrary register.
905 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000906 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
907 // We use vpush and vpop for saving and restoring floating point registers, which take
908 // a SRegister and the number of registers to save/restore after that SRegister. We
909 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
910 // but in the range.
911 if (fpu_spill_mask_ != 0) {
912 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
913 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
914 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
915 fpu_spill_mask_ |= (1 << i);
916 }
917 }
918}
919
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100920static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100921 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100922}
923
924static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100925 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100926}
927
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000928void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000929 bool skip_overflow_check =
930 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000931 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000932 __ Bind(&frame_entry_label_);
933
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000934 if (HasEmptyFrame()) {
935 return;
936 }
937
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100938 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000939 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
940 __ LoadFromOffset(kLoadWord, IP, IP, 0);
941 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100942 }
943
Andreas Gampe501fd632015-09-10 16:11:06 -0700944 __ PushList(core_spill_mask_);
945 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
946 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000947 if (fpu_spill_mask_ != 0) {
948 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
949 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100950 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100951 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000952 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100953 int adjust = GetFrameSize() - FrameEntrySpillSize();
954 __ AddConstant(SP, -adjust);
955 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100956 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957}
958
959void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000960 if (HasEmptyFrame()) {
961 __ bx(LR);
962 return;
963 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100964 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100965 int adjust = GetFrameSize() - FrameEntrySpillSize();
966 __ AddConstant(SP, adjust);
967 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000968 if (fpu_spill_mask_ != 0) {
969 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
970 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -0700971 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100972 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000973 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700974 // Pop LR into PC to return.
975 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
976 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
977 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100978 __ cfi().RestoreState();
979 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000980}
981
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100982void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700983 Label* label = GetLabelOf(block);
984 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000985}
986
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100987Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100988 switch (type) {
989 case Primitive::kPrimBoolean:
990 case Primitive::kPrimByte:
991 case Primitive::kPrimChar:
992 case Primitive::kPrimShort:
993 case Primitive::kPrimInt:
994 case Primitive::kPrimNot: {
995 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000996 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100997 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100998 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100999 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001000 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001001 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001002 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001003
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001004 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001006 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001008 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001009 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001010 if (calling_convention.GetRegisterAt(index) == R1) {
1011 // Skip R1, and use R2_R3 instead.
1012 gp_index_++;
1013 index++;
1014 }
1015 }
1016 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1017 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001018 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001019
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001020 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001021 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001022 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001023 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1024 }
1025 }
1026
1027 case Primitive::kPrimFloat: {
1028 uint32_t stack_index = stack_index_++;
1029 if (float_index_ % 2 == 0) {
1030 float_index_ = std::max(double_index_, float_index_);
1031 }
1032 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1033 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1034 } else {
1035 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1036 }
1037 }
1038
1039 case Primitive::kPrimDouble: {
1040 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1041 uint32_t stack_index = stack_index_;
1042 stack_index_ += 2;
1043 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1044 uint32_t index = double_index_;
1045 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001046 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001047 calling_convention.GetFpuRegisterAt(index),
1048 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001049 DCHECK(ExpectedPairLayout(result));
1050 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001051 } else {
1052 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001053 }
1054 }
1055
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001056 case Primitive::kPrimVoid:
1057 LOG(FATAL) << "Unexpected parameter type " << type;
1058 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001059 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001060 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001061}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001062
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001063Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001064 switch (type) {
1065 case Primitive::kPrimBoolean:
1066 case Primitive::kPrimByte:
1067 case Primitive::kPrimChar:
1068 case Primitive::kPrimShort:
1069 case Primitive::kPrimInt:
1070 case Primitive::kPrimNot: {
1071 return Location::RegisterLocation(R0);
1072 }
1073
1074 case Primitive::kPrimFloat: {
1075 return Location::FpuRegisterLocation(S0);
1076 }
1077
1078 case Primitive::kPrimLong: {
1079 return Location::RegisterPairLocation(R0, R1);
1080 }
1081
1082 case Primitive::kPrimDouble: {
1083 return Location::FpuRegisterPairLocation(S0, S1);
1084 }
1085
1086 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001087 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001088 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001089
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001090 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001091}
1092
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001093Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1094 return Location::RegisterLocation(kMethodRegisterArgument);
1095}
1096
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097void CodeGeneratorARM::Move32(Location destination, Location source) {
1098 if (source.Equals(destination)) {
1099 return;
1100 }
1101 if (destination.IsRegister()) {
1102 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001103 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001108 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 } else if (destination.IsFpuRegister()) {
1110 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001113 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001114 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001115 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001118 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001120 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001121 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001122 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001124 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001125 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1126 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 }
1128 }
1129}
1130
1131void CodeGeneratorARM::Move64(Location destination, Location source) {
1132 if (source.Equals(destination)) {
1133 return;
1134 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001135 if (destination.IsRegisterPair()) {
1136 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001137 EmitParallelMoves(
1138 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1139 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001140 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001141 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001142 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1143 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001144 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001145 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001146 } else if (source.IsFpuRegisterPair()) {
1147 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1148 destination.AsRegisterPairHigh<Register>(),
1149 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150 } else {
1151 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001152 DCHECK(ExpectedPairLayout(destination));
1153 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1154 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001156 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001158 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1159 SP,
1160 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001161 } else if (source.IsRegisterPair()) {
1162 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1163 source.AsRegisterPairLow<Register>(),
1164 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001166 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001167 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 } else {
1169 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001171 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001172 if (source.AsRegisterPairLow<Register>() == R1) {
1173 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001174 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1175 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001176 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001177 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001178 SP, destination.GetStackIndex());
1179 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001180 } else if (source.IsFpuRegisterPair()) {
1181 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1182 SP,
1183 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001184 } else {
1185 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001186 EmitParallelMoves(
1187 Location::StackSlot(source.GetStackIndex()),
1188 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001189 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001190 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001191 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1192 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 }
1194 }
1195}
1196
Calin Juravle175dc732015-08-25 15:42:32 +01001197void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1198 DCHECK(location.IsRegister());
1199 __ LoadImmediate(location.AsRegister<Register>(), value);
1200}
1201
Calin Juravlee460d1d2015-09-29 04:52:17 +01001202void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001203 HParallelMove move(GetGraph()->GetArena());
1204 move.AddMove(src, dst, dst_type, nullptr);
1205 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001206}
1207
1208void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1209 if (location.IsRegister()) {
1210 locations->AddTemp(location);
1211 } else if (location.IsRegisterPair()) {
1212 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1213 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1214 } else {
1215 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1216 }
1217}
1218
Calin Juravle175dc732015-08-25 15:42:32 +01001219void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1220 HInstruction* instruction,
1221 uint32_t dex_pc,
1222 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001223 InvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001224 instruction,
1225 dex_pc,
1226 slow_path);
1227}
1228
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001229void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1230 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001231 uint32_t dex_pc,
1232 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001233 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001234 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1235 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001236 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001237}
1238
Roland Levillaindec8f632016-07-22 17:10:06 +01001239void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1240 HInstruction* instruction,
1241 SlowPathCode* slow_path) {
1242 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1243 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1244 __ blx(LR);
1245}
1246
David Brazdilfc6a86a2015-06-26 10:33:45 +00001247void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001248 DCHECK(!successor->IsExitBlock());
1249
1250 HBasicBlock* block = got->GetBlock();
1251 HInstruction* previous = got->GetPrevious();
1252
1253 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001254 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001255 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1256 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1257 return;
1258 }
1259
1260 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1261 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1262 }
1263 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001264 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001265 }
1266}
1267
David Brazdilfc6a86a2015-06-26 10:33:45 +00001268void LocationsBuilderARM::VisitGoto(HGoto* got) {
1269 got->SetLocations(nullptr);
1270}
1271
1272void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1273 HandleGoto(got, got->GetSuccessor());
1274}
1275
1276void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1277 try_boundary->SetLocations(nullptr);
1278}
1279
1280void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1281 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1282 if (!successor->IsExitBlock()) {
1283 HandleGoto(try_boundary, successor);
1284 }
1285}
1286
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001287void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001288 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001289}
1290
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001291void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001292}
1293
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001294void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1295 Primitive::Type type = instruction->InputAt(0)->GetType();
1296 Location lhs_loc = instruction->GetLocations()->InAt(0);
1297 Location rhs_loc = instruction->GetLocations()->InAt(1);
1298 if (rhs_loc.IsConstant()) {
1299 // 0.0 is the only immediate that can be encoded directly in
1300 // a VCMP instruction.
1301 //
1302 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1303 // specify that in a floating-point comparison, positive zero
1304 // and negative zero are considered equal, so we can use the
1305 // literal 0.0 for both cases here.
1306 //
1307 // Note however that some methods (Float.equal, Float.compare,
1308 // Float.compareTo, Double.equal, Double.compare,
1309 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1310 // StrictMath.min) consider 0.0 to be (strictly) greater than
1311 // -0.0. So if we ever translate calls to these methods into a
1312 // HCompare instruction, we must handle the -0.0 case with
1313 // care here.
1314 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1315 if (type == Primitive::kPrimFloat) {
1316 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1317 } else {
1318 DCHECK_EQ(type, Primitive::kPrimDouble);
1319 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1320 }
1321 } else {
1322 if (type == Primitive::kPrimFloat) {
1323 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1324 } else {
1325 DCHECK_EQ(type, Primitive::kPrimDouble);
1326 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1327 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1328 }
1329 }
1330}
1331
Roland Levillain4fa13f62015-07-06 18:11:54 +01001332void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1333 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001334 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001335 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001336 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001337}
1338
1339void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1340 Label* true_label,
1341 Label* false_label) {
1342 LocationSummary* locations = cond->GetLocations();
1343 Location left = locations->InAt(0);
1344 Location right = locations->InAt(1);
1345 IfCondition if_cond = cond->GetCondition();
1346
1347 Register left_high = left.AsRegisterPairHigh<Register>();
1348 Register left_low = left.AsRegisterPairLow<Register>();
1349 IfCondition true_high_cond = if_cond;
1350 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001351 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001352
1353 // Set the conditions for the test, remembering that == needs to be
1354 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001355 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001356 switch (if_cond) {
1357 case kCondEQ:
1358 case kCondNE:
1359 // Nothing to do.
1360 break;
1361 case kCondLT:
1362 false_high_cond = kCondGT;
1363 break;
1364 case kCondLE:
1365 true_high_cond = kCondLT;
1366 break;
1367 case kCondGT:
1368 false_high_cond = kCondLT;
1369 break;
1370 case kCondGE:
1371 true_high_cond = kCondGT;
1372 break;
Aart Bike9f37602015-10-09 11:15:55 -07001373 case kCondB:
1374 false_high_cond = kCondA;
1375 break;
1376 case kCondBE:
1377 true_high_cond = kCondB;
1378 break;
1379 case kCondA:
1380 false_high_cond = kCondB;
1381 break;
1382 case kCondAE:
1383 true_high_cond = kCondA;
1384 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001385 }
1386 if (right.IsConstant()) {
1387 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1388 int32_t val_low = Low32Bits(value);
1389 int32_t val_high = High32Bits(value);
1390
Vladimir Markoac6ac102015-12-17 12:14:00 +00001391 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001392 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001393 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001394 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001395 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001396 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001397 __ b(true_label, ARMCondition(true_high_cond));
1398 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 }
1400 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001401 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 } else {
1403 Register right_high = right.AsRegisterPairHigh<Register>();
1404 Register right_low = right.AsRegisterPairLow<Register>();
1405
1406 __ cmp(left_high, ShifterOperand(right_high));
1407 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001408 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001409 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001410 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001411 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001412 __ b(true_label, ARMCondition(true_high_cond));
1413 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001414 }
1415 // Must be equal high, so compare the lows.
1416 __ cmp(left_low, ShifterOperand(right_low));
1417 }
1418 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001419 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001420 __ b(true_label, final_condition);
1421}
1422
David Brazdil0debae72015-11-12 18:37:00 +00001423void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1424 Label* true_target_in,
1425 Label* false_target_in) {
1426 // Generated branching requires both targets to be explicit. If either of the
1427 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1428 Label fallthrough_target;
1429 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1430 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1431
Roland Levillain4fa13f62015-07-06 18:11:54 +01001432 Primitive::Type type = condition->InputAt(0)->GetType();
1433 switch (type) {
1434 case Primitive::kPrimLong:
1435 GenerateLongComparesAndJumps(condition, true_target, false_target);
1436 break;
1437 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001438 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001439 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001440 GenerateFPJumps(condition, true_target, false_target);
1441 break;
1442 default:
1443 LOG(FATAL) << "Unexpected compare type " << type;
1444 }
1445
David Brazdil0debae72015-11-12 18:37:00 +00001446 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001447 __ b(false_target);
1448 }
David Brazdil0debae72015-11-12 18:37:00 +00001449
1450 if (fallthrough_target.IsLinked()) {
1451 __ Bind(&fallthrough_target);
1452 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001453}
1454
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001455void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001456 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001457 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001458 Label* false_target) {
1459 HInstruction* cond = instruction->InputAt(condition_input_index);
1460
1461 if (true_target == nullptr && false_target == nullptr) {
1462 // Nothing to do. The code always falls through.
1463 return;
1464 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001465 // Constant condition, statically compared against "true" (integer value 1).
1466 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001467 if (true_target != nullptr) {
1468 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001469 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001470 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001471 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001472 if (false_target != nullptr) {
1473 __ b(false_target);
1474 }
1475 }
1476 return;
1477 }
1478
1479 // The following code generates these patterns:
1480 // (1) true_target == nullptr && false_target != nullptr
1481 // - opposite condition true => branch to false_target
1482 // (2) true_target != nullptr && false_target == nullptr
1483 // - condition true => branch to true_target
1484 // (3) true_target != nullptr && false_target != nullptr
1485 // - condition true => branch to true_target
1486 // - branch to false_target
1487 if (IsBooleanValueOrMaterializedCondition(cond)) {
1488 // Condition has been materialized, compare the output to 0.
1489 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1490 DCHECK(cond_val.IsRegister());
1491 if (true_target == nullptr) {
1492 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1493 } else {
1494 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001495 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001496 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001497 // Condition has not been materialized. Use its inputs as the comparison and
1498 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001499 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001500
1501 // If this is a long or FP comparison that has been folded into
1502 // the HCondition, generate the comparison directly.
1503 Primitive::Type type = condition->InputAt(0)->GetType();
1504 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1505 GenerateCompareTestAndBranch(condition, true_target, false_target);
1506 return;
1507 }
1508
1509 LocationSummary* locations = cond->GetLocations();
1510 DCHECK(locations->InAt(0).IsRegister());
1511 Register left = locations->InAt(0).AsRegister<Register>();
1512 Location right = locations->InAt(1);
1513 if (right.IsRegister()) {
1514 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001515 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001516 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001517 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001518 }
1519 if (true_target == nullptr) {
1520 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1521 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001522 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001523 }
Dave Allison20dfc792014-06-16 20:44:29 -07001524 }
David Brazdil0debae72015-11-12 18:37:00 +00001525
1526 // If neither branch falls through (case 3), the conditional branch to `true_target`
1527 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1528 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001529 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001530 }
1531}
1532
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001533void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001534 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1535 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001536 locations->SetInAt(0, Location::RequiresRegister());
1537 }
1538}
1539
1540void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001541 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1542 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1543 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1544 nullptr : codegen_->GetLabelOf(true_successor);
1545 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1546 nullptr : codegen_->GetLabelOf(false_successor);
1547 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001548}
1549
1550void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1551 LocationSummary* locations = new (GetGraph()->GetArena())
1552 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001553 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001554 locations->SetInAt(0, Location::RequiresRegister());
1555 }
1556}
1557
1558void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001559 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001560 GenerateTestAndBranch(deoptimize,
1561 /* condition_input_index */ 0,
1562 slow_path->GetEntryLabel(),
1563 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001564}
Dave Allison20dfc792014-06-16 20:44:29 -07001565
David Brazdil74eb1b22015-12-14 11:44:01 +00001566void LocationsBuilderARM::VisitSelect(HSelect* select) {
1567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1568 if (Primitive::IsFloatingPointType(select->GetType())) {
1569 locations->SetInAt(0, Location::RequiresFpuRegister());
1570 locations->SetInAt(1, Location::RequiresFpuRegister());
1571 } else {
1572 locations->SetInAt(0, Location::RequiresRegister());
1573 locations->SetInAt(1, Location::RequiresRegister());
1574 }
1575 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1576 locations->SetInAt(2, Location::RequiresRegister());
1577 }
1578 locations->SetOut(Location::SameAsFirstInput());
1579}
1580
1581void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1582 LocationSummary* locations = select->GetLocations();
1583 Label false_target;
1584 GenerateTestAndBranch(select,
1585 /* condition_input_index */ 2,
1586 /* true_target */ nullptr,
1587 &false_target);
1588 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1589 __ Bind(&false_target);
1590}
1591
David Srbecky0cf44932015-12-09 14:09:59 +00001592void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1593 new (GetGraph()->GetArena()) LocationSummary(info);
1594}
1595
David Srbeckyd28f4a02016-03-14 17:14:24 +00001596void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1597 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001598}
1599
1600void CodeGeneratorARM::GenerateNop() {
1601 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001602}
1603
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001604void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001605 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001606 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001607 // Handle the long/FP comparisons made in instruction simplification.
1608 switch (cond->InputAt(0)->GetType()) {
1609 case Primitive::kPrimLong:
1610 locations->SetInAt(0, Location::RequiresRegister());
1611 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001612 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001613 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1614 }
1615 break;
1616
1617 case Primitive::kPrimFloat:
1618 case Primitive::kPrimDouble:
1619 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001620 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001621 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1623 }
1624 break;
1625
1626 default:
1627 locations->SetInAt(0, Location::RequiresRegister());
1628 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001629 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1631 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001632 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001633}
1634
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001635void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001636 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001637 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001638 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001639
1640 LocationSummary* locations = cond->GetLocations();
1641 Location left = locations->InAt(0);
1642 Location right = locations->InAt(1);
1643 Register out = locations->Out().AsRegister<Register>();
1644 Label true_label, false_label;
1645
1646 switch (cond->InputAt(0)->GetType()) {
1647 default: {
1648 // Integer case.
1649 if (right.IsRegister()) {
1650 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1651 } else {
1652 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001653 __ CmpConstant(left.AsRegister<Register>(),
1654 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001655 }
Aart Bike9f37602015-10-09 11:15:55 -07001656 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001657 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001658 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001659 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001660 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001661 return;
1662 }
1663 case Primitive::kPrimLong:
1664 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1665 break;
1666 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001667 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001668 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001669 GenerateFPJumps(cond, &true_label, &false_label);
1670 break;
1671 }
1672
1673 // Convert the jumps into the result.
1674 Label done_label;
1675
1676 // False case: result = 0.
1677 __ Bind(&false_label);
1678 __ LoadImmediate(out, 0);
1679 __ b(&done_label);
1680
1681 // True case: result = 1.
1682 __ Bind(&true_label);
1683 __ LoadImmediate(out, 1);
1684 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001685}
1686
1687void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001688 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001689}
1690
1691void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001692 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001693}
1694
1695void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001696 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001697}
1698
1699void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001700 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001701}
1702
1703void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001704 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001705}
1706
1707void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001708 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001709}
1710
1711void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001712 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001713}
1714
1715void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001716 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001717}
1718
1719void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001720 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001721}
1722
1723void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001724 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001725}
1726
1727void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001728 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001729}
1730
1731void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001732 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001733}
1734
Aart Bike9f37602015-10-09 11:15:55 -07001735void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001736 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001737}
1738
1739void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001740 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001741}
1742
1743void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001744 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001745}
1746
1747void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001748 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001749}
1750
1751void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001752 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001753}
1754
1755void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001756 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001757}
1758
1759void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001760 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001761}
1762
1763void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001764 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001765}
1766
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001767void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001768 LocationSummary* locations =
1769 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001770 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001771}
1772
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001773void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001774 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001775}
1776
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001777void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1778 LocationSummary* locations =
1779 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1780 locations->SetOut(Location::ConstantLocation(constant));
1781}
1782
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001783void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001784 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001785}
1786
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001787void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001790 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001791}
1792
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001793void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001794 // Will be generated at use site.
1795}
1796
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001797void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1800 locations->SetOut(Location::ConstantLocation(constant));
1801}
1802
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001803void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001804 // Will be generated at use site.
1805}
1806
1807void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1808 LocationSummary* locations =
1809 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1810 locations->SetOut(Location::ConstantLocation(constant));
1811}
1812
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001813void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001814 // Will be generated at use site.
1815}
1816
Calin Juravle27df7582015-04-17 19:12:31 +01001817void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1818 memory_barrier->SetLocations(nullptr);
1819}
1820
1821void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001822 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001823}
1824
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001825void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001826 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001827}
1828
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001829void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001830 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001831}
1832
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001833void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001834 LocationSummary* locations =
1835 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001836 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837}
1838
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001839void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001840 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001841}
1842
Calin Juravle175dc732015-08-25 15:42:32 +01001843void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1844 // The trampoline uses the same calling convention as dex calling conventions,
1845 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1846 // the method_idx.
1847 HandleInvoke(invoke);
1848}
1849
1850void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1851 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1852}
1853
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001854void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001855 // Explicit clinit checks triggered by static invokes must have been pruned by
1856 // art::PrepareForRegisterAllocation.
1857 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001858
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001859 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001860 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001861 codegen_->GetInstructionSetFeatures());
1862 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001863 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1864 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1865 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001866 return;
1867 }
1868
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001869 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001870
1871 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1872 if (invoke->HasPcRelativeDexCache()) {
1873 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1874 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001875}
1876
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001877static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1878 if (invoke->GetLocations()->Intrinsified()) {
1879 IntrinsicCodeGeneratorARM intrinsic(codegen);
1880 intrinsic.Dispatch(invoke);
1881 return true;
1882 }
1883 return false;
1884}
1885
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001886void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001887 // Explicit clinit checks triggered by static invokes must have been pruned by
1888 // art::PrepareForRegisterAllocation.
1889 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001890
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001891 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1892 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001893 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001894
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001895 LocationSummary* locations = invoke->GetLocations();
1896 codegen_->GenerateStaticOrDirectCall(
1897 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001898 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001899}
1900
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001901void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001902 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001903 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001904}
1905
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001906void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001907 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001908 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001909 codegen_->GetInstructionSetFeatures());
1910 if (intrinsic.TryDispatch(invoke)) {
1911 return;
1912 }
1913
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001914 HandleInvoke(invoke);
1915}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001916
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001917void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001918 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1919 return;
1920 }
1921
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001922 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001923 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001924 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001925}
1926
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001927void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1928 HandleInvoke(invoke);
1929 // Add the hidden argument.
1930 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1931}
1932
1933void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1934 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001935 LocationSummary* locations = invoke->GetLocations();
1936 Register temp = locations->GetTemp(0).AsRegister<Register>();
1937 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001938 Location receiver = locations->InAt(0);
1939 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1940
Roland Levillain3b359c72015-11-17 19:35:12 +00001941 // Set the hidden argument. This is safe to do this here, as R12
1942 // won't be modified thereafter, before the `blx` (call) instruction.
1943 DCHECK_EQ(R12, hidden_reg);
1944 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001945
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001946 if (receiver.IsStackSlot()) {
1947 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001948 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001949 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1950 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001951 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001952 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001953 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001954 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001955 // Instead of simply (possibly) unpoisoning `temp` here, we should
1956 // emit a read barrier for the previous class reference load.
1957 // However this is not required in practice, as this is an
1958 // intermediate/temporary reference and because the current
1959 // concurrent copying collector keeps the from-space memory
1960 // intact/accessible until the end of the marking phase (the
1961 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001962 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001963 __ LoadFromOffset(kLoadWord, temp, temp,
1964 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1965 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00001966 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001967 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001968 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001969 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07001970 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001971 // LR = temp->GetEntryPoint();
1972 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1973 // LR();
1974 __ blx(LR);
1975 DCHECK(!codegen_->IsLeafMethod());
1976 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1977}
1978
Roland Levillain88cb1752014-10-20 16:36:47 +01001979void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1980 LocationSummary* locations =
1981 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1982 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001983 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001984 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1986 break;
1987 }
1988 case Primitive::kPrimLong: {
1989 locations->SetInAt(0, Location::RequiresRegister());
1990 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001991 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001992 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001993
Roland Levillain88cb1752014-10-20 16:36:47 +01001994 case Primitive::kPrimFloat:
1995 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001996 locations->SetInAt(0, Location::RequiresFpuRegister());
1997 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001998 break;
1999
2000 default:
2001 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2002 }
2003}
2004
2005void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2006 LocationSummary* locations = neg->GetLocations();
2007 Location out = locations->Out();
2008 Location in = locations->InAt(0);
2009 switch (neg->GetResultType()) {
2010 case Primitive::kPrimInt:
2011 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002013 break;
2014
2015 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002016 DCHECK(in.IsRegisterPair());
2017 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2018 __ rsbs(out.AsRegisterPairLow<Register>(),
2019 in.AsRegisterPairLow<Register>(),
2020 ShifterOperand(0));
2021 // We cannot emit an RSC (Reverse Subtract with Carry)
2022 // instruction here, as it does not exist in the Thumb-2
2023 // instruction set. We use the following approach
2024 // using SBC and SUB instead.
2025 //
2026 // out.hi = -C
2027 __ sbc(out.AsRegisterPairHigh<Register>(),
2028 out.AsRegisterPairHigh<Register>(),
2029 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2030 // out.hi = out.hi - in.hi
2031 __ sub(out.AsRegisterPairHigh<Register>(),
2032 out.AsRegisterPairHigh<Register>(),
2033 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2034 break;
2035
Roland Levillain88cb1752014-10-20 16:36:47 +01002036 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002037 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002038 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002039 break;
2040
Roland Levillain88cb1752014-10-20 16:36:47 +01002041 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002042 DCHECK(in.IsFpuRegisterPair());
2043 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2044 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002045 break;
2046
2047 default:
2048 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2049 }
2050}
2051
Roland Levillaindff1f282014-11-05 14:15:05 +00002052void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002053 Primitive::Type result_type = conversion->GetResultType();
2054 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002055 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002056
Roland Levillain5b3ee562015-04-14 16:02:41 +01002057 // The float-to-long, double-to-long and long-to-float type conversions
2058 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002059 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002060 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2061 && result_type == Primitive::kPrimLong)
2062 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002063 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002064 : LocationSummary::kNoCall;
2065 LocationSummary* locations =
2066 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2067
David Brazdilb2bd1c52015-03-25 11:17:37 +00002068 // The Java language does not allow treating boolean as an integral type but
2069 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002070
Roland Levillaindff1f282014-11-05 14:15:05 +00002071 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002072 case Primitive::kPrimByte:
2073 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002074 case Primitive::kPrimLong:
2075 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002076 case Primitive::kPrimBoolean:
2077 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002078 case Primitive::kPrimShort:
2079 case Primitive::kPrimInt:
2080 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002081 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002082 locations->SetInAt(0, Location::RequiresRegister());
2083 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2084 break;
2085
2086 default:
2087 LOG(FATAL) << "Unexpected type conversion from " << input_type
2088 << " to " << result_type;
2089 }
2090 break;
2091
Roland Levillain01a8d712014-11-14 16:27:39 +00002092 case Primitive::kPrimShort:
2093 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002094 case Primitive::kPrimLong:
2095 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002096 case Primitive::kPrimBoolean:
2097 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002098 case Primitive::kPrimByte:
2099 case Primitive::kPrimInt:
2100 case Primitive::kPrimChar:
2101 // Processing a Dex `int-to-short' instruction.
2102 locations->SetInAt(0, Location::RequiresRegister());
2103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2104 break;
2105
2106 default:
2107 LOG(FATAL) << "Unexpected type conversion from " << input_type
2108 << " to " << result_type;
2109 }
2110 break;
2111
Roland Levillain946e1432014-11-11 17:35:19 +00002112 case Primitive::kPrimInt:
2113 switch (input_type) {
2114 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002115 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002116 locations->SetInAt(0, Location::Any());
2117 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2118 break;
2119
2120 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002121 // Processing a Dex `float-to-int' instruction.
2122 locations->SetInAt(0, Location::RequiresFpuRegister());
2123 locations->SetOut(Location::RequiresRegister());
2124 locations->AddTemp(Location::RequiresFpuRegister());
2125 break;
2126
Roland Levillain946e1432014-11-11 17:35:19 +00002127 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002128 // Processing a Dex `double-to-int' instruction.
2129 locations->SetInAt(0, Location::RequiresFpuRegister());
2130 locations->SetOut(Location::RequiresRegister());
2131 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002132 break;
2133
2134 default:
2135 LOG(FATAL) << "Unexpected type conversion from " << input_type
2136 << " to " << result_type;
2137 }
2138 break;
2139
Roland Levillaindff1f282014-11-05 14:15:05 +00002140 case Primitive::kPrimLong:
2141 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002142 case Primitive::kPrimBoolean:
2143 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002144 case Primitive::kPrimByte:
2145 case Primitive::kPrimShort:
2146 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002147 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002148 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002149 locations->SetInAt(0, Location::RequiresRegister());
2150 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2151 break;
2152
Roland Levillain624279f2014-12-04 11:54:28 +00002153 case Primitive::kPrimFloat: {
2154 // Processing a Dex `float-to-long' instruction.
2155 InvokeRuntimeCallingConvention calling_convention;
2156 locations->SetInAt(0, Location::FpuRegisterLocation(
2157 calling_convention.GetFpuRegisterAt(0)));
2158 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2159 break;
2160 }
2161
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002162 case Primitive::kPrimDouble: {
2163 // Processing a Dex `double-to-long' instruction.
2164 InvokeRuntimeCallingConvention calling_convention;
2165 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2166 calling_convention.GetFpuRegisterAt(0),
2167 calling_convention.GetFpuRegisterAt(1)));
2168 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002169 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002170 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002171
2172 default:
2173 LOG(FATAL) << "Unexpected type conversion from " << input_type
2174 << " to " << result_type;
2175 }
2176 break;
2177
Roland Levillain981e4542014-11-14 11:47:14 +00002178 case Primitive::kPrimChar:
2179 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002180 case Primitive::kPrimLong:
2181 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002182 case Primitive::kPrimBoolean:
2183 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002184 case Primitive::kPrimByte:
2185 case Primitive::kPrimShort:
2186 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002187 // Processing a Dex `int-to-char' instruction.
2188 locations->SetInAt(0, Location::RequiresRegister());
2189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2190 break;
2191
2192 default:
2193 LOG(FATAL) << "Unexpected type conversion from " << input_type
2194 << " to " << result_type;
2195 }
2196 break;
2197
Roland Levillaindff1f282014-11-05 14:15:05 +00002198 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002199 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002200 case Primitive::kPrimBoolean:
2201 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002202 case Primitive::kPrimByte:
2203 case Primitive::kPrimShort:
2204 case Primitive::kPrimInt:
2205 case Primitive::kPrimChar:
2206 // Processing a Dex `int-to-float' instruction.
2207 locations->SetInAt(0, Location::RequiresRegister());
2208 locations->SetOut(Location::RequiresFpuRegister());
2209 break;
2210
Roland Levillain5b3ee562015-04-14 16:02:41 +01002211 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002212 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002213 InvokeRuntimeCallingConvention calling_convention;
2214 locations->SetInAt(0, Location::RegisterPairLocation(
2215 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2216 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002217 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002218 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002219
Roland Levillaincff13742014-11-17 14:32:17 +00002220 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002221 // Processing a Dex `double-to-float' instruction.
2222 locations->SetInAt(0, Location::RequiresFpuRegister());
2223 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002224 break;
2225
2226 default:
2227 LOG(FATAL) << "Unexpected type conversion from " << input_type
2228 << " to " << result_type;
2229 };
2230 break;
2231
Roland Levillaindff1f282014-11-05 14:15:05 +00002232 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002233 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002234 case Primitive::kPrimBoolean:
2235 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002236 case Primitive::kPrimByte:
2237 case Primitive::kPrimShort:
2238 case Primitive::kPrimInt:
2239 case Primitive::kPrimChar:
2240 // Processing a Dex `int-to-double' instruction.
2241 locations->SetInAt(0, Location::RequiresRegister());
2242 locations->SetOut(Location::RequiresFpuRegister());
2243 break;
2244
2245 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002246 // Processing a Dex `long-to-double' instruction.
2247 locations->SetInAt(0, Location::RequiresRegister());
2248 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002249 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002250 locations->AddTemp(Location::RequiresFpuRegister());
2251 break;
2252
Roland Levillaincff13742014-11-17 14:32:17 +00002253 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002254 // Processing a Dex `float-to-double' instruction.
2255 locations->SetInAt(0, Location::RequiresFpuRegister());
2256 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002257 break;
2258
2259 default:
2260 LOG(FATAL) << "Unexpected type conversion from " << input_type
2261 << " to " << result_type;
2262 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002263 break;
2264
2265 default:
2266 LOG(FATAL) << "Unexpected type conversion from " << input_type
2267 << " to " << result_type;
2268 }
2269}
2270
2271void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2272 LocationSummary* locations = conversion->GetLocations();
2273 Location out = locations->Out();
2274 Location in = locations->InAt(0);
2275 Primitive::Type result_type = conversion->GetResultType();
2276 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002277 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002278 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002279 case Primitive::kPrimByte:
2280 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002281 case Primitive::kPrimLong:
2282 // Type conversion from long to byte is a result of code transformations.
2283 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2284 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002285 case Primitive::kPrimBoolean:
2286 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002287 case Primitive::kPrimShort:
2288 case Primitive::kPrimInt:
2289 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002290 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002291 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002292 break;
2293
2294 default:
2295 LOG(FATAL) << "Unexpected type conversion from " << input_type
2296 << " to " << result_type;
2297 }
2298 break;
2299
Roland Levillain01a8d712014-11-14 16:27:39 +00002300 case Primitive::kPrimShort:
2301 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002302 case Primitive::kPrimLong:
2303 // Type conversion from long to short is a result of code transformations.
2304 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2305 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002306 case Primitive::kPrimBoolean:
2307 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002308 case Primitive::kPrimByte:
2309 case Primitive::kPrimInt:
2310 case Primitive::kPrimChar:
2311 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002313 break;
2314
2315 default:
2316 LOG(FATAL) << "Unexpected type conversion from " << input_type
2317 << " to " << result_type;
2318 }
2319 break;
2320
Roland Levillain946e1432014-11-11 17:35:19 +00002321 case Primitive::kPrimInt:
2322 switch (input_type) {
2323 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002324 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002325 DCHECK(out.IsRegister());
2326 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002328 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002329 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002330 } else {
2331 DCHECK(in.IsConstant());
2332 DCHECK(in.GetConstant()->IsLongConstant());
2333 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002334 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002335 }
2336 break;
2337
Roland Levillain3f8f9362014-12-02 17:45:01 +00002338 case Primitive::kPrimFloat: {
2339 // Processing a Dex `float-to-int' instruction.
2340 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002341 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002342 __ vmovrs(out.AsRegister<Register>(), temp);
2343 break;
2344 }
2345
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002346 case Primitive::kPrimDouble: {
2347 // Processing a Dex `double-to-int' instruction.
2348 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002349 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002350 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002351 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002352 }
Roland Levillain946e1432014-11-11 17:35:19 +00002353
2354 default:
2355 LOG(FATAL) << "Unexpected type conversion from " << input_type
2356 << " to " << result_type;
2357 }
2358 break;
2359
Roland Levillaindff1f282014-11-05 14:15:05 +00002360 case Primitive::kPrimLong:
2361 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimShort:
2366 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002367 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002368 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002369 DCHECK(out.IsRegisterPair());
2370 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002371 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002372 // Sign extension.
2373 __ Asr(out.AsRegisterPairHigh<Register>(),
2374 out.AsRegisterPairLow<Register>(),
2375 31);
2376 break;
2377
2378 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002379 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002380 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2381 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002382 conversion->GetDexPc(),
2383 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002384 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002385 break;
2386
Roland Levillaindff1f282014-11-05 14:15:05 +00002387 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002388 // Processing a Dex `double-to-long' instruction.
2389 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2390 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002391 conversion->GetDexPc(),
2392 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002393 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002394 break;
2395
2396 default:
2397 LOG(FATAL) << "Unexpected type conversion from " << input_type
2398 << " to " << result_type;
2399 }
2400 break;
2401
Roland Levillain981e4542014-11-14 11:47:14 +00002402 case Primitive::kPrimChar:
2403 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002404 case Primitive::kPrimLong:
2405 // Type conversion from long to char is a result of code transformations.
2406 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2407 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002408 case Primitive::kPrimBoolean:
2409 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002410 case Primitive::kPrimByte:
2411 case Primitive::kPrimShort:
2412 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002413 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002414 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002415 break;
2416
2417 default:
2418 LOG(FATAL) << "Unexpected type conversion from " << input_type
2419 << " to " << result_type;
2420 }
2421 break;
2422
Roland Levillaindff1f282014-11-05 14:15:05 +00002423 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002424 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002425 case Primitive::kPrimBoolean:
2426 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002427 case Primitive::kPrimByte:
2428 case Primitive::kPrimShort:
2429 case Primitive::kPrimInt:
2430 case Primitive::kPrimChar: {
2431 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002432 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2433 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002434 break;
2435 }
2436
Roland Levillain5b3ee562015-04-14 16:02:41 +01002437 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002438 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002439 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2440 conversion,
2441 conversion->GetDexPc(),
2442 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002443 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002444 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002445
Roland Levillaincff13742014-11-17 14:32:17 +00002446 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002447 // Processing a Dex `double-to-float' instruction.
2448 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2449 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002450 break;
2451
2452 default:
2453 LOG(FATAL) << "Unexpected type conversion from " << input_type
2454 << " to " << result_type;
2455 };
2456 break;
2457
Roland Levillaindff1f282014-11-05 14:15:05 +00002458 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002459 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002460 case Primitive::kPrimBoolean:
2461 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002462 case Primitive::kPrimByte:
2463 case Primitive::kPrimShort:
2464 case Primitive::kPrimInt:
2465 case Primitive::kPrimChar: {
2466 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002467 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002468 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2469 out.AsFpuRegisterPairLow<SRegister>());
2470 break;
2471 }
2472
Roland Levillain647b9ed2014-11-27 12:06:00 +00002473 case Primitive::kPrimLong: {
2474 // Processing a Dex `long-to-double' instruction.
2475 Register low = in.AsRegisterPairLow<Register>();
2476 Register high = in.AsRegisterPairHigh<Register>();
2477 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2478 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002479 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002480 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002481 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2482 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002483
Roland Levillain682393c2015-04-14 15:57:52 +01002484 // temp_d = int-to-double(high)
2485 __ vmovsr(temp_s, high);
2486 __ vcvtdi(temp_d, temp_s);
2487 // constant_d = k2Pow32EncodingForDouble
2488 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2489 // out_d = unsigned-to-double(low)
2490 __ vmovsr(out_s, low);
2491 __ vcvtdu(out_d, out_s);
2492 // out_d += temp_d * constant_d
2493 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002494 break;
2495 }
2496
Roland Levillaincff13742014-11-17 14:32:17 +00002497 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002498 // Processing a Dex `float-to-double' instruction.
2499 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2500 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002501 break;
2502
2503 default:
2504 LOG(FATAL) << "Unexpected type conversion from " << input_type
2505 << " to " << result_type;
2506 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002507 break;
2508
2509 default:
2510 LOG(FATAL) << "Unexpected type conversion from " << input_type
2511 << " to " << result_type;
2512 }
2513}
2514
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002515void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002516 LocationSummary* locations =
2517 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002518 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002519 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002520 locations->SetInAt(0, Location::RequiresRegister());
2521 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2523 break;
2524 }
2525
2526 case Primitive::kPrimLong: {
2527 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002528 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002530 break;
2531 }
2532
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002533 case Primitive::kPrimFloat:
2534 case Primitive::kPrimDouble: {
2535 locations->SetInAt(0, Location::RequiresFpuRegister());
2536 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002537 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002538 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002539 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002540
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002541 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002542 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002543 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002544}
2545
2546void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2547 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002548 Location out = locations->Out();
2549 Location first = locations->InAt(0);
2550 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002551 switch (add->GetResultType()) {
2552 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002553 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002554 __ add(out.AsRegister<Register>(),
2555 first.AsRegister<Register>(),
2556 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002557 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002558 __ AddConstant(out.AsRegister<Register>(),
2559 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002560 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002561 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002562 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002563
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002564 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002565 if (second.IsConstant()) {
2566 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2567 GenerateAddLongConst(out, first, value);
2568 } else {
2569 DCHECK(second.IsRegisterPair());
2570 __ adds(out.AsRegisterPairLow<Register>(),
2571 first.AsRegisterPairLow<Register>(),
2572 ShifterOperand(second.AsRegisterPairLow<Register>()));
2573 __ adc(out.AsRegisterPairHigh<Register>(),
2574 first.AsRegisterPairHigh<Register>(),
2575 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2576 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002577 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002578 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002579
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002580 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002581 __ vadds(out.AsFpuRegister<SRegister>(),
2582 first.AsFpuRegister<SRegister>(),
2583 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002584 break;
2585
2586 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002587 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2588 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2589 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002590 break;
2591
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002592 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002593 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002594 }
2595}
2596
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002597void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002598 LocationSummary* locations =
2599 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002600 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002601 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002602 locations->SetInAt(0, Location::RequiresRegister());
2603 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2605 break;
2606 }
2607
2608 case Primitive::kPrimLong: {
2609 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002610 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002612 break;
2613 }
Calin Juravle11351682014-10-23 15:38:15 +01002614 case Primitive::kPrimFloat:
2615 case Primitive::kPrimDouble: {
2616 locations->SetInAt(0, Location::RequiresFpuRegister());
2617 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002618 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002619 break;
Calin Juravle11351682014-10-23 15:38:15 +01002620 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002621 default:
Calin Juravle11351682014-10-23 15:38:15 +01002622 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002623 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002624}
2625
2626void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2627 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002628 Location out = locations->Out();
2629 Location first = locations->InAt(0);
2630 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002631 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002632 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002633 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002634 __ sub(out.AsRegister<Register>(),
2635 first.AsRegister<Register>(),
2636 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002637 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002638 __ AddConstant(out.AsRegister<Register>(),
2639 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002640 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002641 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002642 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002643 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002644
Calin Juravle11351682014-10-23 15:38:15 +01002645 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002646 if (second.IsConstant()) {
2647 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2648 GenerateAddLongConst(out, first, -value);
2649 } else {
2650 DCHECK(second.IsRegisterPair());
2651 __ subs(out.AsRegisterPairLow<Register>(),
2652 first.AsRegisterPairLow<Register>(),
2653 ShifterOperand(second.AsRegisterPairLow<Register>()));
2654 __ sbc(out.AsRegisterPairHigh<Register>(),
2655 first.AsRegisterPairHigh<Register>(),
2656 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2657 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002658 break;
Calin Juravle11351682014-10-23 15:38:15 +01002659 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002660
Calin Juravle11351682014-10-23 15:38:15 +01002661 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002662 __ vsubs(out.AsFpuRegister<SRegister>(),
2663 first.AsFpuRegister<SRegister>(),
2664 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002665 break;
Calin Juravle11351682014-10-23 15:38:15 +01002666 }
2667
2668 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002669 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2670 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2671 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002672 break;
2673 }
2674
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002675
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002676 default:
Calin Juravle11351682014-10-23 15:38:15 +01002677 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002678 }
2679}
2680
Calin Juravle34bacdf2014-10-07 20:23:36 +01002681void LocationsBuilderARM::VisitMul(HMul* mul) {
2682 LocationSummary* locations =
2683 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2684 switch (mul->GetResultType()) {
2685 case Primitive::kPrimInt:
2686 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002687 locations->SetInAt(0, Location::RequiresRegister());
2688 locations->SetInAt(1, Location::RequiresRegister());
2689 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002690 break;
2691 }
2692
Calin Juravleb5bfa962014-10-21 18:02:24 +01002693 case Primitive::kPrimFloat:
2694 case Primitive::kPrimDouble: {
2695 locations->SetInAt(0, Location::RequiresFpuRegister());
2696 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002697 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002698 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002699 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002700
2701 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002702 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002703 }
2704}
2705
2706void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2707 LocationSummary* locations = mul->GetLocations();
2708 Location out = locations->Out();
2709 Location first = locations->InAt(0);
2710 Location second = locations->InAt(1);
2711 switch (mul->GetResultType()) {
2712 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002713 __ mul(out.AsRegister<Register>(),
2714 first.AsRegister<Register>(),
2715 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002716 break;
2717 }
2718 case Primitive::kPrimLong: {
2719 Register out_hi = out.AsRegisterPairHigh<Register>();
2720 Register out_lo = out.AsRegisterPairLow<Register>();
2721 Register in1_hi = first.AsRegisterPairHigh<Register>();
2722 Register in1_lo = first.AsRegisterPairLow<Register>();
2723 Register in2_hi = second.AsRegisterPairHigh<Register>();
2724 Register in2_lo = second.AsRegisterPairLow<Register>();
2725
2726 // Extra checks to protect caused by the existence of R1_R2.
2727 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2728 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2729 DCHECK_NE(out_hi, in1_lo);
2730 DCHECK_NE(out_hi, in2_lo);
2731
2732 // input: in1 - 64 bits, in2 - 64 bits
2733 // output: out
2734 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2735 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2736 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2737
2738 // IP <- in1.lo * in2.hi
2739 __ mul(IP, in1_lo, in2_hi);
2740 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2741 __ mla(out_hi, in1_hi, in2_lo, IP);
2742 // out.lo <- (in1.lo * in2.lo)[31:0];
2743 __ umull(out_lo, IP, in1_lo, in2_lo);
2744 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2745 __ add(out_hi, out_hi, ShifterOperand(IP));
2746 break;
2747 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002748
2749 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002750 __ vmuls(out.AsFpuRegister<SRegister>(),
2751 first.AsFpuRegister<SRegister>(),
2752 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002753 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002754 }
2755
2756 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002757 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2758 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2759 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002760 break;
2761 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002762
2763 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002764 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002765 }
2766}
2767
Zheng Xuc6667102015-05-15 16:08:45 +08002768void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2769 DCHECK(instruction->IsDiv() || instruction->IsRem());
2770 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2771
2772 LocationSummary* locations = instruction->GetLocations();
2773 Location second = locations->InAt(1);
2774 DCHECK(second.IsConstant());
2775
2776 Register out = locations->Out().AsRegister<Register>();
2777 Register dividend = locations->InAt(0).AsRegister<Register>();
2778 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2779 DCHECK(imm == 1 || imm == -1);
2780
2781 if (instruction->IsRem()) {
2782 __ LoadImmediate(out, 0);
2783 } else {
2784 if (imm == 1) {
2785 __ Mov(out, dividend);
2786 } else {
2787 __ rsb(out, dividend, ShifterOperand(0));
2788 }
2789 }
2790}
2791
2792void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2793 DCHECK(instruction->IsDiv() || instruction->IsRem());
2794 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2795
2796 LocationSummary* locations = instruction->GetLocations();
2797 Location second = locations->InAt(1);
2798 DCHECK(second.IsConstant());
2799
2800 Register out = locations->Out().AsRegister<Register>();
2801 Register dividend = locations->InAt(0).AsRegister<Register>();
2802 Register temp = locations->GetTemp(0).AsRegister<Register>();
2803 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002804 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002805 int ctz_imm = CTZ(abs_imm);
2806
2807 if (ctz_imm == 1) {
2808 __ Lsr(temp, dividend, 32 - ctz_imm);
2809 } else {
2810 __ Asr(temp, dividend, 31);
2811 __ Lsr(temp, temp, 32 - ctz_imm);
2812 }
2813 __ add(out, temp, ShifterOperand(dividend));
2814
2815 if (instruction->IsDiv()) {
2816 __ Asr(out, out, ctz_imm);
2817 if (imm < 0) {
2818 __ rsb(out, out, ShifterOperand(0));
2819 }
2820 } else {
2821 __ ubfx(out, out, 0, ctz_imm);
2822 __ sub(out, out, ShifterOperand(temp));
2823 }
2824}
2825
2826void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2827 DCHECK(instruction->IsDiv() || instruction->IsRem());
2828 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2829
2830 LocationSummary* locations = instruction->GetLocations();
2831 Location second = locations->InAt(1);
2832 DCHECK(second.IsConstant());
2833
2834 Register out = locations->Out().AsRegister<Register>();
2835 Register dividend = locations->InAt(0).AsRegister<Register>();
2836 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2837 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2838 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2839
2840 int64_t magic;
2841 int shift;
2842 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2843
2844 __ LoadImmediate(temp1, magic);
2845 __ smull(temp2, temp1, dividend, temp1);
2846
2847 if (imm > 0 && magic < 0) {
2848 __ add(temp1, temp1, ShifterOperand(dividend));
2849 } else if (imm < 0 && magic > 0) {
2850 __ sub(temp1, temp1, ShifterOperand(dividend));
2851 }
2852
2853 if (shift != 0) {
2854 __ Asr(temp1, temp1, shift);
2855 }
2856
2857 if (instruction->IsDiv()) {
2858 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2859 } else {
2860 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2861 // TODO: Strength reduction for mls.
2862 __ LoadImmediate(temp2, imm);
2863 __ mls(out, temp1, temp2, dividend);
2864 }
2865}
2866
2867void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2868 DCHECK(instruction->IsDiv() || instruction->IsRem());
2869 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2870
2871 LocationSummary* locations = instruction->GetLocations();
2872 Location second = locations->InAt(1);
2873 DCHECK(second.IsConstant());
2874
2875 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2876 if (imm == 0) {
2877 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2878 } else if (imm == 1 || imm == -1) {
2879 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002880 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002881 DivRemByPowerOfTwo(instruction);
2882 } else {
2883 DCHECK(imm <= -2 || imm >= 2);
2884 GenerateDivRemWithAnyConstant(instruction);
2885 }
2886}
2887
Calin Juravle7c4954d2014-10-28 16:57:40 +00002888void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002889 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2890 if (div->GetResultType() == Primitive::kPrimLong) {
2891 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002892 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002893 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2894 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002895 } else if (div->GetResultType() == Primitive::kPrimInt &&
2896 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2897 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002898 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002899 }
2900
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2902
Calin Juravle7c4954d2014-10-28 16:57:40 +00002903 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002904 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002905 if (div->InputAt(1)->IsConstant()) {
2906 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002907 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002908 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002909 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2910 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002911 // No temp register required.
2912 } else {
2913 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002914 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002915 locations->AddTemp(Location::RequiresRegister());
2916 }
2917 }
2918 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002919 locations->SetInAt(0, Location::RequiresRegister());
2920 locations->SetInAt(1, Location::RequiresRegister());
2921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2922 } else {
2923 InvokeRuntimeCallingConvention calling_convention;
2924 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2925 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2926 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2927 // we only need the former.
2928 locations->SetOut(Location::RegisterLocation(R0));
2929 }
Calin Juravled0d48522014-11-04 16:40:20 +00002930 break;
2931 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002932 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002933 InvokeRuntimeCallingConvention calling_convention;
2934 locations->SetInAt(0, Location::RegisterPairLocation(
2935 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2936 locations->SetInAt(1, Location::RegisterPairLocation(
2937 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002938 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002939 break;
2940 }
2941 case Primitive::kPrimFloat:
2942 case Primitive::kPrimDouble: {
2943 locations->SetInAt(0, Location::RequiresFpuRegister());
2944 locations->SetInAt(1, Location::RequiresFpuRegister());
2945 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2946 break;
2947 }
2948
2949 default:
2950 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2951 }
2952}
2953
2954void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2955 LocationSummary* locations = div->GetLocations();
2956 Location out = locations->Out();
2957 Location first = locations->InAt(0);
2958 Location second = locations->InAt(1);
2959
2960 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002961 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002962 if (second.IsConstant()) {
2963 GenerateDivRemConstantIntegral(div);
2964 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002965 __ sdiv(out.AsRegister<Register>(),
2966 first.AsRegister<Register>(),
2967 second.AsRegister<Register>());
2968 } else {
2969 InvokeRuntimeCallingConvention calling_convention;
2970 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2971 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2972 DCHECK_EQ(R0, out.AsRegister<Register>());
2973
2974 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002975 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002976 }
Calin Juravled0d48522014-11-04 16:40:20 +00002977 break;
2978 }
2979
Calin Juravle7c4954d2014-10-28 16:57:40 +00002980 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002981 InvokeRuntimeCallingConvention calling_convention;
2982 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2983 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2984 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2985 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2986 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002987 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002988
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002989 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002990 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002991 break;
2992 }
2993
2994 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002995 __ vdivs(out.AsFpuRegister<SRegister>(),
2996 first.AsFpuRegister<SRegister>(),
2997 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002998 break;
2999 }
3000
3001 case Primitive::kPrimDouble: {
3002 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3003 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3004 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3005 break;
3006 }
3007
3008 default:
3009 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3010 }
3011}
3012
Calin Juravlebacfec32014-11-14 15:54:36 +00003013void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003014 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003015
3016 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003017 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003018 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3019 // sdiv will be replaced by other instruction sequence.
3020 call_kind = LocationSummary::kNoCall;
3021 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3022 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003023 // Have hardware divide instruction for int, do it with three instructions.
3024 call_kind = LocationSummary::kNoCall;
3025 }
3026
Calin Juravlebacfec32014-11-14 15:54:36 +00003027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3028
Calin Juravled2ec87d2014-12-08 14:24:46 +00003029 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003030 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003031 if (rem->InputAt(1)->IsConstant()) {
3032 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003033 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003035 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3036 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003037 // No temp register required.
3038 } else {
3039 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003040 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003041 locations->AddTemp(Location::RequiresRegister());
3042 }
3043 }
3044 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003045 locations->SetInAt(0, Location::RequiresRegister());
3046 locations->SetInAt(1, Location::RequiresRegister());
3047 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3048 locations->AddTemp(Location::RequiresRegister());
3049 } else {
3050 InvokeRuntimeCallingConvention calling_convention;
3051 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3052 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3053 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3054 // we only need the latter.
3055 locations->SetOut(Location::RegisterLocation(R1));
3056 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003057 break;
3058 }
3059 case Primitive::kPrimLong: {
3060 InvokeRuntimeCallingConvention calling_convention;
3061 locations->SetInAt(0, Location::RegisterPairLocation(
3062 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3063 locations->SetInAt(1, Location::RegisterPairLocation(
3064 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3065 // The runtime helper puts the output in R2,R3.
3066 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3067 break;
3068 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003069 case Primitive::kPrimFloat: {
3070 InvokeRuntimeCallingConvention calling_convention;
3071 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3072 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3073 locations->SetOut(Location::FpuRegisterLocation(S0));
3074 break;
3075 }
3076
Calin Juravlebacfec32014-11-14 15:54:36 +00003077 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003078 InvokeRuntimeCallingConvention calling_convention;
3079 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3080 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3081 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3082 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3083 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003084 break;
3085 }
3086
3087 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003088 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003089 }
3090}
3091
3092void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3093 LocationSummary* locations = rem->GetLocations();
3094 Location out = locations->Out();
3095 Location first = locations->InAt(0);
3096 Location second = locations->InAt(1);
3097
Calin Juravled2ec87d2014-12-08 14:24:46 +00003098 Primitive::Type type = rem->GetResultType();
3099 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003100 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003101 if (second.IsConstant()) {
3102 GenerateDivRemConstantIntegral(rem);
3103 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003104 Register reg1 = first.AsRegister<Register>();
3105 Register reg2 = second.AsRegister<Register>();
3106 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003107
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003108 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003109 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003110 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003111 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003112 } else {
3113 InvokeRuntimeCallingConvention calling_convention;
3114 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3115 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3116 DCHECK_EQ(R1, out.AsRegister<Register>());
3117
3118 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003119 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003120 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003121 break;
3122 }
3123
3124 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003125 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003126 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003127 break;
3128 }
3129
Calin Juravled2ec87d2014-12-08 14:24:46 +00003130 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003131 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003132 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003133 break;
3134 }
3135
Calin Juravlebacfec32014-11-14 15:54:36 +00003136 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003137 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003138 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003139 break;
3140 }
3141
3142 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003143 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003144 }
3145}
3146
Calin Juravled0d48522014-11-04 16:40:20 +00003147void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003148 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3149 ? LocationSummary::kCallOnSlowPath
3150 : LocationSummary::kNoCall;
3151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003152 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003153 if (instruction->HasUses()) {
3154 locations->SetOut(Location::SameAsFirstInput());
3155 }
3156}
3157
3158void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003159 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003160 codegen_->AddSlowPath(slow_path);
3161
3162 LocationSummary* locations = instruction->GetLocations();
3163 Location value = locations->InAt(0);
3164
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003165 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003166 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003167 case Primitive::kPrimByte:
3168 case Primitive::kPrimChar:
3169 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003170 case Primitive::kPrimInt: {
3171 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003172 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003173 } else {
3174 DCHECK(value.IsConstant()) << value;
3175 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3176 __ b(slow_path->GetEntryLabel());
3177 }
3178 }
3179 break;
3180 }
3181 case Primitive::kPrimLong: {
3182 if (value.IsRegisterPair()) {
3183 __ orrs(IP,
3184 value.AsRegisterPairLow<Register>(),
3185 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3186 __ b(slow_path->GetEntryLabel(), EQ);
3187 } else {
3188 DCHECK(value.IsConstant()) << value;
3189 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3190 __ b(slow_path->GetEntryLabel());
3191 }
3192 }
3193 break;
3194 default:
3195 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3196 }
3197 }
Calin Juravled0d48522014-11-04 16:40:20 +00003198}
3199
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003200void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3201 Register in = locations->InAt(0).AsRegister<Register>();
3202 Location rhs = locations->InAt(1);
3203 Register out = locations->Out().AsRegister<Register>();
3204
3205 if (rhs.IsConstant()) {
3206 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3207 // so map all rotations to a +ve. equivalent in that range.
3208 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3209 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3210 if (rot) {
3211 // Rotate, mapping left rotations to right equivalents if necessary.
3212 // (e.g. left by 2 bits == right by 30.)
3213 __ Ror(out, in, rot);
3214 } else if (out != in) {
3215 __ Mov(out, in);
3216 }
3217 } else {
3218 __ Ror(out, in, rhs.AsRegister<Register>());
3219 }
3220}
3221
3222// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3223// rotates by swapping input regs (effectively rotating by the first 32-bits of
3224// a larger rotation) or flipping direction (thus treating larger right/left
3225// rotations as sub-word sized rotations in the other direction) as appropriate.
3226void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3227 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3228 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3229 Location rhs = locations->InAt(1);
3230 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3231 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3232
3233 if (rhs.IsConstant()) {
3234 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3235 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003236 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003237 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3238 // logic below to a simple pair of binary orr.
3239 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3240 if (rot >= kArmBitsPerWord) {
3241 rot -= kArmBitsPerWord;
3242 std::swap(in_reg_hi, in_reg_lo);
3243 }
3244 // Rotate, or mov to out for zero or word size rotations.
3245 if (rot != 0u) {
3246 __ Lsr(out_reg_hi, in_reg_hi, rot);
3247 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3248 __ Lsr(out_reg_lo, in_reg_lo, rot);
3249 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3250 } else {
3251 __ Mov(out_reg_lo, in_reg_lo);
3252 __ Mov(out_reg_hi, in_reg_hi);
3253 }
3254 } else {
3255 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3256 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3257 Label end;
3258 Label shift_by_32_plus_shift_right;
3259
3260 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3261 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3262 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3263 __ b(&shift_by_32_plus_shift_right, CC);
3264
3265 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3266 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3267 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3268 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3269 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3270 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3271 __ Lsr(shift_left, in_reg_hi, shift_right);
3272 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3273 __ b(&end);
3274
3275 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3276 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3277 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3278 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3279 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3280 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3281 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3282 __ Lsl(shift_right, in_reg_hi, shift_left);
3283 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3284
3285 __ Bind(&end);
3286 }
3287}
Roland Levillain22c49222016-03-18 14:04:28 +00003288
3289void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003290 LocationSummary* locations =
3291 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3292 switch (ror->GetResultType()) {
3293 case Primitive::kPrimInt: {
3294 locations->SetInAt(0, Location::RequiresRegister());
3295 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3297 break;
3298 }
3299 case Primitive::kPrimLong: {
3300 locations->SetInAt(0, Location::RequiresRegister());
3301 if (ror->InputAt(1)->IsConstant()) {
3302 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3303 } else {
3304 locations->SetInAt(1, Location::RequiresRegister());
3305 locations->AddTemp(Location::RequiresRegister());
3306 locations->AddTemp(Location::RequiresRegister());
3307 }
3308 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3309 break;
3310 }
3311 default:
3312 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3313 }
3314}
3315
Roland Levillain22c49222016-03-18 14:04:28 +00003316void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003317 LocationSummary* locations = ror->GetLocations();
3318 Primitive::Type type = ror->GetResultType();
3319 switch (type) {
3320 case Primitive::kPrimInt: {
3321 HandleIntegerRotate(locations);
3322 break;
3323 }
3324 case Primitive::kPrimLong: {
3325 HandleLongRotate(locations);
3326 break;
3327 }
3328 default:
3329 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003330 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003331 }
3332}
3333
Calin Juravle9aec02f2014-11-18 23:06:35 +00003334void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3335 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3336
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003337 LocationSummary* locations =
3338 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003339
3340 switch (op->GetResultType()) {
3341 case Primitive::kPrimInt: {
3342 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003343 if (op->InputAt(1)->IsConstant()) {
3344 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3346 } else {
3347 locations->SetInAt(1, Location::RequiresRegister());
3348 // Make the output overlap, as it will be used to hold the masked
3349 // second input.
3350 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3351 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003352 break;
3353 }
3354 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003355 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003356 if (op->InputAt(1)->IsConstant()) {
3357 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3358 // For simplicity, use kOutputOverlap even though we only require that low registers
3359 // don't clash with high registers which the register allocator currently guarantees.
3360 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3361 } else {
3362 locations->SetInAt(1, Location::RequiresRegister());
3363 locations->AddTemp(Location::RequiresRegister());
3364 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3365 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003366 break;
3367 }
3368 default:
3369 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3370 }
3371}
3372
3373void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3374 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3375
3376 LocationSummary* locations = op->GetLocations();
3377 Location out = locations->Out();
3378 Location first = locations->InAt(0);
3379 Location second = locations->InAt(1);
3380
3381 Primitive::Type type = op->GetResultType();
3382 switch (type) {
3383 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003384 Register out_reg = out.AsRegister<Register>();
3385 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003386 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003387 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003388 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003389 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003390 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003391 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003392 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003393 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003394 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003395 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003396 }
3397 } else {
3398 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003399 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003400 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003401 __ Mov(out_reg, first_reg);
3402 } else if (op->IsShl()) {
3403 __ Lsl(out_reg, first_reg, shift_value);
3404 } else if (op->IsShr()) {
3405 __ Asr(out_reg, first_reg, shift_value);
3406 } else {
3407 __ Lsr(out_reg, first_reg, shift_value);
3408 }
3409 }
3410 break;
3411 }
3412 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003413 Register o_h = out.AsRegisterPairHigh<Register>();
3414 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003415
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003416 Register high = first.AsRegisterPairHigh<Register>();
3417 Register low = first.AsRegisterPairLow<Register>();
3418
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003419 if (second.IsRegister()) {
3420 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003421
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003422 Register second_reg = second.AsRegister<Register>();
3423
3424 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003425 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003426 // Shift the high part
3427 __ Lsl(o_h, high, o_l);
3428 // Shift the low part and `or` what overflew on the high part
3429 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3430 __ Lsr(temp, low, temp);
3431 __ orr(o_h, o_h, ShifterOperand(temp));
3432 // If the shift is > 32 bits, override the high part
3433 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3434 __ it(PL);
3435 __ Lsl(o_h, low, temp, PL);
3436 // Shift the low part
3437 __ Lsl(o_l, low, o_l);
3438 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003439 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003440 // Shift the low part
3441 __ Lsr(o_l, low, o_h);
3442 // Shift the high part and `or` what underflew on the low part
3443 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3444 __ Lsl(temp, high, temp);
3445 __ orr(o_l, o_l, ShifterOperand(temp));
3446 // If the shift is > 32 bits, override the low part
3447 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3448 __ it(PL);
3449 __ Asr(o_l, high, temp, PL);
3450 // Shift the high part
3451 __ Asr(o_h, high, o_h);
3452 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003453 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003454 // same as Shr except we use `Lsr`s and not `Asr`s
3455 __ Lsr(o_l, low, o_h);
3456 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3457 __ Lsl(temp, high, temp);
3458 __ orr(o_l, o_l, ShifterOperand(temp));
3459 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3460 __ it(PL);
3461 __ Lsr(o_l, high, temp, PL);
3462 __ Lsr(o_h, high, o_h);
3463 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003464 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003465 // Register allocator doesn't create partial overlap.
3466 DCHECK_NE(o_l, high);
3467 DCHECK_NE(o_h, low);
3468 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003469 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003470 if (shift_value > 32) {
3471 if (op->IsShl()) {
3472 __ Lsl(o_h, low, shift_value - 32);
3473 __ LoadImmediate(o_l, 0);
3474 } else if (op->IsShr()) {
3475 __ Asr(o_l, high, shift_value - 32);
3476 __ Asr(o_h, high, 31);
3477 } else {
3478 __ Lsr(o_l, high, shift_value - 32);
3479 __ LoadImmediate(o_h, 0);
3480 }
3481 } else if (shift_value == 32) {
3482 if (op->IsShl()) {
3483 __ mov(o_h, ShifterOperand(low));
3484 __ LoadImmediate(o_l, 0);
3485 } else if (op->IsShr()) {
3486 __ mov(o_l, ShifterOperand(high));
3487 __ Asr(o_h, high, 31);
3488 } else {
3489 __ mov(o_l, ShifterOperand(high));
3490 __ LoadImmediate(o_h, 0);
3491 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003492 } else if (shift_value == 1) {
3493 if (op->IsShl()) {
3494 __ Lsls(o_l, low, 1);
3495 __ adc(o_h, high, ShifterOperand(high));
3496 } else if (op->IsShr()) {
3497 __ Asrs(o_h, high, 1);
3498 __ Rrx(o_l, low);
3499 } else {
3500 __ Lsrs(o_h, high, 1);
3501 __ Rrx(o_l, low);
3502 }
3503 } else {
3504 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003505 if (op->IsShl()) {
3506 __ Lsl(o_h, high, shift_value);
3507 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3508 __ Lsl(o_l, low, shift_value);
3509 } else if (op->IsShr()) {
3510 __ Lsr(o_l, low, shift_value);
3511 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3512 __ Asr(o_h, high, shift_value);
3513 } else {
3514 __ Lsr(o_l, low, shift_value);
3515 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3516 __ Lsr(o_h, high, shift_value);
3517 }
3518 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003519 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003520 break;
3521 }
3522 default:
3523 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003524 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003525 }
3526}
3527
3528void LocationsBuilderARM::VisitShl(HShl* shl) {
3529 HandleShift(shl);
3530}
3531
3532void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3533 HandleShift(shl);
3534}
3535
3536void LocationsBuilderARM::VisitShr(HShr* shr) {
3537 HandleShift(shr);
3538}
3539
3540void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3541 HandleShift(shr);
3542}
3543
3544void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3545 HandleShift(ushr);
3546}
3547
3548void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3549 HandleShift(ushr);
3550}
3551
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003552void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003553 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003554 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003555 if (instruction->IsStringAlloc()) {
3556 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3557 } else {
3558 InvokeRuntimeCallingConvention calling_convention;
3559 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3560 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3561 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003562 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003563}
3564
3565void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003566 // Note: if heap poisoning is enabled, the entry point takes cares
3567 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003568 if (instruction->IsStringAlloc()) {
3569 // String is allocated through StringFactory. Call NewEmptyString entry point.
3570 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003571 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003572 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3573 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3574 __ blx(LR);
3575 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3576 } else {
3577 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3578 instruction,
3579 instruction->GetDexPc(),
3580 nullptr);
3581 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3582 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003583}
3584
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003585void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3586 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003588 InvokeRuntimeCallingConvention calling_convention;
3589 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003590 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003591 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003592 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003593}
3594
3595void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3596 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003597 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003598 // Note: if heap poisoning is enabled, the entry point takes cares
3599 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003600 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003601 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003602 instruction->GetDexPc(),
3603 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003604 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003605}
3606
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003607void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003608 LocationSummary* locations =
3609 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003610 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3611 if (location.IsStackSlot()) {
3612 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3613 } else if (location.IsDoubleStackSlot()) {
3614 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003615 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003616 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003617}
3618
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003619void InstructionCodeGeneratorARM::VisitParameterValue(
3620 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003621 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003622}
3623
3624void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3625 LocationSummary* locations =
3626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3627 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3628}
3629
3630void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3631 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003632}
3633
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003634void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003635 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003636 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003637 locations->SetInAt(0, Location::RequiresRegister());
3638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003639}
3640
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003641void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3642 LocationSummary* locations = not_->GetLocations();
3643 Location out = locations->Out();
3644 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003645 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003646 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003647 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003648 break;
3649
3650 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003651 __ mvn(out.AsRegisterPairLow<Register>(),
3652 ShifterOperand(in.AsRegisterPairLow<Register>()));
3653 __ mvn(out.AsRegisterPairHigh<Register>(),
3654 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003655 break;
3656
3657 default:
3658 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3659 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003660}
3661
David Brazdil66d126e2015-04-03 16:02:44 +01003662void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3663 LocationSummary* locations =
3664 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3665 locations->SetInAt(0, Location::RequiresRegister());
3666 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3667}
3668
3669void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003670 LocationSummary* locations = bool_not->GetLocations();
3671 Location out = locations->Out();
3672 Location in = locations->InAt(0);
3673 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3674}
3675
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003676void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003677 LocationSummary* locations =
3678 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003679 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003680 case Primitive::kPrimBoolean:
3681 case Primitive::kPrimByte:
3682 case Primitive::kPrimShort:
3683 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003684 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003685 case Primitive::kPrimLong: {
3686 locations->SetInAt(0, Location::RequiresRegister());
3687 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003688 // Output overlaps because it is written before doing the low comparison.
3689 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003690 break;
3691 }
3692 case Primitive::kPrimFloat:
3693 case Primitive::kPrimDouble: {
3694 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003695 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003696 locations->SetOut(Location::RequiresRegister());
3697 break;
3698 }
3699 default:
3700 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3701 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003702}
3703
3704void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003705 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003706 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003707 Location left = locations->InAt(0);
3708 Location right = locations->InAt(1);
3709
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003710 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003711 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003712 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003713 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003714 case Primitive::kPrimBoolean:
3715 case Primitive::kPrimByte:
3716 case Primitive::kPrimShort:
3717 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003718 case Primitive::kPrimInt: {
3719 __ LoadImmediate(out, 0);
3720 __ cmp(left.AsRegister<Register>(),
3721 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3722 less_cond = LT;
3723 break;
3724 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003725 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003726 __ cmp(left.AsRegisterPairHigh<Register>(),
3727 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003728 __ b(&less, LT);
3729 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003730 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003731 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003732 __ cmp(left.AsRegisterPairLow<Register>(),
3733 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003734 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003735 break;
3736 }
3737 case Primitive::kPrimFloat:
3738 case Primitive::kPrimDouble: {
3739 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003740 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003741 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003742 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003743 break;
3744 }
3745 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003746 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003747 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003748 }
Aart Bika19616e2016-02-01 18:57:58 -08003749
Calin Juravleddb7df22014-11-25 20:56:51 +00003750 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003751 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003752
3753 __ Bind(&greater);
3754 __ LoadImmediate(out, 1);
3755 __ b(&done);
3756
3757 __ Bind(&less);
3758 __ LoadImmediate(out, -1);
3759
3760 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003761}
3762
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003763void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003764 LocationSummary* locations =
3765 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003766 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003767 locations->SetInAt(i, Location::Any());
3768 }
3769 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003770}
3771
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003772void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003773 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003774}
3775
Roland Levillainc9285912015-12-18 10:38:42 +00003776void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3777 // TODO (ported from quick): revisit ARM barrier kinds.
3778 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003779 switch (kind) {
3780 case MemBarrierKind::kAnyStore:
3781 case MemBarrierKind::kLoadAny:
3782 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003783 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003784 break;
3785 }
3786 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003787 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003788 break;
3789 }
3790 default:
3791 LOG(FATAL) << "Unexpected memory barrier " << kind;
3792 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003793 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003794}
3795
3796void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3797 uint32_t offset,
3798 Register out_lo,
3799 Register out_hi) {
3800 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003801 // Ensure `out_lo` is different from `addr`, so that loading
3802 // `offset` into `out_lo` does not clutter `addr`.
3803 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003804 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003805 __ add(IP, addr, ShifterOperand(out_lo));
3806 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003807 }
3808 __ ldrexd(out_lo, out_hi, addr);
3809}
3810
3811void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3812 uint32_t offset,
3813 Register value_lo,
3814 Register value_hi,
3815 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003816 Register temp2,
3817 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003818 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003819 if (offset != 0) {
3820 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003821 __ add(IP, addr, ShifterOperand(temp1));
3822 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003823 }
3824 __ Bind(&fail);
3825 // We need a load followed by store. (The address used in a STREX instruction must
3826 // be the same as the address in the most recently executed LDREX instruction.)
3827 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003828 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003829 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003830 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003831}
3832
3833void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3834 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3835
Nicolas Geoffray39468442014-09-02 15:17:15 +01003836 LocationSummary* locations =
3837 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003838 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003839
Calin Juravle52c48962014-12-16 17:02:57 +00003840 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003841 if (Primitive::IsFloatingPointType(field_type)) {
3842 locations->SetInAt(1, Location::RequiresFpuRegister());
3843 } else {
3844 locations->SetInAt(1, Location::RequiresRegister());
3845 }
3846
Calin Juravle52c48962014-12-16 17:02:57 +00003847 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003848 bool generate_volatile = field_info.IsVolatile()
3849 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003850 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003851 bool needs_write_barrier =
3852 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003853 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003854 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003855 if (needs_write_barrier) {
3856 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003857 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003858 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003859 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003860 // - registers need to be consecutive
3861 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003862 // We don't test for ARM yet, and the assertion makes sure that we
3863 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003864 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3865
3866 locations->AddTemp(Location::RequiresRegister());
3867 locations->AddTemp(Location::RequiresRegister());
3868 if (field_type == Primitive::kPrimDouble) {
3869 // For doubles we need two more registers to copy the value.
3870 locations->AddTemp(Location::RegisterLocation(R2));
3871 locations->AddTemp(Location::RegisterLocation(R3));
3872 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003873 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003874}
3875
Calin Juravle52c48962014-12-16 17:02:57 +00003876void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003877 const FieldInfo& field_info,
3878 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003879 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3880
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003881 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003882 Register base = locations->InAt(0).AsRegister<Register>();
3883 Location value = locations->InAt(1);
3884
3885 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003886 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003887 Primitive::Type field_type = field_info.GetFieldType();
3888 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003889 bool needs_write_barrier =
3890 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003891
3892 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003893 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003894 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003895
3896 switch (field_type) {
3897 case Primitive::kPrimBoolean:
3898 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003899 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003900 break;
3901 }
3902
3903 case Primitive::kPrimShort:
3904 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003905 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003906 break;
3907 }
3908
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003909 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003910 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003911 if (kPoisonHeapReferences && needs_write_barrier) {
3912 // Note that in the case where `value` is a null reference,
3913 // we do not enter this block, as a null reference does not
3914 // need poisoning.
3915 DCHECK_EQ(field_type, Primitive::kPrimNot);
3916 Register temp = locations->GetTemp(0).AsRegister<Register>();
3917 __ Mov(temp, value.AsRegister<Register>());
3918 __ PoisonHeapReference(temp);
3919 __ StoreToOffset(kStoreWord, temp, base, offset);
3920 } else {
3921 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3922 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003923 break;
3924 }
3925
3926 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003927 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003928 GenerateWideAtomicStore(base, offset,
3929 value.AsRegisterPairLow<Register>(),
3930 value.AsRegisterPairHigh<Register>(),
3931 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003932 locations->GetTemp(1).AsRegister<Register>(),
3933 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003934 } else {
3935 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003936 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003937 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003938 break;
3939 }
3940
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003941 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003942 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003943 break;
3944 }
3945
3946 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003947 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003948 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003949 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3950 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3951
3952 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3953
3954 GenerateWideAtomicStore(base, offset,
3955 value_reg_lo,
3956 value_reg_hi,
3957 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003958 locations->GetTemp(3).AsRegister<Register>(),
3959 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003960 } else {
3961 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003962 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003963 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003964 break;
3965 }
3966
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003967 case Primitive::kPrimVoid:
3968 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003969 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003970 }
Calin Juravle52c48962014-12-16 17:02:57 +00003971
Calin Juravle77520bc2015-01-12 18:45:46 +00003972 // Longs and doubles are handled in the switch.
3973 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3974 codegen_->MaybeRecordImplicitNullCheck(instruction);
3975 }
3976
3977 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3978 Register temp = locations->GetTemp(0).AsRegister<Register>();
3979 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003980 codegen_->MarkGCCard(
3981 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003982 }
3983
Calin Juravle52c48962014-12-16 17:02:57 +00003984 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003985 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003986 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003987}
3988
Calin Juravle52c48962014-12-16 17:02:57 +00003989void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3990 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003991
3992 bool object_field_get_with_read_barrier =
3993 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003994 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003995 new (GetGraph()->GetArena()) LocationSummary(instruction,
3996 object_field_get_with_read_barrier ?
3997 LocationSummary::kCallOnSlowPath :
3998 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003999 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004000
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004001 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004002 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004003 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004004 // The output overlaps in case of volatile long: we don't want the
4005 // code generated by GenerateWideAtomicLoad to overwrite the
4006 // object's location. Likewise, in the case of an object field get
4007 // with read barriers enabled, we do not want the load to overwrite
4008 // the object's location, as we need it to emit the read barrier.
4009 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4010 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004011
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004012 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4013 locations->SetOut(Location::RequiresFpuRegister());
4014 } else {
4015 locations->SetOut(Location::RequiresRegister(),
4016 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4017 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004018 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004019 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004020 // - registers need to be consecutive
4021 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004022 // We don't test for ARM yet, and the assertion makes sure that we
4023 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004024 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4025 locations->AddTemp(Location::RequiresRegister());
4026 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004027 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4028 // We need a temporary register for the read barrier marking slow
4029 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4030 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004031 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004032}
4033
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004034Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4035 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4036 << input->GetType();
4037 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4038 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4039 return Location::ConstantLocation(input->AsConstant());
4040 } else {
4041 return Location::RequiresFpuRegister();
4042 }
4043}
4044
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004045Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4046 Opcode opcode) {
4047 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4048 if (constant->IsConstant() &&
4049 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4050 return Location::ConstantLocation(constant->AsConstant());
4051 }
4052 return Location::RequiresRegister();
4053}
4054
4055bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4056 Opcode opcode) {
4057 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4058 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004059 Opcode high_opcode = opcode;
4060 SetCc low_set_cc = kCcDontCare;
4061 switch (opcode) {
4062 case SUB:
4063 // Flip the operation to an ADD.
4064 value = -value;
4065 opcode = ADD;
4066 FALLTHROUGH_INTENDED;
4067 case ADD:
4068 if (Low32Bits(value) == 0u) {
4069 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4070 }
4071 high_opcode = ADC;
4072 low_set_cc = kCcSet;
4073 break;
4074 default:
4075 break;
4076 }
4077 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4078 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004079 } else {
4080 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4081 }
4082}
4083
Vladimir Marko59751a72016-08-05 14:37:27 +01004084bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4085 Opcode opcode,
4086 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004087 ShifterOperand so;
4088 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004089 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004090 return true;
4091 }
4092 Opcode neg_opcode = kNoOperand;
4093 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004094 case AND: neg_opcode = BIC; value = ~value; break;
4095 case ORR: neg_opcode = ORN; value = ~value; break;
4096 case ADD: neg_opcode = SUB; value = -value; break;
4097 case ADC: neg_opcode = SBC; value = ~value; break;
4098 case SUB: neg_opcode = ADD; value = -value; break;
4099 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004100 default:
4101 return false;
4102 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004103 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004104}
4105
Calin Juravle52c48962014-12-16 17:02:57 +00004106void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4107 const FieldInfo& field_info) {
4108 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004109
Calin Juravle52c48962014-12-16 17:02:57 +00004110 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004111 Location base_loc = locations->InAt(0);
4112 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004113 Location out = locations->Out();
4114 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004115 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004116 Primitive::Type field_type = field_info.GetFieldType();
4117 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4118
4119 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004120 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004121 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004122 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004123
Roland Levillainc9285912015-12-18 10:38:42 +00004124 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004125 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004126 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004127
Roland Levillainc9285912015-12-18 10:38:42 +00004128 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004129 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004130 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004131
Roland Levillainc9285912015-12-18 10:38:42 +00004132 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004133 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004134 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135
4136 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004137 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004138 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004139
4140 case Primitive::kPrimNot: {
4141 // /* HeapReference<Object> */ out = *(base + offset)
4142 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4143 Location temp_loc = locations->GetTemp(0);
4144 // Note that a potential implicit null check is handled in this
4145 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4146 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4147 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4148 if (is_volatile) {
4149 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4150 }
4151 } else {
4152 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4153 codegen_->MaybeRecordImplicitNullCheck(instruction);
4154 if (is_volatile) {
4155 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4156 }
4157 // If read barriers are enabled, emit read barriers other than
4158 // Baker's using a slow path (and also unpoison the loaded
4159 // reference, if heap poisoning is enabled).
4160 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4161 }
4162 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004163 }
4164
Roland Levillainc9285912015-12-18 10:38:42 +00004165 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004166 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004167 GenerateWideAtomicLoad(base, offset,
4168 out.AsRegisterPairLow<Register>(),
4169 out.AsRegisterPairHigh<Register>());
4170 } else {
4171 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4172 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004173 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004174
Roland Levillainc9285912015-12-18 10:38:42 +00004175 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004176 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004177 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004178
4179 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004180 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004181 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004182 Register lo = locations->GetTemp(0).AsRegister<Register>();
4183 Register hi = locations->GetTemp(1).AsRegister<Register>();
4184 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004185 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004186 __ vmovdrr(out_reg, lo, hi);
4187 } else {
4188 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004189 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004190 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004191 break;
4192 }
4193
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004194 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004195 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004196 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004197 }
Calin Juravle52c48962014-12-16 17:02:57 +00004198
Roland Levillainc9285912015-12-18 10:38:42 +00004199 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4200 // Potential implicit null checks, in the case of reference or
4201 // double fields, are handled in the previous switch statement.
4202 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004203 codegen_->MaybeRecordImplicitNullCheck(instruction);
4204 }
4205
Calin Juravle52c48962014-12-16 17:02:57 +00004206 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004207 if (field_type == Primitive::kPrimNot) {
4208 // Memory barriers, in the case of references, are also handled
4209 // in the previous switch statement.
4210 } else {
4211 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4212 }
Roland Levillain4d027112015-07-01 15:41:14 +01004213 }
Calin Juravle52c48962014-12-16 17:02:57 +00004214}
4215
4216void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4217 HandleFieldSet(instruction, instruction->GetFieldInfo());
4218}
4219
4220void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004221 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004222}
4223
4224void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4225 HandleFieldGet(instruction, instruction->GetFieldInfo());
4226}
4227
4228void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4229 HandleFieldGet(instruction, instruction->GetFieldInfo());
4230}
4231
4232void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4233 HandleFieldGet(instruction, instruction->GetFieldInfo());
4234}
4235
4236void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4237 HandleFieldGet(instruction, instruction->GetFieldInfo());
4238}
4239
4240void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4241 HandleFieldSet(instruction, instruction->GetFieldInfo());
4242}
4243
4244void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004245 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246}
4247
Calin Juravlee460d1d2015-09-29 04:52:17 +01004248void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4249 HUnresolvedInstanceFieldGet* instruction) {
4250 FieldAccessCallingConventionARM calling_convention;
4251 codegen_->CreateUnresolvedFieldLocationSummary(
4252 instruction, instruction->GetFieldType(), calling_convention);
4253}
4254
4255void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4256 HUnresolvedInstanceFieldGet* instruction) {
4257 FieldAccessCallingConventionARM calling_convention;
4258 codegen_->GenerateUnresolvedFieldAccess(instruction,
4259 instruction->GetFieldType(),
4260 instruction->GetFieldIndex(),
4261 instruction->GetDexPc(),
4262 calling_convention);
4263}
4264
4265void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4266 HUnresolvedInstanceFieldSet* instruction) {
4267 FieldAccessCallingConventionARM calling_convention;
4268 codegen_->CreateUnresolvedFieldLocationSummary(
4269 instruction, instruction->GetFieldType(), calling_convention);
4270}
4271
4272void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4273 HUnresolvedInstanceFieldSet* instruction) {
4274 FieldAccessCallingConventionARM calling_convention;
4275 codegen_->GenerateUnresolvedFieldAccess(instruction,
4276 instruction->GetFieldType(),
4277 instruction->GetFieldIndex(),
4278 instruction->GetDexPc(),
4279 calling_convention);
4280}
4281
4282void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4283 HUnresolvedStaticFieldGet* instruction) {
4284 FieldAccessCallingConventionARM calling_convention;
4285 codegen_->CreateUnresolvedFieldLocationSummary(
4286 instruction, instruction->GetFieldType(), calling_convention);
4287}
4288
4289void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4290 HUnresolvedStaticFieldGet* instruction) {
4291 FieldAccessCallingConventionARM calling_convention;
4292 codegen_->GenerateUnresolvedFieldAccess(instruction,
4293 instruction->GetFieldType(),
4294 instruction->GetFieldIndex(),
4295 instruction->GetDexPc(),
4296 calling_convention);
4297}
4298
4299void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4300 HUnresolvedStaticFieldSet* instruction) {
4301 FieldAccessCallingConventionARM calling_convention;
4302 codegen_->CreateUnresolvedFieldLocationSummary(
4303 instruction, instruction->GetFieldType(), calling_convention);
4304}
4305
4306void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4307 HUnresolvedStaticFieldSet* instruction) {
4308 FieldAccessCallingConventionARM calling_convention;
4309 codegen_->GenerateUnresolvedFieldAccess(instruction,
4310 instruction->GetFieldType(),
4311 instruction->GetFieldIndex(),
4312 instruction->GetDexPc(),
4313 calling_convention);
4314}
4315
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004317 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4318 ? LocationSummary::kCallOnSlowPath
4319 : LocationSummary::kNoCall;
4320 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004321 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004322 if (instruction->HasUses()) {
4323 locations->SetOut(Location::SameAsFirstInput());
4324 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004325}
4326
Calin Juravle2ae48182016-03-16 14:05:09 +00004327void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4328 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004329 return;
4330 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004331 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004332
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004333 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004334 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004335}
4336
Calin Juravle2ae48182016-03-16 14:05:09 +00004337void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004338 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004339 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004340
4341 LocationSummary* locations = instruction->GetLocations();
4342 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004343
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004344 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004345}
4346
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004347void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004348 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004349}
4350
Artem Serov6c916792016-07-11 14:02:34 +01004351static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4352 switch (type) {
4353 case Primitive::kPrimNot:
4354 return kLoadWord;
4355 case Primitive::kPrimBoolean:
4356 return kLoadUnsignedByte;
4357 case Primitive::kPrimByte:
4358 return kLoadSignedByte;
4359 case Primitive::kPrimChar:
4360 return kLoadUnsignedHalfword;
4361 case Primitive::kPrimShort:
4362 return kLoadSignedHalfword;
4363 case Primitive::kPrimInt:
4364 return kLoadWord;
4365 case Primitive::kPrimLong:
4366 return kLoadWordPair;
4367 case Primitive::kPrimFloat:
4368 return kLoadSWord;
4369 case Primitive::kPrimDouble:
4370 return kLoadDWord;
4371 default:
4372 LOG(FATAL) << "Unreachable type " << type;
4373 UNREACHABLE();
4374 }
4375}
4376
4377static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4378 switch (type) {
4379 case Primitive::kPrimNot:
4380 return kStoreWord;
4381 case Primitive::kPrimBoolean:
4382 case Primitive::kPrimByte:
4383 return kStoreByte;
4384 case Primitive::kPrimChar:
4385 case Primitive::kPrimShort:
4386 return kStoreHalfword;
4387 case Primitive::kPrimInt:
4388 return kStoreWord;
4389 case Primitive::kPrimLong:
4390 return kStoreWordPair;
4391 case Primitive::kPrimFloat:
4392 return kStoreSWord;
4393 case Primitive::kPrimDouble:
4394 return kStoreDWord;
4395 default:
4396 LOG(FATAL) << "Unreachable type " << type;
4397 UNREACHABLE();
4398 }
4399}
4400
4401void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4402 Location out_loc,
4403 Register base,
4404 Register reg_offset,
4405 Condition cond) {
4406 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4407 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4408
4409 switch (type) {
4410 case Primitive::kPrimByte:
4411 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4412 break;
4413 case Primitive::kPrimBoolean:
4414 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4415 break;
4416 case Primitive::kPrimShort:
4417 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4418 break;
4419 case Primitive::kPrimChar:
4420 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4421 break;
4422 case Primitive::kPrimNot:
4423 case Primitive::kPrimInt:
4424 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4425 break;
4426 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4427 case Primitive::kPrimLong:
4428 case Primitive::kPrimFloat:
4429 case Primitive::kPrimDouble:
4430 default:
4431 LOG(FATAL) << "Unreachable type " << type;
4432 UNREACHABLE();
4433 }
4434}
4435
4436void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4437 Location loc,
4438 Register base,
4439 Register reg_offset,
4440 Condition cond) {
4441 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4442 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4443
4444 switch (type) {
4445 case Primitive::kPrimByte:
4446 case Primitive::kPrimBoolean:
4447 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4448 break;
4449 case Primitive::kPrimShort:
4450 case Primitive::kPrimChar:
4451 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4452 break;
4453 case Primitive::kPrimNot:
4454 case Primitive::kPrimInt:
4455 __ str(loc.AsRegister<Register>(), mem_address, cond);
4456 break;
4457 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4458 case Primitive::kPrimLong:
4459 case Primitive::kPrimFloat:
4460 case Primitive::kPrimDouble:
4461 default:
4462 LOG(FATAL) << "Unreachable type " << type;
4463 UNREACHABLE();
4464 }
4465}
4466
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004467void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004468 bool object_array_get_with_read_barrier =
4469 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004470 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004471 new (GetGraph()->GetArena()) LocationSummary(instruction,
4472 object_array_get_with_read_barrier ?
4473 LocationSummary::kCallOnSlowPath :
4474 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004475 locations->SetInAt(0, Location::RequiresRegister());
4476 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004477 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4478 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4479 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004480 // The output overlaps in the case of an object array get with
4481 // read barriers enabled: we do not want the move to overwrite the
4482 // array's location, as we need it to emit the read barrier.
4483 locations->SetOut(
4484 Location::RequiresRegister(),
4485 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004486 }
Roland Levillainc9285912015-12-18 10:38:42 +00004487 // We need a temporary register for the read barrier marking slow
4488 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4489 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4490 locations->AddTemp(Location::RequiresRegister());
4491 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004492}
4493
4494void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4495 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004496 Location obj_loc = locations->InAt(0);
4497 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004498 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004499 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004500 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004501 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004502 HInstruction* array_instr = instruction->GetArray();
4503 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004504 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4505 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004506
Roland Levillain4d027112015-07-01 15:41:14 +01004507 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004508 case Primitive::kPrimBoolean:
4509 case Primitive::kPrimByte:
4510 case Primitive::kPrimShort:
4511 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004512 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004513 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004514 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4515 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4516
4517 LoadOperandType load_type = GetLoadOperandType(type);
4518 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004519 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004520 Register temp = IP;
4521
4522 if (has_intermediate_address) {
4523 // We do not need to compute the intermediate address from the array: the
4524 // input instruction has done it already. See the comment in
4525 // `TryExtractArrayAccessAddress()`.
4526 if (kIsDebugBuild) {
4527 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4528 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4529 }
4530 temp = obj;
4531 } else {
4532 __ add(temp, obj, ShifterOperand(data_offset));
4533 }
4534 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 }
4536 break;
4537 }
4538
Roland Levillainc9285912015-12-18 10:38:42 +00004539 case Primitive::kPrimNot: {
4540 static_assert(
4541 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4542 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004543 // /* HeapReference<Object> */ out =
4544 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4545 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4546 Location temp = locations->GetTemp(0);
4547 // Note that a potential implicit null check is handled in this
4548 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4549 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4550 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4551 } else {
4552 Register out = out_loc.AsRegister<Register>();
4553 if (index.IsConstant()) {
4554 size_t offset =
4555 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4556 __ LoadFromOffset(kLoadWord, out, obj, offset);
4557 codegen_->MaybeRecordImplicitNullCheck(instruction);
4558 // If read barriers are enabled, emit read barriers other than
4559 // Baker's using a slow path (and also unpoison the loaded
4560 // reference, if heap poisoning is enabled).
4561 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4562 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004563 Register temp = IP;
4564
4565 if (has_intermediate_address) {
4566 // We do not need to compute the intermediate address from the array: the
4567 // input instruction has done it already. See the comment in
4568 // `TryExtractArrayAccessAddress()`.
4569 if (kIsDebugBuild) {
4570 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4571 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4572 }
4573 temp = obj;
4574 } else {
4575 __ add(temp, obj, ShifterOperand(data_offset));
4576 }
4577 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004578
Roland Levillainc9285912015-12-18 10:38:42 +00004579 codegen_->MaybeRecordImplicitNullCheck(instruction);
4580 // If read barriers are enabled, emit read barriers other than
4581 // Baker's using a slow path (and also unpoison the loaded
4582 // reference, if heap poisoning is enabled).
4583 codegen_->MaybeGenerateReadBarrierSlow(
4584 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4585 }
4586 }
4587 break;
4588 }
4589
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004592 size_t offset =
4593 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004594 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004596 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004597 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598 }
4599 break;
4600 }
4601
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004602 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004603 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004604 if (index.IsConstant()) {
4605 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004606 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004607 } else {
4608 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004609 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004610 }
4611 break;
4612 }
4613
4614 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004615 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004616 if (index.IsConstant()) {
4617 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004618 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004619 } else {
4620 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004621 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004622 }
4623 break;
4624 }
4625
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004627 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004628 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 }
Roland Levillain4d027112015-07-01 15:41:14 +01004630
4631 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004632 // Potential implicit null checks, in the case of reference
4633 // arrays, are handled in the previous switch statement.
4634 } else {
4635 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004636 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004637}
4638
4639void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004640 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004641
4642 bool needs_write_barrier =
4643 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004644 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004645
Nicolas Geoffray39468442014-09-02 15:17:15 +01004646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004647 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004648 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004649 LocationSummary::kCallOnSlowPath :
4650 LocationSummary::kNoCall);
4651
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004652 locations->SetInAt(0, Location::RequiresRegister());
4653 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4654 if (Primitive::IsFloatingPointType(value_type)) {
4655 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004656 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004657 locations->SetInAt(2, Location::RequiresRegister());
4658 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004659 if (needs_write_barrier) {
4660 // Temporary registers for the write barrier.
4661 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004662 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004663 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004664}
4665
4666void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4667 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004668 Location array_loc = locations->InAt(0);
4669 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004670 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004671 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004672 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004673 bool needs_write_barrier =
4674 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004675 uint32_t data_offset =
4676 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4677 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004678 HInstruction* array_instr = instruction->GetArray();
4679 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004680 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4681 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682
4683 switch (value_type) {
4684 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004685 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004686 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004687 case Primitive::kPrimChar:
4688 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004690 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4691 uint32_t full_offset =
4692 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4693 StoreOperandType store_type = GetStoreOperandType(value_type);
4694 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004695 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004696 Register temp = IP;
4697
4698 if (has_intermediate_address) {
4699 // We do not need to compute the intermediate address from the array: the
4700 // input instruction has done it already. See the comment in
4701 // `TryExtractArrayAccessAddress()`.
4702 if (kIsDebugBuild) {
4703 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4704 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4705 }
4706 temp = array;
4707 } else {
4708 __ add(temp, array, ShifterOperand(data_offset));
4709 }
Artem Serov6c916792016-07-11 14:02:34 +01004710 codegen_->StoreToShiftedRegOffset(value_type,
4711 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004712 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004713 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004714 }
4715 break;
4716 }
4717
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004719 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004720 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4721 // See the comment in instruction_simplifier_shared.cc.
4722 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004723
4724 if (instruction->InputAt(2)->IsNullConstant()) {
4725 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004726 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004727 size_t offset =
4728 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004729 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004730 } else {
4731 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004732 __ add(IP, array, ShifterOperand(data_offset));
4733 codegen_->StoreToShiftedRegOffset(value_type,
4734 value_loc,
4735 IP,
4736 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004737 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004738 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004739 DCHECK(!needs_write_barrier);
4740 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004741 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004742 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004743
4744 DCHECK(needs_write_barrier);
4745 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4746 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4747 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4748 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4749 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4750 Label done;
4751 SlowPathCode* slow_path = nullptr;
4752
Roland Levillain3b359c72015-11-17 19:35:12 +00004753 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004754 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4755 codegen_->AddSlowPath(slow_path);
4756 if (instruction->GetValueCanBeNull()) {
4757 Label non_zero;
4758 __ CompareAndBranchIfNonZero(value, &non_zero);
4759 if (index.IsConstant()) {
4760 size_t offset =
4761 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4762 __ StoreToOffset(kStoreWord, value, array, offset);
4763 } else {
4764 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004765 __ add(IP, array, ShifterOperand(data_offset));
4766 codegen_->StoreToShiftedRegOffset(value_type,
4767 value_loc,
4768 IP,
4769 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004770 }
4771 codegen_->MaybeRecordImplicitNullCheck(instruction);
4772 __ b(&done);
4773 __ Bind(&non_zero);
4774 }
4775
Roland Levillain3b359c72015-11-17 19:35:12 +00004776 if (kEmitCompilerReadBarrier) {
4777 // When read barriers are enabled, the type checking
4778 // instrumentation requires two read barriers:
4779 //
4780 // __ Mov(temp2, temp1);
4781 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4782 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004783 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004784 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4785 //
4786 // // /* HeapReference<Class> */ temp2 = value->klass_
4787 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004788 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004789 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4790 //
4791 // __ cmp(temp1, ShifterOperand(temp2));
4792 //
4793 // However, the second read barrier may trash `temp`, as it
4794 // is a temporary register, and as such would not be saved
4795 // along with live registers before calling the runtime (nor
4796 // restored afterwards). So in this case, we bail out and
4797 // delegate the work to the array set slow path.
4798 //
4799 // TODO: Extend the register allocator to support a new
4800 // "(locally) live temp" location so as to avoid always
4801 // going into the slow path when read barriers are enabled.
4802 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004803 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004804 // /* HeapReference<Class> */ temp1 = array->klass_
4805 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4806 codegen_->MaybeRecordImplicitNullCheck(instruction);
4807 __ MaybeUnpoisonHeapReference(temp1);
4808
4809 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4810 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4811 // /* HeapReference<Class> */ temp2 = value->klass_
4812 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4813 // If heap poisoning is enabled, no need to unpoison `temp1`
4814 // nor `temp2`, as we are comparing two poisoned references.
4815 __ cmp(temp1, ShifterOperand(temp2));
4816
4817 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4818 Label do_put;
4819 __ b(&do_put, EQ);
4820 // If heap poisoning is enabled, the `temp1` reference has
4821 // not been unpoisoned yet; unpoison it now.
4822 __ MaybeUnpoisonHeapReference(temp1);
4823
4824 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4825 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4826 // If heap poisoning is enabled, no need to unpoison
4827 // `temp1`, as we are comparing against null below.
4828 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4829 __ Bind(&do_put);
4830 } else {
4831 __ b(slow_path->GetEntryLabel(), NE);
4832 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004833 }
4834 }
4835
Artem Serov6c916792016-07-11 14:02:34 +01004836 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004837 if (kPoisonHeapReferences) {
4838 // Note that in the case where `value` is a null reference,
4839 // we do not enter this block, as a null reference does not
4840 // need poisoning.
4841 DCHECK_EQ(value_type, Primitive::kPrimNot);
4842 __ Mov(temp1, value);
4843 __ PoisonHeapReference(temp1);
4844 source = temp1;
4845 }
4846
4847 if (index.IsConstant()) {
4848 size_t offset =
4849 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4850 __ StoreToOffset(kStoreWord, source, array, offset);
4851 } else {
4852 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004853
4854 __ add(IP, array, ShifterOperand(data_offset));
4855 codegen_->StoreToShiftedRegOffset(value_type,
4856 Location::RegisterLocation(source),
4857 IP,
4858 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004859 }
4860
Roland Levillain3b359c72015-11-17 19:35:12 +00004861 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004862 codegen_->MaybeRecordImplicitNullCheck(instruction);
4863 }
4864
4865 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4866
4867 if (done.IsLinked()) {
4868 __ Bind(&done);
4869 }
4870
4871 if (slow_path != nullptr) {
4872 __ Bind(slow_path->GetExitLabel());
4873 }
4874
4875 break;
4876 }
4877
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004878 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004879 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004881 size_t offset =
4882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004883 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004884 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004885 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004886 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887 }
4888 break;
4889 }
4890
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004891 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004892 Location value = locations->InAt(2);
4893 DCHECK(value.IsFpuRegister());
4894 if (index.IsConstant()) {
4895 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004896 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004897 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004898 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004899 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4900 }
4901 break;
4902 }
4903
4904 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004905 Location value = locations->InAt(2);
4906 DCHECK(value.IsFpuRegisterPair());
4907 if (index.IsConstant()) {
4908 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004909 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004910 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004912 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4913 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004914
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004915 break;
4916 }
4917
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004918 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004919 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004920 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004921 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004922
Roland Levillain80e67092016-01-08 16:04:55 +00004923 // Objects are handled in the switch.
4924 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004925 codegen_->MaybeRecordImplicitNullCheck(instruction);
4926 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004927}
4928
4929void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004930 LocationSummary* locations =
4931 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004932 locations->SetInAt(0, Location::RequiresRegister());
4933 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934}
4935
4936void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4937 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004938 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004939 Register obj = locations->InAt(0).AsRegister<Register>();
4940 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004941 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004942 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004943}
4944
Artem Serov328429f2016-07-06 16:23:04 +01004945void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00004946 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4947 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01004948 LocationSummary* locations =
4949 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4950
4951 locations->SetInAt(0, Location::RequiresRegister());
4952 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4953 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4954}
4955
4956void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4957 LocationSummary* locations = instruction->GetLocations();
4958 Location out = locations->Out();
4959 Location first = locations->InAt(0);
4960 Location second = locations->InAt(1);
4961
Roland Levillain4a3aa572016-08-15 13:17:06 +00004962 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4963 DCHECK(!kEmitCompilerReadBarrier);
4964
Artem Serov328429f2016-07-06 16:23:04 +01004965 if (second.IsRegister()) {
4966 __ add(out.AsRegister<Register>(),
4967 first.AsRegister<Register>(),
4968 ShifterOperand(second.AsRegister<Register>()));
4969 } else {
4970 __ AddConstant(out.AsRegister<Register>(),
4971 first.AsRegister<Register>(),
4972 second.GetConstant()->AsIntConstant()->GetValue());
4973 }
4974}
4975
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004976void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004977 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4978 ? LocationSummary::kCallOnSlowPath
4979 : LocationSummary::kNoCall;
4980 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004981 locations->SetInAt(0, Location::RequiresRegister());
4982 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004983 if (instruction->HasUses()) {
4984 locations->SetOut(Location::SameAsFirstInput());
4985 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004986}
4987
4988void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4989 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004990 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004991 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004992 codegen_->AddSlowPath(slow_path);
4993
Roland Levillain271ab9c2014-11-27 15:23:57 +00004994 Register index = locations->InAt(0).AsRegister<Register>();
4995 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996
4997 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004998 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999}
5000
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005001void CodeGeneratorARM::MarkGCCard(Register temp,
5002 Register card,
5003 Register object,
5004 Register value,
5005 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005006 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005007 if (can_be_null) {
5008 __ CompareAndBranchIfZero(value, &is_null);
5009 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005010 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5012 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005013 if (can_be_null) {
5014 __ Bind(&is_null);
5015 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005016}
5017
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005018void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005019 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005020}
5021
5022void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005023 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5024}
5025
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005026void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
5027 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5028}
5029
5030void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005031 HBasicBlock* block = instruction->GetBlock();
5032 if (block->GetLoopInformation() != nullptr) {
5033 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5034 // The back edge will generate the suspend check.
5035 return;
5036 }
5037 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5038 // The goto will generate the suspend check.
5039 return;
5040 }
5041 GenerateSuspendCheck(instruction, nullptr);
5042}
5043
5044void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5045 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005046 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005047 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5048 if (slow_path == nullptr) {
5049 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5050 instruction->SetSlowPath(slow_path);
5051 codegen_->AddSlowPath(slow_path);
5052 if (successor != nullptr) {
5053 DCHECK(successor->IsLoopHeader());
5054 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5055 }
5056 } else {
5057 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5058 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005059
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005060 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005061 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005062 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005063 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005064 __ Bind(slow_path->GetReturnLabel());
5065 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005066 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005067 __ b(slow_path->GetEntryLabel());
5068 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005069}
5070
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005071ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5072 return codegen_->GetAssembler();
5073}
5074
5075void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005076 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005077 Location source = move->GetSource();
5078 Location destination = move->GetDestination();
5079
5080 if (source.IsRegister()) {
5081 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005082 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005083 } else if (destination.IsFpuRegister()) {
5084 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005085 } else {
5086 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005087 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005088 SP, destination.GetStackIndex());
5089 }
5090 } else if (source.IsStackSlot()) {
5091 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005092 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005093 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005094 } else if (destination.IsFpuRegister()) {
5095 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005096 } else {
5097 DCHECK(destination.IsStackSlot());
5098 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5099 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5100 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005101 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005102 if (destination.IsRegister()) {
5103 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5104 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005105 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005106 } else {
5107 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005108 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5109 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005110 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005111 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005112 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5113 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005114 } else if (destination.IsRegisterPair()) {
5115 DCHECK(ExpectedPairLayout(destination));
5116 __ LoadFromOffset(
5117 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5118 } else {
5119 DCHECK(destination.IsFpuRegisterPair()) << destination;
5120 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5121 SP,
5122 source.GetStackIndex());
5123 }
5124 } else if (source.IsRegisterPair()) {
5125 if (destination.IsRegisterPair()) {
5126 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5127 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005128 } else if (destination.IsFpuRegisterPair()) {
5129 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5130 source.AsRegisterPairLow<Register>(),
5131 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005132 } else {
5133 DCHECK(destination.IsDoubleStackSlot()) << destination;
5134 DCHECK(ExpectedPairLayout(source));
5135 __ StoreToOffset(
5136 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5137 }
5138 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005139 if (destination.IsRegisterPair()) {
5140 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5141 destination.AsRegisterPairHigh<Register>(),
5142 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5143 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005144 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5145 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5146 } else {
5147 DCHECK(destination.IsDoubleStackSlot()) << destination;
5148 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5149 SP,
5150 destination.GetStackIndex());
5151 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005152 } else {
5153 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005154 HConstant* constant = source.GetConstant();
5155 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5156 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005157 if (destination.IsRegister()) {
5158 __ LoadImmediate(destination.AsRegister<Register>(), value);
5159 } else {
5160 DCHECK(destination.IsStackSlot());
5161 __ LoadImmediate(IP, value);
5162 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5163 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005164 } else if (constant->IsLongConstant()) {
5165 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005166 if (destination.IsRegisterPair()) {
5167 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5168 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005169 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005170 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005171 __ LoadImmediate(IP, Low32Bits(value));
5172 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5173 __ LoadImmediate(IP, High32Bits(value));
5174 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5175 }
5176 } else if (constant->IsDoubleConstant()) {
5177 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005178 if (destination.IsFpuRegisterPair()) {
5179 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005180 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005181 DCHECK(destination.IsDoubleStackSlot()) << destination;
5182 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005183 __ LoadImmediate(IP, Low32Bits(int_value));
5184 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5185 __ LoadImmediate(IP, High32Bits(int_value));
5186 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5187 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005188 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005189 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005190 float value = constant->AsFloatConstant()->GetValue();
5191 if (destination.IsFpuRegister()) {
5192 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5193 } else {
5194 DCHECK(destination.IsStackSlot());
5195 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5196 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5197 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005198 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005199 }
5200}
5201
5202void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5203 __ Mov(IP, reg);
5204 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5205 __ StoreToOffset(kStoreWord, IP, SP, mem);
5206}
5207
5208void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5209 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5210 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5211 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5212 SP, mem1 + stack_offset);
5213 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5214 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5215 SP, mem2 + stack_offset);
5216 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5217}
5218
5219void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005220 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005221 Location source = move->GetSource();
5222 Location destination = move->GetDestination();
5223
5224 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005225 DCHECK_NE(source.AsRegister<Register>(), IP);
5226 DCHECK_NE(destination.AsRegister<Register>(), IP);
5227 __ Mov(IP, source.AsRegister<Register>());
5228 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5229 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005230 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005231 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005232 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005233 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005234 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5235 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005236 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005237 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005238 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005239 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005240 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005241 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005242 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005243 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005244 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5245 destination.AsRegisterPairHigh<Register>(),
5246 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005247 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005248 Register low_reg = source.IsRegisterPair()
5249 ? source.AsRegisterPairLow<Register>()
5250 : destination.AsRegisterPairLow<Register>();
5251 int mem = source.IsRegisterPair()
5252 ? destination.GetStackIndex()
5253 : source.GetStackIndex();
5254 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005255 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005256 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005257 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005258 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005259 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5260 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005261 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005262 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005263 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005264 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5265 DRegister reg = source.IsFpuRegisterPair()
5266 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5267 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5268 int mem = source.IsFpuRegisterPair()
5269 ? destination.GetStackIndex()
5270 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005271 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005272 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005273 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005274 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5275 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5276 : destination.AsFpuRegister<SRegister>();
5277 int mem = source.IsFpuRegister()
5278 ? destination.GetStackIndex()
5279 : source.GetStackIndex();
5280
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005281 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005282 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005283 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005284 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005285 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5286 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005287 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005288 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005289 }
5290}
5291
5292void ParallelMoveResolverARM::SpillScratch(int reg) {
5293 __ Push(static_cast<Register>(reg));
5294}
5295
5296void ParallelMoveResolverARM::RestoreScratch(int reg) {
5297 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005298}
5299
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005300HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5301 HLoadClass::LoadKind desired_class_load_kind) {
5302 if (kEmitCompilerReadBarrier) {
5303 switch (desired_class_load_kind) {
5304 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5305 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5306 case HLoadClass::LoadKind::kBootImageAddress:
5307 // TODO: Implement for read barrier.
5308 return HLoadClass::LoadKind::kDexCacheViaMethod;
5309 default:
5310 break;
5311 }
5312 }
5313 switch (desired_class_load_kind) {
5314 case HLoadClass::LoadKind::kReferrersClass:
5315 break;
5316 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5317 DCHECK(!GetCompilerOptions().GetCompilePic());
5318 break;
5319 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5320 DCHECK(GetCompilerOptions().GetCompilePic());
5321 break;
5322 case HLoadClass::LoadKind::kBootImageAddress:
5323 break;
5324 case HLoadClass::LoadKind::kDexCacheAddress:
5325 DCHECK(Runtime::Current()->UseJitCompilation());
5326 break;
5327 case HLoadClass::LoadKind::kDexCachePcRelative:
5328 DCHECK(!Runtime::Current()->UseJitCompilation());
5329 // We disable pc-relative load when there is an irreducible loop, as the optimization
5330 // is incompatible with it.
5331 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5332 // with irreducible loops.
5333 if (GetGraph()->HasIrreducibleLoops()) {
5334 return HLoadClass::LoadKind::kDexCacheViaMethod;
5335 }
5336 break;
5337 case HLoadClass::LoadKind::kDexCacheViaMethod:
5338 break;
5339 }
5340 return desired_class_load_kind;
5341}
5342
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005343void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005344 if (cls->NeedsAccessCheck()) {
5345 InvokeRuntimeCallingConvention calling_convention;
5346 CodeGenerator::CreateLoadClassLocationSummary(
5347 cls,
5348 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5349 Location::RegisterLocation(R0),
5350 /* code_generator_supports_read_barrier */ true);
5351 return;
5352 }
5353
5354 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5355 ? LocationSummary::kCallOnSlowPath
5356 : LocationSummary::kNoCall;
5357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5358 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5359 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5360 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5361 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5362 locations->SetInAt(0, Location::RequiresRegister());
5363 }
5364 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005365}
5366
5367void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005368 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005369 if (cls->NeedsAccessCheck()) {
5370 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5371 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5372 cls,
5373 cls->GetDexPc(),
5374 nullptr);
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
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005382 bool generate_null_check = false;
5383 switch (cls->GetLoadKind()) {
5384 case HLoadClass::LoadKind::kReferrersClass: {
5385 DCHECK(!cls->CanCallRuntime());
5386 DCHECK(!cls->MustGenerateClinitCheck());
5387 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5388 Register current_method = locations->InAt(0).AsRegister<Register>();
5389 GenerateGcRootFieldLoad(
5390 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5391 break;
5392 }
5393 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5394 DCHECK(!kEmitCompilerReadBarrier);
5395 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5396 cls->GetTypeIndex()));
5397 break;
5398 }
5399 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5400 DCHECK(!kEmitCompilerReadBarrier);
5401 CodeGeneratorARM::PcRelativePatchInfo* labels =
5402 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5403 __ BindTrackedLabel(&labels->movw_label);
5404 __ movw(out, /* placeholder */ 0u);
5405 __ BindTrackedLabel(&labels->movt_label);
5406 __ movt(out, /* placeholder */ 0u);
5407 __ BindTrackedLabel(&labels->add_pc_label);
5408 __ add(out, out, ShifterOperand(PC));
5409 break;
5410 }
5411 case HLoadClass::LoadKind::kBootImageAddress: {
5412 DCHECK(!kEmitCompilerReadBarrier);
5413 DCHECK_NE(cls->GetAddress(), 0u);
5414 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5415 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5416 break;
5417 }
5418 case HLoadClass::LoadKind::kDexCacheAddress: {
5419 DCHECK_NE(cls->GetAddress(), 0u);
5420 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5421 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5422 // a 128B range. To try and reduce the number of literals if we load multiple types,
5423 // simply split the dex cache address to a 128B aligned base loaded from a literal
5424 // and the remaining offset embedded in the load.
5425 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5426 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5427 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5428 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5429 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5430 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5431 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5432 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5433 generate_null_check = !cls->IsInDexCache();
5434 break;
5435 }
5436 case HLoadClass::LoadKind::kDexCachePcRelative: {
5437 Register base_reg = locations->InAt(0).AsRegister<Register>();
5438 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5439 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5440 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5441 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5442 generate_null_check = !cls->IsInDexCache();
5443 break;
5444 }
5445 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5446 // /* GcRoot<mirror::Class>[] */ out =
5447 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5448 Register current_method = locations->InAt(0).AsRegister<Register>();
5449 __ LoadFromOffset(kLoadWord,
5450 out,
5451 current_method,
5452 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5453 // /* GcRoot<mirror::Class> */ out = out[type_index]
5454 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5455 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5456 generate_null_check = !cls->IsInDexCache();
5457 }
5458 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005459
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005460 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5461 DCHECK(cls->CanCallRuntime());
5462 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5463 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5464 codegen_->AddSlowPath(slow_path);
5465 if (generate_null_check) {
5466 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5467 }
5468 if (cls->MustGenerateClinitCheck()) {
5469 GenerateClassInitializationCheck(slow_path, out);
5470 } else {
5471 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005472 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005473 }
5474}
5475
5476void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5477 LocationSummary* locations =
5478 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5479 locations->SetInAt(0, Location::RequiresRegister());
5480 if (check->HasUses()) {
5481 locations->SetOut(Location::SameAsFirstInput());
5482 }
5483}
5484
5485void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005486 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005487 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005488 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005489 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005490 GenerateClassInitializationCheck(slow_path,
5491 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005492}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005493
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005494void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005495 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005496 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5497 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5498 __ b(slow_path->GetEntryLabel(), LT);
5499 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5500 // properly. Therefore, we do a memory fence.
5501 __ dmb(ISH);
5502 __ Bind(slow_path->GetExitLabel());
5503}
5504
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005505HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5506 HLoadString::LoadKind desired_string_load_kind) {
5507 if (kEmitCompilerReadBarrier) {
5508 switch (desired_string_load_kind) {
5509 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5510 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5511 case HLoadString::LoadKind::kBootImageAddress:
5512 // TODO: Implement for read barrier.
5513 return HLoadString::LoadKind::kDexCacheViaMethod;
5514 default:
5515 break;
5516 }
5517 }
5518 switch (desired_string_load_kind) {
5519 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5520 DCHECK(!GetCompilerOptions().GetCompilePic());
5521 break;
5522 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5523 DCHECK(GetCompilerOptions().GetCompilePic());
5524 break;
5525 case HLoadString::LoadKind::kBootImageAddress:
5526 break;
5527 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005528 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005529 break;
5530 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005531 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005532 // We disable pc-relative load when there is an irreducible loop, as the optimization
5533 // is incompatible with it.
5534 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5535 // with irreducible loops.
5536 if (GetGraph()->HasIrreducibleLoops()) {
5537 return HLoadString::LoadKind::kDexCacheViaMethod;
5538 }
5539 break;
5540 case HLoadString::LoadKind::kDexCacheViaMethod:
5541 break;
5542 }
5543 return desired_string_load_kind;
5544}
5545
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005546void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005547 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005548 ? LocationSummary::kCallOnSlowPath
5549 : LocationSummary::kNoCall;
5550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005551 HLoadString::LoadKind load_kind = load->GetLoadKind();
5552 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5553 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5554 locations->SetInAt(0, Location::RequiresRegister());
5555 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005556 locations->SetOut(Location::RequiresRegister());
5557}
5558
5559void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005560 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005561 Location out_loc = locations->Out();
5562 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005563
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005564 switch (load->GetLoadKind()) {
5565 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5566 DCHECK(!kEmitCompilerReadBarrier);
5567 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5568 load->GetStringIndex()));
5569 return; // No dex cache slow path.
5570 }
5571 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5572 DCHECK(!kEmitCompilerReadBarrier);
5573 CodeGeneratorARM::PcRelativePatchInfo* labels =
5574 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5575 __ BindTrackedLabel(&labels->movw_label);
5576 __ movw(out, /* placeholder */ 0u);
5577 __ BindTrackedLabel(&labels->movt_label);
5578 __ movt(out, /* placeholder */ 0u);
5579 __ BindTrackedLabel(&labels->add_pc_label);
5580 __ add(out, out, ShifterOperand(PC));
5581 return; // No dex cache slow path.
5582 }
5583 case HLoadString::LoadKind::kBootImageAddress: {
5584 DCHECK(!kEmitCompilerReadBarrier);
5585 DCHECK_NE(load->GetAddress(), 0u);
5586 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5587 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5588 return; // No dex cache slow path.
5589 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005590 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005591 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005592 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005593
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005594 // TODO: Re-add the compiler code to do string dex cache lookup again.
5595 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5596 codegen_->AddSlowPath(slow_path);
5597 __ b(slow_path->GetEntryLabel());
5598 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005599}
5600
David Brazdilcb1c0552015-08-04 16:22:25 +01005601static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005602 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005603}
5604
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005605void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5606 LocationSummary* locations =
5607 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5608 locations->SetOut(Location::RequiresRegister());
5609}
5610
5611void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005612 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005613 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5614}
5615
5616void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5617 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5618}
5619
5620void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005621 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005622 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005623}
5624
5625void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5626 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005627 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005628 InvokeRuntimeCallingConvention calling_convention;
5629 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5630}
5631
5632void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5633 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005634 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005635 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005636}
5637
Roland Levillainc9285912015-12-18 10:38:42 +00005638static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5639 return kEmitCompilerReadBarrier &&
5640 (kUseBakerReadBarrier ||
5641 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5642 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5643 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5644}
5645
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005646void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005647 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005648 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5649 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005650 case TypeCheckKind::kExactCheck:
5651 case TypeCheckKind::kAbstractClassCheck:
5652 case TypeCheckKind::kClassHierarchyCheck:
5653 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005654 call_kind =
5655 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005656 break;
5657 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005658 case TypeCheckKind::kUnresolvedCheck:
5659 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005660 call_kind = LocationSummary::kCallOnSlowPath;
5661 break;
5662 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005663
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005665 locations->SetInAt(0, Location::RequiresRegister());
5666 locations->SetInAt(1, Location::RequiresRegister());
5667 // The "out" register is used as a temporary, so it overlaps with the inputs.
5668 // Note that TypeCheckSlowPathARM uses this register too.
5669 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5670 // When read barriers are enabled, we need a temporary register for
5671 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005672 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005673 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005674 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005675}
5676
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005677void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005678 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005679 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005680 Location obj_loc = locations->InAt(0);
5681 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005682 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005683 Location out_loc = locations->Out();
5684 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005685 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005686 locations->GetTemp(0) :
5687 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005688 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005689 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5690 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5691 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005692 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005693 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005694
5695 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005696 // avoid null check if we know obj is not null.
5697 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005698 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005699 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005700
Roland Levillain3b359c72015-11-17 19:35:12 +00005701 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005702 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005703
Roland Levillainc9285912015-12-18 10:38:42 +00005704 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005705 case TypeCheckKind::kExactCheck: {
5706 __ cmp(out, ShifterOperand(cls));
5707 // Classes must be equal for the instanceof to succeed.
5708 __ b(&zero, NE);
5709 __ LoadImmediate(out, 1);
5710 __ b(&done);
5711 break;
5712 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005713
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005714 case TypeCheckKind::kAbstractClassCheck: {
5715 // If the class is abstract, we eagerly fetch the super class of the
5716 // object to avoid doing a comparison we know will fail.
5717 Label loop;
5718 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005719 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005720 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005721 // If `out` is null, we use it for the result, and jump to `done`.
5722 __ CompareAndBranchIfZero(out, &done);
5723 __ cmp(out, ShifterOperand(cls));
5724 __ b(&loop, NE);
5725 __ LoadImmediate(out, 1);
5726 if (zero.IsLinked()) {
5727 __ b(&done);
5728 }
5729 break;
5730 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005731
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005732 case TypeCheckKind::kClassHierarchyCheck: {
5733 // Walk over the class hierarchy to find a match.
5734 Label loop, success;
5735 __ Bind(&loop);
5736 __ cmp(out, ShifterOperand(cls));
5737 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005738 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005739 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005740 __ CompareAndBranchIfNonZero(out, &loop);
5741 // If `out` is null, we use it for the result, and jump to `done`.
5742 __ b(&done);
5743 __ Bind(&success);
5744 __ LoadImmediate(out, 1);
5745 if (zero.IsLinked()) {
5746 __ b(&done);
5747 }
5748 break;
5749 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005750
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005751 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005752 // Do an exact check.
5753 Label exact_check;
5754 __ cmp(out, ShifterOperand(cls));
5755 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005756 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005757 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005758 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005759 // If `out` is null, we use it for the result, and jump to `done`.
5760 __ CompareAndBranchIfZero(out, &done);
5761 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5762 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5763 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005764 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765 __ LoadImmediate(out, 1);
5766 __ b(&done);
5767 break;
5768 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005769
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005770 case TypeCheckKind::kArrayCheck: {
5771 __ cmp(out, ShifterOperand(cls));
5772 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005773 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5774 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 codegen_->AddSlowPath(slow_path);
5776 __ b(slow_path->GetEntryLabel(), NE);
5777 __ LoadImmediate(out, 1);
5778 if (zero.IsLinked()) {
5779 __ b(&done);
5780 }
5781 break;
5782 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005783
Calin Juravle98893e12015-10-02 21:05:03 +01005784 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005785 case TypeCheckKind::kInterfaceCheck: {
5786 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005787 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005788 // cases.
5789 //
5790 // We cannot directly call the InstanceofNonTrivial runtime
5791 // entry point without resorting to a type checking slow path
5792 // here (i.e. by calling InvokeRuntime directly), as it would
5793 // require to assign fixed registers for the inputs of this
5794 // HInstanceOf instruction (following the runtime calling
5795 // convention), which might be cluttered by the potential first
5796 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005797 //
5798 // TODO: Introduce a new runtime entry point taking the object
5799 // to test (instead of its class) as argument, and let it deal
5800 // with the read barrier issues. This will let us refactor this
5801 // case of the `switch` code as it was previously (with a direct
5802 // call to the runtime not using a type checking slow path).
5803 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005804 DCHECK(locations->OnlyCallsOnSlowPath());
5805 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5806 /* is_fatal */ false);
5807 codegen_->AddSlowPath(slow_path);
5808 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 if (zero.IsLinked()) {
5810 __ b(&done);
5811 }
5812 break;
5813 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005814 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005815
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005816 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005817 __ Bind(&zero);
5818 __ LoadImmediate(out, 0);
5819 }
5820
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005821 if (done.IsLinked()) {
5822 __ Bind(&done);
5823 }
5824
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005825 if (slow_path != nullptr) {
5826 __ Bind(slow_path->GetExitLabel());
5827 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005828}
5829
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005830void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5832 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5833
Roland Levillain3b359c72015-11-17 19:35:12 +00005834 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5835 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 case TypeCheckKind::kExactCheck:
5837 case TypeCheckKind::kAbstractClassCheck:
5838 case TypeCheckKind::kClassHierarchyCheck:
5839 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005840 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5841 LocationSummary::kCallOnSlowPath :
5842 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005843 break;
5844 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005845 case TypeCheckKind::kUnresolvedCheck:
5846 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005847 call_kind = LocationSummary::kCallOnSlowPath;
5848 break;
5849 }
5850
Roland Levillain3b359c72015-11-17 19:35:12 +00005851 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5852 locations->SetInAt(0, Location::RequiresRegister());
5853 locations->SetInAt(1, Location::RequiresRegister());
5854 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5855 locations->AddTemp(Location::RequiresRegister());
5856 // When read barriers are enabled, we need an additional temporary
5857 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005858 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005859 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005861}
5862
5863void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005864 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005865 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005866 Location obj_loc = locations->InAt(0);
5867 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005868 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005869 Location temp_loc = locations->GetTemp(0);
5870 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005871 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005872 locations->GetTemp(1) :
5873 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005874 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005875 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5876 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5877 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005878
Roland Levillain3b359c72015-11-17 19:35:12 +00005879 bool is_type_check_slow_path_fatal =
5880 (type_check_kind == TypeCheckKind::kExactCheck ||
5881 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5882 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5883 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5884 !instruction->CanThrowIntoCatchBlock();
5885 SlowPathCode* type_check_slow_path =
5886 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5887 is_type_check_slow_path_fatal);
5888 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889
5890 Label done;
5891 // Avoid null check if we know obj is not null.
5892 if (instruction->MustDoNullCheck()) {
5893 __ CompareAndBranchIfZero(obj, &done);
5894 }
5895
Roland Levillain3b359c72015-11-17 19:35:12 +00005896 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005897 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005898
Roland Levillain3b359c72015-11-17 19:35:12 +00005899 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005900 case TypeCheckKind::kExactCheck:
5901 case TypeCheckKind::kArrayCheck: {
5902 __ cmp(temp, ShifterOperand(cls));
5903 // Jump to slow path for throwing the exception or doing a
5904 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005905 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906 break;
5907 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005908
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005909 case TypeCheckKind::kAbstractClassCheck: {
5910 // If the class is abstract, we eagerly fetch the super class of the
5911 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005912 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005913 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005914 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005915 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005916
5917 // If the class reference currently in `temp` is not null, jump
5918 // to the `compare_classes` label to compare it with the checked
5919 // class.
5920 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5921 // Otherwise, jump to the slow path to throw the exception.
5922 //
5923 // But before, move back the object's class into `temp` before
5924 // going into the slow path, as it has been overwritten in the
5925 // meantime.
5926 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005927 GenerateReferenceLoadTwoRegisters(
5928 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005929 __ b(type_check_slow_path->GetEntryLabel());
5930
5931 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 __ cmp(temp, ShifterOperand(cls));
5933 __ b(&loop, NE);
5934 break;
5935 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005936
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937 case TypeCheckKind::kClassHierarchyCheck: {
5938 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005939 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940 __ Bind(&loop);
5941 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005942 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005943
Roland Levillain3b359c72015-11-17 19:35:12 +00005944 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005945 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005946
5947 // If the class reference currently in `temp` is not null, jump
5948 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005949 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005950 // Otherwise, jump to the slow path to throw the exception.
5951 //
5952 // But before, move back the object's class into `temp` before
5953 // going into the slow path, as it has been overwritten in the
5954 // meantime.
5955 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005956 GenerateReferenceLoadTwoRegisters(
5957 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005958 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 break;
5960 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005961
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005962 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005963 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005964 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005965 __ cmp(temp, ShifterOperand(cls));
5966 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005967
5968 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005969 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005970 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005971
5972 // If the component type is not null (i.e. the object is indeed
5973 // an array), jump to label `check_non_primitive_component_type`
5974 // to further check that this component type is not a primitive
5975 // type.
5976 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5977 // Otherwise, jump to the slow path to throw the exception.
5978 //
5979 // But before, move back the object's class into `temp` before
5980 // going into the slow path, as it has been overwritten in the
5981 // meantime.
5982 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005983 GenerateReferenceLoadTwoRegisters(
5984 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005985 __ b(type_check_slow_path->GetEntryLabel());
5986
5987 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005989 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5990 __ CompareAndBranchIfZero(temp, &done);
5991 // Same comment as above regarding `temp` and the slow path.
5992 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005993 GenerateReferenceLoadTwoRegisters(
5994 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005995 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996 break;
5997 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005998
Calin Juravle98893e12015-10-02 21:05:03 +01005999 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006000 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006001 // We always go into the type check slow path for the unresolved
6002 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006003 //
6004 // We cannot directly call the CheckCast runtime entry point
6005 // without resorting to a type checking slow path here (i.e. by
6006 // calling InvokeRuntime directly), as it would require to
6007 // assign fixed registers for the inputs of this HInstanceOf
6008 // instruction (following the runtime calling convention), which
6009 // might be cluttered by the potential first read barrier
6010 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006011 //
6012 // TODO: Introduce a new runtime entry point taking the object
6013 // to test (instead of its class) as argument, and let it deal
6014 // with the read barrier issues. This will let us refactor this
6015 // case of the `switch` code as it was previously (with a direct
6016 // call to the runtime not using a type checking slow path).
6017 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006018 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006019 break;
6020 }
6021 __ Bind(&done);
6022
Roland Levillain3b359c72015-11-17 19:35:12 +00006023 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006024}
6025
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006026void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6027 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006029 InvokeRuntimeCallingConvention calling_convention;
6030 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6031}
6032
6033void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6034 codegen_->InvokeRuntime(instruction->IsEnter()
6035 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
6036 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00006037 instruction->GetDexPc(),
6038 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006039 if (instruction->IsEnter()) {
6040 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6041 } else {
6042 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6043 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006044}
6045
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006046void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6047void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6048void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006049
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006050void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006051 LocationSummary* locations =
6052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6053 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6054 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006055 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006056 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006057 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006059}
6060
6061void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6062 HandleBitwiseOperation(instruction);
6063}
6064
6065void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6066 HandleBitwiseOperation(instruction);
6067}
6068
6069void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6070 HandleBitwiseOperation(instruction);
6071}
6072
Artem Serov7fc63502016-02-09 17:15:29 +00006073
6074void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6075 LocationSummary* locations =
6076 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6077 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6078 || instruction->GetResultType() == Primitive::kPrimLong);
6079
6080 locations->SetInAt(0, Location::RequiresRegister());
6081 locations->SetInAt(1, Location::RequiresRegister());
6082 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6083}
6084
6085void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6086 LocationSummary* locations = instruction->GetLocations();
6087 Location first = locations->InAt(0);
6088 Location second = locations->InAt(1);
6089 Location out = locations->Out();
6090
6091 if (instruction->GetResultType() == Primitive::kPrimInt) {
6092 Register first_reg = first.AsRegister<Register>();
6093 ShifterOperand second_reg(second.AsRegister<Register>());
6094 Register out_reg = out.AsRegister<Register>();
6095
6096 switch (instruction->GetOpKind()) {
6097 case HInstruction::kAnd:
6098 __ bic(out_reg, first_reg, second_reg);
6099 break;
6100 case HInstruction::kOr:
6101 __ orn(out_reg, first_reg, second_reg);
6102 break;
6103 // There is no EON on arm.
6104 case HInstruction::kXor:
6105 default:
6106 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6107 UNREACHABLE();
6108 }
6109 return;
6110
6111 } else {
6112 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6113 Register first_low = first.AsRegisterPairLow<Register>();
6114 Register first_high = first.AsRegisterPairHigh<Register>();
6115 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6116 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6117 Register out_low = out.AsRegisterPairLow<Register>();
6118 Register out_high = out.AsRegisterPairHigh<Register>();
6119
6120 switch (instruction->GetOpKind()) {
6121 case HInstruction::kAnd:
6122 __ bic(out_low, first_low, second_low);
6123 __ bic(out_high, first_high, second_high);
6124 break;
6125 case HInstruction::kOr:
6126 __ orn(out_low, first_low, second_low);
6127 __ orn(out_high, first_high, second_high);
6128 break;
6129 // There is no EON on arm.
6130 case HInstruction::kXor:
6131 default:
6132 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6133 UNREACHABLE();
6134 }
6135 }
6136}
6137
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006138void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6139 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6140 if (value == 0xffffffffu) {
6141 if (out != first) {
6142 __ mov(out, ShifterOperand(first));
6143 }
6144 return;
6145 }
6146 if (value == 0u) {
6147 __ mov(out, ShifterOperand(0));
6148 return;
6149 }
6150 ShifterOperand so;
6151 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6152 __ and_(out, first, so);
6153 } else {
6154 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6155 __ bic(out, first, ShifterOperand(~value));
6156 }
6157}
6158
6159void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6160 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6161 if (value == 0u) {
6162 if (out != first) {
6163 __ mov(out, ShifterOperand(first));
6164 }
6165 return;
6166 }
6167 if (value == 0xffffffffu) {
6168 __ mvn(out, ShifterOperand(0));
6169 return;
6170 }
6171 ShifterOperand so;
6172 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6173 __ orr(out, first, so);
6174 } else {
6175 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6176 __ orn(out, first, ShifterOperand(~value));
6177 }
6178}
6179
6180void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6181 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6182 if (value == 0u) {
6183 if (out != first) {
6184 __ mov(out, ShifterOperand(first));
6185 }
6186 return;
6187 }
6188 __ eor(out, first, ShifterOperand(value));
6189}
6190
Vladimir Marko59751a72016-08-05 14:37:27 +01006191void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6192 Location first,
6193 uint64_t value) {
6194 Register out_low = out.AsRegisterPairLow<Register>();
6195 Register out_high = out.AsRegisterPairHigh<Register>();
6196 Register first_low = first.AsRegisterPairLow<Register>();
6197 Register first_high = first.AsRegisterPairHigh<Register>();
6198 uint32_t value_low = Low32Bits(value);
6199 uint32_t value_high = High32Bits(value);
6200 if (value_low == 0u) {
6201 if (out_low != first_low) {
6202 __ mov(out_low, ShifterOperand(first_low));
6203 }
6204 __ AddConstant(out_high, first_high, value_high);
6205 return;
6206 }
6207 __ AddConstantSetFlags(out_low, first_low, value_low);
6208 ShifterOperand so;
6209 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6210 __ adc(out_high, first_high, so);
6211 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6212 __ sbc(out_high, first_high, so);
6213 } else {
6214 LOG(FATAL) << "Unexpected constant " << value_high;
6215 UNREACHABLE();
6216 }
6217}
6218
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006219void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6220 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006221 Location first = locations->InAt(0);
6222 Location second = locations->InAt(1);
6223 Location out = locations->Out();
6224
6225 if (second.IsConstant()) {
6226 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6227 uint32_t value_low = Low32Bits(value);
6228 if (instruction->GetResultType() == Primitive::kPrimInt) {
6229 Register first_reg = first.AsRegister<Register>();
6230 Register out_reg = out.AsRegister<Register>();
6231 if (instruction->IsAnd()) {
6232 GenerateAndConst(out_reg, first_reg, value_low);
6233 } else if (instruction->IsOr()) {
6234 GenerateOrrConst(out_reg, first_reg, value_low);
6235 } else {
6236 DCHECK(instruction->IsXor());
6237 GenerateEorConst(out_reg, first_reg, value_low);
6238 }
6239 } else {
6240 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6241 uint32_t value_high = High32Bits(value);
6242 Register first_low = first.AsRegisterPairLow<Register>();
6243 Register first_high = first.AsRegisterPairHigh<Register>();
6244 Register out_low = out.AsRegisterPairLow<Register>();
6245 Register out_high = out.AsRegisterPairHigh<Register>();
6246 if (instruction->IsAnd()) {
6247 GenerateAndConst(out_low, first_low, value_low);
6248 GenerateAndConst(out_high, first_high, value_high);
6249 } else if (instruction->IsOr()) {
6250 GenerateOrrConst(out_low, first_low, value_low);
6251 GenerateOrrConst(out_high, first_high, value_high);
6252 } else {
6253 DCHECK(instruction->IsXor());
6254 GenerateEorConst(out_low, first_low, value_low);
6255 GenerateEorConst(out_high, first_high, value_high);
6256 }
6257 }
6258 return;
6259 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006260
6261 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006262 Register first_reg = first.AsRegister<Register>();
6263 ShifterOperand second_reg(second.AsRegister<Register>());
6264 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006265 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006266 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006267 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006268 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006269 } else {
6270 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006271 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006272 }
6273 } else {
6274 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006275 Register first_low = first.AsRegisterPairLow<Register>();
6276 Register first_high = first.AsRegisterPairHigh<Register>();
6277 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6278 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6279 Register out_low = out.AsRegisterPairLow<Register>();
6280 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006281 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006282 __ and_(out_low, first_low, second_low);
6283 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006284 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006285 __ orr(out_low, first_low, second_low);
6286 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006287 } else {
6288 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006289 __ eor(out_low, first_low, second_low);
6290 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006291 }
6292 }
6293}
6294
Roland Levillainc9285912015-12-18 10:38:42 +00006295void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6296 Location out,
6297 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006298 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006299 Register out_reg = out.AsRegister<Register>();
6300 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006301 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006302 if (kUseBakerReadBarrier) {
6303 // Load with fast path based Baker's read barrier.
6304 // /* HeapReference<Object> */ out = *(out + offset)
6305 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006306 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006307 } else {
6308 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006309 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006310 // in the following move operation, as we will need it for the
6311 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006312 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006313 // /* HeapReference<Object> */ out = *(out + offset)
6314 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006315 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006316 }
6317 } else {
6318 // Plain load with no read barrier.
6319 // /* HeapReference<Object> */ out = *(out + offset)
6320 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6321 __ MaybeUnpoisonHeapReference(out_reg);
6322 }
6323}
6324
6325void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6326 Location out,
6327 Location obj,
6328 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006329 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006330 Register out_reg = out.AsRegister<Register>();
6331 Register obj_reg = obj.AsRegister<Register>();
6332 if (kEmitCompilerReadBarrier) {
6333 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006334 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006335 // Load with fast path based Baker's read barrier.
6336 // /* HeapReference<Object> */ out = *(obj + offset)
6337 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006338 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006339 } else {
6340 // Load with slow path based read barrier.
6341 // /* HeapReference<Object> */ out = *(obj + offset)
6342 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6343 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6344 }
6345 } else {
6346 // Plain load with no read barrier.
6347 // /* HeapReference<Object> */ out = *(obj + offset)
6348 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6349 __ MaybeUnpoisonHeapReference(out_reg);
6350 }
6351}
6352
6353void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6354 Location root,
6355 Register obj,
6356 uint32_t offset) {
6357 Register root_reg = root.AsRegister<Register>();
6358 if (kEmitCompilerReadBarrier) {
6359 if (kUseBakerReadBarrier) {
6360 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6361 // Baker's read barrier are used:
6362 //
6363 // root = obj.field;
6364 // if (Thread::Current()->GetIsGcMarking()) {
6365 // root = ReadBarrier::Mark(root)
6366 // }
6367
6368 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6369 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6370 static_assert(
6371 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6372 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6373 "have different sizes.");
6374 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6375 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6376 "have different sizes.");
6377
Vladimir Marko953437b2016-08-24 08:30:46 +00006378 // Slow path marking the GC root `root`.
Roland Levillainc9285912015-12-18 10:38:42 +00006379 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006380 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006381 codegen_->AddSlowPath(slow_path);
6382
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006383 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006384 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006385 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006386 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6387 __ Bind(slow_path->GetExitLabel());
6388 } else {
6389 // GC root loaded through a slow path for read barriers other
6390 // than Baker's.
6391 // /* GcRoot<mirror::Object>* */ root = obj + offset
6392 __ AddConstant(root_reg, obj, offset);
6393 // /* mirror::Object* */ root = root->Read()
6394 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6395 }
6396 } else {
6397 // Plain GC root load with no read barrier.
6398 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6399 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6400 // Note that GC roots are not affected by heap poisoning, thus we
6401 // do not have to unpoison `root_reg` here.
6402 }
6403}
6404
6405void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6406 Location ref,
6407 Register obj,
6408 uint32_t offset,
6409 Location temp,
6410 bool needs_null_check) {
6411 DCHECK(kEmitCompilerReadBarrier);
6412 DCHECK(kUseBakerReadBarrier);
6413
6414 // /* HeapReference<Object> */ ref = *(obj + offset)
6415 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006416 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006417 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006418 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006419}
6420
6421void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6422 Location ref,
6423 Register obj,
6424 uint32_t data_offset,
6425 Location index,
6426 Location temp,
6427 bool needs_null_check) {
6428 DCHECK(kEmitCompilerReadBarrier);
6429 DCHECK(kUseBakerReadBarrier);
6430
Roland Levillainbfea3352016-06-23 13:48:47 +01006431 static_assert(
6432 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6433 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006434 // /* HeapReference<Object> */ ref =
6435 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006436 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006437 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006438 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006439}
6440
6441void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6442 Location ref,
6443 Register obj,
6444 uint32_t offset,
6445 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006446 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006447 Location temp,
6448 bool needs_null_check) {
6449 DCHECK(kEmitCompilerReadBarrier);
6450 DCHECK(kUseBakerReadBarrier);
6451
6452 // In slow path based read barriers, the read barrier call is
6453 // inserted after the original load. However, in fast path based
6454 // Baker's read barriers, we need to perform the load of
6455 // mirror::Object::monitor_ *before* the original reference load.
6456 // This load-load ordering is required by the read barrier.
6457 // The fast path/slow path (for Baker's algorithm) should look like:
6458 //
6459 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6460 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6461 // HeapReference<Object> ref = *src; // Original reference load.
6462 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6463 // if (is_gray) {
6464 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6465 // }
6466 //
6467 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006468 // slightly more complex as it performs additional checks that we do
6469 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006470
6471 Register ref_reg = ref.AsRegister<Register>();
6472 Register temp_reg = temp.AsRegister<Register>();
6473 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6474
6475 // /* int32_t */ monitor = obj->monitor_
6476 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6477 if (needs_null_check) {
6478 MaybeRecordImplicitNullCheck(instruction);
6479 }
6480 // /* LockWord */ lock_word = LockWord(monitor)
6481 static_assert(sizeof(LockWord) == sizeof(int32_t),
6482 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006483
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006484 // Introduce a dependency on the lock_word including the rb_state,
6485 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006486 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006487 // `obj` is unchanged by this operation, but its value now depends
6488 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006489 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006490
6491 // The actual reference load.
6492 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006493 // Load types involving an "index": ArrayGet and
6494 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6495 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006496 if (index.IsConstant()) {
6497 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006498 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006499 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6500 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006501 // Handle the special case of the
6502 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6503 // a register pair as index ("long offset"), of which only the low
6504 // part contains data.
6505 Register index_reg = index.IsRegisterPair()
6506 ? index.AsRegisterPairLow<Register>()
6507 : index.AsRegister<Register>();
6508 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006509 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6510 }
6511 } else {
6512 // /* HeapReference<Object> */ ref = *(obj + offset)
6513 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6514 }
6515
6516 // Object* ref = ref_addr->AsMirrorPtr()
6517 __ MaybeUnpoisonHeapReference(ref_reg);
6518
Vladimir Marko953437b2016-08-24 08:30:46 +00006519 // Slow path marking the object `ref` when it is gray.
Roland Levillainc9285912015-12-18 10:38:42 +00006520 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006521 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006522 AddSlowPath(slow_path);
6523
6524 // if (rb_state == ReadBarrier::gray_ptr_)
6525 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006526 // Given the numeric representation, it's enough to check the low bit of the
6527 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6528 // which can be a 16-bit instruction unlike the TST immediate.
6529 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6530 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6531 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6532 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6533 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006534 __ Bind(slow_path->GetExitLabel());
6535}
6536
6537void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6538 Location out,
6539 Location ref,
6540 Location obj,
6541 uint32_t offset,
6542 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006543 DCHECK(kEmitCompilerReadBarrier);
6544
Roland Levillainc9285912015-12-18 10:38:42 +00006545 // Insert a slow path based read barrier *after* the reference load.
6546 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006547 // If heap poisoning is enabled, the unpoisoning of the loaded
6548 // reference will be carried out by the runtime within the slow
6549 // path.
6550 //
6551 // Note that `ref` currently does not get unpoisoned (when heap
6552 // poisoning is enabled), which is alright as the `ref` argument is
6553 // not used by the artReadBarrierSlow entry point.
6554 //
6555 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6556 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6557 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6558 AddSlowPath(slow_path);
6559
Roland Levillain3b359c72015-11-17 19:35:12 +00006560 __ b(slow_path->GetEntryLabel());
6561 __ Bind(slow_path->GetExitLabel());
6562}
6563
Roland Levillainc9285912015-12-18 10:38:42 +00006564void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6565 Location out,
6566 Location ref,
6567 Location obj,
6568 uint32_t offset,
6569 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006570 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006571 // Baker's read barriers shall be handled by the fast path
6572 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6573 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006574 // If heap poisoning is enabled, unpoisoning will be taken care of
6575 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006576 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006577 } else if (kPoisonHeapReferences) {
6578 __ UnpoisonHeapReference(out.AsRegister<Register>());
6579 }
6580}
6581
Roland Levillainc9285912015-12-18 10:38:42 +00006582void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6583 Location out,
6584 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006585 DCHECK(kEmitCompilerReadBarrier);
6586
Roland Levillainc9285912015-12-18 10:38:42 +00006587 // Insert a slow path based read barrier *after* the GC root load.
6588 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006589 // Note that GC roots are not affected by heap poisoning, so we do
6590 // not need to do anything special for this here.
6591 SlowPathCode* slow_path =
6592 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6593 AddSlowPath(slow_path);
6594
Roland Levillain3b359c72015-11-17 19:35:12 +00006595 __ b(slow_path->GetEntryLabel());
6596 __ Bind(slow_path->GetExitLabel());
6597}
6598
Vladimir Markodc151b22015-10-15 18:02:30 +01006599HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6600 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6601 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006602 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6603 // We disable pc-relative load when there is an irreducible loop, as the optimization
6604 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006605 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6606 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006607 if (GetGraph()->HasIrreducibleLoops() &&
6608 (dispatch_info.method_load_kind ==
6609 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6610 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6611 }
6612
6613 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006614 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6615 if (&outer_dex_file != target_method.dex_file) {
6616 // Calls across dex files are more likely to exceed the available BL range,
6617 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6618 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6619 (desired_dispatch_info.method_load_kind ==
6620 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6621 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6622 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6623 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006624 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006625 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006626 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006627 0u
6628 };
6629 }
6630 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006631 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006632}
6633
Vladimir Markob4536b72015-11-24 13:45:23 +00006634Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6635 Register temp) {
6636 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6637 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6638 if (!invoke->GetLocations()->Intrinsified()) {
6639 return location.AsRegister<Register>();
6640 }
6641 // For intrinsics we allow any location, so it may be on the stack.
6642 if (!location.IsRegister()) {
6643 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6644 return temp;
6645 }
6646 // For register locations, check if the register was saved. If so, get it from the stack.
6647 // Note: There is a chance that the register was saved but not overwritten, so we could
6648 // save one load. However, since this is just an intrinsic slow path we prefer this
6649 // simple and more robust approach rather that trying to determine if that's the case.
6650 SlowPathCode* slow_path = GetCurrentSlowPath();
6651 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6652 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6653 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6654 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6655 return temp;
6656 }
6657 return location.AsRegister<Register>();
6658}
6659
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006660void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006661 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006662 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006663 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6664 // LR = code address from literal pool with link-time patch.
6665 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006666 break;
6667 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6668 // LR = invoke->GetDirectCodePtr();
6669 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006670 break;
6671 default:
6672 break;
6673 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006674
Vladimir Marko58155012015-08-19 12:49:41 +00006675 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6676 switch (invoke->GetMethodLoadKind()) {
6677 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6678 // temp = thread->string_init_entrypoint
6679 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6680 break;
6681 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006682 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006683 break;
6684 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6685 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6686 break;
6687 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6688 __ LoadLiteral(temp.AsRegister<Register>(),
6689 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6690 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006691 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6692 HArmDexCacheArraysBase* base =
6693 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6694 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6695 temp.AsRegister<Register>());
6696 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6697 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6698 break;
6699 }
Vladimir Marko58155012015-08-19 12:49:41 +00006700 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006701 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006702 Register method_reg;
6703 Register reg = temp.AsRegister<Register>();
6704 if (current_method.IsRegister()) {
6705 method_reg = current_method.AsRegister<Register>();
6706 } else {
6707 DCHECK(invoke->GetLocations()->Intrinsified());
6708 DCHECK(!current_method.IsValid());
6709 method_reg = reg;
6710 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6711 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006712 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6713 __ LoadFromOffset(kLoadWord,
6714 reg,
6715 method_reg,
6716 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006717 // temp = temp[index_in_cache];
6718 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6719 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006720 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6721 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006722 }
Vladimir Marko58155012015-08-19 12:49:41 +00006723 }
6724
6725 switch (invoke->GetCodePtrLocation()) {
6726 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6727 __ bl(GetFrameEntryLabel());
6728 break;
6729 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006730 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006731 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006732 // Arbitrarily branch to the BL itself, override at link time.
6733 __ bl(&relative_call_patches_.back().label);
6734 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006735 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6736 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6737 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006738 // LR()
6739 __ blx(LR);
6740 break;
6741 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6742 // LR = callee_method->entry_point_from_quick_compiled_code_
6743 __ LoadFromOffset(
6744 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006745 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006746 // LR()
6747 __ blx(LR);
6748 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006749 }
6750
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006751 DCHECK(!IsLeafMethod());
6752}
6753
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006754void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6755 Register temp = temp_location.AsRegister<Register>();
6756 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6757 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006758
6759 // Use the calling convention instead of the location of the receiver, as
6760 // intrinsics may have put the receiver in a different register. In the intrinsics
6761 // slow path, the arguments have been moved to the right place, so here we are
6762 // guaranteed that the receiver is the first register of the calling convention.
6763 InvokeDexCallingConvention calling_convention;
6764 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006765 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006766 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006767 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006768 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006769 // Instead of simply (possibly) unpoisoning `temp` here, we should
6770 // emit a read barrier for the previous class reference load.
6771 // However this is not required in practice, as this is an
6772 // intermediate/temporary reference and because the current
6773 // concurrent copying collector keeps the from-space memory
6774 // intact/accessible until the end of the marking phase (the
6775 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006776 __ MaybeUnpoisonHeapReference(temp);
6777 // temp = temp->GetMethodAt(method_offset);
6778 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006779 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006780 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6781 // LR = temp->GetEntryPoint();
6782 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6783 // LR();
6784 __ blx(LR);
6785}
6786
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006787CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6788 const DexFile& dex_file, uint32_t string_index) {
6789 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6790}
6791
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006792CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6793 const DexFile& dex_file, uint32_t type_index) {
6794 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6795}
6796
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006797CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6798 const DexFile& dex_file, uint32_t element_offset) {
6799 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6800}
6801
6802CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6803 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6804 patches->emplace_back(dex_file, offset_or_index);
6805 return &patches->back();
6806}
6807
6808Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6809 uint32_t string_index) {
6810 return boot_image_string_patches_.GetOrCreate(
6811 StringReference(&dex_file, string_index),
6812 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6813}
6814
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006815Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6816 uint32_t type_index) {
6817 return boot_image_type_patches_.GetOrCreate(
6818 TypeReference(&dex_file, type_index),
6819 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6820}
6821
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006822Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6823 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6824 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6825 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6826}
6827
6828Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6829 return DeduplicateUint32Literal(address, &uint32_literals_);
6830}
6831
Vladimir Marko58155012015-08-19 12:49:41 +00006832void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6833 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006834 size_t size =
6835 method_patches_.size() +
6836 call_patches_.size() +
6837 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006838 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6839 boot_image_string_patches_.size() +
6840 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006841 boot_image_type_patches_.size() +
6842 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006843 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006844 linker_patches->reserve(size);
6845 for (const auto& entry : method_patches_) {
6846 const MethodReference& target_method = entry.first;
6847 Literal* literal = entry.second;
6848 DCHECK(literal->GetLabel()->IsBound());
6849 uint32_t literal_offset = literal->GetLabel()->Position();
6850 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6851 target_method.dex_file,
6852 target_method.dex_method_index));
6853 }
6854 for (const auto& entry : call_patches_) {
6855 const MethodReference& target_method = entry.first;
6856 Literal* literal = entry.second;
6857 DCHECK(literal->GetLabel()->IsBound());
6858 uint32_t literal_offset = literal->GetLabel()->Position();
6859 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6860 target_method.dex_file,
6861 target_method.dex_method_index));
6862 }
6863 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6864 uint32_t literal_offset = info.label.Position();
6865 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6866 info.target_method.dex_file,
6867 info.target_method.dex_method_index));
6868 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006869 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6870 const DexFile& dex_file = info.target_dex_file;
6871 size_t base_element_offset = info.offset_or_index;
6872 DCHECK(info.add_pc_label.IsBound());
6873 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006874 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006875 DCHECK(info.movw_label.IsBound());
6876 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006877 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6878 &dex_file,
6879 add_pc_offset,
6880 base_element_offset));
6881 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006882 DCHECK(info.movt_label.IsBound());
6883 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006884 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6885 &dex_file,
6886 add_pc_offset,
6887 base_element_offset));
6888 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006889 for (const auto& entry : boot_image_string_patches_) {
6890 const StringReference& target_string = entry.first;
6891 Literal* literal = entry.second;
6892 DCHECK(literal->GetLabel()->IsBound());
6893 uint32_t literal_offset = literal->GetLabel()->Position();
6894 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6895 target_string.dex_file,
6896 target_string.string_index));
6897 }
6898 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6899 const DexFile& dex_file = info.target_dex_file;
6900 uint32_t string_index = info.offset_or_index;
6901 DCHECK(info.add_pc_label.IsBound());
6902 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6903 // Add MOVW patch.
6904 DCHECK(info.movw_label.IsBound());
6905 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6906 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6907 &dex_file,
6908 add_pc_offset,
6909 string_index));
6910 // Add MOVT patch.
6911 DCHECK(info.movt_label.IsBound());
6912 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6913 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6914 &dex_file,
6915 add_pc_offset,
6916 string_index));
6917 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006918 for (const auto& entry : boot_image_type_patches_) {
6919 const TypeReference& target_type = entry.first;
6920 Literal* literal = entry.second;
6921 DCHECK(literal->GetLabel()->IsBound());
6922 uint32_t literal_offset = literal->GetLabel()->Position();
6923 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6924 target_type.dex_file,
6925 target_type.type_index));
6926 }
6927 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6928 const DexFile& dex_file = info.target_dex_file;
6929 uint32_t type_index = info.offset_or_index;
6930 DCHECK(info.add_pc_label.IsBound());
6931 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6932 // Add MOVW patch.
6933 DCHECK(info.movw_label.IsBound());
6934 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6935 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6936 &dex_file,
6937 add_pc_offset,
6938 type_index));
6939 // Add MOVT patch.
6940 DCHECK(info.movt_label.IsBound());
6941 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6942 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6943 &dex_file,
6944 add_pc_offset,
6945 type_index));
6946 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006947 for (const auto& entry : boot_image_address_patches_) {
6948 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6949 Literal* literal = entry.second;
6950 DCHECK(literal->GetLabel()->IsBound());
6951 uint32_t literal_offset = literal->GetLabel()->Position();
6952 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6953 }
6954}
6955
6956Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6957 return map->GetOrCreate(
6958 value,
6959 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006960}
6961
6962Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6963 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006964 return map->GetOrCreate(
6965 target_method,
6966 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006967}
6968
6969Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6970 return DeduplicateMethodLiteral(target_method, &method_patches_);
6971}
6972
6973Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6974 return DeduplicateMethodLiteral(target_method, &call_patches_);
6975}
6976
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006977void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6978 LocationSummary* locations =
6979 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6980 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6981 Location::RequiresRegister());
6982 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6983 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6985}
6986
6987void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6988 LocationSummary* locations = instr->GetLocations();
6989 Register res = locations->Out().AsRegister<Register>();
6990 Register accumulator =
6991 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6992 Register mul_left =
6993 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6994 Register mul_right =
6995 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6996
6997 if (instr->GetOpKind() == HInstruction::kAdd) {
6998 __ mla(res, mul_left, mul_right, accumulator);
6999 } else {
7000 __ mls(res, mul_left, mul_right, accumulator);
7001 }
7002}
7003
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007004void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007005 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007006 LOG(FATAL) << "Unreachable";
7007}
7008
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007009void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007010 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007011 LOG(FATAL) << "Unreachable";
7012}
7013
Mark Mendellfe57faa2015-09-18 09:26:15 -04007014// Simple implementation of packed switch - generate cascaded compare/jumps.
7015void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7016 LocationSummary* locations =
7017 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7018 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007019 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007020 codegen_->GetAssembler()->IsThumb()) {
7021 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7022 if (switch_instr->GetStartValue() != 0) {
7023 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7024 }
7025 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007026}
7027
7028void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7029 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007030 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007031 LocationSummary* locations = switch_instr->GetLocations();
7032 Register value_reg = locations->InAt(0).AsRegister<Register>();
7033 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7034
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007035 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007036 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007037 Register temp_reg = IP;
7038 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7039 // the immediate, because IP is used as the destination register. For the other
7040 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7041 // and they can be encoded in the instruction without making use of IP register.
7042 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7043
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007044 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007045 // Jump to successors[0] if value == lower_bound.
7046 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7047 int32_t last_index = 0;
7048 for (; num_entries - last_index > 2; last_index += 2) {
7049 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7050 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7051 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7052 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7053 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7054 }
7055 if (num_entries - last_index == 2) {
7056 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007057 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007058 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007059 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007060
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007061 // And the default for any other value.
7062 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7063 __ b(codegen_->GetLabelOf(default_block));
7064 }
7065 } else {
7066 // Create a table lookup.
7067 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7068
7069 // Materialize a pointer to the switch table
7070 std::vector<Label*> labels(num_entries);
7071 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7072 for (uint32_t i = 0; i < num_entries; i++) {
7073 labels[i] = codegen_->GetLabelOf(successors[i]);
7074 }
7075 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7076
7077 // Remove the bias.
7078 Register key_reg;
7079 if (lower_bound != 0) {
7080 key_reg = locations->GetTemp(1).AsRegister<Register>();
7081 __ AddConstant(key_reg, value_reg, -lower_bound);
7082 } else {
7083 key_reg = value_reg;
7084 }
7085
7086 // Check whether the value is in the table, jump to default block if not.
7087 __ CmpConstant(key_reg, num_entries - 1);
7088 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7089
7090 // Load the displacement from the table.
7091 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7092
7093 // Dispatch is a direct add to the PC (for Thumb2).
7094 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007095 }
7096}
7097
Vladimir Markob4536b72015-11-24 13:45:23 +00007098void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7099 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7100 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007101}
7102
7103void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7104 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007105 CodeGeneratorARM::PcRelativePatchInfo* labels =
7106 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007107 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007108 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007109 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007110 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007111 __ BindTrackedLabel(&labels->add_pc_label);
7112 __ add(base_reg, base_reg, ShifterOperand(PC));
7113}
7114
Andreas Gampe85b62f22015-09-09 13:15:38 -07007115void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7116 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007117 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007118 return;
7119 }
7120
7121 DCHECK_NE(type, Primitive::kPrimVoid);
7122
7123 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7124 if (return_loc.Equals(trg)) {
7125 return;
7126 }
7127
7128 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7129 // with the last branch.
7130 if (type == Primitive::kPrimLong) {
7131 HParallelMove parallel_move(GetGraph()->GetArena());
7132 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7133 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7134 GetMoveResolver()->EmitNativeCode(&parallel_move);
7135 } else if (type == Primitive::kPrimDouble) {
7136 HParallelMove parallel_move(GetGraph()->GetArena());
7137 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7138 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7139 GetMoveResolver()->EmitNativeCode(&parallel_move);
7140 } else {
7141 // Let the parallel move resolver take care of all of this.
7142 HParallelMove parallel_move(GetGraph()->GetArena());
7143 parallel_move.AddMove(return_loc, trg, type, nullptr);
7144 GetMoveResolver()->EmitNativeCode(&parallel_move);
7145 }
7146}
7147
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007148void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7149 LocationSummary* locations =
7150 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7151 locations->SetInAt(0, Location::RequiresRegister());
7152 locations->SetOut(Location::RequiresRegister());
7153}
7154
7155void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7156 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007157 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007158 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007159 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007160 __ LoadFromOffset(kLoadWord,
7161 locations->Out().AsRegister<Register>(),
7162 locations->InAt(0).AsRegister<Register>(),
7163 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007164 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007165 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007166 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007167 __ LoadFromOffset(kLoadWord,
7168 locations->Out().AsRegister<Register>(),
7169 locations->InAt(0).AsRegister<Register>(),
7170 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7171 __ LoadFromOffset(kLoadWord,
7172 locations->Out().AsRegister<Register>(),
7173 locations->Out().AsRegister<Register>(),
7174 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007175 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007176}
7177
Roland Levillain4d027112015-07-01 15:41:14 +01007178#undef __
7179#undef QUICK_ENTRY_POINT
7180
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007181} // namespace arm
7182} // namespace art