blob: 6d9c55cd7559672fede29aa47d89a229e81692cf [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();
4645 bool object_array_set_with_read_barrier =
4646 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004647
Nicolas Geoffray39468442014-09-02 15:17:15 +01004648 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004649 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004650 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4651 LocationSummary::kCallOnSlowPath :
4652 LocationSummary::kNoCall);
4653
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004654 locations->SetInAt(0, Location::RequiresRegister());
4655 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4656 if (Primitive::IsFloatingPointType(value_type)) {
4657 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004659 locations->SetInAt(2, Location::RequiresRegister());
4660 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004661 if (needs_write_barrier) {
4662 // Temporary registers for the write barrier.
4663 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004664 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004665 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004666}
4667
4668void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4669 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004670 Location array_loc = locations->InAt(0);
4671 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004672 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004673 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004674 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004675 bool needs_write_barrier =
4676 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004677 uint32_t data_offset =
4678 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4679 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004680 HInstruction* array_instr = instruction->GetArray();
4681 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004682 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4683 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004684
4685 switch (value_type) {
4686 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004687 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004688 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004689 case Primitive::kPrimChar:
4690 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004692 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4693 uint32_t full_offset =
4694 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4695 StoreOperandType store_type = GetStoreOperandType(value_type);
4696 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004697 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004698 Register temp = IP;
4699
4700 if (has_intermediate_address) {
4701 // We do not need to compute the intermediate address from the array: the
4702 // input instruction has done it already. See the comment in
4703 // `TryExtractArrayAccessAddress()`.
4704 if (kIsDebugBuild) {
4705 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4706 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4707 }
4708 temp = array;
4709 } else {
4710 __ add(temp, array, ShifterOperand(data_offset));
4711 }
Artem Serov6c916792016-07-11 14:02:34 +01004712 codegen_->StoreToShiftedRegOffset(value_type,
4713 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004714 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004715 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716 }
4717 break;
4718 }
4719
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004720 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004721 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004722 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4723 // See the comment in instruction_simplifier_shared.cc.
4724 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004725
4726 if (instruction->InputAt(2)->IsNullConstant()) {
4727 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004728 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004729 size_t offset =
4730 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004731 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004732 } else {
4733 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004734 __ add(IP, array, ShifterOperand(data_offset));
4735 codegen_->StoreToShiftedRegOffset(value_type,
4736 value_loc,
4737 IP,
4738 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004739 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004740 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004741 DCHECK(!needs_write_barrier);
4742 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004743 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004744 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004745
4746 DCHECK(needs_write_barrier);
4747 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4748 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4749 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4750 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4751 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4752 Label done;
4753 SlowPathCode* slow_path = nullptr;
4754
Roland Levillain3b359c72015-11-17 19:35:12 +00004755 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004756 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4757 codegen_->AddSlowPath(slow_path);
4758 if (instruction->GetValueCanBeNull()) {
4759 Label non_zero;
4760 __ CompareAndBranchIfNonZero(value, &non_zero);
4761 if (index.IsConstant()) {
4762 size_t offset =
4763 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4764 __ StoreToOffset(kStoreWord, value, array, offset);
4765 } else {
4766 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004767 __ add(IP, array, ShifterOperand(data_offset));
4768 codegen_->StoreToShiftedRegOffset(value_type,
4769 value_loc,
4770 IP,
4771 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004772 }
4773 codegen_->MaybeRecordImplicitNullCheck(instruction);
4774 __ b(&done);
4775 __ Bind(&non_zero);
4776 }
4777
Roland Levillain3b359c72015-11-17 19:35:12 +00004778 if (kEmitCompilerReadBarrier) {
4779 // When read barriers are enabled, the type checking
4780 // instrumentation requires two read barriers:
4781 //
4782 // __ Mov(temp2, temp1);
4783 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4784 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004785 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004786 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4787 //
4788 // // /* HeapReference<Class> */ temp2 = value->klass_
4789 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004790 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004791 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4792 //
4793 // __ cmp(temp1, ShifterOperand(temp2));
4794 //
4795 // However, the second read barrier may trash `temp`, as it
4796 // is a temporary register, and as such would not be saved
4797 // along with live registers before calling the runtime (nor
4798 // restored afterwards). So in this case, we bail out and
4799 // delegate the work to the array set slow path.
4800 //
4801 // TODO: Extend the register allocator to support a new
4802 // "(locally) live temp" location so as to avoid always
4803 // going into the slow path when read barriers are enabled.
4804 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004805 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004806 // /* HeapReference<Class> */ temp1 = array->klass_
4807 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4808 codegen_->MaybeRecordImplicitNullCheck(instruction);
4809 __ MaybeUnpoisonHeapReference(temp1);
4810
4811 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4812 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4813 // /* HeapReference<Class> */ temp2 = value->klass_
4814 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4815 // If heap poisoning is enabled, no need to unpoison `temp1`
4816 // nor `temp2`, as we are comparing two poisoned references.
4817 __ cmp(temp1, ShifterOperand(temp2));
4818
4819 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4820 Label do_put;
4821 __ b(&do_put, EQ);
4822 // If heap poisoning is enabled, the `temp1` reference has
4823 // not been unpoisoned yet; unpoison it now.
4824 __ MaybeUnpoisonHeapReference(temp1);
4825
4826 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4827 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4828 // If heap poisoning is enabled, no need to unpoison
4829 // `temp1`, as we are comparing against null below.
4830 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4831 __ Bind(&do_put);
4832 } else {
4833 __ b(slow_path->GetEntryLabel(), NE);
4834 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 }
4836 }
4837
Artem Serov6c916792016-07-11 14:02:34 +01004838 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004839 if (kPoisonHeapReferences) {
4840 // Note that in the case where `value` is a null reference,
4841 // we do not enter this block, as a null reference does not
4842 // need poisoning.
4843 DCHECK_EQ(value_type, Primitive::kPrimNot);
4844 __ Mov(temp1, value);
4845 __ PoisonHeapReference(temp1);
4846 source = temp1;
4847 }
4848
4849 if (index.IsConstant()) {
4850 size_t offset =
4851 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4852 __ StoreToOffset(kStoreWord, source, array, offset);
4853 } else {
4854 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004855
4856 __ add(IP, array, ShifterOperand(data_offset));
4857 codegen_->StoreToShiftedRegOffset(value_type,
4858 Location::RegisterLocation(source),
4859 IP,
4860 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004861 }
4862
Roland Levillain3b359c72015-11-17 19:35:12 +00004863 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004864 codegen_->MaybeRecordImplicitNullCheck(instruction);
4865 }
4866
4867 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4868
4869 if (done.IsLinked()) {
4870 __ Bind(&done);
4871 }
4872
4873 if (slow_path != nullptr) {
4874 __ Bind(slow_path->GetExitLabel());
4875 }
4876
4877 break;
4878 }
4879
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004881 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004882 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004883 size_t offset =
4884 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004885 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004887 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004888 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004889 }
4890 break;
4891 }
4892
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004893 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004894 Location value = locations->InAt(2);
4895 DCHECK(value.IsFpuRegister());
4896 if (index.IsConstant()) {
4897 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004898 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004899 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004900 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004901 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4902 }
4903 break;
4904 }
4905
4906 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004907 Location value = locations->InAt(2);
4908 DCHECK(value.IsFpuRegisterPair());
4909 if (index.IsConstant()) {
4910 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004912 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004913 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004914 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4915 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004916
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004917 break;
4918 }
4919
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004920 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004921 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004922 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004924
Roland Levillain80e67092016-01-08 16:04:55 +00004925 // Objects are handled in the switch.
4926 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004927 codegen_->MaybeRecordImplicitNullCheck(instruction);
4928 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004929}
4930
4931void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004932 LocationSummary* locations =
4933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004934 locations->SetInAt(0, Location::RequiresRegister());
4935 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004936}
4937
4938void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4939 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004940 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004941 Register obj = locations->InAt(0).AsRegister<Register>();
4942 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004943 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004944 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004945}
4946
Artem Serov328429f2016-07-06 16:23:04 +01004947void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00004948 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4949 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01004950 LocationSummary* locations =
4951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4952
4953 locations->SetInAt(0, Location::RequiresRegister());
4954 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4956}
4957
4958void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4959 LocationSummary* locations = instruction->GetLocations();
4960 Location out = locations->Out();
4961 Location first = locations->InAt(0);
4962 Location second = locations->InAt(1);
4963
Roland Levillain4a3aa572016-08-15 13:17:06 +00004964 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4965 DCHECK(!kEmitCompilerReadBarrier);
4966
Artem Serov328429f2016-07-06 16:23:04 +01004967 if (second.IsRegister()) {
4968 __ add(out.AsRegister<Register>(),
4969 first.AsRegister<Register>(),
4970 ShifterOperand(second.AsRegister<Register>()));
4971 } else {
4972 __ AddConstant(out.AsRegister<Register>(),
4973 first.AsRegister<Register>(),
4974 second.GetConstant()->AsIntConstant()->GetValue());
4975 }
4976}
4977
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004978void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004979 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4980 ? LocationSummary::kCallOnSlowPath
4981 : LocationSummary::kNoCall;
4982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004983 locations->SetInAt(0, Location::RequiresRegister());
4984 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004985 if (instruction->HasUses()) {
4986 locations->SetOut(Location::SameAsFirstInput());
4987 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004988}
4989
4990void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4991 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004992 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004993 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004994 codegen_->AddSlowPath(slow_path);
4995
Roland Levillain271ab9c2014-11-27 15:23:57 +00004996 Register index = locations->InAt(0).AsRegister<Register>();
4997 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004998
4999 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005000 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005001}
5002
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005003void CodeGeneratorARM::MarkGCCard(Register temp,
5004 Register card,
5005 Register object,
5006 Register value,
5007 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005008 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005009 if (can_be_null) {
5010 __ CompareAndBranchIfZero(value, &is_null);
5011 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005012 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005013 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5014 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005015 if (can_be_null) {
5016 __ Bind(&is_null);
5017 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005018}
5019
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005020void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005021 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005022}
5023
5024void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005025 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5026}
5027
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005028void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
5029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5030}
5031
5032void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005033 HBasicBlock* block = instruction->GetBlock();
5034 if (block->GetLoopInformation() != nullptr) {
5035 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5036 // The back edge will generate the suspend check.
5037 return;
5038 }
5039 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5040 // The goto will generate the suspend check.
5041 return;
5042 }
5043 GenerateSuspendCheck(instruction, nullptr);
5044}
5045
5046void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5047 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005048 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005049 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5050 if (slow_path == nullptr) {
5051 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5052 instruction->SetSlowPath(slow_path);
5053 codegen_->AddSlowPath(slow_path);
5054 if (successor != nullptr) {
5055 DCHECK(successor->IsLoopHeader());
5056 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5057 }
5058 } else {
5059 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5060 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005061
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005062 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005063 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005064 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005065 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005066 __ Bind(slow_path->GetReturnLabel());
5067 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005068 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005069 __ b(slow_path->GetEntryLabel());
5070 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005071}
5072
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005073ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5074 return codegen_->GetAssembler();
5075}
5076
5077void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005078 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005079 Location source = move->GetSource();
5080 Location destination = move->GetDestination();
5081
5082 if (source.IsRegister()) {
5083 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005084 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005085 } else if (destination.IsFpuRegister()) {
5086 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005087 } else {
5088 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005089 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005090 SP, destination.GetStackIndex());
5091 }
5092 } else if (source.IsStackSlot()) {
5093 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005094 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005095 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005096 } else if (destination.IsFpuRegister()) {
5097 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005098 } else {
5099 DCHECK(destination.IsStackSlot());
5100 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5101 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5102 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005103 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005104 if (destination.IsRegister()) {
5105 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5106 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005107 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005108 } else {
5109 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005110 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5111 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005112 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005113 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005114 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5115 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005116 } else if (destination.IsRegisterPair()) {
5117 DCHECK(ExpectedPairLayout(destination));
5118 __ LoadFromOffset(
5119 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5120 } else {
5121 DCHECK(destination.IsFpuRegisterPair()) << destination;
5122 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5123 SP,
5124 source.GetStackIndex());
5125 }
5126 } else if (source.IsRegisterPair()) {
5127 if (destination.IsRegisterPair()) {
5128 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5129 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005130 } else if (destination.IsFpuRegisterPair()) {
5131 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5132 source.AsRegisterPairLow<Register>(),
5133 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005134 } else {
5135 DCHECK(destination.IsDoubleStackSlot()) << destination;
5136 DCHECK(ExpectedPairLayout(source));
5137 __ StoreToOffset(
5138 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5139 }
5140 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005141 if (destination.IsRegisterPair()) {
5142 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5143 destination.AsRegisterPairHigh<Register>(),
5144 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5145 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005146 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5147 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5148 } else {
5149 DCHECK(destination.IsDoubleStackSlot()) << destination;
5150 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5151 SP,
5152 destination.GetStackIndex());
5153 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005154 } else {
5155 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005156 HConstant* constant = source.GetConstant();
5157 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5158 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005159 if (destination.IsRegister()) {
5160 __ LoadImmediate(destination.AsRegister<Register>(), value);
5161 } else {
5162 DCHECK(destination.IsStackSlot());
5163 __ LoadImmediate(IP, value);
5164 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5165 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005166 } else if (constant->IsLongConstant()) {
5167 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005168 if (destination.IsRegisterPair()) {
5169 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5170 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005171 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005172 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005173 __ LoadImmediate(IP, Low32Bits(value));
5174 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5175 __ LoadImmediate(IP, High32Bits(value));
5176 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5177 }
5178 } else if (constant->IsDoubleConstant()) {
5179 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005180 if (destination.IsFpuRegisterPair()) {
5181 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005182 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005183 DCHECK(destination.IsDoubleStackSlot()) << destination;
5184 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005185 __ LoadImmediate(IP, Low32Bits(int_value));
5186 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5187 __ LoadImmediate(IP, High32Bits(int_value));
5188 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5189 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005190 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005191 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005192 float value = constant->AsFloatConstant()->GetValue();
5193 if (destination.IsFpuRegister()) {
5194 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5195 } else {
5196 DCHECK(destination.IsStackSlot());
5197 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5198 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5199 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005200 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005201 }
5202}
5203
5204void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5205 __ Mov(IP, reg);
5206 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5207 __ StoreToOffset(kStoreWord, IP, SP, mem);
5208}
5209
5210void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5211 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5212 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5213 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5214 SP, mem1 + stack_offset);
5215 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5216 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5217 SP, mem2 + stack_offset);
5218 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5219}
5220
5221void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005222 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005223 Location source = move->GetSource();
5224 Location destination = move->GetDestination();
5225
5226 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005227 DCHECK_NE(source.AsRegister<Register>(), IP);
5228 DCHECK_NE(destination.AsRegister<Register>(), IP);
5229 __ Mov(IP, source.AsRegister<Register>());
5230 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5231 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005232 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005233 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005234 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005235 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005236 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5237 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005238 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005239 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005240 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005241 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005242 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005243 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005244 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005245 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005246 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5247 destination.AsRegisterPairHigh<Register>(),
5248 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005249 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005250 Register low_reg = source.IsRegisterPair()
5251 ? source.AsRegisterPairLow<Register>()
5252 : destination.AsRegisterPairLow<Register>();
5253 int mem = source.IsRegisterPair()
5254 ? destination.GetStackIndex()
5255 : source.GetStackIndex();
5256 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005257 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005258 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005259 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005260 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005261 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5262 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005263 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005264 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005265 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005266 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5267 DRegister reg = source.IsFpuRegisterPair()
5268 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5269 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5270 int mem = source.IsFpuRegisterPair()
5271 ? destination.GetStackIndex()
5272 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005273 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005274 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005275 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005276 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5277 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5278 : destination.AsFpuRegister<SRegister>();
5279 int mem = source.IsFpuRegister()
5280 ? destination.GetStackIndex()
5281 : source.GetStackIndex();
5282
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005283 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005284 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005285 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005286 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005287 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5288 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005289 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005290 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005291 }
5292}
5293
5294void ParallelMoveResolverARM::SpillScratch(int reg) {
5295 __ Push(static_cast<Register>(reg));
5296}
5297
5298void ParallelMoveResolverARM::RestoreScratch(int reg) {
5299 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005300}
5301
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005302HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5303 HLoadClass::LoadKind desired_class_load_kind) {
5304 if (kEmitCompilerReadBarrier) {
5305 switch (desired_class_load_kind) {
5306 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5307 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5308 case HLoadClass::LoadKind::kBootImageAddress:
5309 // TODO: Implement for read barrier.
5310 return HLoadClass::LoadKind::kDexCacheViaMethod;
5311 default:
5312 break;
5313 }
5314 }
5315 switch (desired_class_load_kind) {
5316 case HLoadClass::LoadKind::kReferrersClass:
5317 break;
5318 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5319 DCHECK(!GetCompilerOptions().GetCompilePic());
5320 break;
5321 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5322 DCHECK(GetCompilerOptions().GetCompilePic());
5323 break;
5324 case HLoadClass::LoadKind::kBootImageAddress:
5325 break;
5326 case HLoadClass::LoadKind::kDexCacheAddress:
5327 DCHECK(Runtime::Current()->UseJitCompilation());
5328 break;
5329 case HLoadClass::LoadKind::kDexCachePcRelative:
5330 DCHECK(!Runtime::Current()->UseJitCompilation());
5331 // We disable pc-relative load when there is an irreducible loop, as the optimization
5332 // is incompatible with it.
5333 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5334 // with irreducible loops.
5335 if (GetGraph()->HasIrreducibleLoops()) {
5336 return HLoadClass::LoadKind::kDexCacheViaMethod;
5337 }
5338 break;
5339 case HLoadClass::LoadKind::kDexCacheViaMethod:
5340 break;
5341 }
5342 return desired_class_load_kind;
5343}
5344
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005345void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005346 if (cls->NeedsAccessCheck()) {
5347 InvokeRuntimeCallingConvention calling_convention;
5348 CodeGenerator::CreateLoadClassLocationSummary(
5349 cls,
5350 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5351 Location::RegisterLocation(R0),
5352 /* code_generator_supports_read_barrier */ true);
5353 return;
5354 }
5355
5356 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5357 ? LocationSummary::kCallOnSlowPath
5358 : LocationSummary::kNoCall;
5359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5360 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5361 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5362 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5363 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5364 locations->SetInAt(0, Location::RequiresRegister());
5365 }
5366 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005367}
5368
5369void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005370 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005371 if (cls->NeedsAccessCheck()) {
5372 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5373 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5374 cls,
5375 cls->GetDexPc(),
5376 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005377 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005378 return;
5379 }
5380
Roland Levillain3b359c72015-11-17 19:35:12 +00005381 Location out_loc = locations->Out();
5382 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005383
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005384 bool generate_null_check = false;
5385 switch (cls->GetLoadKind()) {
5386 case HLoadClass::LoadKind::kReferrersClass: {
5387 DCHECK(!cls->CanCallRuntime());
5388 DCHECK(!cls->MustGenerateClinitCheck());
5389 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5390 Register current_method = locations->InAt(0).AsRegister<Register>();
5391 GenerateGcRootFieldLoad(
5392 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5393 break;
5394 }
5395 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5396 DCHECK(!kEmitCompilerReadBarrier);
5397 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5398 cls->GetTypeIndex()));
5399 break;
5400 }
5401 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5402 DCHECK(!kEmitCompilerReadBarrier);
5403 CodeGeneratorARM::PcRelativePatchInfo* labels =
5404 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5405 __ BindTrackedLabel(&labels->movw_label);
5406 __ movw(out, /* placeholder */ 0u);
5407 __ BindTrackedLabel(&labels->movt_label);
5408 __ movt(out, /* placeholder */ 0u);
5409 __ BindTrackedLabel(&labels->add_pc_label);
5410 __ add(out, out, ShifterOperand(PC));
5411 break;
5412 }
5413 case HLoadClass::LoadKind::kBootImageAddress: {
5414 DCHECK(!kEmitCompilerReadBarrier);
5415 DCHECK_NE(cls->GetAddress(), 0u);
5416 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5417 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5418 break;
5419 }
5420 case HLoadClass::LoadKind::kDexCacheAddress: {
5421 DCHECK_NE(cls->GetAddress(), 0u);
5422 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5423 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5424 // a 128B range. To try and reduce the number of literals if we load multiple types,
5425 // simply split the dex cache address to a 128B aligned base loaded from a literal
5426 // and the remaining offset embedded in the load.
5427 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5428 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5429 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5430 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5431 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5432 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5433 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5434 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5435 generate_null_check = !cls->IsInDexCache();
5436 break;
5437 }
5438 case HLoadClass::LoadKind::kDexCachePcRelative: {
5439 Register base_reg = locations->InAt(0).AsRegister<Register>();
5440 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5441 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5442 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5443 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5444 generate_null_check = !cls->IsInDexCache();
5445 break;
5446 }
5447 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5448 // /* GcRoot<mirror::Class>[] */ out =
5449 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5450 Register current_method = locations->InAt(0).AsRegister<Register>();
5451 __ LoadFromOffset(kLoadWord,
5452 out,
5453 current_method,
5454 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5455 // /* GcRoot<mirror::Class> */ out = out[type_index]
5456 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5457 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5458 generate_null_check = !cls->IsInDexCache();
5459 }
5460 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005461
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005462 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5463 DCHECK(cls->CanCallRuntime());
5464 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5465 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5466 codegen_->AddSlowPath(slow_path);
5467 if (generate_null_check) {
5468 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5469 }
5470 if (cls->MustGenerateClinitCheck()) {
5471 GenerateClassInitializationCheck(slow_path, out);
5472 } else {
5473 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005474 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005475 }
5476}
5477
5478void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5479 LocationSummary* locations =
5480 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5481 locations->SetInAt(0, Location::RequiresRegister());
5482 if (check->HasUses()) {
5483 locations->SetOut(Location::SameAsFirstInput());
5484 }
5485}
5486
5487void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005488 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005489 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005490 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005491 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005492 GenerateClassInitializationCheck(slow_path,
5493 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005494}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005495
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005496void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005497 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005498 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5499 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5500 __ b(slow_path->GetEntryLabel(), LT);
5501 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5502 // properly. Therefore, we do a memory fence.
5503 __ dmb(ISH);
5504 __ Bind(slow_path->GetExitLabel());
5505}
5506
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005507HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5508 HLoadString::LoadKind desired_string_load_kind) {
5509 if (kEmitCompilerReadBarrier) {
5510 switch (desired_string_load_kind) {
5511 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5512 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5513 case HLoadString::LoadKind::kBootImageAddress:
5514 // TODO: Implement for read barrier.
5515 return HLoadString::LoadKind::kDexCacheViaMethod;
5516 default:
5517 break;
5518 }
5519 }
5520 switch (desired_string_load_kind) {
5521 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5522 DCHECK(!GetCompilerOptions().GetCompilePic());
5523 break;
5524 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5525 DCHECK(GetCompilerOptions().GetCompilePic());
5526 break;
5527 case HLoadString::LoadKind::kBootImageAddress:
5528 break;
5529 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005530 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005531 break;
5532 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005533 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005534 // We disable pc-relative load when there is an irreducible loop, as the optimization
5535 // is incompatible with it.
5536 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5537 // with irreducible loops.
5538 if (GetGraph()->HasIrreducibleLoops()) {
5539 return HLoadString::LoadKind::kDexCacheViaMethod;
5540 }
5541 break;
5542 case HLoadString::LoadKind::kDexCacheViaMethod:
5543 break;
5544 }
5545 return desired_string_load_kind;
5546}
5547
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005548void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005549 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005550 ? LocationSummary::kCallOnSlowPath
5551 : LocationSummary::kNoCall;
5552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005553 HLoadString::LoadKind load_kind = load->GetLoadKind();
5554 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5555 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5556 locations->SetInAt(0, Location::RequiresRegister());
5557 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005558 locations->SetOut(Location::RequiresRegister());
5559}
5560
5561void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005562 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005563 Location out_loc = locations->Out();
5564 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005565
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005566 switch (load->GetLoadKind()) {
5567 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5568 DCHECK(!kEmitCompilerReadBarrier);
5569 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5570 load->GetStringIndex()));
5571 return; // No dex cache slow path.
5572 }
5573 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5574 DCHECK(!kEmitCompilerReadBarrier);
5575 CodeGeneratorARM::PcRelativePatchInfo* labels =
5576 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5577 __ BindTrackedLabel(&labels->movw_label);
5578 __ movw(out, /* placeholder */ 0u);
5579 __ BindTrackedLabel(&labels->movt_label);
5580 __ movt(out, /* placeholder */ 0u);
5581 __ BindTrackedLabel(&labels->add_pc_label);
5582 __ add(out, out, ShifterOperand(PC));
5583 return; // No dex cache slow path.
5584 }
5585 case HLoadString::LoadKind::kBootImageAddress: {
5586 DCHECK(!kEmitCompilerReadBarrier);
5587 DCHECK_NE(load->GetAddress(), 0u);
5588 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5589 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5590 return; // No dex cache slow path.
5591 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005592 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005593 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005594 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005595
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005596 // TODO: Re-add the compiler code to do string dex cache lookup again.
5597 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5598 codegen_->AddSlowPath(slow_path);
5599 __ b(slow_path->GetEntryLabel());
5600 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005601}
5602
David Brazdilcb1c0552015-08-04 16:22:25 +01005603static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005604 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005605}
5606
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005607void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5608 LocationSummary* locations =
5609 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5610 locations->SetOut(Location::RequiresRegister());
5611}
5612
5613void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005614 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005615 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5616}
5617
5618void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5619 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5620}
5621
5622void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005623 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005624 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005625}
5626
5627void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5628 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005629 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005630 InvokeRuntimeCallingConvention calling_convention;
5631 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5632}
5633
5634void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5635 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005636 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005637 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005638}
5639
Roland Levillainc9285912015-12-18 10:38:42 +00005640static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5641 return kEmitCompilerReadBarrier &&
5642 (kUseBakerReadBarrier ||
5643 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5644 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5645 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5646}
5647
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005648void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005649 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005650 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5651 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005652 case TypeCheckKind::kExactCheck:
5653 case TypeCheckKind::kAbstractClassCheck:
5654 case TypeCheckKind::kClassHierarchyCheck:
5655 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005656 call_kind =
5657 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005658 break;
5659 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005660 case TypeCheckKind::kUnresolvedCheck:
5661 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005662 call_kind = LocationSummary::kCallOnSlowPath;
5663 break;
5664 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005665
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005667 locations->SetInAt(0, Location::RequiresRegister());
5668 locations->SetInAt(1, Location::RequiresRegister());
5669 // The "out" register is used as a temporary, so it overlaps with the inputs.
5670 // Note that TypeCheckSlowPathARM uses this register too.
5671 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5672 // When read barriers are enabled, we need a temporary register for
5673 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005674 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005675 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005677}
5678
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005679void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005680 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005681 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005682 Location obj_loc = locations->InAt(0);
5683 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005684 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005685 Location out_loc = locations->Out();
5686 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005687 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005688 locations->GetTemp(0) :
5689 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005690 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005691 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5692 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5693 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005694 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005695 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005696
5697 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005698 // avoid null check if we know obj is not null.
5699 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005700 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005701 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005702
Roland Levillain3b359c72015-11-17 19:35:12 +00005703 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005704 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005705
Roland Levillainc9285912015-12-18 10:38:42 +00005706 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005707 case TypeCheckKind::kExactCheck: {
5708 __ cmp(out, ShifterOperand(cls));
5709 // Classes must be equal for the instanceof to succeed.
5710 __ b(&zero, NE);
5711 __ LoadImmediate(out, 1);
5712 __ b(&done);
5713 break;
5714 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005715
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005716 case TypeCheckKind::kAbstractClassCheck: {
5717 // If the class is abstract, we eagerly fetch the super class of the
5718 // object to avoid doing a comparison we know will fail.
5719 Label loop;
5720 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005721 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005722 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005723 // If `out` is null, we use it for the result, and jump to `done`.
5724 __ CompareAndBranchIfZero(out, &done);
5725 __ cmp(out, ShifterOperand(cls));
5726 __ b(&loop, NE);
5727 __ LoadImmediate(out, 1);
5728 if (zero.IsLinked()) {
5729 __ b(&done);
5730 }
5731 break;
5732 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005733
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005734 case TypeCheckKind::kClassHierarchyCheck: {
5735 // Walk over the class hierarchy to find a match.
5736 Label loop, success;
5737 __ Bind(&loop);
5738 __ cmp(out, ShifterOperand(cls));
5739 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005740 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005741 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 __ CompareAndBranchIfNonZero(out, &loop);
5743 // If `out` is null, we use it for the result, and jump to `done`.
5744 __ b(&done);
5745 __ Bind(&success);
5746 __ LoadImmediate(out, 1);
5747 if (zero.IsLinked()) {
5748 __ b(&done);
5749 }
5750 break;
5751 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005752
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005753 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005754 // Do an exact check.
5755 Label exact_check;
5756 __ cmp(out, ShifterOperand(cls));
5757 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005758 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005759 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005760 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 // If `out` is null, we use it for the result, and jump to `done`.
5762 __ CompareAndBranchIfZero(out, &done);
5763 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5764 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5765 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005766 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005767 __ LoadImmediate(out, 1);
5768 __ b(&done);
5769 break;
5770 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005771
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005772 case TypeCheckKind::kArrayCheck: {
5773 __ cmp(out, ShifterOperand(cls));
5774 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005775 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5776 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005777 codegen_->AddSlowPath(slow_path);
5778 __ b(slow_path->GetEntryLabel(), NE);
5779 __ LoadImmediate(out, 1);
5780 if (zero.IsLinked()) {
5781 __ b(&done);
5782 }
5783 break;
5784 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005785
Calin Juravle98893e12015-10-02 21:05:03 +01005786 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005787 case TypeCheckKind::kInterfaceCheck: {
5788 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005789 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005790 // cases.
5791 //
5792 // We cannot directly call the InstanceofNonTrivial runtime
5793 // entry point without resorting to a type checking slow path
5794 // here (i.e. by calling InvokeRuntime directly), as it would
5795 // require to assign fixed registers for the inputs of this
5796 // HInstanceOf instruction (following the runtime calling
5797 // convention), which might be cluttered by the potential first
5798 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005799 //
5800 // TODO: Introduce a new runtime entry point taking the object
5801 // to test (instead of its class) as argument, and let it deal
5802 // with the read barrier issues. This will let us refactor this
5803 // case of the `switch` code as it was previously (with a direct
5804 // call to the runtime not using a type checking slow path).
5805 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005806 DCHECK(locations->OnlyCallsOnSlowPath());
5807 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5808 /* is_fatal */ false);
5809 codegen_->AddSlowPath(slow_path);
5810 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005811 if (zero.IsLinked()) {
5812 __ b(&done);
5813 }
5814 break;
5815 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005816 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005817
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005818 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005819 __ Bind(&zero);
5820 __ LoadImmediate(out, 0);
5821 }
5822
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005823 if (done.IsLinked()) {
5824 __ Bind(&done);
5825 }
5826
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005827 if (slow_path != nullptr) {
5828 __ Bind(slow_path->GetExitLabel());
5829 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005830}
5831
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005832void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005833 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5834 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5835
Roland Levillain3b359c72015-11-17 19:35:12 +00005836 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5837 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 case TypeCheckKind::kExactCheck:
5839 case TypeCheckKind::kAbstractClassCheck:
5840 case TypeCheckKind::kClassHierarchyCheck:
5841 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005842 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5843 LocationSummary::kCallOnSlowPath :
5844 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005845 break;
5846 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005847 case TypeCheckKind::kUnresolvedCheck:
5848 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005849 call_kind = LocationSummary::kCallOnSlowPath;
5850 break;
5851 }
5852
Roland Levillain3b359c72015-11-17 19:35:12 +00005853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5854 locations->SetInAt(0, Location::RequiresRegister());
5855 locations->SetInAt(1, Location::RequiresRegister());
5856 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5857 locations->AddTemp(Location::RequiresRegister());
5858 // When read barriers are enabled, we need an additional temporary
5859 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005860 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005861 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005863}
5864
5865void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005866 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005867 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005868 Location obj_loc = locations->InAt(0);
5869 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005870 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005871 Location temp_loc = locations->GetTemp(0);
5872 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005873 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005874 locations->GetTemp(1) :
5875 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005876 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005877 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5878 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5879 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005880
Roland Levillain3b359c72015-11-17 19:35:12 +00005881 bool is_type_check_slow_path_fatal =
5882 (type_check_kind == TypeCheckKind::kExactCheck ||
5883 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5884 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5885 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5886 !instruction->CanThrowIntoCatchBlock();
5887 SlowPathCode* type_check_slow_path =
5888 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5889 is_type_check_slow_path_fatal);
5890 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891
5892 Label done;
5893 // Avoid null check if we know obj is not null.
5894 if (instruction->MustDoNullCheck()) {
5895 __ CompareAndBranchIfZero(obj, &done);
5896 }
5897
Roland Levillain3b359c72015-11-17 19:35:12 +00005898 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005899 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005900
Roland Levillain3b359c72015-11-17 19:35:12 +00005901 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005902 case TypeCheckKind::kExactCheck:
5903 case TypeCheckKind::kArrayCheck: {
5904 __ cmp(temp, ShifterOperand(cls));
5905 // Jump to slow path for throwing the exception or doing a
5906 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005907 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908 break;
5909 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005910
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005911 case TypeCheckKind::kAbstractClassCheck: {
5912 // If the class is abstract, we eagerly fetch the super class of the
5913 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005914 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005915 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005916 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005917 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005918
5919 // If the class reference currently in `temp` is not null, jump
5920 // to the `compare_classes` label to compare it with the checked
5921 // class.
5922 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5923 // Otherwise, jump to the slow path to throw the exception.
5924 //
5925 // But before, move back the object's class into `temp` before
5926 // going into the slow path, as it has been overwritten in the
5927 // meantime.
5928 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005929 GenerateReferenceLoadTwoRegisters(
5930 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005931 __ b(type_check_slow_path->GetEntryLabel());
5932
5933 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934 __ cmp(temp, ShifterOperand(cls));
5935 __ b(&loop, NE);
5936 break;
5937 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005938
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005939 case TypeCheckKind::kClassHierarchyCheck: {
5940 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005941 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005942 __ Bind(&loop);
5943 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005944 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005945
Roland Levillain3b359c72015-11-17 19:35:12 +00005946 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005947 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005948
5949 // If the class reference currently in `temp` is not null, jump
5950 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005951 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005952 // Otherwise, jump to the slow path to throw the exception.
5953 //
5954 // But before, move back the object's class into `temp` before
5955 // going into the slow path, as it has been overwritten in the
5956 // meantime.
5957 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005958 GenerateReferenceLoadTwoRegisters(
5959 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005960 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005961 break;
5962 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005963
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005964 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005965 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005966 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005967 __ cmp(temp, ShifterOperand(cls));
5968 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005969
5970 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005971 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005972 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005973
5974 // If the component type is not null (i.e. the object is indeed
5975 // an array), jump to label `check_non_primitive_component_type`
5976 // to further check that this component type is not a primitive
5977 // type.
5978 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5979 // Otherwise, jump to the slow path to throw the exception.
5980 //
5981 // But before, move back the object's class into `temp` before
5982 // going into the slow path, as it has been overwritten in the
5983 // meantime.
5984 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005985 GenerateReferenceLoadTwoRegisters(
5986 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005987 __ b(type_check_slow_path->GetEntryLabel());
5988
5989 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005990 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005991 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5992 __ CompareAndBranchIfZero(temp, &done);
5993 // Same comment as above regarding `temp` and the slow path.
5994 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005995 GenerateReferenceLoadTwoRegisters(
5996 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005997 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005998 break;
5999 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006000
Calin Juravle98893e12015-10-02 21:05:03 +01006001 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006002 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006003 // We always go into the type check slow path for the unresolved
6004 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006005 //
6006 // We cannot directly call the CheckCast runtime entry point
6007 // without resorting to a type checking slow path here (i.e. by
6008 // calling InvokeRuntime directly), as it would require to
6009 // assign fixed registers for the inputs of this HInstanceOf
6010 // instruction (following the runtime calling convention), which
6011 // might be cluttered by the potential first read barrier
6012 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006013 //
6014 // TODO: Introduce a new runtime entry point taking the object
6015 // to test (instead of its class) as argument, and let it deal
6016 // with the read barrier issues. This will let us refactor this
6017 // case of the `switch` code as it was previously (with a direct
6018 // call to the runtime not using a type checking slow path).
6019 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006020 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006021 break;
6022 }
6023 __ Bind(&done);
6024
Roland Levillain3b359c72015-11-17 19:35:12 +00006025 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006026}
6027
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006028void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6029 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006030 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006031 InvokeRuntimeCallingConvention calling_convention;
6032 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6033}
6034
6035void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6036 codegen_->InvokeRuntime(instruction->IsEnter()
6037 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
6038 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00006039 instruction->GetDexPc(),
6040 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006041 if (instruction->IsEnter()) {
6042 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6043 } else {
6044 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6045 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006046}
6047
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006048void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6049void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6050void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006051
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006052void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006053 LocationSummary* locations =
6054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6055 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6056 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006057 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006058 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006059 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006060 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006061}
6062
6063void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6064 HandleBitwiseOperation(instruction);
6065}
6066
6067void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6068 HandleBitwiseOperation(instruction);
6069}
6070
6071void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6072 HandleBitwiseOperation(instruction);
6073}
6074
Artem Serov7fc63502016-02-09 17:15:29 +00006075
6076void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6077 LocationSummary* locations =
6078 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6079 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6080 || instruction->GetResultType() == Primitive::kPrimLong);
6081
6082 locations->SetInAt(0, Location::RequiresRegister());
6083 locations->SetInAt(1, Location::RequiresRegister());
6084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6085}
6086
6087void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6088 LocationSummary* locations = instruction->GetLocations();
6089 Location first = locations->InAt(0);
6090 Location second = locations->InAt(1);
6091 Location out = locations->Out();
6092
6093 if (instruction->GetResultType() == Primitive::kPrimInt) {
6094 Register first_reg = first.AsRegister<Register>();
6095 ShifterOperand second_reg(second.AsRegister<Register>());
6096 Register out_reg = out.AsRegister<Register>();
6097
6098 switch (instruction->GetOpKind()) {
6099 case HInstruction::kAnd:
6100 __ bic(out_reg, first_reg, second_reg);
6101 break;
6102 case HInstruction::kOr:
6103 __ orn(out_reg, first_reg, second_reg);
6104 break;
6105 // There is no EON on arm.
6106 case HInstruction::kXor:
6107 default:
6108 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6109 UNREACHABLE();
6110 }
6111 return;
6112
6113 } else {
6114 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6115 Register first_low = first.AsRegisterPairLow<Register>();
6116 Register first_high = first.AsRegisterPairHigh<Register>();
6117 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6118 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6119 Register out_low = out.AsRegisterPairLow<Register>();
6120 Register out_high = out.AsRegisterPairHigh<Register>();
6121
6122 switch (instruction->GetOpKind()) {
6123 case HInstruction::kAnd:
6124 __ bic(out_low, first_low, second_low);
6125 __ bic(out_high, first_high, second_high);
6126 break;
6127 case HInstruction::kOr:
6128 __ orn(out_low, first_low, second_low);
6129 __ orn(out_high, first_high, second_high);
6130 break;
6131 // There is no EON on arm.
6132 case HInstruction::kXor:
6133 default:
6134 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6135 UNREACHABLE();
6136 }
6137 }
6138}
6139
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006140void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6141 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6142 if (value == 0xffffffffu) {
6143 if (out != first) {
6144 __ mov(out, ShifterOperand(first));
6145 }
6146 return;
6147 }
6148 if (value == 0u) {
6149 __ mov(out, ShifterOperand(0));
6150 return;
6151 }
6152 ShifterOperand so;
6153 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6154 __ and_(out, first, so);
6155 } else {
6156 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6157 __ bic(out, first, ShifterOperand(~value));
6158 }
6159}
6160
6161void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6162 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6163 if (value == 0u) {
6164 if (out != first) {
6165 __ mov(out, ShifterOperand(first));
6166 }
6167 return;
6168 }
6169 if (value == 0xffffffffu) {
6170 __ mvn(out, ShifterOperand(0));
6171 return;
6172 }
6173 ShifterOperand so;
6174 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6175 __ orr(out, first, so);
6176 } else {
6177 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6178 __ orn(out, first, ShifterOperand(~value));
6179 }
6180}
6181
6182void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6183 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6184 if (value == 0u) {
6185 if (out != first) {
6186 __ mov(out, ShifterOperand(first));
6187 }
6188 return;
6189 }
6190 __ eor(out, first, ShifterOperand(value));
6191}
6192
Vladimir Marko59751a72016-08-05 14:37:27 +01006193void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6194 Location first,
6195 uint64_t value) {
6196 Register out_low = out.AsRegisterPairLow<Register>();
6197 Register out_high = out.AsRegisterPairHigh<Register>();
6198 Register first_low = first.AsRegisterPairLow<Register>();
6199 Register first_high = first.AsRegisterPairHigh<Register>();
6200 uint32_t value_low = Low32Bits(value);
6201 uint32_t value_high = High32Bits(value);
6202 if (value_low == 0u) {
6203 if (out_low != first_low) {
6204 __ mov(out_low, ShifterOperand(first_low));
6205 }
6206 __ AddConstant(out_high, first_high, value_high);
6207 return;
6208 }
6209 __ AddConstantSetFlags(out_low, first_low, value_low);
6210 ShifterOperand so;
6211 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6212 __ adc(out_high, first_high, so);
6213 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6214 __ sbc(out_high, first_high, so);
6215 } else {
6216 LOG(FATAL) << "Unexpected constant " << value_high;
6217 UNREACHABLE();
6218 }
6219}
6220
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006221void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6222 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006223 Location first = locations->InAt(0);
6224 Location second = locations->InAt(1);
6225 Location out = locations->Out();
6226
6227 if (second.IsConstant()) {
6228 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6229 uint32_t value_low = Low32Bits(value);
6230 if (instruction->GetResultType() == Primitive::kPrimInt) {
6231 Register first_reg = first.AsRegister<Register>();
6232 Register out_reg = out.AsRegister<Register>();
6233 if (instruction->IsAnd()) {
6234 GenerateAndConst(out_reg, first_reg, value_low);
6235 } else if (instruction->IsOr()) {
6236 GenerateOrrConst(out_reg, first_reg, value_low);
6237 } else {
6238 DCHECK(instruction->IsXor());
6239 GenerateEorConst(out_reg, first_reg, value_low);
6240 }
6241 } else {
6242 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6243 uint32_t value_high = High32Bits(value);
6244 Register first_low = first.AsRegisterPairLow<Register>();
6245 Register first_high = first.AsRegisterPairHigh<Register>();
6246 Register out_low = out.AsRegisterPairLow<Register>();
6247 Register out_high = out.AsRegisterPairHigh<Register>();
6248 if (instruction->IsAnd()) {
6249 GenerateAndConst(out_low, first_low, value_low);
6250 GenerateAndConst(out_high, first_high, value_high);
6251 } else if (instruction->IsOr()) {
6252 GenerateOrrConst(out_low, first_low, value_low);
6253 GenerateOrrConst(out_high, first_high, value_high);
6254 } else {
6255 DCHECK(instruction->IsXor());
6256 GenerateEorConst(out_low, first_low, value_low);
6257 GenerateEorConst(out_high, first_high, value_high);
6258 }
6259 }
6260 return;
6261 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006262
6263 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006264 Register first_reg = first.AsRegister<Register>();
6265 ShifterOperand second_reg(second.AsRegister<Register>());
6266 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006267 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006268 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006269 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006270 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006271 } else {
6272 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006273 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006274 }
6275 } else {
6276 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006277 Register first_low = first.AsRegisterPairLow<Register>();
6278 Register first_high = first.AsRegisterPairHigh<Register>();
6279 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6280 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6281 Register out_low = out.AsRegisterPairLow<Register>();
6282 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006283 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006284 __ and_(out_low, first_low, second_low);
6285 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006286 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006287 __ orr(out_low, first_low, second_low);
6288 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006289 } else {
6290 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006291 __ eor(out_low, first_low, second_low);
6292 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006293 }
6294 }
6295}
6296
Roland Levillainc9285912015-12-18 10:38:42 +00006297void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6298 Location out,
6299 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006300 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006301 Register out_reg = out.AsRegister<Register>();
6302 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006303 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006304 if (kUseBakerReadBarrier) {
6305 // Load with fast path based Baker's read barrier.
6306 // /* HeapReference<Object> */ out = *(out + offset)
6307 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006308 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006309 } else {
6310 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006311 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006312 // in the following move operation, as we will need it for the
6313 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006314 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006315 // /* HeapReference<Object> */ out = *(out + offset)
6316 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006317 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006318 }
6319 } else {
6320 // Plain load with no read barrier.
6321 // /* HeapReference<Object> */ out = *(out + offset)
6322 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6323 __ MaybeUnpoisonHeapReference(out_reg);
6324 }
6325}
6326
6327void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6328 Location out,
6329 Location obj,
6330 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006331 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006332 Register out_reg = out.AsRegister<Register>();
6333 Register obj_reg = obj.AsRegister<Register>();
6334 if (kEmitCompilerReadBarrier) {
6335 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006336 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006337 // Load with fast path based Baker's read barrier.
6338 // /* HeapReference<Object> */ out = *(obj + offset)
6339 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006340 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006341 } else {
6342 // Load with slow path based read barrier.
6343 // /* HeapReference<Object> */ out = *(obj + offset)
6344 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6345 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6346 }
6347 } else {
6348 // Plain load with no read barrier.
6349 // /* HeapReference<Object> */ out = *(obj + offset)
6350 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6351 __ MaybeUnpoisonHeapReference(out_reg);
6352 }
6353}
6354
6355void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6356 Location root,
6357 Register obj,
6358 uint32_t offset) {
6359 Register root_reg = root.AsRegister<Register>();
6360 if (kEmitCompilerReadBarrier) {
6361 if (kUseBakerReadBarrier) {
6362 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6363 // Baker's read barrier are used:
6364 //
6365 // root = obj.field;
6366 // if (Thread::Current()->GetIsGcMarking()) {
6367 // root = ReadBarrier::Mark(root)
6368 // }
6369
6370 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6371 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6372 static_assert(
6373 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6374 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6375 "have different sizes.");
6376 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6377 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6378 "have different sizes.");
6379
Vladimir Marko953437b2016-08-24 08:30:46 +00006380 // Slow path marking the GC root `root`.
Roland Levillainc9285912015-12-18 10:38:42 +00006381 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006382 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006383 codegen_->AddSlowPath(slow_path);
6384
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006385 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006386 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006387 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006388 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6389 __ Bind(slow_path->GetExitLabel());
6390 } else {
6391 // GC root loaded through a slow path for read barriers other
6392 // than Baker's.
6393 // /* GcRoot<mirror::Object>* */ root = obj + offset
6394 __ AddConstant(root_reg, obj, offset);
6395 // /* mirror::Object* */ root = root->Read()
6396 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6397 }
6398 } else {
6399 // Plain GC root load with no read barrier.
6400 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6401 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6402 // Note that GC roots are not affected by heap poisoning, thus we
6403 // do not have to unpoison `root_reg` here.
6404 }
6405}
6406
6407void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6408 Location ref,
6409 Register obj,
6410 uint32_t offset,
6411 Location temp,
6412 bool needs_null_check) {
6413 DCHECK(kEmitCompilerReadBarrier);
6414 DCHECK(kUseBakerReadBarrier);
6415
6416 // /* HeapReference<Object> */ ref = *(obj + offset)
6417 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006418 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006419 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006420 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006421}
6422
6423void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6424 Location ref,
6425 Register obj,
6426 uint32_t data_offset,
6427 Location index,
6428 Location temp,
6429 bool needs_null_check) {
6430 DCHECK(kEmitCompilerReadBarrier);
6431 DCHECK(kUseBakerReadBarrier);
6432
Roland Levillainbfea3352016-06-23 13:48:47 +01006433 static_assert(
6434 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6435 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006436 // /* HeapReference<Object> */ ref =
6437 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006438 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006439 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006440 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006441}
6442
6443void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6444 Location ref,
6445 Register obj,
6446 uint32_t offset,
6447 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006448 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006449 Location temp,
6450 bool needs_null_check) {
6451 DCHECK(kEmitCompilerReadBarrier);
6452 DCHECK(kUseBakerReadBarrier);
6453
6454 // In slow path based read barriers, the read barrier call is
6455 // inserted after the original load. However, in fast path based
6456 // Baker's read barriers, we need to perform the load of
6457 // mirror::Object::monitor_ *before* the original reference load.
6458 // This load-load ordering is required by the read barrier.
6459 // The fast path/slow path (for Baker's algorithm) should look like:
6460 //
6461 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6462 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6463 // HeapReference<Object> ref = *src; // Original reference load.
6464 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6465 // if (is_gray) {
6466 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6467 // }
6468 //
6469 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006470 // slightly more complex as it performs additional checks that we do
6471 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006472
6473 Register ref_reg = ref.AsRegister<Register>();
6474 Register temp_reg = temp.AsRegister<Register>();
6475 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6476
6477 // /* int32_t */ monitor = obj->monitor_
6478 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6479 if (needs_null_check) {
6480 MaybeRecordImplicitNullCheck(instruction);
6481 }
6482 // /* LockWord */ lock_word = LockWord(monitor)
6483 static_assert(sizeof(LockWord) == sizeof(int32_t),
6484 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006485
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006486 // Introduce a dependency on the lock_word including the rb_state,
6487 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006488 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006489 // `obj` is unchanged by this operation, but its value now depends
6490 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006491 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006492
6493 // The actual reference load.
6494 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006495 // Load types involving an "index": ArrayGet and
6496 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6497 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006498 if (index.IsConstant()) {
6499 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006500 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006501 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6502 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006503 // Handle the special case of the
6504 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6505 // a register pair as index ("long offset"), of which only the low
6506 // part contains data.
6507 Register index_reg = index.IsRegisterPair()
6508 ? index.AsRegisterPairLow<Register>()
6509 : index.AsRegister<Register>();
6510 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006511 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6512 }
6513 } else {
6514 // /* HeapReference<Object> */ ref = *(obj + offset)
6515 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6516 }
6517
6518 // Object* ref = ref_addr->AsMirrorPtr()
6519 __ MaybeUnpoisonHeapReference(ref_reg);
6520
Vladimir Marko953437b2016-08-24 08:30:46 +00006521 // Slow path marking the object `ref` when it is gray.
Roland Levillainc9285912015-12-18 10:38:42 +00006522 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006523 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006524 AddSlowPath(slow_path);
6525
6526 // if (rb_state == ReadBarrier::gray_ptr_)
6527 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006528 // Given the numeric representation, it's enough to check the low bit of the
6529 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6530 // which can be a 16-bit instruction unlike the TST immediate.
6531 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6532 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6533 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6534 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6535 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006536 __ Bind(slow_path->GetExitLabel());
6537}
6538
6539void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6540 Location out,
6541 Location ref,
6542 Location obj,
6543 uint32_t offset,
6544 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006545 DCHECK(kEmitCompilerReadBarrier);
6546
Roland Levillainc9285912015-12-18 10:38:42 +00006547 // Insert a slow path based read barrier *after* the reference load.
6548 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006549 // If heap poisoning is enabled, the unpoisoning of the loaded
6550 // reference will be carried out by the runtime within the slow
6551 // path.
6552 //
6553 // Note that `ref` currently does not get unpoisoned (when heap
6554 // poisoning is enabled), which is alright as the `ref` argument is
6555 // not used by the artReadBarrierSlow entry point.
6556 //
6557 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6558 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6559 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6560 AddSlowPath(slow_path);
6561
Roland Levillain3b359c72015-11-17 19:35:12 +00006562 __ b(slow_path->GetEntryLabel());
6563 __ Bind(slow_path->GetExitLabel());
6564}
6565
Roland Levillainc9285912015-12-18 10:38:42 +00006566void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6567 Location out,
6568 Location ref,
6569 Location obj,
6570 uint32_t offset,
6571 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006572 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006573 // Baker's read barriers shall be handled by the fast path
6574 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6575 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006576 // If heap poisoning is enabled, unpoisoning will be taken care of
6577 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006578 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006579 } else if (kPoisonHeapReferences) {
6580 __ UnpoisonHeapReference(out.AsRegister<Register>());
6581 }
6582}
6583
Roland Levillainc9285912015-12-18 10:38:42 +00006584void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6585 Location out,
6586 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006587 DCHECK(kEmitCompilerReadBarrier);
6588
Roland Levillainc9285912015-12-18 10:38:42 +00006589 // Insert a slow path based read barrier *after* the GC root load.
6590 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006591 // Note that GC roots are not affected by heap poisoning, so we do
6592 // not need to do anything special for this here.
6593 SlowPathCode* slow_path =
6594 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6595 AddSlowPath(slow_path);
6596
Roland Levillain3b359c72015-11-17 19:35:12 +00006597 __ b(slow_path->GetEntryLabel());
6598 __ Bind(slow_path->GetExitLabel());
6599}
6600
Vladimir Markodc151b22015-10-15 18:02:30 +01006601HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6602 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6603 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006604 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6605 // We disable pc-relative load when there is an irreducible loop, as the optimization
6606 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006607 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6608 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006609 if (GetGraph()->HasIrreducibleLoops() &&
6610 (dispatch_info.method_load_kind ==
6611 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6612 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6613 }
6614
6615 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006616 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6617 if (&outer_dex_file != target_method.dex_file) {
6618 // Calls across dex files are more likely to exceed the available BL range,
6619 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6620 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6621 (desired_dispatch_info.method_load_kind ==
6622 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6623 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6624 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6625 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006626 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006627 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006628 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006629 0u
6630 };
6631 }
6632 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006633 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006634}
6635
Vladimir Markob4536b72015-11-24 13:45:23 +00006636Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6637 Register temp) {
6638 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6639 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6640 if (!invoke->GetLocations()->Intrinsified()) {
6641 return location.AsRegister<Register>();
6642 }
6643 // For intrinsics we allow any location, so it may be on the stack.
6644 if (!location.IsRegister()) {
6645 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6646 return temp;
6647 }
6648 // For register locations, check if the register was saved. If so, get it from the stack.
6649 // Note: There is a chance that the register was saved but not overwritten, so we could
6650 // save one load. However, since this is just an intrinsic slow path we prefer this
6651 // simple and more robust approach rather that trying to determine if that's the case.
6652 SlowPathCode* slow_path = GetCurrentSlowPath();
6653 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6654 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6655 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6656 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6657 return temp;
6658 }
6659 return location.AsRegister<Register>();
6660}
6661
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006662void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006663 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006664 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006665 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6666 // LR = code address from literal pool with link-time patch.
6667 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006668 break;
6669 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6670 // LR = invoke->GetDirectCodePtr();
6671 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006672 break;
6673 default:
6674 break;
6675 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006676
Vladimir Marko58155012015-08-19 12:49:41 +00006677 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6678 switch (invoke->GetMethodLoadKind()) {
6679 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6680 // temp = thread->string_init_entrypoint
6681 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6682 break;
6683 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006684 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006685 break;
6686 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6687 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6688 break;
6689 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6690 __ LoadLiteral(temp.AsRegister<Register>(),
6691 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6692 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006693 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6694 HArmDexCacheArraysBase* base =
6695 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6696 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6697 temp.AsRegister<Register>());
6698 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6699 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6700 break;
6701 }
Vladimir Marko58155012015-08-19 12:49:41 +00006702 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006703 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006704 Register method_reg;
6705 Register reg = temp.AsRegister<Register>();
6706 if (current_method.IsRegister()) {
6707 method_reg = current_method.AsRegister<Register>();
6708 } else {
6709 DCHECK(invoke->GetLocations()->Intrinsified());
6710 DCHECK(!current_method.IsValid());
6711 method_reg = reg;
6712 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6713 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006714 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6715 __ LoadFromOffset(kLoadWord,
6716 reg,
6717 method_reg,
6718 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006719 // temp = temp[index_in_cache];
6720 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6721 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006722 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6723 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006724 }
Vladimir Marko58155012015-08-19 12:49:41 +00006725 }
6726
6727 switch (invoke->GetCodePtrLocation()) {
6728 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6729 __ bl(GetFrameEntryLabel());
6730 break;
6731 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006732 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006733 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006734 // Arbitrarily branch to the BL itself, override at link time.
6735 __ bl(&relative_call_patches_.back().label);
6736 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006737 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6738 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6739 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006740 // LR()
6741 __ blx(LR);
6742 break;
6743 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6744 // LR = callee_method->entry_point_from_quick_compiled_code_
6745 __ LoadFromOffset(
6746 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006747 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006748 // LR()
6749 __ blx(LR);
6750 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006751 }
6752
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006753 DCHECK(!IsLeafMethod());
6754}
6755
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006756void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6757 Register temp = temp_location.AsRegister<Register>();
6758 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6759 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006760
6761 // Use the calling convention instead of the location of the receiver, as
6762 // intrinsics may have put the receiver in a different register. In the intrinsics
6763 // slow path, the arguments have been moved to the right place, so here we are
6764 // guaranteed that the receiver is the first register of the calling convention.
6765 InvokeDexCallingConvention calling_convention;
6766 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006767 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006768 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006769 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006770 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006771 // Instead of simply (possibly) unpoisoning `temp` here, we should
6772 // emit a read barrier for the previous class reference load.
6773 // However this is not required in practice, as this is an
6774 // intermediate/temporary reference and because the current
6775 // concurrent copying collector keeps the from-space memory
6776 // intact/accessible until the end of the marking phase (the
6777 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006778 __ MaybeUnpoisonHeapReference(temp);
6779 // temp = temp->GetMethodAt(method_offset);
6780 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006781 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006782 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6783 // LR = temp->GetEntryPoint();
6784 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6785 // LR();
6786 __ blx(LR);
6787}
6788
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006789CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6790 const DexFile& dex_file, uint32_t string_index) {
6791 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6792}
6793
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006794CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6795 const DexFile& dex_file, uint32_t type_index) {
6796 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6797}
6798
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006799CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6800 const DexFile& dex_file, uint32_t element_offset) {
6801 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6802}
6803
6804CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6805 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6806 patches->emplace_back(dex_file, offset_or_index);
6807 return &patches->back();
6808}
6809
6810Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6811 uint32_t string_index) {
6812 return boot_image_string_patches_.GetOrCreate(
6813 StringReference(&dex_file, string_index),
6814 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6815}
6816
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006817Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6818 uint32_t type_index) {
6819 return boot_image_type_patches_.GetOrCreate(
6820 TypeReference(&dex_file, type_index),
6821 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6822}
6823
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006824Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6825 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6826 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6827 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6828}
6829
6830Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6831 return DeduplicateUint32Literal(address, &uint32_literals_);
6832}
6833
Vladimir Marko58155012015-08-19 12:49:41 +00006834void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6835 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006836 size_t size =
6837 method_patches_.size() +
6838 call_patches_.size() +
6839 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006840 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6841 boot_image_string_patches_.size() +
6842 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006843 boot_image_type_patches_.size() +
6844 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006845 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006846 linker_patches->reserve(size);
6847 for (const auto& entry : method_patches_) {
6848 const MethodReference& target_method = entry.first;
6849 Literal* literal = entry.second;
6850 DCHECK(literal->GetLabel()->IsBound());
6851 uint32_t literal_offset = literal->GetLabel()->Position();
6852 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6853 target_method.dex_file,
6854 target_method.dex_method_index));
6855 }
6856 for (const auto& entry : call_patches_) {
6857 const MethodReference& target_method = entry.first;
6858 Literal* literal = entry.second;
6859 DCHECK(literal->GetLabel()->IsBound());
6860 uint32_t literal_offset = literal->GetLabel()->Position();
6861 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6862 target_method.dex_file,
6863 target_method.dex_method_index));
6864 }
6865 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6866 uint32_t literal_offset = info.label.Position();
6867 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6868 info.target_method.dex_file,
6869 info.target_method.dex_method_index));
6870 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006871 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6872 const DexFile& dex_file = info.target_dex_file;
6873 size_t base_element_offset = info.offset_or_index;
6874 DCHECK(info.add_pc_label.IsBound());
6875 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006876 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006877 DCHECK(info.movw_label.IsBound());
6878 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006879 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6880 &dex_file,
6881 add_pc_offset,
6882 base_element_offset));
6883 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006884 DCHECK(info.movt_label.IsBound());
6885 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006886 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6887 &dex_file,
6888 add_pc_offset,
6889 base_element_offset));
6890 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006891 for (const auto& entry : boot_image_string_patches_) {
6892 const StringReference& target_string = entry.first;
6893 Literal* literal = entry.second;
6894 DCHECK(literal->GetLabel()->IsBound());
6895 uint32_t literal_offset = literal->GetLabel()->Position();
6896 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6897 target_string.dex_file,
6898 target_string.string_index));
6899 }
6900 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6901 const DexFile& dex_file = info.target_dex_file;
6902 uint32_t string_index = info.offset_or_index;
6903 DCHECK(info.add_pc_label.IsBound());
6904 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6905 // Add MOVW patch.
6906 DCHECK(info.movw_label.IsBound());
6907 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6908 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6909 &dex_file,
6910 add_pc_offset,
6911 string_index));
6912 // Add MOVT patch.
6913 DCHECK(info.movt_label.IsBound());
6914 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6915 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6916 &dex_file,
6917 add_pc_offset,
6918 string_index));
6919 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006920 for (const auto& entry : boot_image_type_patches_) {
6921 const TypeReference& target_type = entry.first;
6922 Literal* literal = entry.second;
6923 DCHECK(literal->GetLabel()->IsBound());
6924 uint32_t literal_offset = literal->GetLabel()->Position();
6925 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6926 target_type.dex_file,
6927 target_type.type_index));
6928 }
6929 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6930 const DexFile& dex_file = info.target_dex_file;
6931 uint32_t type_index = info.offset_or_index;
6932 DCHECK(info.add_pc_label.IsBound());
6933 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6934 // Add MOVW patch.
6935 DCHECK(info.movw_label.IsBound());
6936 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6937 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6938 &dex_file,
6939 add_pc_offset,
6940 type_index));
6941 // Add MOVT patch.
6942 DCHECK(info.movt_label.IsBound());
6943 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6944 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6945 &dex_file,
6946 add_pc_offset,
6947 type_index));
6948 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006949 for (const auto& entry : boot_image_address_patches_) {
6950 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6951 Literal* literal = entry.second;
6952 DCHECK(literal->GetLabel()->IsBound());
6953 uint32_t literal_offset = literal->GetLabel()->Position();
6954 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6955 }
6956}
6957
6958Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6959 return map->GetOrCreate(
6960 value,
6961 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006962}
6963
6964Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6965 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006966 return map->GetOrCreate(
6967 target_method,
6968 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006969}
6970
6971Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6972 return DeduplicateMethodLiteral(target_method, &method_patches_);
6973}
6974
6975Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6976 return DeduplicateMethodLiteral(target_method, &call_patches_);
6977}
6978
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006979void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6980 LocationSummary* locations =
6981 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6982 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6983 Location::RequiresRegister());
6984 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6985 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6987}
6988
6989void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6990 LocationSummary* locations = instr->GetLocations();
6991 Register res = locations->Out().AsRegister<Register>();
6992 Register accumulator =
6993 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6994 Register mul_left =
6995 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6996 Register mul_right =
6997 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6998
6999 if (instr->GetOpKind() == HInstruction::kAdd) {
7000 __ mla(res, mul_left, mul_right, accumulator);
7001 } else {
7002 __ mls(res, mul_left, mul_right, accumulator);
7003 }
7004}
7005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007006void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007007 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007008 LOG(FATAL) << "Unreachable";
7009}
7010
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007011void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007012 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007013 LOG(FATAL) << "Unreachable";
7014}
7015
Mark Mendellfe57faa2015-09-18 09:26:15 -04007016// Simple implementation of packed switch - generate cascaded compare/jumps.
7017void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7018 LocationSummary* locations =
7019 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7020 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007021 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007022 codegen_->GetAssembler()->IsThumb()) {
7023 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7024 if (switch_instr->GetStartValue() != 0) {
7025 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7026 }
7027 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007028}
7029
7030void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7031 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007032 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007033 LocationSummary* locations = switch_instr->GetLocations();
7034 Register value_reg = locations->InAt(0).AsRegister<Register>();
7035 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7036
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007037 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007038 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007039 Register temp_reg = IP;
7040 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7041 // the immediate, because IP is used as the destination register. For the other
7042 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7043 // and they can be encoded in the instruction without making use of IP register.
7044 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7045
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007046 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007047 // Jump to successors[0] if value == lower_bound.
7048 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7049 int32_t last_index = 0;
7050 for (; num_entries - last_index > 2; last_index += 2) {
7051 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7052 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7053 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7054 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7055 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7056 }
7057 if (num_entries - last_index == 2) {
7058 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007059 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007060 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007061 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007062
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007063 // And the default for any other value.
7064 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7065 __ b(codegen_->GetLabelOf(default_block));
7066 }
7067 } else {
7068 // Create a table lookup.
7069 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7070
7071 // Materialize a pointer to the switch table
7072 std::vector<Label*> labels(num_entries);
7073 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7074 for (uint32_t i = 0; i < num_entries; i++) {
7075 labels[i] = codegen_->GetLabelOf(successors[i]);
7076 }
7077 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7078
7079 // Remove the bias.
7080 Register key_reg;
7081 if (lower_bound != 0) {
7082 key_reg = locations->GetTemp(1).AsRegister<Register>();
7083 __ AddConstant(key_reg, value_reg, -lower_bound);
7084 } else {
7085 key_reg = value_reg;
7086 }
7087
7088 // Check whether the value is in the table, jump to default block if not.
7089 __ CmpConstant(key_reg, num_entries - 1);
7090 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7091
7092 // Load the displacement from the table.
7093 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7094
7095 // Dispatch is a direct add to the PC (for Thumb2).
7096 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007097 }
7098}
7099
Vladimir Markob4536b72015-11-24 13:45:23 +00007100void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7101 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7102 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007103}
7104
7105void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7106 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007107 CodeGeneratorARM::PcRelativePatchInfo* labels =
7108 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007109 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007110 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007111 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007112 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007113 __ BindTrackedLabel(&labels->add_pc_label);
7114 __ add(base_reg, base_reg, ShifterOperand(PC));
7115}
7116
Andreas Gampe85b62f22015-09-09 13:15:38 -07007117void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7118 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007119 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007120 return;
7121 }
7122
7123 DCHECK_NE(type, Primitive::kPrimVoid);
7124
7125 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7126 if (return_loc.Equals(trg)) {
7127 return;
7128 }
7129
7130 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7131 // with the last branch.
7132 if (type == Primitive::kPrimLong) {
7133 HParallelMove parallel_move(GetGraph()->GetArena());
7134 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7135 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7136 GetMoveResolver()->EmitNativeCode(&parallel_move);
7137 } else if (type == Primitive::kPrimDouble) {
7138 HParallelMove parallel_move(GetGraph()->GetArena());
7139 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7140 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7141 GetMoveResolver()->EmitNativeCode(&parallel_move);
7142 } else {
7143 // Let the parallel move resolver take care of all of this.
7144 HParallelMove parallel_move(GetGraph()->GetArena());
7145 parallel_move.AddMove(return_loc, trg, type, nullptr);
7146 GetMoveResolver()->EmitNativeCode(&parallel_move);
7147 }
7148}
7149
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007150void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7151 LocationSummary* locations =
7152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7153 locations->SetInAt(0, Location::RequiresRegister());
7154 locations->SetOut(Location::RequiresRegister());
7155}
7156
7157void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7158 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007159 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007160 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007161 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007162 __ LoadFromOffset(kLoadWord,
7163 locations->Out().AsRegister<Register>(),
7164 locations->InAt(0).AsRegister<Register>(),
7165 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007166 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007167 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007168 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007169 __ LoadFromOffset(kLoadWord,
7170 locations->Out().AsRegister<Register>(),
7171 locations->InAt(0).AsRegister<Register>(),
7172 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7173 __ LoadFromOffset(kLoadWord,
7174 locations->Out().AsRegister<Register>(),
7175 locations->Out().AsRegister<Register>(),
7176 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007177 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007178}
7179
Roland Levillain4d027112015-07-01 15:41:14 +01007180#undef __
7181#undef QUICK_ENTRY_POINT
7182
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007183} // namespace arm
7184} // namespace art