blob: 236ed20fc02006ca5f1f8cb1a9c86b9c85b64d74 [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
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070062// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, 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 Geoffraya8ac9132015-03-13 16:36:36 +0000122 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100123 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000124 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000125 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000126 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 if (successor_ == nullptr) {
128 __ b(GetReturnLabel());
129 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 }
133
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 Label* GetReturnLabel() {
135 DCHECK(successor_ == nullptr);
136 return &return_label_;
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100139 HBasicBlock* GetSuccessor() const {
140 return successor_;
141 }
142
Alexandre Rames9931f312015-06-19 14:47:01 +0100143 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
144
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100146 // If not null, the block to branch to after the suspend check.
147 HBasicBlock* const successor_;
148
149 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 Label return_label_;
151
152 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
153};
154
Andreas Gampe85b62f22015-09-09 13:15:38 -0700155class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100157 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000158 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159
Alexandre Rames67555f72014-11-18 10:55:16 +0000160 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100161 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100162 LocationSummary* locations = instruction_->GetLocations();
163
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000165 if (instruction_->CanThrowIntoCatchBlock()) {
166 // Live registers will be restored in the catch block if caught.
167 SaveLiveRegisters(codegen, instruction_->GetLocations());
168 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000169 // We're moving two locations to locations that could overlap, so we need a parallel
170 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000172 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100173 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100175 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100176 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100177 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
178 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100179 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
180 ? QUICK_ENTRY_POINT(pThrowStringBounds)
181 : QUICK_ENTRY_POINT(pThrowArrayBounds);
182 arm_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
183 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000184 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 }
186
Alexandre Rames8158f282015-08-07 10:26:17 +0100187 bool IsFatal() const OVERRIDE { return true; }
188
Alexandre Rames9931f312015-06-19 14:47:01 +0100189 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
190
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
193};
194
Andreas Gampe85b62f22015-09-09 13:15:38 -0700195class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 LoadClassSlowPathARM(HLoadClass* cls,
198 HInstruction* at,
199 uint32_t dex_pc,
200 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204
Alexandre Rames67555f72014-11-18 10:55:16 +0000205 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LocationSummary* locations = at_->GetLocations();
207
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100208 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
209 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 int32_t entry_point_offset = do_clinit_
215 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
216 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000217 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000218 if (do_clinit_) {
219 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
220 } else {
221 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
222 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223
224 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000225 Location out = locations->Out();
226 if (out.IsValid()) {
227 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
229 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231 __ b(GetExitLabel());
232 }
233
Alexandre Rames9931f312015-06-19 14:47:01 +0100234 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
235
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100236 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 // The class this slow path will load.
238 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 // The instruction where this slow path is happening.
241 // (Might be the load class or an initialization check).
242 HInstruction* const at_;
243
244 // The dex PC of `at_`.
245 const uint32_t dex_pc_;
246
247 // Whether to initialize the class.
248 const bool do_clinit_;
249
250 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100251};
252
Andreas Gampe85b62f22015-09-09 13:15:38 -0700253class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000255 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258 LocationSummary* locations = instruction_->GetLocations();
259 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
260
261 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000264
265 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000266 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
267 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000268 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000270 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
272
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 __ b(GetExitLabel());
275 }
276
Alexandre Rames9931f312015-06-19 14:47:01 +0100277 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
278
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000279 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000280 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
281};
282
Andreas Gampe85b62f22015-09-09 13:15:38 -0700283class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000285 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
Alexandre Rames67555f72014-11-18 10:55:16 +0000288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100290 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
291 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 DCHECK(instruction_->IsCheckCast()
293 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
295 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
296 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000297
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000298 if (!is_fatal_) {
299 SaveLiveRegisters(codegen, locations);
300 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 // We're moving two locations to locations that could overlap, so we need a parallel
303 // move resolver.
304 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100309 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100310 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
311 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100314 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
315 instruction_,
316 instruction_->GetDexPc(),
317 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000318 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700319 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
321 } else {
322 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
324 instruction_,
325 instruction_->GetDexPc(),
326 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000327 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 if (!is_fatal_) {
331 RestoreLiveRegisters(codegen, locations);
332 __ b(GetExitLabel());
333 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 }
335
Alexandre Rames9931f312015-06-19 14:47:01 +0100336 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
337
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000338 bool IsFatal() const OVERRIDE { return is_fatal_; }
339
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000341 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342
343 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
344};
345
Andreas Gampe85b62f22015-09-09 13:15:38 -0700346class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700347 public:
Aart Bik42249c32016-01-07 15:33:50 -0800348 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800352 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800355 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
356 instruction_,
357 instruction_->GetDexPc(),
358 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000359 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700360 }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
363
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700364 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
366};
367
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100368class ArraySetSlowPathARM : public SlowPathCode {
369 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000370 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100371
372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
373 LocationSummary* locations = instruction_->GetLocations();
374 __ Bind(GetEntryLabel());
375 SaveLiveRegisters(codegen, locations);
376
377 InvokeRuntimeCallingConvention calling_convention;
378 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
379 parallel_move.AddMove(
380 locations->InAt(0),
381 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
382 Primitive::kPrimNot,
383 nullptr);
384 parallel_move.AddMove(
385 locations->InAt(1),
386 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
387 Primitive::kPrimInt,
388 nullptr);
389 parallel_move.AddMove(
390 locations->InAt(2),
391 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
392 Primitive::kPrimNot,
393 nullptr);
394 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
395
396 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
397 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
398 instruction_,
399 instruction_->GetDexPc(),
400 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402 RestoreLiveRegisters(codegen, locations);
403 __ b(GetExitLabel());
404 }
405
406 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
407
408 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100409 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
410};
411
Roland Levillainc9285912015-12-18 10:38:42 +0000412// Slow path marking an object during a read barrier.
413class ReadBarrierMarkSlowPathARM : public SlowPathCode {
414 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100415 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
416 : SlowPathCode(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000417 DCHECK(kEmitCompilerReadBarrier);
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
421
422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100424 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000425 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100426 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000427 DCHECK(instruction_->IsInstanceFieldGet() ||
428 instruction_->IsStaticFieldGet() ||
429 instruction_->IsArrayGet() ||
430 instruction_->IsLoadClass() ||
431 instruction_->IsLoadString() ||
432 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100433 instruction_->IsCheckCast() ||
434 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
435 instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000436 << "Unexpected instruction in read barrier marking slow path: "
437 << instruction_->DebugName();
438
439 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100440 // No need to save live registers; it's taken care of by the
441 // entrypoint. Also, there is no need to update the stack mask,
442 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000443 InvokeRuntimeCallingConvention calling_convention;
444 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100445 DCHECK_NE(reg, SP);
446 DCHECK_NE(reg, LR);
447 DCHECK_NE(reg, PC);
448 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
449 // "Compact" slow path, saving two moves.
450 //
451 // Instead of using the standard runtime calling convention (input
452 // and output in R0):
453 //
454 // R0 <- obj
455 // R0 <- ReadBarrierMark(R0)
456 // obj <- R0
457 //
458 // we just use rX (the register holding `obj`) as input and output
459 // of a dedicated entrypoint:
460 //
461 // rX <- ReadBarrierMarkRegX(rX)
462 //
463 int32_t entry_point_offset =
464 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmWordSize>(reg);
465 // TODO: Do not emit a stack map for this runtime call.
466 arm_codegen->InvokeRuntime(entry_point_offset,
Roland Levillainc9285912015-12-18 10:38:42 +0000467 instruction_,
468 instruction_->GetDexPc(),
469 this);
Roland Levillainc9285912015-12-18 10:38:42 +0000470 __ b(GetExitLabel());
471 }
472
473 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000474 const Location obj_;
475
476 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
477};
478
Roland Levillain3b359c72015-11-17 19:35:12 +0000479// Slow path generating a read barrier for a heap reference.
480class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
481 public:
482 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
483 Location out,
484 Location ref,
485 Location obj,
486 uint32_t offset,
487 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000488 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000489 out_(out),
490 ref_(ref),
491 obj_(obj),
492 offset_(offset),
493 index_(index) {
494 DCHECK(kEmitCompilerReadBarrier);
495 // If `obj` is equal to `out` or `ref`, it means the initial object
496 // has been overwritten by (or after) the heap object reference load
497 // to be instrumented, e.g.:
498 //
499 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000500 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000501 //
502 // In that case, we have lost the information about the original
503 // object, and the emitted read barrier cannot work properly.
504 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
505 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
506 }
507
508 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
509 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
510 LocationSummary* locations = instruction_->GetLocations();
511 Register reg_out = out_.AsRegister<Register>();
512 DCHECK(locations->CanCall());
513 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100514 DCHECK(instruction_->IsInstanceFieldGet() ||
515 instruction_->IsStaticFieldGet() ||
516 instruction_->IsArrayGet() ||
517 instruction_->IsInstanceOf() ||
518 instruction_->IsCheckCast() ||
519 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillainc9285912015-12-18 10:38:42 +0000520 instruction_->GetLocations()->Intrinsified()))
521 << "Unexpected instruction in read barrier for heap reference slow path: "
522 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000523
524 __ Bind(GetEntryLabel());
525 SaveLiveRegisters(codegen, locations);
526
527 // We may have to change the index's value, but as `index_` is a
528 // constant member (like other "inputs" of this slow path),
529 // introduce a copy of it, `index`.
530 Location index = index_;
531 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100532 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000533 if (instruction_->IsArrayGet()) {
534 // Compute the actual memory offset and store it in `index`.
535 Register index_reg = index_.AsRegister<Register>();
536 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
537 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
538 // We are about to change the value of `index_reg` (see the
539 // calls to art::arm::Thumb2Assembler::Lsl and
540 // art::arm::Thumb2Assembler::AddConstant below), but it has
541 // not been saved by the previous call to
542 // art::SlowPathCode::SaveLiveRegisters, as it is a
543 // callee-save register --
544 // art::SlowPathCode::SaveLiveRegisters does not consider
545 // callee-save registers, as it has been designed with the
546 // assumption that callee-save registers are supposed to be
547 // handled by the called function. So, as a callee-save
548 // register, `index_reg` _would_ eventually be saved onto
549 // the stack, but it would be too late: we would have
550 // changed its value earlier. Therefore, we manually save
551 // it here into another freely available register,
552 // `free_reg`, chosen of course among the caller-save
553 // registers (as a callee-save `free_reg` register would
554 // exhibit the same problem).
555 //
556 // Note we could have requested a temporary register from
557 // the register allocator instead; but we prefer not to, as
558 // this is a slow path, and we know we can find a
559 // caller-save register that is available.
560 Register free_reg = FindAvailableCallerSaveRegister(codegen);
561 __ Mov(free_reg, index_reg);
562 index_reg = free_reg;
563 index = Location::RegisterLocation(index_reg);
564 } else {
565 // The initial register stored in `index_` has already been
566 // saved in the call to art::SlowPathCode::SaveLiveRegisters
567 // (as it is not a callee-save register), so we can freely
568 // use it.
569 }
570 // Shifting the index value contained in `index_reg` by the scale
571 // factor (2) cannot overflow in practice, as the runtime is
572 // unable to allocate object arrays with a size larger than
573 // 2^26 - 1 (that is, 2^28 - 4 bytes).
574 __ Lsl(index_reg, index_reg, TIMES_4);
575 static_assert(
576 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
577 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
578 __ AddConstant(index_reg, index_reg, offset_);
579 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100580 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
581 // intrinsics, `index_` is not shifted by a scale factor of 2
582 // (as in the case of ArrayGet), as it is actually an offset
583 // to an object field within an object.
584 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000585 DCHECK(instruction_->GetLocations()->Intrinsified());
586 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
587 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
588 << instruction_->AsInvoke()->GetIntrinsic();
589 DCHECK_EQ(offset_, 0U);
590 DCHECK(index_.IsRegisterPair());
591 // UnsafeGet's offset location is a register pair, the low
592 // part contains the correct offset.
593 index = index_.ToLow();
594 }
595 }
596
597 // We're moving two or three locations to locations that could
598 // overlap, so we need a parallel move resolver.
599 InvokeRuntimeCallingConvention calling_convention;
600 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
601 parallel_move.AddMove(ref_,
602 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
603 Primitive::kPrimNot,
604 nullptr);
605 parallel_move.AddMove(obj_,
606 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
607 Primitive::kPrimNot,
608 nullptr);
609 if (index.IsValid()) {
610 parallel_move.AddMove(index,
611 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
612 Primitive::kPrimInt,
613 nullptr);
614 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
615 } else {
616 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
617 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
618 }
619 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
620 instruction_,
621 instruction_->GetDexPc(),
622 this);
623 CheckEntrypointTypes<
624 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
625 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
626
627 RestoreLiveRegisters(codegen, locations);
628 __ b(GetExitLabel());
629 }
630
631 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
632
633 private:
634 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
635 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
636 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
637 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
638 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
639 return static_cast<Register>(i);
640 }
641 }
642 // We shall never fail to find a free caller-save register, as
643 // there are more than two core caller-save registers on ARM
644 // (meaning it is possible to find one which is different from
645 // `ref` and `obj`).
646 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
647 LOG(FATAL) << "Could not find a free caller-save register";
648 UNREACHABLE();
649 }
650
Roland Levillain3b359c72015-11-17 19:35:12 +0000651 const Location out_;
652 const Location ref_;
653 const Location obj_;
654 const uint32_t offset_;
655 // An additional location containing an index to an array.
656 // Only used for HArrayGet and the UnsafeGetObject &
657 // UnsafeGetObjectVolatile intrinsics.
658 const Location index_;
659
660 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
661};
662
663// Slow path generating a read barrier for a GC root.
664class ReadBarrierForRootSlowPathARM : public SlowPathCode {
665 public:
666 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000667 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000668 DCHECK(kEmitCompilerReadBarrier);
669 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000670
671 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
672 LocationSummary* locations = instruction_->GetLocations();
673 Register reg_out = out_.AsRegister<Register>();
674 DCHECK(locations->CanCall());
675 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000676 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
677 << "Unexpected instruction in read barrier for GC root slow path: "
678 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000679
680 __ Bind(GetEntryLabel());
681 SaveLiveRegisters(codegen, locations);
682
683 InvokeRuntimeCallingConvention calling_convention;
684 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
685 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
686 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
687 instruction_,
688 instruction_->GetDexPc(),
689 this);
690 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
691 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
692
693 RestoreLiveRegisters(codegen, locations);
694 __ b(GetExitLabel());
695 }
696
697 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
698
699 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000700 const Location out_;
701 const Location root_;
702
703 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
704};
705
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000706#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700707// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
708#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700709
Aart Bike9f37602015-10-09 11:15:55 -0700710inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700711 switch (cond) {
712 case kCondEQ: return EQ;
713 case kCondNE: return NE;
714 case kCondLT: return LT;
715 case kCondLE: return LE;
716 case kCondGT: return GT;
717 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700718 case kCondB: return LO;
719 case kCondBE: return LS;
720 case kCondA: return HI;
721 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700722 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100723 LOG(FATAL) << "Unreachable";
724 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700725}
726
Aart Bike9f37602015-10-09 11:15:55 -0700727// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100728inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700729 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730 case kCondEQ: return EQ;
731 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700732 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100733 case kCondLT: return LO;
734 case kCondLE: return LS;
735 case kCondGT: return HI;
736 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700737 // Unsigned remain unchanged.
738 case kCondB: return LO;
739 case kCondBE: return LS;
740 case kCondA: return HI;
741 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700742 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100743 LOG(FATAL) << "Unreachable";
744 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700745}
746
Vladimir Markod6e069b2016-01-18 11:11:01 +0000747inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
748 // The ARM condition codes can express all the necessary branches, see the
749 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
750 // There is no dex instruction or HIR that would need the missing conditions
751 // "equal or unordered" or "not equal".
752 switch (cond) {
753 case kCondEQ: return EQ;
754 case kCondNE: return NE /* unordered */;
755 case kCondLT: return gt_bias ? CC : LT /* unordered */;
756 case kCondLE: return gt_bias ? LS : LE /* unordered */;
757 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
758 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
759 default:
760 LOG(FATAL) << "UNREACHABLE";
761 UNREACHABLE();
762 }
763}
764
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100765void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100766 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100767}
768
769void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100770 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100771}
772
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100773size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
774 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
775 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100776}
777
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100778size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
779 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
780 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100781}
782
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000783size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
784 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
785 return kArmWordSize;
786}
787
788size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
789 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
790 return kArmWordSize;
791}
792
Calin Juravle34166012014-12-19 17:22:29 +0000793CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000794 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100795 const CompilerOptions& compiler_options,
796 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000797 : CodeGenerator(graph,
798 kNumberOfCoreRegisters,
799 kNumberOfSRegisters,
800 kNumberOfRegisterPairs,
801 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
802 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000803 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
804 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100805 compiler_options,
806 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100807 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100809 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100810 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100811 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000812 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000813 uint32_literals_(std::less<uint32_t>(),
814 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100815 method_patches_(MethodReferenceComparator(),
816 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
817 call_patches_(MethodReferenceComparator(),
818 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000819 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000820 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
821 boot_image_string_patches_(StringReferenceValueComparator(),
822 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
823 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100824 boot_image_type_patches_(TypeReferenceValueComparator(),
825 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
826 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000827 boot_image_address_patches_(std::less<uint32_t>(),
828 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700829 // Always save the LR register to mimic Quick.
830 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100831}
832
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000833void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
834 // Ensure that we fix up branches and literal loads and emit the literal pool.
835 __ FinalizeCode();
836
837 // Adjust native pc offsets in stack maps.
838 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
839 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
840 uint32_t new_position = __ GetAdjustedPosition(old_position);
841 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
842 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100843 // Adjust pc offsets for the disassembly information.
844 if (disasm_info_ != nullptr) {
845 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
846 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
847 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
848 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
849 it.second.start = __ GetAdjustedPosition(it.second.start);
850 it.second.end = __ GetAdjustedPosition(it.second.end);
851 }
852 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
853 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
854 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
855 }
856 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000857
858 CodeGenerator::Finalize(allocator);
859}
860
David Brazdil58282f42016-01-14 12:45:10 +0000861void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100862 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100863 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100864
865 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100866 blocked_core_registers_[SP] = true;
867 blocked_core_registers_[LR] = true;
868 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100869
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100871 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100872
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100873 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100874 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100875
David Brazdil58282f42016-01-14 12:45:10 +0000876 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100877 // Stubs do not save callee-save floating point registers. If the graph
878 // is debuggable, we need to deal with these registers differently. For
879 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000880 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
881 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
882 }
883 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100884
885 UpdateBlockedPairRegisters();
886}
887
888void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
889 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
890 ArmManagedRegister current =
891 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
892 if (blocked_core_registers_[current.AsRegisterPairLow()]
893 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
894 blocked_register_pairs_[i] = true;
895 }
896 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100897}
898
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100899InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800900 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100901 assembler_(codegen->GetAssembler()),
902 codegen_(codegen) {}
903
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000904void CodeGeneratorARM::ComputeSpillMask() {
905 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
906 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000907 // There is no easy instruction to restore just the PC on thumb2. We spill and
908 // restore another arbitrary register.
909 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000910 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
911 // We use vpush and vpop for saving and restoring floating point registers, which take
912 // a SRegister and the number of registers to save/restore after that SRegister. We
913 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
914 // but in the range.
915 if (fpu_spill_mask_ != 0) {
916 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
917 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
918 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
919 fpu_spill_mask_ |= (1 << i);
920 }
921 }
922}
923
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100924static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100925 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100926}
927
928static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100929 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930}
931
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000932void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000933 bool skip_overflow_check =
934 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000935 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000936 __ Bind(&frame_entry_label_);
937
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000938 if (HasEmptyFrame()) {
939 return;
940 }
941
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100942 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000943 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
944 __ LoadFromOffset(kLoadWord, IP, IP, 0);
945 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100946 }
947
Andreas Gampe501fd632015-09-10 16:11:06 -0700948 __ PushList(core_spill_mask_);
949 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
950 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000951 if (fpu_spill_mask_ != 0) {
952 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
953 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100954 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100955 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000956 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100957 int adjust = GetFrameSize() - FrameEntrySpillSize();
958 __ AddConstant(SP, -adjust);
959 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100960 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000961}
962
963void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000964 if (HasEmptyFrame()) {
965 __ bx(LR);
966 return;
967 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100968 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100969 int adjust = GetFrameSize() - FrameEntrySpillSize();
970 __ AddConstant(SP, adjust);
971 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000972 if (fpu_spill_mask_ != 0) {
973 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
974 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100975 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
976 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000977 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700978 // Pop LR into PC to return.
979 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
980 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
981 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100982 __ cfi().RestoreState();
983 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000984}
985
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100986void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700987 Label* label = GetLabelOf(block);
988 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000989}
990
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100991Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100992 switch (type) {
993 case Primitive::kPrimBoolean:
994 case Primitive::kPrimByte:
995 case Primitive::kPrimChar:
996 case Primitive::kPrimShort:
997 case Primitive::kPrimInt:
998 case Primitive::kPrimNot: {
999 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001000 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001001 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001002 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001003 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001004 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001005 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001006 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001008 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001009 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001010 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001011 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001012 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001014 if (calling_convention.GetRegisterAt(index) == R1) {
1015 // Skip R1, and use R2_R3 instead.
1016 gp_index_++;
1017 index++;
1018 }
1019 }
1020 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1021 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001022 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001023
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001024 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001025 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001026 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001027 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1028 }
1029 }
1030
1031 case Primitive::kPrimFloat: {
1032 uint32_t stack_index = stack_index_++;
1033 if (float_index_ % 2 == 0) {
1034 float_index_ = std::max(double_index_, float_index_);
1035 }
1036 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1037 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1038 } else {
1039 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1040 }
1041 }
1042
1043 case Primitive::kPrimDouble: {
1044 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1045 uint32_t stack_index = stack_index_;
1046 stack_index_ += 2;
1047 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1048 uint32_t index = double_index_;
1049 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001050 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001051 calling_convention.GetFpuRegisterAt(index),
1052 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001053 DCHECK(ExpectedPairLayout(result));
1054 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001055 } else {
1056 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001057 }
1058 }
1059
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001060 case Primitive::kPrimVoid:
1061 LOG(FATAL) << "Unexpected parameter type " << type;
1062 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001063 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001064 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001065}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001066
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001067Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001068 switch (type) {
1069 case Primitive::kPrimBoolean:
1070 case Primitive::kPrimByte:
1071 case Primitive::kPrimChar:
1072 case Primitive::kPrimShort:
1073 case Primitive::kPrimInt:
1074 case Primitive::kPrimNot: {
1075 return Location::RegisterLocation(R0);
1076 }
1077
1078 case Primitive::kPrimFloat: {
1079 return Location::FpuRegisterLocation(S0);
1080 }
1081
1082 case Primitive::kPrimLong: {
1083 return Location::RegisterPairLocation(R0, R1);
1084 }
1085
1086 case Primitive::kPrimDouble: {
1087 return Location::FpuRegisterPairLocation(S0, S1);
1088 }
1089
1090 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001091 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001092 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001093
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001094 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001095}
1096
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001097Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1098 return Location::RegisterLocation(kMethodRegisterArgument);
1099}
1100
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101void CodeGeneratorARM::Move32(Location destination, Location source) {
1102 if (source.Equals(destination)) {
1103 return;
1104 }
1105 if (destination.IsRegister()) {
1106 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001108 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 } else if (destination.IsFpuRegister()) {
1114 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001115 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001117 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001119 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001120 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001121 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001122 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001124 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001125 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001126 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001128 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001129 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1130 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001131 }
1132 }
1133}
1134
1135void CodeGeneratorARM::Move64(Location destination, Location source) {
1136 if (source.Equals(destination)) {
1137 return;
1138 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001139 if (destination.IsRegisterPair()) {
1140 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001141 EmitParallelMoves(
1142 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1143 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001144 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001145 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001146 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1147 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001149 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001150 } else if (source.IsFpuRegisterPair()) {
1151 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1152 destination.AsRegisterPairHigh<Register>(),
1153 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 } else {
1155 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001156 DCHECK(ExpectedPairLayout(destination));
1157 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1158 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001160 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001161 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001162 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1163 SP,
1164 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001165 } else if (source.IsRegisterPair()) {
1166 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1167 source.AsRegisterPairLow<Register>(),
1168 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001169 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001170 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 } else {
1173 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001175 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001176 if (source.AsRegisterPairLow<Register>() == R1) {
1177 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1179 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001181 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 SP, destination.GetStackIndex());
1183 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001184 } else if (source.IsFpuRegisterPair()) {
1185 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1186 SP,
1187 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188 } else {
1189 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001190 EmitParallelMoves(
1191 Location::StackSlot(source.GetStackIndex()),
1192 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001193 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001194 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001195 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1196 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197 }
1198 }
1199}
1200
Calin Juravle175dc732015-08-25 15:42:32 +01001201void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1202 DCHECK(location.IsRegister());
1203 __ LoadImmediate(location.AsRegister<Register>(), value);
1204}
1205
Calin Juravlee460d1d2015-09-29 04:52:17 +01001206void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001207 HParallelMove move(GetGraph()->GetArena());
1208 move.AddMove(src, dst, dst_type, nullptr);
1209 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001210}
1211
1212void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1213 if (location.IsRegister()) {
1214 locations->AddTemp(location);
1215 } else if (location.IsRegisterPair()) {
1216 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1217 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1218 } else {
1219 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1220 }
1221}
1222
Calin Juravle175dc732015-08-25 15:42:32 +01001223void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1224 HInstruction* instruction,
1225 uint32_t dex_pc,
1226 SlowPathCode* slow_path) {
1227 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1228 instruction,
1229 dex_pc,
1230 slow_path);
1231}
1232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001233void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1234 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001235 uint32_t dex_pc,
1236 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001237 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001238 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1239 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001240 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001241}
1242
David Brazdilfc6a86a2015-06-26 10:33:45 +00001243void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001244 DCHECK(!successor->IsExitBlock());
1245
1246 HBasicBlock* block = got->GetBlock();
1247 HInstruction* previous = got->GetPrevious();
1248
1249 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001250 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001251 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1252 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1253 return;
1254 }
1255
1256 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1257 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1258 }
1259 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001260 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001261 }
1262}
1263
David Brazdilfc6a86a2015-06-26 10:33:45 +00001264void LocationsBuilderARM::VisitGoto(HGoto* got) {
1265 got->SetLocations(nullptr);
1266}
1267
1268void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1269 HandleGoto(got, got->GetSuccessor());
1270}
1271
1272void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1273 try_boundary->SetLocations(nullptr);
1274}
1275
1276void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1277 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1278 if (!successor->IsExitBlock()) {
1279 HandleGoto(try_boundary, successor);
1280 }
1281}
1282
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001283void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001284 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001285}
1286
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001287void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001288}
1289
Roland Levillain4fa13f62015-07-06 18:11:54 +01001290void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1291 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001292 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001293 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001294 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001295}
1296
1297void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1298 Label* true_label,
1299 Label* false_label) {
1300 LocationSummary* locations = cond->GetLocations();
1301 Location left = locations->InAt(0);
1302 Location right = locations->InAt(1);
1303 IfCondition if_cond = cond->GetCondition();
1304
1305 Register left_high = left.AsRegisterPairHigh<Register>();
1306 Register left_low = left.AsRegisterPairLow<Register>();
1307 IfCondition true_high_cond = if_cond;
1308 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001309 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001310
1311 // Set the conditions for the test, remembering that == needs to be
1312 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001313 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001314 switch (if_cond) {
1315 case kCondEQ:
1316 case kCondNE:
1317 // Nothing to do.
1318 break;
1319 case kCondLT:
1320 false_high_cond = kCondGT;
1321 break;
1322 case kCondLE:
1323 true_high_cond = kCondLT;
1324 break;
1325 case kCondGT:
1326 false_high_cond = kCondLT;
1327 break;
1328 case kCondGE:
1329 true_high_cond = kCondGT;
1330 break;
Aart Bike9f37602015-10-09 11:15:55 -07001331 case kCondB:
1332 false_high_cond = kCondA;
1333 break;
1334 case kCondBE:
1335 true_high_cond = kCondB;
1336 break;
1337 case kCondA:
1338 false_high_cond = kCondB;
1339 break;
1340 case kCondAE:
1341 true_high_cond = kCondA;
1342 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343 }
1344 if (right.IsConstant()) {
1345 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1346 int32_t val_low = Low32Bits(value);
1347 int32_t val_high = High32Bits(value);
1348
Vladimir Markoac6ac102015-12-17 12:14:00 +00001349 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001350 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001351 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001352 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001353 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001355 __ b(true_label, ARMCondition(true_high_cond));
1356 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001357 }
1358 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001359 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001360 } else {
1361 Register right_high = right.AsRegisterPairHigh<Register>();
1362 Register right_low = right.AsRegisterPairLow<Register>();
1363
1364 __ cmp(left_high, ShifterOperand(right_high));
1365 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001366 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001367 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001368 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001369 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001370 __ b(true_label, ARMCondition(true_high_cond));
1371 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001372 }
1373 // Must be equal high, so compare the lows.
1374 __ cmp(left_low, ShifterOperand(right_low));
1375 }
1376 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001377 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001378 __ b(true_label, final_condition);
1379}
1380
David Brazdil0debae72015-11-12 18:37:00 +00001381void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1382 Label* true_target_in,
1383 Label* false_target_in) {
1384 // Generated branching requires both targets to be explicit. If either of the
1385 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1386 Label fallthrough_target;
1387 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1388 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1389
Roland Levillain4fa13f62015-07-06 18:11:54 +01001390 LocationSummary* locations = condition->GetLocations();
1391 Location left = locations->InAt(0);
1392 Location right = locations->InAt(1);
1393
Roland Levillain4fa13f62015-07-06 18:11:54 +01001394 Primitive::Type type = condition->InputAt(0)->GetType();
1395 switch (type) {
1396 case Primitive::kPrimLong:
1397 GenerateLongComparesAndJumps(condition, true_target, false_target);
1398 break;
1399 case Primitive::kPrimFloat:
1400 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1401 GenerateFPJumps(condition, true_target, false_target);
1402 break;
1403 case Primitive::kPrimDouble:
1404 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1405 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1406 GenerateFPJumps(condition, true_target, false_target);
1407 break;
1408 default:
1409 LOG(FATAL) << "Unexpected compare type " << type;
1410 }
1411
David Brazdil0debae72015-11-12 18:37:00 +00001412 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001413 __ b(false_target);
1414 }
David Brazdil0debae72015-11-12 18:37:00 +00001415
1416 if (fallthrough_target.IsLinked()) {
1417 __ Bind(&fallthrough_target);
1418 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001419}
1420
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001421void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001422 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001423 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001424 Label* false_target) {
1425 HInstruction* cond = instruction->InputAt(condition_input_index);
1426
1427 if (true_target == nullptr && false_target == nullptr) {
1428 // Nothing to do. The code always falls through.
1429 return;
1430 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001431 // Constant condition, statically compared against "true" (integer value 1).
1432 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001433 if (true_target != nullptr) {
1434 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001435 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001436 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001437 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001438 if (false_target != nullptr) {
1439 __ b(false_target);
1440 }
1441 }
1442 return;
1443 }
1444
1445 // The following code generates these patterns:
1446 // (1) true_target == nullptr && false_target != nullptr
1447 // - opposite condition true => branch to false_target
1448 // (2) true_target != nullptr && false_target == nullptr
1449 // - condition true => branch to true_target
1450 // (3) true_target != nullptr && false_target != nullptr
1451 // - condition true => branch to true_target
1452 // - branch to false_target
1453 if (IsBooleanValueOrMaterializedCondition(cond)) {
1454 // Condition has been materialized, compare the output to 0.
1455 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1456 DCHECK(cond_val.IsRegister());
1457 if (true_target == nullptr) {
1458 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1459 } else {
1460 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001461 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001462 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001463 // Condition has not been materialized. Use its inputs as the comparison and
1464 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001465 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001466
1467 // If this is a long or FP comparison that has been folded into
1468 // the HCondition, generate the comparison directly.
1469 Primitive::Type type = condition->InputAt(0)->GetType();
1470 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1471 GenerateCompareTestAndBranch(condition, true_target, false_target);
1472 return;
1473 }
1474
1475 LocationSummary* locations = cond->GetLocations();
1476 DCHECK(locations->InAt(0).IsRegister());
1477 Register left = locations->InAt(0).AsRegister<Register>();
1478 Location right = locations->InAt(1);
1479 if (right.IsRegister()) {
1480 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001481 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001482 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001483 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001484 }
1485 if (true_target == nullptr) {
1486 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1487 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001488 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001489 }
Dave Allison20dfc792014-06-16 20:44:29 -07001490 }
David Brazdil0debae72015-11-12 18:37:00 +00001491
1492 // If neither branch falls through (case 3), the conditional branch to `true_target`
1493 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1494 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001495 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001496 }
1497}
1498
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001499void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1501 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001502 locations->SetInAt(0, Location::RequiresRegister());
1503 }
1504}
1505
1506void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001507 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1508 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1509 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1510 nullptr : codegen_->GetLabelOf(true_successor);
1511 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1512 nullptr : codegen_->GetLabelOf(false_successor);
1513 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001514}
1515
1516void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1517 LocationSummary* locations = new (GetGraph()->GetArena())
1518 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001519 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001520 locations->SetInAt(0, Location::RequiresRegister());
1521 }
1522}
1523
1524void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001525 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001526 GenerateTestAndBranch(deoptimize,
1527 /* condition_input_index */ 0,
1528 slow_path->GetEntryLabel(),
1529 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001530}
Dave Allison20dfc792014-06-16 20:44:29 -07001531
David Brazdil74eb1b22015-12-14 11:44:01 +00001532void LocationsBuilderARM::VisitSelect(HSelect* select) {
1533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1534 if (Primitive::IsFloatingPointType(select->GetType())) {
1535 locations->SetInAt(0, Location::RequiresFpuRegister());
1536 locations->SetInAt(1, Location::RequiresFpuRegister());
1537 } else {
1538 locations->SetInAt(0, Location::RequiresRegister());
1539 locations->SetInAt(1, Location::RequiresRegister());
1540 }
1541 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1542 locations->SetInAt(2, Location::RequiresRegister());
1543 }
1544 locations->SetOut(Location::SameAsFirstInput());
1545}
1546
1547void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1548 LocationSummary* locations = select->GetLocations();
1549 Label false_target;
1550 GenerateTestAndBranch(select,
1551 /* condition_input_index */ 2,
1552 /* true_target */ nullptr,
1553 &false_target);
1554 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1555 __ Bind(&false_target);
1556}
1557
David Srbecky0cf44932015-12-09 14:09:59 +00001558void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1559 new (GetGraph()->GetArena()) LocationSummary(info);
1560}
1561
David Srbeckyd28f4a02016-03-14 17:14:24 +00001562void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1563 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001564}
1565
1566void CodeGeneratorARM::GenerateNop() {
1567 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001568}
1569
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001570void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001571 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001572 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001573 // Handle the long/FP comparisons made in instruction simplification.
1574 switch (cond->InputAt(0)->GetType()) {
1575 case Primitive::kPrimLong:
1576 locations->SetInAt(0, Location::RequiresRegister());
1577 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001578 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001579 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1580 }
1581 break;
1582
1583 case Primitive::kPrimFloat:
1584 case Primitive::kPrimDouble:
1585 locations->SetInAt(0, Location::RequiresFpuRegister());
1586 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001587 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1589 }
1590 break;
1591
1592 default:
1593 locations->SetInAt(0, Location::RequiresRegister());
1594 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001595 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1597 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001598 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001599}
1600
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001601void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001602 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001603 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001604 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001605
1606 LocationSummary* locations = cond->GetLocations();
1607 Location left = locations->InAt(0);
1608 Location right = locations->InAt(1);
1609 Register out = locations->Out().AsRegister<Register>();
1610 Label true_label, false_label;
1611
1612 switch (cond->InputAt(0)->GetType()) {
1613 default: {
1614 // Integer case.
1615 if (right.IsRegister()) {
1616 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1617 } else {
1618 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001619 __ CmpConstant(left.AsRegister<Register>(),
1620 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001621 }
Aart Bike9f37602015-10-09 11:15:55 -07001622 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001623 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001624 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001625 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001626 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001627 return;
1628 }
1629 case Primitive::kPrimLong:
1630 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1631 break;
1632 case Primitive::kPrimFloat:
1633 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1634 GenerateFPJumps(cond, &true_label, &false_label);
1635 break;
1636 case Primitive::kPrimDouble:
1637 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1638 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1639 GenerateFPJumps(cond, &true_label, &false_label);
1640 break;
1641 }
1642
1643 // Convert the jumps into the result.
1644 Label done_label;
1645
1646 // False case: result = 0.
1647 __ Bind(&false_label);
1648 __ LoadImmediate(out, 0);
1649 __ b(&done_label);
1650
1651 // True case: result = 1.
1652 __ Bind(&true_label);
1653 __ LoadImmediate(out, 1);
1654 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001655}
1656
1657void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001658 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001659}
1660
1661void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001662 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001663}
1664
1665void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001666 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001667}
1668
1669void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001670 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001671}
1672
1673void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001674 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001675}
1676
1677void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001678 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001679}
1680
1681void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001682 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001683}
1684
1685void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001686 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001687}
1688
1689void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001690 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001691}
1692
1693void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001694 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001695}
1696
1697void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001698 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001699}
1700
1701void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001703}
1704
Aart Bike9f37602015-10-09 11:15:55 -07001705void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001706 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001707}
1708
1709void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001710 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001711}
1712
1713void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001715}
1716
1717void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001719}
1720
1721void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001723}
1724
1725void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001727}
1728
1729void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001731}
1732
1733void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001735}
1736
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001737void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001738 LocationSummary* locations =
1739 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001740 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001741}
1742
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001743void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001744 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001745}
1746
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001747void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1748 LocationSummary* locations =
1749 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1750 locations->SetOut(Location::ConstantLocation(constant));
1751}
1752
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001753void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001754 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001755}
1756
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001757void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001758 LocationSummary* locations =
1759 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001760 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001761}
1762
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001763void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001764 // Will be generated at use site.
1765}
1766
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001767void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1768 LocationSummary* locations =
1769 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1770 locations->SetOut(Location::ConstantLocation(constant));
1771}
1772
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001773void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001774 // Will be generated at use site.
1775}
1776
1777void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1778 LocationSummary* locations =
1779 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1780 locations->SetOut(Location::ConstantLocation(constant));
1781}
1782
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001783void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001784 // Will be generated at use site.
1785}
1786
Calin Juravle27df7582015-04-17 19:12:31 +01001787void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1788 memory_barrier->SetLocations(nullptr);
1789}
1790
1791void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001792 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001793}
1794
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001796 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797}
1798
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001799void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001800 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001801}
1802
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001803void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001804 LocationSummary* locations =
1805 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001806 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807}
1808
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001809void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001810 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001811}
1812
Calin Juravle175dc732015-08-25 15:42:32 +01001813void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1814 // The trampoline uses the same calling convention as dex calling conventions,
1815 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1816 // the method_idx.
1817 HandleInvoke(invoke);
1818}
1819
1820void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1821 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1822}
1823
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001824void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001825 // Explicit clinit checks triggered by static invokes must have been pruned by
1826 // art::PrepareForRegisterAllocation.
1827 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001828
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001829 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001830 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001831 codegen_->GetInstructionSetFeatures());
1832 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001833 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1834 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1835 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001836 return;
1837 }
1838
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001839 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001840
1841 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1842 if (invoke->HasPcRelativeDexCache()) {
1843 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1844 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001845}
1846
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001847static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1848 if (invoke->GetLocations()->Intrinsified()) {
1849 IntrinsicCodeGeneratorARM intrinsic(codegen);
1850 intrinsic.Dispatch(invoke);
1851 return true;
1852 }
1853 return false;
1854}
1855
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001856void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001857 // Explicit clinit checks triggered by static invokes must have been pruned by
1858 // art::PrepareForRegisterAllocation.
1859 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001860
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001861 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1862 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001863 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001864
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001865 LocationSummary* locations = invoke->GetLocations();
1866 codegen_->GenerateStaticOrDirectCall(
1867 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001868 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001869}
1870
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001871void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001872 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001873 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001874}
1875
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001876void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001877 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001878 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001879 codegen_->GetInstructionSetFeatures());
1880 if (intrinsic.TryDispatch(invoke)) {
1881 return;
1882 }
1883
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001884 HandleInvoke(invoke);
1885}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001886
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001887void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001888 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1889 return;
1890 }
1891
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001892 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001893 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001894 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001895}
1896
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001897void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1898 HandleInvoke(invoke);
1899 // Add the hidden argument.
1900 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1901}
1902
1903void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1904 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001905 LocationSummary* locations = invoke->GetLocations();
1906 Register temp = locations->GetTemp(0).AsRegister<Register>();
1907 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001908 Location receiver = locations->InAt(0);
1909 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1910
Roland Levillain3b359c72015-11-17 19:35:12 +00001911 // Set the hidden argument. This is safe to do this here, as R12
1912 // won't be modified thereafter, before the `blx` (call) instruction.
1913 DCHECK_EQ(R12, hidden_reg);
1914 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001915
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001916 if (receiver.IsStackSlot()) {
1917 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001918 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001919 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1920 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001921 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001923 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001924 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001925 // Instead of simply (possibly) unpoisoning `temp` here, we should
1926 // emit a read barrier for the previous class reference load.
1927 // However this is not required in practice, as this is an
1928 // intermediate/temporary reference and because the current
1929 // concurrent copying collector keeps the from-space memory
1930 // intact/accessible until the end of the marking phase (the
1931 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001932 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001933 __ LoadFromOffset(kLoadWord, temp, temp,
1934 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1935 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00001936 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001937 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001938 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001939 uint32_t entry_point =
1940 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001941 // LR = temp->GetEntryPoint();
1942 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1943 // LR();
1944 __ blx(LR);
1945 DCHECK(!codegen_->IsLeafMethod());
1946 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1947}
1948
Roland Levillain88cb1752014-10-20 16:36:47 +01001949void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1950 LocationSummary* locations =
1951 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1952 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001953 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001954 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1956 break;
1957 }
1958 case Primitive::kPrimLong: {
1959 locations->SetInAt(0, Location::RequiresRegister());
1960 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001961 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001962 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001963
Roland Levillain88cb1752014-10-20 16:36:47 +01001964 case Primitive::kPrimFloat:
1965 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001966 locations->SetInAt(0, Location::RequiresFpuRegister());
1967 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001968 break;
1969
1970 default:
1971 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1972 }
1973}
1974
1975void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1976 LocationSummary* locations = neg->GetLocations();
1977 Location out = locations->Out();
1978 Location in = locations->InAt(0);
1979 switch (neg->GetResultType()) {
1980 case Primitive::kPrimInt:
1981 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001982 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001983 break;
1984
1985 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001986 DCHECK(in.IsRegisterPair());
1987 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1988 __ rsbs(out.AsRegisterPairLow<Register>(),
1989 in.AsRegisterPairLow<Register>(),
1990 ShifterOperand(0));
1991 // We cannot emit an RSC (Reverse Subtract with Carry)
1992 // instruction here, as it does not exist in the Thumb-2
1993 // instruction set. We use the following approach
1994 // using SBC and SUB instead.
1995 //
1996 // out.hi = -C
1997 __ sbc(out.AsRegisterPairHigh<Register>(),
1998 out.AsRegisterPairHigh<Register>(),
1999 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2000 // out.hi = out.hi - in.hi
2001 __ sub(out.AsRegisterPairHigh<Register>(),
2002 out.AsRegisterPairHigh<Register>(),
2003 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2004 break;
2005
Roland Levillain88cb1752014-10-20 16:36:47 +01002006 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002007 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002008 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002009 break;
2010
Roland Levillain88cb1752014-10-20 16:36:47 +01002011 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002012 DCHECK(in.IsFpuRegisterPair());
2013 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2014 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002015 break;
2016
2017 default:
2018 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2019 }
2020}
2021
Roland Levillaindff1f282014-11-05 14:15:05 +00002022void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002023 Primitive::Type result_type = conversion->GetResultType();
2024 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002025 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002026
Roland Levillain5b3ee562015-04-14 16:02:41 +01002027 // The float-to-long, double-to-long and long-to-float type conversions
2028 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002029 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002030 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2031 && result_type == Primitive::kPrimLong)
2032 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002033 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002034 : LocationSummary::kNoCall;
2035 LocationSummary* locations =
2036 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2037
David Brazdilb2bd1c52015-03-25 11:17:37 +00002038 // The Java language does not allow treating boolean as an integral type but
2039 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002040
Roland Levillaindff1f282014-11-05 14:15:05 +00002041 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002042 case Primitive::kPrimByte:
2043 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002044 case Primitive::kPrimLong:
2045 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002046 case Primitive::kPrimBoolean:
2047 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002048 case Primitive::kPrimShort:
2049 case Primitive::kPrimInt:
2050 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002051 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002052 locations->SetInAt(0, Location::RequiresRegister());
2053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected type conversion from " << input_type
2058 << " to " << result_type;
2059 }
2060 break;
2061
Roland Levillain01a8d712014-11-14 16:27:39 +00002062 case Primitive::kPrimShort:
2063 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002064 case Primitive::kPrimLong:
2065 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002066 case Primitive::kPrimBoolean:
2067 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002068 case Primitive::kPrimByte:
2069 case Primitive::kPrimInt:
2070 case Primitive::kPrimChar:
2071 // Processing a Dex `int-to-short' instruction.
2072 locations->SetInAt(0, Location::RequiresRegister());
2073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2074 break;
2075
2076 default:
2077 LOG(FATAL) << "Unexpected type conversion from " << input_type
2078 << " to " << result_type;
2079 }
2080 break;
2081
Roland Levillain946e1432014-11-11 17:35:19 +00002082 case Primitive::kPrimInt:
2083 switch (input_type) {
2084 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002085 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002086 locations->SetInAt(0, Location::Any());
2087 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2088 break;
2089
2090 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002091 // Processing a Dex `float-to-int' instruction.
2092 locations->SetInAt(0, Location::RequiresFpuRegister());
2093 locations->SetOut(Location::RequiresRegister());
2094 locations->AddTemp(Location::RequiresFpuRegister());
2095 break;
2096
Roland Levillain946e1432014-11-11 17:35:19 +00002097 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002098 // Processing a Dex `double-to-int' instruction.
2099 locations->SetInAt(0, Location::RequiresFpuRegister());
2100 locations->SetOut(Location::RequiresRegister());
2101 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002102 break;
2103
2104 default:
2105 LOG(FATAL) << "Unexpected type conversion from " << input_type
2106 << " to " << result_type;
2107 }
2108 break;
2109
Roland Levillaindff1f282014-11-05 14:15:05 +00002110 case Primitive::kPrimLong:
2111 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002112 case Primitive::kPrimBoolean:
2113 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002114 case Primitive::kPrimByte:
2115 case Primitive::kPrimShort:
2116 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002117 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002118 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002119 locations->SetInAt(0, Location::RequiresRegister());
2120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2121 break;
2122
Roland Levillain624279f2014-12-04 11:54:28 +00002123 case Primitive::kPrimFloat: {
2124 // Processing a Dex `float-to-long' instruction.
2125 InvokeRuntimeCallingConvention calling_convention;
2126 locations->SetInAt(0, Location::FpuRegisterLocation(
2127 calling_convention.GetFpuRegisterAt(0)));
2128 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2129 break;
2130 }
2131
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002132 case Primitive::kPrimDouble: {
2133 // Processing a Dex `double-to-long' instruction.
2134 InvokeRuntimeCallingConvention calling_convention;
2135 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2136 calling_convention.GetFpuRegisterAt(0),
2137 calling_convention.GetFpuRegisterAt(1)));
2138 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002139 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002140 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002141
2142 default:
2143 LOG(FATAL) << "Unexpected type conversion from " << input_type
2144 << " to " << result_type;
2145 }
2146 break;
2147
Roland Levillain981e4542014-11-14 11:47:14 +00002148 case Primitive::kPrimChar:
2149 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002150 case Primitive::kPrimLong:
2151 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002152 case Primitive::kPrimBoolean:
2153 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002154 case Primitive::kPrimByte:
2155 case Primitive::kPrimShort:
2156 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002157 // Processing a Dex `int-to-char' instruction.
2158 locations->SetInAt(0, Location::RequiresRegister());
2159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2160 break;
2161
2162 default:
2163 LOG(FATAL) << "Unexpected type conversion from " << input_type
2164 << " to " << result_type;
2165 }
2166 break;
2167
Roland Levillaindff1f282014-11-05 14:15:05 +00002168 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002169 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002170 case Primitive::kPrimBoolean:
2171 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002172 case Primitive::kPrimByte:
2173 case Primitive::kPrimShort:
2174 case Primitive::kPrimInt:
2175 case Primitive::kPrimChar:
2176 // Processing a Dex `int-to-float' instruction.
2177 locations->SetInAt(0, Location::RequiresRegister());
2178 locations->SetOut(Location::RequiresFpuRegister());
2179 break;
2180
Roland Levillain5b3ee562015-04-14 16:02:41 +01002181 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002182 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002183 InvokeRuntimeCallingConvention calling_convention;
2184 locations->SetInAt(0, Location::RegisterPairLocation(
2185 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2186 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002187 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002188 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002189
Roland Levillaincff13742014-11-17 14:32:17 +00002190 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002191 // Processing a Dex `double-to-float' instruction.
2192 locations->SetInAt(0, Location::RequiresFpuRegister());
2193 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002194 break;
2195
2196 default:
2197 LOG(FATAL) << "Unexpected type conversion from " << input_type
2198 << " to " << result_type;
2199 };
2200 break;
2201
Roland Levillaindff1f282014-11-05 14:15:05 +00002202 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002203 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002204 case Primitive::kPrimBoolean:
2205 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002206 case Primitive::kPrimByte:
2207 case Primitive::kPrimShort:
2208 case Primitive::kPrimInt:
2209 case Primitive::kPrimChar:
2210 // Processing a Dex `int-to-double' instruction.
2211 locations->SetInAt(0, Location::RequiresRegister());
2212 locations->SetOut(Location::RequiresFpuRegister());
2213 break;
2214
2215 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002216 // Processing a Dex `long-to-double' instruction.
2217 locations->SetInAt(0, Location::RequiresRegister());
2218 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002219 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002220 locations->AddTemp(Location::RequiresFpuRegister());
2221 break;
2222
Roland Levillaincff13742014-11-17 14:32:17 +00002223 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002224 // Processing a Dex `float-to-double' instruction.
2225 locations->SetInAt(0, Location::RequiresFpuRegister());
2226 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002227 break;
2228
2229 default:
2230 LOG(FATAL) << "Unexpected type conversion from " << input_type
2231 << " to " << result_type;
2232 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002233 break;
2234
2235 default:
2236 LOG(FATAL) << "Unexpected type conversion from " << input_type
2237 << " to " << result_type;
2238 }
2239}
2240
2241void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2242 LocationSummary* locations = conversion->GetLocations();
2243 Location out = locations->Out();
2244 Location in = locations->InAt(0);
2245 Primitive::Type result_type = conversion->GetResultType();
2246 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002247 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002248 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002249 case Primitive::kPrimByte:
2250 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002251 case Primitive::kPrimLong:
2252 // Type conversion from long to byte is a result of code transformations.
2253 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2254 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002255 case Primitive::kPrimBoolean:
2256 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002257 case Primitive::kPrimShort:
2258 case Primitive::kPrimInt:
2259 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002260 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002262 break;
2263
2264 default:
2265 LOG(FATAL) << "Unexpected type conversion from " << input_type
2266 << " to " << result_type;
2267 }
2268 break;
2269
Roland Levillain01a8d712014-11-14 16:27:39 +00002270 case Primitive::kPrimShort:
2271 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002272 case Primitive::kPrimLong:
2273 // Type conversion from long to short is a result of code transformations.
2274 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2275 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002276 case Primitive::kPrimBoolean:
2277 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002278 case Primitive::kPrimByte:
2279 case Primitive::kPrimInt:
2280 case Primitive::kPrimChar:
2281 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002282 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002283 break;
2284
2285 default:
2286 LOG(FATAL) << "Unexpected type conversion from " << input_type
2287 << " to " << result_type;
2288 }
2289 break;
2290
Roland Levillain946e1432014-11-11 17:35:19 +00002291 case Primitive::kPrimInt:
2292 switch (input_type) {
2293 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002294 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002295 DCHECK(out.IsRegister());
2296 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002297 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002298 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002299 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002300 } else {
2301 DCHECK(in.IsConstant());
2302 DCHECK(in.GetConstant()->IsLongConstant());
2303 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002304 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002305 }
2306 break;
2307
Roland Levillain3f8f9362014-12-02 17:45:01 +00002308 case Primitive::kPrimFloat: {
2309 // Processing a Dex `float-to-int' instruction.
2310 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002311 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002312 __ vmovrs(out.AsRegister<Register>(), temp);
2313 break;
2314 }
2315
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002316 case Primitive::kPrimDouble: {
2317 // Processing a Dex `double-to-int' instruction.
2318 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002319 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002320 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002321 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002322 }
Roland Levillain946e1432014-11-11 17:35:19 +00002323
2324 default:
2325 LOG(FATAL) << "Unexpected type conversion from " << input_type
2326 << " to " << result_type;
2327 }
2328 break;
2329
Roland Levillaindff1f282014-11-05 14:15:05 +00002330 case Primitive::kPrimLong:
2331 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002332 case Primitive::kPrimBoolean:
2333 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002334 case Primitive::kPrimByte:
2335 case Primitive::kPrimShort:
2336 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002337 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002338 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002339 DCHECK(out.IsRegisterPair());
2340 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002341 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002342 // Sign extension.
2343 __ Asr(out.AsRegisterPairHigh<Register>(),
2344 out.AsRegisterPairLow<Register>(),
2345 31);
2346 break;
2347
2348 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002349 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002350 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2351 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002352 conversion->GetDexPc(),
2353 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002354 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002355 break;
2356
Roland Levillaindff1f282014-11-05 14:15:05 +00002357 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002358 // Processing a Dex `double-to-long' instruction.
2359 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2360 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002361 conversion->GetDexPc(),
2362 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002363 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 break;
2365
2366 default:
2367 LOG(FATAL) << "Unexpected type conversion from " << input_type
2368 << " to " << result_type;
2369 }
2370 break;
2371
Roland Levillain981e4542014-11-14 11:47:14 +00002372 case Primitive::kPrimChar:
2373 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002374 case Primitive::kPrimLong:
2375 // Type conversion from long to char is a result of code transformations.
2376 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2377 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002378 case Primitive::kPrimBoolean:
2379 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002380 case Primitive::kPrimByte:
2381 case Primitive::kPrimShort:
2382 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002383 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002384 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002385 break;
2386
2387 default:
2388 LOG(FATAL) << "Unexpected type conversion from " << input_type
2389 << " to " << result_type;
2390 }
2391 break;
2392
Roland Levillaindff1f282014-11-05 14:15:05 +00002393 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002394 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002395 case Primitive::kPrimBoolean:
2396 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002397 case Primitive::kPrimByte:
2398 case Primitive::kPrimShort:
2399 case Primitive::kPrimInt:
2400 case Primitive::kPrimChar: {
2401 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002402 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2403 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002404 break;
2405 }
2406
Roland Levillain5b3ee562015-04-14 16:02:41 +01002407 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002408 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002409 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2410 conversion,
2411 conversion->GetDexPc(),
2412 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002413 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002414 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002415
Roland Levillaincff13742014-11-17 14:32:17 +00002416 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002417 // Processing a Dex `double-to-float' instruction.
2418 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2419 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 };
2426 break;
2427
Roland Levillaindff1f282014-11-05 14:15:05 +00002428 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002429 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002430 case Primitive::kPrimBoolean:
2431 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002432 case Primitive::kPrimByte:
2433 case Primitive::kPrimShort:
2434 case Primitive::kPrimInt:
2435 case Primitive::kPrimChar: {
2436 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002438 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2439 out.AsFpuRegisterPairLow<SRegister>());
2440 break;
2441 }
2442
Roland Levillain647b9ed2014-11-27 12:06:00 +00002443 case Primitive::kPrimLong: {
2444 // Processing a Dex `long-to-double' instruction.
2445 Register low = in.AsRegisterPairLow<Register>();
2446 Register high = in.AsRegisterPairHigh<Register>();
2447 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2448 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002449 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002450 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002451 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2452 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002453
Roland Levillain682393c2015-04-14 15:57:52 +01002454 // temp_d = int-to-double(high)
2455 __ vmovsr(temp_s, high);
2456 __ vcvtdi(temp_d, temp_s);
2457 // constant_d = k2Pow32EncodingForDouble
2458 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2459 // out_d = unsigned-to-double(low)
2460 __ vmovsr(out_s, low);
2461 __ vcvtdu(out_d, out_s);
2462 // out_d += temp_d * constant_d
2463 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002464 break;
2465 }
2466
Roland Levillaincff13742014-11-17 14:32:17 +00002467 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002468 // Processing a Dex `float-to-double' instruction.
2469 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2470 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002471 break;
2472
2473 default:
2474 LOG(FATAL) << "Unexpected type conversion from " << input_type
2475 << " to " << result_type;
2476 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002477 break;
2478
2479 default:
2480 LOG(FATAL) << "Unexpected type conversion from " << input_type
2481 << " to " << result_type;
2482 }
2483}
2484
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002485void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002486 LocationSummary* locations =
2487 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002488 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002489 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002490 locations->SetInAt(0, Location::RequiresRegister());
2491 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2493 break;
2494 }
2495
2496 case Primitive::kPrimLong: {
2497 locations->SetInAt(0, Location::RequiresRegister());
2498 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002500 break;
2501 }
2502
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002503 case Primitive::kPrimFloat:
2504 case Primitive::kPrimDouble: {
2505 locations->SetInAt(0, Location::RequiresFpuRegister());
2506 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002507 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002508 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002509 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002510
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002511 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002512 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002514}
2515
2516void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2517 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002518 Location out = locations->Out();
2519 Location first = locations->InAt(0);
2520 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002521 switch (add->GetResultType()) {
2522 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002523 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002524 __ add(out.AsRegister<Register>(),
2525 first.AsRegister<Register>(),
2526 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002527 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002528 __ AddConstant(out.AsRegister<Register>(),
2529 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002531 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002532 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002533
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002534 case Primitive::kPrimLong: {
2535 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002536 __ adds(out.AsRegisterPairLow<Register>(),
2537 first.AsRegisterPairLow<Register>(),
2538 ShifterOperand(second.AsRegisterPairLow<Register>()));
2539 __ adc(out.AsRegisterPairHigh<Register>(),
2540 first.AsRegisterPairHigh<Register>(),
2541 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002542 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002543 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002544
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002545 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002546 __ vadds(out.AsFpuRegister<SRegister>(),
2547 first.AsFpuRegister<SRegister>(),
2548 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002549 break;
2550
2551 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002552 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2553 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2554 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002555 break;
2556
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002557 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002558 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002559 }
2560}
2561
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002562void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002565 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002566 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002567 locations->SetInAt(0, Location::RequiresRegister());
2568 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2570 break;
2571 }
2572
2573 case Primitive::kPrimLong: {
2574 locations->SetInAt(0, Location::RequiresRegister());
2575 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002577 break;
2578 }
Calin Juravle11351682014-10-23 15:38:15 +01002579 case Primitive::kPrimFloat:
2580 case Primitive::kPrimDouble: {
2581 locations->SetInAt(0, Location::RequiresFpuRegister());
2582 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002583 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002584 break;
Calin Juravle11351682014-10-23 15:38:15 +01002585 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002586 default:
Calin Juravle11351682014-10-23 15:38:15 +01002587 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002588 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002589}
2590
2591void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2592 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002593 Location out = locations->Out();
2594 Location first = locations->InAt(0);
2595 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002596 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002597 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002598 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002599 __ sub(out.AsRegister<Register>(),
2600 first.AsRegister<Register>(),
2601 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002602 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ AddConstant(out.AsRegister<Register>(),
2604 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002605 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002606 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002607 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002608 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002609
Calin Juravle11351682014-10-23 15:38:15 +01002610 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002611 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002612 __ subs(out.AsRegisterPairLow<Register>(),
2613 first.AsRegisterPairLow<Register>(),
2614 ShifterOperand(second.AsRegisterPairLow<Register>()));
2615 __ sbc(out.AsRegisterPairHigh<Register>(),
2616 first.AsRegisterPairHigh<Register>(),
2617 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002618 break;
Calin Juravle11351682014-10-23 15:38:15 +01002619 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002620
Calin Juravle11351682014-10-23 15:38:15 +01002621 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002622 __ vsubs(out.AsFpuRegister<SRegister>(),
2623 first.AsFpuRegister<SRegister>(),
2624 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002625 break;
Calin Juravle11351682014-10-23 15:38:15 +01002626 }
2627
2628 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002629 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2630 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2631 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002632 break;
2633 }
2634
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002635
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002636 default:
Calin Juravle11351682014-10-23 15:38:15 +01002637 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002638 }
2639}
2640
Calin Juravle34bacdf2014-10-07 20:23:36 +01002641void LocationsBuilderARM::VisitMul(HMul* mul) {
2642 LocationSummary* locations =
2643 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2644 switch (mul->GetResultType()) {
2645 case Primitive::kPrimInt:
2646 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetInAt(1, Location::RequiresRegister());
2649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002650 break;
2651 }
2652
Calin Juravleb5bfa962014-10-21 18:02:24 +01002653 case Primitive::kPrimFloat:
2654 case Primitive::kPrimDouble: {
2655 locations->SetInAt(0, Location::RequiresFpuRegister());
2656 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002657 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002658 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002659 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002660
2661 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002662 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002663 }
2664}
2665
2666void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2667 LocationSummary* locations = mul->GetLocations();
2668 Location out = locations->Out();
2669 Location first = locations->InAt(0);
2670 Location second = locations->InAt(1);
2671 switch (mul->GetResultType()) {
2672 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002673 __ mul(out.AsRegister<Register>(),
2674 first.AsRegister<Register>(),
2675 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002676 break;
2677 }
2678 case Primitive::kPrimLong: {
2679 Register out_hi = out.AsRegisterPairHigh<Register>();
2680 Register out_lo = out.AsRegisterPairLow<Register>();
2681 Register in1_hi = first.AsRegisterPairHigh<Register>();
2682 Register in1_lo = first.AsRegisterPairLow<Register>();
2683 Register in2_hi = second.AsRegisterPairHigh<Register>();
2684 Register in2_lo = second.AsRegisterPairLow<Register>();
2685
2686 // Extra checks to protect caused by the existence of R1_R2.
2687 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2688 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2689 DCHECK_NE(out_hi, in1_lo);
2690 DCHECK_NE(out_hi, in2_lo);
2691
2692 // input: in1 - 64 bits, in2 - 64 bits
2693 // output: out
2694 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2695 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2696 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2697
2698 // IP <- in1.lo * in2.hi
2699 __ mul(IP, in1_lo, in2_hi);
2700 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2701 __ mla(out_hi, in1_hi, in2_lo, IP);
2702 // out.lo <- (in1.lo * in2.lo)[31:0];
2703 __ umull(out_lo, IP, in1_lo, in2_lo);
2704 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2705 __ add(out_hi, out_hi, ShifterOperand(IP));
2706 break;
2707 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002708
2709 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002710 __ vmuls(out.AsFpuRegister<SRegister>(),
2711 first.AsFpuRegister<SRegister>(),
2712 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002713 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002714 }
2715
2716 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002717 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2718 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2719 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002720 break;
2721 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002722
2723 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002724 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002725 }
2726}
2727
Zheng Xuc6667102015-05-15 16:08:45 +08002728void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2729 DCHECK(instruction->IsDiv() || instruction->IsRem());
2730 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2731
2732 LocationSummary* locations = instruction->GetLocations();
2733 Location second = locations->InAt(1);
2734 DCHECK(second.IsConstant());
2735
2736 Register out = locations->Out().AsRegister<Register>();
2737 Register dividend = locations->InAt(0).AsRegister<Register>();
2738 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2739 DCHECK(imm == 1 || imm == -1);
2740
2741 if (instruction->IsRem()) {
2742 __ LoadImmediate(out, 0);
2743 } else {
2744 if (imm == 1) {
2745 __ Mov(out, dividend);
2746 } else {
2747 __ rsb(out, dividend, ShifterOperand(0));
2748 }
2749 }
2750}
2751
2752void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2753 DCHECK(instruction->IsDiv() || instruction->IsRem());
2754 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2755
2756 LocationSummary* locations = instruction->GetLocations();
2757 Location second = locations->InAt(1);
2758 DCHECK(second.IsConstant());
2759
2760 Register out = locations->Out().AsRegister<Register>();
2761 Register dividend = locations->InAt(0).AsRegister<Register>();
2762 Register temp = locations->GetTemp(0).AsRegister<Register>();
2763 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002764 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002765 int ctz_imm = CTZ(abs_imm);
2766
2767 if (ctz_imm == 1) {
2768 __ Lsr(temp, dividend, 32 - ctz_imm);
2769 } else {
2770 __ Asr(temp, dividend, 31);
2771 __ Lsr(temp, temp, 32 - ctz_imm);
2772 }
2773 __ add(out, temp, ShifterOperand(dividend));
2774
2775 if (instruction->IsDiv()) {
2776 __ Asr(out, out, ctz_imm);
2777 if (imm < 0) {
2778 __ rsb(out, out, ShifterOperand(0));
2779 }
2780 } else {
2781 __ ubfx(out, out, 0, ctz_imm);
2782 __ sub(out, out, ShifterOperand(temp));
2783 }
2784}
2785
2786void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2787 DCHECK(instruction->IsDiv() || instruction->IsRem());
2788 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2789
2790 LocationSummary* locations = instruction->GetLocations();
2791 Location second = locations->InAt(1);
2792 DCHECK(second.IsConstant());
2793
2794 Register out = locations->Out().AsRegister<Register>();
2795 Register dividend = locations->InAt(0).AsRegister<Register>();
2796 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2797 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2798 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2799
2800 int64_t magic;
2801 int shift;
2802 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2803
2804 __ LoadImmediate(temp1, magic);
2805 __ smull(temp2, temp1, dividend, temp1);
2806
2807 if (imm > 0 && magic < 0) {
2808 __ add(temp1, temp1, ShifterOperand(dividend));
2809 } else if (imm < 0 && magic > 0) {
2810 __ sub(temp1, temp1, ShifterOperand(dividend));
2811 }
2812
2813 if (shift != 0) {
2814 __ Asr(temp1, temp1, shift);
2815 }
2816
2817 if (instruction->IsDiv()) {
2818 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2819 } else {
2820 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2821 // TODO: Strength reduction for mls.
2822 __ LoadImmediate(temp2, imm);
2823 __ mls(out, temp1, temp2, dividend);
2824 }
2825}
2826
2827void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(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 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2836 if (imm == 0) {
2837 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2838 } else if (imm == 1 || imm == -1) {
2839 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002840 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002841 DivRemByPowerOfTwo(instruction);
2842 } else {
2843 DCHECK(imm <= -2 || imm >= 2);
2844 GenerateDivRemWithAnyConstant(instruction);
2845 }
2846}
2847
Calin Juravle7c4954d2014-10-28 16:57:40 +00002848void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002849 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2850 if (div->GetResultType() == Primitive::kPrimLong) {
2851 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002852 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002853 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2854 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002855 } else if (div->GetResultType() == Primitive::kPrimInt &&
2856 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2857 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002858 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002859 }
2860
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2862
Calin Juravle7c4954d2014-10-28 16:57:40 +00002863 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002864 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002865 if (div->InputAt(1)->IsConstant()) {
2866 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002867 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002869 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2870 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002871 // No temp register required.
2872 } else {
2873 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002874 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002875 locations->AddTemp(Location::RequiresRegister());
2876 }
2877 }
2878 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002879 locations->SetInAt(0, Location::RequiresRegister());
2880 locations->SetInAt(1, Location::RequiresRegister());
2881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2882 } else {
2883 InvokeRuntimeCallingConvention calling_convention;
2884 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2885 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2886 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2887 // we only need the former.
2888 locations->SetOut(Location::RegisterLocation(R0));
2889 }
Calin Juravled0d48522014-11-04 16:40:20 +00002890 break;
2891 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002892 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002893 InvokeRuntimeCallingConvention calling_convention;
2894 locations->SetInAt(0, Location::RegisterPairLocation(
2895 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2896 locations->SetInAt(1, Location::RegisterPairLocation(
2897 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002898 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002899 break;
2900 }
2901 case Primitive::kPrimFloat:
2902 case Primitive::kPrimDouble: {
2903 locations->SetInAt(0, Location::RequiresFpuRegister());
2904 locations->SetInAt(1, Location::RequiresFpuRegister());
2905 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2906 break;
2907 }
2908
2909 default:
2910 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2911 }
2912}
2913
2914void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2915 LocationSummary* locations = div->GetLocations();
2916 Location out = locations->Out();
2917 Location first = locations->InAt(0);
2918 Location second = locations->InAt(1);
2919
2920 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002921 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002922 if (second.IsConstant()) {
2923 GenerateDivRemConstantIntegral(div);
2924 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002925 __ sdiv(out.AsRegister<Register>(),
2926 first.AsRegister<Register>(),
2927 second.AsRegister<Register>());
2928 } else {
2929 InvokeRuntimeCallingConvention calling_convention;
2930 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2931 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2932 DCHECK_EQ(R0, out.AsRegister<Register>());
2933
2934 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002935 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002936 }
Calin Juravled0d48522014-11-04 16:40:20 +00002937 break;
2938 }
2939
Calin Juravle7c4954d2014-10-28 16:57:40 +00002940 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002941 InvokeRuntimeCallingConvention calling_convention;
2942 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2943 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2944 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2945 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2946 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002947 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002948
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002949 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002950 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002951 break;
2952 }
2953
2954 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002955 __ vdivs(out.AsFpuRegister<SRegister>(),
2956 first.AsFpuRegister<SRegister>(),
2957 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002958 break;
2959 }
2960
2961 case Primitive::kPrimDouble: {
2962 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2963 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2964 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2965 break;
2966 }
2967
2968 default:
2969 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2970 }
2971}
2972
Calin Juravlebacfec32014-11-14 15:54:36 +00002973void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002974 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002975
2976 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002977 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002978 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2979 // sdiv will be replaced by other instruction sequence.
2980 call_kind = LocationSummary::kNoCall;
2981 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2982 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002983 // Have hardware divide instruction for int, do it with three instructions.
2984 call_kind = LocationSummary::kNoCall;
2985 }
2986
Calin Juravlebacfec32014-11-14 15:54:36 +00002987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2988
Calin Juravled2ec87d2014-12-08 14:24:46 +00002989 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002990 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002991 if (rem->InputAt(1)->IsConstant()) {
2992 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002993 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002995 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
2996 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002997 // No temp register required.
2998 } else {
2999 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003000 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003001 locations->AddTemp(Location::RequiresRegister());
3002 }
3003 }
3004 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003005 locations->SetInAt(0, Location::RequiresRegister());
3006 locations->SetInAt(1, Location::RequiresRegister());
3007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3008 locations->AddTemp(Location::RequiresRegister());
3009 } else {
3010 InvokeRuntimeCallingConvention calling_convention;
3011 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3012 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3013 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3014 // we only need the latter.
3015 locations->SetOut(Location::RegisterLocation(R1));
3016 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003017 break;
3018 }
3019 case Primitive::kPrimLong: {
3020 InvokeRuntimeCallingConvention calling_convention;
3021 locations->SetInAt(0, Location::RegisterPairLocation(
3022 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3023 locations->SetInAt(1, Location::RegisterPairLocation(
3024 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3025 // The runtime helper puts the output in R2,R3.
3026 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3027 break;
3028 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003029 case Primitive::kPrimFloat: {
3030 InvokeRuntimeCallingConvention calling_convention;
3031 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3032 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3033 locations->SetOut(Location::FpuRegisterLocation(S0));
3034 break;
3035 }
3036
Calin Juravlebacfec32014-11-14 15:54:36 +00003037 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003038 InvokeRuntimeCallingConvention calling_convention;
3039 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3040 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3041 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3042 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3043 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003044 break;
3045 }
3046
3047 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003048 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003049 }
3050}
3051
3052void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3053 LocationSummary* locations = rem->GetLocations();
3054 Location out = locations->Out();
3055 Location first = locations->InAt(0);
3056 Location second = locations->InAt(1);
3057
Calin Juravled2ec87d2014-12-08 14:24:46 +00003058 Primitive::Type type = rem->GetResultType();
3059 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003060 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003061 if (second.IsConstant()) {
3062 GenerateDivRemConstantIntegral(rem);
3063 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003064 Register reg1 = first.AsRegister<Register>();
3065 Register reg2 = second.AsRegister<Register>();
3066 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003067
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003068 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003069 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003070 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003071 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003072 } else {
3073 InvokeRuntimeCallingConvention calling_convention;
3074 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3075 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3076 DCHECK_EQ(R1, out.AsRegister<Register>());
3077
3078 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003079 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003080 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003081 break;
3082 }
3083
3084 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003085 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003086 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003087 break;
3088 }
3089
Calin Juravled2ec87d2014-12-08 14:24:46 +00003090 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003091 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003092 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003093 break;
3094 }
3095
Calin Juravlebacfec32014-11-14 15:54:36 +00003096 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003097 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003098 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003099 break;
3100 }
3101
3102 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003103 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003104 }
3105}
3106
Calin Juravled0d48522014-11-04 16:40:20 +00003107void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003108 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3109 ? LocationSummary::kCallOnSlowPath
3110 : LocationSummary::kNoCall;
3111 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003112 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003113 if (instruction->HasUses()) {
3114 locations->SetOut(Location::SameAsFirstInput());
3115 }
3116}
3117
3118void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003119 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003120 codegen_->AddSlowPath(slow_path);
3121
3122 LocationSummary* locations = instruction->GetLocations();
3123 Location value = locations->InAt(0);
3124
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003125 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003126 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003127 case Primitive::kPrimByte:
3128 case Primitive::kPrimChar:
3129 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003130 case Primitive::kPrimInt: {
3131 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003132 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003133 } else {
3134 DCHECK(value.IsConstant()) << value;
3135 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3136 __ b(slow_path->GetEntryLabel());
3137 }
3138 }
3139 break;
3140 }
3141 case Primitive::kPrimLong: {
3142 if (value.IsRegisterPair()) {
3143 __ orrs(IP,
3144 value.AsRegisterPairLow<Register>(),
3145 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3146 __ b(slow_path->GetEntryLabel(), EQ);
3147 } else {
3148 DCHECK(value.IsConstant()) << value;
3149 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3150 __ b(slow_path->GetEntryLabel());
3151 }
3152 }
3153 break;
3154 default:
3155 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3156 }
3157 }
Calin Juravled0d48522014-11-04 16:40:20 +00003158}
3159
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003160void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3161 Register in = locations->InAt(0).AsRegister<Register>();
3162 Location rhs = locations->InAt(1);
3163 Register out = locations->Out().AsRegister<Register>();
3164
3165 if (rhs.IsConstant()) {
3166 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3167 // so map all rotations to a +ve. equivalent in that range.
3168 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3169 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3170 if (rot) {
3171 // Rotate, mapping left rotations to right equivalents if necessary.
3172 // (e.g. left by 2 bits == right by 30.)
3173 __ Ror(out, in, rot);
3174 } else if (out != in) {
3175 __ Mov(out, in);
3176 }
3177 } else {
3178 __ Ror(out, in, rhs.AsRegister<Register>());
3179 }
3180}
3181
3182// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3183// rotates by swapping input regs (effectively rotating by the first 32-bits of
3184// a larger rotation) or flipping direction (thus treating larger right/left
3185// rotations as sub-word sized rotations in the other direction) as appropriate.
3186void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3187 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3188 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3189 Location rhs = locations->InAt(1);
3190 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3191 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3192
3193 if (rhs.IsConstant()) {
3194 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3195 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003196 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003197 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3198 // logic below to a simple pair of binary orr.
3199 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3200 if (rot >= kArmBitsPerWord) {
3201 rot -= kArmBitsPerWord;
3202 std::swap(in_reg_hi, in_reg_lo);
3203 }
3204 // Rotate, or mov to out for zero or word size rotations.
3205 if (rot != 0u) {
3206 __ Lsr(out_reg_hi, in_reg_hi, rot);
3207 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3208 __ Lsr(out_reg_lo, in_reg_lo, rot);
3209 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3210 } else {
3211 __ Mov(out_reg_lo, in_reg_lo);
3212 __ Mov(out_reg_hi, in_reg_hi);
3213 }
3214 } else {
3215 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3216 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3217 Label end;
3218 Label shift_by_32_plus_shift_right;
3219
3220 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3221 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3222 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3223 __ b(&shift_by_32_plus_shift_right, CC);
3224
3225 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3226 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3227 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3228 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3229 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3230 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3231 __ Lsr(shift_left, in_reg_hi, shift_right);
3232 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3233 __ b(&end);
3234
3235 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3236 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3237 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3238 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3239 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3240 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3241 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3242 __ Lsl(shift_right, in_reg_hi, shift_left);
3243 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3244
3245 __ Bind(&end);
3246 }
3247}
Roland Levillain22c49222016-03-18 14:04:28 +00003248
3249void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003250 LocationSummary* locations =
3251 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3252 switch (ror->GetResultType()) {
3253 case Primitive::kPrimInt: {
3254 locations->SetInAt(0, Location::RequiresRegister());
3255 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3256 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3257 break;
3258 }
3259 case Primitive::kPrimLong: {
3260 locations->SetInAt(0, Location::RequiresRegister());
3261 if (ror->InputAt(1)->IsConstant()) {
3262 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3263 } else {
3264 locations->SetInAt(1, Location::RequiresRegister());
3265 locations->AddTemp(Location::RequiresRegister());
3266 locations->AddTemp(Location::RequiresRegister());
3267 }
3268 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3269 break;
3270 }
3271 default:
3272 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3273 }
3274}
3275
Roland Levillain22c49222016-03-18 14:04:28 +00003276void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003277 LocationSummary* locations = ror->GetLocations();
3278 Primitive::Type type = ror->GetResultType();
3279 switch (type) {
3280 case Primitive::kPrimInt: {
3281 HandleIntegerRotate(locations);
3282 break;
3283 }
3284 case Primitive::kPrimLong: {
3285 HandleLongRotate(locations);
3286 break;
3287 }
3288 default:
3289 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003290 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003291 }
3292}
3293
Calin Juravle9aec02f2014-11-18 23:06:35 +00003294void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3295 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3296
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003297 LocationSummary* locations =
3298 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003299
3300 switch (op->GetResultType()) {
3301 case Primitive::kPrimInt: {
3302 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003303 if (op->InputAt(1)->IsConstant()) {
3304 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3306 } else {
3307 locations->SetInAt(1, Location::RequiresRegister());
3308 // Make the output overlap, as it will be used to hold the masked
3309 // second input.
3310 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3311 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003312 break;
3313 }
3314 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003315 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003316 if (op->InputAt(1)->IsConstant()) {
3317 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3318 // For simplicity, use kOutputOverlap even though we only require that low registers
3319 // don't clash with high registers which the register allocator currently guarantees.
3320 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3321 } else {
3322 locations->SetInAt(1, Location::RequiresRegister());
3323 locations->AddTemp(Location::RequiresRegister());
3324 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3325 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003326 break;
3327 }
3328 default:
3329 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3330 }
3331}
3332
3333void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3334 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3335
3336 LocationSummary* locations = op->GetLocations();
3337 Location out = locations->Out();
3338 Location first = locations->InAt(0);
3339 Location second = locations->InAt(1);
3340
3341 Primitive::Type type = op->GetResultType();
3342 switch (type) {
3343 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 Register out_reg = out.AsRegister<Register>();
3345 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003346 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003348 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003349 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003350 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003351 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003352 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003353 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003354 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003355 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003356 }
3357 } else {
3358 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003359 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003360 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003361 __ Mov(out_reg, first_reg);
3362 } else if (op->IsShl()) {
3363 __ Lsl(out_reg, first_reg, shift_value);
3364 } else if (op->IsShr()) {
3365 __ Asr(out_reg, first_reg, shift_value);
3366 } else {
3367 __ Lsr(out_reg, first_reg, shift_value);
3368 }
3369 }
3370 break;
3371 }
3372 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003373 Register o_h = out.AsRegisterPairHigh<Register>();
3374 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003375
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003376 Register high = first.AsRegisterPairHigh<Register>();
3377 Register low = first.AsRegisterPairLow<Register>();
3378
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003379 if (second.IsRegister()) {
3380 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003381
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003382 Register second_reg = second.AsRegister<Register>();
3383
3384 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003385 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003386 // Shift the high part
3387 __ Lsl(o_h, high, o_l);
3388 // Shift the low part and `or` what overflew on the high part
3389 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3390 __ Lsr(temp, low, temp);
3391 __ orr(o_h, o_h, ShifterOperand(temp));
3392 // If the shift is > 32 bits, override the high part
3393 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3394 __ it(PL);
3395 __ Lsl(o_h, low, temp, PL);
3396 // Shift the low part
3397 __ Lsl(o_l, low, o_l);
3398 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003399 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003400 // Shift the low part
3401 __ Lsr(o_l, low, o_h);
3402 // Shift the high part and `or` what underflew on the low part
3403 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3404 __ Lsl(temp, high, temp);
3405 __ orr(o_l, o_l, ShifterOperand(temp));
3406 // If the shift is > 32 bits, override the low part
3407 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3408 __ it(PL);
3409 __ Asr(o_l, high, temp, PL);
3410 // Shift the high part
3411 __ Asr(o_h, high, o_h);
3412 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003413 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003414 // same as Shr except we use `Lsr`s and not `Asr`s
3415 __ Lsr(o_l, low, o_h);
3416 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3417 __ Lsl(temp, high, temp);
3418 __ orr(o_l, o_l, ShifterOperand(temp));
3419 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3420 __ it(PL);
3421 __ Lsr(o_l, high, temp, PL);
3422 __ Lsr(o_h, high, o_h);
3423 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003424 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003425 // Register allocator doesn't create partial overlap.
3426 DCHECK_NE(o_l, high);
3427 DCHECK_NE(o_h, low);
3428 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003429 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003430 if (shift_value > 32) {
3431 if (op->IsShl()) {
3432 __ Lsl(o_h, low, shift_value - 32);
3433 __ LoadImmediate(o_l, 0);
3434 } else if (op->IsShr()) {
3435 __ Asr(o_l, high, shift_value - 32);
3436 __ Asr(o_h, high, 31);
3437 } else {
3438 __ Lsr(o_l, high, shift_value - 32);
3439 __ LoadImmediate(o_h, 0);
3440 }
3441 } else if (shift_value == 32) {
3442 if (op->IsShl()) {
3443 __ mov(o_h, ShifterOperand(low));
3444 __ LoadImmediate(o_l, 0);
3445 } else if (op->IsShr()) {
3446 __ mov(o_l, ShifterOperand(high));
3447 __ Asr(o_h, high, 31);
3448 } else {
3449 __ mov(o_l, ShifterOperand(high));
3450 __ LoadImmediate(o_h, 0);
3451 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003452 } else if (shift_value == 1) {
3453 if (op->IsShl()) {
3454 __ Lsls(o_l, low, 1);
3455 __ adc(o_h, high, ShifterOperand(high));
3456 } else if (op->IsShr()) {
3457 __ Asrs(o_h, high, 1);
3458 __ Rrx(o_l, low);
3459 } else {
3460 __ Lsrs(o_h, high, 1);
3461 __ Rrx(o_l, low);
3462 }
3463 } else {
3464 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003465 if (op->IsShl()) {
3466 __ Lsl(o_h, high, shift_value);
3467 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3468 __ Lsl(o_l, low, shift_value);
3469 } else if (op->IsShr()) {
3470 __ Lsr(o_l, low, shift_value);
3471 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3472 __ Asr(o_h, high, shift_value);
3473 } else {
3474 __ Lsr(o_l, low, shift_value);
3475 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3476 __ Lsr(o_h, high, shift_value);
3477 }
3478 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003479 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003480 break;
3481 }
3482 default:
3483 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003484 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003485 }
3486}
3487
3488void LocationsBuilderARM::VisitShl(HShl* shl) {
3489 HandleShift(shl);
3490}
3491
3492void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3493 HandleShift(shl);
3494}
3495
3496void LocationsBuilderARM::VisitShr(HShr* shr) {
3497 HandleShift(shr);
3498}
3499
3500void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3501 HandleShift(shr);
3502}
3503
3504void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3505 HandleShift(ushr);
3506}
3507
3508void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3509 HandleShift(ushr);
3510}
3511
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003512void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003513 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003514 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003515 if (instruction->IsStringAlloc()) {
3516 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3517 } else {
3518 InvokeRuntimeCallingConvention calling_convention;
3519 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3520 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3521 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003522 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003523}
3524
3525void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003526 // Note: if heap poisoning is enabled, the entry point takes cares
3527 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003528 if (instruction->IsStringAlloc()) {
3529 // String is allocated through StringFactory. Call NewEmptyString entry point.
3530 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3531 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3532 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3533 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3534 __ blx(LR);
3535 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3536 } else {
3537 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3538 instruction,
3539 instruction->GetDexPc(),
3540 nullptr);
3541 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3542 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003543}
3544
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003545void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3546 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003548 InvokeRuntimeCallingConvention calling_convention;
3549 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003550 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003551 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003552 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003553}
3554
3555void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3556 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003557 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003558 // Note: if heap poisoning is enabled, the entry point takes cares
3559 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003560 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003561 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003562 instruction->GetDexPc(),
3563 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003564 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003565}
3566
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003567void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003570 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3571 if (location.IsStackSlot()) {
3572 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3573 } else if (location.IsDoubleStackSlot()) {
3574 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003575 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003576 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003577}
3578
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003579void InstructionCodeGeneratorARM::VisitParameterValue(
3580 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003581 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003582}
3583
3584void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3585 LocationSummary* locations =
3586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3587 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3588}
3589
3590void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3591 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003592}
3593
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003594void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003595 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003596 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003597 locations->SetInAt(0, Location::RequiresRegister());
3598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003599}
3600
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003601void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3602 LocationSummary* locations = not_->GetLocations();
3603 Location out = locations->Out();
3604 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003605 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003606 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003608 break;
3609
3610 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003611 __ mvn(out.AsRegisterPairLow<Register>(),
3612 ShifterOperand(in.AsRegisterPairLow<Register>()));
3613 __ mvn(out.AsRegisterPairHigh<Register>(),
3614 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003615 break;
3616
3617 default:
3618 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3619 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003620}
3621
David Brazdil66d126e2015-04-03 16:02:44 +01003622void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3623 LocationSummary* locations =
3624 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3625 locations->SetInAt(0, Location::RequiresRegister());
3626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3627}
3628
3629void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003630 LocationSummary* locations = bool_not->GetLocations();
3631 Location out = locations->Out();
3632 Location in = locations->InAt(0);
3633 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3634}
3635
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003636void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003637 LocationSummary* locations =
3638 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003639 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003640 case Primitive::kPrimBoolean:
3641 case Primitive::kPrimByte:
3642 case Primitive::kPrimShort:
3643 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003644 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003645 case Primitive::kPrimLong: {
3646 locations->SetInAt(0, Location::RequiresRegister());
3647 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003648 // Output overlaps because it is written before doing the low comparison.
3649 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003650 break;
3651 }
3652 case Primitive::kPrimFloat:
3653 case Primitive::kPrimDouble: {
3654 locations->SetInAt(0, Location::RequiresFpuRegister());
3655 locations->SetInAt(1, Location::RequiresFpuRegister());
3656 locations->SetOut(Location::RequiresRegister());
3657 break;
3658 }
3659 default:
3660 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3661 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003662}
3663
3664void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003665 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003667 Location left = locations->InAt(0);
3668 Location right = locations->InAt(1);
3669
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003670 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003671 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003672 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003673 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003674 case Primitive::kPrimBoolean:
3675 case Primitive::kPrimByte:
3676 case Primitive::kPrimShort:
3677 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003678 case Primitive::kPrimInt: {
3679 __ LoadImmediate(out, 0);
3680 __ cmp(left.AsRegister<Register>(),
3681 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3682 less_cond = LT;
3683 break;
3684 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003685 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003686 __ cmp(left.AsRegisterPairHigh<Register>(),
3687 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003688 __ b(&less, LT);
3689 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003690 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003691 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003692 __ cmp(left.AsRegisterPairLow<Register>(),
3693 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003694 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003695 break;
3696 }
3697 case Primitive::kPrimFloat:
3698 case Primitive::kPrimDouble: {
3699 __ LoadImmediate(out, 0);
3700 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003701 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003702 } else {
3703 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3704 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3705 }
3706 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003707 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003708 break;
3709 }
3710 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003711 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003712 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003713 }
Aart Bika19616e2016-02-01 18:57:58 -08003714
Calin Juravleddb7df22014-11-25 20:56:51 +00003715 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003716 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003717
3718 __ Bind(&greater);
3719 __ LoadImmediate(out, 1);
3720 __ b(&done);
3721
3722 __ Bind(&less);
3723 __ LoadImmediate(out, -1);
3724
3725 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003726}
3727
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003728void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003729 LocationSummary* locations =
3730 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003731 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003732 locations->SetInAt(i, Location::Any());
3733 }
3734 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003735}
3736
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003737void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003738 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003739}
3740
Roland Levillainc9285912015-12-18 10:38:42 +00003741void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3742 // TODO (ported from quick): revisit ARM barrier kinds.
3743 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003744 switch (kind) {
3745 case MemBarrierKind::kAnyStore:
3746 case MemBarrierKind::kLoadAny:
3747 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003748 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003749 break;
3750 }
3751 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003752 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003753 break;
3754 }
3755 default:
3756 LOG(FATAL) << "Unexpected memory barrier " << kind;
3757 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003758 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003759}
3760
3761void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3762 uint32_t offset,
3763 Register out_lo,
3764 Register out_hi) {
3765 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003766 // Ensure `out_lo` is different from `addr`, so that loading
3767 // `offset` into `out_lo` does not clutter `addr`.
3768 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003769 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003770 __ add(IP, addr, ShifterOperand(out_lo));
3771 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003772 }
3773 __ ldrexd(out_lo, out_hi, addr);
3774}
3775
3776void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3777 uint32_t offset,
3778 Register value_lo,
3779 Register value_hi,
3780 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003781 Register temp2,
3782 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003783 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003784 if (offset != 0) {
3785 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003786 __ add(IP, addr, ShifterOperand(temp1));
3787 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003788 }
3789 __ Bind(&fail);
3790 // We need a load followed by store. (The address used in a STREX instruction must
3791 // be the same as the address in the most recently executed LDREX instruction.)
3792 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003793 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003794 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003795 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003796}
3797
3798void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3799 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3800
Nicolas Geoffray39468442014-09-02 15:17:15 +01003801 LocationSummary* locations =
3802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003803 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003804
Calin Juravle52c48962014-12-16 17:02:57 +00003805 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003806 if (Primitive::IsFloatingPointType(field_type)) {
3807 locations->SetInAt(1, Location::RequiresFpuRegister());
3808 } else {
3809 locations->SetInAt(1, Location::RequiresRegister());
3810 }
3811
Calin Juravle52c48962014-12-16 17:02:57 +00003812 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003813 bool generate_volatile = field_info.IsVolatile()
3814 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003815 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003816 bool needs_write_barrier =
3817 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003818 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003819 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003820 if (needs_write_barrier) {
3821 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003822 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003823 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003824 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003825 // - registers need to be consecutive
3826 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003827 // We don't test for ARM yet, and the assertion makes sure that we
3828 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003829 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3830
3831 locations->AddTemp(Location::RequiresRegister());
3832 locations->AddTemp(Location::RequiresRegister());
3833 if (field_type == Primitive::kPrimDouble) {
3834 // For doubles we need two more registers to copy the value.
3835 locations->AddTemp(Location::RegisterLocation(R2));
3836 locations->AddTemp(Location::RegisterLocation(R3));
3837 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003838 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839}
3840
Calin Juravle52c48962014-12-16 17:02:57 +00003841void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003842 const FieldInfo& field_info,
3843 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003844 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3845
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003846 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003847 Register base = locations->InAt(0).AsRegister<Register>();
3848 Location value = locations->InAt(1);
3849
3850 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003851 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003852 Primitive::Type field_type = field_info.GetFieldType();
3853 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003854 bool needs_write_barrier =
3855 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003856
3857 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003858 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003859 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003860
3861 switch (field_type) {
3862 case Primitive::kPrimBoolean:
3863 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003864 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003865 break;
3866 }
3867
3868 case Primitive::kPrimShort:
3869 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003870 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003871 break;
3872 }
3873
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003874 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003875 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003876 if (kPoisonHeapReferences && needs_write_barrier) {
3877 // Note that in the case where `value` is a null reference,
3878 // we do not enter this block, as a null reference does not
3879 // need poisoning.
3880 DCHECK_EQ(field_type, Primitive::kPrimNot);
3881 Register temp = locations->GetTemp(0).AsRegister<Register>();
3882 __ Mov(temp, value.AsRegister<Register>());
3883 __ PoisonHeapReference(temp);
3884 __ StoreToOffset(kStoreWord, temp, base, offset);
3885 } else {
3886 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3887 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003888 break;
3889 }
3890
3891 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003892 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003893 GenerateWideAtomicStore(base, offset,
3894 value.AsRegisterPairLow<Register>(),
3895 value.AsRegisterPairHigh<Register>(),
3896 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003897 locations->GetTemp(1).AsRegister<Register>(),
3898 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003899 } else {
3900 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003901 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003902 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003903 break;
3904 }
3905
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003906 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003907 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003908 break;
3909 }
3910
3911 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003912 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003913 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003914 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3915 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3916
3917 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3918
3919 GenerateWideAtomicStore(base, offset,
3920 value_reg_lo,
3921 value_reg_hi,
3922 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003923 locations->GetTemp(3).AsRegister<Register>(),
3924 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003925 } else {
3926 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003927 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003928 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003929 break;
3930 }
3931
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003932 case Primitive::kPrimVoid:
3933 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003934 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003935 }
Calin Juravle52c48962014-12-16 17:02:57 +00003936
Calin Juravle77520bc2015-01-12 18:45:46 +00003937 // Longs and doubles are handled in the switch.
3938 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3939 codegen_->MaybeRecordImplicitNullCheck(instruction);
3940 }
3941
3942 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3943 Register temp = locations->GetTemp(0).AsRegister<Register>();
3944 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003945 codegen_->MarkGCCard(
3946 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003947 }
3948
Calin Juravle52c48962014-12-16 17:02:57 +00003949 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003950 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003951 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003952}
3953
Calin Juravle52c48962014-12-16 17:02:57 +00003954void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3955 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003956
3957 bool object_field_get_with_read_barrier =
3958 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003959 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003960 new (GetGraph()->GetArena()) LocationSummary(instruction,
3961 object_field_get_with_read_barrier ?
3962 LocationSummary::kCallOnSlowPath :
3963 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003964 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003965
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003966 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003967 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003968 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003969 // The output overlaps in case of volatile long: we don't want the
3970 // code generated by GenerateWideAtomicLoad to overwrite the
3971 // object's location. Likewise, in the case of an object field get
3972 // with read barriers enabled, we do not want the load to overwrite
3973 // the object's location, as we need it to emit the read barrier.
3974 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3975 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003976
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003977 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3978 locations->SetOut(Location::RequiresFpuRegister());
3979 } else {
3980 locations->SetOut(Location::RequiresRegister(),
3981 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3982 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003983 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00003984 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003985 // - registers need to be consecutive
3986 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003987 // We don't test for ARM yet, and the assertion makes sure that we
3988 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003989 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3990 locations->AddTemp(Location::RequiresRegister());
3991 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00003992 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3993 // We need a temporary register for the read barrier marking slow
3994 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3995 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003996 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003997}
3998
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003999Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4000 Opcode opcode) {
4001 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4002 if (constant->IsConstant() &&
4003 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4004 return Location::ConstantLocation(constant->AsConstant());
4005 }
4006 return Location::RequiresRegister();
4007}
4008
4009bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4010 Opcode opcode) {
4011 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4012 if (Primitive::Is64BitType(input_cst->GetType())) {
4013 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4014 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4015 } else {
4016 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4017 }
4018}
4019
4020bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4021 ShifterOperand so;
4022 ArmAssembler* assembler = codegen_->GetAssembler();
4023 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4024 return true;
4025 }
4026 Opcode neg_opcode = kNoOperand;
4027 switch (opcode) {
4028 case AND:
4029 neg_opcode = BIC;
4030 break;
4031 case ORR:
4032 neg_opcode = ORN;
4033 break;
4034 default:
4035 return false;
4036 }
4037 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4038}
4039
Calin Juravle52c48962014-12-16 17:02:57 +00004040void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4041 const FieldInfo& field_info) {
4042 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004043
Calin Juravle52c48962014-12-16 17:02:57 +00004044 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004045 Location base_loc = locations->InAt(0);
4046 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004047 Location out = locations->Out();
4048 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004049 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004050 Primitive::Type field_type = field_info.GetFieldType();
4051 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4052
4053 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004054 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004055 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004056 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004057
Roland Levillainc9285912015-12-18 10:38:42 +00004058 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004059 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004060 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004061
Roland Levillainc9285912015-12-18 10:38:42 +00004062 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004063 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004064 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004065
Roland Levillainc9285912015-12-18 10:38:42 +00004066 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004067 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004068 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004069
4070 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004071 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004072 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004073
4074 case Primitive::kPrimNot: {
4075 // /* HeapReference<Object> */ out = *(base + offset)
4076 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4077 Location temp_loc = locations->GetTemp(0);
4078 // Note that a potential implicit null check is handled in this
4079 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4080 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4081 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4082 if (is_volatile) {
4083 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4084 }
4085 } else {
4086 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4087 codegen_->MaybeRecordImplicitNullCheck(instruction);
4088 if (is_volatile) {
4089 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4090 }
4091 // If read barriers are enabled, emit read barriers other than
4092 // Baker's using a slow path (and also unpoison the loaded
4093 // reference, if heap poisoning is enabled).
4094 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4095 }
4096 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004097 }
4098
Roland Levillainc9285912015-12-18 10:38:42 +00004099 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004100 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004101 GenerateWideAtomicLoad(base, offset,
4102 out.AsRegisterPairLow<Register>(),
4103 out.AsRegisterPairHigh<Register>());
4104 } else {
4105 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4106 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004108
Roland Levillainc9285912015-12-18 10:38:42 +00004109 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004110 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004111 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004112
4113 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004114 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004115 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004116 Register lo = locations->GetTemp(0).AsRegister<Register>();
4117 Register hi = locations->GetTemp(1).AsRegister<Register>();
4118 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004119 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004120 __ vmovdrr(out_reg, lo, hi);
4121 } else {
4122 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004123 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004124 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004125 break;
4126 }
4127
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004128 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004129 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004130 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004131 }
Calin Juravle52c48962014-12-16 17:02:57 +00004132
Roland Levillainc9285912015-12-18 10:38:42 +00004133 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4134 // Potential implicit null checks, in the case of reference or
4135 // double fields, are handled in the previous switch statement.
4136 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004137 codegen_->MaybeRecordImplicitNullCheck(instruction);
4138 }
4139
Calin Juravle52c48962014-12-16 17:02:57 +00004140 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004141 if (field_type == Primitive::kPrimNot) {
4142 // Memory barriers, in the case of references, are also handled
4143 // in the previous switch statement.
4144 } else {
4145 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4146 }
Roland Levillain4d027112015-07-01 15:41:14 +01004147 }
Calin Juravle52c48962014-12-16 17:02:57 +00004148}
4149
4150void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4151 HandleFieldSet(instruction, instruction->GetFieldInfo());
4152}
4153
4154void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004155 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004156}
4157
4158void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4159 HandleFieldGet(instruction, instruction->GetFieldInfo());
4160}
4161
4162void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4163 HandleFieldGet(instruction, instruction->GetFieldInfo());
4164}
4165
4166void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4167 HandleFieldGet(instruction, instruction->GetFieldInfo());
4168}
4169
4170void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4171 HandleFieldGet(instruction, instruction->GetFieldInfo());
4172}
4173
4174void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4175 HandleFieldSet(instruction, instruction->GetFieldInfo());
4176}
4177
4178void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004179 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004180}
4181
Calin Juravlee460d1d2015-09-29 04:52:17 +01004182void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4183 HUnresolvedInstanceFieldGet* instruction) {
4184 FieldAccessCallingConventionARM calling_convention;
4185 codegen_->CreateUnresolvedFieldLocationSummary(
4186 instruction, instruction->GetFieldType(), calling_convention);
4187}
4188
4189void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4190 HUnresolvedInstanceFieldGet* instruction) {
4191 FieldAccessCallingConventionARM calling_convention;
4192 codegen_->GenerateUnresolvedFieldAccess(instruction,
4193 instruction->GetFieldType(),
4194 instruction->GetFieldIndex(),
4195 instruction->GetDexPc(),
4196 calling_convention);
4197}
4198
4199void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4200 HUnresolvedInstanceFieldSet* instruction) {
4201 FieldAccessCallingConventionARM calling_convention;
4202 codegen_->CreateUnresolvedFieldLocationSummary(
4203 instruction, instruction->GetFieldType(), calling_convention);
4204}
4205
4206void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4207 HUnresolvedInstanceFieldSet* instruction) {
4208 FieldAccessCallingConventionARM calling_convention;
4209 codegen_->GenerateUnresolvedFieldAccess(instruction,
4210 instruction->GetFieldType(),
4211 instruction->GetFieldIndex(),
4212 instruction->GetDexPc(),
4213 calling_convention);
4214}
4215
4216void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4217 HUnresolvedStaticFieldGet* instruction) {
4218 FieldAccessCallingConventionARM calling_convention;
4219 codegen_->CreateUnresolvedFieldLocationSummary(
4220 instruction, instruction->GetFieldType(), calling_convention);
4221}
4222
4223void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4224 HUnresolvedStaticFieldGet* instruction) {
4225 FieldAccessCallingConventionARM calling_convention;
4226 codegen_->GenerateUnresolvedFieldAccess(instruction,
4227 instruction->GetFieldType(),
4228 instruction->GetFieldIndex(),
4229 instruction->GetDexPc(),
4230 calling_convention);
4231}
4232
4233void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4234 HUnresolvedStaticFieldSet* instruction) {
4235 FieldAccessCallingConventionARM calling_convention;
4236 codegen_->CreateUnresolvedFieldLocationSummary(
4237 instruction, instruction->GetFieldType(), calling_convention);
4238}
4239
4240void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4241 HUnresolvedStaticFieldSet* instruction) {
4242 FieldAccessCallingConventionARM calling_convention;
4243 codegen_->GenerateUnresolvedFieldAccess(instruction,
4244 instruction->GetFieldType(),
4245 instruction->GetFieldIndex(),
4246 instruction->GetDexPc(),
4247 calling_convention);
4248}
4249
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004250void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004251 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4252 ? LocationSummary::kCallOnSlowPath
4253 : LocationSummary::kNoCall;
4254 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004255 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004256 if (instruction->HasUses()) {
4257 locations->SetOut(Location::SameAsFirstInput());
4258 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004259}
4260
Calin Juravle2ae48182016-03-16 14:05:09 +00004261void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4262 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004263 return;
4264 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004265 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004266
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004267 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004268 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004269}
4270
Calin Juravle2ae48182016-03-16 14:05:09 +00004271void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004272 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004273 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274
4275 LocationSummary* locations = instruction->GetLocations();
4276 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004278 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279}
4280
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004281void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004282 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004283}
4284
Artem Serov6c916792016-07-11 14:02:34 +01004285static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4286 switch (type) {
4287 case Primitive::kPrimNot:
4288 return kLoadWord;
4289 case Primitive::kPrimBoolean:
4290 return kLoadUnsignedByte;
4291 case Primitive::kPrimByte:
4292 return kLoadSignedByte;
4293 case Primitive::kPrimChar:
4294 return kLoadUnsignedHalfword;
4295 case Primitive::kPrimShort:
4296 return kLoadSignedHalfword;
4297 case Primitive::kPrimInt:
4298 return kLoadWord;
4299 case Primitive::kPrimLong:
4300 return kLoadWordPair;
4301 case Primitive::kPrimFloat:
4302 return kLoadSWord;
4303 case Primitive::kPrimDouble:
4304 return kLoadDWord;
4305 default:
4306 LOG(FATAL) << "Unreachable type " << type;
4307 UNREACHABLE();
4308 }
4309}
4310
4311static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4312 switch (type) {
4313 case Primitive::kPrimNot:
4314 return kStoreWord;
4315 case Primitive::kPrimBoolean:
4316 case Primitive::kPrimByte:
4317 return kStoreByte;
4318 case Primitive::kPrimChar:
4319 case Primitive::kPrimShort:
4320 return kStoreHalfword;
4321 case Primitive::kPrimInt:
4322 return kStoreWord;
4323 case Primitive::kPrimLong:
4324 return kStoreWordPair;
4325 case Primitive::kPrimFloat:
4326 return kStoreSWord;
4327 case Primitive::kPrimDouble:
4328 return kStoreDWord;
4329 default:
4330 LOG(FATAL) << "Unreachable type " << type;
4331 UNREACHABLE();
4332 }
4333}
4334
4335void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4336 Location out_loc,
4337 Register base,
4338 Register reg_offset,
4339 Condition cond) {
4340 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4341 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4342
4343 switch (type) {
4344 case Primitive::kPrimByte:
4345 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4346 break;
4347 case Primitive::kPrimBoolean:
4348 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4349 break;
4350 case Primitive::kPrimShort:
4351 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4352 break;
4353 case Primitive::kPrimChar:
4354 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4355 break;
4356 case Primitive::kPrimNot:
4357 case Primitive::kPrimInt:
4358 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4359 break;
4360 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4361 case Primitive::kPrimLong:
4362 case Primitive::kPrimFloat:
4363 case Primitive::kPrimDouble:
4364 default:
4365 LOG(FATAL) << "Unreachable type " << type;
4366 UNREACHABLE();
4367 }
4368}
4369
4370void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4371 Location loc,
4372 Register base,
4373 Register reg_offset,
4374 Condition cond) {
4375 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4376 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4377
4378 switch (type) {
4379 case Primitive::kPrimByte:
4380 case Primitive::kPrimBoolean:
4381 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4382 break;
4383 case Primitive::kPrimShort:
4384 case Primitive::kPrimChar:
4385 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4386 break;
4387 case Primitive::kPrimNot:
4388 case Primitive::kPrimInt:
4389 __ str(loc.AsRegister<Register>(), mem_address, cond);
4390 break;
4391 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4392 case Primitive::kPrimLong:
4393 case Primitive::kPrimFloat:
4394 case Primitive::kPrimDouble:
4395 default:
4396 LOG(FATAL) << "Unreachable type " << type;
4397 UNREACHABLE();
4398 }
4399}
4400
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004401void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004402 bool object_array_get_with_read_barrier =
4403 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004404 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004405 new (GetGraph()->GetArena()) LocationSummary(instruction,
4406 object_array_get_with_read_barrier ?
4407 LocationSummary::kCallOnSlowPath :
4408 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004409 locations->SetInAt(0, Location::RequiresRegister());
4410 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004411 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4412 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4413 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004414 // The output overlaps in the case of an object array get with
4415 // read barriers enabled: we do not want the move to overwrite the
4416 // array's location, as we need it to emit the read barrier.
4417 locations->SetOut(
4418 Location::RequiresRegister(),
4419 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004420 }
Roland Levillainc9285912015-12-18 10:38:42 +00004421 // We need a temporary register for the read barrier marking slow
4422 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4423 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4424 locations->AddTemp(Location::RequiresRegister());
4425 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004426}
4427
4428void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4429 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004430 Location obj_loc = locations->InAt(0);
4431 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004433 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004434 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004435 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004436 HInstruction* array_instr = instruction->GetArray();
4437 bool has_intermediate_address = array_instr->IsIntermediateAddress();
4438 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4439 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004440
Roland Levillain4d027112015-07-01 15:41:14 +01004441 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004442 case Primitive::kPrimBoolean:
4443 case Primitive::kPrimByte:
4444 case Primitive::kPrimShort:
4445 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004446 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004447 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004448 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4449 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4450
4451 LoadOperandType load_type = GetLoadOperandType(type);
4452 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004453 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004454 Register temp = IP;
4455
4456 if (has_intermediate_address) {
4457 // We do not need to compute the intermediate address from the array: the
4458 // input instruction has done it already. See the comment in
4459 // `TryExtractArrayAccessAddress()`.
4460 if (kIsDebugBuild) {
4461 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4462 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4463 }
4464 temp = obj;
4465 } else {
4466 __ add(temp, obj, ShifterOperand(data_offset));
4467 }
4468 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004469 }
4470 break;
4471 }
4472
Roland Levillainc9285912015-12-18 10:38:42 +00004473 case Primitive::kPrimNot: {
4474 static_assert(
4475 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4476 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004477 // /* HeapReference<Object> */ out =
4478 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4479 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4480 Location temp = locations->GetTemp(0);
4481 // Note that a potential implicit null check is handled in this
4482 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4483 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4484 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4485 } else {
4486 Register out = out_loc.AsRegister<Register>();
4487 if (index.IsConstant()) {
4488 size_t offset =
4489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4490 __ LoadFromOffset(kLoadWord, out, obj, offset);
4491 codegen_->MaybeRecordImplicitNullCheck(instruction);
4492 // If read barriers are enabled, emit read barriers other than
4493 // Baker's using a slow path (and also unpoison the loaded
4494 // reference, if heap poisoning is enabled).
4495 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4496 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004497 Register temp = IP;
4498
4499 if (has_intermediate_address) {
4500 // We do not need to compute the intermediate address from the array: the
4501 // input instruction has done it already. See the comment in
4502 // `TryExtractArrayAccessAddress()`.
4503 if (kIsDebugBuild) {
4504 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4505 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4506 }
4507 temp = obj;
4508 } else {
4509 __ add(temp, obj, ShifterOperand(data_offset));
4510 }
4511 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004512
Roland Levillainc9285912015-12-18 10:38:42 +00004513 codegen_->MaybeRecordImplicitNullCheck(instruction);
4514 // If read barriers are enabled, emit read barriers other than
4515 // Baker's using a slow path (and also unpoison the loaded
4516 // reference, if heap poisoning is enabled).
4517 codegen_->MaybeGenerateReadBarrierSlow(
4518 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4519 }
4520 }
4521 break;
4522 }
4523
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004524 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004525 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004526 size_t offset =
4527 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004528 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004530 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004531 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532 }
4533 break;
4534 }
4535
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004536 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004537 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004538 if (index.IsConstant()) {
4539 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004540 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004541 } else {
4542 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004543 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004544 }
4545 break;
4546 }
4547
4548 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004549 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004550 if (index.IsConstant()) {
4551 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004552 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004553 } else {
4554 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004555 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004556 }
4557 break;
4558 }
4559
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004560 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004561 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004562 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004563 }
Roland Levillain4d027112015-07-01 15:41:14 +01004564
4565 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004566 // Potential implicit null checks, in the case of reference
4567 // arrays, are handled in the previous switch statement.
4568 } else {
4569 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004570 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004571}
4572
4573void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004574 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004575
4576 bool needs_write_barrier =
4577 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004578 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4579 bool object_array_set_with_read_barrier =
4580 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004581
Nicolas Geoffray39468442014-09-02 15:17:15 +01004582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004583 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004584 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4585 LocationSummary::kCallOnSlowPath :
4586 LocationSummary::kNoCall);
4587
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004588 locations->SetInAt(0, Location::RequiresRegister());
4589 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4590 if (Primitive::IsFloatingPointType(value_type)) {
4591 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004593 locations->SetInAt(2, Location::RequiresRegister());
4594 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004595 if (needs_write_barrier) {
4596 // Temporary registers for the write barrier.
4597 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004598 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004599 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004600}
4601
4602void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4603 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004604 Location array_loc = locations->InAt(0);
4605 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004606 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004607 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004608 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004609 bool needs_write_barrier =
4610 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004611 uint32_t data_offset =
4612 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4613 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004614 HInstruction* array_instr = instruction->GetArray();
4615 bool has_intermediate_address = array_instr->IsIntermediateAddress();
4616 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4617 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004618
4619 switch (value_type) {
4620 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004621 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004623 case Primitive::kPrimChar:
4624 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004625 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004626 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4627 uint32_t full_offset =
4628 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4629 StoreOperandType store_type = GetStoreOperandType(value_type);
4630 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004631 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004632 Register temp = IP;
4633
4634 if (has_intermediate_address) {
4635 // We do not need to compute the intermediate address from the array: the
4636 // input instruction has done it already. See the comment in
4637 // `TryExtractArrayAccessAddress()`.
4638 if (kIsDebugBuild) {
4639 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4640 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4641 }
4642 temp = array;
4643 } else {
4644 __ add(temp, array, ShifterOperand(data_offset));
4645 }
Artem Serov6c916792016-07-11 14:02:34 +01004646 codegen_->StoreToShiftedRegOffset(value_type,
4647 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004648 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004649 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004650 }
4651 break;
4652 }
4653
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004654 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004655 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004656 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4657 // See the comment in instruction_simplifier_shared.cc.
4658 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004659
4660 if (instruction->InputAt(2)->IsNullConstant()) {
4661 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004662 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004663 size_t offset =
4664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004665 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004666 } else {
4667 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004668 __ add(IP, array, ShifterOperand(data_offset));
4669 codegen_->StoreToShiftedRegOffset(value_type,
4670 value_loc,
4671 IP,
4672 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004673 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004674 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004675 DCHECK(!needs_write_barrier);
4676 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004677 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004678 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004679
4680 DCHECK(needs_write_barrier);
4681 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4682 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4683 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4684 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4685 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4686 Label done;
4687 SlowPathCode* slow_path = nullptr;
4688
Roland Levillain3b359c72015-11-17 19:35:12 +00004689 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004690 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4691 codegen_->AddSlowPath(slow_path);
4692 if (instruction->GetValueCanBeNull()) {
4693 Label non_zero;
4694 __ CompareAndBranchIfNonZero(value, &non_zero);
4695 if (index.IsConstant()) {
4696 size_t offset =
4697 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4698 __ StoreToOffset(kStoreWord, value, array, offset);
4699 } else {
4700 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004701 __ add(IP, array, ShifterOperand(data_offset));
4702 codegen_->StoreToShiftedRegOffset(value_type,
4703 value_loc,
4704 IP,
4705 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004706 }
4707 codegen_->MaybeRecordImplicitNullCheck(instruction);
4708 __ b(&done);
4709 __ Bind(&non_zero);
4710 }
4711
Roland Levillain3b359c72015-11-17 19:35:12 +00004712 if (kEmitCompilerReadBarrier) {
4713 // When read barriers are enabled, the type checking
4714 // instrumentation requires two read barriers:
4715 //
4716 // __ Mov(temp2, temp1);
4717 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4718 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004719 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004720 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4721 //
4722 // // /* HeapReference<Class> */ temp2 = value->klass_
4723 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004724 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004725 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4726 //
4727 // __ cmp(temp1, ShifterOperand(temp2));
4728 //
4729 // However, the second read barrier may trash `temp`, as it
4730 // is a temporary register, and as such would not be saved
4731 // along with live registers before calling the runtime (nor
4732 // restored afterwards). So in this case, we bail out and
4733 // delegate the work to the array set slow path.
4734 //
4735 // TODO: Extend the register allocator to support a new
4736 // "(locally) live temp" location so as to avoid always
4737 // going into the slow path when read barriers are enabled.
4738 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004739 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004740 // /* HeapReference<Class> */ temp1 = array->klass_
4741 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4742 codegen_->MaybeRecordImplicitNullCheck(instruction);
4743 __ MaybeUnpoisonHeapReference(temp1);
4744
4745 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4746 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4747 // /* HeapReference<Class> */ temp2 = value->klass_
4748 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4749 // If heap poisoning is enabled, no need to unpoison `temp1`
4750 // nor `temp2`, as we are comparing two poisoned references.
4751 __ cmp(temp1, ShifterOperand(temp2));
4752
4753 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4754 Label do_put;
4755 __ b(&do_put, EQ);
4756 // If heap poisoning is enabled, the `temp1` reference has
4757 // not been unpoisoned yet; unpoison it now.
4758 __ MaybeUnpoisonHeapReference(temp1);
4759
4760 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4761 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4762 // If heap poisoning is enabled, no need to unpoison
4763 // `temp1`, as we are comparing against null below.
4764 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4765 __ Bind(&do_put);
4766 } else {
4767 __ b(slow_path->GetEntryLabel(), NE);
4768 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004769 }
4770 }
4771
Artem Serov6c916792016-07-11 14:02:34 +01004772 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 if (kPoisonHeapReferences) {
4774 // Note that in the case where `value` is a null reference,
4775 // we do not enter this block, as a null reference does not
4776 // need poisoning.
4777 DCHECK_EQ(value_type, Primitive::kPrimNot);
4778 __ Mov(temp1, value);
4779 __ PoisonHeapReference(temp1);
4780 source = temp1;
4781 }
4782
4783 if (index.IsConstant()) {
4784 size_t offset =
4785 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4786 __ StoreToOffset(kStoreWord, source, array, offset);
4787 } else {
4788 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004789
4790 __ add(IP, array, ShifterOperand(data_offset));
4791 codegen_->StoreToShiftedRegOffset(value_type,
4792 Location::RegisterLocation(source),
4793 IP,
4794 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004795 }
4796
Roland Levillain3b359c72015-11-17 19:35:12 +00004797 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004798 codegen_->MaybeRecordImplicitNullCheck(instruction);
4799 }
4800
4801 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4802
4803 if (done.IsLinked()) {
4804 __ Bind(&done);
4805 }
4806
4807 if (slow_path != nullptr) {
4808 __ Bind(slow_path->GetExitLabel());
4809 }
4810
4811 break;
4812 }
4813
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004814 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004815 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004816 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004817 size_t offset =
4818 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004819 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004820 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004821 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004822 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004823 }
4824 break;
4825 }
4826
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004827 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004828 Location value = locations->InAt(2);
4829 DCHECK(value.IsFpuRegister());
4830 if (index.IsConstant()) {
4831 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004833 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004834 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004835 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4836 }
4837 break;
4838 }
4839
4840 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004841 Location value = locations->InAt(2);
4842 DCHECK(value.IsFpuRegisterPair());
4843 if (index.IsConstant()) {
4844 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004845 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004846 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004847 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004848 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4849 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004850
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004851 break;
4852 }
4853
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004854 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004855 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004856 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004858
Roland Levillain80e67092016-01-08 16:04:55 +00004859 // Objects are handled in the switch.
4860 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004861 codegen_->MaybeRecordImplicitNullCheck(instruction);
4862 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004863}
4864
4865void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004866 LocationSummary* locations =
4867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004868 locations->SetInAt(0, Location::RequiresRegister());
4869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870}
4871
4872void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4873 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004874 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004875 Register obj = locations->InAt(0).AsRegister<Register>();
4876 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004878 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879}
4880
Artem Serov328429f2016-07-06 16:23:04 +01004881void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4882 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4883 DCHECK(!kEmitCompilerReadBarrier);
4884 LocationSummary* locations =
4885 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4886
4887 locations->SetInAt(0, Location::RequiresRegister());
4888 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4890}
4891
4892void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4893 LocationSummary* locations = instruction->GetLocations();
4894 Location out = locations->Out();
4895 Location first = locations->InAt(0);
4896 Location second = locations->InAt(1);
4897
4898 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4899 DCHECK(!kEmitCompilerReadBarrier);
4900
4901 if (second.IsRegister()) {
4902 __ add(out.AsRegister<Register>(),
4903 first.AsRegister<Register>(),
4904 ShifterOperand(second.AsRegister<Register>()));
4905 } else {
4906 __ AddConstant(out.AsRegister<Register>(),
4907 first.AsRegister<Register>(),
4908 second.GetConstant()->AsIntConstant()->GetValue());
4909 }
4910}
4911
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004912void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004913 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4914 ? LocationSummary::kCallOnSlowPath
4915 : LocationSummary::kNoCall;
4916 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004917 locations->SetInAt(0, Location::RequiresRegister());
4918 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004919 if (instruction->HasUses()) {
4920 locations->SetOut(Location::SameAsFirstInput());
4921 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004922}
4923
4924void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4925 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004926 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004927 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 codegen_->AddSlowPath(slow_path);
4929
Roland Levillain271ab9c2014-11-27 15:23:57 +00004930 Register index = locations->InAt(0).AsRegister<Register>();
4931 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004932
4933 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004934 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004935}
4936
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004937void CodeGeneratorARM::MarkGCCard(Register temp,
4938 Register card,
4939 Register object,
4940 Register value,
4941 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004942 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004943 if (can_be_null) {
4944 __ CompareAndBranchIfZero(value, &is_null);
4945 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4947 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4948 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004949 if (can_be_null) {
4950 __ Bind(&is_null);
4951 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004952}
4953
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004954void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004955 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004956}
4957
4958void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004959 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4960}
4961
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004962void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4964}
4965
4966void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004967 HBasicBlock* block = instruction->GetBlock();
4968 if (block->GetLoopInformation() != nullptr) {
4969 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4970 // The back edge will generate the suspend check.
4971 return;
4972 }
4973 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4974 // The goto will generate the suspend check.
4975 return;
4976 }
4977 GenerateSuspendCheck(instruction, nullptr);
4978}
4979
4980void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4981 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004982 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004983 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4984 if (slow_path == nullptr) {
4985 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4986 instruction->SetSlowPath(slow_path);
4987 codegen_->AddSlowPath(slow_path);
4988 if (successor != nullptr) {
4989 DCHECK(successor->IsLoopHeader());
4990 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4991 }
4992 } else {
4993 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4994 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004995
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004996 __ LoadFromOffset(
4997 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004998 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004999 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005000 __ Bind(slow_path->GetReturnLabel());
5001 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005002 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005003 __ b(slow_path->GetEntryLabel());
5004 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005005}
5006
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005007ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5008 return codegen_->GetAssembler();
5009}
5010
5011void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005012 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005013 Location source = move->GetSource();
5014 Location destination = move->GetDestination();
5015
5016 if (source.IsRegister()) {
5017 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005018 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005019 } else if (destination.IsFpuRegister()) {
5020 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005021 } else {
5022 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005023 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005024 SP, destination.GetStackIndex());
5025 }
5026 } else if (source.IsStackSlot()) {
5027 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005028 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005029 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005030 } else if (destination.IsFpuRegister()) {
5031 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005032 } else {
5033 DCHECK(destination.IsStackSlot());
5034 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5035 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5036 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005037 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005038 if (destination.IsRegister()) {
5039 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5040 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005041 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005042 } else {
5043 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005044 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5045 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005046 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005047 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005048 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5049 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005050 } else if (destination.IsRegisterPair()) {
5051 DCHECK(ExpectedPairLayout(destination));
5052 __ LoadFromOffset(
5053 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5054 } else {
5055 DCHECK(destination.IsFpuRegisterPair()) << destination;
5056 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5057 SP,
5058 source.GetStackIndex());
5059 }
5060 } else if (source.IsRegisterPair()) {
5061 if (destination.IsRegisterPair()) {
5062 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5063 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005064 } else if (destination.IsFpuRegisterPair()) {
5065 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5066 source.AsRegisterPairLow<Register>(),
5067 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005068 } else {
5069 DCHECK(destination.IsDoubleStackSlot()) << destination;
5070 DCHECK(ExpectedPairLayout(source));
5071 __ StoreToOffset(
5072 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5073 }
5074 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005075 if (destination.IsRegisterPair()) {
5076 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5077 destination.AsRegisterPairHigh<Register>(),
5078 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5079 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005080 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5081 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5082 } else {
5083 DCHECK(destination.IsDoubleStackSlot()) << destination;
5084 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5085 SP,
5086 destination.GetStackIndex());
5087 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005088 } else {
5089 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005090 HConstant* constant = source.GetConstant();
5091 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5092 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005093 if (destination.IsRegister()) {
5094 __ LoadImmediate(destination.AsRegister<Register>(), value);
5095 } else {
5096 DCHECK(destination.IsStackSlot());
5097 __ LoadImmediate(IP, value);
5098 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5099 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005100 } else if (constant->IsLongConstant()) {
5101 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005102 if (destination.IsRegisterPair()) {
5103 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5104 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005105 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005106 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005107 __ LoadImmediate(IP, Low32Bits(value));
5108 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5109 __ LoadImmediate(IP, High32Bits(value));
5110 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5111 }
5112 } else if (constant->IsDoubleConstant()) {
5113 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005114 if (destination.IsFpuRegisterPair()) {
5115 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005116 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005117 DCHECK(destination.IsDoubleStackSlot()) << destination;
5118 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005119 __ LoadImmediate(IP, Low32Bits(int_value));
5120 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5121 __ LoadImmediate(IP, High32Bits(int_value));
5122 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5123 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005124 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005125 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005126 float value = constant->AsFloatConstant()->GetValue();
5127 if (destination.IsFpuRegister()) {
5128 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5129 } else {
5130 DCHECK(destination.IsStackSlot());
5131 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5132 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5133 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005134 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005135 }
5136}
5137
5138void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5139 __ Mov(IP, reg);
5140 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5141 __ StoreToOffset(kStoreWord, IP, SP, mem);
5142}
5143
5144void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5145 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5146 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5147 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5148 SP, mem1 + stack_offset);
5149 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5150 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5151 SP, mem2 + stack_offset);
5152 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5153}
5154
5155void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005156 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005157 Location source = move->GetSource();
5158 Location destination = move->GetDestination();
5159
5160 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005161 DCHECK_NE(source.AsRegister<Register>(), IP);
5162 DCHECK_NE(destination.AsRegister<Register>(), IP);
5163 __ Mov(IP, source.AsRegister<Register>());
5164 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5165 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005166 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005167 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005168 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005169 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005170 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5171 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005172 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005173 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005174 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005175 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005176 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005177 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005178 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005179 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005180 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5181 destination.AsRegisterPairHigh<Register>(),
5182 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005183 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005184 Register low_reg = source.IsRegisterPair()
5185 ? source.AsRegisterPairLow<Register>()
5186 : destination.AsRegisterPairLow<Register>();
5187 int mem = source.IsRegisterPair()
5188 ? destination.GetStackIndex()
5189 : source.GetStackIndex();
5190 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005191 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005192 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005193 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005194 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005195 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5196 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005197 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005198 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005199 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005200 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5201 DRegister reg = source.IsFpuRegisterPair()
5202 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5203 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5204 int mem = source.IsFpuRegisterPair()
5205 ? destination.GetStackIndex()
5206 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005207 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005208 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005209 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005210 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5211 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5212 : destination.AsFpuRegister<SRegister>();
5213 int mem = source.IsFpuRegister()
5214 ? destination.GetStackIndex()
5215 : source.GetStackIndex();
5216
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005217 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005218 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005219 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005220 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005221 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5222 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005223 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005224 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005225 }
5226}
5227
5228void ParallelMoveResolverARM::SpillScratch(int reg) {
5229 __ Push(static_cast<Register>(reg));
5230}
5231
5232void ParallelMoveResolverARM::RestoreScratch(int reg) {
5233 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005234}
5235
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005236HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5237 HLoadClass::LoadKind desired_class_load_kind) {
5238 if (kEmitCompilerReadBarrier) {
5239 switch (desired_class_load_kind) {
5240 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5241 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5242 case HLoadClass::LoadKind::kBootImageAddress:
5243 // TODO: Implement for read barrier.
5244 return HLoadClass::LoadKind::kDexCacheViaMethod;
5245 default:
5246 break;
5247 }
5248 }
5249 switch (desired_class_load_kind) {
5250 case HLoadClass::LoadKind::kReferrersClass:
5251 break;
5252 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5253 DCHECK(!GetCompilerOptions().GetCompilePic());
5254 break;
5255 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5256 DCHECK(GetCompilerOptions().GetCompilePic());
5257 break;
5258 case HLoadClass::LoadKind::kBootImageAddress:
5259 break;
5260 case HLoadClass::LoadKind::kDexCacheAddress:
5261 DCHECK(Runtime::Current()->UseJitCompilation());
5262 break;
5263 case HLoadClass::LoadKind::kDexCachePcRelative:
5264 DCHECK(!Runtime::Current()->UseJitCompilation());
5265 // We disable pc-relative load when there is an irreducible loop, as the optimization
5266 // is incompatible with it.
5267 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5268 // with irreducible loops.
5269 if (GetGraph()->HasIrreducibleLoops()) {
5270 return HLoadClass::LoadKind::kDexCacheViaMethod;
5271 }
5272 break;
5273 case HLoadClass::LoadKind::kDexCacheViaMethod:
5274 break;
5275 }
5276 return desired_class_load_kind;
5277}
5278
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005279void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005280 if (cls->NeedsAccessCheck()) {
5281 InvokeRuntimeCallingConvention calling_convention;
5282 CodeGenerator::CreateLoadClassLocationSummary(
5283 cls,
5284 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5285 Location::RegisterLocation(R0),
5286 /* code_generator_supports_read_barrier */ true);
5287 return;
5288 }
5289
5290 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5291 ? LocationSummary::kCallOnSlowPath
5292 : LocationSummary::kNoCall;
5293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5294 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5295 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5296 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5297 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5298 locations->SetInAt(0, Location::RequiresRegister());
5299 }
5300 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005301}
5302
5303void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005304 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005305 if (cls->NeedsAccessCheck()) {
5306 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5307 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5308 cls,
5309 cls->GetDexPc(),
5310 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005311 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005312 return;
5313 }
5314
Roland Levillain3b359c72015-11-17 19:35:12 +00005315 Location out_loc = locations->Out();
5316 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005317
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005318 bool generate_null_check = false;
5319 switch (cls->GetLoadKind()) {
5320 case HLoadClass::LoadKind::kReferrersClass: {
5321 DCHECK(!cls->CanCallRuntime());
5322 DCHECK(!cls->MustGenerateClinitCheck());
5323 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5324 Register current_method = locations->InAt(0).AsRegister<Register>();
5325 GenerateGcRootFieldLoad(
5326 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5327 break;
5328 }
5329 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5330 DCHECK(!kEmitCompilerReadBarrier);
5331 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5332 cls->GetTypeIndex()));
5333 break;
5334 }
5335 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5336 DCHECK(!kEmitCompilerReadBarrier);
5337 CodeGeneratorARM::PcRelativePatchInfo* labels =
5338 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5339 __ BindTrackedLabel(&labels->movw_label);
5340 __ movw(out, /* placeholder */ 0u);
5341 __ BindTrackedLabel(&labels->movt_label);
5342 __ movt(out, /* placeholder */ 0u);
5343 __ BindTrackedLabel(&labels->add_pc_label);
5344 __ add(out, out, ShifterOperand(PC));
5345 break;
5346 }
5347 case HLoadClass::LoadKind::kBootImageAddress: {
5348 DCHECK(!kEmitCompilerReadBarrier);
5349 DCHECK_NE(cls->GetAddress(), 0u);
5350 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5351 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5352 break;
5353 }
5354 case HLoadClass::LoadKind::kDexCacheAddress: {
5355 DCHECK_NE(cls->GetAddress(), 0u);
5356 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5357 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5358 // a 128B range. To try and reduce the number of literals if we load multiple types,
5359 // simply split the dex cache address to a 128B aligned base loaded from a literal
5360 // and the remaining offset embedded in the load.
5361 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5362 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5363 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5364 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5365 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5366 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5367 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5368 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5369 generate_null_check = !cls->IsInDexCache();
5370 break;
5371 }
5372 case HLoadClass::LoadKind::kDexCachePcRelative: {
5373 Register base_reg = locations->InAt(0).AsRegister<Register>();
5374 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5375 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5376 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5377 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5378 generate_null_check = !cls->IsInDexCache();
5379 break;
5380 }
5381 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5382 // /* GcRoot<mirror::Class>[] */ out =
5383 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5384 Register current_method = locations->InAt(0).AsRegister<Register>();
5385 __ LoadFromOffset(kLoadWord,
5386 out,
5387 current_method,
5388 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5389 // /* GcRoot<mirror::Class> */ out = out[type_index]
5390 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5391 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5392 generate_null_check = !cls->IsInDexCache();
5393 }
5394 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005395
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005396 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5397 DCHECK(cls->CanCallRuntime());
5398 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5399 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5400 codegen_->AddSlowPath(slow_path);
5401 if (generate_null_check) {
5402 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5403 }
5404 if (cls->MustGenerateClinitCheck()) {
5405 GenerateClassInitializationCheck(slow_path, out);
5406 } else {
5407 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005408 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005409 }
5410}
5411
5412void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5413 LocationSummary* locations =
5414 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5415 locations->SetInAt(0, Location::RequiresRegister());
5416 if (check->HasUses()) {
5417 locations->SetOut(Location::SameAsFirstInput());
5418 }
5419}
5420
5421void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005422 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005423 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005424 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005425 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005426 GenerateClassInitializationCheck(slow_path,
5427 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005428}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005429
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005430void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005431 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005432 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5433 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5434 __ b(slow_path->GetEntryLabel(), LT);
5435 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5436 // properly. Therefore, we do a memory fence.
5437 __ dmb(ISH);
5438 __ Bind(slow_path->GetExitLabel());
5439}
5440
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005441HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5442 HLoadString::LoadKind desired_string_load_kind) {
5443 if (kEmitCompilerReadBarrier) {
5444 switch (desired_string_load_kind) {
5445 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5446 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5447 case HLoadString::LoadKind::kBootImageAddress:
5448 // TODO: Implement for read barrier.
5449 return HLoadString::LoadKind::kDexCacheViaMethod;
5450 default:
5451 break;
5452 }
5453 }
5454 switch (desired_string_load_kind) {
5455 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5456 DCHECK(!GetCompilerOptions().GetCompilePic());
5457 break;
5458 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5459 DCHECK(GetCompilerOptions().GetCompilePic());
5460 break;
5461 case HLoadString::LoadKind::kBootImageAddress:
5462 break;
5463 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005464 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005465 break;
5466 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005467 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005468 // We disable pc-relative load when there is an irreducible loop, as the optimization
5469 // is incompatible with it.
5470 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5471 // with irreducible loops.
5472 if (GetGraph()->HasIrreducibleLoops()) {
5473 return HLoadString::LoadKind::kDexCacheViaMethod;
5474 }
5475 break;
5476 case HLoadString::LoadKind::kDexCacheViaMethod:
5477 break;
5478 }
5479 return desired_string_load_kind;
5480}
5481
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005482void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005483 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005484 ? LocationSummary::kCallOnSlowPath
5485 : LocationSummary::kNoCall;
5486 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005487 HLoadString::LoadKind load_kind = load->GetLoadKind();
5488 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5489 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5490 locations->SetInAt(0, Location::RequiresRegister());
5491 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005492 locations->SetOut(Location::RequiresRegister());
5493}
5494
5495void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005496 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005497 Location out_loc = locations->Out();
5498 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005499
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005500 switch (load->GetLoadKind()) {
5501 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5502 DCHECK(!kEmitCompilerReadBarrier);
5503 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5504 load->GetStringIndex()));
5505 return; // No dex cache slow path.
5506 }
5507 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5508 DCHECK(!kEmitCompilerReadBarrier);
5509 CodeGeneratorARM::PcRelativePatchInfo* labels =
5510 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5511 __ BindTrackedLabel(&labels->movw_label);
5512 __ movw(out, /* placeholder */ 0u);
5513 __ BindTrackedLabel(&labels->movt_label);
5514 __ movt(out, /* placeholder */ 0u);
5515 __ BindTrackedLabel(&labels->add_pc_label);
5516 __ add(out, out, ShifterOperand(PC));
5517 return; // No dex cache slow path.
5518 }
5519 case HLoadString::LoadKind::kBootImageAddress: {
5520 DCHECK(!kEmitCompilerReadBarrier);
5521 DCHECK_NE(load->GetAddress(), 0u);
5522 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5523 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5524 return; // No dex cache slow path.
5525 }
5526 case HLoadString::LoadKind::kDexCacheAddress: {
5527 DCHECK_NE(load->GetAddress(), 0u);
5528 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5529 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5530 // a 128B range. To try and reduce the number of literals if we load multiple strings,
5531 // simply split the dex cache address to a 128B aligned base loaded from a literal
5532 // and the remaining offset embedded in the load.
5533 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
5534 DCHECK_ALIGNED(load->GetAddress(), 4u);
5535 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5536 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5537 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5538 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005539 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005540 GenerateGcRootFieldLoad(load, out_loc, out, offset);
5541 break;
5542 }
5543 case HLoadString::LoadKind::kDexCachePcRelative: {
5544 Register base_reg = locations->InAt(0).AsRegister<Register>();
5545 HArmDexCacheArraysBase* base = load->InputAt(0)->AsArmDexCacheArraysBase();
5546 int32_t offset = load->GetDexCacheElementOffset() - base->GetElementOffset();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005547 // /* GcRoot<mirror::String> */ out = *(dex_cache_arrays_base + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005548 GenerateGcRootFieldLoad(load, out_loc, base_reg, offset);
5549 break;
5550 }
5551 case HLoadString::LoadKind::kDexCacheViaMethod: {
5552 Register current_method = locations->InAt(0).AsRegister<Register>();
5553
5554 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5555 GenerateGcRootFieldLoad(
5556 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5557 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5558 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
5559 // /* GcRoot<mirror::String> */ out = out[string_index]
5560 GenerateGcRootFieldLoad(
5561 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
5562 break;
5563 }
5564 default:
5565 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5566 UNREACHABLE();
5567 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005568
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005569 if (!load->IsInDexCache()) {
5570 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5571 codegen_->AddSlowPath(slow_path);
5572 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5573 __ Bind(slow_path->GetExitLabel());
5574 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005575}
5576
David Brazdilcb1c0552015-08-04 16:22:25 +01005577static int32_t GetExceptionTlsOffset() {
5578 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5579}
5580
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005581void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5582 LocationSummary* locations =
5583 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5584 locations->SetOut(Location::RequiresRegister());
5585}
5586
5587void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005588 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005589 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5590}
5591
5592void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5593 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5594}
5595
5596void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005597 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005598 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005599}
5600
5601void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5602 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005603 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005604 InvokeRuntimeCallingConvention calling_convention;
5605 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5606}
5607
5608void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5609 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005610 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005611 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005612}
5613
Roland Levillainc9285912015-12-18 10:38:42 +00005614static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5615 return kEmitCompilerReadBarrier &&
5616 (kUseBakerReadBarrier ||
5617 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5618 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5619 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5620}
5621
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005622void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005623 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005624 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5625 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005626 case TypeCheckKind::kExactCheck:
5627 case TypeCheckKind::kAbstractClassCheck:
5628 case TypeCheckKind::kClassHierarchyCheck:
5629 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005630 call_kind =
5631 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005632 break;
5633 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005634 case TypeCheckKind::kUnresolvedCheck:
5635 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005636 call_kind = LocationSummary::kCallOnSlowPath;
5637 break;
5638 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005639
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005641 locations->SetInAt(0, Location::RequiresRegister());
5642 locations->SetInAt(1, Location::RequiresRegister());
5643 // The "out" register is used as a temporary, so it overlaps with the inputs.
5644 // Note that TypeCheckSlowPathARM uses this register too.
5645 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5646 // When read barriers are enabled, we need a temporary register for
5647 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005648 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005649 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005650 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005651}
5652
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005653void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005654 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005655 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005656 Location obj_loc = locations->InAt(0);
5657 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005658 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005659 Location out_loc = locations->Out();
5660 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005661 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005662 locations->GetTemp(0) :
5663 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005664 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005665 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5666 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5667 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005668 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005669 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005670
5671 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005672 // avoid null check if we know obj is not null.
5673 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005674 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005675 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676
Roland Levillain3b359c72015-11-17 19:35:12 +00005677 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005678 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005679
Roland Levillainc9285912015-12-18 10:38:42 +00005680 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 case TypeCheckKind::kExactCheck: {
5682 __ cmp(out, ShifterOperand(cls));
5683 // Classes must be equal for the instanceof to succeed.
5684 __ b(&zero, NE);
5685 __ LoadImmediate(out, 1);
5686 __ b(&done);
5687 break;
5688 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005689
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005690 case TypeCheckKind::kAbstractClassCheck: {
5691 // If the class is abstract, we eagerly fetch the super class of the
5692 // object to avoid doing a comparison we know will fail.
5693 Label loop;
5694 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005695 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005696 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005697 // If `out` is null, we use it for the result, and jump to `done`.
5698 __ CompareAndBranchIfZero(out, &done);
5699 __ cmp(out, ShifterOperand(cls));
5700 __ b(&loop, NE);
5701 __ LoadImmediate(out, 1);
5702 if (zero.IsLinked()) {
5703 __ b(&done);
5704 }
5705 break;
5706 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005707
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005708 case TypeCheckKind::kClassHierarchyCheck: {
5709 // Walk over the class hierarchy to find a match.
5710 Label loop, success;
5711 __ Bind(&loop);
5712 __ cmp(out, ShifterOperand(cls));
5713 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005714 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005715 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005716 __ CompareAndBranchIfNonZero(out, &loop);
5717 // If `out` is null, we use it for the result, and jump to `done`.
5718 __ b(&done);
5719 __ Bind(&success);
5720 __ LoadImmediate(out, 1);
5721 if (zero.IsLinked()) {
5722 __ b(&done);
5723 }
5724 break;
5725 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005726
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005728 // Do an exact check.
5729 Label exact_check;
5730 __ cmp(out, ShifterOperand(cls));
5731 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005732 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005733 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005734 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005735 // If `out` is null, we use it for the result, and jump to `done`.
5736 __ CompareAndBranchIfZero(out, &done);
5737 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5738 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5739 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005740 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005741 __ LoadImmediate(out, 1);
5742 __ b(&done);
5743 break;
5744 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005745
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005746 case TypeCheckKind::kArrayCheck: {
5747 __ cmp(out, ShifterOperand(cls));
5748 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005749 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5750 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005751 codegen_->AddSlowPath(slow_path);
5752 __ b(slow_path->GetEntryLabel(), NE);
5753 __ LoadImmediate(out, 1);
5754 if (zero.IsLinked()) {
5755 __ b(&done);
5756 }
5757 break;
5758 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005759
Calin Juravle98893e12015-10-02 21:05:03 +01005760 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005761 case TypeCheckKind::kInterfaceCheck: {
5762 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005763 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005764 // cases.
5765 //
5766 // We cannot directly call the InstanceofNonTrivial runtime
5767 // entry point without resorting to a type checking slow path
5768 // here (i.e. by calling InvokeRuntime directly), as it would
5769 // require to assign fixed registers for the inputs of this
5770 // HInstanceOf instruction (following the runtime calling
5771 // convention), which might be cluttered by the potential first
5772 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005773 //
5774 // TODO: Introduce a new runtime entry point taking the object
5775 // to test (instead of its class) as argument, and let it deal
5776 // with the read barrier issues. This will let us refactor this
5777 // case of the `switch` code as it was previously (with a direct
5778 // call to the runtime not using a type checking slow path).
5779 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005780 DCHECK(locations->OnlyCallsOnSlowPath());
5781 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5782 /* is_fatal */ false);
5783 codegen_->AddSlowPath(slow_path);
5784 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005785 if (zero.IsLinked()) {
5786 __ b(&done);
5787 }
5788 break;
5789 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005790 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005791
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005792 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005793 __ Bind(&zero);
5794 __ LoadImmediate(out, 0);
5795 }
5796
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 if (done.IsLinked()) {
5798 __ Bind(&done);
5799 }
5800
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005801 if (slow_path != nullptr) {
5802 __ Bind(slow_path->GetExitLabel());
5803 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005804}
5805
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005806void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5808 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5809
Roland Levillain3b359c72015-11-17 19:35:12 +00005810 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5811 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 case TypeCheckKind::kExactCheck:
5813 case TypeCheckKind::kAbstractClassCheck:
5814 case TypeCheckKind::kClassHierarchyCheck:
5815 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005816 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5817 LocationSummary::kCallOnSlowPath :
5818 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 break;
5820 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005821 case TypeCheckKind::kUnresolvedCheck:
5822 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005823 call_kind = LocationSummary::kCallOnSlowPath;
5824 break;
5825 }
5826
Roland Levillain3b359c72015-11-17 19:35:12 +00005827 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5828 locations->SetInAt(0, Location::RequiresRegister());
5829 locations->SetInAt(1, Location::RequiresRegister());
5830 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5831 locations->AddTemp(Location::RequiresRegister());
5832 // When read barriers are enabled, we need an additional temporary
5833 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005834 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005835 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005837}
5838
5839void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005840 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005841 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005842 Location obj_loc = locations->InAt(0);
5843 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005844 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005845 Location temp_loc = locations->GetTemp(0);
5846 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005847 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005848 locations->GetTemp(1) :
5849 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005850 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5852 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5853 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005854
Roland Levillain3b359c72015-11-17 19:35:12 +00005855 bool is_type_check_slow_path_fatal =
5856 (type_check_kind == TypeCheckKind::kExactCheck ||
5857 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5858 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5859 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5860 !instruction->CanThrowIntoCatchBlock();
5861 SlowPathCode* type_check_slow_path =
5862 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5863 is_type_check_slow_path_fatal);
5864 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865
5866 Label done;
5867 // Avoid null check if we know obj is not null.
5868 if (instruction->MustDoNullCheck()) {
5869 __ CompareAndBranchIfZero(obj, &done);
5870 }
5871
Roland Levillain3b359c72015-11-17 19:35:12 +00005872 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005873 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005874
Roland Levillain3b359c72015-11-17 19:35:12 +00005875 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 case TypeCheckKind::kExactCheck:
5877 case TypeCheckKind::kArrayCheck: {
5878 __ cmp(temp, ShifterOperand(cls));
5879 // Jump to slow path for throwing the exception or doing a
5880 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005881 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005882 break;
5883 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005884
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 case TypeCheckKind::kAbstractClassCheck: {
5886 // If the class is abstract, we eagerly fetch the super class of the
5887 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005888 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005890 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005891 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005892
5893 // If the class reference currently in `temp` is not null, jump
5894 // to the `compare_classes` label to compare it with the checked
5895 // class.
5896 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5897 // Otherwise, jump to the slow path to throw the exception.
5898 //
5899 // But before, move back the object's class into `temp` before
5900 // going into the slow path, as it has been overwritten in the
5901 // meantime.
5902 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005903 GenerateReferenceLoadTwoRegisters(
5904 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005905 __ b(type_check_slow_path->GetEntryLabel());
5906
5907 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908 __ cmp(temp, ShifterOperand(cls));
5909 __ b(&loop, NE);
5910 break;
5911 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005912
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005913 case TypeCheckKind::kClassHierarchyCheck: {
5914 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005915 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 __ Bind(&loop);
5917 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005918 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005919
Roland Levillain3b359c72015-11-17 19:35:12 +00005920 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005921 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005922
5923 // If the class reference currently in `temp` is not null, jump
5924 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005926 // Otherwise, jump to the slow path to throw the exception.
5927 //
5928 // But before, move back the object's class into `temp` before
5929 // going into the slow path, as it has been overwritten in the
5930 // meantime.
5931 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005932 GenerateReferenceLoadTwoRegisters(
5933 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005934 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005935 break;
5936 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005937
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005939 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005940 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005941 __ cmp(temp, ShifterOperand(cls));
5942 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005943
5944 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005945 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005946 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005947
5948 // If the component type is not null (i.e. the object is indeed
5949 // an array), jump to label `check_non_primitive_component_type`
5950 // to further check that this component type is not a primitive
5951 // type.
5952 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5953 // Otherwise, jump to the slow path to throw the exception.
5954 //
5955 // But before, move back the object's class into `temp` before
5956 // going into the slow path, as it has been overwritten in the
5957 // meantime.
5958 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005959 GenerateReferenceLoadTwoRegisters(
5960 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005961 __ b(type_check_slow_path->GetEntryLabel());
5962
5963 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005964 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005965 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5966 __ CompareAndBranchIfZero(temp, &done);
5967 // Same comment as above regarding `temp` and the slow path.
5968 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005969 GenerateReferenceLoadTwoRegisters(
5970 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005971 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 break;
5973 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005974
Calin Juravle98893e12015-10-02 21:05:03 +01005975 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005976 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005977 // We always go into the type check slow path for the unresolved
5978 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005979 //
5980 // We cannot directly call the CheckCast runtime entry point
5981 // without resorting to a type checking slow path here (i.e. by
5982 // calling InvokeRuntime directly), as it would require to
5983 // assign fixed registers for the inputs of this HInstanceOf
5984 // instruction (following the runtime calling convention), which
5985 // might be cluttered by the potential first read barrier
5986 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005987 //
5988 // TODO: Introduce a new runtime entry point taking the object
5989 // to test (instead of its class) as argument, and let it deal
5990 // with the read barrier issues. This will let us refactor this
5991 // case of the `switch` code as it was previously (with a direct
5992 // call to the runtime not using a type checking slow path).
5993 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005994 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005995 break;
5996 }
5997 __ Bind(&done);
5998
Roland Levillain3b359c72015-11-17 19:35:12 +00005999 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006000}
6001
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006002void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6003 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006004 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006005 InvokeRuntimeCallingConvention calling_convention;
6006 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6007}
6008
6009void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6010 codegen_->InvokeRuntime(instruction->IsEnter()
6011 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
6012 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00006013 instruction->GetDexPc(),
6014 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006015 if (instruction->IsEnter()) {
6016 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6017 } else {
6018 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6019 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006020}
6021
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006022void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6023void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6024void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006025
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006026void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006027 LocationSummary* locations =
6028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6029 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6030 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006031 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006032 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006033 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006035}
6036
6037void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6038 HandleBitwiseOperation(instruction);
6039}
6040
6041void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6042 HandleBitwiseOperation(instruction);
6043}
6044
6045void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6046 HandleBitwiseOperation(instruction);
6047}
6048
Artem Serov7fc63502016-02-09 17:15:29 +00006049
6050void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6051 LocationSummary* locations =
6052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6053 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6054 || instruction->GetResultType() == Primitive::kPrimLong);
6055
6056 locations->SetInAt(0, Location::RequiresRegister());
6057 locations->SetInAt(1, Location::RequiresRegister());
6058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6059}
6060
6061void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6062 LocationSummary* locations = instruction->GetLocations();
6063 Location first = locations->InAt(0);
6064 Location second = locations->InAt(1);
6065 Location out = locations->Out();
6066
6067 if (instruction->GetResultType() == Primitive::kPrimInt) {
6068 Register first_reg = first.AsRegister<Register>();
6069 ShifterOperand second_reg(second.AsRegister<Register>());
6070 Register out_reg = out.AsRegister<Register>();
6071
6072 switch (instruction->GetOpKind()) {
6073 case HInstruction::kAnd:
6074 __ bic(out_reg, first_reg, second_reg);
6075 break;
6076 case HInstruction::kOr:
6077 __ orn(out_reg, first_reg, second_reg);
6078 break;
6079 // There is no EON on arm.
6080 case HInstruction::kXor:
6081 default:
6082 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6083 UNREACHABLE();
6084 }
6085 return;
6086
6087 } else {
6088 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6089 Register first_low = first.AsRegisterPairLow<Register>();
6090 Register first_high = first.AsRegisterPairHigh<Register>();
6091 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6092 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6093 Register out_low = out.AsRegisterPairLow<Register>();
6094 Register out_high = out.AsRegisterPairHigh<Register>();
6095
6096 switch (instruction->GetOpKind()) {
6097 case HInstruction::kAnd:
6098 __ bic(out_low, first_low, second_low);
6099 __ bic(out_high, first_high, second_high);
6100 break;
6101 case HInstruction::kOr:
6102 __ orn(out_low, first_low, second_low);
6103 __ orn(out_high, first_high, second_high);
6104 break;
6105 // There is no EON on arm.
6106 case HInstruction::kXor:
6107 default:
6108 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6109 UNREACHABLE();
6110 }
6111 }
6112}
6113
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006114void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6115 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6116 if (value == 0xffffffffu) {
6117 if (out != first) {
6118 __ mov(out, ShifterOperand(first));
6119 }
6120 return;
6121 }
6122 if (value == 0u) {
6123 __ mov(out, ShifterOperand(0));
6124 return;
6125 }
6126 ShifterOperand so;
6127 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6128 __ and_(out, first, so);
6129 } else {
6130 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6131 __ bic(out, first, ShifterOperand(~value));
6132 }
6133}
6134
6135void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6136 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6137 if (value == 0u) {
6138 if (out != first) {
6139 __ mov(out, ShifterOperand(first));
6140 }
6141 return;
6142 }
6143 if (value == 0xffffffffu) {
6144 __ mvn(out, ShifterOperand(0));
6145 return;
6146 }
6147 ShifterOperand so;
6148 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6149 __ orr(out, first, so);
6150 } else {
6151 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6152 __ orn(out, first, ShifterOperand(~value));
6153 }
6154}
6155
6156void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6157 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6158 if (value == 0u) {
6159 if (out != first) {
6160 __ mov(out, ShifterOperand(first));
6161 }
6162 return;
6163 }
6164 __ eor(out, first, ShifterOperand(value));
6165}
6166
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006167void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6168 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006169 Location first = locations->InAt(0);
6170 Location second = locations->InAt(1);
6171 Location out = locations->Out();
6172
6173 if (second.IsConstant()) {
6174 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6175 uint32_t value_low = Low32Bits(value);
6176 if (instruction->GetResultType() == Primitive::kPrimInt) {
6177 Register first_reg = first.AsRegister<Register>();
6178 Register out_reg = out.AsRegister<Register>();
6179 if (instruction->IsAnd()) {
6180 GenerateAndConst(out_reg, first_reg, value_low);
6181 } else if (instruction->IsOr()) {
6182 GenerateOrrConst(out_reg, first_reg, value_low);
6183 } else {
6184 DCHECK(instruction->IsXor());
6185 GenerateEorConst(out_reg, first_reg, value_low);
6186 }
6187 } else {
6188 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6189 uint32_t value_high = High32Bits(value);
6190 Register first_low = first.AsRegisterPairLow<Register>();
6191 Register first_high = first.AsRegisterPairHigh<Register>();
6192 Register out_low = out.AsRegisterPairLow<Register>();
6193 Register out_high = out.AsRegisterPairHigh<Register>();
6194 if (instruction->IsAnd()) {
6195 GenerateAndConst(out_low, first_low, value_low);
6196 GenerateAndConst(out_high, first_high, value_high);
6197 } else if (instruction->IsOr()) {
6198 GenerateOrrConst(out_low, first_low, value_low);
6199 GenerateOrrConst(out_high, first_high, value_high);
6200 } else {
6201 DCHECK(instruction->IsXor());
6202 GenerateEorConst(out_low, first_low, value_low);
6203 GenerateEorConst(out_high, first_high, value_high);
6204 }
6205 }
6206 return;
6207 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006208
6209 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006210 Register first_reg = first.AsRegister<Register>();
6211 ShifterOperand second_reg(second.AsRegister<Register>());
6212 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006213 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006214 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006215 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006216 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006217 } else {
6218 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006219 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006220 }
6221 } else {
6222 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006223 Register first_low = first.AsRegisterPairLow<Register>();
6224 Register first_high = first.AsRegisterPairHigh<Register>();
6225 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6226 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6227 Register out_low = out.AsRegisterPairLow<Register>();
6228 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006229 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006230 __ and_(out_low, first_low, second_low);
6231 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006232 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006233 __ orr(out_low, first_low, second_low);
6234 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006235 } else {
6236 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006237 __ eor(out_low, first_low, second_low);
6238 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006239 }
6240 }
6241}
6242
Roland Levillainc9285912015-12-18 10:38:42 +00006243void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6244 Location out,
6245 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006246 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006247 Register out_reg = out.AsRegister<Register>();
6248 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006249 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006250 if (kUseBakerReadBarrier) {
6251 // Load with fast path based Baker's read barrier.
6252 // /* HeapReference<Object> */ out = *(out + offset)
6253 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006254 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006255 } else {
6256 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006257 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006258 // in the following move operation, as we will need it for the
6259 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006260 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006261 // /* HeapReference<Object> */ out = *(out + offset)
6262 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006263 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006264 }
6265 } else {
6266 // Plain load with no read barrier.
6267 // /* HeapReference<Object> */ out = *(out + offset)
6268 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6269 __ MaybeUnpoisonHeapReference(out_reg);
6270 }
6271}
6272
6273void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6274 Location out,
6275 Location obj,
6276 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006277 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006278 Register out_reg = out.AsRegister<Register>();
6279 Register obj_reg = obj.AsRegister<Register>();
6280 if (kEmitCompilerReadBarrier) {
6281 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006282 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006283 // Load with fast path based Baker's read barrier.
6284 // /* HeapReference<Object> */ out = *(obj + offset)
6285 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006286 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006287 } else {
6288 // Load with slow path based read barrier.
6289 // /* HeapReference<Object> */ out = *(obj + offset)
6290 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6291 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6292 }
6293 } else {
6294 // Plain load with no read barrier.
6295 // /* HeapReference<Object> */ out = *(obj + offset)
6296 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6297 __ MaybeUnpoisonHeapReference(out_reg);
6298 }
6299}
6300
6301void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6302 Location root,
6303 Register obj,
6304 uint32_t offset) {
6305 Register root_reg = root.AsRegister<Register>();
6306 if (kEmitCompilerReadBarrier) {
6307 if (kUseBakerReadBarrier) {
6308 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6309 // Baker's read barrier are used:
6310 //
6311 // root = obj.field;
6312 // if (Thread::Current()->GetIsGcMarking()) {
6313 // root = ReadBarrier::Mark(root)
6314 // }
6315
6316 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6317 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6318 static_assert(
6319 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6320 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6321 "have different sizes.");
6322 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6323 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6324 "have different sizes.");
6325
6326 // Slow path used to mark the GC root `root`.
6327 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006328 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006329 codegen_->AddSlowPath(slow_path);
6330
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006331 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006332 __ LoadFromOffset(
6333 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6334 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6335 __ Bind(slow_path->GetExitLabel());
6336 } else {
6337 // GC root loaded through a slow path for read barriers other
6338 // than Baker's.
6339 // /* GcRoot<mirror::Object>* */ root = obj + offset
6340 __ AddConstant(root_reg, obj, offset);
6341 // /* mirror::Object* */ root = root->Read()
6342 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6343 }
6344 } else {
6345 // Plain GC root load with no read barrier.
6346 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6347 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6348 // Note that GC roots are not affected by heap poisoning, thus we
6349 // do not have to unpoison `root_reg` here.
6350 }
6351}
6352
6353void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6354 Location ref,
6355 Register obj,
6356 uint32_t offset,
6357 Location temp,
6358 bool needs_null_check) {
6359 DCHECK(kEmitCompilerReadBarrier);
6360 DCHECK(kUseBakerReadBarrier);
6361
6362 // /* HeapReference<Object> */ ref = *(obj + offset)
6363 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006364 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006365 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006366 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006367}
6368
6369void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6370 Location ref,
6371 Register obj,
6372 uint32_t data_offset,
6373 Location index,
6374 Location temp,
6375 bool needs_null_check) {
6376 DCHECK(kEmitCompilerReadBarrier);
6377 DCHECK(kUseBakerReadBarrier);
6378
Roland Levillainbfea3352016-06-23 13:48:47 +01006379 static_assert(
6380 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6381 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006382 // /* HeapReference<Object> */ ref =
6383 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006384 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006385 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006386 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006387}
6388
6389void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6390 Location ref,
6391 Register obj,
6392 uint32_t offset,
6393 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006394 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006395 Location temp,
6396 bool needs_null_check) {
6397 DCHECK(kEmitCompilerReadBarrier);
6398 DCHECK(kUseBakerReadBarrier);
6399
6400 // In slow path based read barriers, the read barrier call is
6401 // inserted after the original load. However, in fast path based
6402 // Baker's read barriers, we need to perform the load of
6403 // mirror::Object::monitor_ *before* the original reference load.
6404 // This load-load ordering is required by the read barrier.
6405 // The fast path/slow path (for Baker's algorithm) should look like:
6406 //
6407 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6408 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6409 // HeapReference<Object> ref = *src; // Original reference load.
6410 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6411 // if (is_gray) {
6412 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6413 // }
6414 //
6415 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006416 // slightly more complex as it performs additional checks that we do
6417 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006418
6419 Register ref_reg = ref.AsRegister<Register>();
6420 Register temp_reg = temp.AsRegister<Register>();
6421 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6422
6423 // /* int32_t */ monitor = obj->monitor_
6424 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6425 if (needs_null_check) {
6426 MaybeRecordImplicitNullCheck(instruction);
6427 }
6428 // /* LockWord */ lock_word = LockWord(monitor)
6429 static_assert(sizeof(LockWord) == sizeof(int32_t),
6430 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006431
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006432 // Introduce a dependency on the lock_word including the rb_state,
6433 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006434 // a memory barrier (which would be more expensive).
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006435 // obj is unchanged by this operation, but its value now depends on temp_reg.
6436 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006437
6438 // The actual reference load.
6439 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006440 // Load types involving an "index": ArrayGet and
6441 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6442 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006443 if (index.IsConstant()) {
6444 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006445 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006446 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6447 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006448 // Handle the special case of the
6449 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6450 // a register pair as index ("long offset"), of which only the low
6451 // part contains data.
6452 Register index_reg = index.IsRegisterPair()
6453 ? index.AsRegisterPairLow<Register>()
6454 : index.AsRegister<Register>();
6455 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006456 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6457 }
6458 } else {
6459 // /* HeapReference<Object> */ ref = *(obj + offset)
6460 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6461 }
6462
6463 // Object* ref = ref_addr->AsMirrorPtr()
6464 __ MaybeUnpoisonHeapReference(ref_reg);
6465
6466 // Slow path used to mark the object `ref` when it is gray.
6467 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006468 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006469 AddSlowPath(slow_path);
6470
6471 // if (rb_state == ReadBarrier::gray_ptr_)
6472 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006473 // Given the numeric representation, it's enough to check the low bit of the
6474 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6475 // which can be a 16-bit instruction unlike the TST immediate.
6476 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6477 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6478 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6479 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6480 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006481 __ Bind(slow_path->GetExitLabel());
6482}
6483
6484void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6485 Location out,
6486 Location ref,
6487 Location obj,
6488 uint32_t offset,
6489 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006490 DCHECK(kEmitCompilerReadBarrier);
6491
Roland Levillainc9285912015-12-18 10:38:42 +00006492 // Insert a slow path based read barrier *after* the reference load.
6493 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006494 // If heap poisoning is enabled, the unpoisoning of the loaded
6495 // reference will be carried out by the runtime within the slow
6496 // path.
6497 //
6498 // Note that `ref` currently does not get unpoisoned (when heap
6499 // poisoning is enabled), which is alright as the `ref` argument is
6500 // not used by the artReadBarrierSlow entry point.
6501 //
6502 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6503 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6504 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6505 AddSlowPath(slow_path);
6506
Roland Levillain3b359c72015-11-17 19:35:12 +00006507 __ b(slow_path->GetEntryLabel());
6508 __ Bind(slow_path->GetExitLabel());
6509}
6510
Roland Levillainc9285912015-12-18 10:38:42 +00006511void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6512 Location out,
6513 Location ref,
6514 Location obj,
6515 uint32_t offset,
6516 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006517 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006518 // Baker's read barriers shall be handled by the fast path
6519 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6520 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006521 // If heap poisoning is enabled, unpoisoning will be taken care of
6522 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006523 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006524 } else if (kPoisonHeapReferences) {
6525 __ UnpoisonHeapReference(out.AsRegister<Register>());
6526 }
6527}
6528
Roland Levillainc9285912015-12-18 10:38:42 +00006529void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6530 Location out,
6531 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006532 DCHECK(kEmitCompilerReadBarrier);
6533
Roland Levillainc9285912015-12-18 10:38:42 +00006534 // Insert a slow path based read barrier *after* the GC root load.
6535 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006536 // Note that GC roots are not affected by heap poisoning, so we do
6537 // not need to do anything special for this here.
6538 SlowPathCode* slow_path =
6539 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6540 AddSlowPath(slow_path);
6541
Roland Levillain3b359c72015-11-17 19:35:12 +00006542 __ b(slow_path->GetEntryLabel());
6543 __ Bind(slow_path->GetExitLabel());
6544}
6545
Vladimir Markodc151b22015-10-15 18:02:30 +01006546HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6547 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6548 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006549 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6550 // We disable pc-relative load when there is an irreducible loop, as the optimization
6551 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006552 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6553 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006554 if (GetGraph()->HasIrreducibleLoops() &&
6555 (dispatch_info.method_load_kind ==
6556 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6557 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6558 }
6559
6560 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006561 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6562 if (&outer_dex_file != target_method.dex_file) {
6563 // Calls across dex files are more likely to exceed the available BL range,
6564 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6565 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6566 (desired_dispatch_info.method_load_kind ==
6567 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6568 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6569 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6570 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006571 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006572 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006573 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006574 0u
6575 };
6576 }
6577 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006578 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006579}
6580
Vladimir Markob4536b72015-11-24 13:45:23 +00006581Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6582 Register temp) {
6583 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6584 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6585 if (!invoke->GetLocations()->Intrinsified()) {
6586 return location.AsRegister<Register>();
6587 }
6588 // For intrinsics we allow any location, so it may be on the stack.
6589 if (!location.IsRegister()) {
6590 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6591 return temp;
6592 }
6593 // For register locations, check if the register was saved. If so, get it from the stack.
6594 // Note: There is a chance that the register was saved but not overwritten, so we could
6595 // save one load. However, since this is just an intrinsic slow path we prefer this
6596 // simple and more robust approach rather that trying to determine if that's the case.
6597 SlowPathCode* slow_path = GetCurrentSlowPath();
6598 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6599 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6600 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6601 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6602 return temp;
6603 }
6604 return location.AsRegister<Register>();
6605}
6606
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006607void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006608 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006609 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006610 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6611 // LR = code address from literal pool with link-time patch.
6612 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006613 break;
6614 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6615 // LR = invoke->GetDirectCodePtr();
6616 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006617 break;
6618 default:
6619 break;
6620 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006621
Vladimir Marko58155012015-08-19 12:49:41 +00006622 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6623 switch (invoke->GetMethodLoadKind()) {
6624 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6625 // temp = thread->string_init_entrypoint
6626 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6627 break;
6628 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006629 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006630 break;
6631 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6632 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6633 break;
6634 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6635 __ LoadLiteral(temp.AsRegister<Register>(),
6636 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6637 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006638 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6639 HArmDexCacheArraysBase* base =
6640 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6641 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6642 temp.AsRegister<Register>());
6643 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6644 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6645 break;
6646 }
Vladimir Marko58155012015-08-19 12:49:41 +00006647 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006648 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006649 Register method_reg;
6650 Register reg = temp.AsRegister<Register>();
6651 if (current_method.IsRegister()) {
6652 method_reg = current_method.AsRegister<Register>();
6653 } else {
6654 DCHECK(invoke->GetLocations()->Intrinsified());
6655 DCHECK(!current_method.IsValid());
6656 method_reg = reg;
6657 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6658 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006659 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6660 __ LoadFromOffset(kLoadWord,
6661 reg,
6662 method_reg,
6663 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006664 // temp = temp[index_in_cache];
6665 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6666 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006667 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6668 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006669 }
Vladimir Marko58155012015-08-19 12:49:41 +00006670 }
6671
6672 switch (invoke->GetCodePtrLocation()) {
6673 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6674 __ bl(GetFrameEntryLabel());
6675 break;
6676 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006677 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006678 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006679 // Arbitrarily branch to the BL itself, override at link time.
6680 __ bl(&relative_call_patches_.back().label);
6681 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006682 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6683 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6684 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006685 // LR()
6686 __ blx(LR);
6687 break;
6688 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6689 // LR = callee_method->entry_point_from_quick_compiled_code_
6690 __ LoadFromOffset(
6691 kLoadWord, LR, callee_method.AsRegister<Register>(),
6692 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6693 // LR()
6694 __ blx(LR);
6695 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006696 }
6697
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006698 DCHECK(!IsLeafMethod());
6699}
6700
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006701void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6702 Register temp = temp_location.AsRegister<Register>();
6703 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6704 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006705
6706 // Use the calling convention instead of the location of the receiver, as
6707 // intrinsics may have put the receiver in a different register. In the intrinsics
6708 // slow path, the arguments have been moved to the right place, so here we are
6709 // guaranteed that the receiver is the first register of the calling convention.
6710 InvokeDexCallingConvention calling_convention;
6711 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006712 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006713 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006714 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006715 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006716 // Instead of simply (possibly) unpoisoning `temp` here, we should
6717 // emit a read barrier for the previous class reference load.
6718 // However this is not required in practice, as this is an
6719 // intermediate/temporary reference and because the current
6720 // concurrent copying collector keeps the from-space memory
6721 // intact/accessible until the end of the marking phase (the
6722 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006723 __ MaybeUnpoisonHeapReference(temp);
6724 // temp = temp->GetMethodAt(method_offset);
6725 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6726 kArmWordSize).Int32Value();
6727 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6728 // LR = temp->GetEntryPoint();
6729 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6730 // LR();
6731 __ blx(LR);
6732}
6733
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006734CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6735 const DexFile& dex_file, uint32_t string_index) {
6736 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6737}
6738
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006739CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6740 const DexFile& dex_file, uint32_t type_index) {
6741 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6742}
6743
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006744CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6745 const DexFile& dex_file, uint32_t element_offset) {
6746 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6747}
6748
6749CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6750 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6751 patches->emplace_back(dex_file, offset_or_index);
6752 return &patches->back();
6753}
6754
6755Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6756 uint32_t string_index) {
6757 return boot_image_string_patches_.GetOrCreate(
6758 StringReference(&dex_file, string_index),
6759 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6760}
6761
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006762Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6763 uint32_t type_index) {
6764 return boot_image_type_patches_.GetOrCreate(
6765 TypeReference(&dex_file, type_index),
6766 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6767}
6768
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006769Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6770 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6771 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6772 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6773}
6774
6775Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6776 return DeduplicateUint32Literal(address, &uint32_literals_);
6777}
6778
Vladimir Marko58155012015-08-19 12:49:41 +00006779void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6780 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006781 size_t size =
6782 method_patches_.size() +
6783 call_patches_.size() +
6784 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006785 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6786 boot_image_string_patches_.size() +
6787 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006788 boot_image_type_patches_.size() +
6789 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006790 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006791 linker_patches->reserve(size);
6792 for (const auto& entry : method_patches_) {
6793 const MethodReference& target_method = entry.first;
6794 Literal* literal = entry.second;
6795 DCHECK(literal->GetLabel()->IsBound());
6796 uint32_t literal_offset = literal->GetLabel()->Position();
6797 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6798 target_method.dex_file,
6799 target_method.dex_method_index));
6800 }
6801 for (const auto& entry : call_patches_) {
6802 const MethodReference& target_method = entry.first;
6803 Literal* literal = entry.second;
6804 DCHECK(literal->GetLabel()->IsBound());
6805 uint32_t literal_offset = literal->GetLabel()->Position();
6806 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6807 target_method.dex_file,
6808 target_method.dex_method_index));
6809 }
6810 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6811 uint32_t literal_offset = info.label.Position();
6812 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6813 info.target_method.dex_file,
6814 info.target_method.dex_method_index));
6815 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006816 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6817 const DexFile& dex_file = info.target_dex_file;
6818 size_t base_element_offset = info.offset_or_index;
6819 DCHECK(info.add_pc_label.IsBound());
6820 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006821 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006822 DCHECK(info.movw_label.IsBound());
6823 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006824 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6825 &dex_file,
6826 add_pc_offset,
6827 base_element_offset));
6828 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006829 DCHECK(info.movt_label.IsBound());
6830 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006831 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6832 &dex_file,
6833 add_pc_offset,
6834 base_element_offset));
6835 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006836 for (const auto& entry : boot_image_string_patches_) {
6837 const StringReference& target_string = entry.first;
6838 Literal* literal = entry.second;
6839 DCHECK(literal->GetLabel()->IsBound());
6840 uint32_t literal_offset = literal->GetLabel()->Position();
6841 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6842 target_string.dex_file,
6843 target_string.string_index));
6844 }
6845 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6846 const DexFile& dex_file = info.target_dex_file;
6847 uint32_t string_index = info.offset_or_index;
6848 DCHECK(info.add_pc_label.IsBound());
6849 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6850 // Add MOVW patch.
6851 DCHECK(info.movw_label.IsBound());
6852 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6853 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6854 &dex_file,
6855 add_pc_offset,
6856 string_index));
6857 // Add MOVT patch.
6858 DCHECK(info.movt_label.IsBound());
6859 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6860 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6861 &dex_file,
6862 add_pc_offset,
6863 string_index));
6864 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006865 for (const auto& entry : boot_image_type_patches_) {
6866 const TypeReference& target_type = entry.first;
6867 Literal* literal = entry.second;
6868 DCHECK(literal->GetLabel()->IsBound());
6869 uint32_t literal_offset = literal->GetLabel()->Position();
6870 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6871 target_type.dex_file,
6872 target_type.type_index));
6873 }
6874 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6875 const DexFile& dex_file = info.target_dex_file;
6876 uint32_t type_index = info.offset_or_index;
6877 DCHECK(info.add_pc_label.IsBound());
6878 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6879 // Add MOVW patch.
6880 DCHECK(info.movw_label.IsBound());
6881 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6882 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6883 &dex_file,
6884 add_pc_offset,
6885 type_index));
6886 // Add MOVT patch.
6887 DCHECK(info.movt_label.IsBound());
6888 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6889 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6890 &dex_file,
6891 add_pc_offset,
6892 type_index));
6893 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006894 for (const auto& entry : boot_image_address_patches_) {
6895 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6896 Literal* literal = entry.second;
6897 DCHECK(literal->GetLabel()->IsBound());
6898 uint32_t literal_offset = literal->GetLabel()->Position();
6899 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6900 }
6901}
6902
6903Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6904 return map->GetOrCreate(
6905 value,
6906 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006907}
6908
6909Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6910 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006911 return map->GetOrCreate(
6912 target_method,
6913 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006914}
6915
6916Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6917 return DeduplicateMethodLiteral(target_method, &method_patches_);
6918}
6919
6920Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6921 return DeduplicateMethodLiteral(target_method, &call_patches_);
6922}
6923
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006924void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6925 LocationSummary* locations =
6926 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6927 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6928 Location::RequiresRegister());
6929 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6930 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6932}
6933
6934void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6935 LocationSummary* locations = instr->GetLocations();
6936 Register res = locations->Out().AsRegister<Register>();
6937 Register accumulator =
6938 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6939 Register mul_left =
6940 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6941 Register mul_right =
6942 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6943
6944 if (instr->GetOpKind() == HInstruction::kAdd) {
6945 __ mla(res, mul_left, mul_right, accumulator);
6946 } else {
6947 __ mls(res, mul_left, mul_right, accumulator);
6948 }
6949}
6950
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006951void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006952 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006953 LOG(FATAL) << "Unreachable";
6954}
6955
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006956void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006957 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006958 LOG(FATAL) << "Unreachable";
6959}
6960
Mark Mendellfe57faa2015-09-18 09:26:15 -04006961// Simple implementation of packed switch - generate cascaded compare/jumps.
6962void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6963 LocationSummary* locations =
6964 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6965 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006966 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006967 codegen_->GetAssembler()->IsThumb()) {
6968 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6969 if (switch_instr->GetStartValue() != 0) {
6970 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6971 }
6972 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006973}
6974
6975void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6976 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006977 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006978 LocationSummary* locations = switch_instr->GetLocations();
6979 Register value_reg = locations->InAt(0).AsRegister<Register>();
6980 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6981
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006982 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006983 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006984 Register temp_reg = IP;
6985 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6986 // the immediate, because IP is used as the destination register. For the other
6987 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6988 // and they can be encoded in the instruction without making use of IP register.
6989 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6990
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006991 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006992 // Jump to successors[0] if value == lower_bound.
6993 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6994 int32_t last_index = 0;
6995 for (; num_entries - last_index > 2; last_index += 2) {
6996 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6997 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6998 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6999 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7000 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7001 }
7002 if (num_entries - last_index == 2) {
7003 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007004 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007005 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007006 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007007
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007008 // And the default for any other value.
7009 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7010 __ b(codegen_->GetLabelOf(default_block));
7011 }
7012 } else {
7013 // Create a table lookup.
7014 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7015
7016 // Materialize a pointer to the switch table
7017 std::vector<Label*> labels(num_entries);
7018 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7019 for (uint32_t i = 0; i < num_entries; i++) {
7020 labels[i] = codegen_->GetLabelOf(successors[i]);
7021 }
7022 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7023
7024 // Remove the bias.
7025 Register key_reg;
7026 if (lower_bound != 0) {
7027 key_reg = locations->GetTemp(1).AsRegister<Register>();
7028 __ AddConstant(key_reg, value_reg, -lower_bound);
7029 } else {
7030 key_reg = value_reg;
7031 }
7032
7033 // Check whether the value is in the table, jump to default block if not.
7034 __ CmpConstant(key_reg, num_entries - 1);
7035 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7036
7037 // Load the displacement from the table.
7038 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7039
7040 // Dispatch is a direct add to the PC (for Thumb2).
7041 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007042 }
7043}
7044
Vladimir Markob4536b72015-11-24 13:45:23 +00007045void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7046 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7047 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007048}
7049
7050void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7051 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007052 CodeGeneratorARM::PcRelativePatchInfo* labels =
7053 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007054 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007055 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007056 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007057 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007058 __ BindTrackedLabel(&labels->add_pc_label);
7059 __ add(base_reg, base_reg, ShifterOperand(PC));
7060}
7061
Andreas Gampe85b62f22015-09-09 13:15:38 -07007062void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7063 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007064 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007065 return;
7066 }
7067
7068 DCHECK_NE(type, Primitive::kPrimVoid);
7069
7070 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7071 if (return_loc.Equals(trg)) {
7072 return;
7073 }
7074
7075 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7076 // with the last branch.
7077 if (type == Primitive::kPrimLong) {
7078 HParallelMove parallel_move(GetGraph()->GetArena());
7079 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7080 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7081 GetMoveResolver()->EmitNativeCode(&parallel_move);
7082 } else if (type == Primitive::kPrimDouble) {
7083 HParallelMove parallel_move(GetGraph()->GetArena());
7084 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7085 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7086 GetMoveResolver()->EmitNativeCode(&parallel_move);
7087 } else {
7088 // Let the parallel move resolver take care of all of this.
7089 HParallelMove parallel_move(GetGraph()->GetArena());
7090 parallel_move.AddMove(return_loc, trg, type, nullptr);
7091 GetMoveResolver()->EmitNativeCode(&parallel_move);
7092 }
7093}
7094
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007095void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7096 LocationSummary* locations =
7097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7098 locations->SetInAt(0, Location::RequiresRegister());
7099 locations->SetOut(Location::RequiresRegister());
7100}
7101
7102void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7103 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007104 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007105 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007106 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007107 __ LoadFromOffset(kLoadWord,
7108 locations->Out().AsRegister<Register>(),
7109 locations->InAt(0).AsRegister<Register>(),
7110 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007111 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007112 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007113 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007114 __ LoadFromOffset(kLoadWord,
7115 locations->Out().AsRegister<Register>(),
7116 locations->InAt(0).AsRegister<Register>(),
7117 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7118 __ LoadFromOffset(kLoadWord,
7119 locations->Out().AsRegister<Register>(),
7120 locations->Out().AsRegister<Register>(),
7121 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007122 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007123}
7124
Roland Levillain4d027112015-07-01 15:41:14 +01007125#undef __
7126#undef QUICK_ENTRY_POINT
7127
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007128} // namespace arm
7129} // namespace art