blob: ba2df07094c6b1bd340e1f0aa7c4921ef6601fc3 [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() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100428 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000429 instruction_->IsLoadClass() ||
430 instruction_->IsLoadString() ||
431 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100432 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100433 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
434 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000435 << "Unexpected instruction in read barrier marking slow path: "
436 << instruction_->DebugName();
437
438 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100439 // No need to save live registers; it's taken care of by the
440 // entrypoint. Also, there is no need to update the stack mask,
441 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000442 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100443 DCHECK_NE(reg, SP);
444 DCHECK_NE(reg, LR);
445 DCHECK_NE(reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100446 // IP is used internally by the ReadBarrierMarkRegX entry point
447 // as a temporary, it cannot be the entry point's input/output.
448 DCHECK_NE(reg, IP);
Roland Levillain02b75802016-07-13 11:54:35 +0100449 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
450 // "Compact" slow path, saving two moves.
451 //
452 // Instead of using the standard runtime calling convention (input
453 // and output in R0):
454 //
455 // R0 <- obj
456 // R0 <- ReadBarrierMark(R0)
457 // obj <- R0
458 //
459 // we just use rX (the register holding `obj`) as input and output
460 // of a dedicated entrypoint:
461 //
462 // rX <- ReadBarrierMarkRegX(rX)
463 //
464 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700465 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100466 // This runtime call does not require a stack map.
467 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000468 __ b(GetExitLabel());
469 }
470
471 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000472 const Location obj_;
473
474 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
475};
476
Roland Levillain3b359c72015-11-17 19:35:12 +0000477// Slow path generating a read barrier for a heap reference.
478class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
479 public:
480 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
481 Location out,
482 Location ref,
483 Location obj,
484 uint32_t offset,
485 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000486 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000487 out_(out),
488 ref_(ref),
489 obj_(obj),
490 offset_(offset),
491 index_(index) {
492 DCHECK(kEmitCompilerReadBarrier);
493 // If `obj` is equal to `out` or `ref`, it means the initial object
494 // has been overwritten by (or after) the heap object reference load
495 // to be instrumented, e.g.:
496 //
497 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000498 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000499 //
500 // In that case, we have lost the information about the original
501 // object, and the emitted read barrier cannot work properly.
502 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
503 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
504 }
505
506 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
507 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
508 LocationSummary* locations = instruction_->GetLocations();
509 Register reg_out = out_.AsRegister<Register>();
510 DCHECK(locations->CanCall());
511 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100512 DCHECK(instruction_->IsInstanceFieldGet() ||
513 instruction_->IsStaticFieldGet() ||
514 instruction_->IsArrayGet() ||
515 instruction_->IsInstanceOf() ||
516 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100517 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000518 << "Unexpected instruction in read barrier for heap reference slow path: "
519 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000520
521 __ Bind(GetEntryLabel());
522 SaveLiveRegisters(codegen, locations);
523
524 // We may have to change the index's value, but as `index_` is a
525 // constant member (like other "inputs" of this slow path),
526 // introduce a copy of it, `index`.
527 Location index = index_;
528 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100529 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000530 if (instruction_->IsArrayGet()) {
531 // Compute the actual memory offset and store it in `index`.
532 Register index_reg = index_.AsRegister<Register>();
533 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
534 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
535 // We are about to change the value of `index_reg` (see the
536 // calls to art::arm::Thumb2Assembler::Lsl and
537 // art::arm::Thumb2Assembler::AddConstant below), but it has
538 // not been saved by the previous call to
539 // art::SlowPathCode::SaveLiveRegisters, as it is a
540 // callee-save register --
541 // art::SlowPathCode::SaveLiveRegisters does not consider
542 // callee-save registers, as it has been designed with the
543 // assumption that callee-save registers are supposed to be
544 // handled by the called function. So, as a callee-save
545 // register, `index_reg` _would_ eventually be saved onto
546 // the stack, but it would be too late: we would have
547 // changed its value earlier. Therefore, we manually save
548 // it here into another freely available register,
549 // `free_reg`, chosen of course among the caller-save
550 // registers (as a callee-save `free_reg` register would
551 // exhibit the same problem).
552 //
553 // Note we could have requested a temporary register from
554 // the register allocator instead; but we prefer not to, as
555 // this is a slow path, and we know we can find a
556 // caller-save register that is available.
557 Register free_reg = FindAvailableCallerSaveRegister(codegen);
558 __ Mov(free_reg, index_reg);
559 index_reg = free_reg;
560 index = Location::RegisterLocation(index_reg);
561 } else {
562 // The initial register stored in `index_` has already been
563 // saved in the call to art::SlowPathCode::SaveLiveRegisters
564 // (as it is not a callee-save register), so we can freely
565 // use it.
566 }
567 // Shifting the index value contained in `index_reg` by the scale
568 // factor (2) cannot overflow in practice, as the runtime is
569 // unable to allocate object arrays with a size larger than
570 // 2^26 - 1 (that is, 2^28 - 4 bytes).
571 __ Lsl(index_reg, index_reg, TIMES_4);
572 static_assert(
573 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
574 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
575 __ AddConstant(index_reg, index_reg, offset_);
576 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100577 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
578 // intrinsics, `index_` is not shifted by a scale factor of 2
579 // (as in the case of ArrayGet), as it is actually an offset
580 // to an object field within an object.
581 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000582 DCHECK(instruction_->GetLocations()->Intrinsified());
583 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
584 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
585 << instruction_->AsInvoke()->GetIntrinsic();
586 DCHECK_EQ(offset_, 0U);
587 DCHECK(index_.IsRegisterPair());
588 // UnsafeGet's offset location is a register pair, the low
589 // part contains the correct offset.
590 index = index_.ToLow();
591 }
592 }
593
594 // We're moving two or three locations to locations that could
595 // overlap, so we need a parallel move resolver.
596 InvokeRuntimeCallingConvention calling_convention;
597 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
598 parallel_move.AddMove(ref_,
599 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
600 Primitive::kPrimNot,
601 nullptr);
602 parallel_move.AddMove(obj_,
603 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
604 Primitive::kPrimNot,
605 nullptr);
606 if (index.IsValid()) {
607 parallel_move.AddMove(index,
608 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
609 Primitive::kPrimInt,
610 nullptr);
611 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
612 } else {
613 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
614 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
615 }
616 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
617 instruction_,
618 instruction_->GetDexPc(),
619 this);
620 CheckEntrypointTypes<
621 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
622 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
623
624 RestoreLiveRegisters(codegen, locations);
625 __ b(GetExitLabel());
626 }
627
628 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
629
630 private:
631 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
632 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
633 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
634 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
635 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
636 return static_cast<Register>(i);
637 }
638 }
639 // We shall never fail to find a free caller-save register, as
640 // there are more than two core caller-save registers on ARM
641 // (meaning it is possible to find one which is different from
642 // `ref` and `obj`).
643 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
644 LOG(FATAL) << "Could not find a free caller-save register";
645 UNREACHABLE();
646 }
647
Roland Levillain3b359c72015-11-17 19:35:12 +0000648 const Location out_;
649 const Location ref_;
650 const Location obj_;
651 const uint32_t offset_;
652 // An additional location containing an index to an array.
653 // Only used for HArrayGet and the UnsafeGetObject &
654 // UnsafeGetObjectVolatile intrinsics.
655 const Location index_;
656
657 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
658};
659
660// Slow path generating a read barrier for a GC root.
661class ReadBarrierForRootSlowPathARM : public SlowPathCode {
662 public:
663 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000664 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000665 DCHECK(kEmitCompilerReadBarrier);
666 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000667
668 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
669 LocationSummary* locations = instruction_->GetLocations();
670 Register reg_out = out_.AsRegister<Register>();
671 DCHECK(locations->CanCall());
672 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000673 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
674 << "Unexpected instruction in read barrier for GC root slow path: "
675 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000676
677 __ Bind(GetEntryLabel());
678 SaveLiveRegisters(codegen, locations);
679
680 InvokeRuntimeCallingConvention calling_convention;
681 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
682 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
683 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
684 instruction_,
685 instruction_->GetDexPc(),
686 this);
687 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
688 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
689
690 RestoreLiveRegisters(codegen, locations);
691 __ b(GetExitLabel());
692 }
693
694 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
695
696 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000697 const Location out_;
698 const Location root_;
699
700 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
701};
702
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000703#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100704// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
705#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700706
Aart Bike9f37602015-10-09 11:15:55 -0700707inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700708 switch (cond) {
709 case kCondEQ: return EQ;
710 case kCondNE: return NE;
711 case kCondLT: return LT;
712 case kCondLE: return LE;
713 case kCondGT: return GT;
714 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700715 case kCondB: return LO;
716 case kCondBE: return LS;
717 case kCondA: return HI;
718 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700719 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100720 LOG(FATAL) << "Unreachable";
721 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700722}
723
Aart Bike9f37602015-10-09 11:15:55 -0700724// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100725inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700726 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727 case kCondEQ: return EQ;
728 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700729 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730 case kCondLT: return LO;
731 case kCondLE: return LS;
732 case kCondGT: return HI;
733 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700734 // Unsigned remain unchanged.
735 case kCondB: return LO;
736 case kCondBE: return LS;
737 case kCondA: return HI;
738 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700739 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100740 LOG(FATAL) << "Unreachable";
741 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700742}
743
Vladimir Markod6e069b2016-01-18 11:11:01 +0000744inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
745 // The ARM condition codes can express all the necessary branches, see the
746 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
747 // There is no dex instruction or HIR that would need the missing conditions
748 // "equal or unordered" or "not equal".
749 switch (cond) {
750 case kCondEQ: return EQ;
751 case kCondNE: return NE /* unordered */;
752 case kCondLT: return gt_bias ? CC : LT /* unordered */;
753 case kCondLE: return gt_bias ? LS : LE /* unordered */;
754 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
755 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
756 default:
757 LOG(FATAL) << "UNREACHABLE";
758 UNREACHABLE();
759 }
760}
761
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100762void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100763 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100764}
765
766void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100767 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100768}
769
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100770size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
771 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
772 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100773}
774
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100775size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
776 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
777 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100778}
779
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000780size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
781 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
782 return kArmWordSize;
783}
784
785size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
786 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
787 return kArmWordSize;
788}
789
Calin Juravle34166012014-12-19 17:22:29 +0000790CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000791 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100792 const CompilerOptions& compiler_options,
793 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000794 : CodeGenerator(graph,
795 kNumberOfCoreRegisters,
796 kNumberOfSRegisters,
797 kNumberOfRegisterPairs,
798 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
799 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000800 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
801 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100802 compiler_options,
803 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100804 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100805 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100806 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100807 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100808 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000809 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000810 uint32_literals_(std::less<uint32_t>(),
811 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100812 method_patches_(MethodReferenceComparator(),
813 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
814 call_patches_(MethodReferenceComparator(),
815 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000816 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000817 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
818 boot_image_string_patches_(StringReferenceValueComparator(),
819 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
820 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100821 boot_image_type_patches_(TypeReferenceValueComparator(),
822 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
823 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000824 boot_image_address_patches_(std::less<uint32_t>(),
825 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700826 // Always save the LR register to mimic Quick.
827 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100828}
829
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000830void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
831 // Ensure that we fix up branches and literal loads and emit the literal pool.
832 __ FinalizeCode();
833
834 // Adjust native pc offsets in stack maps.
835 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
836 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
837 uint32_t new_position = __ GetAdjustedPosition(old_position);
838 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
839 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100840 // Adjust pc offsets for the disassembly information.
841 if (disasm_info_ != nullptr) {
842 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
843 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
844 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
845 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
846 it.second.start = __ GetAdjustedPosition(it.second.start);
847 it.second.end = __ GetAdjustedPosition(it.second.end);
848 }
849 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
850 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
851 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
852 }
853 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000854
855 CodeGenerator::Finalize(allocator);
856}
857
David Brazdil58282f42016-01-14 12:45:10 +0000858void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100859 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100860 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100861
862 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100863 blocked_core_registers_[SP] = true;
864 blocked_core_registers_[LR] = true;
865 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100867 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100868 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100869
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100870 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100871 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100872
David Brazdil58282f42016-01-14 12:45:10 +0000873 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100874 // Stubs do not save callee-save floating point registers. If the graph
875 // is debuggable, we need to deal with these registers differently. For
876 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000877 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
878 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
879 }
880 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100881
882 UpdateBlockedPairRegisters();
883}
884
885void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
886 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
887 ArmManagedRegister current =
888 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
889 if (blocked_core_registers_[current.AsRegisterPairLow()]
890 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
891 blocked_register_pairs_[i] = true;
892 }
893 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100894}
895
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100896InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800897 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100898 assembler_(codegen->GetAssembler()),
899 codegen_(codegen) {}
900
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000901void CodeGeneratorARM::ComputeSpillMask() {
902 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
903 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000904 // There is no easy instruction to restore just the PC on thumb2. We spill and
905 // restore another arbitrary register.
906 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000907 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
908 // We use vpush and vpop for saving and restoring floating point registers, which take
909 // a SRegister and the number of registers to save/restore after that SRegister. We
910 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
911 // but in the range.
912 if (fpu_spill_mask_ != 0) {
913 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
914 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
915 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
916 fpu_spill_mask_ |= (1 << i);
917 }
918 }
919}
920
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100921static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100922 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100923}
924
925static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100926 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100927}
928
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000929void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000930 bool skip_overflow_check =
931 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000932 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000933 __ Bind(&frame_entry_label_);
934
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000935 if (HasEmptyFrame()) {
936 return;
937 }
938
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100939 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000940 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
941 __ LoadFromOffset(kLoadWord, IP, IP, 0);
942 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100943 }
944
Andreas Gampe501fd632015-09-10 16:11:06 -0700945 __ PushList(core_spill_mask_);
946 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
947 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000948 if (fpu_spill_mask_ != 0) {
949 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
950 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100951 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100952 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000953 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100954 int adjust = GetFrameSize() - FrameEntrySpillSize();
955 __ AddConstant(SP, -adjust);
956 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100957 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000958}
959
960void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000961 if (HasEmptyFrame()) {
962 __ bx(LR);
963 return;
964 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100965 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100966 int adjust = GetFrameSize() - FrameEntrySpillSize();
967 __ AddConstant(SP, adjust);
968 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000969 if (fpu_spill_mask_ != 0) {
970 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
971 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -0700972 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100973 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000974 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700975 // Pop LR into PC to return.
976 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
977 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
978 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100979 __ cfi().RestoreState();
980 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000981}
982
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100983void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700984 Label* label = GetLabelOf(block);
985 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000986}
987
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100988Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100989 switch (type) {
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimInt:
995 case Primitive::kPrimNot: {
996 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000997 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100999 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001000 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001001 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001002 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001003 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001005 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001006 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001007 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001009 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001011 if (calling_convention.GetRegisterAt(index) == R1) {
1012 // Skip R1, and use R2_R3 instead.
1013 gp_index_++;
1014 index++;
1015 }
1016 }
1017 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1018 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001019 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001020
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001021 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001022 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001023 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001024 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1025 }
1026 }
1027
1028 case Primitive::kPrimFloat: {
1029 uint32_t stack_index = stack_index_++;
1030 if (float_index_ % 2 == 0) {
1031 float_index_ = std::max(double_index_, float_index_);
1032 }
1033 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1034 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1035 } else {
1036 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1037 }
1038 }
1039
1040 case Primitive::kPrimDouble: {
1041 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1042 uint32_t stack_index = stack_index_;
1043 stack_index_ += 2;
1044 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1045 uint32_t index = double_index_;
1046 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001047 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001048 calling_convention.GetFpuRegisterAt(index),
1049 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001050 DCHECK(ExpectedPairLayout(result));
1051 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001052 } else {
1053 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001054 }
1055 }
1056
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001057 case Primitive::kPrimVoid:
1058 LOG(FATAL) << "Unexpected parameter type " << type;
1059 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001060 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001061 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001062}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001063
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001064Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001065 switch (type) {
1066 case Primitive::kPrimBoolean:
1067 case Primitive::kPrimByte:
1068 case Primitive::kPrimChar:
1069 case Primitive::kPrimShort:
1070 case Primitive::kPrimInt:
1071 case Primitive::kPrimNot: {
1072 return Location::RegisterLocation(R0);
1073 }
1074
1075 case Primitive::kPrimFloat: {
1076 return Location::FpuRegisterLocation(S0);
1077 }
1078
1079 case Primitive::kPrimLong: {
1080 return Location::RegisterPairLocation(R0, R1);
1081 }
1082
1083 case Primitive::kPrimDouble: {
1084 return Location::FpuRegisterPairLocation(S0, S1);
1085 }
1086
1087 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001088 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001089 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001090
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001091 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001092}
1093
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001094Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1095 return Location::RegisterLocation(kMethodRegisterArgument);
1096}
1097
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098void CodeGeneratorARM::Move32(Location destination, Location source) {
1099 if (source.Equals(destination)) {
1100 return;
1101 }
1102 if (destination.IsRegister()) {
1103 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001106 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001108 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 } else if (destination.IsFpuRegister()) {
1111 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001112 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001114 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001115 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001116 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001119 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001121 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001123 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001125 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001126 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1127 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 }
1129 }
1130}
1131
1132void CodeGeneratorARM::Move64(Location destination, Location source) {
1133 if (source.Equals(destination)) {
1134 return;
1135 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001136 if (destination.IsRegisterPair()) {
1137 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001138 EmitParallelMoves(
1139 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1140 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001141 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001142 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001143 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1144 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001145 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001146 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001147 } else if (source.IsFpuRegisterPair()) {
1148 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1149 destination.AsRegisterPairHigh<Register>(),
1150 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
1152 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001153 DCHECK(ExpectedPairLayout(destination));
1154 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1155 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001157 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001159 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1160 SP,
1161 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001162 } else if (source.IsRegisterPair()) {
1163 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1164 source.AsRegisterPairLow<Register>(),
1165 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001167 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 } else {
1170 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001172 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 if (source.AsRegisterPairLow<Register>() == R1) {
1174 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001175 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1176 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 SP, destination.GetStackIndex());
1180 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001181 } else if (source.IsFpuRegisterPair()) {
1182 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1183 SP,
1184 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 } else {
1186 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001187 EmitParallelMoves(
1188 Location::StackSlot(source.GetStackIndex()),
1189 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001190 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001191 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001192 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1193 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 }
1195 }
1196}
1197
Calin Juravle175dc732015-08-25 15:42:32 +01001198void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1199 DCHECK(location.IsRegister());
1200 __ LoadImmediate(location.AsRegister<Register>(), value);
1201}
1202
Calin Juravlee460d1d2015-09-29 04:52:17 +01001203void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001204 HParallelMove move(GetGraph()->GetArena());
1205 move.AddMove(src, dst, dst_type, nullptr);
1206 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207}
1208
1209void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1210 if (location.IsRegister()) {
1211 locations->AddTemp(location);
1212 } else if (location.IsRegisterPair()) {
1213 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1214 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1215 } else {
1216 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1217 }
1218}
1219
Calin Juravle175dc732015-08-25 15:42:32 +01001220void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1221 HInstruction* instruction,
1222 uint32_t dex_pc,
1223 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001224 InvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001225 instruction,
1226 dex_pc,
1227 slow_path);
1228}
1229
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001230void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1231 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001232 uint32_t dex_pc,
1233 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001234 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001235 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1236 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001237 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001238}
1239
Roland Levillaindec8f632016-07-22 17:10:06 +01001240void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1241 HInstruction* instruction,
1242 SlowPathCode* slow_path) {
1243 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1244 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1245 __ blx(LR);
1246}
1247
David Brazdilfc6a86a2015-06-26 10:33:45 +00001248void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001249 DCHECK(!successor->IsExitBlock());
1250
1251 HBasicBlock* block = got->GetBlock();
1252 HInstruction* previous = got->GetPrevious();
1253
1254 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001255 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001256 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1257 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1258 return;
1259 }
1260
1261 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1262 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1263 }
1264 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001265 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001266 }
1267}
1268
David Brazdilfc6a86a2015-06-26 10:33:45 +00001269void LocationsBuilderARM::VisitGoto(HGoto* got) {
1270 got->SetLocations(nullptr);
1271}
1272
1273void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1274 HandleGoto(got, got->GetSuccessor());
1275}
1276
1277void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1278 try_boundary->SetLocations(nullptr);
1279}
1280
1281void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1282 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1283 if (!successor->IsExitBlock()) {
1284 HandleGoto(try_boundary, successor);
1285 }
1286}
1287
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001288void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001289 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001290}
1291
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001292void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001293}
1294
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001295void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1296 Primitive::Type type = instruction->InputAt(0)->GetType();
1297 Location lhs_loc = instruction->GetLocations()->InAt(0);
1298 Location rhs_loc = instruction->GetLocations()->InAt(1);
1299 if (rhs_loc.IsConstant()) {
1300 // 0.0 is the only immediate that can be encoded directly in
1301 // a VCMP instruction.
1302 //
1303 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1304 // specify that in a floating-point comparison, positive zero
1305 // and negative zero are considered equal, so we can use the
1306 // literal 0.0 for both cases here.
1307 //
1308 // Note however that some methods (Float.equal, Float.compare,
1309 // Float.compareTo, Double.equal, Double.compare,
1310 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1311 // StrictMath.min) consider 0.0 to be (strictly) greater than
1312 // -0.0. So if we ever translate calls to these methods into a
1313 // HCompare instruction, we must handle the -0.0 case with
1314 // care here.
1315 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1316 if (type == Primitive::kPrimFloat) {
1317 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1318 } else {
1319 DCHECK_EQ(type, Primitive::kPrimDouble);
1320 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1321 }
1322 } else {
1323 if (type == Primitive::kPrimFloat) {
1324 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1325 } else {
1326 DCHECK_EQ(type, Primitive::kPrimDouble);
1327 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1328 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1329 }
1330 }
1331}
1332
Roland Levillain4fa13f62015-07-06 18:11:54 +01001333void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1334 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001335 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001336 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001337 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001338}
1339
1340void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1341 Label* true_label,
1342 Label* false_label) {
1343 LocationSummary* locations = cond->GetLocations();
1344 Location left = locations->InAt(0);
1345 Location right = locations->InAt(1);
1346 IfCondition if_cond = cond->GetCondition();
1347
1348 Register left_high = left.AsRegisterPairHigh<Register>();
1349 Register left_low = left.AsRegisterPairLow<Register>();
1350 IfCondition true_high_cond = if_cond;
1351 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001352 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001353
1354 // Set the conditions for the test, remembering that == needs to be
1355 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001356 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001357 switch (if_cond) {
1358 case kCondEQ:
1359 case kCondNE:
1360 // Nothing to do.
1361 break;
1362 case kCondLT:
1363 false_high_cond = kCondGT;
1364 break;
1365 case kCondLE:
1366 true_high_cond = kCondLT;
1367 break;
1368 case kCondGT:
1369 false_high_cond = kCondLT;
1370 break;
1371 case kCondGE:
1372 true_high_cond = kCondGT;
1373 break;
Aart Bike9f37602015-10-09 11:15:55 -07001374 case kCondB:
1375 false_high_cond = kCondA;
1376 break;
1377 case kCondBE:
1378 true_high_cond = kCondB;
1379 break;
1380 case kCondA:
1381 false_high_cond = kCondB;
1382 break;
1383 case kCondAE:
1384 true_high_cond = kCondA;
1385 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001386 }
1387 if (right.IsConstant()) {
1388 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1389 int32_t val_low = Low32Bits(value);
1390 int32_t val_high = High32Bits(value);
1391
Vladimir Markoac6ac102015-12-17 12:14:00 +00001392 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001393 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001394 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001395 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001396 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001398 __ b(true_label, ARMCondition(true_high_cond));
1399 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001400 }
1401 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001402 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001403 } else {
1404 Register right_high = right.AsRegisterPairHigh<Register>();
1405 Register right_low = right.AsRegisterPairLow<Register>();
1406
1407 __ cmp(left_high, ShifterOperand(right_high));
1408 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001409 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001410 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001411 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001413 __ b(true_label, ARMCondition(true_high_cond));
1414 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001415 }
1416 // Must be equal high, so compare the lows.
1417 __ cmp(left_low, ShifterOperand(right_low));
1418 }
1419 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001420 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001421 __ b(true_label, final_condition);
1422}
1423
David Brazdil0debae72015-11-12 18:37:00 +00001424void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1425 Label* true_target_in,
1426 Label* false_target_in) {
1427 // Generated branching requires both targets to be explicit. If either of the
1428 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1429 Label fallthrough_target;
1430 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1431 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1432
Roland Levillain4fa13f62015-07-06 18:11:54 +01001433 Primitive::Type type = condition->InputAt(0)->GetType();
1434 switch (type) {
1435 case Primitive::kPrimLong:
1436 GenerateLongComparesAndJumps(condition, true_target, false_target);
1437 break;
1438 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001439 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001440 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001441 GenerateFPJumps(condition, true_target, false_target);
1442 break;
1443 default:
1444 LOG(FATAL) << "Unexpected compare type " << type;
1445 }
1446
David Brazdil0debae72015-11-12 18:37:00 +00001447 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001448 __ b(false_target);
1449 }
David Brazdil0debae72015-11-12 18:37:00 +00001450
1451 if (fallthrough_target.IsLinked()) {
1452 __ Bind(&fallthrough_target);
1453 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001454}
1455
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001456void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001457 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001458 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001459 Label* false_target) {
1460 HInstruction* cond = instruction->InputAt(condition_input_index);
1461
1462 if (true_target == nullptr && false_target == nullptr) {
1463 // Nothing to do. The code always falls through.
1464 return;
1465 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001466 // Constant condition, statically compared against "true" (integer value 1).
1467 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001468 if (true_target != nullptr) {
1469 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001470 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001471 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001472 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001473 if (false_target != nullptr) {
1474 __ b(false_target);
1475 }
1476 }
1477 return;
1478 }
1479
1480 // The following code generates these patterns:
1481 // (1) true_target == nullptr && false_target != nullptr
1482 // - opposite condition true => branch to false_target
1483 // (2) true_target != nullptr && false_target == nullptr
1484 // - condition true => branch to true_target
1485 // (3) true_target != nullptr && false_target != nullptr
1486 // - condition true => branch to true_target
1487 // - branch to false_target
1488 if (IsBooleanValueOrMaterializedCondition(cond)) {
1489 // Condition has been materialized, compare the output to 0.
1490 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1491 DCHECK(cond_val.IsRegister());
1492 if (true_target == nullptr) {
1493 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1494 } else {
1495 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001496 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001497 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001498 // Condition has not been materialized. Use its inputs as the comparison and
1499 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001500 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001501
1502 // If this is a long or FP comparison that has been folded into
1503 // the HCondition, generate the comparison directly.
1504 Primitive::Type type = condition->InputAt(0)->GetType();
1505 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1506 GenerateCompareTestAndBranch(condition, true_target, false_target);
1507 return;
1508 }
1509
1510 LocationSummary* locations = cond->GetLocations();
1511 DCHECK(locations->InAt(0).IsRegister());
1512 Register left = locations->InAt(0).AsRegister<Register>();
1513 Location right = locations->InAt(1);
1514 if (right.IsRegister()) {
1515 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001516 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001517 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001518 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001519 }
1520 if (true_target == nullptr) {
1521 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1522 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001523 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001524 }
Dave Allison20dfc792014-06-16 20:44:29 -07001525 }
David Brazdil0debae72015-11-12 18:37:00 +00001526
1527 // If neither branch falls through (case 3), the conditional branch to `true_target`
1528 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1529 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001530 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001531 }
1532}
1533
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001534void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1536 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001537 locations->SetInAt(0, Location::RequiresRegister());
1538 }
1539}
1540
1541void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001542 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1543 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1544 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1545 nullptr : codegen_->GetLabelOf(true_successor);
1546 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1547 nullptr : codegen_->GetLabelOf(false_successor);
1548 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001549}
1550
1551void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1552 LocationSummary* locations = new (GetGraph()->GetArena())
1553 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001554 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001555 locations->SetInAt(0, Location::RequiresRegister());
1556 }
1557}
1558
1559void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001560 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001561 GenerateTestAndBranch(deoptimize,
1562 /* condition_input_index */ 0,
1563 slow_path->GetEntryLabel(),
1564 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001565}
Dave Allison20dfc792014-06-16 20:44:29 -07001566
David Brazdil74eb1b22015-12-14 11:44:01 +00001567void LocationsBuilderARM::VisitSelect(HSelect* select) {
1568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1569 if (Primitive::IsFloatingPointType(select->GetType())) {
1570 locations->SetInAt(0, Location::RequiresFpuRegister());
1571 locations->SetInAt(1, Location::RequiresFpuRegister());
1572 } else {
1573 locations->SetInAt(0, Location::RequiresRegister());
1574 locations->SetInAt(1, Location::RequiresRegister());
1575 }
1576 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1577 locations->SetInAt(2, Location::RequiresRegister());
1578 }
1579 locations->SetOut(Location::SameAsFirstInput());
1580}
1581
1582void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1583 LocationSummary* locations = select->GetLocations();
1584 Label false_target;
1585 GenerateTestAndBranch(select,
1586 /* condition_input_index */ 2,
1587 /* true_target */ nullptr,
1588 &false_target);
1589 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1590 __ Bind(&false_target);
1591}
1592
David Srbecky0cf44932015-12-09 14:09:59 +00001593void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1594 new (GetGraph()->GetArena()) LocationSummary(info);
1595}
1596
David Srbeckyd28f4a02016-03-14 17:14:24 +00001597void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1598 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001599}
1600
1601void CodeGeneratorARM::GenerateNop() {
1602 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001603}
1604
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001605void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001606 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001607 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001608 // Handle the long/FP comparisons made in instruction simplification.
1609 switch (cond->InputAt(0)->GetType()) {
1610 case Primitive::kPrimLong:
1611 locations->SetInAt(0, Location::RequiresRegister());
1612 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001613 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001614 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1615 }
1616 break;
1617
1618 case Primitive::kPrimFloat:
1619 case Primitive::kPrimDouble:
1620 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001621 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001622 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001623 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1624 }
1625 break;
1626
1627 default:
1628 locations->SetInAt(0, Location::RequiresRegister());
1629 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001630 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001631 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1632 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001633 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001634}
1635
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001636void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001637 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001638 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001639 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001640
1641 LocationSummary* locations = cond->GetLocations();
1642 Location left = locations->InAt(0);
1643 Location right = locations->InAt(1);
1644 Register out = locations->Out().AsRegister<Register>();
1645 Label true_label, false_label;
1646
1647 switch (cond->InputAt(0)->GetType()) {
1648 default: {
1649 // Integer case.
1650 if (right.IsRegister()) {
1651 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1652 } else {
1653 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001654 __ CmpConstant(left.AsRegister<Register>(),
1655 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001656 }
Aart Bike9f37602015-10-09 11:15:55 -07001657 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001658 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001659 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001660 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001661 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001662 return;
1663 }
1664 case Primitive::kPrimLong:
1665 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1666 break;
1667 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001668 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001669 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001670 GenerateFPJumps(cond, &true_label, &false_label);
1671 break;
1672 }
1673
1674 // Convert the jumps into the result.
1675 Label done_label;
1676
1677 // False case: result = 0.
1678 __ Bind(&false_label);
1679 __ LoadImmediate(out, 0);
1680 __ b(&done_label);
1681
1682 // True case: result = 1.
1683 __ Bind(&true_label);
1684 __ LoadImmediate(out, 1);
1685 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001686}
1687
1688void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001689 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001690}
1691
1692void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001693 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001694}
1695
1696void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001697 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001698}
1699
1700void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001701 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001702}
1703
1704void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001705 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001706}
1707
1708void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001709 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001710}
1711
1712void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001713 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001714}
1715
1716void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001717 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001718}
1719
1720void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001721 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001722}
1723
1724void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001725 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001726}
1727
1728void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001734}
1735
Aart Bike9f37602015-10-09 11:15:55 -07001736void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001738}
1739
1740void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001742}
1743
1744void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001746}
1747
1748void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001750}
1751
1752void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001754}
1755
1756void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001758}
1759
1760void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001762}
1763
1764void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001766}
1767
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001768void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001769 LocationSummary* locations =
1770 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001771 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001772}
1773
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001774void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001775 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001776}
1777
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001778void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1779 LocationSummary* locations =
1780 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1781 locations->SetOut(Location::ConstantLocation(constant));
1782}
1783
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001784void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001785 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001786}
1787
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001788void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001789 LocationSummary* locations =
1790 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001791 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001792}
1793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001794void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001795 // Will be generated at use site.
1796}
1797
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001798void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1799 LocationSummary* locations =
1800 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1801 locations->SetOut(Location::ConstantLocation(constant));
1802}
1803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001804void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001805 // Will be generated at use site.
1806}
1807
1808void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1809 LocationSummary* locations =
1810 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1811 locations->SetOut(Location::ConstantLocation(constant));
1812}
1813
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001814void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001815 // Will be generated at use site.
1816}
1817
Calin Juravle27df7582015-04-17 19:12:31 +01001818void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1819 memory_barrier->SetLocations(nullptr);
1820}
1821
1822void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001823 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001824}
1825
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001826void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001827 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828}
1829
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001830void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001832}
1833
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001837 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001838}
1839
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001840void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001841 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001842}
1843
Calin Juravle175dc732015-08-25 15:42:32 +01001844void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1845 // The trampoline uses the same calling convention as dex calling conventions,
1846 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1847 // the method_idx.
1848 HandleInvoke(invoke);
1849}
1850
1851void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1852 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1853}
1854
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001855void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001856 // Explicit clinit checks triggered by static invokes must have been pruned by
1857 // art::PrepareForRegisterAllocation.
1858 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001859
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001860 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001861 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001862 codegen_->GetInstructionSetFeatures());
1863 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001864 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1865 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1866 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001867 return;
1868 }
1869
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001870 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001871
1872 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1873 if (invoke->HasPcRelativeDexCache()) {
1874 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1875 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001876}
1877
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001878static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1879 if (invoke->GetLocations()->Intrinsified()) {
1880 IntrinsicCodeGeneratorARM intrinsic(codegen);
1881 intrinsic.Dispatch(invoke);
1882 return true;
1883 }
1884 return false;
1885}
1886
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001887void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001888 // Explicit clinit checks triggered by static invokes must have been pruned by
1889 // art::PrepareForRegisterAllocation.
1890 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001891
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001892 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1893 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001894 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001895
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001896 LocationSummary* locations = invoke->GetLocations();
1897 codegen_->GenerateStaticOrDirectCall(
1898 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001899 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001900}
1901
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001902void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001903 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001904 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001905}
1906
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001907void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001908 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001909 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001910 codegen_->GetInstructionSetFeatures());
1911 if (intrinsic.TryDispatch(invoke)) {
1912 return;
1913 }
1914
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001915 HandleInvoke(invoke);
1916}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001917
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001918void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001919 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1920 return;
1921 }
1922
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001923 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001924 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001925 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001926}
1927
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001928void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1929 HandleInvoke(invoke);
1930 // Add the hidden argument.
1931 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1932}
1933
1934void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1935 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001936 LocationSummary* locations = invoke->GetLocations();
1937 Register temp = locations->GetTemp(0).AsRegister<Register>();
1938 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001939 Location receiver = locations->InAt(0);
1940 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1941
Roland Levillain3b359c72015-11-17 19:35:12 +00001942 // Set the hidden argument. This is safe to do this here, as R12
1943 // won't be modified thereafter, before the `blx` (call) instruction.
1944 DCHECK_EQ(R12, hidden_reg);
1945 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001946
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001947 if (receiver.IsStackSlot()) {
1948 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001949 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001950 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1951 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001952 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001953 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001954 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001955 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001956 // Instead of simply (possibly) unpoisoning `temp` here, we should
1957 // emit a read barrier for the previous class reference load.
1958 // However this is not required in practice, as this is an
1959 // intermediate/temporary reference and because the current
1960 // concurrent copying collector keeps the from-space memory
1961 // intact/accessible until the end of the marking phase (the
1962 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001963 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001964 __ LoadFromOffset(kLoadWord, temp, temp,
1965 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1966 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00001967 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001968 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001969 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001970 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07001971 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001972 // LR = temp->GetEntryPoint();
1973 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1974 // LR();
1975 __ blx(LR);
1976 DCHECK(!codegen_->IsLeafMethod());
1977 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1978}
1979
Roland Levillain88cb1752014-10-20 16:36:47 +01001980void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1981 LocationSummary* locations =
1982 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1983 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001984 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001985 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001986 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1987 break;
1988 }
1989 case Primitive::kPrimLong: {
1990 locations->SetInAt(0, Location::RequiresRegister());
1991 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001992 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001993 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001994
Roland Levillain88cb1752014-10-20 16:36:47 +01001995 case Primitive::kPrimFloat:
1996 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001997 locations->SetInAt(0, Location::RequiresFpuRegister());
1998 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001999 break;
2000
2001 default:
2002 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2003 }
2004}
2005
2006void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2007 LocationSummary* locations = neg->GetLocations();
2008 Location out = locations->Out();
2009 Location in = locations->InAt(0);
2010 switch (neg->GetResultType()) {
2011 case Primitive::kPrimInt:
2012 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002013 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002014 break;
2015
2016 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002017 DCHECK(in.IsRegisterPair());
2018 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2019 __ rsbs(out.AsRegisterPairLow<Register>(),
2020 in.AsRegisterPairLow<Register>(),
2021 ShifterOperand(0));
2022 // We cannot emit an RSC (Reverse Subtract with Carry)
2023 // instruction here, as it does not exist in the Thumb-2
2024 // instruction set. We use the following approach
2025 // using SBC and SUB instead.
2026 //
2027 // out.hi = -C
2028 __ sbc(out.AsRegisterPairHigh<Register>(),
2029 out.AsRegisterPairHigh<Register>(),
2030 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2031 // out.hi = out.hi - in.hi
2032 __ sub(out.AsRegisterPairHigh<Register>(),
2033 out.AsRegisterPairHigh<Register>(),
2034 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2035 break;
2036
Roland Levillain88cb1752014-10-20 16:36:47 +01002037 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002038 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002039 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002040 break;
2041
Roland Levillain88cb1752014-10-20 16:36:47 +01002042 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002043 DCHECK(in.IsFpuRegisterPair());
2044 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2045 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002046 break;
2047
2048 default:
2049 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2050 }
2051}
2052
Roland Levillaindff1f282014-11-05 14:15:05 +00002053void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002054 Primitive::Type result_type = conversion->GetResultType();
2055 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002056 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002057
Roland Levillain5b3ee562015-04-14 16:02:41 +01002058 // The float-to-long, double-to-long and long-to-float type conversions
2059 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002060 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002061 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2062 && result_type == Primitive::kPrimLong)
2063 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002064 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002065 : LocationSummary::kNoCall;
2066 LocationSummary* locations =
2067 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2068
David Brazdilb2bd1c52015-03-25 11:17:37 +00002069 // The Java language does not allow treating boolean as an integral type but
2070 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002071
Roland Levillaindff1f282014-11-05 14:15:05 +00002072 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002073 case Primitive::kPrimByte:
2074 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002075 case Primitive::kPrimLong:
2076 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002077 case Primitive::kPrimBoolean:
2078 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002079 case Primitive::kPrimShort:
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002082 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002083 locations->SetInAt(0, Location::RequiresRegister());
2084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2085 break;
2086
2087 default:
2088 LOG(FATAL) << "Unexpected type conversion from " << input_type
2089 << " to " << result_type;
2090 }
2091 break;
2092
Roland Levillain01a8d712014-11-14 16:27:39 +00002093 case Primitive::kPrimShort:
2094 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002095 case Primitive::kPrimLong:
2096 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002097 case Primitive::kPrimBoolean:
2098 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002099 case Primitive::kPrimByte:
2100 case Primitive::kPrimInt:
2101 case Primitive::kPrimChar:
2102 // Processing a Dex `int-to-short' instruction.
2103 locations->SetInAt(0, Location::RequiresRegister());
2104 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2105 break;
2106
2107 default:
2108 LOG(FATAL) << "Unexpected type conversion from " << input_type
2109 << " to " << result_type;
2110 }
2111 break;
2112
Roland Levillain946e1432014-11-11 17:35:19 +00002113 case Primitive::kPrimInt:
2114 switch (input_type) {
2115 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002116 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002117 locations->SetInAt(0, Location::Any());
2118 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2119 break;
2120
2121 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002122 // Processing a Dex `float-to-int' instruction.
2123 locations->SetInAt(0, Location::RequiresFpuRegister());
2124 locations->SetOut(Location::RequiresRegister());
2125 locations->AddTemp(Location::RequiresFpuRegister());
2126 break;
2127
Roland Levillain946e1432014-11-11 17:35:19 +00002128 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002129 // Processing a Dex `double-to-int' instruction.
2130 locations->SetInAt(0, Location::RequiresFpuRegister());
2131 locations->SetOut(Location::RequiresRegister());
2132 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002133 break;
2134
2135 default:
2136 LOG(FATAL) << "Unexpected type conversion from " << input_type
2137 << " to " << result_type;
2138 }
2139 break;
2140
Roland Levillaindff1f282014-11-05 14:15:05 +00002141 case Primitive::kPrimLong:
2142 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002143 case Primitive::kPrimBoolean:
2144 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002145 case Primitive::kPrimByte:
2146 case Primitive::kPrimShort:
2147 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002148 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002149 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002150 locations->SetInAt(0, Location::RequiresRegister());
2151 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2152 break;
2153
Roland Levillain624279f2014-12-04 11:54:28 +00002154 case Primitive::kPrimFloat: {
2155 // Processing a Dex `float-to-long' instruction.
2156 InvokeRuntimeCallingConvention calling_convention;
2157 locations->SetInAt(0, Location::FpuRegisterLocation(
2158 calling_convention.GetFpuRegisterAt(0)));
2159 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2160 break;
2161 }
2162
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002163 case Primitive::kPrimDouble: {
2164 // Processing a Dex `double-to-long' instruction.
2165 InvokeRuntimeCallingConvention calling_convention;
2166 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2167 calling_convention.GetFpuRegisterAt(0),
2168 calling_convention.GetFpuRegisterAt(1)));
2169 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002170 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002171 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002172
2173 default:
2174 LOG(FATAL) << "Unexpected type conversion from " << input_type
2175 << " to " << result_type;
2176 }
2177 break;
2178
Roland Levillain981e4542014-11-14 11:47:14 +00002179 case Primitive::kPrimChar:
2180 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002181 case Primitive::kPrimLong:
2182 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002183 case Primitive::kPrimBoolean:
2184 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002185 case Primitive::kPrimByte:
2186 case Primitive::kPrimShort:
2187 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002188 // Processing a Dex `int-to-char' instruction.
2189 locations->SetInAt(0, Location::RequiresRegister());
2190 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2191 break;
2192
2193 default:
2194 LOG(FATAL) << "Unexpected type conversion from " << input_type
2195 << " to " << result_type;
2196 }
2197 break;
2198
Roland Levillaindff1f282014-11-05 14:15:05 +00002199 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002200 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002201 case Primitive::kPrimBoolean:
2202 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002203 case Primitive::kPrimByte:
2204 case Primitive::kPrimShort:
2205 case Primitive::kPrimInt:
2206 case Primitive::kPrimChar:
2207 // Processing a Dex `int-to-float' instruction.
2208 locations->SetInAt(0, Location::RequiresRegister());
2209 locations->SetOut(Location::RequiresFpuRegister());
2210 break;
2211
Roland Levillain5b3ee562015-04-14 16:02:41 +01002212 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002213 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002214 InvokeRuntimeCallingConvention calling_convention;
2215 locations->SetInAt(0, Location::RegisterPairLocation(
2216 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2217 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002218 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002219 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002220
Roland Levillaincff13742014-11-17 14:32:17 +00002221 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002222 // Processing a Dex `double-to-float' instruction.
2223 locations->SetInAt(0, Location::RequiresFpuRegister());
2224 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002225 break;
2226
2227 default:
2228 LOG(FATAL) << "Unexpected type conversion from " << input_type
2229 << " to " << result_type;
2230 };
2231 break;
2232
Roland Levillaindff1f282014-11-05 14:15:05 +00002233 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002234 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002235 case Primitive::kPrimBoolean:
2236 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002237 case Primitive::kPrimByte:
2238 case Primitive::kPrimShort:
2239 case Primitive::kPrimInt:
2240 case Primitive::kPrimChar:
2241 // Processing a Dex `int-to-double' instruction.
2242 locations->SetInAt(0, Location::RequiresRegister());
2243 locations->SetOut(Location::RequiresFpuRegister());
2244 break;
2245
2246 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002247 // Processing a Dex `long-to-double' instruction.
2248 locations->SetInAt(0, Location::RequiresRegister());
2249 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002250 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002251 locations->AddTemp(Location::RequiresFpuRegister());
2252 break;
2253
Roland Levillaincff13742014-11-17 14:32:17 +00002254 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002255 // Processing a Dex `float-to-double' instruction.
2256 locations->SetInAt(0, Location::RequiresFpuRegister());
2257 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002264 break;
2265
2266 default:
2267 LOG(FATAL) << "Unexpected type conversion from " << input_type
2268 << " to " << result_type;
2269 }
2270}
2271
2272void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2273 LocationSummary* locations = conversion->GetLocations();
2274 Location out = locations->Out();
2275 Location in = locations->InAt(0);
2276 Primitive::Type result_type = conversion->GetResultType();
2277 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002278 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002279 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002280 case Primitive::kPrimByte:
2281 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002282 case Primitive::kPrimLong:
2283 // Type conversion from long to byte is a result of code transformations.
2284 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2285 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002286 case Primitive::kPrimBoolean:
2287 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002288 case Primitive::kPrimShort:
2289 case Primitive::kPrimInt:
2290 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002291 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002292 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002293 break;
2294
2295 default:
2296 LOG(FATAL) << "Unexpected type conversion from " << input_type
2297 << " to " << result_type;
2298 }
2299 break;
2300
Roland Levillain01a8d712014-11-14 16:27:39 +00002301 case Primitive::kPrimShort:
2302 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002303 case Primitive::kPrimLong:
2304 // Type conversion from long to short is a result of code transformations.
2305 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2306 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002307 case Primitive::kPrimBoolean:
2308 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002309 case Primitive::kPrimByte:
2310 case Primitive::kPrimInt:
2311 case Primitive::kPrimChar:
2312 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002314 break;
2315
2316 default:
2317 LOG(FATAL) << "Unexpected type conversion from " << input_type
2318 << " to " << result_type;
2319 }
2320 break;
2321
Roland Levillain946e1432014-11-11 17:35:19 +00002322 case Primitive::kPrimInt:
2323 switch (input_type) {
2324 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002325 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002326 DCHECK(out.IsRegister());
2327 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002328 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002329 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002330 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002331 } else {
2332 DCHECK(in.IsConstant());
2333 DCHECK(in.GetConstant()->IsLongConstant());
2334 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002335 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002336 }
2337 break;
2338
Roland Levillain3f8f9362014-12-02 17:45:01 +00002339 case Primitive::kPrimFloat: {
2340 // Processing a Dex `float-to-int' instruction.
2341 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002342 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002343 __ vmovrs(out.AsRegister<Register>(), temp);
2344 break;
2345 }
2346
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002347 case Primitive::kPrimDouble: {
2348 // Processing a Dex `double-to-int' instruction.
2349 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002350 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002351 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002352 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002353 }
Roland Levillain946e1432014-11-11 17:35:19 +00002354
2355 default:
2356 LOG(FATAL) << "Unexpected type conversion from " << input_type
2357 << " to " << result_type;
2358 }
2359 break;
2360
Roland Levillaindff1f282014-11-05 14:15:05 +00002361 case Primitive::kPrimLong:
2362 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002363 case Primitive::kPrimBoolean:
2364 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002365 case Primitive::kPrimByte:
2366 case Primitive::kPrimShort:
2367 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002368 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002369 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002370 DCHECK(out.IsRegisterPair());
2371 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002372 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002373 // Sign extension.
2374 __ Asr(out.AsRegisterPairHigh<Register>(),
2375 out.AsRegisterPairLow<Register>(),
2376 31);
2377 break;
2378
2379 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002380 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002381 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2382 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002383 conversion->GetDexPc(),
2384 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002385 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002386 break;
2387
Roland Levillaindff1f282014-11-05 14:15:05 +00002388 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002389 // Processing a Dex `double-to-long' instruction.
2390 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2391 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002392 conversion->GetDexPc(),
2393 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002394 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002395 break;
2396
2397 default:
2398 LOG(FATAL) << "Unexpected type conversion from " << input_type
2399 << " to " << result_type;
2400 }
2401 break;
2402
Roland Levillain981e4542014-11-14 11:47:14 +00002403 case Primitive::kPrimChar:
2404 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002405 case Primitive::kPrimLong:
2406 // Type conversion from long to char is a result of code transformations.
2407 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2408 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002409 case Primitive::kPrimBoolean:
2410 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002411 case Primitive::kPrimByte:
2412 case Primitive::kPrimShort:
2413 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002414 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002415 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002416 break;
2417
2418 default:
2419 LOG(FATAL) << "Unexpected type conversion from " << input_type
2420 << " to " << result_type;
2421 }
2422 break;
2423
Roland Levillaindff1f282014-11-05 14:15:05 +00002424 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002425 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002426 case Primitive::kPrimBoolean:
2427 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002428 case Primitive::kPrimByte:
2429 case Primitive::kPrimShort:
2430 case Primitive::kPrimInt:
2431 case Primitive::kPrimChar: {
2432 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002433 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2434 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002435 break;
2436 }
2437
Roland Levillain5b3ee562015-04-14 16:02:41 +01002438 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002439 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002440 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2441 conversion,
2442 conversion->GetDexPc(),
2443 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002444 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002445 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002446
Roland Levillaincff13742014-11-17 14:32:17 +00002447 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002448 // Processing a Dex `double-to-float' instruction.
2449 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2450 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 };
2457 break;
2458
Roland Levillaindff1f282014-11-05 14:15:05 +00002459 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002460 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002461 case Primitive::kPrimBoolean:
2462 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002463 case Primitive::kPrimByte:
2464 case Primitive::kPrimShort:
2465 case Primitive::kPrimInt:
2466 case Primitive::kPrimChar: {
2467 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002469 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2470 out.AsFpuRegisterPairLow<SRegister>());
2471 break;
2472 }
2473
Roland Levillain647b9ed2014-11-27 12:06:00 +00002474 case Primitive::kPrimLong: {
2475 // Processing a Dex `long-to-double' instruction.
2476 Register low = in.AsRegisterPairLow<Register>();
2477 Register high = in.AsRegisterPairHigh<Register>();
2478 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2479 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002480 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002481 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002482 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2483 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002484
Roland Levillain682393c2015-04-14 15:57:52 +01002485 // temp_d = int-to-double(high)
2486 __ vmovsr(temp_s, high);
2487 __ vcvtdi(temp_d, temp_s);
2488 // constant_d = k2Pow32EncodingForDouble
2489 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2490 // out_d = unsigned-to-double(low)
2491 __ vmovsr(out_s, low);
2492 __ vcvtdu(out_d, out_s);
2493 // out_d += temp_d * constant_d
2494 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002495 break;
2496 }
2497
Roland Levillaincff13742014-11-17 14:32:17 +00002498 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002499 // Processing a Dex `float-to-double' instruction.
2500 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2501 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002502 break;
2503
2504 default:
2505 LOG(FATAL) << "Unexpected type conversion from " << input_type
2506 << " to " << result_type;
2507 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002508 break;
2509
2510 default:
2511 LOG(FATAL) << "Unexpected type conversion from " << input_type
2512 << " to " << result_type;
2513 }
2514}
2515
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002516void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002517 LocationSummary* locations =
2518 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002519 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002520 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002521 locations->SetInAt(0, Location::RequiresRegister());
2522 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2524 break;
2525 }
2526
2527 case Primitive::kPrimLong: {
2528 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002529 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002530 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002531 break;
2532 }
2533
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002534 case Primitive::kPrimFloat:
2535 case Primitive::kPrimDouble: {
2536 locations->SetInAt(0, Location::RequiresFpuRegister());
2537 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002538 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002539 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002540 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002541
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002542 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002543 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002544 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002545}
2546
2547void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2548 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002549 Location out = locations->Out();
2550 Location first = locations->InAt(0);
2551 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002552 switch (add->GetResultType()) {
2553 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002554 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002555 __ add(out.AsRegister<Register>(),
2556 first.AsRegister<Register>(),
2557 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002558 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002559 __ AddConstant(out.AsRegister<Register>(),
2560 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002561 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002562 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002563 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002564
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002565 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002566 if (second.IsConstant()) {
2567 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2568 GenerateAddLongConst(out, first, value);
2569 } else {
2570 DCHECK(second.IsRegisterPair());
2571 __ adds(out.AsRegisterPairLow<Register>(),
2572 first.AsRegisterPairLow<Register>(),
2573 ShifterOperand(second.AsRegisterPairLow<Register>()));
2574 __ adc(out.AsRegisterPairHigh<Register>(),
2575 first.AsRegisterPairHigh<Register>(),
2576 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2577 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002578 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002579 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002580
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002581 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002582 __ vadds(out.AsFpuRegister<SRegister>(),
2583 first.AsFpuRegister<SRegister>(),
2584 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002585 break;
2586
2587 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002588 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2589 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2590 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002591 break;
2592
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002593 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002594 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002595 }
2596}
2597
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002598void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002599 LocationSummary* locations =
2600 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002601 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002602 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002603 locations->SetInAt(0, Location::RequiresRegister());
2604 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2606 break;
2607 }
2608
2609 case Primitive::kPrimLong: {
2610 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002611 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002613 break;
2614 }
Calin Juravle11351682014-10-23 15:38:15 +01002615 case Primitive::kPrimFloat:
2616 case Primitive::kPrimDouble: {
2617 locations->SetInAt(0, Location::RequiresFpuRegister());
2618 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002619 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002620 break;
Calin Juravle11351682014-10-23 15:38:15 +01002621 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002622 default:
Calin Juravle11351682014-10-23 15:38:15 +01002623 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002624 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002625}
2626
2627void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2628 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002629 Location out = locations->Out();
2630 Location first = locations->InAt(0);
2631 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002632 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002633 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002634 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002635 __ sub(out.AsRegister<Register>(),
2636 first.AsRegister<Register>(),
2637 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002638 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002639 __ AddConstant(out.AsRegister<Register>(),
2640 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002641 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002642 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002643 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002644 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002645
Calin Juravle11351682014-10-23 15:38:15 +01002646 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002647 if (second.IsConstant()) {
2648 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2649 GenerateAddLongConst(out, first, -value);
2650 } else {
2651 DCHECK(second.IsRegisterPair());
2652 __ subs(out.AsRegisterPairLow<Register>(),
2653 first.AsRegisterPairLow<Register>(),
2654 ShifterOperand(second.AsRegisterPairLow<Register>()));
2655 __ sbc(out.AsRegisterPairHigh<Register>(),
2656 first.AsRegisterPairHigh<Register>(),
2657 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2658 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002659 break;
Calin Juravle11351682014-10-23 15:38:15 +01002660 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002661
Calin Juravle11351682014-10-23 15:38:15 +01002662 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002663 __ vsubs(out.AsFpuRegister<SRegister>(),
2664 first.AsFpuRegister<SRegister>(),
2665 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002666 break;
Calin Juravle11351682014-10-23 15:38:15 +01002667 }
2668
2669 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002670 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2671 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2672 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002673 break;
2674 }
2675
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002676
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002677 default:
Calin Juravle11351682014-10-23 15:38:15 +01002678 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002679 }
2680}
2681
Calin Juravle34bacdf2014-10-07 20:23:36 +01002682void LocationsBuilderARM::VisitMul(HMul* mul) {
2683 LocationSummary* locations =
2684 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2685 switch (mul->GetResultType()) {
2686 case Primitive::kPrimInt:
2687 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002688 locations->SetInAt(0, Location::RequiresRegister());
2689 locations->SetInAt(1, Location::RequiresRegister());
2690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002691 break;
2692 }
2693
Calin Juravleb5bfa962014-10-21 18:02:24 +01002694 case Primitive::kPrimFloat:
2695 case Primitive::kPrimDouble: {
2696 locations->SetInAt(0, Location::RequiresFpuRegister());
2697 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002698 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002699 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002700 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002701
2702 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002703 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002704 }
2705}
2706
2707void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2708 LocationSummary* locations = mul->GetLocations();
2709 Location out = locations->Out();
2710 Location first = locations->InAt(0);
2711 Location second = locations->InAt(1);
2712 switch (mul->GetResultType()) {
2713 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002714 __ mul(out.AsRegister<Register>(),
2715 first.AsRegister<Register>(),
2716 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002717 break;
2718 }
2719 case Primitive::kPrimLong: {
2720 Register out_hi = out.AsRegisterPairHigh<Register>();
2721 Register out_lo = out.AsRegisterPairLow<Register>();
2722 Register in1_hi = first.AsRegisterPairHigh<Register>();
2723 Register in1_lo = first.AsRegisterPairLow<Register>();
2724 Register in2_hi = second.AsRegisterPairHigh<Register>();
2725 Register in2_lo = second.AsRegisterPairLow<Register>();
2726
2727 // Extra checks to protect caused by the existence of R1_R2.
2728 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2729 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2730 DCHECK_NE(out_hi, in1_lo);
2731 DCHECK_NE(out_hi, in2_lo);
2732
2733 // input: in1 - 64 bits, in2 - 64 bits
2734 // output: out
2735 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2736 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2737 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2738
2739 // IP <- in1.lo * in2.hi
2740 __ mul(IP, in1_lo, in2_hi);
2741 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2742 __ mla(out_hi, in1_hi, in2_lo, IP);
2743 // out.lo <- (in1.lo * in2.lo)[31:0];
2744 __ umull(out_lo, IP, in1_lo, in2_lo);
2745 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2746 __ add(out_hi, out_hi, ShifterOperand(IP));
2747 break;
2748 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002749
2750 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002751 __ vmuls(out.AsFpuRegister<SRegister>(),
2752 first.AsFpuRegister<SRegister>(),
2753 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002754 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002755 }
2756
2757 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002758 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2759 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2760 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002761 break;
2762 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002763
2764 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002765 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002766 }
2767}
2768
Zheng Xuc6667102015-05-15 16:08:45 +08002769void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2770 DCHECK(instruction->IsDiv() || instruction->IsRem());
2771 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2772
2773 LocationSummary* locations = instruction->GetLocations();
2774 Location second = locations->InAt(1);
2775 DCHECK(second.IsConstant());
2776
2777 Register out = locations->Out().AsRegister<Register>();
2778 Register dividend = locations->InAt(0).AsRegister<Register>();
2779 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2780 DCHECK(imm == 1 || imm == -1);
2781
2782 if (instruction->IsRem()) {
2783 __ LoadImmediate(out, 0);
2784 } else {
2785 if (imm == 1) {
2786 __ Mov(out, dividend);
2787 } else {
2788 __ rsb(out, dividend, ShifterOperand(0));
2789 }
2790 }
2791}
2792
2793void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2794 DCHECK(instruction->IsDiv() || instruction->IsRem());
2795 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2796
2797 LocationSummary* locations = instruction->GetLocations();
2798 Location second = locations->InAt(1);
2799 DCHECK(second.IsConstant());
2800
2801 Register out = locations->Out().AsRegister<Register>();
2802 Register dividend = locations->InAt(0).AsRegister<Register>();
2803 Register temp = locations->GetTemp(0).AsRegister<Register>();
2804 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002805 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002806 int ctz_imm = CTZ(abs_imm);
2807
2808 if (ctz_imm == 1) {
2809 __ Lsr(temp, dividend, 32 - ctz_imm);
2810 } else {
2811 __ Asr(temp, dividend, 31);
2812 __ Lsr(temp, temp, 32 - ctz_imm);
2813 }
2814 __ add(out, temp, ShifterOperand(dividend));
2815
2816 if (instruction->IsDiv()) {
2817 __ Asr(out, out, ctz_imm);
2818 if (imm < 0) {
2819 __ rsb(out, out, ShifterOperand(0));
2820 }
2821 } else {
2822 __ ubfx(out, out, 0, ctz_imm);
2823 __ sub(out, out, ShifterOperand(temp));
2824 }
2825}
2826
2827void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2828 DCHECK(instruction->IsDiv() || instruction->IsRem());
2829 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2830
2831 LocationSummary* locations = instruction->GetLocations();
2832 Location second = locations->InAt(1);
2833 DCHECK(second.IsConstant());
2834
2835 Register out = locations->Out().AsRegister<Register>();
2836 Register dividend = locations->InAt(0).AsRegister<Register>();
2837 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2838 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2839 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2840
2841 int64_t magic;
2842 int shift;
2843 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2844
2845 __ LoadImmediate(temp1, magic);
2846 __ smull(temp2, temp1, dividend, temp1);
2847
2848 if (imm > 0 && magic < 0) {
2849 __ add(temp1, temp1, ShifterOperand(dividend));
2850 } else if (imm < 0 && magic > 0) {
2851 __ sub(temp1, temp1, ShifterOperand(dividend));
2852 }
2853
2854 if (shift != 0) {
2855 __ Asr(temp1, temp1, shift);
2856 }
2857
2858 if (instruction->IsDiv()) {
2859 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2860 } else {
2861 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2862 // TODO: Strength reduction for mls.
2863 __ LoadImmediate(temp2, imm);
2864 __ mls(out, temp1, temp2, dividend);
2865 }
2866}
2867
2868void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2869 DCHECK(instruction->IsDiv() || instruction->IsRem());
2870 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2871
2872 LocationSummary* locations = instruction->GetLocations();
2873 Location second = locations->InAt(1);
2874 DCHECK(second.IsConstant());
2875
2876 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2877 if (imm == 0) {
2878 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2879 } else if (imm == 1 || imm == -1) {
2880 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002881 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002882 DivRemByPowerOfTwo(instruction);
2883 } else {
2884 DCHECK(imm <= -2 || imm >= 2);
2885 GenerateDivRemWithAnyConstant(instruction);
2886 }
2887}
2888
Calin Juravle7c4954d2014-10-28 16:57:40 +00002889void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002890 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2891 if (div->GetResultType() == Primitive::kPrimLong) {
2892 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002893 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002894 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2895 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002896 } else if (div->GetResultType() == Primitive::kPrimInt &&
2897 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2898 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002899 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002900 }
2901
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2903
Calin Juravle7c4954d2014-10-28 16:57:40 +00002904 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002905 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002906 if (div->InputAt(1)->IsConstant()) {
2907 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002908 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002909 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002910 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2911 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002912 // No temp register required.
2913 } else {
2914 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002915 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002916 locations->AddTemp(Location::RequiresRegister());
2917 }
2918 }
2919 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002920 locations->SetInAt(0, Location::RequiresRegister());
2921 locations->SetInAt(1, Location::RequiresRegister());
2922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2923 } else {
2924 InvokeRuntimeCallingConvention calling_convention;
2925 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2926 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2927 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2928 // we only need the former.
2929 locations->SetOut(Location::RegisterLocation(R0));
2930 }
Calin Juravled0d48522014-11-04 16:40:20 +00002931 break;
2932 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002933 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002934 InvokeRuntimeCallingConvention calling_convention;
2935 locations->SetInAt(0, Location::RegisterPairLocation(
2936 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2937 locations->SetInAt(1, Location::RegisterPairLocation(
2938 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002939 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002940 break;
2941 }
2942 case Primitive::kPrimFloat:
2943 case Primitive::kPrimDouble: {
2944 locations->SetInAt(0, Location::RequiresFpuRegister());
2945 locations->SetInAt(1, Location::RequiresFpuRegister());
2946 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2947 break;
2948 }
2949
2950 default:
2951 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2952 }
2953}
2954
2955void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2956 LocationSummary* locations = div->GetLocations();
2957 Location out = locations->Out();
2958 Location first = locations->InAt(0);
2959 Location second = locations->InAt(1);
2960
2961 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002962 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002963 if (second.IsConstant()) {
2964 GenerateDivRemConstantIntegral(div);
2965 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002966 __ sdiv(out.AsRegister<Register>(),
2967 first.AsRegister<Register>(),
2968 second.AsRegister<Register>());
2969 } else {
2970 InvokeRuntimeCallingConvention calling_convention;
2971 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2972 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2973 DCHECK_EQ(R0, out.AsRegister<Register>());
2974
2975 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002976 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002977 }
Calin Juravled0d48522014-11-04 16:40:20 +00002978 break;
2979 }
2980
Calin Juravle7c4954d2014-10-28 16:57:40 +00002981 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002982 InvokeRuntimeCallingConvention calling_convention;
2983 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2984 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2985 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2986 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2987 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002988 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002989
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002990 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002991 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002992 break;
2993 }
2994
2995 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002996 __ vdivs(out.AsFpuRegister<SRegister>(),
2997 first.AsFpuRegister<SRegister>(),
2998 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002999 break;
3000 }
3001
3002 case Primitive::kPrimDouble: {
3003 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3004 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3005 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3006 break;
3007 }
3008
3009 default:
3010 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3011 }
3012}
3013
Calin Juravlebacfec32014-11-14 15:54:36 +00003014void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003015 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003016
3017 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003018 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003019 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3020 // sdiv will be replaced by other instruction sequence.
3021 call_kind = LocationSummary::kNoCall;
3022 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3023 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003024 // Have hardware divide instruction for int, do it with three instructions.
3025 call_kind = LocationSummary::kNoCall;
3026 }
3027
Calin Juravlebacfec32014-11-14 15:54:36 +00003028 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3029
Calin Juravled2ec87d2014-12-08 14:24:46 +00003030 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003031 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003032 if (rem->InputAt(1)->IsConstant()) {
3033 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003034 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003036 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3037 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003038 // No temp register required.
3039 } else {
3040 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003041 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003042 locations->AddTemp(Location::RequiresRegister());
3043 }
3044 }
3045 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003046 locations->SetInAt(0, Location::RequiresRegister());
3047 locations->SetInAt(1, Location::RequiresRegister());
3048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3049 locations->AddTemp(Location::RequiresRegister());
3050 } else {
3051 InvokeRuntimeCallingConvention calling_convention;
3052 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3053 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3054 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3055 // we only need the latter.
3056 locations->SetOut(Location::RegisterLocation(R1));
3057 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003058 break;
3059 }
3060 case Primitive::kPrimLong: {
3061 InvokeRuntimeCallingConvention calling_convention;
3062 locations->SetInAt(0, Location::RegisterPairLocation(
3063 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3064 locations->SetInAt(1, Location::RegisterPairLocation(
3065 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3066 // The runtime helper puts the output in R2,R3.
3067 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3068 break;
3069 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003070 case Primitive::kPrimFloat: {
3071 InvokeRuntimeCallingConvention calling_convention;
3072 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3073 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3074 locations->SetOut(Location::FpuRegisterLocation(S0));
3075 break;
3076 }
3077
Calin Juravlebacfec32014-11-14 15:54:36 +00003078 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003079 InvokeRuntimeCallingConvention calling_convention;
3080 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3081 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3082 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3083 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3084 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003085 break;
3086 }
3087
3088 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003089 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003090 }
3091}
3092
3093void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3094 LocationSummary* locations = rem->GetLocations();
3095 Location out = locations->Out();
3096 Location first = locations->InAt(0);
3097 Location second = locations->InAt(1);
3098
Calin Juravled2ec87d2014-12-08 14:24:46 +00003099 Primitive::Type type = rem->GetResultType();
3100 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003101 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003102 if (second.IsConstant()) {
3103 GenerateDivRemConstantIntegral(rem);
3104 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003105 Register reg1 = first.AsRegister<Register>();
3106 Register reg2 = second.AsRegister<Register>();
3107 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003108
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003109 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003110 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003111 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003112 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003113 } else {
3114 InvokeRuntimeCallingConvention calling_convention;
3115 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3116 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3117 DCHECK_EQ(R1, out.AsRegister<Register>());
3118
3119 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003120 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003121 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003122 break;
3123 }
3124
3125 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003126 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003127 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003128 break;
3129 }
3130
Calin Juravled2ec87d2014-12-08 14:24:46 +00003131 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003132 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003133 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003134 break;
3135 }
3136
Calin Juravlebacfec32014-11-14 15:54:36 +00003137 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003138 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003139 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003140 break;
3141 }
3142
3143 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003144 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003145 }
3146}
3147
Calin Juravled0d48522014-11-04 16:40:20 +00003148void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003149 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3150 ? LocationSummary::kCallOnSlowPath
3151 : LocationSummary::kNoCall;
3152 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003153 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003154 if (instruction->HasUses()) {
3155 locations->SetOut(Location::SameAsFirstInput());
3156 }
3157}
3158
3159void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003160 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003161 codegen_->AddSlowPath(slow_path);
3162
3163 LocationSummary* locations = instruction->GetLocations();
3164 Location value = locations->InAt(0);
3165
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003166 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003167 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003168 case Primitive::kPrimByte:
3169 case Primitive::kPrimChar:
3170 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003171 case Primitive::kPrimInt: {
3172 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003173 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003174 } else {
3175 DCHECK(value.IsConstant()) << value;
3176 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3177 __ b(slow_path->GetEntryLabel());
3178 }
3179 }
3180 break;
3181 }
3182 case Primitive::kPrimLong: {
3183 if (value.IsRegisterPair()) {
3184 __ orrs(IP,
3185 value.AsRegisterPairLow<Register>(),
3186 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3187 __ b(slow_path->GetEntryLabel(), EQ);
3188 } else {
3189 DCHECK(value.IsConstant()) << value;
3190 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3191 __ b(slow_path->GetEntryLabel());
3192 }
3193 }
3194 break;
3195 default:
3196 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3197 }
3198 }
Calin Juravled0d48522014-11-04 16:40:20 +00003199}
3200
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003201void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3202 Register in = locations->InAt(0).AsRegister<Register>();
3203 Location rhs = locations->InAt(1);
3204 Register out = locations->Out().AsRegister<Register>();
3205
3206 if (rhs.IsConstant()) {
3207 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3208 // so map all rotations to a +ve. equivalent in that range.
3209 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3210 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3211 if (rot) {
3212 // Rotate, mapping left rotations to right equivalents if necessary.
3213 // (e.g. left by 2 bits == right by 30.)
3214 __ Ror(out, in, rot);
3215 } else if (out != in) {
3216 __ Mov(out, in);
3217 }
3218 } else {
3219 __ Ror(out, in, rhs.AsRegister<Register>());
3220 }
3221}
3222
3223// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3224// rotates by swapping input regs (effectively rotating by the first 32-bits of
3225// a larger rotation) or flipping direction (thus treating larger right/left
3226// rotations as sub-word sized rotations in the other direction) as appropriate.
3227void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3228 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3229 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3230 Location rhs = locations->InAt(1);
3231 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3232 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3233
3234 if (rhs.IsConstant()) {
3235 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3236 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003237 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003238 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3239 // logic below to a simple pair of binary orr.
3240 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3241 if (rot >= kArmBitsPerWord) {
3242 rot -= kArmBitsPerWord;
3243 std::swap(in_reg_hi, in_reg_lo);
3244 }
3245 // Rotate, or mov to out for zero or word size rotations.
3246 if (rot != 0u) {
3247 __ Lsr(out_reg_hi, in_reg_hi, rot);
3248 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3249 __ Lsr(out_reg_lo, in_reg_lo, rot);
3250 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3251 } else {
3252 __ Mov(out_reg_lo, in_reg_lo);
3253 __ Mov(out_reg_hi, in_reg_hi);
3254 }
3255 } else {
3256 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3257 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3258 Label end;
3259 Label shift_by_32_plus_shift_right;
3260
3261 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3262 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3263 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3264 __ b(&shift_by_32_plus_shift_right, CC);
3265
3266 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3267 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3268 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3269 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3270 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3271 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3272 __ Lsr(shift_left, in_reg_hi, shift_right);
3273 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3274 __ b(&end);
3275
3276 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3277 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3278 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3279 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3280 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3281 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3282 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3283 __ Lsl(shift_right, in_reg_hi, shift_left);
3284 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3285
3286 __ Bind(&end);
3287 }
3288}
Roland Levillain22c49222016-03-18 14:04:28 +00003289
3290void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003291 LocationSummary* locations =
3292 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3293 switch (ror->GetResultType()) {
3294 case Primitive::kPrimInt: {
3295 locations->SetInAt(0, Location::RequiresRegister());
3296 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3297 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3298 break;
3299 }
3300 case Primitive::kPrimLong: {
3301 locations->SetInAt(0, Location::RequiresRegister());
3302 if (ror->InputAt(1)->IsConstant()) {
3303 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3304 } else {
3305 locations->SetInAt(1, Location::RequiresRegister());
3306 locations->AddTemp(Location::RequiresRegister());
3307 locations->AddTemp(Location::RequiresRegister());
3308 }
3309 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3310 break;
3311 }
3312 default:
3313 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3314 }
3315}
3316
Roland Levillain22c49222016-03-18 14:04:28 +00003317void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003318 LocationSummary* locations = ror->GetLocations();
3319 Primitive::Type type = ror->GetResultType();
3320 switch (type) {
3321 case Primitive::kPrimInt: {
3322 HandleIntegerRotate(locations);
3323 break;
3324 }
3325 case Primitive::kPrimLong: {
3326 HandleLongRotate(locations);
3327 break;
3328 }
3329 default:
3330 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003331 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003332 }
3333}
3334
Calin Juravle9aec02f2014-11-18 23:06:35 +00003335void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3336 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3337
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003338 LocationSummary* locations =
3339 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003340
3341 switch (op->GetResultType()) {
3342 case Primitive::kPrimInt: {
3343 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003344 if (op->InputAt(1)->IsConstant()) {
3345 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3347 } else {
3348 locations->SetInAt(1, Location::RequiresRegister());
3349 // Make the output overlap, as it will be used to hold the masked
3350 // second input.
3351 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3352 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003353 break;
3354 }
3355 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003356 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003357 if (op->InputAt(1)->IsConstant()) {
3358 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3359 // For simplicity, use kOutputOverlap even though we only require that low registers
3360 // don't clash with high registers which the register allocator currently guarantees.
3361 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3362 } else {
3363 locations->SetInAt(1, Location::RequiresRegister());
3364 locations->AddTemp(Location::RequiresRegister());
3365 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3366 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003367 break;
3368 }
3369 default:
3370 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3371 }
3372}
3373
3374void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3375 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3376
3377 LocationSummary* locations = op->GetLocations();
3378 Location out = locations->Out();
3379 Location first = locations->InAt(0);
3380 Location second = locations->InAt(1);
3381
3382 Primitive::Type type = op->GetResultType();
3383 switch (type) {
3384 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003385 Register out_reg = out.AsRegister<Register>();
3386 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003387 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003388 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003389 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003390 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003391 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003392 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003393 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003394 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003395 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003396 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003397 }
3398 } else {
3399 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003400 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003401 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003402 __ Mov(out_reg, first_reg);
3403 } else if (op->IsShl()) {
3404 __ Lsl(out_reg, first_reg, shift_value);
3405 } else if (op->IsShr()) {
3406 __ Asr(out_reg, first_reg, shift_value);
3407 } else {
3408 __ Lsr(out_reg, first_reg, shift_value);
3409 }
3410 }
3411 break;
3412 }
3413 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003414 Register o_h = out.AsRegisterPairHigh<Register>();
3415 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003416
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003417 Register high = first.AsRegisterPairHigh<Register>();
3418 Register low = first.AsRegisterPairLow<Register>();
3419
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003420 if (second.IsRegister()) {
3421 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003422
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003423 Register second_reg = second.AsRegister<Register>();
3424
3425 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003426 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003427 // Shift the high part
3428 __ Lsl(o_h, high, o_l);
3429 // Shift the low part and `or` what overflew on the high part
3430 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3431 __ Lsr(temp, low, temp);
3432 __ orr(o_h, o_h, ShifterOperand(temp));
3433 // If the shift is > 32 bits, override the high part
3434 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3435 __ it(PL);
3436 __ Lsl(o_h, low, temp, PL);
3437 // Shift the low part
3438 __ Lsl(o_l, low, o_l);
3439 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003440 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003441 // Shift the low part
3442 __ Lsr(o_l, low, o_h);
3443 // Shift the high part and `or` what underflew on the low part
3444 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3445 __ Lsl(temp, high, temp);
3446 __ orr(o_l, o_l, ShifterOperand(temp));
3447 // If the shift is > 32 bits, override the low part
3448 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3449 __ it(PL);
3450 __ Asr(o_l, high, temp, PL);
3451 // Shift the high part
3452 __ Asr(o_h, high, o_h);
3453 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003454 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003455 // same as Shr except we use `Lsr`s and not `Asr`s
3456 __ Lsr(o_l, low, o_h);
3457 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3458 __ Lsl(temp, high, temp);
3459 __ orr(o_l, o_l, ShifterOperand(temp));
3460 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3461 __ it(PL);
3462 __ Lsr(o_l, high, temp, PL);
3463 __ Lsr(o_h, high, o_h);
3464 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003465 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003466 // Register allocator doesn't create partial overlap.
3467 DCHECK_NE(o_l, high);
3468 DCHECK_NE(o_h, low);
3469 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003470 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003471 if (shift_value > 32) {
3472 if (op->IsShl()) {
3473 __ Lsl(o_h, low, shift_value - 32);
3474 __ LoadImmediate(o_l, 0);
3475 } else if (op->IsShr()) {
3476 __ Asr(o_l, high, shift_value - 32);
3477 __ Asr(o_h, high, 31);
3478 } else {
3479 __ Lsr(o_l, high, shift_value - 32);
3480 __ LoadImmediate(o_h, 0);
3481 }
3482 } else if (shift_value == 32) {
3483 if (op->IsShl()) {
3484 __ mov(o_h, ShifterOperand(low));
3485 __ LoadImmediate(o_l, 0);
3486 } else if (op->IsShr()) {
3487 __ mov(o_l, ShifterOperand(high));
3488 __ Asr(o_h, high, 31);
3489 } else {
3490 __ mov(o_l, ShifterOperand(high));
3491 __ LoadImmediate(o_h, 0);
3492 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003493 } else if (shift_value == 1) {
3494 if (op->IsShl()) {
3495 __ Lsls(o_l, low, 1);
3496 __ adc(o_h, high, ShifterOperand(high));
3497 } else if (op->IsShr()) {
3498 __ Asrs(o_h, high, 1);
3499 __ Rrx(o_l, low);
3500 } else {
3501 __ Lsrs(o_h, high, 1);
3502 __ Rrx(o_l, low);
3503 }
3504 } else {
3505 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003506 if (op->IsShl()) {
3507 __ Lsl(o_h, high, shift_value);
3508 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3509 __ Lsl(o_l, low, shift_value);
3510 } else if (op->IsShr()) {
3511 __ Lsr(o_l, low, shift_value);
3512 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3513 __ Asr(o_h, high, shift_value);
3514 } else {
3515 __ Lsr(o_l, low, shift_value);
3516 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3517 __ Lsr(o_h, high, shift_value);
3518 }
3519 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003520 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003521 break;
3522 }
3523 default:
3524 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003525 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003526 }
3527}
3528
3529void LocationsBuilderARM::VisitShl(HShl* shl) {
3530 HandleShift(shl);
3531}
3532
3533void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3534 HandleShift(shl);
3535}
3536
3537void LocationsBuilderARM::VisitShr(HShr* shr) {
3538 HandleShift(shr);
3539}
3540
3541void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3542 HandleShift(shr);
3543}
3544
3545void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3546 HandleShift(ushr);
3547}
3548
3549void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3550 HandleShift(ushr);
3551}
3552
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003553void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003554 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003555 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003556 if (instruction->IsStringAlloc()) {
3557 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3558 } else {
3559 InvokeRuntimeCallingConvention calling_convention;
3560 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3561 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3562 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003563 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003564}
3565
3566void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003567 // Note: if heap poisoning is enabled, the entry point takes cares
3568 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003569 if (instruction->IsStringAlloc()) {
3570 // String is allocated through StringFactory. Call NewEmptyString entry point.
3571 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003572 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003573 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3574 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3575 __ blx(LR);
3576 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3577 } else {
3578 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3579 instruction,
3580 instruction->GetDexPc(),
3581 nullptr);
3582 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3583 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003584}
3585
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003586void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3587 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003589 InvokeRuntimeCallingConvention calling_convention;
3590 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003591 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003592 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003593 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003594}
3595
3596void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3597 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003598 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003599 // Note: if heap poisoning is enabled, the entry point takes cares
3600 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003601 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003602 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003603 instruction->GetDexPc(),
3604 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003605 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003606}
3607
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003608void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003609 LocationSummary* locations =
3610 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003611 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3612 if (location.IsStackSlot()) {
3613 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3614 } else if (location.IsDoubleStackSlot()) {
3615 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003616 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003617 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003618}
3619
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003620void InstructionCodeGeneratorARM::VisitParameterValue(
3621 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003622 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003623}
3624
3625void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3626 LocationSummary* locations =
3627 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3628 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3629}
3630
3631void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3632 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003633}
3634
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003635void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003636 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003637 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003638 locations->SetInAt(0, Location::RequiresRegister());
3639 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003640}
3641
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003642void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3643 LocationSummary* locations = not_->GetLocations();
3644 Location out = locations->Out();
3645 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003646 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003647 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003649 break;
3650
3651 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003652 __ mvn(out.AsRegisterPairLow<Register>(),
3653 ShifterOperand(in.AsRegisterPairLow<Register>()));
3654 __ mvn(out.AsRegisterPairHigh<Register>(),
3655 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003656 break;
3657
3658 default:
3659 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3660 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003661}
3662
David Brazdil66d126e2015-04-03 16:02:44 +01003663void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3664 LocationSummary* locations =
3665 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3666 locations->SetInAt(0, Location::RequiresRegister());
3667 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3668}
3669
3670void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003671 LocationSummary* locations = bool_not->GetLocations();
3672 Location out = locations->Out();
3673 Location in = locations->InAt(0);
3674 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3675}
3676
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003677void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003678 LocationSummary* locations =
3679 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003680 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003681 case Primitive::kPrimBoolean:
3682 case Primitive::kPrimByte:
3683 case Primitive::kPrimShort:
3684 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003685 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003686 case Primitive::kPrimLong: {
3687 locations->SetInAt(0, Location::RequiresRegister());
3688 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003689 // Output overlaps because it is written before doing the low comparison.
3690 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003691 break;
3692 }
3693 case Primitive::kPrimFloat:
3694 case Primitive::kPrimDouble: {
3695 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003696 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003697 locations->SetOut(Location::RequiresRegister());
3698 break;
3699 }
3700 default:
3701 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3702 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003703}
3704
3705void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003706 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003707 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003708 Location left = locations->InAt(0);
3709 Location right = locations->InAt(1);
3710
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003711 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003712 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003713 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003714 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003715 case Primitive::kPrimBoolean:
3716 case Primitive::kPrimByte:
3717 case Primitive::kPrimShort:
3718 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003719 case Primitive::kPrimInt: {
3720 __ LoadImmediate(out, 0);
3721 __ cmp(left.AsRegister<Register>(),
3722 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3723 less_cond = LT;
3724 break;
3725 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003726 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003727 __ cmp(left.AsRegisterPairHigh<Register>(),
3728 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003729 __ b(&less, LT);
3730 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003731 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003732 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003733 __ cmp(left.AsRegisterPairLow<Register>(),
3734 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003735 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003736 break;
3737 }
3738 case Primitive::kPrimFloat:
3739 case Primitive::kPrimDouble: {
3740 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003741 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003742 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003743 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003744 break;
3745 }
3746 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003747 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003748 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003749 }
Aart Bika19616e2016-02-01 18:57:58 -08003750
Calin Juravleddb7df22014-11-25 20:56:51 +00003751 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003752 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003753
3754 __ Bind(&greater);
3755 __ LoadImmediate(out, 1);
3756 __ b(&done);
3757
3758 __ Bind(&less);
3759 __ LoadImmediate(out, -1);
3760
3761 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003762}
3763
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003764void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003765 LocationSummary* locations =
3766 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003767 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003768 locations->SetInAt(i, Location::Any());
3769 }
3770 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003771}
3772
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003773void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003774 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003775}
3776
Roland Levillainc9285912015-12-18 10:38:42 +00003777void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3778 // TODO (ported from quick): revisit ARM barrier kinds.
3779 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003780 switch (kind) {
3781 case MemBarrierKind::kAnyStore:
3782 case MemBarrierKind::kLoadAny:
3783 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003784 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003785 break;
3786 }
3787 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003788 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003789 break;
3790 }
3791 default:
3792 LOG(FATAL) << "Unexpected memory barrier " << kind;
3793 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003794 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003795}
3796
3797void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3798 uint32_t offset,
3799 Register out_lo,
3800 Register out_hi) {
3801 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003802 // Ensure `out_lo` is different from `addr`, so that loading
3803 // `offset` into `out_lo` does not clutter `addr`.
3804 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003805 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003806 __ add(IP, addr, ShifterOperand(out_lo));
3807 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003808 }
3809 __ ldrexd(out_lo, out_hi, addr);
3810}
3811
3812void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3813 uint32_t offset,
3814 Register value_lo,
3815 Register value_hi,
3816 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003817 Register temp2,
3818 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003819 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003820 if (offset != 0) {
3821 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003822 __ add(IP, addr, ShifterOperand(temp1));
3823 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003824 }
3825 __ Bind(&fail);
3826 // We need a load followed by store. (The address used in a STREX instruction must
3827 // be the same as the address in the most recently executed LDREX instruction.)
3828 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003829 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003830 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003831 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003832}
3833
3834void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3835 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3836
Nicolas Geoffray39468442014-09-02 15:17:15 +01003837 LocationSummary* locations =
3838 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003839 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003840
Calin Juravle52c48962014-12-16 17:02:57 +00003841 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003842 if (Primitive::IsFloatingPointType(field_type)) {
3843 locations->SetInAt(1, Location::RequiresFpuRegister());
3844 } else {
3845 locations->SetInAt(1, Location::RequiresRegister());
3846 }
3847
Calin Juravle52c48962014-12-16 17:02:57 +00003848 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003849 bool generate_volatile = field_info.IsVolatile()
3850 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003851 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003852 bool needs_write_barrier =
3853 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003854 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003855 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003856 if (needs_write_barrier) {
3857 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003858 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003859 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003860 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003861 // - registers need to be consecutive
3862 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003863 // We don't test for ARM yet, and the assertion makes sure that we
3864 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003865 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3866
3867 locations->AddTemp(Location::RequiresRegister());
3868 locations->AddTemp(Location::RequiresRegister());
3869 if (field_type == Primitive::kPrimDouble) {
3870 // For doubles we need two more registers to copy the value.
3871 locations->AddTemp(Location::RegisterLocation(R2));
3872 locations->AddTemp(Location::RegisterLocation(R3));
3873 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003874 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003875}
3876
Calin Juravle52c48962014-12-16 17:02:57 +00003877void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003878 const FieldInfo& field_info,
3879 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003880 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3881
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003882 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003883 Register base = locations->InAt(0).AsRegister<Register>();
3884 Location value = locations->InAt(1);
3885
3886 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003887 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003888 Primitive::Type field_type = field_info.GetFieldType();
3889 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003890 bool needs_write_barrier =
3891 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003892
3893 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003894 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003895 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003896
3897 switch (field_type) {
3898 case Primitive::kPrimBoolean:
3899 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003900 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003901 break;
3902 }
3903
3904 case Primitive::kPrimShort:
3905 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003906 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003907 break;
3908 }
3909
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003910 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003911 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003912 if (kPoisonHeapReferences && needs_write_barrier) {
3913 // Note that in the case where `value` is a null reference,
3914 // we do not enter this block, as a null reference does not
3915 // need poisoning.
3916 DCHECK_EQ(field_type, Primitive::kPrimNot);
3917 Register temp = locations->GetTemp(0).AsRegister<Register>();
3918 __ Mov(temp, value.AsRegister<Register>());
3919 __ PoisonHeapReference(temp);
3920 __ StoreToOffset(kStoreWord, temp, base, offset);
3921 } else {
3922 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3923 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003924 break;
3925 }
3926
3927 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003928 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003929 GenerateWideAtomicStore(base, offset,
3930 value.AsRegisterPairLow<Register>(),
3931 value.AsRegisterPairHigh<Register>(),
3932 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003933 locations->GetTemp(1).AsRegister<Register>(),
3934 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003935 } else {
3936 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003937 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003938 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003939 break;
3940 }
3941
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003942 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003943 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003944 break;
3945 }
3946
3947 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003948 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003949 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003950 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3951 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3952
3953 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3954
3955 GenerateWideAtomicStore(base, offset,
3956 value_reg_lo,
3957 value_reg_hi,
3958 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003959 locations->GetTemp(3).AsRegister<Register>(),
3960 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003961 } else {
3962 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003963 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003964 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003965 break;
3966 }
3967
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003968 case Primitive::kPrimVoid:
3969 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003970 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971 }
Calin Juravle52c48962014-12-16 17:02:57 +00003972
Calin Juravle77520bc2015-01-12 18:45:46 +00003973 // Longs and doubles are handled in the switch.
3974 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3975 codegen_->MaybeRecordImplicitNullCheck(instruction);
3976 }
3977
3978 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3979 Register temp = locations->GetTemp(0).AsRegister<Register>();
3980 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003981 codegen_->MarkGCCard(
3982 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003983 }
3984
Calin Juravle52c48962014-12-16 17:02:57 +00003985 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003986 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003987 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003988}
3989
Calin Juravle52c48962014-12-16 17:02:57 +00003990void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3991 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003992
3993 bool object_field_get_with_read_barrier =
3994 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003995 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003996 new (GetGraph()->GetArena()) LocationSummary(instruction,
3997 object_field_get_with_read_barrier ?
3998 LocationSummary::kCallOnSlowPath :
3999 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004000 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004001
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004002 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004003 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004004 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004005 // The output overlaps in case of volatile long: we don't want the
4006 // code generated by GenerateWideAtomicLoad to overwrite the
4007 // object's location. Likewise, in the case of an object field get
4008 // with read barriers enabled, we do not want the load to overwrite
4009 // the object's location, as we need it to emit the read barrier.
4010 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4011 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004012
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004013 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4014 locations->SetOut(Location::RequiresFpuRegister());
4015 } else {
4016 locations->SetOut(Location::RequiresRegister(),
4017 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4018 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004019 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004020 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004021 // - registers need to be consecutive
4022 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004023 // We don't test for ARM yet, and the assertion makes sure that we
4024 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004025 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4026 locations->AddTemp(Location::RequiresRegister());
4027 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004028 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4029 // We need a temporary register for the read barrier marking slow
4030 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4031 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004032 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004033}
4034
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004035Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4036 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4037 << input->GetType();
4038 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4039 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4040 return Location::ConstantLocation(input->AsConstant());
4041 } else {
4042 return Location::RequiresFpuRegister();
4043 }
4044}
4045
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004046Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4047 Opcode opcode) {
4048 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4049 if (constant->IsConstant() &&
4050 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4051 return Location::ConstantLocation(constant->AsConstant());
4052 }
4053 return Location::RequiresRegister();
4054}
4055
4056bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4057 Opcode opcode) {
4058 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4059 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004060 Opcode high_opcode = opcode;
4061 SetCc low_set_cc = kCcDontCare;
4062 switch (opcode) {
4063 case SUB:
4064 // Flip the operation to an ADD.
4065 value = -value;
4066 opcode = ADD;
4067 FALLTHROUGH_INTENDED;
4068 case ADD:
4069 if (Low32Bits(value) == 0u) {
4070 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4071 }
4072 high_opcode = ADC;
4073 low_set_cc = kCcSet;
4074 break;
4075 default:
4076 break;
4077 }
4078 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4079 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004080 } else {
4081 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4082 }
4083}
4084
Vladimir Marko59751a72016-08-05 14:37:27 +01004085bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4086 Opcode opcode,
4087 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004088 ShifterOperand so;
4089 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004090 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004091 return true;
4092 }
4093 Opcode neg_opcode = kNoOperand;
4094 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004095 case AND: neg_opcode = BIC; value = ~value; break;
4096 case ORR: neg_opcode = ORN; value = ~value; break;
4097 case ADD: neg_opcode = SUB; value = -value; break;
4098 case ADC: neg_opcode = SBC; value = ~value; break;
4099 case SUB: neg_opcode = ADD; value = -value; break;
4100 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004101 default:
4102 return false;
4103 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004104 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004105}
4106
Calin Juravle52c48962014-12-16 17:02:57 +00004107void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4108 const FieldInfo& field_info) {
4109 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004110
Calin Juravle52c48962014-12-16 17:02:57 +00004111 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004112 Location base_loc = locations->InAt(0);
4113 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004114 Location out = locations->Out();
4115 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004116 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004117 Primitive::Type field_type = field_info.GetFieldType();
4118 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4119
4120 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004121 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004122 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004123 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004124
Roland Levillainc9285912015-12-18 10:38:42 +00004125 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004126 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004127 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004128
Roland Levillainc9285912015-12-18 10:38:42 +00004129 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004130 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004131 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004132
Roland Levillainc9285912015-12-18 10:38:42 +00004133 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004134 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004136
4137 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004138 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004139 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004140
4141 case Primitive::kPrimNot: {
4142 // /* HeapReference<Object> */ out = *(base + offset)
4143 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4144 Location temp_loc = locations->GetTemp(0);
4145 // Note that a potential implicit null check is handled in this
4146 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4147 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4148 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4149 if (is_volatile) {
4150 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4151 }
4152 } else {
4153 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4154 codegen_->MaybeRecordImplicitNullCheck(instruction);
4155 if (is_volatile) {
4156 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4157 }
4158 // If read barriers are enabled, emit read barriers other than
4159 // Baker's using a slow path (and also unpoison the loaded
4160 // reference, if heap poisoning is enabled).
4161 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4162 }
4163 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004164 }
4165
Roland Levillainc9285912015-12-18 10:38:42 +00004166 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004167 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004168 GenerateWideAtomicLoad(base, offset,
4169 out.AsRegisterPairLow<Register>(),
4170 out.AsRegisterPairHigh<Register>());
4171 } else {
4172 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4173 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004174 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004175
Roland Levillainc9285912015-12-18 10:38:42 +00004176 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004177 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004178 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004179
4180 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004181 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004182 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004183 Register lo = locations->GetTemp(0).AsRegister<Register>();
4184 Register hi = locations->GetTemp(1).AsRegister<Register>();
4185 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004186 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004187 __ vmovdrr(out_reg, lo, hi);
4188 } else {
4189 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004190 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004191 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004192 break;
4193 }
4194
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004195 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004196 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004197 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004198 }
Calin Juravle52c48962014-12-16 17:02:57 +00004199
Roland Levillainc9285912015-12-18 10:38:42 +00004200 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4201 // Potential implicit null checks, in the case of reference or
4202 // double fields, are handled in the previous switch statement.
4203 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004204 codegen_->MaybeRecordImplicitNullCheck(instruction);
4205 }
4206
Calin Juravle52c48962014-12-16 17:02:57 +00004207 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004208 if (field_type == Primitive::kPrimNot) {
4209 // Memory barriers, in the case of references, are also handled
4210 // in the previous switch statement.
4211 } else {
4212 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4213 }
Roland Levillain4d027112015-07-01 15:41:14 +01004214 }
Calin Juravle52c48962014-12-16 17:02:57 +00004215}
4216
4217void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4218 HandleFieldSet(instruction, instruction->GetFieldInfo());
4219}
4220
4221void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004222 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004223}
4224
4225void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4226 HandleFieldGet(instruction, instruction->GetFieldInfo());
4227}
4228
4229void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4230 HandleFieldGet(instruction, instruction->GetFieldInfo());
4231}
4232
4233void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4234 HandleFieldGet(instruction, instruction->GetFieldInfo());
4235}
4236
4237void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4238 HandleFieldGet(instruction, instruction->GetFieldInfo());
4239}
4240
4241void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4242 HandleFieldSet(instruction, instruction->GetFieldInfo());
4243}
4244
4245void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004246 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004247}
4248
Calin Juravlee460d1d2015-09-29 04:52:17 +01004249void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4250 HUnresolvedInstanceFieldGet* instruction) {
4251 FieldAccessCallingConventionARM calling_convention;
4252 codegen_->CreateUnresolvedFieldLocationSummary(
4253 instruction, instruction->GetFieldType(), calling_convention);
4254}
4255
4256void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4257 HUnresolvedInstanceFieldGet* instruction) {
4258 FieldAccessCallingConventionARM calling_convention;
4259 codegen_->GenerateUnresolvedFieldAccess(instruction,
4260 instruction->GetFieldType(),
4261 instruction->GetFieldIndex(),
4262 instruction->GetDexPc(),
4263 calling_convention);
4264}
4265
4266void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4267 HUnresolvedInstanceFieldSet* instruction) {
4268 FieldAccessCallingConventionARM calling_convention;
4269 codegen_->CreateUnresolvedFieldLocationSummary(
4270 instruction, instruction->GetFieldType(), calling_convention);
4271}
4272
4273void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4274 HUnresolvedInstanceFieldSet* instruction) {
4275 FieldAccessCallingConventionARM calling_convention;
4276 codegen_->GenerateUnresolvedFieldAccess(instruction,
4277 instruction->GetFieldType(),
4278 instruction->GetFieldIndex(),
4279 instruction->GetDexPc(),
4280 calling_convention);
4281}
4282
4283void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4284 HUnresolvedStaticFieldGet* instruction) {
4285 FieldAccessCallingConventionARM calling_convention;
4286 codegen_->CreateUnresolvedFieldLocationSummary(
4287 instruction, instruction->GetFieldType(), calling_convention);
4288}
4289
4290void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4291 HUnresolvedStaticFieldGet* instruction) {
4292 FieldAccessCallingConventionARM calling_convention;
4293 codegen_->GenerateUnresolvedFieldAccess(instruction,
4294 instruction->GetFieldType(),
4295 instruction->GetFieldIndex(),
4296 instruction->GetDexPc(),
4297 calling_convention);
4298}
4299
4300void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4301 HUnresolvedStaticFieldSet* instruction) {
4302 FieldAccessCallingConventionARM calling_convention;
4303 codegen_->CreateUnresolvedFieldLocationSummary(
4304 instruction, instruction->GetFieldType(), calling_convention);
4305}
4306
4307void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4308 HUnresolvedStaticFieldSet* instruction) {
4309 FieldAccessCallingConventionARM calling_convention;
4310 codegen_->GenerateUnresolvedFieldAccess(instruction,
4311 instruction->GetFieldType(),
4312 instruction->GetFieldIndex(),
4313 instruction->GetDexPc(),
4314 calling_convention);
4315}
4316
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004317void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004318 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4319 ? LocationSummary::kCallOnSlowPath
4320 : LocationSummary::kNoCall;
4321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004322 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004323 if (instruction->HasUses()) {
4324 locations->SetOut(Location::SameAsFirstInput());
4325 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004326}
4327
Calin Juravle2ae48182016-03-16 14:05:09 +00004328void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4329 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004330 return;
4331 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004332 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004333
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004334 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004335 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004336}
4337
Calin Juravle2ae48182016-03-16 14:05:09 +00004338void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004339 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004340 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341
4342 LocationSummary* locations = instruction->GetLocations();
4343 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004344
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004345 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004346}
4347
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004348void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004349 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004350}
4351
Artem Serov6c916792016-07-11 14:02:34 +01004352static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4353 switch (type) {
4354 case Primitive::kPrimNot:
4355 return kLoadWord;
4356 case Primitive::kPrimBoolean:
4357 return kLoadUnsignedByte;
4358 case Primitive::kPrimByte:
4359 return kLoadSignedByte;
4360 case Primitive::kPrimChar:
4361 return kLoadUnsignedHalfword;
4362 case Primitive::kPrimShort:
4363 return kLoadSignedHalfword;
4364 case Primitive::kPrimInt:
4365 return kLoadWord;
4366 case Primitive::kPrimLong:
4367 return kLoadWordPair;
4368 case Primitive::kPrimFloat:
4369 return kLoadSWord;
4370 case Primitive::kPrimDouble:
4371 return kLoadDWord;
4372 default:
4373 LOG(FATAL) << "Unreachable type " << type;
4374 UNREACHABLE();
4375 }
4376}
4377
4378static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4379 switch (type) {
4380 case Primitive::kPrimNot:
4381 return kStoreWord;
4382 case Primitive::kPrimBoolean:
4383 case Primitive::kPrimByte:
4384 return kStoreByte;
4385 case Primitive::kPrimChar:
4386 case Primitive::kPrimShort:
4387 return kStoreHalfword;
4388 case Primitive::kPrimInt:
4389 return kStoreWord;
4390 case Primitive::kPrimLong:
4391 return kStoreWordPair;
4392 case Primitive::kPrimFloat:
4393 return kStoreSWord;
4394 case Primitive::kPrimDouble:
4395 return kStoreDWord;
4396 default:
4397 LOG(FATAL) << "Unreachable type " << type;
4398 UNREACHABLE();
4399 }
4400}
4401
4402void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4403 Location out_loc,
4404 Register base,
4405 Register reg_offset,
4406 Condition cond) {
4407 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4408 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4409
4410 switch (type) {
4411 case Primitive::kPrimByte:
4412 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4413 break;
4414 case Primitive::kPrimBoolean:
4415 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4416 break;
4417 case Primitive::kPrimShort:
4418 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4419 break;
4420 case Primitive::kPrimChar:
4421 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4422 break;
4423 case Primitive::kPrimNot:
4424 case Primitive::kPrimInt:
4425 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4426 break;
4427 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4428 case Primitive::kPrimLong:
4429 case Primitive::kPrimFloat:
4430 case Primitive::kPrimDouble:
4431 default:
4432 LOG(FATAL) << "Unreachable type " << type;
4433 UNREACHABLE();
4434 }
4435}
4436
4437void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4438 Location loc,
4439 Register base,
4440 Register reg_offset,
4441 Condition cond) {
4442 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4443 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4444
4445 switch (type) {
4446 case Primitive::kPrimByte:
4447 case Primitive::kPrimBoolean:
4448 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4449 break;
4450 case Primitive::kPrimShort:
4451 case Primitive::kPrimChar:
4452 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4453 break;
4454 case Primitive::kPrimNot:
4455 case Primitive::kPrimInt:
4456 __ str(loc.AsRegister<Register>(), mem_address, cond);
4457 break;
4458 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4459 case Primitive::kPrimLong:
4460 case Primitive::kPrimFloat:
4461 case Primitive::kPrimDouble:
4462 default:
4463 LOG(FATAL) << "Unreachable type " << type;
4464 UNREACHABLE();
4465 }
4466}
4467
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004468void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004469 bool object_array_get_with_read_barrier =
4470 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004471 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004472 new (GetGraph()->GetArena()) LocationSummary(instruction,
4473 object_array_get_with_read_barrier ?
4474 LocationSummary::kCallOnSlowPath :
4475 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004476 locations->SetInAt(0, Location::RequiresRegister());
4477 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004478 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4479 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4480 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004481 // The output overlaps in the case of an object array get with
4482 // read barriers enabled: we do not want the move to overwrite the
4483 // array's location, as we need it to emit the read barrier.
4484 locations->SetOut(
4485 Location::RequiresRegister(),
4486 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004487 }
Roland Levillainc9285912015-12-18 10:38:42 +00004488 // We need a temporary register for the read barrier marking slow
4489 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4490 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4491 locations->AddTemp(Location::RequiresRegister());
4492 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004493}
4494
4495void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4496 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004497 Location obj_loc = locations->InAt(0);
4498 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004499 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004500 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004501 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004502 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004503 HInstruction* array_instr = instruction->GetArray();
4504 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004505 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4506 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004507
Roland Levillain4d027112015-07-01 15:41:14 +01004508 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004509 case Primitive::kPrimBoolean:
4510 case Primitive::kPrimByte:
4511 case Primitive::kPrimShort:
4512 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004513 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004514 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004515 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4516 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4517
4518 LoadOperandType load_type = GetLoadOperandType(type);
4519 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004520 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004521 Register temp = IP;
4522
4523 if (has_intermediate_address) {
4524 // We do not need to compute the intermediate address from the array: the
4525 // input instruction has done it already. See the comment in
4526 // `TryExtractArrayAccessAddress()`.
4527 if (kIsDebugBuild) {
4528 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4529 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4530 }
4531 temp = obj;
4532 } else {
4533 __ add(temp, obj, ShifterOperand(data_offset));
4534 }
4535 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004536 }
4537 break;
4538 }
4539
Roland Levillainc9285912015-12-18 10:38:42 +00004540 case Primitive::kPrimNot: {
4541 static_assert(
4542 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4543 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004544 // /* HeapReference<Object> */ out =
4545 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4546 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4547 Location temp = locations->GetTemp(0);
4548 // Note that a potential implicit null check is handled in this
4549 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4550 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4551 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4552 } else {
4553 Register out = out_loc.AsRegister<Register>();
4554 if (index.IsConstant()) {
4555 size_t offset =
4556 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4557 __ LoadFromOffset(kLoadWord, out, obj, offset);
4558 codegen_->MaybeRecordImplicitNullCheck(instruction);
4559 // If read barriers are enabled, emit read barriers other than
4560 // Baker's using a slow path (and also unpoison the loaded
4561 // reference, if heap poisoning is enabled).
4562 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4563 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004564 Register temp = IP;
4565
4566 if (has_intermediate_address) {
4567 // We do not need to compute the intermediate address from the array: the
4568 // input instruction has done it already. See the comment in
4569 // `TryExtractArrayAccessAddress()`.
4570 if (kIsDebugBuild) {
4571 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4572 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4573 }
4574 temp = obj;
4575 } else {
4576 __ add(temp, obj, ShifterOperand(data_offset));
4577 }
4578 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004579
Roland Levillainc9285912015-12-18 10:38:42 +00004580 codegen_->MaybeRecordImplicitNullCheck(instruction);
4581 // If read barriers are enabled, emit read barriers other than
4582 // Baker's using a slow path (and also unpoison the loaded
4583 // reference, if heap poisoning is enabled).
4584 codegen_->MaybeGenerateReadBarrierSlow(
4585 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4586 }
4587 }
4588 break;
4589 }
4590
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004593 size_t offset =
4594 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004595 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004596 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004597 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004598 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004599 }
4600 break;
4601 }
4602
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004603 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004604 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004605 if (index.IsConstant()) {
4606 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004607 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004608 } else {
4609 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004610 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004611 }
4612 break;
4613 }
4614
4615 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004616 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004617 if (index.IsConstant()) {
4618 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004619 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004620 } else {
4621 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004622 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004623 }
4624 break;
4625 }
4626
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004628 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004629 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004630 }
Roland Levillain4d027112015-07-01 15:41:14 +01004631
4632 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004633 // Potential implicit null checks, in the case of reference
4634 // arrays, are handled in the previous switch statement.
4635 } else {
4636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004637 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638}
4639
4640void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004641 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004642
4643 bool needs_write_barrier =
4644 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004645 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004646
Nicolas Geoffray39468442014-09-02 15:17:15 +01004647 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004648 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004649 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004650 LocationSummary::kCallOnSlowPath :
4651 LocationSummary::kNoCall);
4652
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004653 locations->SetInAt(0, Location::RequiresRegister());
4654 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4655 if (Primitive::IsFloatingPointType(value_type)) {
4656 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004657 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004658 locations->SetInAt(2, Location::RequiresRegister());
4659 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004660 if (needs_write_barrier) {
4661 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004662 // These registers may be used for Baker read barriers too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004663 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);
Roland Levillain16d9f942016-08-25 17:27:56 +01004747 Location temp1_loc = locations->GetTemp(0);
4748 Register temp1 = temp1_loc.AsRegister<Register>();
4749 Location temp2_loc = locations->GetTemp(1);
4750 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004751 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4752 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4753 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4754 Label done;
4755 SlowPathCode* slow_path = nullptr;
4756
Roland Levillain3b359c72015-11-17 19:35:12 +00004757 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004758 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4759 codegen_->AddSlowPath(slow_path);
4760 if (instruction->GetValueCanBeNull()) {
4761 Label non_zero;
4762 __ CompareAndBranchIfNonZero(value, &non_zero);
4763 if (index.IsConstant()) {
4764 size_t offset =
4765 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4766 __ StoreToOffset(kStoreWord, value, array, offset);
4767 } else {
4768 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004769 __ add(IP, array, ShifterOperand(data_offset));
4770 codegen_->StoreToShiftedRegOffset(value_type,
4771 value_loc,
4772 IP,
4773 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004774 }
4775 codegen_->MaybeRecordImplicitNullCheck(instruction);
4776 __ b(&done);
4777 __ Bind(&non_zero);
4778 }
4779
Roland Levillain3b359c72015-11-17 19:35:12 +00004780 if (kEmitCompilerReadBarrier) {
Roland Levillain16d9f942016-08-25 17:27:56 +01004781 if (!kUseBakerReadBarrier) {
4782 // When (non-Baker) read barriers are enabled, the type
4783 // checking instrumentation requires two read barriers
4784 // generated by CodeGeneratorARM::GenerateReadBarrierSlow:
4785 //
4786 // __ Mov(temp2, temp1);
4787 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4788 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4789 // codegen_->GenerateReadBarrierSlow(
4790 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4791 //
4792 // // /* HeapReference<Class> */ temp2 = value->klass_
4793 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4794 // codegen_->GenerateReadBarrierSlow(
4795 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4796 //
4797 // __ cmp(temp1, ShifterOperand(temp2));
4798 //
4799 // However, the second read barrier may trash `temp`, as it
4800 // is a temporary register, and as such would not be saved
4801 // along with live registers before calling the runtime (nor
4802 // restored afterwards). So in this case, we bail out and
4803 // delegate the work to the array set slow path.
4804 //
4805 // TODO: Extend the register allocator to support a new
4806 // "(locally) live temp" location so as to avoid always
4807 // going into the slow path when read barriers are enabled?
4808 //
4809 // There is no such problem with Baker read barriers (see below).
4810 __ b(slow_path->GetEntryLabel());
4811 } else {
4812 Register temp3 = IP;
4813 Location temp3_loc = Location::RegisterLocation(temp3);
4814
4815 // Note: `temp3` (scratch register IP) cannot be used as
4816 // `ref` argument of GenerateFieldLoadWithBakerReadBarrier
4817 // calls below (see ReadBarrierMarkSlowPathARM for more
4818 // details).
4819
4820 // /* HeapReference<Class> */ temp1 = array->klass_
4821 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4822 temp1_loc,
4823 array,
4824 class_offset,
4825 temp3_loc,
4826 /* needs_null_check */ true);
4827
4828 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4829 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4830 temp1_loc,
4831 temp1,
4832 component_offset,
4833 temp3_loc,
4834 /* needs_null_check */ false);
4835 // Register `temp1` is not trashed by the read barrier
4836 // emitted by GenerateFieldLoadWithBakerReadBarrier below,
4837 // as that method produces a call to a ReadBarrierMarkRegX
4838 // entry point, which saves all potentially live registers,
4839 // including temporaries such a `temp1`.
4840 // /* HeapReference<Class> */ temp2 = value->klass_
4841 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4842 temp2_loc,
4843 value,
4844 class_offset,
4845 temp3_loc,
4846 /* needs_null_check */ false);
4847 // If heap poisoning is enabled, `temp1` and `temp2` have
4848 // been unpoisoned by the the previous calls to
4849 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4850 __ cmp(temp1, ShifterOperand(temp2));
4851
4852 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4853 Label do_put;
4854 __ b(&do_put, EQ);
4855 // We do not need to emit a read barrier for the
4856 // following heap reference load, as `temp1` is only used
4857 // in a comparison with null below, and this reference
4858 // is not kept afterwards.
4859 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4860 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4861 // If heap poisoning is enabled, no need to unpoison
4862 // `temp`, as we are comparing against null below.
4863 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4864 __ Bind(&do_put);
4865 } else {
4866 __ b(slow_path->GetEntryLabel(), NE);
4867 }
4868 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004869 } else {
Roland Levillain16d9f942016-08-25 17:27:56 +01004870 // Non read barrier code.
4871
Roland Levillain3b359c72015-11-17 19:35:12 +00004872 // /* HeapReference<Class> */ temp1 = array->klass_
4873 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4874 codegen_->MaybeRecordImplicitNullCheck(instruction);
4875 __ MaybeUnpoisonHeapReference(temp1);
4876
4877 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4878 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4879 // /* HeapReference<Class> */ temp2 = value->klass_
4880 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4881 // If heap poisoning is enabled, no need to unpoison `temp1`
4882 // nor `temp2`, as we are comparing two poisoned references.
4883 __ cmp(temp1, ShifterOperand(temp2));
4884
4885 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4886 Label do_put;
4887 __ b(&do_put, EQ);
4888 // If heap poisoning is enabled, the `temp1` reference has
4889 // not been unpoisoned yet; unpoison it now.
4890 __ MaybeUnpoisonHeapReference(temp1);
4891
4892 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4893 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4894 // If heap poisoning is enabled, no need to unpoison
4895 // `temp1`, as we are comparing against null below.
4896 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4897 __ Bind(&do_put);
4898 } else {
4899 __ b(slow_path->GetEntryLabel(), NE);
4900 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004901 }
4902 }
4903
Artem Serov6c916792016-07-11 14:02:34 +01004904 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004905 if (kPoisonHeapReferences) {
4906 // Note that in the case where `value` is a null reference,
4907 // we do not enter this block, as a null reference does not
4908 // need poisoning.
4909 DCHECK_EQ(value_type, Primitive::kPrimNot);
4910 __ Mov(temp1, value);
4911 __ PoisonHeapReference(temp1);
4912 source = temp1;
4913 }
4914
4915 if (index.IsConstant()) {
4916 size_t offset =
4917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4918 __ StoreToOffset(kStoreWord, source, array, offset);
4919 } else {
4920 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004921
4922 __ add(IP, array, ShifterOperand(data_offset));
4923 codegen_->StoreToShiftedRegOffset(value_type,
4924 Location::RegisterLocation(source),
4925 IP,
4926 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004927 }
4928
Roland Levillain3b359c72015-11-17 19:35:12 +00004929 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004930 codegen_->MaybeRecordImplicitNullCheck(instruction);
4931 }
4932
4933 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4934
4935 if (done.IsLinked()) {
4936 __ Bind(&done);
4937 }
4938
4939 if (slow_path != nullptr) {
4940 __ Bind(slow_path->GetExitLabel());
4941 }
4942
4943 break;
4944 }
4945
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004947 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004948 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004949 size_t offset =
4950 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004951 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004952 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004953 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004954 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004955 }
4956 break;
4957 }
4958
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004959 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004960 Location value = locations->InAt(2);
4961 DCHECK(value.IsFpuRegister());
4962 if (index.IsConstant()) {
4963 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004964 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004965 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004966 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004967 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4968 }
4969 break;
4970 }
4971
4972 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004973 Location value = locations->InAt(2);
4974 DCHECK(value.IsFpuRegisterPair());
4975 if (index.IsConstant()) {
4976 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004977 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004978 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004979 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004980 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4981 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004982
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004983 break;
4984 }
4985
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004986 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004987 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004988 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004989 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004990
Roland Levillain80e67092016-01-08 16:04:55 +00004991 // Objects are handled in the switch.
4992 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004993 codegen_->MaybeRecordImplicitNullCheck(instruction);
4994 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995}
4996
4997void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004998 LocationSummary* locations =
4999 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005000 locations->SetInAt(0, Location::RequiresRegister());
5001 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005002}
5003
5004void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5005 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005006 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005007 Register obj = locations->InAt(0).AsRegister<Register>();
5008 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005010 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011}
5012
Artem Serov328429f2016-07-06 16:23:04 +01005013void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00005014 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5015 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01005016 LocationSummary* locations =
5017 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5018
5019 locations->SetInAt(0, Location::RequiresRegister());
5020 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5022}
5023
5024void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5025 LocationSummary* locations = instruction->GetLocations();
5026 Location out = locations->Out();
5027 Location first = locations->InAt(0);
5028 Location second = locations->InAt(1);
5029
Roland Levillain4a3aa572016-08-15 13:17:06 +00005030 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5031 DCHECK(!kEmitCompilerReadBarrier);
5032
Artem Serov328429f2016-07-06 16:23:04 +01005033 if (second.IsRegister()) {
5034 __ add(out.AsRegister<Register>(),
5035 first.AsRegister<Register>(),
5036 ShifterOperand(second.AsRegister<Register>()));
5037 } else {
5038 __ AddConstant(out.AsRegister<Register>(),
5039 first.AsRegister<Register>(),
5040 second.GetConstant()->AsIntConstant()->GetValue());
5041 }
5042}
5043
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005045 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5046 ? LocationSummary::kCallOnSlowPath
5047 : LocationSummary::kNoCall;
5048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005049 locations->SetInAt(0, Location::RequiresRegister());
5050 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005051 if (instruction->HasUses()) {
5052 locations->SetOut(Location::SameAsFirstInput());
5053 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054}
5055
5056void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5057 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005058 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005059 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060 codegen_->AddSlowPath(slow_path);
5061
Roland Levillain271ab9c2014-11-27 15:23:57 +00005062 Register index = locations->InAt(0).AsRegister<Register>();
5063 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064
5065 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005066 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067}
5068
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005069void CodeGeneratorARM::MarkGCCard(Register temp,
5070 Register card,
5071 Register object,
5072 Register value,
5073 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005074 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005075 if (can_be_null) {
5076 __ CompareAndBranchIfZero(value, &is_null);
5077 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005078 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5080 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005081 if (can_be_null) {
5082 __ Bind(&is_null);
5083 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084}
5085
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005086void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005087 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005088}
5089
5090void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005091 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5092}
5093
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005094void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
5095 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5096}
5097
5098void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005099 HBasicBlock* block = instruction->GetBlock();
5100 if (block->GetLoopInformation() != nullptr) {
5101 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5102 // The back edge will generate the suspend check.
5103 return;
5104 }
5105 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5106 // The goto will generate the suspend check.
5107 return;
5108 }
5109 GenerateSuspendCheck(instruction, nullptr);
5110}
5111
5112void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5113 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005114 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005115 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5116 if (slow_path == nullptr) {
5117 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5118 instruction->SetSlowPath(slow_path);
5119 codegen_->AddSlowPath(slow_path);
5120 if (successor != nullptr) {
5121 DCHECK(successor->IsLoopHeader());
5122 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5123 }
5124 } else {
5125 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5126 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005127
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005128 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005129 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005130 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005131 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005132 __ Bind(slow_path->GetReturnLabel());
5133 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005134 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005135 __ b(slow_path->GetEntryLabel());
5136 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005137}
5138
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005139ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5140 return codegen_->GetAssembler();
5141}
5142
5143void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005144 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005145 Location source = move->GetSource();
5146 Location destination = move->GetDestination();
5147
5148 if (source.IsRegister()) {
5149 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005150 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005151 } else if (destination.IsFpuRegister()) {
5152 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005153 } else {
5154 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005155 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005156 SP, destination.GetStackIndex());
5157 }
5158 } else if (source.IsStackSlot()) {
5159 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005160 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005161 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005162 } else if (destination.IsFpuRegister()) {
5163 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005164 } else {
5165 DCHECK(destination.IsStackSlot());
5166 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5167 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5168 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005169 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005170 if (destination.IsRegister()) {
5171 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5172 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005173 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005174 } else {
5175 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005176 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5177 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005178 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005179 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005180 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5181 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005182 } else if (destination.IsRegisterPair()) {
5183 DCHECK(ExpectedPairLayout(destination));
5184 __ LoadFromOffset(
5185 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5186 } else {
5187 DCHECK(destination.IsFpuRegisterPair()) << destination;
5188 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5189 SP,
5190 source.GetStackIndex());
5191 }
5192 } else if (source.IsRegisterPair()) {
5193 if (destination.IsRegisterPair()) {
5194 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5195 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005196 } else if (destination.IsFpuRegisterPair()) {
5197 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5198 source.AsRegisterPairLow<Register>(),
5199 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005200 } else {
5201 DCHECK(destination.IsDoubleStackSlot()) << destination;
5202 DCHECK(ExpectedPairLayout(source));
5203 __ StoreToOffset(
5204 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5205 }
5206 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005207 if (destination.IsRegisterPair()) {
5208 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5209 destination.AsRegisterPairHigh<Register>(),
5210 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5211 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005212 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5213 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5214 } else {
5215 DCHECK(destination.IsDoubleStackSlot()) << destination;
5216 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5217 SP,
5218 destination.GetStackIndex());
5219 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005220 } else {
5221 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005222 HConstant* constant = source.GetConstant();
5223 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5224 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005225 if (destination.IsRegister()) {
5226 __ LoadImmediate(destination.AsRegister<Register>(), value);
5227 } else {
5228 DCHECK(destination.IsStackSlot());
5229 __ LoadImmediate(IP, value);
5230 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5231 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005232 } else if (constant->IsLongConstant()) {
5233 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005234 if (destination.IsRegisterPair()) {
5235 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5236 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005237 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005238 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005239 __ LoadImmediate(IP, Low32Bits(value));
5240 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5241 __ LoadImmediate(IP, High32Bits(value));
5242 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5243 }
5244 } else if (constant->IsDoubleConstant()) {
5245 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005246 if (destination.IsFpuRegisterPair()) {
5247 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005248 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005249 DCHECK(destination.IsDoubleStackSlot()) << destination;
5250 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005251 __ LoadImmediate(IP, Low32Bits(int_value));
5252 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5253 __ LoadImmediate(IP, High32Bits(int_value));
5254 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5255 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005256 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005257 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005258 float value = constant->AsFloatConstant()->GetValue();
5259 if (destination.IsFpuRegister()) {
5260 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5261 } else {
5262 DCHECK(destination.IsStackSlot());
5263 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5264 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5265 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005266 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005267 }
5268}
5269
5270void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5271 __ Mov(IP, reg);
5272 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5273 __ StoreToOffset(kStoreWord, IP, SP, mem);
5274}
5275
5276void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5277 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5278 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5279 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5280 SP, mem1 + stack_offset);
5281 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5282 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5283 SP, mem2 + stack_offset);
5284 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5285}
5286
5287void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005288 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005289 Location source = move->GetSource();
5290 Location destination = move->GetDestination();
5291
5292 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 DCHECK_NE(source.AsRegister<Register>(), IP);
5294 DCHECK_NE(destination.AsRegister<Register>(), IP);
5295 __ Mov(IP, source.AsRegister<Register>());
5296 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5297 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005298 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005299 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005300 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005301 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005302 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5303 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005304 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005305 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005306 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005307 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005308 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005309 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005310 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005311 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005312 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5313 destination.AsRegisterPairHigh<Register>(),
5314 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005315 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005316 Register low_reg = source.IsRegisterPair()
5317 ? source.AsRegisterPairLow<Register>()
5318 : destination.AsRegisterPairLow<Register>();
5319 int mem = source.IsRegisterPair()
5320 ? destination.GetStackIndex()
5321 : source.GetStackIndex();
5322 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005323 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005324 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005325 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005326 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005327 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5328 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005329 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005330 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005331 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005332 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5333 DRegister reg = source.IsFpuRegisterPair()
5334 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5335 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5336 int mem = source.IsFpuRegisterPair()
5337 ? destination.GetStackIndex()
5338 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005339 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005340 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005341 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005342 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5343 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5344 : destination.AsFpuRegister<SRegister>();
5345 int mem = source.IsFpuRegister()
5346 ? destination.GetStackIndex()
5347 : source.GetStackIndex();
5348
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005349 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005350 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005351 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005352 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005353 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5354 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005355 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005356 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005357 }
5358}
5359
5360void ParallelMoveResolverARM::SpillScratch(int reg) {
5361 __ Push(static_cast<Register>(reg));
5362}
5363
5364void ParallelMoveResolverARM::RestoreScratch(int reg) {
5365 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005366}
5367
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005368HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5369 HLoadClass::LoadKind desired_class_load_kind) {
5370 if (kEmitCompilerReadBarrier) {
5371 switch (desired_class_load_kind) {
5372 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5373 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5374 case HLoadClass::LoadKind::kBootImageAddress:
5375 // TODO: Implement for read barrier.
5376 return HLoadClass::LoadKind::kDexCacheViaMethod;
5377 default:
5378 break;
5379 }
5380 }
5381 switch (desired_class_load_kind) {
5382 case HLoadClass::LoadKind::kReferrersClass:
5383 break;
5384 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5385 DCHECK(!GetCompilerOptions().GetCompilePic());
5386 break;
5387 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5388 DCHECK(GetCompilerOptions().GetCompilePic());
5389 break;
5390 case HLoadClass::LoadKind::kBootImageAddress:
5391 break;
5392 case HLoadClass::LoadKind::kDexCacheAddress:
5393 DCHECK(Runtime::Current()->UseJitCompilation());
5394 break;
5395 case HLoadClass::LoadKind::kDexCachePcRelative:
5396 DCHECK(!Runtime::Current()->UseJitCompilation());
5397 // We disable pc-relative load when there is an irreducible loop, as the optimization
5398 // is incompatible with it.
5399 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5400 // with irreducible loops.
5401 if (GetGraph()->HasIrreducibleLoops()) {
5402 return HLoadClass::LoadKind::kDexCacheViaMethod;
5403 }
5404 break;
5405 case HLoadClass::LoadKind::kDexCacheViaMethod:
5406 break;
5407 }
5408 return desired_class_load_kind;
5409}
5410
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005411void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005412 if (cls->NeedsAccessCheck()) {
5413 InvokeRuntimeCallingConvention calling_convention;
5414 CodeGenerator::CreateLoadClassLocationSummary(
5415 cls,
5416 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5417 Location::RegisterLocation(R0),
5418 /* code_generator_supports_read_barrier */ true);
5419 return;
5420 }
5421
5422 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5423 ? LocationSummary::kCallOnSlowPath
5424 : LocationSummary::kNoCall;
5425 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5426 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5427 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5428 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5429 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5430 locations->SetInAt(0, Location::RequiresRegister());
5431 }
5432 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005433}
5434
5435void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005436 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005437 if (cls->NeedsAccessCheck()) {
5438 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5439 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5440 cls,
5441 cls->GetDexPc(),
5442 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005443 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005444 return;
5445 }
5446
Roland Levillain3b359c72015-11-17 19:35:12 +00005447 Location out_loc = locations->Out();
5448 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005449
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005450 bool generate_null_check = false;
5451 switch (cls->GetLoadKind()) {
5452 case HLoadClass::LoadKind::kReferrersClass: {
5453 DCHECK(!cls->CanCallRuntime());
5454 DCHECK(!cls->MustGenerateClinitCheck());
5455 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5456 Register current_method = locations->InAt(0).AsRegister<Register>();
5457 GenerateGcRootFieldLoad(
5458 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5459 break;
5460 }
5461 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5462 DCHECK(!kEmitCompilerReadBarrier);
5463 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5464 cls->GetTypeIndex()));
5465 break;
5466 }
5467 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5468 DCHECK(!kEmitCompilerReadBarrier);
5469 CodeGeneratorARM::PcRelativePatchInfo* labels =
5470 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5471 __ BindTrackedLabel(&labels->movw_label);
5472 __ movw(out, /* placeholder */ 0u);
5473 __ BindTrackedLabel(&labels->movt_label);
5474 __ movt(out, /* placeholder */ 0u);
5475 __ BindTrackedLabel(&labels->add_pc_label);
5476 __ add(out, out, ShifterOperand(PC));
5477 break;
5478 }
5479 case HLoadClass::LoadKind::kBootImageAddress: {
5480 DCHECK(!kEmitCompilerReadBarrier);
5481 DCHECK_NE(cls->GetAddress(), 0u);
5482 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5483 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5484 break;
5485 }
5486 case HLoadClass::LoadKind::kDexCacheAddress: {
5487 DCHECK_NE(cls->GetAddress(), 0u);
5488 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5489 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5490 // a 128B range. To try and reduce the number of literals if we load multiple types,
5491 // simply split the dex cache address to a 128B aligned base loaded from a literal
5492 // and the remaining offset embedded in the load.
5493 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5494 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5495 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5496 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5497 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5498 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5499 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5500 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5501 generate_null_check = !cls->IsInDexCache();
5502 break;
5503 }
5504 case HLoadClass::LoadKind::kDexCachePcRelative: {
5505 Register base_reg = locations->InAt(0).AsRegister<Register>();
5506 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5507 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5508 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5509 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5510 generate_null_check = !cls->IsInDexCache();
5511 break;
5512 }
5513 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5514 // /* GcRoot<mirror::Class>[] */ out =
5515 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5516 Register current_method = locations->InAt(0).AsRegister<Register>();
5517 __ LoadFromOffset(kLoadWord,
5518 out,
5519 current_method,
5520 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5521 // /* GcRoot<mirror::Class> */ out = out[type_index]
5522 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5523 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5524 generate_null_check = !cls->IsInDexCache();
5525 }
5526 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005527
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005528 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5529 DCHECK(cls->CanCallRuntime());
5530 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5531 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5532 codegen_->AddSlowPath(slow_path);
5533 if (generate_null_check) {
5534 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5535 }
5536 if (cls->MustGenerateClinitCheck()) {
5537 GenerateClassInitializationCheck(slow_path, out);
5538 } else {
5539 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005540 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005541 }
5542}
5543
5544void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5545 LocationSummary* locations =
5546 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5547 locations->SetInAt(0, Location::RequiresRegister());
5548 if (check->HasUses()) {
5549 locations->SetOut(Location::SameAsFirstInput());
5550 }
5551}
5552
5553void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005554 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005555 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005556 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005557 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005558 GenerateClassInitializationCheck(slow_path,
5559 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005560}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005561
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005562void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005563 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005564 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5565 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5566 __ b(slow_path->GetEntryLabel(), LT);
5567 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5568 // properly. Therefore, we do a memory fence.
5569 __ dmb(ISH);
5570 __ Bind(slow_path->GetExitLabel());
5571}
5572
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005573HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5574 HLoadString::LoadKind desired_string_load_kind) {
5575 if (kEmitCompilerReadBarrier) {
5576 switch (desired_string_load_kind) {
5577 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5578 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5579 case HLoadString::LoadKind::kBootImageAddress:
5580 // TODO: Implement for read barrier.
5581 return HLoadString::LoadKind::kDexCacheViaMethod;
5582 default:
5583 break;
5584 }
5585 }
5586 switch (desired_string_load_kind) {
5587 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5588 DCHECK(!GetCompilerOptions().GetCompilePic());
5589 break;
5590 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5591 DCHECK(GetCompilerOptions().GetCompilePic());
5592 break;
5593 case HLoadString::LoadKind::kBootImageAddress:
5594 break;
5595 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005596 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005597 break;
5598 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005599 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005600 // We disable pc-relative load when there is an irreducible loop, as the optimization
5601 // is incompatible with it.
5602 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5603 // with irreducible loops.
5604 if (GetGraph()->HasIrreducibleLoops()) {
5605 return HLoadString::LoadKind::kDexCacheViaMethod;
5606 }
5607 break;
5608 case HLoadString::LoadKind::kDexCacheViaMethod:
5609 break;
5610 }
5611 return desired_string_load_kind;
5612}
5613
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005614void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005615 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005616 ? LocationSummary::kCallOnSlowPath
5617 : LocationSummary::kNoCall;
5618 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005619 HLoadString::LoadKind load_kind = load->GetLoadKind();
5620 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5621 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5622 locations->SetInAt(0, Location::RequiresRegister());
5623 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005624 locations->SetOut(Location::RequiresRegister());
5625}
5626
5627void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005628 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005629 Location out_loc = locations->Out();
5630 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005631
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005632 switch (load->GetLoadKind()) {
5633 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5634 DCHECK(!kEmitCompilerReadBarrier);
5635 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5636 load->GetStringIndex()));
5637 return; // No dex cache slow path.
5638 }
5639 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5640 DCHECK(!kEmitCompilerReadBarrier);
5641 CodeGeneratorARM::PcRelativePatchInfo* labels =
5642 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5643 __ BindTrackedLabel(&labels->movw_label);
5644 __ movw(out, /* placeholder */ 0u);
5645 __ BindTrackedLabel(&labels->movt_label);
5646 __ movt(out, /* placeholder */ 0u);
5647 __ BindTrackedLabel(&labels->add_pc_label);
5648 __ add(out, out, ShifterOperand(PC));
5649 return; // No dex cache slow path.
5650 }
5651 case HLoadString::LoadKind::kBootImageAddress: {
5652 DCHECK(!kEmitCompilerReadBarrier);
5653 DCHECK_NE(load->GetAddress(), 0u);
5654 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5655 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5656 return; // No dex cache slow path.
5657 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005658 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005659 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005660 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005661
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005662 // TODO: Re-add the compiler code to do string dex cache lookup again.
5663 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5664 codegen_->AddSlowPath(slow_path);
5665 __ b(slow_path->GetEntryLabel());
5666 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005667}
5668
David Brazdilcb1c0552015-08-04 16:22:25 +01005669static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005670 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005671}
5672
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005673void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5674 LocationSummary* locations =
5675 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5676 locations->SetOut(Location::RequiresRegister());
5677}
5678
5679void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005680 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005681 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5682}
5683
5684void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5685 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5686}
5687
5688void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005689 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005690 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005691}
5692
5693void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5694 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005695 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005696 InvokeRuntimeCallingConvention calling_convention;
5697 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5698}
5699
5700void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5701 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005702 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005703 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005704}
5705
Roland Levillainc9285912015-12-18 10:38:42 +00005706static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5707 return kEmitCompilerReadBarrier &&
5708 (kUseBakerReadBarrier ||
5709 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5710 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5711 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5712}
5713
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005714void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005715 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005716 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5717 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005718 case TypeCheckKind::kExactCheck:
5719 case TypeCheckKind::kAbstractClassCheck:
5720 case TypeCheckKind::kClassHierarchyCheck:
5721 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005722 call_kind =
5723 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 break;
5725 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005726 case TypeCheckKind::kUnresolvedCheck:
5727 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005728 call_kind = LocationSummary::kCallOnSlowPath;
5729 break;
5730 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005731
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005732 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005733 locations->SetInAt(0, Location::RequiresRegister());
5734 locations->SetInAt(1, Location::RequiresRegister());
5735 // The "out" register is used as a temporary, so it overlaps with the inputs.
5736 // Note that TypeCheckSlowPathARM uses this register too.
5737 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5738 // When read barriers are enabled, we need a temporary register for
5739 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005740 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005741 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005743}
5744
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005745void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005746 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005747 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005748 Location obj_loc = locations->InAt(0);
5749 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005750 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005751 Location out_loc = locations->Out();
5752 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005753 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005754 locations->GetTemp(0) :
5755 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005756 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005757 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5758 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5759 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005760 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005761 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005762
5763 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005764 // avoid null check if we know obj is not null.
5765 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005766 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005767 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005768
Roland Levillain3b359c72015-11-17 19:35:12 +00005769 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005770 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005771
Roland Levillainc9285912015-12-18 10:38:42 +00005772 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 case TypeCheckKind::kExactCheck: {
5774 __ cmp(out, ShifterOperand(cls));
5775 // Classes must be equal for the instanceof to succeed.
5776 __ b(&zero, NE);
5777 __ LoadImmediate(out, 1);
5778 __ b(&done);
5779 break;
5780 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005781
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005782 case TypeCheckKind::kAbstractClassCheck: {
5783 // If the class is abstract, we eagerly fetch the super class of the
5784 // object to avoid doing a comparison we know will fail.
5785 Label loop;
5786 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005787 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005788 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005789 // If `out` is null, we use it for the result, and jump to `done`.
5790 __ CompareAndBranchIfZero(out, &done);
5791 __ cmp(out, ShifterOperand(cls));
5792 __ b(&loop, NE);
5793 __ LoadImmediate(out, 1);
5794 if (zero.IsLinked()) {
5795 __ b(&done);
5796 }
5797 break;
5798 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005799
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005800 case TypeCheckKind::kClassHierarchyCheck: {
5801 // Walk over the class hierarchy to find a match.
5802 Label loop, success;
5803 __ Bind(&loop);
5804 __ cmp(out, ShifterOperand(cls));
5805 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005806 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005807 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005808 __ CompareAndBranchIfNonZero(out, &loop);
5809 // If `out` is null, we use it for the result, and jump to `done`.
5810 __ b(&done);
5811 __ Bind(&success);
5812 __ LoadImmediate(out, 1);
5813 if (zero.IsLinked()) {
5814 __ b(&done);
5815 }
5816 break;
5817 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005818
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005820 // Do an exact check.
5821 Label exact_check;
5822 __ cmp(out, ShifterOperand(cls));
5823 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005824 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005825 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005826 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005827 // If `out` is null, we use it for the result, and jump to `done`.
5828 __ CompareAndBranchIfZero(out, &done);
5829 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5830 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5831 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005832 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005833 __ LoadImmediate(out, 1);
5834 __ b(&done);
5835 break;
5836 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005837
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 case TypeCheckKind::kArrayCheck: {
5839 __ cmp(out, ShifterOperand(cls));
5840 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005841 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5842 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005843 codegen_->AddSlowPath(slow_path);
5844 __ b(slow_path->GetEntryLabel(), NE);
5845 __ LoadImmediate(out, 1);
5846 if (zero.IsLinked()) {
5847 __ b(&done);
5848 }
5849 break;
5850 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005851
Calin Juravle98893e12015-10-02 21:05:03 +01005852 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005853 case TypeCheckKind::kInterfaceCheck: {
5854 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005855 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005856 // cases.
5857 //
5858 // We cannot directly call the InstanceofNonTrivial runtime
5859 // entry point without resorting to a type checking slow path
5860 // here (i.e. by calling InvokeRuntime directly), as it would
5861 // require to assign fixed registers for the inputs of this
5862 // HInstanceOf instruction (following the runtime calling
5863 // convention), which might be cluttered by the potential first
5864 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005865 //
5866 // TODO: Introduce a new runtime entry point taking the object
5867 // to test (instead of its class) as argument, and let it deal
5868 // with the read barrier issues. This will let us refactor this
5869 // case of the `switch` code as it was previously (with a direct
5870 // call to the runtime not using a type checking slow path).
5871 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005872 DCHECK(locations->OnlyCallsOnSlowPath());
5873 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5874 /* is_fatal */ false);
5875 codegen_->AddSlowPath(slow_path);
5876 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005877 if (zero.IsLinked()) {
5878 __ b(&done);
5879 }
5880 break;
5881 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005882 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005883
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005885 __ Bind(&zero);
5886 __ LoadImmediate(out, 0);
5887 }
5888
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 if (done.IsLinked()) {
5890 __ Bind(&done);
5891 }
5892
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005893 if (slow_path != nullptr) {
5894 __ Bind(slow_path->GetExitLabel());
5895 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005896}
5897
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005898void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005899 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5900 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5901
Roland Levillain3b359c72015-11-17 19:35:12 +00005902 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5903 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005904 case TypeCheckKind::kExactCheck:
5905 case TypeCheckKind::kAbstractClassCheck:
5906 case TypeCheckKind::kClassHierarchyCheck:
5907 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005908 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5909 LocationSummary::kCallOnSlowPath :
5910 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005911 break;
5912 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005913 case TypeCheckKind::kUnresolvedCheck:
5914 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005915 call_kind = LocationSummary::kCallOnSlowPath;
5916 break;
5917 }
5918
Roland Levillain3b359c72015-11-17 19:35:12 +00005919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5920 locations->SetInAt(0, Location::RequiresRegister());
5921 locations->SetInAt(1, Location::RequiresRegister());
5922 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5923 locations->AddTemp(Location::RequiresRegister());
5924 // When read barriers are enabled, we need an additional temporary
5925 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005926 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005927 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005928 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005929}
5930
5931void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005932 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005933 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005934 Location obj_loc = locations->InAt(0);
5935 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005936 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005937 Location temp_loc = locations->GetTemp(0);
5938 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005939 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005940 locations->GetTemp(1) :
5941 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005942 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5944 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5945 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005946
Roland Levillain3b359c72015-11-17 19:35:12 +00005947 bool is_type_check_slow_path_fatal =
5948 (type_check_kind == TypeCheckKind::kExactCheck ||
5949 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5950 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5951 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5952 !instruction->CanThrowIntoCatchBlock();
5953 SlowPathCode* type_check_slow_path =
5954 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5955 is_type_check_slow_path_fatal);
5956 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957
5958 Label done;
5959 // Avoid null check if we know obj is not null.
5960 if (instruction->MustDoNullCheck()) {
5961 __ CompareAndBranchIfZero(obj, &done);
5962 }
5963
Roland Levillain3b359c72015-11-17 19:35:12 +00005964 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005965 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005966
Roland Levillain3b359c72015-11-17 19:35:12 +00005967 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 case TypeCheckKind::kExactCheck:
5969 case TypeCheckKind::kArrayCheck: {
5970 __ cmp(temp, ShifterOperand(cls));
5971 // Jump to slow path for throwing the exception or doing a
5972 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005973 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005974 break;
5975 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005976
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 case TypeCheckKind::kAbstractClassCheck: {
5978 // If the class is abstract, we eagerly fetch the super class of the
5979 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005980 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005981 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005982 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005983 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005984
5985 // If the class reference currently in `temp` is not null, jump
5986 // to the `compare_classes` label to compare it with the checked
5987 // class.
5988 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5989 // Otherwise, jump to the slow path to throw the exception.
5990 //
5991 // But before, move back the object's class into `temp` before
5992 // going into the slow path, as it has been overwritten in the
5993 // meantime.
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());
5998
5999 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006000 __ cmp(temp, ShifterOperand(cls));
6001 __ b(&loop, NE);
6002 break;
6003 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006004
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006005 case TypeCheckKind::kClassHierarchyCheck: {
6006 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006007 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006008 __ Bind(&loop);
6009 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006010 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006011
Roland Levillain3b359c72015-11-17 19:35:12 +00006012 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006013 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006014
6015 // If the class reference currently in `temp` is not null, jump
6016 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006017 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006018 // Otherwise, jump to the slow path to throw the exception.
6019 //
6020 // But before, move back the object's class into `temp` before
6021 // going into the slow path, as it has been overwritten in the
6022 // meantime.
6023 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006024 GenerateReferenceLoadTwoRegisters(
6025 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006026 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006027 break;
6028 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006029
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006030 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006031 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006032 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006033 __ cmp(temp, ShifterOperand(cls));
6034 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006035
6036 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006037 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006038 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006039
6040 // If the component type is not null (i.e. the object is indeed
6041 // an array), jump to label `check_non_primitive_component_type`
6042 // to further check that this component type is not a primitive
6043 // type.
6044 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
6045 // Otherwise, jump to the slow path to throw the exception.
6046 //
6047 // But before, move back the object's class into `temp` before
6048 // going into the slow path, as it has been overwritten in the
6049 // meantime.
6050 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006051 GenerateReferenceLoadTwoRegisters(
6052 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006053 __ b(type_check_slow_path->GetEntryLabel());
6054
6055 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006056 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006057 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6058 __ CompareAndBranchIfZero(temp, &done);
6059 // Same comment as above regarding `temp` and the slow path.
6060 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006061 GenerateReferenceLoadTwoRegisters(
6062 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006063 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006064 break;
6065 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006066
Calin Juravle98893e12015-10-02 21:05:03 +01006067 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006068 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006069 // We always go into the type check slow path for the unresolved
6070 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006071 //
6072 // We cannot directly call the CheckCast runtime entry point
6073 // without resorting to a type checking slow path here (i.e. by
6074 // calling InvokeRuntime directly), as it would require to
6075 // assign fixed registers for the inputs of this HInstanceOf
6076 // instruction (following the runtime calling convention), which
6077 // might be cluttered by the potential first read barrier
6078 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006079 //
6080 // TODO: Introduce a new runtime entry point taking the object
6081 // to test (instead of its class) as argument, and let it deal
6082 // with the read barrier issues. This will let us refactor this
6083 // case of the `switch` code as it was previously (with a direct
6084 // call to the runtime not using a type checking slow path).
6085 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006086 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006087 break;
6088 }
6089 __ Bind(&done);
6090
Roland Levillain3b359c72015-11-17 19:35:12 +00006091 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006092}
6093
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006094void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6095 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006097 InvokeRuntimeCallingConvention calling_convention;
6098 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6099}
6100
6101void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6102 codegen_->InvokeRuntime(instruction->IsEnter()
6103 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
6104 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00006105 instruction->GetDexPc(),
6106 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006107 if (instruction->IsEnter()) {
6108 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6109 } else {
6110 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6111 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006112}
6113
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006114void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6115void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6116void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006117
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006118void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006119 LocationSummary* locations =
6120 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6121 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6122 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006123 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006124 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006125 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006126 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006127}
6128
6129void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6130 HandleBitwiseOperation(instruction);
6131}
6132
6133void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6134 HandleBitwiseOperation(instruction);
6135}
6136
6137void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6138 HandleBitwiseOperation(instruction);
6139}
6140
Artem Serov7fc63502016-02-09 17:15:29 +00006141
6142void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6143 LocationSummary* locations =
6144 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6145 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6146 || instruction->GetResultType() == Primitive::kPrimLong);
6147
6148 locations->SetInAt(0, Location::RequiresRegister());
6149 locations->SetInAt(1, Location::RequiresRegister());
6150 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6151}
6152
6153void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6154 LocationSummary* locations = instruction->GetLocations();
6155 Location first = locations->InAt(0);
6156 Location second = locations->InAt(1);
6157 Location out = locations->Out();
6158
6159 if (instruction->GetResultType() == Primitive::kPrimInt) {
6160 Register first_reg = first.AsRegister<Register>();
6161 ShifterOperand second_reg(second.AsRegister<Register>());
6162 Register out_reg = out.AsRegister<Register>();
6163
6164 switch (instruction->GetOpKind()) {
6165 case HInstruction::kAnd:
6166 __ bic(out_reg, first_reg, second_reg);
6167 break;
6168 case HInstruction::kOr:
6169 __ orn(out_reg, first_reg, second_reg);
6170 break;
6171 // There is no EON on arm.
6172 case HInstruction::kXor:
6173 default:
6174 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6175 UNREACHABLE();
6176 }
6177 return;
6178
6179 } else {
6180 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6181 Register first_low = first.AsRegisterPairLow<Register>();
6182 Register first_high = first.AsRegisterPairHigh<Register>();
6183 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6184 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6185 Register out_low = out.AsRegisterPairLow<Register>();
6186 Register out_high = out.AsRegisterPairHigh<Register>();
6187
6188 switch (instruction->GetOpKind()) {
6189 case HInstruction::kAnd:
6190 __ bic(out_low, first_low, second_low);
6191 __ bic(out_high, first_high, second_high);
6192 break;
6193 case HInstruction::kOr:
6194 __ orn(out_low, first_low, second_low);
6195 __ orn(out_high, first_high, second_high);
6196 break;
6197 // There is no EON on arm.
6198 case HInstruction::kXor:
6199 default:
6200 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6201 UNREACHABLE();
6202 }
6203 }
6204}
6205
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006206void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6207 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6208 if (value == 0xffffffffu) {
6209 if (out != first) {
6210 __ mov(out, ShifterOperand(first));
6211 }
6212 return;
6213 }
6214 if (value == 0u) {
6215 __ mov(out, ShifterOperand(0));
6216 return;
6217 }
6218 ShifterOperand so;
6219 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6220 __ and_(out, first, so);
6221 } else {
6222 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6223 __ bic(out, first, ShifterOperand(~value));
6224 }
6225}
6226
6227void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6228 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6229 if (value == 0u) {
6230 if (out != first) {
6231 __ mov(out, ShifterOperand(first));
6232 }
6233 return;
6234 }
6235 if (value == 0xffffffffu) {
6236 __ mvn(out, ShifterOperand(0));
6237 return;
6238 }
6239 ShifterOperand so;
6240 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6241 __ orr(out, first, so);
6242 } else {
6243 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6244 __ orn(out, first, ShifterOperand(~value));
6245 }
6246}
6247
6248void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6249 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6250 if (value == 0u) {
6251 if (out != first) {
6252 __ mov(out, ShifterOperand(first));
6253 }
6254 return;
6255 }
6256 __ eor(out, first, ShifterOperand(value));
6257}
6258
Vladimir Marko59751a72016-08-05 14:37:27 +01006259void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6260 Location first,
6261 uint64_t value) {
6262 Register out_low = out.AsRegisterPairLow<Register>();
6263 Register out_high = out.AsRegisterPairHigh<Register>();
6264 Register first_low = first.AsRegisterPairLow<Register>();
6265 Register first_high = first.AsRegisterPairHigh<Register>();
6266 uint32_t value_low = Low32Bits(value);
6267 uint32_t value_high = High32Bits(value);
6268 if (value_low == 0u) {
6269 if (out_low != first_low) {
6270 __ mov(out_low, ShifterOperand(first_low));
6271 }
6272 __ AddConstant(out_high, first_high, value_high);
6273 return;
6274 }
6275 __ AddConstantSetFlags(out_low, first_low, value_low);
6276 ShifterOperand so;
6277 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6278 __ adc(out_high, first_high, so);
6279 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6280 __ sbc(out_high, first_high, so);
6281 } else {
6282 LOG(FATAL) << "Unexpected constant " << value_high;
6283 UNREACHABLE();
6284 }
6285}
6286
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006287void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6288 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006289 Location first = locations->InAt(0);
6290 Location second = locations->InAt(1);
6291 Location out = locations->Out();
6292
6293 if (second.IsConstant()) {
6294 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6295 uint32_t value_low = Low32Bits(value);
6296 if (instruction->GetResultType() == Primitive::kPrimInt) {
6297 Register first_reg = first.AsRegister<Register>();
6298 Register out_reg = out.AsRegister<Register>();
6299 if (instruction->IsAnd()) {
6300 GenerateAndConst(out_reg, first_reg, value_low);
6301 } else if (instruction->IsOr()) {
6302 GenerateOrrConst(out_reg, first_reg, value_low);
6303 } else {
6304 DCHECK(instruction->IsXor());
6305 GenerateEorConst(out_reg, first_reg, value_low);
6306 }
6307 } else {
6308 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6309 uint32_t value_high = High32Bits(value);
6310 Register first_low = first.AsRegisterPairLow<Register>();
6311 Register first_high = first.AsRegisterPairHigh<Register>();
6312 Register out_low = out.AsRegisterPairLow<Register>();
6313 Register out_high = out.AsRegisterPairHigh<Register>();
6314 if (instruction->IsAnd()) {
6315 GenerateAndConst(out_low, first_low, value_low);
6316 GenerateAndConst(out_high, first_high, value_high);
6317 } else if (instruction->IsOr()) {
6318 GenerateOrrConst(out_low, first_low, value_low);
6319 GenerateOrrConst(out_high, first_high, value_high);
6320 } else {
6321 DCHECK(instruction->IsXor());
6322 GenerateEorConst(out_low, first_low, value_low);
6323 GenerateEorConst(out_high, first_high, value_high);
6324 }
6325 }
6326 return;
6327 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006328
6329 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006330 Register first_reg = first.AsRegister<Register>();
6331 ShifterOperand second_reg(second.AsRegister<Register>());
6332 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006333 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006334 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006335 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006336 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006337 } else {
6338 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006339 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006340 }
6341 } else {
6342 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006343 Register first_low = first.AsRegisterPairLow<Register>();
6344 Register first_high = first.AsRegisterPairHigh<Register>();
6345 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6346 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6347 Register out_low = out.AsRegisterPairLow<Register>();
6348 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006349 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006350 __ and_(out_low, first_low, second_low);
6351 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006352 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006353 __ orr(out_low, first_low, second_low);
6354 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006355 } else {
6356 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006357 __ eor(out_low, first_low, second_low);
6358 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006359 }
6360 }
6361}
6362
Roland Levillainc9285912015-12-18 10:38:42 +00006363void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6364 Location out,
6365 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006366 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006367 Register out_reg = out.AsRegister<Register>();
6368 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006369 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006370 if (kUseBakerReadBarrier) {
6371 // Load with fast path based Baker's read barrier.
6372 // /* HeapReference<Object> */ out = *(out + offset)
6373 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006374 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006375 } else {
6376 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006377 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006378 // in the following move operation, as we will need it for the
6379 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006380 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006381 // /* HeapReference<Object> */ out = *(out + offset)
6382 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006383 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006384 }
6385 } else {
6386 // Plain load with no read barrier.
6387 // /* HeapReference<Object> */ out = *(out + offset)
6388 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6389 __ MaybeUnpoisonHeapReference(out_reg);
6390 }
6391}
6392
6393void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6394 Location out,
6395 Location obj,
6396 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006397 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006398 Register out_reg = out.AsRegister<Register>();
6399 Register obj_reg = obj.AsRegister<Register>();
6400 if (kEmitCompilerReadBarrier) {
6401 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006402 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006403 // Load with fast path based Baker's read barrier.
6404 // /* HeapReference<Object> */ out = *(obj + offset)
6405 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006406 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006407 } else {
6408 // Load with slow path based read barrier.
6409 // /* HeapReference<Object> */ out = *(obj + offset)
6410 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6411 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6412 }
6413 } else {
6414 // Plain load with no read barrier.
6415 // /* HeapReference<Object> */ out = *(obj + offset)
6416 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6417 __ MaybeUnpoisonHeapReference(out_reg);
6418 }
6419}
6420
6421void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6422 Location root,
6423 Register obj,
6424 uint32_t offset) {
6425 Register root_reg = root.AsRegister<Register>();
6426 if (kEmitCompilerReadBarrier) {
6427 if (kUseBakerReadBarrier) {
6428 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6429 // Baker's read barrier are used:
6430 //
6431 // root = obj.field;
6432 // if (Thread::Current()->GetIsGcMarking()) {
6433 // root = ReadBarrier::Mark(root)
6434 // }
6435
6436 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6437 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6438 static_assert(
6439 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6440 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6441 "have different sizes.");
6442 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6443 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6444 "have different sizes.");
6445
Vladimir Marko953437b2016-08-24 08:30:46 +00006446 // Slow path marking the GC root `root`.
Roland Levillainc9285912015-12-18 10:38:42 +00006447 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006448 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006449 codegen_->AddSlowPath(slow_path);
6450
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006451 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006452 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006453 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006454 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6455 __ Bind(slow_path->GetExitLabel());
6456 } else {
6457 // GC root loaded through a slow path for read barriers other
6458 // than Baker's.
6459 // /* GcRoot<mirror::Object>* */ root = obj + offset
6460 __ AddConstant(root_reg, obj, offset);
6461 // /* mirror::Object* */ root = root->Read()
6462 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6463 }
6464 } else {
6465 // Plain GC root load with no read barrier.
6466 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6467 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6468 // Note that GC roots are not affected by heap poisoning, thus we
6469 // do not have to unpoison `root_reg` here.
6470 }
6471}
6472
6473void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6474 Location ref,
6475 Register obj,
6476 uint32_t offset,
6477 Location temp,
6478 bool needs_null_check) {
6479 DCHECK(kEmitCompilerReadBarrier);
6480 DCHECK(kUseBakerReadBarrier);
6481
6482 // /* HeapReference<Object> */ ref = *(obj + offset)
6483 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006484 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006485 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006486 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006487}
6488
6489void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6490 Location ref,
6491 Register obj,
6492 uint32_t data_offset,
6493 Location index,
6494 Location temp,
6495 bool needs_null_check) {
6496 DCHECK(kEmitCompilerReadBarrier);
6497 DCHECK(kUseBakerReadBarrier);
6498
Roland Levillainbfea3352016-06-23 13:48:47 +01006499 static_assert(
6500 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6501 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006502 // /* HeapReference<Object> */ ref =
6503 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006504 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006505 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006506 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006507}
6508
6509void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6510 Location ref,
6511 Register obj,
6512 uint32_t offset,
6513 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006514 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006515 Location temp,
6516 bool needs_null_check) {
6517 DCHECK(kEmitCompilerReadBarrier);
6518 DCHECK(kUseBakerReadBarrier);
6519
6520 // In slow path based read barriers, the read barrier call is
6521 // inserted after the original load. However, in fast path based
6522 // Baker's read barriers, we need to perform the load of
6523 // mirror::Object::monitor_ *before* the original reference load.
6524 // This load-load ordering is required by the read barrier.
6525 // The fast path/slow path (for Baker's algorithm) should look like:
6526 //
6527 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6528 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6529 // HeapReference<Object> ref = *src; // Original reference load.
6530 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6531 // if (is_gray) {
6532 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6533 // }
6534 //
6535 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006536 // slightly more complex as it performs additional checks that we do
6537 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006538
6539 Register ref_reg = ref.AsRegister<Register>();
6540 Register temp_reg = temp.AsRegister<Register>();
6541 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6542
6543 // /* int32_t */ monitor = obj->monitor_
6544 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6545 if (needs_null_check) {
6546 MaybeRecordImplicitNullCheck(instruction);
6547 }
6548 // /* LockWord */ lock_word = LockWord(monitor)
6549 static_assert(sizeof(LockWord) == sizeof(int32_t),
6550 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006551
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006552 // Introduce a dependency on the lock_word including the rb_state,
6553 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006554 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006555 // `obj` is unchanged by this operation, but its value now depends
6556 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006557 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006558
6559 // The actual reference load.
6560 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006561 // Load types involving an "index": ArrayGet and
6562 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6563 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006564 if (index.IsConstant()) {
6565 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006566 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006567 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6568 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006569 // Handle the special case of the
6570 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6571 // a register pair as index ("long offset"), of which only the low
6572 // part contains data.
6573 Register index_reg = index.IsRegisterPair()
6574 ? index.AsRegisterPairLow<Register>()
6575 : index.AsRegister<Register>();
6576 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006577 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6578 }
6579 } else {
6580 // /* HeapReference<Object> */ ref = *(obj + offset)
6581 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6582 }
6583
6584 // Object* ref = ref_addr->AsMirrorPtr()
6585 __ MaybeUnpoisonHeapReference(ref_reg);
6586
Vladimir Marko953437b2016-08-24 08:30:46 +00006587 // Slow path marking the object `ref` when it is gray.
Roland Levillainc9285912015-12-18 10:38:42 +00006588 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006589 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006590 AddSlowPath(slow_path);
6591
6592 // if (rb_state == ReadBarrier::gray_ptr_)
6593 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006594 // Given the numeric representation, it's enough to check the low bit of the
6595 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6596 // which can be a 16-bit instruction unlike the TST immediate.
6597 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6598 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6599 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6600 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6601 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006602 __ Bind(slow_path->GetExitLabel());
6603}
6604
6605void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6606 Location out,
6607 Location ref,
6608 Location obj,
6609 uint32_t offset,
6610 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006611 DCHECK(kEmitCompilerReadBarrier);
6612
Roland Levillainc9285912015-12-18 10:38:42 +00006613 // Insert a slow path based read barrier *after* the reference load.
6614 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006615 // If heap poisoning is enabled, the unpoisoning of the loaded
6616 // reference will be carried out by the runtime within the slow
6617 // path.
6618 //
6619 // Note that `ref` currently does not get unpoisoned (when heap
6620 // poisoning is enabled), which is alright as the `ref` argument is
6621 // not used by the artReadBarrierSlow entry point.
6622 //
6623 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6624 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6625 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6626 AddSlowPath(slow_path);
6627
Roland Levillain3b359c72015-11-17 19:35:12 +00006628 __ b(slow_path->GetEntryLabel());
6629 __ Bind(slow_path->GetExitLabel());
6630}
6631
Roland Levillainc9285912015-12-18 10:38:42 +00006632void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6633 Location out,
6634 Location ref,
6635 Location obj,
6636 uint32_t offset,
6637 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006638 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006639 // Baker's read barriers shall be handled by the fast path
6640 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6641 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006642 // If heap poisoning is enabled, unpoisoning will be taken care of
6643 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006644 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006645 } else if (kPoisonHeapReferences) {
6646 __ UnpoisonHeapReference(out.AsRegister<Register>());
6647 }
6648}
6649
Roland Levillainc9285912015-12-18 10:38:42 +00006650void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6651 Location out,
6652 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006653 DCHECK(kEmitCompilerReadBarrier);
6654
Roland Levillainc9285912015-12-18 10:38:42 +00006655 // Insert a slow path based read barrier *after* the GC root load.
6656 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006657 // Note that GC roots are not affected by heap poisoning, so we do
6658 // not need to do anything special for this here.
6659 SlowPathCode* slow_path =
6660 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6661 AddSlowPath(slow_path);
6662
Roland Levillain3b359c72015-11-17 19:35:12 +00006663 __ b(slow_path->GetEntryLabel());
6664 __ Bind(slow_path->GetExitLabel());
6665}
6666
Vladimir Markodc151b22015-10-15 18:02:30 +01006667HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6668 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6669 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006670 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6671 // We disable pc-relative load when there is an irreducible loop, as the optimization
6672 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006673 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6674 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006675 if (GetGraph()->HasIrreducibleLoops() &&
6676 (dispatch_info.method_load_kind ==
6677 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6678 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6679 }
6680
6681 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006682 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6683 if (&outer_dex_file != target_method.dex_file) {
6684 // Calls across dex files are more likely to exceed the available BL range,
6685 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6686 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6687 (desired_dispatch_info.method_load_kind ==
6688 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6689 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6690 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6691 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006692 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006693 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006694 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006695 0u
6696 };
6697 }
6698 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006699 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006700}
6701
Vladimir Markob4536b72015-11-24 13:45:23 +00006702Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6703 Register temp) {
6704 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6705 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6706 if (!invoke->GetLocations()->Intrinsified()) {
6707 return location.AsRegister<Register>();
6708 }
6709 // For intrinsics we allow any location, so it may be on the stack.
6710 if (!location.IsRegister()) {
6711 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6712 return temp;
6713 }
6714 // For register locations, check if the register was saved. If so, get it from the stack.
6715 // Note: There is a chance that the register was saved but not overwritten, so we could
6716 // save one load. However, since this is just an intrinsic slow path we prefer this
6717 // simple and more robust approach rather that trying to determine if that's the case.
6718 SlowPathCode* slow_path = GetCurrentSlowPath();
6719 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6720 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6721 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6722 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6723 return temp;
6724 }
6725 return location.AsRegister<Register>();
6726}
6727
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006728void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006729 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006730 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006731 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6732 // LR = code address from literal pool with link-time patch.
6733 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006734 break;
6735 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6736 // LR = invoke->GetDirectCodePtr();
6737 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006738 break;
6739 default:
6740 break;
6741 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006742
Vladimir Marko58155012015-08-19 12:49:41 +00006743 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6744 switch (invoke->GetMethodLoadKind()) {
6745 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6746 // temp = thread->string_init_entrypoint
6747 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6748 break;
6749 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006750 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006751 break;
6752 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6753 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6754 break;
6755 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6756 __ LoadLiteral(temp.AsRegister<Register>(),
6757 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6758 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006759 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6760 HArmDexCacheArraysBase* base =
6761 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6762 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6763 temp.AsRegister<Register>());
6764 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6765 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6766 break;
6767 }
Vladimir Marko58155012015-08-19 12:49:41 +00006768 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006769 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006770 Register method_reg;
6771 Register reg = temp.AsRegister<Register>();
6772 if (current_method.IsRegister()) {
6773 method_reg = current_method.AsRegister<Register>();
6774 } else {
6775 DCHECK(invoke->GetLocations()->Intrinsified());
6776 DCHECK(!current_method.IsValid());
6777 method_reg = reg;
6778 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6779 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006780 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6781 __ LoadFromOffset(kLoadWord,
6782 reg,
6783 method_reg,
6784 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006785 // temp = temp[index_in_cache];
6786 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6787 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006788 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6789 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006790 }
Vladimir Marko58155012015-08-19 12:49:41 +00006791 }
6792
6793 switch (invoke->GetCodePtrLocation()) {
6794 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6795 __ bl(GetFrameEntryLabel());
6796 break;
6797 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006798 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006799 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006800 // Arbitrarily branch to the BL itself, override at link time.
6801 __ bl(&relative_call_patches_.back().label);
6802 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006803 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6804 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6805 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006806 // LR()
6807 __ blx(LR);
6808 break;
6809 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6810 // LR = callee_method->entry_point_from_quick_compiled_code_
6811 __ LoadFromOffset(
6812 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006813 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006814 // LR()
6815 __ blx(LR);
6816 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006817 }
6818
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006819 DCHECK(!IsLeafMethod());
6820}
6821
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006822void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6823 Register temp = temp_location.AsRegister<Register>();
6824 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6825 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006826
6827 // Use the calling convention instead of the location of the receiver, as
6828 // intrinsics may have put the receiver in a different register. In the intrinsics
6829 // slow path, the arguments have been moved to the right place, so here we are
6830 // guaranteed that the receiver is the first register of the calling convention.
6831 InvokeDexCallingConvention calling_convention;
6832 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006833 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006834 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006835 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006836 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006837 // Instead of simply (possibly) unpoisoning `temp` here, we should
6838 // emit a read barrier for the previous class reference load.
6839 // However this is not required in practice, as this is an
6840 // intermediate/temporary reference and because the current
6841 // concurrent copying collector keeps the from-space memory
6842 // intact/accessible until the end of the marking phase (the
6843 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006844 __ MaybeUnpoisonHeapReference(temp);
6845 // temp = temp->GetMethodAt(method_offset);
6846 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006847 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006848 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6849 // LR = temp->GetEntryPoint();
6850 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6851 // LR();
6852 __ blx(LR);
6853}
6854
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006855CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6856 const DexFile& dex_file, uint32_t string_index) {
6857 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6858}
6859
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006860CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6861 const DexFile& dex_file, uint32_t type_index) {
6862 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6863}
6864
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006865CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6866 const DexFile& dex_file, uint32_t element_offset) {
6867 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6868}
6869
6870CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6871 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6872 patches->emplace_back(dex_file, offset_or_index);
6873 return &patches->back();
6874}
6875
6876Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6877 uint32_t string_index) {
6878 return boot_image_string_patches_.GetOrCreate(
6879 StringReference(&dex_file, string_index),
6880 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6881}
6882
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006883Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6884 uint32_t type_index) {
6885 return boot_image_type_patches_.GetOrCreate(
6886 TypeReference(&dex_file, type_index),
6887 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6888}
6889
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006890Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6891 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6892 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6893 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6894}
6895
6896Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6897 return DeduplicateUint32Literal(address, &uint32_literals_);
6898}
6899
Vladimir Marko58155012015-08-19 12:49:41 +00006900void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6901 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006902 size_t size =
6903 method_patches_.size() +
6904 call_patches_.size() +
6905 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006906 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6907 boot_image_string_patches_.size() +
6908 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006909 boot_image_type_patches_.size() +
6910 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006911 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006912 linker_patches->reserve(size);
6913 for (const auto& entry : method_patches_) {
6914 const MethodReference& target_method = entry.first;
6915 Literal* literal = entry.second;
6916 DCHECK(literal->GetLabel()->IsBound());
6917 uint32_t literal_offset = literal->GetLabel()->Position();
6918 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6919 target_method.dex_file,
6920 target_method.dex_method_index));
6921 }
6922 for (const auto& entry : call_patches_) {
6923 const MethodReference& target_method = entry.first;
6924 Literal* literal = entry.second;
6925 DCHECK(literal->GetLabel()->IsBound());
6926 uint32_t literal_offset = literal->GetLabel()->Position();
6927 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6928 target_method.dex_file,
6929 target_method.dex_method_index));
6930 }
6931 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6932 uint32_t literal_offset = info.label.Position();
6933 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6934 info.target_method.dex_file,
6935 info.target_method.dex_method_index));
6936 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006937 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6938 const DexFile& dex_file = info.target_dex_file;
6939 size_t base_element_offset = info.offset_or_index;
6940 DCHECK(info.add_pc_label.IsBound());
6941 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006942 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006943 DCHECK(info.movw_label.IsBound());
6944 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006945 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6946 &dex_file,
6947 add_pc_offset,
6948 base_element_offset));
6949 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006950 DCHECK(info.movt_label.IsBound());
6951 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006952 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6953 &dex_file,
6954 add_pc_offset,
6955 base_element_offset));
6956 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006957 for (const auto& entry : boot_image_string_patches_) {
6958 const StringReference& target_string = entry.first;
6959 Literal* literal = entry.second;
6960 DCHECK(literal->GetLabel()->IsBound());
6961 uint32_t literal_offset = literal->GetLabel()->Position();
6962 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6963 target_string.dex_file,
6964 target_string.string_index));
6965 }
6966 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6967 const DexFile& dex_file = info.target_dex_file;
6968 uint32_t string_index = info.offset_or_index;
6969 DCHECK(info.add_pc_label.IsBound());
6970 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6971 // Add MOVW patch.
6972 DCHECK(info.movw_label.IsBound());
6973 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6974 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6975 &dex_file,
6976 add_pc_offset,
6977 string_index));
6978 // Add MOVT patch.
6979 DCHECK(info.movt_label.IsBound());
6980 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6981 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6982 &dex_file,
6983 add_pc_offset,
6984 string_index));
6985 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006986 for (const auto& entry : boot_image_type_patches_) {
6987 const TypeReference& target_type = entry.first;
6988 Literal* literal = entry.second;
6989 DCHECK(literal->GetLabel()->IsBound());
6990 uint32_t literal_offset = literal->GetLabel()->Position();
6991 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6992 target_type.dex_file,
6993 target_type.type_index));
6994 }
6995 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6996 const DexFile& dex_file = info.target_dex_file;
6997 uint32_t type_index = info.offset_or_index;
6998 DCHECK(info.add_pc_label.IsBound());
6999 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7000 // Add MOVW patch.
7001 DCHECK(info.movw_label.IsBound());
7002 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7003 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
7004 &dex_file,
7005 add_pc_offset,
7006 type_index));
7007 // Add MOVT patch.
7008 DCHECK(info.movt_label.IsBound());
7009 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7010 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
7011 &dex_file,
7012 add_pc_offset,
7013 type_index));
7014 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007015 for (const auto& entry : boot_image_address_patches_) {
7016 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7017 Literal* literal = entry.second;
7018 DCHECK(literal->GetLabel()->IsBound());
7019 uint32_t literal_offset = literal->GetLabel()->Position();
7020 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7021 }
7022}
7023
7024Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7025 return map->GetOrCreate(
7026 value,
7027 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007028}
7029
7030Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7031 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007032 return map->GetOrCreate(
7033 target_method,
7034 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007035}
7036
7037Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7038 return DeduplicateMethodLiteral(target_method, &method_patches_);
7039}
7040
7041Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7042 return DeduplicateMethodLiteral(target_method, &call_patches_);
7043}
7044
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007045void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7046 LocationSummary* locations =
7047 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7048 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7049 Location::RequiresRegister());
7050 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7051 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7052 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7053}
7054
7055void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7056 LocationSummary* locations = instr->GetLocations();
7057 Register res = locations->Out().AsRegister<Register>();
7058 Register accumulator =
7059 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7060 Register mul_left =
7061 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7062 Register mul_right =
7063 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7064
7065 if (instr->GetOpKind() == HInstruction::kAdd) {
7066 __ mla(res, mul_left, mul_right, accumulator);
7067 } else {
7068 __ mls(res, mul_left, mul_right, accumulator);
7069 }
7070}
7071
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007072void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007073 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007074 LOG(FATAL) << "Unreachable";
7075}
7076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007077void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007078 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007079 LOG(FATAL) << "Unreachable";
7080}
7081
Mark Mendellfe57faa2015-09-18 09:26:15 -04007082// Simple implementation of packed switch - generate cascaded compare/jumps.
7083void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7084 LocationSummary* locations =
7085 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7086 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007087 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007088 codegen_->GetAssembler()->IsThumb()) {
7089 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7090 if (switch_instr->GetStartValue() != 0) {
7091 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7092 }
7093 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007094}
7095
7096void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7097 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007098 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007099 LocationSummary* locations = switch_instr->GetLocations();
7100 Register value_reg = locations->InAt(0).AsRegister<Register>();
7101 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7102
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007103 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007104 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007105 Register temp_reg = IP;
7106 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7107 // the immediate, because IP is used as the destination register. For the other
7108 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7109 // and they can be encoded in the instruction without making use of IP register.
7110 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7111
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007112 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007113 // Jump to successors[0] if value == lower_bound.
7114 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7115 int32_t last_index = 0;
7116 for (; num_entries - last_index > 2; last_index += 2) {
7117 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7118 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7119 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7120 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7121 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7122 }
7123 if (num_entries - last_index == 2) {
7124 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007125 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007126 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007127 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007128
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007129 // And the default for any other value.
7130 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7131 __ b(codegen_->GetLabelOf(default_block));
7132 }
7133 } else {
7134 // Create a table lookup.
7135 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7136
7137 // Materialize a pointer to the switch table
7138 std::vector<Label*> labels(num_entries);
7139 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7140 for (uint32_t i = 0; i < num_entries; i++) {
7141 labels[i] = codegen_->GetLabelOf(successors[i]);
7142 }
7143 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7144
7145 // Remove the bias.
7146 Register key_reg;
7147 if (lower_bound != 0) {
7148 key_reg = locations->GetTemp(1).AsRegister<Register>();
7149 __ AddConstant(key_reg, value_reg, -lower_bound);
7150 } else {
7151 key_reg = value_reg;
7152 }
7153
7154 // Check whether the value is in the table, jump to default block if not.
7155 __ CmpConstant(key_reg, num_entries - 1);
7156 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7157
7158 // Load the displacement from the table.
7159 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7160
7161 // Dispatch is a direct add to the PC (for Thumb2).
7162 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007163 }
7164}
7165
Vladimir Markob4536b72015-11-24 13:45:23 +00007166void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7167 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7168 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007169}
7170
7171void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7172 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007173 CodeGeneratorARM::PcRelativePatchInfo* labels =
7174 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007175 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007176 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007177 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007178 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007179 __ BindTrackedLabel(&labels->add_pc_label);
7180 __ add(base_reg, base_reg, ShifterOperand(PC));
7181}
7182
Andreas Gampe85b62f22015-09-09 13:15:38 -07007183void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7184 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007185 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007186 return;
7187 }
7188
7189 DCHECK_NE(type, Primitive::kPrimVoid);
7190
7191 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7192 if (return_loc.Equals(trg)) {
7193 return;
7194 }
7195
7196 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7197 // with the last branch.
7198 if (type == Primitive::kPrimLong) {
7199 HParallelMove parallel_move(GetGraph()->GetArena());
7200 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7201 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7202 GetMoveResolver()->EmitNativeCode(&parallel_move);
7203 } else if (type == Primitive::kPrimDouble) {
7204 HParallelMove parallel_move(GetGraph()->GetArena());
7205 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7206 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7207 GetMoveResolver()->EmitNativeCode(&parallel_move);
7208 } else {
7209 // Let the parallel move resolver take care of all of this.
7210 HParallelMove parallel_move(GetGraph()->GetArena());
7211 parallel_move.AddMove(return_loc, trg, type, nullptr);
7212 GetMoveResolver()->EmitNativeCode(&parallel_move);
7213 }
7214}
7215
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007216void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7217 LocationSummary* locations =
7218 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7219 locations->SetInAt(0, Location::RequiresRegister());
7220 locations->SetOut(Location::RequiresRegister());
7221}
7222
7223void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7224 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007225 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007226 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007227 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007228 __ LoadFromOffset(kLoadWord,
7229 locations->Out().AsRegister<Register>(),
7230 locations->InAt(0).AsRegister<Register>(),
7231 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007232 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007233 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007234 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007235 __ LoadFromOffset(kLoadWord,
7236 locations->Out().AsRegister<Register>(),
7237 locations->InAt(0).AsRegister<Register>(),
7238 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7239 __ LoadFromOffset(kLoadWord,
7240 locations->Out().AsRegister<Register>(),
7241 locations->Out().AsRegister<Register>(),
7242 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007243 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007244}
7245
Roland Levillain4d027112015-07-01 15:41:14 +01007246#undef __
7247#undef QUICK_ENTRY_POINT
7248
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007249} // namespace arm
7250} // namespace art