blob: 663c68a17bb8082f3e0fd4a61ced11a081066862 [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<
319 kQuickInstanceofNonTrivial, uint32_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:
415 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 : SlowPathCode(instruction), out_(out), 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();
424 Register reg_out = out_.AsRegister<Register>();
425 DCHECK(locations->CanCall());
426 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
427 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());
440 SaveLiveRegisters(codegen, locations);
441
442 InvokeRuntimeCallingConvention calling_convention;
443 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
444 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
445 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
446 instruction_,
447 instruction_->GetDexPc(),
448 this);
449 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
450 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
451
452 RestoreLiveRegisters(codegen, locations);
453 __ b(GetExitLabel());
454 }
455
456 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000457 const Location out_;
458 const Location obj_;
459
460 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
461};
462
Roland Levillain3b359c72015-11-17 19:35:12 +0000463// Slow path generating a read barrier for a heap reference.
464class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
465 public:
466 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
467 Location out,
468 Location ref,
469 Location obj,
470 uint32_t offset,
471 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000472 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000473 out_(out),
474 ref_(ref),
475 obj_(obj),
476 offset_(offset),
477 index_(index) {
478 DCHECK(kEmitCompilerReadBarrier);
479 // If `obj` is equal to `out` or `ref`, it means the initial object
480 // has been overwritten by (or after) the heap object reference load
481 // to be instrumented, e.g.:
482 //
483 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000484 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000485 //
486 // In that case, we have lost the information about the original
487 // object, and the emitted read barrier cannot work properly.
488 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
489 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
490 }
491
492 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
493 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
494 LocationSummary* locations = instruction_->GetLocations();
495 Register reg_out = out_.AsRegister<Register>();
496 DCHECK(locations->CanCall());
497 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100498 DCHECK(instruction_->IsInstanceFieldGet() ||
499 instruction_->IsStaticFieldGet() ||
500 instruction_->IsArrayGet() ||
501 instruction_->IsInstanceOf() ||
502 instruction_->IsCheckCast() ||
503 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillainc9285912015-12-18 10:38:42 +0000504 instruction_->GetLocations()->Intrinsified()))
505 << "Unexpected instruction in read barrier for heap reference slow path: "
506 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000507
508 __ Bind(GetEntryLabel());
509 SaveLiveRegisters(codegen, locations);
510
511 // We may have to change the index's value, but as `index_` is a
512 // constant member (like other "inputs" of this slow path),
513 // introduce a copy of it, `index`.
514 Location index = index_;
515 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100516 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000517 if (instruction_->IsArrayGet()) {
518 // Compute the actual memory offset and store it in `index`.
519 Register index_reg = index_.AsRegister<Register>();
520 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
521 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
522 // We are about to change the value of `index_reg` (see the
523 // calls to art::arm::Thumb2Assembler::Lsl and
524 // art::arm::Thumb2Assembler::AddConstant below), but it has
525 // not been saved by the previous call to
526 // art::SlowPathCode::SaveLiveRegisters, as it is a
527 // callee-save register --
528 // art::SlowPathCode::SaveLiveRegisters does not consider
529 // callee-save registers, as it has been designed with the
530 // assumption that callee-save registers are supposed to be
531 // handled by the called function. So, as a callee-save
532 // register, `index_reg` _would_ eventually be saved onto
533 // the stack, but it would be too late: we would have
534 // changed its value earlier. Therefore, we manually save
535 // it here into another freely available register,
536 // `free_reg`, chosen of course among the caller-save
537 // registers (as a callee-save `free_reg` register would
538 // exhibit the same problem).
539 //
540 // Note we could have requested a temporary register from
541 // the register allocator instead; but we prefer not to, as
542 // this is a slow path, and we know we can find a
543 // caller-save register that is available.
544 Register free_reg = FindAvailableCallerSaveRegister(codegen);
545 __ Mov(free_reg, index_reg);
546 index_reg = free_reg;
547 index = Location::RegisterLocation(index_reg);
548 } else {
549 // The initial register stored in `index_` has already been
550 // saved in the call to art::SlowPathCode::SaveLiveRegisters
551 // (as it is not a callee-save register), so we can freely
552 // use it.
553 }
554 // Shifting the index value contained in `index_reg` by the scale
555 // factor (2) cannot overflow in practice, as the runtime is
556 // unable to allocate object arrays with a size larger than
557 // 2^26 - 1 (that is, 2^28 - 4 bytes).
558 __ Lsl(index_reg, index_reg, TIMES_4);
559 static_assert(
560 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
561 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
562 __ AddConstant(index_reg, index_reg, offset_);
563 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100564 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
565 // intrinsics, `index_` is not shifted by a scale factor of 2
566 // (as in the case of ArrayGet), as it is actually an offset
567 // to an object field within an object.
568 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000569 DCHECK(instruction_->GetLocations()->Intrinsified());
570 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
571 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
572 << instruction_->AsInvoke()->GetIntrinsic();
573 DCHECK_EQ(offset_, 0U);
574 DCHECK(index_.IsRegisterPair());
575 // UnsafeGet's offset location is a register pair, the low
576 // part contains the correct offset.
577 index = index_.ToLow();
578 }
579 }
580
581 // We're moving two or three locations to locations that could
582 // overlap, so we need a parallel move resolver.
583 InvokeRuntimeCallingConvention calling_convention;
584 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
585 parallel_move.AddMove(ref_,
586 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
587 Primitive::kPrimNot,
588 nullptr);
589 parallel_move.AddMove(obj_,
590 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
591 Primitive::kPrimNot,
592 nullptr);
593 if (index.IsValid()) {
594 parallel_move.AddMove(index,
595 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
596 Primitive::kPrimInt,
597 nullptr);
598 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
599 } else {
600 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
601 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
602 }
603 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
604 instruction_,
605 instruction_->GetDexPc(),
606 this);
607 CheckEntrypointTypes<
608 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
609 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
610
611 RestoreLiveRegisters(codegen, locations);
612 __ b(GetExitLabel());
613 }
614
615 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
616
617 private:
618 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
619 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
620 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
621 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
622 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
623 return static_cast<Register>(i);
624 }
625 }
626 // We shall never fail to find a free caller-save register, as
627 // there are more than two core caller-save registers on ARM
628 // (meaning it is possible to find one which is different from
629 // `ref` and `obj`).
630 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
631 LOG(FATAL) << "Could not find a free caller-save register";
632 UNREACHABLE();
633 }
634
Roland Levillain3b359c72015-11-17 19:35:12 +0000635 const Location out_;
636 const Location ref_;
637 const Location obj_;
638 const uint32_t offset_;
639 // An additional location containing an index to an array.
640 // Only used for HArrayGet and the UnsafeGetObject &
641 // UnsafeGetObjectVolatile intrinsics.
642 const Location index_;
643
644 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
645};
646
647// Slow path generating a read barrier for a GC root.
648class ReadBarrierForRootSlowPathARM : public SlowPathCode {
649 public:
650 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000651 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000652 DCHECK(kEmitCompilerReadBarrier);
653 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000654
655 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
656 LocationSummary* locations = instruction_->GetLocations();
657 Register reg_out = out_.AsRegister<Register>();
658 DCHECK(locations->CanCall());
659 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000660 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
661 << "Unexpected instruction in read barrier for GC root slow path: "
662 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000663
664 __ Bind(GetEntryLabel());
665 SaveLiveRegisters(codegen, locations);
666
667 InvokeRuntimeCallingConvention calling_convention;
668 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
669 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
670 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
671 instruction_,
672 instruction_->GetDexPc(),
673 this);
674 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
675 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
676
677 RestoreLiveRegisters(codegen, locations);
678 __ b(GetExitLabel());
679 }
680
681 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
682
683 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000684 const Location out_;
685 const Location root_;
686
687 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
688};
689
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000690#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700691// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
692#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700693
Aart Bike9f37602015-10-09 11:15:55 -0700694inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700695 switch (cond) {
696 case kCondEQ: return EQ;
697 case kCondNE: return NE;
698 case kCondLT: return LT;
699 case kCondLE: return LE;
700 case kCondGT: return GT;
701 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700702 case kCondB: return LO;
703 case kCondBE: return LS;
704 case kCondA: return HI;
705 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700706 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100707 LOG(FATAL) << "Unreachable";
708 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700709}
710
Aart Bike9f37602015-10-09 11:15:55 -0700711// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100712inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700713 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100714 case kCondEQ: return EQ;
715 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700716 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100717 case kCondLT: return LO;
718 case kCondLE: return LS;
719 case kCondGT: return HI;
720 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700721 // Unsigned remain unchanged.
722 case kCondB: return LO;
723 case kCondBE: return LS;
724 case kCondA: return HI;
725 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700726 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727 LOG(FATAL) << "Unreachable";
728 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700729}
730
Vladimir Markod6e069b2016-01-18 11:11:01 +0000731inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
732 // The ARM condition codes can express all the necessary branches, see the
733 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
734 // There is no dex instruction or HIR that would need the missing conditions
735 // "equal or unordered" or "not equal".
736 switch (cond) {
737 case kCondEQ: return EQ;
738 case kCondNE: return NE /* unordered */;
739 case kCondLT: return gt_bias ? CC : LT /* unordered */;
740 case kCondLE: return gt_bias ? LS : LE /* unordered */;
741 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
742 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
743 default:
744 LOG(FATAL) << "UNREACHABLE";
745 UNREACHABLE();
746 }
747}
748
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100750 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100751}
752
753void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100754 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100755}
756
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100757size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
758 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
759 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100760}
761
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100762size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
763 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
764 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100765}
766
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000767size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
768 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
769 return kArmWordSize;
770}
771
772size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
773 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
774 return kArmWordSize;
775}
776
Calin Juravle34166012014-12-19 17:22:29 +0000777CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000778 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100779 const CompilerOptions& compiler_options,
780 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000781 : CodeGenerator(graph,
782 kNumberOfCoreRegisters,
783 kNumberOfSRegisters,
784 kNumberOfRegisterPairs,
785 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
786 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000787 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
788 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100789 compiler_options,
790 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100791 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100792 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100793 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100794 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100795 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000796 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000797 uint32_literals_(std::less<uint32_t>(),
798 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100799 method_patches_(MethodReferenceComparator(),
800 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
801 call_patches_(MethodReferenceComparator(),
802 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000803 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000804 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
805 boot_image_string_patches_(StringReferenceValueComparator(),
806 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
807 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100808 boot_image_type_patches_(TypeReferenceValueComparator(),
809 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
810 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000811 boot_image_address_patches_(std::less<uint32_t>(),
812 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700813 // Always save the LR register to mimic Quick.
814 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100815}
816
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000817void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
818 // Ensure that we fix up branches and literal loads and emit the literal pool.
819 __ FinalizeCode();
820
821 // Adjust native pc offsets in stack maps.
822 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
823 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
824 uint32_t new_position = __ GetAdjustedPosition(old_position);
825 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
826 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100827 // Adjust pc offsets for the disassembly information.
828 if (disasm_info_ != nullptr) {
829 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
830 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
831 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
832 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
833 it.second.start = __ GetAdjustedPosition(it.second.start);
834 it.second.end = __ GetAdjustedPosition(it.second.end);
835 }
836 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
837 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
838 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
839 }
840 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000841
842 CodeGenerator::Finalize(allocator);
843}
844
David Brazdil58282f42016-01-14 12:45:10 +0000845void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100846 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100847 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848
849 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100850 blocked_core_registers_[SP] = true;
851 blocked_core_registers_[LR] = true;
852 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100853
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100854 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100855 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100856
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100857 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100858 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100859
David Brazdil58282f42016-01-14 12:45:10 +0000860 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100861 // Stubs do not save callee-save floating point registers. If the graph
862 // is debuggable, we need to deal with these registers differently. For
863 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000864 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
865 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
866 }
867 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100868
869 UpdateBlockedPairRegisters();
870}
871
872void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
873 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
874 ArmManagedRegister current =
875 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
876 if (blocked_core_registers_[current.AsRegisterPairLow()]
877 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
878 blocked_register_pairs_[i] = true;
879 }
880 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100881}
882
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100883InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800884 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100885 assembler_(codegen->GetAssembler()),
886 codegen_(codegen) {}
887
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000888void CodeGeneratorARM::ComputeSpillMask() {
889 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
890 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000891 // There is no easy instruction to restore just the PC on thumb2. We spill and
892 // restore another arbitrary register.
893 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000894 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
895 // We use vpush and vpop for saving and restoring floating point registers, which take
896 // a SRegister and the number of registers to save/restore after that SRegister. We
897 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
898 // but in the range.
899 if (fpu_spill_mask_ != 0) {
900 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
901 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
902 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
903 fpu_spill_mask_ |= (1 << i);
904 }
905 }
906}
907
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100908static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100909 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100910}
911
912static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100913 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100914}
915
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000916void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000917 bool skip_overflow_check =
918 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000919 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000920 __ Bind(&frame_entry_label_);
921
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000922 if (HasEmptyFrame()) {
923 return;
924 }
925
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100926 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000927 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
928 __ LoadFromOffset(kLoadWord, IP, IP, 0);
929 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100930 }
931
Andreas Gampe501fd632015-09-10 16:11:06 -0700932 __ PushList(core_spill_mask_);
933 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
934 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000935 if (fpu_spill_mask_ != 0) {
936 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
937 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100938 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100939 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000940 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100941 int adjust = GetFrameSize() - FrameEntrySpillSize();
942 __ AddConstant(SP, -adjust);
943 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100944 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000945}
946
947void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000948 if (HasEmptyFrame()) {
949 __ bx(LR);
950 return;
951 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100952 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100953 int adjust = GetFrameSize() - FrameEntrySpillSize();
954 __ AddConstant(SP, adjust);
955 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000956 if (fpu_spill_mask_ != 0) {
957 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
958 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100959 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
960 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000961 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700962 // Pop LR into PC to return.
963 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
964 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
965 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100966 __ cfi().RestoreState();
967 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000968}
969
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100970void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700971 Label* label = GetLabelOf(block);
972 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000973}
974
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100975Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 switch (type) {
977 case Primitive::kPrimBoolean:
978 case Primitive::kPrimByte:
979 case Primitive::kPrimChar:
980 case Primitive::kPrimShort:
981 case Primitive::kPrimInt:
982 case Primitive::kPrimNot: {
983 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000984 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100985 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100986 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100987 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000988 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100989 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100990 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100991
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000992 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100993 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000994 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100995 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000996 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100997 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000998 if (calling_convention.GetRegisterAt(index) == R1) {
999 // Skip R1, and use R2_R3 instead.
1000 gp_index_++;
1001 index++;
1002 }
1003 }
1004 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1005 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001006 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001007
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001008 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001009 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001011 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1012 }
1013 }
1014
1015 case Primitive::kPrimFloat: {
1016 uint32_t stack_index = stack_index_++;
1017 if (float_index_ % 2 == 0) {
1018 float_index_ = std::max(double_index_, float_index_);
1019 }
1020 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1021 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1022 } else {
1023 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1024 }
1025 }
1026
1027 case Primitive::kPrimDouble: {
1028 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1029 uint32_t stack_index = stack_index_;
1030 stack_index_ += 2;
1031 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1032 uint32_t index = double_index_;
1033 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001034 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001035 calling_convention.GetFpuRegisterAt(index),
1036 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001037 DCHECK(ExpectedPairLayout(result));
1038 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001039 } else {
1040 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001041 }
1042 }
1043
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001044 case Primitive::kPrimVoid:
1045 LOG(FATAL) << "Unexpected parameter type " << type;
1046 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001047 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001048 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001049}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001050
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001051Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001052 switch (type) {
1053 case Primitive::kPrimBoolean:
1054 case Primitive::kPrimByte:
1055 case Primitive::kPrimChar:
1056 case Primitive::kPrimShort:
1057 case Primitive::kPrimInt:
1058 case Primitive::kPrimNot: {
1059 return Location::RegisterLocation(R0);
1060 }
1061
1062 case Primitive::kPrimFloat: {
1063 return Location::FpuRegisterLocation(S0);
1064 }
1065
1066 case Primitive::kPrimLong: {
1067 return Location::RegisterPairLocation(R0, R1);
1068 }
1069
1070 case Primitive::kPrimDouble: {
1071 return Location::FpuRegisterPairLocation(S0, S1);
1072 }
1073
1074 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001075 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001076 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001077
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001078 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001079}
1080
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001081Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1082 return Location::RegisterLocation(kMethodRegisterArgument);
1083}
1084
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085void CodeGeneratorARM::Move32(Location destination, Location source) {
1086 if (source.Equals(destination)) {
1087 return;
1088 }
1089 if (destination.IsRegister()) {
1090 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001091 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001093 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001095 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 } else if (destination.IsFpuRegister()) {
1098 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001099 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001101 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001103 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001106 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001108 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001110 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001111 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001112 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001113 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1114 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 }
1116 }
1117}
1118
1119void CodeGeneratorARM::Move64(Location destination, Location source) {
1120 if (source.Equals(destination)) {
1121 return;
1122 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001123 if (destination.IsRegisterPair()) {
1124 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001125 EmitParallelMoves(
1126 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1127 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001128 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001129 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001130 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1131 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001133 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001134 } else if (source.IsFpuRegisterPair()) {
1135 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1136 destination.AsRegisterPairHigh<Register>(),
1137 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138 } else {
1139 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001140 DCHECK(ExpectedPairLayout(destination));
1141 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1142 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001144 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001145 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001146 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1147 SP,
1148 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001149 } else if (source.IsRegisterPair()) {
1150 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1151 source.AsRegisterPairLow<Register>(),
1152 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001153 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001154 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001155 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 } else {
1157 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001158 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001159 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001160 if (source.AsRegisterPairLow<Register>() == R1) {
1161 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001162 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1163 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001165 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 SP, destination.GetStackIndex());
1167 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001168 } else if (source.IsFpuRegisterPair()) {
1169 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1170 SP,
1171 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 } else {
1173 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001174 EmitParallelMoves(
1175 Location::StackSlot(source.GetStackIndex()),
1176 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001177 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001178 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001179 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1180 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 }
1182 }
1183}
1184
Calin Juravle175dc732015-08-25 15:42:32 +01001185void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1186 DCHECK(location.IsRegister());
1187 __ LoadImmediate(location.AsRegister<Register>(), value);
1188}
1189
Calin Juravlee460d1d2015-09-29 04:52:17 +01001190void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001191 HParallelMove move(GetGraph()->GetArena());
1192 move.AddMove(src, dst, dst_type, nullptr);
1193 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001194}
1195
1196void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1197 if (location.IsRegister()) {
1198 locations->AddTemp(location);
1199 } else if (location.IsRegisterPair()) {
1200 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1201 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1202 } else {
1203 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1204 }
1205}
1206
Calin Juravle175dc732015-08-25 15:42:32 +01001207void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1208 HInstruction* instruction,
1209 uint32_t dex_pc,
1210 SlowPathCode* slow_path) {
1211 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1212 instruction,
1213 dex_pc,
1214 slow_path);
1215}
1216
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001217void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1218 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001219 uint32_t dex_pc,
1220 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001221 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001222 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1223 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001224 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001225}
1226
David Brazdilfc6a86a2015-06-26 10:33:45 +00001227void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001228 DCHECK(!successor->IsExitBlock());
1229
1230 HBasicBlock* block = got->GetBlock();
1231 HInstruction* previous = got->GetPrevious();
1232
1233 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001234 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001235 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1236 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1237 return;
1238 }
1239
1240 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1241 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1242 }
1243 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001244 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001245 }
1246}
1247
David Brazdilfc6a86a2015-06-26 10:33:45 +00001248void LocationsBuilderARM::VisitGoto(HGoto* got) {
1249 got->SetLocations(nullptr);
1250}
1251
1252void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1253 HandleGoto(got, got->GetSuccessor());
1254}
1255
1256void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1257 try_boundary->SetLocations(nullptr);
1258}
1259
1260void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1261 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1262 if (!successor->IsExitBlock()) {
1263 HandleGoto(try_boundary, successor);
1264 }
1265}
1266
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001267void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001268 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001269}
1270
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001271void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001272}
1273
Roland Levillain4fa13f62015-07-06 18:11:54 +01001274void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1275 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001276 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001277 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001278 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001279}
1280
1281void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1282 Label* true_label,
1283 Label* false_label) {
1284 LocationSummary* locations = cond->GetLocations();
1285 Location left = locations->InAt(0);
1286 Location right = locations->InAt(1);
1287 IfCondition if_cond = cond->GetCondition();
1288
1289 Register left_high = left.AsRegisterPairHigh<Register>();
1290 Register left_low = left.AsRegisterPairLow<Register>();
1291 IfCondition true_high_cond = if_cond;
1292 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001293 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001294
1295 // Set the conditions for the test, remembering that == needs to be
1296 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001297 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001298 switch (if_cond) {
1299 case kCondEQ:
1300 case kCondNE:
1301 // Nothing to do.
1302 break;
1303 case kCondLT:
1304 false_high_cond = kCondGT;
1305 break;
1306 case kCondLE:
1307 true_high_cond = kCondLT;
1308 break;
1309 case kCondGT:
1310 false_high_cond = kCondLT;
1311 break;
1312 case kCondGE:
1313 true_high_cond = kCondGT;
1314 break;
Aart Bike9f37602015-10-09 11:15:55 -07001315 case kCondB:
1316 false_high_cond = kCondA;
1317 break;
1318 case kCondBE:
1319 true_high_cond = kCondB;
1320 break;
1321 case kCondA:
1322 false_high_cond = kCondB;
1323 break;
1324 case kCondAE:
1325 true_high_cond = kCondA;
1326 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001327 }
1328 if (right.IsConstant()) {
1329 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1330 int32_t val_low = Low32Bits(value);
1331 int32_t val_high = High32Bits(value);
1332
Vladimir Markoac6ac102015-12-17 12:14:00 +00001333 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001334 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001335 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001336 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001337 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001338 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001339 __ b(true_label, ARMCondition(true_high_cond));
1340 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001341 }
1342 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001343 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001344 } else {
1345 Register right_high = right.AsRegisterPairHigh<Register>();
1346 Register right_low = right.AsRegisterPairLow<Register>();
1347
1348 __ cmp(left_high, ShifterOperand(right_high));
1349 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001350 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001351 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001352 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001353 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001354 __ b(true_label, ARMCondition(true_high_cond));
1355 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001356 }
1357 // Must be equal high, so compare the lows.
1358 __ cmp(left_low, ShifterOperand(right_low));
1359 }
1360 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001361 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001362 __ b(true_label, final_condition);
1363}
1364
David Brazdil0debae72015-11-12 18:37:00 +00001365void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1366 Label* true_target_in,
1367 Label* false_target_in) {
1368 // Generated branching requires both targets to be explicit. If either of the
1369 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1370 Label fallthrough_target;
1371 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1372 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1373
Roland Levillain4fa13f62015-07-06 18:11:54 +01001374 LocationSummary* locations = condition->GetLocations();
1375 Location left = locations->InAt(0);
1376 Location right = locations->InAt(1);
1377
Roland Levillain4fa13f62015-07-06 18:11:54 +01001378 Primitive::Type type = condition->InputAt(0)->GetType();
1379 switch (type) {
1380 case Primitive::kPrimLong:
1381 GenerateLongComparesAndJumps(condition, true_target, false_target);
1382 break;
1383 case Primitive::kPrimFloat:
1384 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1385 GenerateFPJumps(condition, true_target, false_target);
1386 break;
1387 case Primitive::kPrimDouble:
1388 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1389 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1390 GenerateFPJumps(condition, true_target, false_target);
1391 break;
1392 default:
1393 LOG(FATAL) << "Unexpected compare type " << type;
1394 }
1395
David Brazdil0debae72015-11-12 18:37:00 +00001396 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397 __ b(false_target);
1398 }
David Brazdil0debae72015-11-12 18:37:00 +00001399
1400 if (fallthrough_target.IsLinked()) {
1401 __ Bind(&fallthrough_target);
1402 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001403}
1404
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001405void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001406 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001407 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001408 Label* false_target) {
1409 HInstruction* cond = instruction->InputAt(condition_input_index);
1410
1411 if (true_target == nullptr && false_target == nullptr) {
1412 // Nothing to do. The code always falls through.
1413 return;
1414 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001415 // Constant condition, statically compared against "true" (integer value 1).
1416 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001417 if (true_target != nullptr) {
1418 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001419 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001420 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001421 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001422 if (false_target != nullptr) {
1423 __ b(false_target);
1424 }
1425 }
1426 return;
1427 }
1428
1429 // The following code generates these patterns:
1430 // (1) true_target == nullptr && false_target != nullptr
1431 // - opposite condition true => branch to false_target
1432 // (2) true_target != nullptr && false_target == nullptr
1433 // - condition true => branch to true_target
1434 // (3) true_target != nullptr && false_target != nullptr
1435 // - condition true => branch to true_target
1436 // - branch to false_target
1437 if (IsBooleanValueOrMaterializedCondition(cond)) {
1438 // Condition has been materialized, compare the output to 0.
1439 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1440 DCHECK(cond_val.IsRegister());
1441 if (true_target == nullptr) {
1442 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1443 } else {
1444 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001445 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001446 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001447 // Condition has not been materialized. Use its inputs as the comparison and
1448 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001449 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001450
1451 // If this is a long or FP comparison that has been folded into
1452 // the HCondition, generate the comparison directly.
1453 Primitive::Type type = condition->InputAt(0)->GetType();
1454 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1455 GenerateCompareTestAndBranch(condition, true_target, false_target);
1456 return;
1457 }
1458
1459 LocationSummary* locations = cond->GetLocations();
1460 DCHECK(locations->InAt(0).IsRegister());
1461 Register left = locations->InAt(0).AsRegister<Register>();
1462 Location right = locations->InAt(1);
1463 if (right.IsRegister()) {
1464 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001465 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001466 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001467 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001468 }
1469 if (true_target == nullptr) {
1470 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1471 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001472 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001473 }
Dave Allison20dfc792014-06-16 20:44:29 -07001474 }
David Brazdil0debae72015-11-12 18:37:00 +00001475
1476 // If neither branch falls through (case 3), the conditional branch to `true_target`
1477 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1478 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001479 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001480 }
1481}
1482
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001483void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1485 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001486 locations->SetInAt(0, Location::RequiresRegister());
1487 }
1488}
1489
1490void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001491 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1492 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1493 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1494 nullptr : codegen_->GetLabelOf(true_successor);
1495 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1496 nullptr : codegen_->GetLabelOf(false_successor);
1497 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001498}
1499
1500void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1501 LocationSummary* locations = new (GetGraph()->GetArena())
1502 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001503 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001504 locations->SetInAt(0, Location::RequiresRegister());
1505 }
1506}
1507
1508void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001509 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001510 GenerateTestAndBranch(deoptimize,
1511 /* condition_input_index */ 0,
1512 slow_path->GetEntryLabel(),
1513 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001514}
Dave Allison20dfc792014-06-16 20:44:29 -07001515
David Brazdil74eb1b22015-12-14 11:44:01 +00001516void LocationsBuilderARM::VisitSelect(HSelect* select) {
1517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1518 if (Primitive::IsFloatingPointType(select->GetType())) {
1519 locations->SetInAt(0, Location::RequiresFpuRegister());
1520 locations->SetInAt(1, Location::RequiresFpuRegister());
1521 } else {
1522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetInAt(1, Location::RequiresRegister());
1524 }
1525 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1526 locations->SetInAt(2, Location::RequiresRegister());
1527 }
1528 locations->SetOut(Location::SameAsFirstInput());
1529}
1530
1531void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1532 LocationSummary* locations = select->GetLocations();
1533 Label false_target;
1534 GenerateTestAndBranch(select,
1535 /* condition_input_index */ 2,
1536 /* true_target */ nullptr,
1537 &false_target);
1538 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1539 __ Bind(&false_target);
1540}
1541
David Srbecky0cf44932015-12-09 14:09:59 +00001542void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1543 new (GetGraph()->GetArena()) LocationSummary(info);
1544}
1545
David Srbeckyd28f4a02016-03-14 17:14:24 +00001546void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1547 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001548}
1549
1550void CodeGeneratorARM::GenerateNop() {
1551 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001552}
1553
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001554void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001555 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001556 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001557 // Handle the long/FP comparisons made in instruction simplification.
1558 switch (cond->InputAt(0)->GetType()) {
1559 case Primitive::kPrimLong:
1560 locations->SetInAt(0, Location::RequiresRegister());
1561 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001562 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001563 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1564 }
1565 break;
1566
1567 case Primitive::kPrimFloat:
1568 case Primitive::kPrimDouble:
1569 locations->SetInAt(0, Location::RequiresFpuRegister());
1570 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001571 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1573 }
1574 break;
1575
1576 default:
1577 locations->SetInAt(0, Location::RequiresRegister());
1578 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001579 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1581 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001582 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001583}
1584
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001585void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001586 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001587 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001588 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001589
1590 LocationSummary* locations = cond->GetLocations();
1591 Location left = locations->InAt(0);
1592 Location right = locations->InAt(1);
1593 Register out = locations->Out().AsRegister<Register>();
1594 Label true_label, false_label;
1595
1596 switch (cond->InputAt(0)->GetType()) {
1597 default: {
1598 // Integer case.
1599 if (right.IsRegister()) {
1600 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1601 } else {
1602 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001603 __ CmpConstant(left.AsRegister<Register>(),
1604 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001605 }
Aart Bike9f37602015-10-09 11:15:55 -07001606 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001607 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001608 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001609 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001610 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001611 return;
1612 }
1613 case Primitive::kPrimLong:
1614 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1615 break;
1616 case Primitive::kPrimFloat:
1617 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1618 GenerateFPJumps(cond, &true_label, &false_label);
1619 break;
1620 case Primitive::kPrimDouble:
1621 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1622 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1623 GenerateFPJumps(cond, &true_label, &false_label);
1624 break;
1625 }
1626
1627 // Convert the jumps into the result.
1628 Label done_label;
1629
1630 // False case: result = 0.
1631 __ Bind(&false_label);
1632 __ LoadImmediate(out, 0);
1633 __ b(&done_label);
1634
1635 // True case: result = 1.
1636 __ Bind(&true_label);
1637 __ LoadImmediate(out, 1);
1638 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001639}
1640
1641void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001642 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001643}
1644
1645void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001646 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001647}
1648
1649void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001650 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001651}
1652
1653void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001654 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001655}
1656
1657void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001658 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001659}
1660
1661void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001662 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001663}
1664
1665void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001666 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001667}
1668
1669void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001670 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001671}
1672
1673void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001674 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001675}
1676
1677void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001678 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001679}
1680
1681void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001682 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001683}
1684
1685void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001686 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001687}
1688
Aart Bike9f37602015-10-09 11:15:55 -07001689void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001690 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001691}
1692
1693void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001694 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001695}
1696
1697void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001698 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001699}
1700
1701void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001703}
1704
1705void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001706 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001707}
1708
1709void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001710 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001711}
1712
1713void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001715}
1716
1717void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001719}
1720
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001721void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001722 LocationSummary* locations =
1723 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001724 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001725}
1726
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001727void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001728 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001729}
1730
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001731void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1732 LocationSummary* locations =
1733 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1734 locations->SetOut(Location::ConstantLocation(constant));
1735}
1736
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001737void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001738 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001739}
1740
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001741void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001742 LocationSummary* locations =
1743 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001744 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001745}
1746
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001747void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001748 // Will be generated at use site.
1749}
1750
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001751void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1752 LocationSummary* locations =
1753 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1754 locations->SetOut(Location::ConstantLocation(constant));
1755}
1756
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001757void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001758 // Will be generated at use site.
1759}
1760
1761void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1762 LocationSummary* locations =
1763 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1764 locations->SetOut(Location::ConstantLocation(constant));
1765}
1766
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001767void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001768 // Will be generated at use site.
1769}
1770
Calin Juravle27df7582015-04-17 19:12:31 +01001771void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1772 memory_barrier->SetLocations(nullptr);
1773}
1774
1775void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001776 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001777}
1778
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001779void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001780 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001781}
1782
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001783void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001784 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001785}
1786
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001787void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001790 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001791}
1792
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001793void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001794 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795}
1796
Calin Juravle175dc732015-08-25 15:42:32 +01001797void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1798 // The trampoline uses the same calling convention as dex calling conventions,
1799 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1800 // the method_idx.
1801 HandleInvoke(invoke);
1802}
1803
1804void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1805 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1806}
1807
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001808void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001809 // Explicit clinit checks triggered by static invokes must have been pruned by
1810 // art::PrepareForRegisterAllocation.
1811 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001812
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001813 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001814 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001815 codegen_->GetInstructionSetFeatures());
1816 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001817 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1818 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1819 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001820 return;
1821 }
1822
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001823 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001824
1825 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1826 if (invoke->HasPcRelativeDexCache()) {
1827 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1828 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001829}
1830
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001831static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1832 if (invoke->GetLocations()->Intrinsified()) {
1833 IntrinsicCodeGeneratorARM intrinsic(codegen);
1834 intrinsic.Dispatch(invoke);
1835 return true;
1836 }
1837 return false;
1838}
1839
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001840void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001841 // Explicit clinit checks triggered by static invokes must have been pruned by
1842 // art::PrepareForRegisterAllocation.
1843 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001844
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001845 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1846 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001847 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001848
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001849 LocationSummary* locations = invoke->GetLocations();
1850 codegen_->GenerateStaticOrDirectCall(
1851 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001852 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001853}
1854
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001855void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001856 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001857 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001858}
1859
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001860void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001861 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001862 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001863 codegen_->GetInstructionSetFeatures());
1864 if (intrinsic.TryDispatch(invoke)) {
1865 return;
1866 }
1867
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001868 HandleInvoke(invoke);
1869}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001870
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001871void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001872 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1873 return;
1874 }
1875
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001876 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001877 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001878 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001879}
1880
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001881void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1882 HandleInvoke(invoke);
1883 // Add the hidden argument.
1884 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1885}
1886
1887void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1888 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001889 LocationSummary* locations = invoke->GetLocations();
1890 Register temp = locations->GetTemp(0).AsRegister<Register>();
1891 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001892 Location receiver = locations->InAt(0);
1893 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1894
Roland Levillain3b359c72015-11-17 19:35:12 +00001895 // Set the hidden argument. This is safe to do this here, as R12
1896 // won't be modified thereafter, before the `blx` (call) instruction.
1897 DCHECK_EQ(R12, hidden_reg);
1898 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001899
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001900 if (receiver.IsStackSlot()) {
1901 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001902 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001903 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1904 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001905 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001906 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001907 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001908 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001909 // Instead of simply (possibly) unpoisoning `temp` here, we should
1910 // emit a read barrier for the previous class reference load.
1911 // However this is not required in practice, as this is an
1912 // intermediate/temporary reference and because the current
1913 // concurrent copying collector keeps the from-space memory
1914 // intact/accessible until the end of the marking phase (the
1915 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001916 __ MaybeUnpoisonHeapReference(temp);
Nelli Kimbadee982016-05-13 13:08:53 +03001917 __ LoadFromOffset(kLoadWord, temp, temp,
1918 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1919 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Nicolas Geoffrayd4ceecc2016-06-29 08:39:47 +00001920 invoke->GetImtIndex() % ImTable::kSize, kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001921 // temp = temp->GetImtEntryAt(method_offset);
Nelli Kimbadee982016-05-13 13:08:53 +03001922 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001923 uint32_t entry_point =
1924 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001925 // LR = temp->GetEntryPoint();
1926 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1927 // LR();
1928 __ blx(LR);
1929 DCHECK(!codegen_->IsLeafMethod());
1930 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1931}
1932
Roland Levillain88cb1752014-10-20 16:36:47 +01001933void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1934 LocationSummary* locations =
1935 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1936 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001937 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001938 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1940 break;
1941 }
1942 case Primitive::kPrimLong: {
1943 locations->SetInAt(0, Location::RequiresRegister());
1944 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001945 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001946 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001947
Roland Levillain88cb1752014-10-20 16:36:47 +01001948 case Primitive::kPrimFloat:
1949 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001950 locations->SetInAt(0, Location::RequiresFpuRegister());
1951 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001952 break;
1953
1954 default:
1955 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1956 }
1957}
1958
1959void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1960 LocationSummary* locations = neg->GetLocations();
1961 Location out = locations->Out();
1962 Location in = locations->InAt(0);
1963 switch (neg->GetResultType()) {
1964 case Primitive::kPrimInt:
1965 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001966 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001967 break;
1968
1969 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001970 DCHECK(in.IsRegisterPair());
1971 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1972 __ rsbs(out.AsRegisterPairLow<Register>(),
1973 in.AsRegisterPairLow<Register>(),
1974 ShifterOperand(0));
1975 // We cannot emit an RSC (Reverse Subtract with Carry)
1976 // instruction here, as it does not exist in the Thumb-2
1977 // instruction set. We use the following approach
1978 // using SBC and SUB instead.
1979 //
1980 // out.hi = -C
1981 __ sbc(out.AsRegisterPairHigh<Register>(),
1982 out.AsRegisterPairHigh<Register>(),
1983 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1984 // out.hi = out.hi - in.hi
1985 __ sub(out.AsRegisterPairHigh<Register>(),
1986 out.AsRegisterPairHigh<Register>(),
1987 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1988 break;
1989
Roland Levillain88cb1752014-10-20 16:36:47 +01001990 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001991 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001992 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001993 break;
1994
Roland Levillain88cb1752014-10-20 16:36:47 +01001995 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001996 DCHECK(in.IsFpuRegisterPair());
1997 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1998 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001999 break;
2000
2001 default:
2002 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2003 }
2004}
2005
Roland Levillaindff1f282014-11-05 14:15:05 +00002006void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002007 Primitive::Type result_type = conversion->GetResultType();
2008 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002009 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002010
Roland Levillain5b3ee562015-04-14 16:02:41 +01002011 // The float-to-long, double-to-long and long-to-float type conversions
2012 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002013 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002014 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2015 && result_type == Primitive::kPrimLong)
2016 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002017 ? LocationSummary::kCall
2018 : LocationSummary::kNoCall;
2019 LocationSummary* locations =
2020 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2021
David Brazdilb2bd1c52015-03-25 11:17:37 +00002022 // The Java language does not allow treating boolean as an integral type but
2023 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002024
Roland Levillaindff1f282014-11-05 14:15:05 +00002025 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002026 case Primitive::kPrimByte:
2027 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002028 case Primitive::kPrimLong:
2029 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002030 case Primitive::kPrimBoolean:
2031 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002032 case Primitive::kPrimShort:
2033 case Primitive::kPrimInt:
2034 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002035 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002036 locations->SetInAt(0, Location::RequiresRegister());
2037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2038 break;
2039
2040 default:
2041 LOG(FATAL) << "Unexpected type conversion from " << input_type
2042 << " to " << result_type;
2043 }
2044 break;
2045
Roland Levillain01a8d712014-11-14 16:27:39 +00002046 case Primitive::kPrimShort:
2047 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002048 case Primitive::kPrimLong:
2049 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002050 case Primitive::kPrimBoolean:
2051 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002052 case Primitive::kPrimByte:
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimChar:
2055 // Processing a Dex `int-to-short' instruction.
2056 locations->SetInAt(0, Location::RequiresRegister());
2057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2058 break;
2059
2060 default:
2061 LOG(FATAL) << "Unexpected type conversion from " << input_type
2062 << " to " << result_type;
2063 }
2064 break;
2065
Roland Levillain946e1432014-11-11 17:35:19 +00002066 case Primitive::kPrimInt:
2067 switch (input_type) {
2068 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002069 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002070 locations->SetInAt(0, Location::Any());
2071 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2072 break;
2073
2074 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002075 // Processing a Dex `float-to-int' instruction.
2076 locations->SetInAt(0, Location::RequiresFpuRegister());
2077 locations->SetOut(Location::RequiresRegister());
2078 locations->AddTemp(Location::RequiresFpuRegister());
2079 break;
2080
Roland Levillain946e1432014-11-11 17:35:19 +00002081 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002082 // Processing a Dex `double-to-int' instruction.
2083 locations->SetInAt(0, Location::RequiresFpuRegister());
2084 locations->SetOut(Location::RequiresRegister());
2085 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002086 break;
2087
2088 default:
2089 LOG(FATAL) << "Unexpected type conversion from " << input_type
2090 << " to " << result_type;
2091 }
2092 break;
2093
Roland Levillaindff1f282014-11-05 14:15:05 +00002094 case Primitive::kPrimLong:
2095 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002096 case Primitive::kPrimBoolean:
2097 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002098 case Primitive::kPrimByte:
2099 case Primitive::kPrimShort:
2100 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002101 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002102 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002103 locations->SetInAt(0, Location::RequiresRegister());
2104 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2105 break;
2106
Roland Levillain624279f2014-12-04 11:54:28 +00002107 case Primitive::kPrimFloat: {
2108 // Processing a Dex `float-to-long' instruction.
2109 InvokeRuntimeCallingConvention calling_convention;
2110 locations->SetInAt(0, Location::FpuRegisterLocation(
2111 calling_convention.GetFpuRegisterAt(0)));
2112 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2113 break;
2114 }
2115
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002116 case Primitive::kPrimDouble: {
2117 // Processing a Dex `double-to-long' instruction.
2118 InvokeRuntimeCallingConvention calling_convention;
2119 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2120 calling_convention.GetFpuRegisterAt(0),
2121 calling_convention.GetFpuRegisterAt(1)));
2122 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002123 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002124 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002125
2126 default:
2127 LOG(FATAL) << "Unexpected type conversion from " << input_type
2128 << " to " << result_type;
2129 }
2130 break;
2131
Roland Levillain981e4542014-11-14 11:47:14 +00002132 case Primitive::kPrimChar:
2133 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002134 case Primitive::kPrimLong:
2135 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002136 case Primitive::kPrimBoolean:
2137 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002138 case Primitive::kPrimByte:
2139 case Primitive::kPrimShort:
2140 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002141 // Processing a Dex `int-to-char' instruction.
2142 locations->SetInAt(0, Location::RequiresRegister());
2143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2144 break;
2145
2146 default:
2147 LOG(FATAL) << "Unexpected type conversion from " << input_type
2148 << " to " << result_type;
2149 }
2150 break;
2151
Roland Levillaindff1f282014-11-05 14:15:05 +00002152 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002153 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002154 case Primitive::kPrimBoolean:
2155 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002156 case Primitive::kPrimByte:
2157 case Primitive::kPrimShort:
2158 case Primitive::kPrimInt:
2159 case Primitive::kPrimChar:
2160 // Processing a Dex `int-to-float' instruction.
2161 locations->SetInAt(0, Location::RequiresRegister());
2162 locations->SetOut(Location::RequiresFpuRegister());
2163 break;
2164
Roland Levillain5b3ee562015-04-14 16:02:41 +01002165 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002166 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002167 InvokeRuntimeCallingConvention calling_convention;
2168 locations->SetInAt(0, Location::RegisterPairLocation(
2169 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2170 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002171 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002172 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002173
Roland Levillaincff13742014-11-17 14:32:17 +00002174 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002175 // Processing a Dex `double-to-float' instruction.
2176 locations->SetInAt(0, Location::RequiresFpuRegister());
2177 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002178 break;
2179
2180 default:
2181 LOG(FATAL) << "Unexpected type conversion from " << input_type
2182 << " to " << result_type;
2183 };
2184 break;
2185
Roland Levillaindff1f282014-11-05 14:15:05 +00002186 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002187 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002188 case Primitive::kPrimBoolean:
2189 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002190 case Primitive::kPrimByte:
2191 case Primitive::kPrimShort:
2192 case Primitive::kPrimInt:
2193 case Primitive::kPrimChar:
2194 // Processing a Dex `int-to-double' instruction.
2195 locations->SetInAt(0, Location::RequiresRegister());
2196 locations->SetOut(Location::RequiresFpuRegister());
2197 break;
2198
2199 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002200 // Processing a Dex `long-to-double' instruction.
2201 locations->SetInAt(0, Location::RequiresRegister());
2202 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002203 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002204 locations->AddTemp(Location::RequiresFpuRegister());
2205 break;
2206
Roland Levillaincff13742014-11-17 14:32:17 +00002207 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002208 // Processing a Dex `float-to-double' instruction.
2209 locations->SetInAt(0, Location::RequiresFpuRegister());
2210 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002211 break;
2212
2213 default:
2214 LOG(FATAL) << "Unexpected type conversion from " << input_type
2215 << " to " << result_type;
2216 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002217 break;
2218
2219 default:
2220 LOG(FATAL) << "Unexpected type conversion from " << input_type
2221 << " to " << result_type;
2222 }
2223}
2224
2225void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2226 LocationSummary* locations = conversion->GetLocations();
2227 Location out = locations->Out();
2228 Location in = locations->InAt(0);
2229 Primitive::Type result_type = conversion->GetResultType();
2230 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002231 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002232 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002233 case Primitive::kPrimByte:
2234 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002235 case Primitive::kPrimLong:
2236 // Type conversion from long to byte is a result of code transformations.
2237 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2238 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002239 case Primitive::kPrimBoolean:
2240 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002241 case Primitive::kPrimShort:
2242 case Primitive::kPrimInt:
2243 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002244 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002245 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002246 break;
2247
2248 default:
2249 LOG(FATAL) << "Unexpected type conversion from " << input_type
2250 << " to " << result_type;
2251 }
2252 break;
2253
Roland Levillain01a8d712014-11-14 16:27:39 +00002254 case Primitive::kPrimShort:
2255 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002256 case Primitive::kPrimLong:
2257 // Type conversion from long to short is a result of code transformations.
2258 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2259 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002260 case Primitive::kPrimBoolean:
2261 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002262 case Primitive::kPrimByte:
2263 case Primitive::kPrimInt:
2264 case Primitive::kPrimChar:
2265 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002266 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002267 break;
2268
2269 default:
2270 LOG(FATAL) << "Unexpected type conversion from " << input_type
2271 << " to " << result_type;
2272 }
2273 break;
2274
Roland Levillain946e1432014-11-11 17:35:19 +00002275 case Primitive::kPrimInt:
2276 switch (input_type) {
2277 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002278 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002279 DCHECK(out.IsRegister());
2280 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002281 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002282 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002283 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002284 } else {
2285 DCHECK(in.IsConstant());
2286 DCHECK(in.GetConstant()->IsLongConstant());
2287 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002288 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002289 }
2290 break;
2291
Roland Levillain3f8f9362014-12-02 17:45:01 +00002292 case Primitive::kPrimFloat: {
2293 // Processing a Dex `float-to-int' instruction.
2294 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2295 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2296 __ vcvtis(temp, temp);
2297 __ vmovrs(out.AsRegister<Register>(), temp);
2298 break;
2299 }
2300
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002301 case Primitive::kPrimDouble: {
2302 // Processing a Dex `double-to-int' instruction.
2303 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2304 DRegister temp_d = FromLowSToD(temp_s);
2305 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2306 __ vcvtid(temp_s, temp_d);
2307 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002308 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002309 }
Roland Levillain946e1432014-11-11 17:35:19 +00002310
2311 default:
2312 LOG(FATAL) << "Unexpected type conversion from " << input_type
2313 << " to " << result_type;
2314 }
2315 break;
2316
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 case Primitive::kPrimLong:
2318 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002319 case Primitive::kPrimBoolean:
2320 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 case Primitive::kPrimByte:
2322 case Primitive::kPrimShort:
2323 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002324 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002325 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002326 DCHECK(out.IsRegisterPair());
2327 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002328 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002329 // Sign extension.
2330 __ Asr(out.AsRegisterPairHigh<Register>(),
2331 out.AsRegisterPairLow<Register>(),
2332 31);
2333 break;
2334
2335 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002336 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002337 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2338 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002339 conversion->GetDexPc(),
2340 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002341 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002342 break;
2343
Roland Levillaindff1f282014-11-05 14:15:05 +00002344 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002345 // Processing a Dex `double-to-long' instruction.
2346 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2347 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002348 conversion->GetDexPc(),
2349 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002350 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002351 break;
2352
2353 default:
2354 LOG(FATAL) << "Unexpected type conversion from " << input_type
2355 << " to " << result_type;
2356 }
2357 break;
2358
Roland Levillain981e4542014-11-14 11:47:14 +00002359 case Primitive::kPrimChar:
2360 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002361 case Primitive::kPrimLong:
2362 // Type conversion from long to char is a result of code transformations.
2363 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2364 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002365 case Primitive::kPrimBoolean:
2366 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002367 case Primitive::kPrimByte:
2368 case Primitive::kPrimShort:
2369 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002370 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002371 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002372 break;
2373
2374 default:
2375 LOG(FATAL) << "Unexpected type conversion from " << input_type
2376 << " to " << result_type;
2377 }
2378 break;
2379
Roland Levillaindff1f282014-11-05 14:15:05 +00002380 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002381 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002382 case Primitive::kPrimBoolean:
2383 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002384 case Primitive::kPrimByte:
2385 case Primitive::kPrimShort:
2386 case Primitive::kPrimInt:
2387 case Primitive::kPrimChar: {
2388 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002389 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2390 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002391 break;
2392 }
2393
Roland Levillain5b3ee562015-04-14 16:02:41 +01002394 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002395 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002396 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2397 conversion,
2398 conversion->GetDexPc(),
2399 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002400 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002401 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002402
Roland Levillaincff13742014-11-17 14:32:17 +00002403 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002404 // Processing a Dex `double-to-float' instruction.
2405 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2406 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002407 break;
2408
2409 default:
2410 LOG(FATAL) << "Unexpected type conversion from " << input_type
2411 << " to " << result_type;
2412 };
2413 break;
2414
Roland Levillaindff1f282014-11-05 14:15:05 +00002415 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002416 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002417 case Primitive::kPrimBoolean:
2418 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002419 case Primitive::kPrimByte:
2420 case Primitive::kPrimShort:
2421 case Primitive::kPrimInt:
2422 case Primitive::kPrimChar: {
2423 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002424 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002425 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2426 out.AsFpuRegisterPairLow<SRegister>());
2427 break;
2428 }
2429
Roland Levillain647b9ed2014-11-27 12:06:00 +00002430 case Primitive::kPrimLong: {
2431 // Processing a Dex `long-to-double' instruction.
2432 Register low = in.AsRegisterPairLow<Register>();
2433 Register high = in.AsRegisterPairHigh<Register>();
2434 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2435 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002436 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002437 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002438 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2439 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002440
Roland Levillain682393c2015-04-14 15:57:52 +01002441 // temp_d = int-to-double(high)
2442 __ vmovsr(temp_s, high);
2443 __ vcvtdi(temp_d, temp_s);
2444 // constant_d = k2Pow32EncodingForDouble
2445 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2446 // out_d = unsigned-to-double(low)
2447 __ vmovsr(out_s, low);
2448 __ vcvtdu(out_d, out_s);
2449 // out_d += temp_d * constant_d
2450 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002451 break;
2452 }
2453
Roland Levillaincff13742014-11-17 14:32:17 +00002454 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002455 // Processing a Dex `float-to-double' instruction.
2456 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2457 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002458 break;
2459
2460 default:
2461 LOG(FATAL) << "Unexpected type conversion from " << input_type
2462 << " to " << result_type;
2463 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002464 break;
2465
2466 default:
2467 LOG(FATAL) << "Unexpected type conversion from " << input_type
2468 << " to " << result_type;
2469 }
2470}
2471
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002472void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002473 LocationSummary* locations =
2474 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002475 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002476 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002477 locations->SetInAt(0, Location::RequiresRegister());
2478 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2480 break;
2481 }
2482
2483 case Primitive::kPrimLong: {
2484 locations->SetInAt(0, Location::RequiresRegister());
2485 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002487 break;
2488 }
2489
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002490 case Primitive::kPrimFloat:
2491 case Primitive::kPrimDouble: {
2492 locations->SetInAt(0, Location::RequiresFpuRegister());
2493 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002495 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002496 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002497
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002498 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002499 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002500 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002501}
2502
2503void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2504 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002505 Location out = locations->Out();
2506 Location first = locations->InAt(0);
2507 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002508 switch (add->GetResultType()) {
2509 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002510 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002511 __ add(out.AsRegister<Register>(),
2512 first.AsRegister<Register>(),
2513 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002514 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ AddConstant(out.AsRegister<Register>(),
2516 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002517 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002518 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002519 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002520
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002521 case Primitive::kPrimLong: {
2522 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002523 __ adds(out.AsRegisterPairLow<Register>(),
2524 first.AsRegisterPairLow<Register>(),
2525 ShifterOperand(second.AsRegisterPairLow<Register>()));
2526 __ adc(out.AsRegisterPairHigh<Register>(),
2527 first.AsRegisterPairHigh<Register>(),
2528 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002529 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002530 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002531
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002532 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002533 __ vadds(out.AsFpuRegister<SRegister>(),
2534 first.AsFpuRegister<SRegister>(),
2535 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002536 break;
2537
2538 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002539 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2540 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2541 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002542 break;
2543
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002544 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002545 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002546 }
2547}
2548
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002549void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002550 LocationSummary* locations =
2551 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002552 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002553 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002554 locations->SetInAt(0, Location::RequiresRegister());
2555 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002556 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2557 break;
2558 }
2559
2560 case Primitive::kPrimLong: {
2561 locations->SetInAt(0, Location::RequiresRegister());
2562 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002564 break;
2565 }
Calin Juravle11351682014-10-23 15:38:15 +01002566 case Primitive::kPrimFloat:
2567 case Primitive::kPrimDouble: {
2568 locations->SetInAt(0, Location::RequiresFpuRegister());
2569 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002570 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002571 break;
Calin Juravle11351682014-10-23 15:38:15 +01002572 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002573 default:
Calin Juravle11351682014-10-23 15:38:15 +01002574 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002575 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002576}
2577
2578void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2579 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002580 Location out = locations->Out();
2581 Location first = locations->InAt(0);
2582 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002583 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002584 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002585 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002586 __ sub(out.AsRegister<Register>(),
2587 first.AsRegister<Register>(),
2588 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002589 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 __ AddConstant(out.AsRegister<Register>(),
2591 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002592 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002593 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002594 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002595 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002596
Calin Juravle11351682014-10-23 15:38:15 +01002597 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002598 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002599 __ subs(out.AsRegisterPairLow<Register>(),
2600 first.AsRegisterPairLow<Register>(),
2601 ShifterOperand(second.AsRegisterPairLow<Register>()));
2602 __ sbc(out.AsRegisterPairHigh<Register>(),
2603 first.AsRegisterPairHigh<Register>(),
2604 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002605 break;
Calin Juravle11351682014-10-23 15:38:15 +01002606 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002607
Calin Juravle11351682014-10-23 15:38:15 +01002608 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002609 __ vsubs(out.AsFpuRegister<SRegister>(),
2610 first.AsFpuRegister<SRegister>(),
2611 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002612 break;
Calin Juravle11351682014-10-23 15:38:15 +01002613 }
2614
2615 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002616 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2617 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2618 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002619 break;
2620 }
2621
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002622
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002623 default:
Calin Juravle11351682014-10-23 15:38:15 +01002624 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002625 }
2626}
2627
Calin Juravle34bacdf2014-10-07 20:23:36 +01002628void LocationsBuilderARM::VisitMul(HMul* mul) {
2629 LocationSummary* locations =
2630 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2631 switch (mul->GetResultType()) {
2632 case Primitive::kPrimInt:
2633 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002634 locations->SetInAt(0, Location::RequiresRegister());
2635 locations->SetInAt(1, Location::RequiresRegister());
2636 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002637 break;
2638 }
2639
Calin Juravleb5bfa962014-10-21 18:02:24 +01002640 case Primitive::kPrimFloat:
2641 case Primitive::kPrimDouble: {
2642 locations->SetInAt(0, Location::RequiresFpuRegister());
2643 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002644 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002645 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002646 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002647
2648 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002649 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002650 }
2651}
2652
2653void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2654 LocationSummary* locations = mul->GetLocations();
2655 Location out = locations->Out();
2656 Location first = locations->InAt(0);
2657 Location second = locations->InAt(1);
2658 switch (mul->GetResultType()) {
2659 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002660 __ mul(out.AsRegister<Register>(),
2661 first.AsRegister<Register>(),
2662 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002663 break;
2664 }
2665 case Primitive::kPrimLong: {
2666 Register out_hi = out.AsRegisterPairHigh<Register>();
2667 Register out_lo = out.AsRegisterPairLow<Register>();
2668 Register in1_hi = first.AsRegisterPairHigh<Register>();
2669 Register in1_lo = first.AsRegisterPairLow<Register>();
2670 Register in2_hi = second.AsRegisterPairHigh<Register>();
2671 Register in2_lo = second.AsRegisterPairLow<Register>();
2672
2673 // Extra checks to protect caused by the existence of R1_R2.
2674 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2675 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2676 DCHECK_NE(out_hi, in1_lo);
2677 DCHECK_NE(out_hi, in2_lo);
2678
2679 // input: in1 - 64 bits, in2 - 64 bits
2680 // output: out
2681 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2682 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2683 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2684
2685 // IP <- in1.lo * in2.hi
2686 __ mul(IP, in1_lo, in2_hi);
2687 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2688 __ mla(out_hi, in1_hi, in2_lo, IP);
2689 // out.lo <- (in1.lo * in2.lo)[31:0];
2690 __ umull(out_lo, IP, in1_lo, in2_lo);
2691 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2692 __ add(out_hi, out_hi, ShifterOperand(IP));
2693 break;
2694 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002695
2696 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002697 __ vmuls(out.AsFpuRegister<SRegister>(),
2698 first.AsFpuRegister<SRegister>(),
2699 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002700 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002701 }
2702
2703 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002704 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2705 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2706 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002707 break;
2708 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002709
2710 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002711 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002712 }
2713}
2714
Zheng Xuc6667102015-05-15 16:08:45 +08002715void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2716 DCHECK(instruction->IsDiv() || instruction->IsRem());
2717 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2718
2719 LocationSummary* locations = instruction->GetLocations();
2720 Location second = locations->InAt(1);
2721 DCHECK(second.IsConstant());
2722
2723 Register out = locations->Out().AsRegister<Register>();
2724 Register dividend = locations->InAt(0).AsRegister<Register>();
2725 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2726 DCHECK(imm == 1 || imm == -1);
2727
2728 if (instruction->IsRem()) {
2729 __ LoadImmediate(out, 0);
2730 } else {
2731 if (imm == 1) {
2732 __ Mov(out, dividend);
2733 } else {
2734 __ rsb(out, dividend, ShifterOperand(0));
2735 }
2736 }
2737}
2738
2739void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2740 DCHECK(instruction->IsDiv() || instruction->IsRem());
2741 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2742
2743 LocationSummary* locations = instruction->GetLocations();
2744 Location second = locations->InAt(1);
2745 DCHECK(second.IsConstant());
2746
2747 Register out = locations->Out().AsRegister<Register>();
2748 Register dividend = locations->InAt(0).AsRegister<Register>();
2749 Register temp = locations->GetTemp(0).AsRegister<Register>();
2750 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002751 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002752 int ctz_imm = CTZ(abs_imm);
2753
2754 if (ctz_imm == 1) {
2755 __ Lsr(temp, dividend, 32 - ctz_imm);
2756 } else {
2757 __ Asr(temp, dividend, 31);
2758 __ Lsr(temp, temp, 32 - ctz_imm);
2759 }
2760 __ add(out, temp, ShifterOperand(dividend));
2761
2762 if (instruction->IsDiv()) {
2763 __ Asr(out, out, ctz_imm);
2764 if (imm < 0) {
2765 __ rsb(out, out, ShifterOperand(0));
2766 }
2767 } else {
2768 __ ubfx(out, out, 0, ctz_imm);
2769 __ sub(out, out, ShifterOperand(temp));
2770 }
2771}
2772
2773void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2774 DCHECK(instruction->IsDiv() || instruction->IsRem());
2775 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2776
2777 LocationSummary* locations = instruction->GetLocations();
2778 Location second = locations->InAt(1);
2779 DCHECK(second.IsConstant());
2780
2781 Register out = locations->Out().AsRegister<Register>();
2782 Register dividend = locations->InAt(0).AsRegister<Register>();
2783 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2784 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2785 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2786
2787 int64_t magic;
2788 int shift;
2789 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2790
2791 __ LoadImmediate(temp1, magic);
2792 __ smull(temp2, temp1, dividend, temp1);
2793
2794 if (imm > 0 && magic < 0) {
2795 __ add(temp1, temp1, ShifterOperand(dividend));
2796 } else if (imm < 0 && magic > 0) {
2797 __ sub(temp1, temp1, ShifterOperand(dividend));
2798 }
2799
2800 if (shift != 0) {
2801 __ Asr(temp1, temp1, shift);
2802 }
2803
2804 if (instruction->IsDiv()) {
2805 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2806 } else {
2807 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2808 // TODO: Strength reduction for mls.
2809 __ LoadImmediate(temp2, imm);
2810 __ mls(out, temp1, temp2, dividend);
2811 }
2812}
2813
2814void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2815 DCHECK(instruction->IsDiv() || instruction->IsRem());
2816 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2817
2818 LocationSummary* locations = instruction->GetLocations();
2819 Location second = locations->InAt(1);
2820 DCHECK(second.IsConstant());
2821
2822 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2823 if (imm == 0) {
2824 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2825 } else if (imm == 1 || imm == -1) {
2826 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002827 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002828 DivRemByPowerOfTwo(instruction);
2829 } else {
2830 DCHECK(imm <= -2 || imm >= 2);
2831 GenerateDivRemWithAnyConstant(instruction);
2832 }
2833}
2834
Calin Juravle7c4954d2014-10-28 16:57:40 +00002835void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002836 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2837 if (div->GetResultType() == Primitive::kPrimLong) {
2838 // pLdiv runtime call.
2839 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002840 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2841 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002842 } else if (div->GetResultType() == Primitive::kPrimInt &&
2843 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2844 // pIdivmod runtime call.
2845 call_kind = LocationSummary::kCall;
2846 }
2847
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2849
Calin Juravle7c4954d2014-10-28 16:57:40 +00002850 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002851 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002852 if (div->InputAt(1)->IsConstant()) {
2853 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002854 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002856 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2857 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002858 // No temp register required.
2859 } else {
2860 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002861 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002862 locations->AddTemp(Location::RequiresRegister());
2863 }
2864 }
2865 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002866 locations->SetInAt(0, Location::RequiresRegister());
2867 locations->SetInAt(1, Location::RequiresRegister());
2868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2869 } else {
2870 InvokeRuntimeCallingConvention calling_convention;
2871 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2872 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2873 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2874 // we only need the former.
2875 locations->SetOut(Location::RegisterLocation(R0));
2876 }
Calin Juravled0d48522014-11-04 16:40:20 +00002877 break;
2878 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002879 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002880 InvokeRuntimeCallingConvention calling_convention;
2881 locations->SetInAt(0, Location::RegisterPairLocation(
2882 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2883 locations->SetInAt(1, Location::RegisterPairLocation(
2884 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002885 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002886 break;
2887 }
2888 case Primitive::kPrimFloat:
2889 case Primitive::kPrimDouble: {
2890 locations->SetInAt(0, Location::RequiresFpuRegister());
2891 locations->SetInAt(1, Location::RequiresFpuRegister());
2892 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2893 break;
2894 }
2895
2896 default:
2897 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2898 }
2899}
2900
2901void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2902 LocationSummary* locations = div->GetLocations();
2903 Location out = locations->Out();
2904 Location first = locations->InAt(0);
2905 Location second = locations->InAt(1);
2906
2907 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002908 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002909 if (second.IsConstant()) {
2910 GenerateDivRemConstantIntegral(div);
2911 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002912 __ sdiv(out.AsRegister<Register>(),
2913 first.AsRegister<Register>(),
2914 second.AsRegister<Register>());
2915 } else {
2916 InvokeRuntimeCallingConvention calling_convention;
2917 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2918 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2919 DCHECK_EQ(R0, out.AsRegister<Register>());
2920
2921 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002922 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002923 }
Calin Juravled0d48522014-11-04 16:40:20 +00002924 break;
2925 }
2926
Calin Juravle7c4954d2014-10-28 16:57:40 +00002927 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002928 InvokeRuntimeCallingConvention calling_convention;
2929 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2930 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2931 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2932 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2933 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002934 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002935
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002936 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002937 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002938 break;
2939 }
2940
2941 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002942 __ vdivs(out.AsFpuRegister<SRegister>(),
2943 first.AsFpuRegister<SRegister>(),
2944 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002945 break;
2946 }
2947
2948 case Primitive::kPrimDouble: {
2949 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2950 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2951 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2952 break;
2953 }
2954
2955 default:
2956 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2957 }
2958}
2959
Calin Juravlebacfec32014-11-14 15:54:36 +00002960void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002961 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002962
2963 // Most remainders are implemented in the runtime.
2964 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002965 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2966 // sdiv will be replaced by other instruction sequence.
2967 call_kind = LocationSummary::kNoCall;
2968 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2969 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002970 // Have hardware divide instruction for int, do it with three instructions.
2971 call_kind = LocationSummary::kNoCall;
2972 }
2973
Calin Juravlebacfec32014-11-14 15:54:36 +00002974 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2975
Calin Juravled2ec87d2014-12-08 14:24:46 +00002976 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002977 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002978 if (rem->InputAt(1)->IsConstant()) {
2979 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002980 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002981 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002982 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
2983 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002984 // No temp register required.
2985 } else {
2986 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002987 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002988 locations->AddTemp(Location::RequiresRegister());
2989 }
2990 }
2991 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002992 locations->SetInAt(0, Location::RequiresRegister());
2993 locations->SetInAt(1, Location::RequiresRegister());
2994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2995 locations->AddTemp(Location::RequiresRegister());
2996 } else {
2997 InvokeRuntimeCallingConvention calling_convention;
2998 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2999 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3000 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3001 // we only need the latter.
3002 locations->SetOut(Location::RegisterLocation(R1));
3003 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003004 break;
3005 }
3006 case Primitive::kPrimLong: {
3007 InvokeRuntimeCallingConvention calling_convention;
3008 locations->SetInAt(0, Location::RegisterPairLocation(
3009 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3010 locations->SetInAt(1, Location::RegisterPairLocation(
3011 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3012 // The runtime helper puts the output in R2,R3.
3013 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3014 break;
3015 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003016 case Primitive::kPrimFloat: {
3017 InvokeRuntimeCallingConvention calling_convention;
3018 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3019 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3020 locations->SetOut(Location::FpuRegisterLocation(S0));
3021 break;
3022 }
3023
Calin Juravlebacfec32014-11-14 15:54:36 +00003024 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003025 InvokeRuntimeCallingConvention calling_convention;
3026 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3027 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3028 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3029 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3030 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003031 break;
3032 }
3033
3034 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003035 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003036 }
3037}
3038
3039void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3040 LocationSummary* locations = rem->GetLocations();
3041 Location out = locations->Out();
3042 Location first = locations->InAt(0);
3043 Location second = locations->InAt(1);
3044
Calin Juravled2ec87d2014-12-08 14:24:46 +00003045 Primitive::Type type = rem->GetResultType();
3046 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003047 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003048 if (second.IsConstant()) {
3049 GenerateDivRemConstantIntegral(rem);
3050 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003051 Register reg1 = first.AsRegister<Register>();
3052 Register reg2 = second.AsRegister<Register>();
3053 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003054
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003055 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003056 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003057 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003058 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003059 } else {
3060 InvokeRuntimeCallingConvention calling_convention;
3061 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3062 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3063 DCHECK_EQ(R1, out.AsRegister<Register>());
3064
3065 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003066 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003067 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003068 break;
3069 }
3070
3071 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003072 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003073 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003074 break;
3075 }
3076
Calin Juravled2ec87d2014-12-08 14:24:46 +00003077 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003078 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003079 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003080 break;
3081 }
3082
Calin Juravlebacfec32014-11-14 15:54:36 +00003083 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003084 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003085 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003086 break;
3087 }
3088
3089 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003090 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003091 }
3092}
3093
Calin Juravled0d48522014-11-04 16:40:20 +00003094void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003095 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3096 ? LocationSummary::kCallOnSlowPath
3097 : LocationSummary::kNoCall;
3098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003099 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003100 if (instruction->HasUses()) {
3101 locations->SetOut(Location::SameAsFirstInput());
3102 }
3103}
3104
3105void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003106 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003107 codegen_->AddSlowPath(slow_path);
3108
3109 LocationSummary* locations = instruction->GetLocations();
3110 Location value = locations->InAt(0);
3111
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003112 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003113 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003114 case Primitive::kPrimByte:
3115 case Primitive::kPrimChar:
3116 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003117 case Primitive::kPrimInt: {
3118 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003119 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003120 } else {
3121 DCHECK(value.IsConstant()) << value;
3122 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3123 __ b(slow_path->GetEntryLabel());
3124 }
3125 }
3126 break;
3127 }
3128 case Primitive::kPrimLong: {
3129 if (value.IsRegisterPair()) {
3130 __ orrs(IP,
3131 value.AsRegisterPairLow<Register>(),
3132 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3133 __ b(slow_path->GetEntryLabel(), EQ);
3134 } else {
3135 DCHECK(value.IsConstant()) << value;
3136 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3137 __ b(slow_path->GetEntryLabel());
3138 }
3139 }
3140 break;
3141 default:
3142 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3143 }
3144 }
Calin Juravled0d48522014-11-04 16:40:20 +00003145}
3146
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003147void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3148 Register in = locations->InAt(0).AsRegister<Register>();
3149 Location rhs = locations->InAt(1);
3150 Register out = locations->Out().AsRegister<Register>();
3151
3152 if (rhs.IsConstant()) {
3153 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3154 // so map all rotations to a +ve. equivalent in that range.
3155 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3156 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3157 if (rot) {
3158 // Rotate, mapping left rotations to right equivalents if necessary.
3159 // (e.g. left by 2 bits == right by 30.)
3160 __ Ror(out, in, rot);
3161 } else if (out != in) {
3162 __ Mov(out, in);
3163 }
3164 } else {
3165 __ Ror(out, in, rhs.AsRegister<Register>());
3166 }
3167}
3168
3169// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3170// rotates by swapping input regs (effectively rotating by the first 32-bits of
3171// a larger rotation) or flipping direction (thus treating larger right/left
3172// rotations as sub-word sized rotations in the other direction) as appropriate.
3173void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3174 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3175 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3176 Location rhs = locations->InAt(1);
3177 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3178 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3179
3180 if (rhs.IsConstant()) {
3181 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3182 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003183 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003184 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3185 // logic below to a simple pair of binary orr.
3186 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3187 if (rot >= kArmBitsPerWord) {
3188 rot -= kArmBitsPerWord;
3189 std::swap(in_reg_hi, in_reg_lo);
3190 }
3191 // Rotate, or mov to out for zero or word size rotations.
3192 if (rot != 0u) {
3193 __ Lsr(out_reg_hi, in_reg_hi, rot);
3194 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3195 __ Lsr(out_reg_lo, in_reg_lo, rot);
3196 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3197 } else {
3198 __ Mov(out_reg_lo, in_reg_lo);
3199 __ Mov(out_reg_hi, in_reg_hi);
3200 }
3201 } else {
3202 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3203 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3204 Label end;
3205 Label shift_by_32_plus_shift_right;
3206
3207 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3208 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3209 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3210 __ b(&shift_by_32_plus_shift_right, CC);
3211
3212 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3213 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3214 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3215 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3216 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3217 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3218 __ Lsr(shift_left, in_reg_hi, shift_right);
3219 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3220 __ b(&end);
3221
3222 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3223 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3224 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3225 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3226 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3227 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3228 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3229 __ Lsl(shift_right, in_reg_hi, shift_left);
3230 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3231
3232 __ Bind(&end);
3233 }
3234}
Roland Levillain22c49222016-03-18 14:04:28 +00003235
3236void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003237 LocationSummary* locations =
3238 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3239 switch (ror->GetResultType()) {
3240 case Primitive::kPrimInt: {
3241 locations->SetInAt(0, Location::RequiresRegister());
3242 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3244 break;
3245 }
3246 case Primitive::kPrimLong: {
3247 locations->SetInAt(0, Location::RequiresRegister());
3248 if (ror->InputAt(1)->IsConstant()) {
3249 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3250 } else {
3251 locations->SetInAt(1, Location::RequiresRegister());
3252 locations->AddTemp(Location::RequiresRegister());
3253 locations->AddTemp(Location::RequiresRegister());
3254 }
3255 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3256 break;
3257 }
3258 default:
3259 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3260 }
3261}
3262
Roland Levillain22c49222016-03-18 14:04:28 +00003263void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003264 LocationSummary* locations = ror->GetLocations();
3265 Primitive::Type type = ror->GetResultType();
3266 switch (type) {
3267 case Primitive::kPrimInt: {
3268 HandleIntegerRotate(locations);
3269 break;
3270 }
3271 case Primitive::kPrimLong: {
3272 HandleLongRotate(locations);
3273 break;
3274 }
3275 default:
3276 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003277 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003278 }
3279}
3280
Calin Juravle9aec02f2014-11-18 23:06:35 +00003281void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3282 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3283
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003284 LocationSummary* locations =
3285 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003286
3287 switch (op->GetResultType()) {
3288 case Primitive::kPrimInt: {
3289 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003290 if (op->InputAt(1)->IsConstant()) {
3291 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3292 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3293 } else {
3294 locations->SetInAt(1, Location::RequiresRegister());
3295 // Make the output overlap, as it will be used to hold the masked
3296 // second input.
3297 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3298 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003299 break;
3300 }
3301 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003302 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 // For simplicity, use kOutputOverlap even though we only require that low registers
3306 // don't clash with high registers which the register allocator currently guarantees.
3307 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3308 } else {
3309 locations->SetInAt(1, Location::RequiresRegister());
3310 locations->AddTemp(Location::RequiresRegister());
3311 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3312 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003313 break;
3314 }
3315 default:
3316 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3317 }
3318}
3319
3320void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3321 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3322
3323 LocationSummary* locations = op->GetLocations();
3324 Location out = locations->Out();
3325 Location first = locations->InAt(0);
3326 Location second = locations->InAt(1);
3327
3328 Primitive::Type type = op->GetResultType();
3329 switch (type) {
3330 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003331 Register out_reg = out.AsRegister<Register>();
3332 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003333 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003334 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003335 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003336 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003337 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003338 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003339 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003340 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003341 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003342 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003343 }
3344 } else {
3345 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003346 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003347 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003348 __ Mov(out_reg, first_reg);
3349 } else if (op->IsShl()) {
3350 __ Lsl(out_reg, first_reg, shift_value);
3351 } else if (op->IsShr()) {
3352 __ Asr(out_reg, first_reg, shift_value);
3353 } else {
3354 __ Lsr(out_reg, first_reg, shift_value);
3355 }
3356 }
3357 break;
3358 }
3359 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003360 Register o_h = out.AsRegisterPairHigh<Register>();
3361 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003362
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003363 Register high = first.AsRegisterPairHigh<Register>();
3364 Register low = first.AsRegisterPairLow<Register>();
3365
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003366 if (second.IsRegister()) {
3367 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003368
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003369 Register second_reg = second.AsRegister<Register>();
3370
3371 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003372 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003373 // Shift the high part
3374 __ Lsl(o_h, high, o_l);
3375 // Shift the low part and `or` what overflew on the high part
3376 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3377 __ Lsr(temp, low, temp);
3378 __ orr(o_h, o_h, ShifterOperand(temp));
3379 // If the shift is > 32 bits, override the high part
3380 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3381 __ it(PL);
3382 __ Lsl(o_h, low, temp, PL);
3383 // Shift the low part
3384 __ Lsl(o_l, low, o_l);
3385 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003386 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003387 // Shift the low part
3388 __ Lsr(o_l, low, o_h);
3389 // Shift the high part and `or` what underflew on the low part
3390 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3391 __ Lsl(temp, high, temp);
3392 __ orr(o_l, o_l, ShifterOperand(temp));
3393 // If the shift is > 32 bits, override the low part
3394 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3395 __ it(PL);
3396 __ Asr(o_l, high, temp, PL);
3397 // Shift the high part
3398 __ Asr(o_h, high, o_h);
3399 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003400 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003401 // same as Shr except we use `Lsr`s and not `Asr`s
3402 __ Lsr(o_l, low, o_h);
3403 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3404 __ Lsl(temp, high, temp);
3405 __ orr(o_l, o_l, ShifterOperand(temp));
3406 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3407 __ it(PL);
3408 __ Lsr(o_l, high, temp, PL);
3409 __ Lsr(o_h, high, o_h);
3410 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003411 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003412 // Register allocator doesn't create partial overlap.
3413 DCHECK_NE(o_l, high);
3414 DCHECK_NE(o_h, low);
3415 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003416 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003417 if (shift_value > 32) {
3418 if (op->IsShl()) {
3419 __ Lsl(o_h, low, shift_value - 32);
3420 __ LoadImmediate(o_l, 0);
3421 } else if (op->IsShr()) {
3422 __ Asr(o_l, high, shift_value - 32);
3423 __ Asr(o_h, high, 31);
3424 } else {
3425 __ Lsr(o_l, high, shift_value - 32);
3426 __ LoadImmediate(o_h, 0);
3427 }
3428 } else if (shift_value == 32) {
3429 if (op->IsShl()) {
3430 __ mov(o_h, ShifterOperand(low));
3431 __ LoadImmediate(o_l, 0);
3432 } else if (op->IsShr()) {
3433 __ mov(o_l, ShifterOperand(high));
3434 __ Asr(o_h, high, 31);
3435 } else {
3436 __ mov(o_l, ShifterOperand(high));
3437 __ LoadImmediate(o_h, 0);
3438 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003439 } else if (shift_value == 1) {
3440 if (op->IsShl()) {
3441 __ Lsls(o_l, low, 1);
3442 __ adc(o_h, high, ShifterOperand(high));
3443 } else if (op->IsShr()) {
3444 __ Asrs(o_h, high, 1);
3445 __ Rrx(o_l, low);
3446 } else {
3447 __ Lsrs(o_h, high, 1);
3448 __ Rrx(o_l, low);
3449 }
3450 } else {
3451 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003452 if (op->IsShl()) {
3453 __ Lsl(o_h, high, shift_value);
3454 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3455 __ Lsl(o_l, low, shift_value);
3456 } else if (op->IsShr()) {
3457 __ Lsr(o_l, low, shift_value);
3458 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3459 __ Asr(o_h, high, shift_value);
3460 } else {
3461 __ Lsr(o_l, low, shift_value);
3462 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3463 __ Lsr(o_h, high, shift_value);
3464 }
3465 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003466 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003467 break;
3468 }
3469 default:
3470 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003471 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003472 }
3473}
3474
3475void LocationsBuilderARM::VisitShl(HShl* shl) {
3476 HandleShift(shl);
3477}
3478
3479void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3480 HandleShift(shl);
3481}
3482
3483void LocationsBuilderARM::VisitShr(HShr* shr) {
3484 HandleShift(shr);
3485}
3486
3487void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3488 HandleShift(shr);
3489}
3490
3491void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3492 HandleShift(ushr);
3493}
3494
3495void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3496 HandleShift(ushr);
3497}
3498
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003499void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003500 LocationSummary* locations =
3501 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
David Brazdil6de19382016-01-08 17:37:10 +00003502 if (instruction->IsStringAlloc()) {
3503 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3504 } else {
3505 InvokeRuntimeCallingConvention calling_convention;
3506 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3507 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3508 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003509 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003510}
3511
3512void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003513 // Note: if heap poisoning is enabled, the entry point takes cares
3514 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003515 if (instruction->IsStringAlloc()) {
3516 // String is allocated through StringFactory. Call NewEmptyString entry point.
3517 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3518 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3519 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3520 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3521 __ blx(LR);
3522 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3523 } else {
3524 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3525 instruction,
3526 instruction->GetDexPc(),
3527 nullptr);
3528 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3529 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003530}
3531
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003532void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3533 LocationSummary* locations =
3534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3535 InvokeRuntimeCallingConvention calling_convention;
3536 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003537 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003538 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003539 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003540}
3541
3542void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3543 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003544 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003545 // Note: if heap poisoning is enabled, the entry point takes cares
3546 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003547 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003548 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003549 instruction->GetDexPc(),
3550 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003551 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003552}
3553
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003554void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003555 LocationSummary* locations =
3556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003557 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3558 if (location.IsStackSlot()) {
3559 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3560 } else if (location.IsDoubleStackSlot()) {
3561 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003562 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003563 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003564}
3565
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003566void InstructionCodeGeneratorARM::VisitParameterValue(
3567 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003568 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003569}
3570
3571void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3572 LocationSummary* locations =
3573 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3574 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3575}
3576
3577void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3578 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003579}
3580
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003581void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003582 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003583 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003584 locations->SetInAt(0, Location::RequiresRegister());
3585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003586}
3587
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003588void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3589 LocationSummary* locations = not_->GetLocations();
3590 Location out = locations->Out();
3591 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003592 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003593 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003594 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003595 break;
3596
3597 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003598 __ mvn(out.AsRegisterPairLow<Register>(),
3599 ShifterOperand(in.AsRegisterPairLow<Register>()));
3600 __ mvn(out.AsRegisterPairHigh<Register>(),
3601 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003602 break;
3603
3604 default:
3605 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3606 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003607}
3608
David Brazdil66d126e2015-04-03 16:02:44 +01003609void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3610 LocationSummary* locations =
3611 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3612 locations->SetInAt(0, Location::RequiresRegister());
3613 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3614}
3615
3616void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003617 LocationSummary* locations = bool_not->GetLocations();
3618 Location out = locations->Out();
3619 Location in = locations->InAt(0);
3620 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3621}
3622
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003623void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003624 LocationSummary* locations =
3625 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003626 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003627 case Primitive::kPrimBoolean:
3628 case Primitive::kPrimByte:
3629 case Primitive::kPrimShort:
3630 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003631 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003632 case Primitive::kPrimLong: {
3633 locations->SetInAt(0, Location::RequiresRegister());
3634 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003635 // Output overlaps because it is written before doing the low comparison.
3636 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003637 break;
3638 }
3639 case Primitive::kPrimFloat:
3640 case Primitive::kPrimDouble: {
3641 locations->SetInAt(0, Location::RequiresFpuRegister());
3642 locations->SetInAt(1, Location::RequiresFpuRegister());
3643 locations->SetOut(Location::RequiresRegister());
3644 break;
3645 }
3646 default:
3647 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3648 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003649}
3650
3651void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003652 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003653 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003654 Location left = locations->InAt(0);
3655 Location right = locations->InAt(1);
3656
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003657 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003658 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003659 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003660 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003661 case Primitive::kPrimBoolean:
3662 case Primitive::kPrimByte:
3663 case Primitive::kPrimShort:
3664 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003665 case Primitive::kPrimInt: {
3666 __ LoadImmediate(out, 0);
3667 __ cmp(left.AsRegister<Register>(),
3668 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3669 less_cond = LT;
3670 break;
3671 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003672 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003673 __ cmp(left.AsRegisterPairHigh<Register>(),
3674 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003675 __ b(&less, LT);
3676 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003677 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003678 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003679 __ cmp(left.AsRegisterPairLow<Register>(),
3680 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003681 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003682 break;
3683 }
3684 case Primitive::kPrimFloat:
3685 case Primitive::kPrimDouble: {
3686 __ LoadImmediate(out, 0);
3687 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003688 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003689 } else {
3690 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3691 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3692 }
3693 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003694 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003695 break;
3696 }
3697 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003698 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003699 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003700 }
Aart Bika19616e2016-02-01 18:57:58 -08003701
Calin Juravleddb7df22014-11-25 20:56:51 +00003702 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003703 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003704
3705 __ Bind(&greater);
3706 __ LoadImmediate(out, 1);
3707 __ b(&done);
3708
3709 __ Bind(&less);
3710 __ LoadImmediate(out, -1);
3711
3712 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003713}
3714
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003715void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003716 LocationSummary* locations =
3717 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003718 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003719 locations->SetInAt(i, Location::Any());
3720 }
3721 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003722}
3723
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003724void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003725 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003726}
3727
Roland Levillainc9285912015-12-18 10:38:42 +00003728void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3729 // TODO (ported from quick): revisit ARM barrier kinds.
3730 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003731 switch (kind) {
3732 case MemBarrierKind::kAnyStore:
3733 case MemBarrierKind::kLoadAny:
3734 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003735 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003736 break;
3737 }
3738 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003739 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003740 break;
3741 }
3742 default:
3743 LOG(FATAL) << "Unexpected memory barrier " << kind;
3744 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003745 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003746}
3747
3748void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3749 uint32_t offset,
3750 Register out_lo,
3751 Register out_hi) {
3752 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003753 // Ensure `out_lo` is different from `addr`, so that loading
3754 // `offset` into `out_lo` does not clutter `addr`.
3755 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003756 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003757 __ add(IP, addr, ShifterOperand(out_lo));
3758 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003759 }
3760 __ ldrexd(out_lo, out_hi, addr);
3761}
3762
3763void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3764 uint32_t offset,
3765 Register value_lo,
3766 Register value_hi,
3767 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003768 Register temp2,
3769 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003770 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003771 if (offset != 0) {
3772 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003773 __ add(IP, addr, ShifterOperand(temp1));
3774 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003775 }
3776 __ Bind(&fail);
3777 // We need a load followed by store. (The address used in a STREX instruction must
3778 // be the same as the address in the most recently executed LDREX instruction.)
3779 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003780 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003781 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003782 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003783}
3784
3785void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3786 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3787
Nicolas Geoffray39468442014-09-02 15:17:15 +01003788 LocationSummary* locations =
3789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003790 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003791
Calin Juravle52c48962014-12-16 17:02:57 +00003792 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003793 if (Primitive::IsFloatingPointType(field_type)) {
3794 locations->SetInAt(1, Location::RequiresFpuRegister());
3795 } else {
3796 locations->SetInAt(1, Location::RequiresRegister());
3797 }
3798
Calin Juravle52c48962014-12-16 17:02:57 +00003799 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003800 bool generate_volatile = field_info.IsVolatile()
3801 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003802 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003803 bool needs_write_barrier =
3804 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003805 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003806 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003807 if (needs_write_barrier) {
3808 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003809 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003810 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003811 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003812 // - registers need to be consecutive
3813 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003814 // We don't test for ARM yet, and the assertion makes sure that we
3815 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003816 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3817
3818 locations->AddTemp(Location::RequiresRegister());
3819 locations->AddTemp(Location::RequiresRegister());
3820 if (field_type == Primitive::kPrimDouble) {
3821 // For doubles we need two more registers to copy the value.
3822 locations->AddTemp(Location::RegisterLocation(R2));
3823 locations->AddTemp(Location::RegisterLocation(R3));
3824 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003825 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003826}
3827
Calin Juravle52c48962014-12-16 17:02:57 +00003828void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003829 const FieldInfo& field_info,
3830 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003831 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3832
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003833 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003834 Register base = locations->InAt(0).AsRegister<Register>();
3835 Location value = locations->InAt(1);
3836
3837 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003838 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003839 Primitive::Type field_type = field_info.GetFieldType();
3840 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003841 bool needs_write_barrier =
3842 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003843
3844 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003845 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003846 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003847
3848 switch (field_type) {
3849 case Primitive::kPrimBoolean:
3850 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003851 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003852 break;
3853 }
3854
3855 case Primitive::kPrimShort:
3856 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003857 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003858 break;
3859 }
3860
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003861 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003863 if (kPoisonHeapReferences && needs_write_barrier) {
3864 // Note that in the case where `value` is a null reference,
3865 // we do not enter this block, as a null reference does not
3866 // need poisoning.
3867 DCHECK_EQ(field_type, Primitive::kPrimNot);
3868 Register temp = locations->GetTemp(0).AsRegister<Register>();
3869 __ Mov(temp, value.AsRegister<Register>());
3870 __ PoisonHeapReference(temp);
3871 __ StoreToOffset(kStoreWord, temp, base, offset);
3872 } else {
3873 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3874 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003875 break;
3876 }
3877
3878 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003879 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003880 GenerateWideAtomicStore(base, offset,
3881 value.AsRegisterPairLow<Register>(),
3882 value.AsRegisterPairHigh<Register>(),
3883 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003884 locations->GetTemp(1).AsRegister<Register>(),
3885 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003886 } else {
3887 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003888 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003889 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003890 break;
3891 }
3892
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003893 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003894 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003895 break;
3896 }
3897
3898 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003899 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003900 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003901 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3902 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3903
3904 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3905
3906 GenerateWideAtomicStore(base, offset,
3907 value_reg_lo,
3908 value_reg_hi,
3909 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003910 locations->GetTemp(3).AsRegister<Register>(),
3911 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003912 } else {
3913 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003914 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003915 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003916 break;
3917 }
3918
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003919 case Primitive::kPrimVoid:
3920 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003921 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003922 }
Calin Juravle52c48962014-12-16 17:02:57 +00003923
Calin Juravle77520bc2015-01-12 18:45:46 +00003924 // Longs and doubles are handled in the switch.
3925 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3926 codegen_->MaybeRecordImplicitNullCheck(instruction);
3927 }
3928
3929 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3930 Register temp = locations->GetTemp(0).AsRegister<Register>();
3931 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003932 codegen_->MarkGCCard(
3933 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003934 }
3935
Calin Juravle52c48962014-12-16 17:02:57 +00003936 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003937 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003938 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003939}
3940
Calin Juravle52c48962014-12-16 17:02:57 +00003941void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3942 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003943
3944 bool object_field_get_with_read_barrier =
3945 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003946 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003947 new (GetGraph()->GetArena()) LocationSummary(instruction,
3948 object_field_get_with_read_barrier ?
3949 LocationSummary::kCallOnSlowPath :
3950 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003951 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003952
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003953 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003954 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003955 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003956 // The output overlaps in case of volatile long: we don't want the
3957 // code generated by GenerateWideAtomicLoad to overwrite the
3958 // object's location. Likewise, in the case of an object field get
3959 // with read barriers enabled, we do not want the load to overwrite
3960 // the object's location, as we need it to emit the read barrier.
3961 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3962 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003963
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003964 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3965 locations->SetOut(Location::RequiresFpuRegister());
3966 } else {
3967 locations->SetOut(Location::RequiresRegister(),
3968 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3969 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003970 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00003971 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003972 // - registers need to be consecutive
3973 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003974 // We don't test for ARM yet, and the assertion makes sure that we
3975 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003976 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3977 locations->AddTemp(Location::RequiresRegister());
3978 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00003979 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3980 // We need a temporary register for the read barrier marking slow
3981 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3982 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003983 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003984}
3985
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003986Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3987 Opcode opcode) {
3988 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3989 if (constant->IsConstant() &&
3990 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3991 return Location::ConstantLocation(constant->AsConstant());
3992 }
3993 return Location::RequiresRegister();
3994}
3995
3996bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3997 Opcode opcode) {
3998 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3999 if (Primitive::Is64BitType(input_cst->GetType())) {
4000 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4001 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4002 } else {
4003 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4004 }
4005}
4006
4007bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4008 ShifterOperand so;
4009 ArmAssembler* assembler = codegen_->GetAssembler();
4010 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4011 return true;
4012 }
4013 Opcode neg_opcode = kNoOperand;
4014 switch (opcode) {
4015 case AND:
4016 neg_opcode = BIC;
4017 break;
4018 case ORR:
4019 neg_opcode = ORN;
4020 break;
4021 default:
4022 return false;
4023 }
4024 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4025}
4026
Calin Juravle52c48962014-12-16 17:02:57 +00004027void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4028 const FieldInfo& field_info) {
4029 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004030
Calin Juravle52c48962014-12-16 17:02:57 +00004031 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004032 Location base_loc = locations->InAt(0);
4033 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004034 Location out = locations->Out();
4035 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004036 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004037 Primitive::Type field_type = field_info.GetFieldType();
4038 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4039
4040 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004041 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004042 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004043 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004044
Roland Levillainc9285912015-12-18 10:38:42 +00004045 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004046 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004047 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004048
Roland Levillainc9285912015-12-18 10:38:42 +00004049 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004050 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004051 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004052
Roland Levillainc9285912015-12-18 10:38:42 +00004053 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004054 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004055 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004056
4057 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004058 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004059 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004060
4061 case Primitive::kPrimNot: {
4062 // /* HeapReference<Object> */ out = *(base + offset)
4063 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4064 Location temp_loc = locations->GetTemp(0);
4065 // Note that a potential implicit null check is handled in this
4066 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4067 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4068 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4069 if (is_volatile) {
4070 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4071 }
4072 } else {
4073 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4074 codegen_->MaybeRecordImplicitNullCheck(instruction);
4075 if (is_volatile) {
4076 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4077 }
4078 // If read barriers are enabled, emit read barriers other than
4079 // Baker's using a slow path (and also unpoison the loaded
4080 // reference, if heap poisoning is enabled).
4081 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4082 }
4083 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004084 }
4085
Roland Levillainc9285912015-12-18 10:38:42 +00004086 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004087 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004088 GenerateWideAtomicLoad(base, offset,
4089 out.AsRegisterPairLow<Register>(),
4090 out.AsRegisterPairHigh<Register>());
4091 } else {
4092 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4093 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004094 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004095
Roland Levillainc9285912015-12-18 10:38:42 +00004096 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004097 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004098 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004099
4100 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004101 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004102 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004103 Register lo = locations->GetTemp(0).AsRegister<Register>();
4104 Register hi = locations->GetTemp(1).AsRegister<Register>();
4105 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004106 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004107 __ vmovdrr(out_reg, lo, hi);
4108 } else {
4109 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004110 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004111 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004112 break;
4113 }
4114
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004115 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004116 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004117 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004118 }
Calin Juravle52c48962014-12-16 17:02:57 +00004119
Roland Levillainc9285912015-12-18 10:38:42 +00004120 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4121 // Potential implicit null checks, in the case of reference or
4122 // double fields, are handled in the previous switch statement.
4123 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004124 codegen_->MaybeRecordImplicitNullCheck(instruction);
4125 }
4126
Calin Juravle52c48962014-12-16 17:02:57 +00004127 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004128 if (field_type == Primitive::kPrimNot) {
4129 // Memory barriers, in the case of references, are also handled
4130 // in the previous switch statement.
4131 } else {
4132 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4133 }
Roland Levillain4d027112015-07-01 15:41:14 +01004134 }
Calin Juravle52c48962014-12-16 17:02:57 +00004135}
4136
4137void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4138 HandleFieldSet(instruction, instruction->GetFieldInfo());
4139}
4140
4141void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004142 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004143}
4144
4145void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4146 HandleFieldGet(instruction, instruction->GetFieldInfo());
4147}
4148
4149void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4150 HandleFieldGet(instruction, instruction->GetFieldInfo());
4151}
4152
4153void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4154 HandleFieldGet(instruction, instruction->GetFieldInfo());
4155}
4156
4157void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4158 HandleFieldGet(instruction, instruction->GetFieldInfo());
4159}
4160
4161void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4162 HandleFieldSet(instruction, instruction->GetFieldInfo());
4163}
4164
4165void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004166 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004167}
4168
Calin Juravlee460d1d2015-09-29 04:52:17 +01004169void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4170 HUnresolvedInstanceFieldGet* instruction) {
4171 FieldAccessCallingConventionARM calling_convention;
4172 codegen_->CreateUnresolvedFieldLocationSummary(
4173 instruction, instruction->GetFieldType(), calling_convention);
4174}
4175
4176void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4177 HUnresolvedInstanceFieldGet* instruction) {
4178 FieldAccessCallingConventionARM calling_convention;
4179 codegen_->GenerateUnresolvedFieldAccess(instruction,
4180 instruction->GetFieldType(),
4181 instruction->GetFieldIndex(),
4182 instruction->GetDexPc(),
4183 calling_convention);
4184}
4185
4186void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4187 HUnresolvedInstanceFieldSet* instruction) {
4188 FieldAccessCallingConventionARM calling_convention;
4189 codegen_->CreateUnresolvedFieldLocationSummary(
4190 instruction, instruction->GetFieldType(), calling_convention);
4191}
4192
4193void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4194 HUnresolvedInstanceFieldSet* instruction) {
4195 FieldAccessCallingConventionARM calling_convention;
4196 codegen_->GenerateUnresolvedFieldAccess(instruction,
4197 instruction->GetFieldType(),
4198 instruction->GetFieldIndex(),
4199 instruction->GetDexPc(),
4200 calling_convention);
4201}
4202
4203void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4204 HUnresolvedStaticFieldGet* instruction) {
4205 FieldAccessCallingConventionARM calling_convention;
4206 codegen_->CreateUnresolvedFieldLocationSummary(
4207 instruction, instruction->GetFieldType(), calling_convention);
4208}
4209
4210void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4211 HUnresolvedStaticFieldGet* instruction) {
4212 FieldAccessCallingConventionARM calling_convention;
4213 codegen_->GenerateUnresolvedFieldAccess(instruction,
4214 instruction->GetFieldType(),
4215 instruction->GetFieldIndex(),
4216 instruction->GetDexPc(),
4217 calling_convention);
4218}
4219
4220void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4221 HUnresolvedStaticFieldSet* instruction) {
4222 FieldAccessCallingConventionARM calling_convention;
4223 codegen_->CreateUnresolvedFieldLocationSummary(
4224 instruction, instruction->GetFieldType(), calling_convention);
4225}
4226
4227void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4228 HUnresolvedStaticFieldSet* instruction) {
4229 FieldAccessCallingConventionARM calling_convention;
4230 codegen_->GenerateUnresolvedFieldAccess(instruction,
4231 instruction->GetFieldType(),
4232 instruction->GetFieldIndex(),
4233 instruction->GetDexPc(),
4234 calling_convention);
4235}
4236
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004237void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004238 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4239 ? LocationSummary::kCallOnSlowPath
4240 : LocationSummary::kNoCall;
4241 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004242 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004243 if (instruction->HasUses()) {
4244 locations->SetOut(Location::SameAsFirstInput());
4245 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246}
4247
Calin Juravle2ae48182016-03-16 14:05:09 +00004248void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4249 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004250 return;
4251 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004252 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004253
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004254 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004255 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004256}
4257
Calin Juravle2ae48182016-03-16 14:05:09 +00004258void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004259 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004260 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004261
4262 LocationSummary* locations = instruction->GetLocations();
4263 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004264
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004265 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004266}
4267
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004268void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004269 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004270}
4271
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004272void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004273 bool object_array_get_with_read_barrier =
4274 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004275 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004276 new (GetGraph()->GetArena()) LocationSummary(instruction,
4277 object_array_get_with_read_barrier ?
4278 LocationSummary::kCallOnSlowPath :
4279 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004280 locations->SetInAt(0, Location::RequiresRegister());
4281 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004282 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4284 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004285 // The output overlaps in the case of an object array get with
4286 // read barriers enabled: we do not want the move to overwrite the
4287 // array's location, as we need it to emit the read barrier.
4288 locations->SetOut(
4289 Location::RequiresRegister(),
4290 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004291 }
Roland Levillainc9285912015-12-18 10:38:42 +00004292 // We need a temporary register for the read barrier marking slow
4293 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4294 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4295 locations->AddTemp(Location::RequiresRegister());
4296 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004297}
4298
4299void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4300 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004301 Location obj_loc = locations->InAt(0);
4302 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004303 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004304 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004305 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004306
Roland Levillainc9285912015-12-18 10:38:42 +00004307 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004308 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004309 case Primitive::kPrimBoolean: {
Roland Levillainc9285912015-12-18 10:38:42 +00004310 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004312 size_t offset =
4313 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004314 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4315 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004316 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4318 }
4319 break;
4320 }
4321
4322 case Primitive::kPrimByte: {
Roland Levillainc9285912015-12-18 10:38:42 +00004323 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004325 size_t offset =
4326 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004327 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4328 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004329 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004330 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4331 }
4332 break;
4333 }
4334
4335 case Primitive::kPrimShort: {
Roland Levillainc9285912015-12-18 10:38:42 +00004336 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004337 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004338 size_t offset =
4339 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4341 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004342 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004343 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4344 }
4345 break;
4346 }
4347
4348 case Primitive::kPrimChar: {
Roland Levillainc9285912015-12-18 10:38:42 +00004349 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004351 size_t offset =
4352 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004353 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4354 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004355 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004356 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4357 }
4358 break;
4359 }
4360
Roland Levillainc9285912015-12-18 10:38:42 +00004361 case Primitive::kPrimInt: {
Roland Levillainc9285912015-12-18 10:38:42 +00004362 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004363 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004364 size_t offset =
4365 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004366 __ LoadFromOffset(kLoadWord, out, obj, offset);
4367 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004369 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4370 }
4371 break;
4372 }
4373
Roland Levillainc9285912015-12-18 10:38:42 +00004374 case Primitive::kPrimNot: {
4375 static_assert(
4376 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4377 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004378 // /* HeapReference<Object> */ out =
4379 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4380 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4381 Location temp = locations->GetTemp(0);
4382 // Note that a potential implicit null check is handled in this
4383 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4384 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4385 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4386 } else {
4387 Register out = out_loc.AsRegister<Register>();
4388 if (index.IsConstant()) {
4389 size_t offset =
4390 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4391 __ LoadFromOffset(kLoadWord, out, obj, offset);
4392 codegen_->MaybeRecordImplicitNullCheck(instruction);
4393 // If read barriers are enabled, emit read barriers other than
4394 // Baker's using a slow path (and also unpoison the loaded
4395 // reference, if heap poisoning is enabled).
4396 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4397 } else {
4398 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4399 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4400 codegen_->MaybeRecordImplicitNullCheck(instruction);
4401 // If read barriers are enabled, emit read barriers other than
4402 // Baker's using a slow path (and also unpoison the loaded
4403 // reference, if heap poisoning is enabled).
4404 codegen_->MaybeGenerateReadBarrierSlow(
4405 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4406 }
4407 }
4408 break;
4409 }
4410
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004411 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004412 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004413 size_t offset =
4414 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004415 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004416 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004417 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004418 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004419 }
4420 break;
4421 }
4422
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004423 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004424 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004425 if (index.IsConstant()) {
4426 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004427 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004428 } else {
4429 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004430 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004431 }
4432 break;
4433 }
4434
4435 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004436 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004437 if (index.IsConstant()) {
4438 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004439 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004440 } else {
4441 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004442 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004443 }
4444 break;
4445 }
4446
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004447 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004448 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004449 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004450 }
Roland Levillain4d027112015-07-01 15:41:14 +01004451
4452 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004453 // Potential implicit null checks, in the case of reference
4454 // arrays, are handled in the previous switch statement.
4455 } else {
4456 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004457 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004458}
4459
4460void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004461 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004462
4463 bool needs_write_barrier =
4464 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004465 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4466 bool object_array_set_with_read_barrier =
4467 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004468
Nicolas Geoffray39468442014-09-02 15:17:15 +01004469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004470 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004471 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4472 LocationSummary::kCallOnSlowPath :
4473 LocationSummary::kNoCall);
4474
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004475 locations->SetInAt(0, Location::RequiresRegister());
4476 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4477 if (Primitive::IsFloatingPointType(value_type)) {
4478 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004479 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004480 locations->SetInAt(2, Location::RequiresRegister());
4481 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004482 if (needs_write_barrier) {
4483 // Temporary registers for the write barrier.
4484 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004485 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004486 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487}
4488
4489void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4490 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004491 Location array_loc = locations->InAt(0);
4492 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004493 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004494 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004495 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004496 bool needs_write_barrier =
4497 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004498
4499 switch (value_type) {
4500 case Primitive::kPrimBoolean:
4501 case Primitive::kPrimByte: {
4502 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004503 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004504 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004505 size_t offset =
4506 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004507 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004508 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004509 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004510 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4511 }
4512 break;
4513 }
4514
4515 case Primitive::kPrimShort:
4516 case Primitive::kPrimChar: {
4517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004518 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004519 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004520 size_t offset =
4521 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004522 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004524 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004525 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4526 }
4527 break;
4528 }
4529
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004530 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004531 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004532 Location value_loc = locations->InAt(2);
4533 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004534 Register source = value;
4535
4536 if (instruction->InputAt(2)->IsNullConstant()) {
4537 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004538 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004539 size_t offset =
4540 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004541 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004542 } else {
4543 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004544 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004545 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004546 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004547 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004548 DCHECK(!needs_write_barrier);
4549 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004550 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004551 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004552
4553 DCHECK(needs_write_barrier);
4554 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4555 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4556 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4557 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4558 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4559 Label done;
4560 SlowPathCode* slow_path = nullptr;
4561
Roland Levillain3b359c72015-11-17 19:35:12 +00004562 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004563 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4564 codegen_->AddSlowPath(slow_path);
4565 if (instruction->GetValueCanBeNull()) {
4566 Label non_zero;
4567 __ CompareAndBranchIfNonZero(value, &non_zero);
4568 if (index.IsConstant()) {
4569 size_t offset =
4570 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4571 __ StoreToOffset(kStoreWord, value, array, offset);
4572 } else {
4573 DCHECK(index.IsRegister()) << index;
4574 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4575 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4576 }
4577 codegen_->MaybeRecordImplicitNullCheck(instruction);
4578 __ b(&done);
4579 __ Bind(&non_zero);
4580 }
4581
Roland Levillain3b359c72015-11-17 19:35:12 +00004582 if (kEmitCompilerReadBarrier) {
4583 // When read barriers are enabled, the type checking
4584 // instrumentation requires two read barriers:
4585 //
4586 // __ Mov(temp2, temp1);
4587 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4588 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004589 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004590 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4591 //
4592 // // /* HeapReference<Class> */ temp2 = value->klass_
4593 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004594 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004595 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4596 //
4597 // __ cmp(temp1, ShifterOperand(temp2));
4598 //
4599 // However, the second read barrier may trash `temp`, as it
4600 // is a temporary register, and as such would not be saved
4601 // along with live registers before calling the runtime (nor
4602 // restored afterwards). So in this case, we bail out and
4603 // delegate the work to the array set slow path.
4604 //
4605 // TODO: Extend the register allocator to support a new
4606 // "(locally) live temp" location so as to avoid always
4607 // going into the slow path when read barriers are enabled.
4608 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004609 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004610 // /* HeapReference<Class> */ temp1 = array->klass_
4611 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4612 codegen_->MaybeRecordImplicitNullCheck(instruction);
4613 __ MaybeUnpoisonHeapReference(temp1);
4614
4615 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4616 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4617 // /* HeapReference<Class> */ temp2 = value->klass_
4618 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4619 // If heap poisoning is enabled, no need to unpoison `temp1`
4620 // nor `temp2`, as we are comparing two poisoned references.
4621 __ cmp(temp1, ShifterOperand(temp2));
4622
4623 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4624 Label do_put;
4625 __ b(&do_put, EQ);
4626 // If heap poisoning is enabled, the `temp1` reference has
4627 // not been unpoisoned yet; unpoison it now.
4628 __ MaybeUnpoisonHeapReference(temp1);
4629
4630 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4631 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4632 // If heap poisoning is enabled, no need to unpoison
4633 // `temp1`, as we are comparing against null below.
4634 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4635 __ Bind(&do_put);
4636 } else {
4637 __ b(slow_path->GetEntryLabel(), NE);
4638 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004639 }
4640 }
4641
4642 if (kPoisonHeapReferences) {
4643 // Note that in the case where `value` is a null reference,
4644 // we do not enter this block, as a null reference does not
4645 // need poisoning.
4646 DCHECK_EQ(value_type, Primitive::kPrimNot);
4647 __ Mov(temp1, value);
4648 __ PoisonHeapReference(temp1);
4649 source = temp1;
4650 }
4651
4652 if (index.IsConstant()) {
4653 size_t offset =
4654 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4655 __ StoreToOffset(kStoreWord, source, array, offset);
4656 } else {
4657 DCHECK(index.IsRegister()) << index;
4658 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4659 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4660 }
4661
Roland Levillain3b359c72015-11-17 19:35:12 +00004662 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004663 codegen_->MaybeRecordImplicitNullCheck(instruction);
4664 }
4665
4666 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4667
4668 if (done.IsLinked()) {
4669 __ Bind(&done);
4670 }
4671
4672 if (slow_path != nullptr) {
4673 __ Bind(slow_path->GetExitLabel());
4674 }
4675
4676 break;
4677 }
4678
4679 case Primitive::kPrimInt: {
4680 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4681 Register value = locations->InAt(2).AsRegister<Register>();
4682 if (index.IsConstant()) {
4683 size_t offset =
4684 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4685 __ StoreToOffset(kStoreWord, value, array, offset);
4686 } else {
4687 DCHECK(index.IsRegister()) << index;
4688 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4689 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4690 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691 break;
4692 }
4693
4694 case Primitive::kPrimLong: {
4695 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004696 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004697 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004698 size_t offset =
4699 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004700 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004701 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004702 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004703 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704 }
4705 break;
4706 }
4707
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004708 case Primitive::kPrimFloat: {
4709 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4710 Location value = locations->InAt(2);
4711 DCHECK(value.IsFpuRegister());
4712 if (index.IsConstant()) {
4713 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004714 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004715 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004716 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004717 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4718 }
4719 break;
4720 }
4721
4722 case Primitive::kPrimDouble: {
4723 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4724 Location value = locations->InAt(2);
4725 DCHECK(value.IsFpuRegisterPair());
4726 if (index.IsConstant()) {
4727 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004728 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004729 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004730 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004731 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4732 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004733
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004734 break;
4735 }
4736
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004738 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004739 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004740 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004741
Roland Levillain80e67092016-01-08 16:04:55 +00004742 // Objects are handled in the switch.
4743 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004744 codegen_->MaybeRecordImplicitNullCheck(instruction);
4745 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746}
4747
4748void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004749 LocationSummary* locations =
4750 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004751 locations->SetInAt(0, Location::RequiresRegister());
4752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004753}
4754
4755void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4756 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004757 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004758 Register obj = locations->InAt(0).AsRegister<Register>();
4759 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004760 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004761 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762}
4763
4764void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004765 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4766 ? LocationSummary::kCallOnSlowPath
4767 : LocationSummary::kNoCall;
4768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004769 locations->SetInAt(0, Location::RequiresRegister());
4770 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004771 if (instruction->HasUses()) {
4772 locations->SetOut(Location::SameAsFirstInput());
4773 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774}
4775
4776void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4777 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004778 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004779 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004780 codegen_->AddSlowPath(slow_path);
4781
Roland Levillain271ab9c2014-11-27 15:23:57 +00004782 Register index = locations->InAt(0).AsRegister<Register>();
4783 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784
4785 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004786 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787}
4788
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004789void CodeGeneratorARM::MarkGCCard(Register temp,
4790 Register card,
4791 Register object,
4792 Register value,
4793 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004794 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004795 if (can_be_null) {
4796 __ CompareAndBranchIfZero(value, &is_null);
4797 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4799 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4800 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004801 if (can_be_null) {
4802 __ Bind(&is_null);
4803 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004804}
4805
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004806void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004807 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004808}
4809
4810void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004811 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4812}
4813
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004814void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4816}
4817
4818void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004819 HBasicBlock* block = instruction->GetBlock();
4820 if (block->GetLoopInformation() != nullptr) {
4821 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4822 // The back edge will generate the suspend check.
4823 return;
4824 }
4825 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4826 // The goto will generate the suspend check.
4827 return;
4828 }
4829 GenerateSuspendCheck(instruction, nullptr);
4830}
4831
4832void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4833 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004834 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004835 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4836 if (slow_path == nullptr) {
4837 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4838 instruction->SetSlowPath(slow_path);
4839 codegen_->AddSlowPath(slow_path);
4840 if (successor != nullptr) {
4841 DCHECK(successor->IsLoopHeader());
4842 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4843 }
4844 } else {
4845 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4846 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004847
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004848 __ LoadFromOffset(
4849 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004850 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004851 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004852 __ Bind(slow_path->GetReturnLabel());
4853 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004854 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004855 __ b(slow_path->GetEntryLabel());
4856 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004857}
4858
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004859ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4860 return codegen_->GetAssembler();
4861}
4862
4863void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004864 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004865 Location source = move->GetSource();
4866 Location destination = move->GetDestination();
4867
4868 if (source.IsRegister()) {
4869 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004870 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004871 } else if (destination.IsFpuRegister()) {
4872 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004873 } else {
4874 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004875 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004876 SP, destination.GetStackIndex());
4877 }
4878 } else if (source.IsStackSlot()) {
4879 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004880 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004881 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004882 } else if (destination.IsFpuRegister()) {
4883 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004884 } else {
4885 DCHECK(destination.IsStackSlot());
4886 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4887 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4888 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004889 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004890 if (destination.IsRegister()) {
4891 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
4892 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004893 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004894 } else {
4895 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004896 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4897 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004898 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004899 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004900 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4901 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004902 } else if (destination.IsRegisterPair()) {
4903 DCHECK(ExpectedPairLayout(destination));
4904 __ LoadFromOffset(
4905 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4906 } else {
4907 DCHECK(destination.IsFpuRegisterPair()) << destination;
4908 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4909 SP,
4910 source.GetStackIndex());
4911 }
4912 } else if (source.IsRegisterPair()) {
4913 if (destination.IsRegisterPair()) {
4914 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4915 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004916 } else if (destination.IsFpuRegisterPair()) {
4917 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4918 source.AsRegisterPairLow<Register>(),
4919 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004920 } else {
4921 DCHECK(destination.IsDoubleStackSlot()) << destination;
4922 DCHECK(ExpectedPairLayout(source));
4923 __ StoreToOffset(
4924 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4925 }
4926 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004927 if (destination.IsRegisterPair()) {
4928 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4929 destination.AsRegisterPairHigh<Register>(),
4930 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4931 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004932 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4933 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4934 } else {
4935 DCHECK(destination.IsDoubleStackSlot()) << destination;
4936 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4937 SP,
4938 destination.GetStackIndex());
4939 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004940 } else {
4941 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004942 HConstant* constant = source.GetConstant();
4943 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4944 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004945 if (destination.IsRegister()) {
4946 __ LoadImmediate(destination.AsRegister<Register>(), value);
4947 } else {
4948 DCHECK(destination.IsStackSlot());
4949 __ LoadImmediate(IP, value);
4950 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4951 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004952 } else if (constant->IsLongConstant()) {
4953 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004954 if (destination.IsRegisterPair()) {
4955 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4956 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004957 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004958 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004959 __ LoadImmediate(IP, Low32Bits(value));
4960 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4961 __ LoadImmediate(IP, High32Bits(value));
4962 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4963 }
4964 } else if (constant->IsDoubleConstant()) {
4965 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004966 if (destination.IsFpuRegisterPair()) {
4967 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004968 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004969 DCHECK(destination.IsDoubleStackSlot()) << destination;
4970 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004971 __ LoadImmediate(IP, Low32Bits(int_value));
4972 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4973 __ LoadImmediate(IP, High32Bits(int_value));
4974 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4975 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004976 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004977 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004978 float value = constant->AsFloatConstant()->GetValue();
4979 if (destination.IsFpuRegister()) {
4980 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4981 } else {
4982 DCHECK(destination.IsStackSlot());
4983 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4984 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4985 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004986 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004987 }
4988}
4989
4990void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4991 __ Mov(IP, reg);
4992 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4993 __ StoreToOffset(kStoreWord, IP, SP, mem);
4994}
4995
4996void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4997 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4998 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4999 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5000 SP, mem1 + stack_offset);
5001 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5002 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5003 SP, mem2 + stack_offset);
5004 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5005}
5006
5007void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005008 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005009 Location source = move->GetSource();
5010 Location destination = move->GetDestination();
5011
5012 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005013 DCHECK_NE(source.AsRegister<Register>(), IP);
5014 DCHECK_NE(destination.AsRegister<Register>(), IP);
5015 __ Mov(IP, source.AsRegister<Register>());
5016 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5017 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005018 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005019 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005020 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005021 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005022 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5023 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005024 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005025 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005026 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005027 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005028 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005029 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005030 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005031 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005032 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5033 destination.AsRegisterPairHigh<Register>(),
5034 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005035 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005036 Register low_reg = source.IsRegisterPair()
5037 ? source.AsRegisterPairLow<Register>()
5038 : destination.AsRegisterPairLow<Register>();
5039 int mem = source.IsRegisterPair()
5040 ? destination.GetStackIndex()
5041 : source.GetStackIndex();
5042 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005043 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005044 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005045 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005046 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005047 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5048 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005049 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005050 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005051 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005052 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5053 DRegister reg = source.IsFpuRegisterPair()
5054 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5055 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5056 int mem = source.IsFpuRegisterPair()
5057 ? destination.GetStackIndex()
5058 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005059 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005060 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005061 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005062 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5063 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5064 : destination.AsFpuRegister<SRegister>();
5065 int mem = source.IsFpuRegister()
5066 ? destination.GetStackIndex()
5067 : source.GetStackIndex();
5068
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005069 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005070 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005071 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005072 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005073 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5074 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005075 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005076 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005077 }
5078}
5079
5080void ParallelMoveResolverARM::SpillScratch(int reg) {
5081 __ Push(static_cast<Register>(reg));
5082}
5083
5084void ParallelMoveResolverARM::RestoreScratch(int reg) {
5085 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005086}
5087
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005088HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5089 HLoadClass::LoadKind desired_class_load_kind) {
5090 if (kEmitCompilerReadBarrier) {
5091 switch (desired_class_load_kind) {
5092 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5093 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5094 case HLoadClass::LoadKind::kBootImageAddress:
5095 // TODO: Implement for read barrier.
5096 return HLoadClass::LoadKind::kDexCacheViaMethod;
5097 default:
5098 break;
5099 }
5100 }
5101 switch (desired_class_load_kind) {
5102 case HLoadClass::LoadKind::kReferrersClass:
5103 break;
5104 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5105 DCHECK(!GetCompilerOptions().GetCompilePic());
5106 break;
5107 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5108 DCHECK(GetCompilerOptions().GetCompilePic());
5109 break;
5110 case HLoadClass::LoadKind::kBootImageAddress:
5111 break;
5112 case HLoadClass::LoadKind::kDexCacheAddress:
5113 DCHECK(Runtime::Current()->UseJitCompilation());
5114 break;
5115 case HLoadClass::LoadKind::kDexCachePcRelative:
5116 DCHECK(!Runtime::Current()->UseJitCompilation());
5117 // We disable pc-relative load when there is an irreducible loop, as the optimization
5118 // is incompatible with it.
5119 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5120 // with irreducible loops.
5121 if (GetGraph()->HasIrreducibleLoops()) {
5122 return HLoadClass::LoadKind::kDexCacheViaMethod;
5123 }
5124 break;
5125 case HLoadClass::LoadKind::kDexCacheViaMethod:
5126 break;
5127 }
5128 return desired_class_load_kind;
5129}
5130
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005131void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005132 if (cls->NeedsAccessCheck()) {
5133 InvokeRuntimeCallingConvention calling_convention;
5134 CodeGenerator::CreateLoadClassLocationSummary(
5135 cls,
5136 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5137 Location::RegisterLocation(R0),
5138 /* code_generator_supports_read_barrier */ true);
5139 return;
5140 }
5141
5142 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5143 ? LocationSummary::kCallOnSlowPath
5144 : LocationSummary::kNoCall;
5145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5146 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5147 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5148 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5149 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5150 locations->SetInAt(0, Location::RequiresRegister());
5151 }
5152 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005153}
5154
5155void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005156 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005157 if (cls->NeedsAccessCheck()) {
5158 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5159 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5160 cls,
5161 cls->GetDexPc(),
5162 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005163 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005164 return;
5165 }
5166
Roland Levillain3b359c72015-11-17 19:35:12 +00005167 Location out_loc = locations->Out();
5168 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005169
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005170 bool generate_null_check = false;
5171 switch (cls->GetLoadKind()) {
5172 case HLoadClass::LoadKind::kReferrersClass: {
5173 DCHECK(!cls->CanCallRuntime());
5174 DCHECK(!cls->MustGenerateClinitCheck());
5175 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5176 Register current_method = locations->InAt(0).AsRegister<Register>();
5177 GenerateGcRootFieldLoad(
5178 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5179 break;
5180 }
5181 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5182 DCHECK(!kEmitCompilerReadBarrier);
5183 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5184 cls->GetTypeIndex()));
5185 break;
5186 }
5187 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5188 DCHECK(!kEmitCompilerReadBarrier);
5189 CodeGeneratorARM::PcRelativePatchInfo* labels =
5190 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5191 __ BindTrackedLabel(&labels->movw_label);
5192 __ movw(out, /* placeholder */ 0u);
5193 __ BindTrackedLabel(&labels->movt_label);
5194 __ movt(out, /* placeholder */ 0u);
5195 __ BindTrackedLabel(&labels->add_pc_label);
5196 __ add(out, out, ShifterOperand(PC));
5197 break;
5198 }
5199 case HLoadClass::LoadKind::kBootImageAddress: {
5200 DCHECK(!kEmitCompilerReadBarrier);
5201 DCHECK_NE(cls->GetAddress(), 0u);
5202 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5203 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5204 break;
5205 }
5206 case HLoadClass::LoadKind::kDexCacheAddress: {
5207 DCHECK_NE(cls->GetAddress(), 0u);
5208 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5209 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5210 // a 128B range. To try and reduce the number of literals if we load multiple types,
5211 // simply split the dex cache address to a 128B aligned base loaded from a literal
5212 // and the remaining offset embedded in the load.
5213 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5214 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5215 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5216 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5217 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5218 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5219 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5220 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5221 generate_null_check = !cls->IsInDexCache();
5222 break;
5223 }
5224 case HLoadClass::LoadKind::kDexCachePcRelative: {
5225 Register base_reg = locations->InAt(0).AsRegister<Register>();
5226 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5227 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5228 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5229 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5230 generate_null_check = !cls->IsInDexCache();
5231 break;
5232 }
5233 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5234 // /* GcRoot<mirror::Class>[] */ out =
5235 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5236 Register current_method = locations->InAt(0).AsRegister<Register>();
5237 __ LoadFromOffset(kLoadWord,
5238 out,
5239 current_method,
5240 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5241 // /* GcRoot<mirror::Class> */ out = out[type_index]
5242 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5243 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5244 generate_null_check = !cls->IsInDexCache();
5245 }
5246 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005247
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005248 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5249 DCHECK(cls->CanCallRuntime());
5250 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5251 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5252 codegen_->AddSlowPath(slow_path);
5253 if (generate_null_check) {
5254 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5255 }
5256 if (cls->MustGenerateClinitCheck()) {
5257 GenerateClassInitializationCheck(slow_path, out);
5258 } else {
5259 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005260 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005261 }
5262}
5263
5264void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5265 LocationSummary* locations =
5266 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5267 locations->SetInAt(0, Location::RequiresRegister());
5268 if (check->HasUses()) {
5269 locations->SetOut(Location::SameAsFirstInput());
5270 }
5271}
5272
5273void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005274 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005275 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005276 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005277 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005278 GenerateClassInitializationCheck(slow_path,
5279 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005280}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005281
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005282void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005283 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005284 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5285 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5286 __ b(slow_path->GetEntryLabel(), LT);
5287 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5288 // properly. Therefore, we do a memory fence.
5289 __ dmb(ISH);
5290 __ Bind(slow_path->GetExitLabel());
5291}
5292
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005293HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5294 HLoadString::LoadKind desired_string_load_kind) {
5295 if (kEmitCompilerReadBarrier) {
5296 switch (desired_string_load_kind) {
5297 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5298 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5299 case HLoadString::LoadKind::kBootImageAddress:
5300 // TODO: Implement for read barrier.
5301 return HLoadString::LoadKind::kDexCacheViaMethod;
5302 default:
5303 break;
5304 }
5305 }
5306 switch (desired_string_load_kind) {
5307 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5308 DCHECK(!GetCompilerOptions().GetCompilePic());
5309 break;
5310 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5311 DCHECK(GetCompilerOptions().GetCompilePic());
5312 break;
5313 case HLoadString::LoadKind::kBootImageAddress:
5314 break;
5315 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005316 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005317 break;
5318 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005319 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005320 // We disable pc-relative load when there is an irreducible loop, as the optimization
5321 // is incompatible with it.
5322 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5323 // with irreducible loops.
5324 if (GetGraph()->HasIrreducibleLoops()) {
5325 return HLoadString::LoadKind::kDexCacheViaMethod;
5326 }
5327 break;
5328 case HLoadString::LoadKind::kDexCacheViaMethod:
5329 break;
5330 }
5331 return desired_string_load_kind;
5332}
5333
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005334void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005335 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005336 ? LocationSummary::kCallOnSlowPath
5337 : LocationSummary::kNoCall;
5338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005339 HLoadString::LoadKind load_kind = load->GetLoadKind();
5340 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5341 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5342 locations->SetInAt(0, Location::RequiresRegister());
5343 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005344 locations->SetOut(Location::RequiresRegister());
5345}
5346
5347void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005348 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005349 Location out_loc = locations->Out();
5350 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005351
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005352 switch (load->GetLoadKind()) {
5353 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5354 DCHECK(!kEmitCompilerReadBarrier);
5355 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5356 load->GetStringIndex()));
5357 return; // No dex cache slow path.
5358 }
5359 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5360 DCHECK(!kEmitCompilerReadBarrier);
5361 CodeGeneratorARM::PcRelativePatchInfo* labels =
5362 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5363 __ BindTrackedLabel(&labels->movw_label);
5364 __ movw(out, /* placeholder */ 0u);
5365 __ BindTrackedLabel(&labels->movt_label);
5366 __ movt(out, /* placeholder */ 0u);
5367 __ BindTrackedLabel(&labels->add_pc_label);
5368 __ add(out, out, ShifterOperand(PC));
5369 return; // No dex cache slow path.
5370 }
5371 case HLoadString::LoadKind::kBootImageAddress: {
5372 DCHECK(!kEmitCompilerReadBarrier);
5373 DCHECK_NE(load->GetAddress(), 0u);
5374 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5375 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5376 return; // No dex cache slow path.
5377 }
5378 case HLoadString::LoadKind::kDexCacheAddress: {
5379 DCHECK_NE(load->GetAddress(), 0u);
5380 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5381 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5382 // a 128B range. To try and reduce the number of literals if we load multiple strings,
5383 // simply split the dex cache address to a 128B aligned base loaded from a literal
5384 // and the remaining offset embedded in the load.
5385 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
5386 DCHECK_ALIGNED(load->GetAddress(), 4u);
5387 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5388 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5389 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5390 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005391 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005392 GenerateGcRootFieldLoad(load, out_loc, out, offset);
5393 break;
5394 }
5395 case HLoadString::LoadKind::kDexCachePcRelative: {
5396 Register base_reg = locations->InAt(0).AsRegister<Register>();
5397 HArmDexCacheArraysBase* base = load->InputAt(0)->AsArmDexCacheArraysBase();
5398 int32_t offset = load->GetDexCacheElementOffset() - base->GetElementOffset();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005399 // /* GcRoot<mirror::String> */ out = *(dex_cache_arrays_base + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005400 GenerateGcRootFieldLoad(load, out_loc, base_reg, offset);
5401 break;
5402 }
5403 case HLoadString::LoadKind::kDexCacheViaMethod: {
5404 Register current_method = locations->InAt(0).AsRegister<Register>();
5405
5406 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5407 GenerateGcRootFieldLoad(
5408 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5409 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5410 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
5411 // /* GcRoot<mirror::String> */ out = out[string_index]
5412 GenerateGcRootFieldLoad(
5413 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
5414 break;
5415 }
5416 default:
5417 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5418 UNREACHABLE();
5419 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005420
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005421 if (!load->IsInDexCache()) {
5422 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5423 codegen_->AddSlowPath(slow_path);
5424 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5425 __ Bind(slow_path->GetExitLabel());
5426 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005427}
5428
David Brazdilcb1c0552015-08-04 16:22:25 +01005429static int32_t GetExceptionTlsOffset() {
5430 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5431}
5432
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005433void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5434 LocationSummary* locations =
5435 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5436 locations->SetOut(Location::RequiresRegister());
5437}
5438
5439void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005440 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005441 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5442}
5443
5444void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5445 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5446}
5447
5448void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005449 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005450 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005451}
5452
5453void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5454 LocationSummary* locations =
5455 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5456 InvokeRuntimeCallingConvention calling_convention;
5457 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5458}
5459
5460void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5461 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005462 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005463 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005464}
5465
Roland Levillainc9285912015-12-18 10:38:42 +00005466static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5467 return kEmitCompilerReadBarrier &&
5468 (kUseBakerReadBarrier ||
5469 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5470 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5471 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5472}
5473
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005474void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005475 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005476 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5477 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005478 case TypeCheckKind::kExactCheck:
5479 case TypeCheckKind::kAbstractClassCheck:
5480 case TypeCheckKind::kClassHierarchyCheck:
5481 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005482 call_kind =
5483 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005484 break;
5485 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005486 case TypeCheckKind::kUnresolvedCheck:
5487 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005488 call_kind = LocationSummary::kCallOnSlowPath;
5489 break;
5490 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005491
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005493 locations->SetInAt(0, Location::RequiresRegister());
5494 locations->SetInAt(1, Location::RequiresRegister());
5495 // The "out" register is used as a temporary, so it overlaps with the inputs.
5496 // Note that TypeCheckSlowPathARM uses this register too.
5497 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5498 // When read barriers are enabled, we need a temporary register for
5499 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005500 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005501 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005502 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005503}
5504
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005505void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005506 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005507 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005508 Location obj_loc = locations->InAt(0);
5509 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005510 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005511 Location out_loc = locations->Out();
5512 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005513 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005514 locations->GetTemp(0) :
5515 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005516 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005517 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5518 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5519 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005520 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005521 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005522
5523 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005524 // avoid null check if we know obj is not null.
5525 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005526 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005527 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005528
Roland Levillain3b359c72015-11-17 19:35:12 +00005529 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005530 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005531
Roland Levillainc9285912015-12-18 10:38:42 +00005532 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005533 case TypeCheckKind::kExactCheck: {
5534 __ cmp(out, ShifterOperand(cls));
5535 // Classes must be equal for the instanceof to succeed.
5536 __ b(&zero, NE);
5537 __ LoadImmediate(out, 1);
5538 __ b(&done);
5539 break;
5540 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005542 case TypeCheckKind::kAbstractClassCheck: {
5543 // If the class is abstract, we eagerly fetch the super class of the
5544 // object to avoid doing a comparison we know will fail.
5545 Label loop;
5546 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005547 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005548 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005549 // If `out` is null, we use it for the result, and jump to `done`.
5550 __ CompareAndBranchIfZero(out, &done);
5551 __ cmp(out, ShifterOperand(cls));
5552 __ b(&loop, NE);
5553 __ LoadImmediate(out, 1);
5554 if (zero.IsLinked()) {
5555 __ b(&done);
5556 }
5557 break;
5558 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005559
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005560 case TypeCheckKind::kClassHierarchyCheck: {
5561 // Walk over the class hierarchy to find a match.
5562 Label loop, success;
5563 __ Bind(&loop);
5564 __ cmp(out, ShifterOperand(cls));
5565 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005566 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005567 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005568 __ CompareAndBranchIfNonZero(out, &loop);
5569 // If `out` is null, we use it for the result, and jump to `done`.
5570 __ b(&done);
5571 __ Bind(&success);
5572 __ LoadImmediate(out, 1);
5573 if (zero.IsLinked()) {
5574 __ b(&done);
5575 }
5576 break;
5577 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005578
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005579 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005580 // Do an exact check.
5581 Label exact_check;
5582 __ cmp(out, ShifterOperand(cls));
5583 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005584 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005585 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005586 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005587 // If `out` is null, we use it for the result, and jump to `done`.
5588 __ CompareAndBranchIfZero(out, &done);
5589 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5590 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5591 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005592 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005593 __ LoadImmediate(out, 1);
5594 __ b(&done);
5595 break;
5596 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005597
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005598 case TypeCheckKind::kArrayCheck: {
5599 __ cmp(out, ShifterOperand(cls));
5600 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005601 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5602 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005603 codegen_->AddSlowPath(slow_path);
5604 __ b(slow_path->GetEntryLabel(), NE);
5605 __ LoadImmediate(out, 1);
5606 if (zero.IsLinked()) {
5607 __ b(&done);
5608 }
5609 break;
5610 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005611
Calin Juravle98893e12015-10-02 21:05:03 +01005612 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005613 case TypeCheckKind::kInterfaceCheck: {
5614 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005615 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005616 // cases.
5617 //
5618 // We cannot directly call the InstanceofNonTrivial runtime
5619 // entry point without resorting to a type checking slow path
5620 // here (i.e. by calling InvokeRuntime directly), as it would
5621 // require to assign fixed registers for the inputs of this
5622 // HInstanceOf instruction (following the runtime calling
5623 // convention), which might be cluttered by the potential first
5624 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005625 //
5626 // TODO: Introduce a new runtime entry point taking the object
5627 // to test (instead of its class) as argument, and let it deal
5628 // with the read barrier issues. This will let us refactor this
5629 // case of the `switch` code as it was previously (with a direct
5630 // call to the runtime not using a type checking slow path).
5631 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005632 DCHECK(locations->OnlyCallsOnSlowPath());
5633 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5634 /* is_fatal */ false);
5635 codegen_->AddSlowPath(slow_path);
5636 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005637 if (zero.IsLinked()) {
5638 __ b(&done);
5639 }
5640 break;
5641 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005642 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005643
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005645 __ Bind(&zero);
5646 __ LoadImmediate(out, 0);
5647 }
5648
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005649 if (done.IsLinked()) {
5650 __ Bind(&done);
5651 }
5652
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005653 if (slow_path != nullptr) {
5654 __ Bind(slow_path->GetExitLabel());
5655 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005656}
5657
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005658void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005659 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5660 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5661
Roland Levillain3b359c72015-11-17 19:35:12 +00005662 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5663 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005664 case TypeCheckKind::kExactCheck:
5665 case TypeCheckKind::kAbstractClassCheck:
5666 case TypeCheckKind::kClassHierarchyCheck:
5667 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005668 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5669 LocationSummary::kCallOnSlowPath :
5670 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 break;
5672 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005673 case TypeCheckKind::kUnresolvedCheck:
5674 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005675 call_kind = LocationSummary::kCallOnSlowPath;
5676 break;
5677 }
5678
Roland Levillain3b359c72015-11-17 19:35:12 +00005679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5680 locations->SetInAt(0, Location::RequiresRegister());
5681 locations->SetInAt(1, Location::RequiresRegister());
5682 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5683 locations->AddTemp(Location::RequiresRegister());
5684 // When read barriers are enabled, we need an additional temporary
5685 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005686 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005687 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005688 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005689}
5690
5691void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005692 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005693 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005694 Location obj_loc = locations->InAt(0);
5695 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005696 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005697 Location temp_loc = locations->GetTemp(0);
5698 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005699 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005700 locations->GetTemp(1) :
5701 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005702 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005703 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5704 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5705 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005706
Roland Levillain3b359c72015-11-17 19:35:12 +00005707 bool is_type_check_slow_path_fatal =
5708 (type_check_kind == TypeCheckKind::kExactCheck ||
5709 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5710 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5711 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5712 !instruction->CanThrowIntoCatchBlock();
5713 SlowPathCode* type_check_slow_path =
5714 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5715 is_type_check_slow_path_fatal);
5716 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005717
5718 Label done;
5719 // Avoid null check if we know obj is not null.
5720 if (instruction->MustDoNullCheck()) {
5721 __ CompareAndBranchIfZero(obj, &done);
5722 }
5723
Roland Levillain3b359c72015-11-17 19:35:12 +00005724 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005725 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005726
Roland Levillain3b359c72015-11-17 19:35:12 +00005727 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005728 case TypeCheckKind::kExactCheck:
5729 case TypeCheckKind::kArrayCheck: {
5730 __ cmp(temp, ShifterOperand(cls));
5731 // Jump to slow path for throwing the exception or doing a
5732 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005733 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005734 break;
5735 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005736
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005737 case TypeCheckKind::kAbstractClassCheck: {
5738 // If the class is abstract, we eagerly fetch the super class of the
5739 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005740 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005741 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005742 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005743 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005744
5745 // If the class reference currently in `temp` is not null, jump
5746 // to the `compare_classes` label to compare it with the checked
5747 // class.
5748 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5749 // Otherwise, jump to the slow path to throw the exception.
5750 //
5751 // But before, move back the object's class into `temp` before
5752 // going into the slow path, as it has been overwritten in the
5753 // meantime.
5754 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005755 GenerateReferenceLoadTwoRegisters(
5756 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005757 __ b(type_check_slow_path->GetEntryLabel());
5758
5759 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005760 __ cmp(temp, ShifterOperand(cls));
5761 __ b(&loop, NE);
5762 break;
5763 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005764
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005765 case TypeCheckKind::kClassHierarchyCheck: {
5766 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005767 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005768 __ Bind(&loop);
5769 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005770 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005771
Roland Levillain3b359c72015-11-17 19:35:12 +00005772 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005773 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005774
5775 // If the class reference currently in `temp` is not null, jump
5776 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005777 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005778 // Otherwise, jump to the slow path to throw the exception.
5779 //
5780 // But before, move back the object's class into `temp` before
5781 // going into the slow path, as it has been overwritten in the
5782 // meantime.
5783 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005784 GenerateReferenceLoadTwoRegisters(
5785 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005786 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 break;
5788 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005789
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005791 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005792 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005793 __ cmp(temp, ShifterOperand(cls));
5794 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005795
5796 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005797 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005798 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005799
5800 // If the component type is not null (i.e. the object is indeed
5801 // an array), jump to label `check_non_primitive_component_type`
5802 // to further check that this component type is not a primitive
5803 // type.
5804 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5805 // Otherwise, jump to the slow path to throw the exception.
5806 //
5807 // But before, move back the object's class into `temp` before
5808 // going into the slow path, as it has been overwritten in the
5809 // meantime.
5810 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005811 GenerateReferenceLoadTwoRegisters(
5812 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005813 __ b(type_check_slow_path->GetEntryLabel());
5814
5815 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005816 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005817 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5818 __ CompareAndBranchIfZero(temp, &done);
5819 // Same comment as above regarding `temp` and the slow path.
5820 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005821 GenerateReferenceLoadTwoRegisters(
5822 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005823 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005824 break;
5825 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005826
Calin Juravle98893e12015-10-02 21:05:03 +01005827 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005828 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005829 // We always go into the type check slow path for the unresolved
5830 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005831 //
5832 // We cannot directly call the CheckCast runtime entry point
5833 // without resorting to a type checking slow path here (i.e. by
5834 // calling InvokeRuntime directly), as it would require to
5835 // assign fixed registers for the inputs of this HInstanceOf
5836 // instruction (following the runtime calling convention), which
5837 // might be cluttered by the potential first read barrier
5838 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005839 //
5840 // TODO: Introduce a new runtime entry point taking the object
5841 // to test (instead of its class) as argument, and let it deal
5842 // with the read barrier issues. This will let us refactor this
5843 // case of the `switch` code as it was previously (with a direct
5844 // call to the runtime not using a type checking slow path).
5845 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005846 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005847 break;
5848 }
5849 __ Bind(&done);
5850
Roland Levillain3b359c72015-11-17 19:35:12 +00005851 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005852}
5853
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005854void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5855 LocationSummary* locations =
5856 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5857 InvokeRuntimeCallingConvention calling_convention;
5858 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5859}
5860
5861void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5862 codegen_->InvokeRuntime(instruction->IsEnter()
5863 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5864 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005865 instruction->GetDexPc(),
5866 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005867 if (instruction->IsEnter()) {
5868 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5869 } else {
5870 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5871 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005872}
5873
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005874void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5875void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5876void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005877
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005878void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005879 LocationSummary* locations =
5880 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5881 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5882 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005883 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005884 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005885 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005887}
5888
5889void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5890 HandleBitwiseOperation(instruction);
5891}
5892
5893void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5894 HandleBitwiseOperation(instruction);
5895}
5896
5897void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5898 HandleBitwiseOperation(instruction);
5899}
5900
Artem Serov7fc63502016-02-09 17:15:29 +00005901
5902void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5903 LocationSummary* locations =
5904 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5905 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5906 || instruction->GetResultType() == Primitive::kPrimLong);
5907
5908 locations->SetInAt(0, Location::RequiresRegister());
5909 locations->SetInAt(1, Location::RequiresRegister());
5910 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5911}
5912
5913void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5914 LocationSummary* locations = instruction->GetLocations();
5915 Location first = locations->InAt(0);
5916 Location second = locations->InAt(1);
5917 Location out = locations->Out();
5918
5919 if (instruction->GetResultType() == Primitive::kPrimInt) {
5920 Register first_reg = first.AsRegister<Register>();
5921 ShifterOperand second_reg(second.AsRegister<Register>());
5922 Register out_reg = out.AsRegister<Register>();
5923
5924 switch (instruction->GetOpKind()) {
5925 case HInstruction::kAnd:
5926 __ bic(out_reg, first_reg, second_reg);
5927 break;
5928 case HInstruction::kOr:
5929 __ orn(out_reg, first_reg, second_reg);
5930 break;
5931 // There is no EON on arm.
5932 case HInstruction::kXor:
5933 default:
5934 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
5935 UNREACHABLE();
5936 }
5937 return;
5938
5939 } else {
5940 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5941 Register first_low = first.AsRegisterPairLow<Register>();
5942 Register first_high = first.AsRegisterPairHigh<Register>();
5943 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5944 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5945 Register out_low = out.AsRegisterPairLow<Register>();
5946 Register out_high = out.AsRegisterPairHigh<Register>();
5947
5948 switch (instruction->GetOpKind()) {
5949 case HInstruction::kAnd:
5950 __ bic(out_low, first_low, second_low);
5951 __ bic(out_high, first_high, second_high);
5952 break;
5953 case HInstruction::kOr:
5954 __ orn(out_low, first_low, second_low);
5955 __ orn(out_high, first_high, second_high);
5956 break;
5957 // There is no EON on arm.
5958 case HInstruction::kXor:
5959 default:
5960 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
5961 UNREACHABLE();
5962 }
5963 }
5964}
5965
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005966void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5967 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5968 if (value == 0xffffffffu) {
5969 if (out != first) {
5970 __ mov(out, ShifterOperand(first));
5971 }
5972 return;
5973 }
5974 if (value == 0u) {
5975 __ mov(out, ShifterOperand(0));
5976 return;
5977 }
5978 ShifterOperand so;
5979 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5980 __ and_(out, first, so);
5981 } else {
5982 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5983 __ bic(out, first, ShifterOperand(~value));
5984 }
5985}
5986
5987void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5988 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5989 if (value == 0u) {
5990 if (out != first) {
5991 __ mov(out, ShifterOperand(first));
5992 }
5993 return;
5994 }
5995 if (value == 0xffffffffu) {
5996 __ mvn(out, ShifterOperand(0));
5997 return;
5998 }
5999 ShifterOperand so;
6000 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6001 __ orr(out, first, so);
6002 } else {
6003 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6004 __ orn(out, first, ShifterOperand(~value));
6005 }
6006}
6007
6008void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6009 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6010 if (value == 0u) {
6011 if (out != first) {
6012 __ mov(out, ShifterOperand(first));
6013 }
6014 return;
6015 }
6016 __ eor(out, first, ShifterOperand(value));
6017}
6018
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006019void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6020 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006021 Location first = locations->InAt(0);
6022 Location second = locations->InAt(1);
6023 Location out = locations->Out();
6024
6025 if (second.IsConstant()) {
6026 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6027 uint32_t value_low = Low32Bits(value);
6028 if (instruction->GetResultType() == Primitive::kPrimInt) {
6029 Register first_reg = first.AsRegister<Register>();
6030 Register out_reg = out.AsRegister<Register>();
6031 if (instruction->IsAnd()) {
6032 GenerateAndConst(out_reg, first_reg, value_low);
6033 } else if (instruction->IsOr()) {
6034 GenerateOrrConst(out_reg, first_reg, value_low);
6035 } else {
6036 DCHECK(instruction->IsXor());
6037 GenerateEorConst(out_reg, first_reg, value_low);
6038 }
6039 } else {
6040 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6041 uint32_t value_high = High32Bits(value);
6042 Register first_low = first.AsRegisterPairLow<Register>();
6043 Register first_high = first.AsRegisterPairHigh<Register>();
6044 Register out_low = out.AsRegisterPairLow<Register>();
6045 Register out_high = out.AsRegisterPairHigh<Register>();
6046 if (instruction->IsAnd()) {
6047 GenerateAndConst(out_low, first_low, value_low);
6048 GenerateAndConst(out_high, first_high, value_high);
6049 } else if (instruction->IsOr()) {
6050 GenerateOrrConst(out_low, first_low, value_low);
6051 GenerateOrrConst(out_high, first_high, value_high);
6052 } else {
6053 DCHECK(instruction->IsXor());
6054 GenerateEorConst(out_low, first_low, value_low);
6055 GenerateEorConst(out_high, first_high, value_high);
6056 }
6057 }
6058 return;
6059 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006060
6061 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006062 Register first_reg = first.AsRegister<Register>();
6063 ShifterOperand second_reg(second.AsRegister<Register>());
6064 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006065 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006066 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006067 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006068 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006069 } else {
6070 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006071 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006072 }
6073 } else {
6074 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006075 Register first_low = first.AsRegisterPairLow<Register>();
6076 Register first_high = first.AsRegisterPairHigh<Register>();
6077 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6078 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6079 Register out_low = out.AsRegisterPairLow<Register>();
6080 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006081 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006082 __ and_(out_low, first_low, second_low);
6083 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006084 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006085 __ orr(out_low, first_low, second_low);
6086 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006087 } else {
6088 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006089 __ eor(out_low, first_low, second_low);
6090 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006091 }
6092 }
6093}
6094
Roland Levillainc9285912015-12-18 10:38:42 +00006095void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6096 Location out,
6097 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006098 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006099 Register out_reg = out.AsRegister<Register>();
6100 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006101 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006102 if (kUseBakerReadBarrier) {
6103 // Load with fast path based Baker's read barrier.
6104 // /* HeapReference<Object> */ out = *(out + offset)
6105 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006106 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006107 } else {
6108 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006109 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006110 // in the following move operation, as we will need it for the
6111 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006112 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006113 // /* HeapReference<Object> */ out = *(out + offset)
6114 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006115 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006116 }
6117 } else {
6118 // Plain load with no read barrier.
6119 // /* HeapReference<Object> */ out = *(out + offset)
6120 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6121 __ MaybeUnpoisonHeapReference(out_reg);
6122 }
6123}
6124
6125void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6126 Location out,
6127 Location obj,
6128 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006129 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006130 Register out_reg = out.AsRegister<Register>();
6131 Register obj_reg = obj.AsRegister<Register>();
6132 if (kEmitCompilerReadBarrier) {
6133 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006134 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006135 // Load with fast path based Baker's read barrier.
6136 // /* HeapReference<Object> */ out = *(obj + offset)
6137 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006138 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006139 } else {
6140 // Load with slow path based read barrier.
6141 // /* HeapReference<Object> */ out = *(obj + offset)
6142 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6143 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6144 }
6145 } else {
6146 // Plain load with no read barrier.
6147 // /* HeapReference<Object> */ out = *(obj + offset)
6148 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6149 __ MaybeUnpoisonHeapReference(out_reg);
6150 }
6151}
6152
6153void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6154 Location root,
6155 Register obj,
6156 uint32_t offset) {
6157 Register root_reg = root.AsRegister<Register>();
6158 if (kEmitCompilerReadBarrier) {
6159 if (kUseBakerReadBarrier) {
6160 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6161 // Baker's read barrier are used:
6162 //
6163 // root = obj.field;
6164 // if (Thread::Current()->GetIsGcMarking()) {
6165 // root = ReadBarrier::Mark(root)
6166 // }
6167
6168 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6169 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6170 static_assert(
6171 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6172 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6173 "have different sizes.");
6174 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6175 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6176 "have different sizes.");
6177
6178 // Slow path used to mark the GC root `root`.
6179 SlowPathCode* slow_path =
6180 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
6181 codegen_->AddSlowPath(slow_path);
6182
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006183 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006184 __ LoadFromOffset(
6185 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6186 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6187 __ Bind(slow_path->GetExitLabel());
6188 } else {
6189 // GC root loaded through a slow path for read barriers other
6190 // than Baker's.
6191 // /* GcRoot<mirror::Object>* */ root = obj + offset
6192 __ AddConstant(root_reg, obj, offset);
6193 // /* mirror::Object* */ root = root->Read()
6194 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6195 }
6196 } else {
6197 // Plain GC root load with no read barrier.
6198 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6199 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6200 // Note that GC roots are not affected by heap poisoning, thus we
6201 // do not have to unpoison `root_reg` here.
6202 }
6203}
6204
6205void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6206 Location ref,
6207 Register obj,
6208 uint32_t offset,
6209 Location temp,
6210 bool needs_null_check) {
6211 DCHECK(kEmitCompilerReadBarrier);
6212 DCHECK(kUseBakerReadBarrier);
6213
6214 // /* HeapReference<Object> */ ref = *(obj + offset)
6215 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006216 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006217 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006218 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006219}
6220
6221void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6222 Location ref,
6223 Register obj,
6224 uint32_t data_offset,
6225 Location index,
6226 Location temp,
6227 bool needs_null_check) {
6228 DCHECK(kEmitCompilerReadBarrier);
6229 DCHECK(kUseBakerReadBarrier);
6230
Roland Levillainbfea3352016-06-23 13:48:47 +01006231 static_assert(
6232 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6233 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006234 // /* HeapReference<Object> */ ref =
6235 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006236 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006237 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006238 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006239}
6240
6241void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6242 Location ref,
6243 Register obj,
6244 uint32_t offset,
6245 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006246 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006247 Location temp,
6248 bool needs_null_check) {
6249 DCHECK(kEmitCompilerReadBarrier);
6250 DCHECK(kUseBakerReadBarrier);
6251
6252 // In slow path based read barriers, the read barrier call is
6253 // inserted after the original load. However, in fast path based
6254 // Baker's read barriers, we need to perform the load of
6255 // mirror::Object::monitor_ *before* the original reference load.
6256 // This load-load ordering is required by the read barrier.
6257 // The fast path/slow path (for Baker's algorithm) should look like:
6258 //
6259 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6260 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6261 // HeapReference<Object> ref = *src; // Original reference load.
6262 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6263 // if (is_gray) {
6264 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6265 // }
6266 //
6267 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006268 // slightly more complex as it performs additional checks that we do
6269 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006270
6271 Register ref_reg = ref.AsRegister<Register>();
6272 Register temp_reg = temp.AsRegister<Register>();
6273 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6274
6275 // /* int32_t */ monitor = obj->monitor_
6276 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6277 if (needs_null_check) {
6278 MaybeRecordImplicitNullCheck(instruction);
6279 }
6280 // /* LockWord */ lock_word = LockWord(monitor)
6281 static_assert(sizeof(LockWord) == sizeof(int32_t),
6282 "art::LockWord and int32_t have different sizes.");
6283 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6284 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6285 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6286 static_assert(
6287 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6288 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6289
6290 // Introduce a dependency on the high bits of rb_state, which shall
6291 // be all zeroes, to prevent load-load reordering, and without using
6292 // a memory barrier (which would be more expensive).
6293 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6294 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6295 // obj is unchanged by this operation, but its value now depends on
6296 // IP, which depends on temp_reg.
6297 __ add(obj, obj, ShifterOperand(IP));
6298
6299 // The actual reference load.
6300 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006301 // Load types involving an "index": ArrayGet and
6302 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6303 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006304 if (index.IsConstant()) {
6305 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006306 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006307 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6308 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006309 // Handle the special case of the
6310 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6311 // a register pair as index ("long offset"), of which only the low
6312 // part contains data.
6313 Register index_reg = index.IsRegisterPair()
6314 ? index.AsRegisterPairLow<Register>()
6315 : index.AsRegister<Register>();
6316 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006317 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6318 }
6319 } else {
6320 // /* HeapReference<Object> */ ref = *(obj + offset)
6321 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6322 }
6323
6324 // Object* ref = ref_addr->AsMirrorPtr()
6325 __ MaybeUnpoisonHeapReference(ref_reg);
6326
6327 // Slow path used to mark the object `ref` when it is gray.
6328 SlowPathCode* slow_path =
6329 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6330 AddSlowPath(slow_path);
6331
6332 // if (rb_state == ReadBarrier::gray_ptr_)
6333 // ref = ReadBarrier::Mark(ref);
6334 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6335 __ b(slow_path->GetEntryLabel(), EQ);
6336 __ Bind(slow_path->GetExitLabel());
6337}
6338
6339void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6340 Location out,
6341 Location ref,
6342 Location obj,
6343 uint32_t offset,
6344 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006345 DCHECK(kEmitCompilerReadBarrier);
6346
Roland Levillainc9285912015-12-18 10:38:42 +00006347 // Insert a slow path based read barrier *after* the reference load.
6348 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006349 // If heap poisoning is enabled, the unpoisoning of the loaded
6350 // reference will be carried out by the runtime within the slow
6351 // path.
6352 //
6353 // Note that `ref` currently does not get unpoisoned (when heap
6354 // poisoning is enabled), which is alright as the `ref` argument is
6355 // not used by the artReadBarrierSlow entry point.
6356 //
6357 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6358 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6359 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6360 AddSlowPath(slow_path);
6361
Roland Levillain3b359c72015-11-17 19:35:12 +00006362 __ b(slow_path->GetEntryLabel());
6363 __ Bind(slow_path->GetExitLabel());
6364}
6365
Roland Levillainc9285912015-12-18 10:38:42 +00006366void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6367 Location out,
6368 Location ref,
6369 Location obj,
6370 uint32_t offset,
6371 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006372 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006373 // Baker's read barriers shall be handled by the fast path
6374 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6375 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006376 // If heap poisoning is enabled, unpoisoning will be taken care of
6377 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006378 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006379 } else if (kPoisonHeapReferences) {
6380 __ UnpoisonHeapReference(out.AsRegister<Register>());
6381 }
6382}
6383
Roland Levillainc9285912015-12-18 10:38:42 +00006384void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6385 Location out,
6386 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006387 DCHECK(kEmitCompilerReadBarrier);
6388
Roland Levillainc9285912015-12-18 10:38:42 +00006389 // Insert a slow path based read barrier *after* the GC root load.
6390 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006391 // Note that GC roots are not affected by heap poisoning, so we do
6392 // not need to do anything special for this here.
6393 SlowPathCode* slow_path =
6394 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6395 AddSlowPath(slow_path);
6396
Roland Levillain3b359c72015-11-17 19:35:12 +00006397 __ b(slow_path->GetEntryLabel());
6398 __ Bind(slow_path->GetExitLabel());
6399}
6400
Vladimir Markodc151b22015-10-15 18:02:30 +01006401HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6402 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6403 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006404 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6405 // We disable pc-relative load when there is an irreducible loop, as the optimization
6406 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006407 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6408 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006409 if (GetGraph()->HasIrreducibleLoops() &&
6410 (dispatch_info.method_load_kind ==
6411 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6412 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6413 }
6414
6415 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006416 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6417 if (&outer_dex_file != target_method.dex_file) {
6418 // Calls across dex files are more likely to exceed the available BL range,
6419 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6420 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6421 (desired_dispatch_info.method_load_kind ==
6422 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6423 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6424 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6425 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006426 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006427 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006428 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006429 0u
6430 };
6431 }
6432 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006433 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006434}
6435
Vladimir Markob4536b72015-11-24 13:45:23 +00006436Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6437 Register temp) {
6438 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6439 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6440 if (!invoke->GetLocations()->Intrinsified()) {
6441 return location.AsRegister<Register>();
6442 }
6443 // For intrinsics we allow any location, so it may be on the stack.
6444 if (!location.IsRegister()) {
6445 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6446 return temp;
6447 }
6448 // For register locations, check if the register was saved. If so, get it from the stack.
6449 // Note: There is a chance that the register was saved but not overwritten, so we could
6450 // save one load. However, since this is just an intrinsic slow path we prefer this
6451 // simple and more robust approach rather that trying to determine if that's the case.
6452 SlowPathCode* slow_path = GetCurrentSlowPath();
6453 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6454 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6455 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6456 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6457 return temp;
6458 }
6459 return location.AsRegister<Register>();
6460}
6461
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006462void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006463 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006464 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006465 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6466 // LR = code address from literal pool with link-time patch.
6467 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006468 break;
6469 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6470 // LR = invoke->GetDirectCodePtr();
6471 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006472 break;
6473 default:
6474 break;
6475 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006476
Vladimir Marko58155012015-08-19 12:49:41 +00006477 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6478 switch (invoke->GetMethodLoadKind()) {
6479 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6480 // temp = thread->string_init_entrypoint
6481 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6482 break;
6483 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006484 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006485 break;
6486 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6487 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6488 break;
6489 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6490 __ LoadLiteral(temp.AsRegister<Register>(),
6491 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6492 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006493 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6494 HArmDexCacheArraysBase* base =
6495 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6496 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6497 temp.AsRegister<Register>());
6498 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6499 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6500 break;
6501 }
Vladimir Marko58155012015-08-19 12:49:41 +00006502 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006503 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006504 Register method_reg;
6505 Register reg = temp.AsRegister<Register>();
6506 if (current_method.IsRegister()) {
6507 method_reg = current_method.AsRegister<Register>();
6508 } else {
6509 DCHECK(invoke->GetLocations()->Intrinsified());
6510 DCHECK(!current_method.IsValid());
6511 method_reg = reg;
6512 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6513 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006514 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6515 __ LoadFromOffset(kLoadWord,
6516 reg,
6517 method_reg,
6518 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006519 // temp = temp[index_in_cache];
6520 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6521 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006522 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6523 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006524 }
Vladimir Marko58155012015-08-19 12:49:41 +00006525 }
6526
6527 switch (invoke->GetCodePtrLocation()) {
6528 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6529 __ bl(GetFrameEntryLabel());
6530 break;
6531 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006532 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006533 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006534 // Arbitrarily branch to the BL itself, override at link time.
6535 __ bl(&relative_call_patches_.back().label);
6536 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006537 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6538 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6539 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006540 // LR()
6541 __ blx(LR);
6542 break;
6543 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6544 // LR = callee_method->entry_point_from_quick_compiled_code_
6545 __ LoadFromOffset(
6546 kLoadWord, LR, callee_method.AsRegister<Register>(),
6547 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6548 // LR()
6549 __ blx(LR);
6550 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006551 }
6552
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006553 DCHECK(!IsLeafMethod());
6554}
6555
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006556void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6557 Register temp = temp_location.AsRegister<Register>();
6558 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6559 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006560
6561 // Use the calling convention instead of the location of the receiver, as
6562 // intrinsics may have put the receiver in a different register. In the intrinsics
6563 // slow path, the arguments have been moved to the right place, so here we are
6564 // guaranteed that the receiver is the first register of the calling convention.
6565 InvokeDexCallingConvention calling_convention;
6566 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006567 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006568 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006569 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006570 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006571 // Instead of simply (possibly) unpoisoning `temp` here, we should
6572 // emit a read barrier for the previous class reference load.
6573 // However this is not required in practice, as this is an
6574 // intermediate/temporary reference and because the current
6575 // concurrent copying collector keeps the from-space memory
6576 // intact/accessible until the end of the marking phase (the
6577 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006578 __ MaybeUnpoisonHeapReference(temp);
6579 // temp = temp->GetMethodAt(method_offset);
6580 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6581 kArmWordSize).Int32Value();
6582 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6583 // LR = temp->GetEntryPoint();
6584 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6585 // LR();
6586 __ blx(LR);
6587}
6588
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6590 const DexFile& dex_file, uint32_t string_index) {
6591 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6592}
6593
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006594CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6595 const DexFile& dex_file, uint32_t type_index) {
6596 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6597}
6598
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006599CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6600 const DexFile& dex_file, uint32_t element_offset) {
6601 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6602}
6603
6604CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6605 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6606 patches->emplace_back(dex_file, offset_or_index);
6607 return &patches->back();
6608}
6609
6610Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6611 uint32_t string_index) {
6612 return boot_image_string_patches_.GetOrCreate(
6613 StringReference(&dex_file, string_index),
6614 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6615}
6616
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006617Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6618 uint32_t type_index) {
6619 return boot_image_type_patches_.GetOrCreate(
6620 TypeReference(&dex_file, type_index),
6621 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6622}
6623
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006624Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6625 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6626 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6627 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6628}
6629
6630Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6631 return DeduplicateUint32Literal(address, &uint32_literals_);
6632}
6633
Vladimir Marko58155012015-08-19 12:49:41 +00006634void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6635 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006636 size_t size =
6637 method_patches_.size() +
6638 call_patches_.size() +
6639 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006640 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6641 boot_image_string_patches_.size() +
6642 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006643 boot_image_type_patches_.size() +
6644 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006645 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006646 linker_patches->reserve(size);
6647 for (const auto& entry : method_patches_) {
6648 const MethodReference& target_method = entry.first;
6649 Literal* literal = entry.second;
6650 DCHECK(literal->GetLabel()->IsBound());
6651 uint32_t literal_offset = literal->GetLabel()->Position();
6652 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6653 target_method.dex_file,
6654 target_method.dex_method_index));
6655 }
6656 for (const auto& entry : call_patches_) {
6657 const MethodReference& target_method = entry.first;
6658 Literal* literal = entry.second;
6659 DCHECK(literal->GetLabel()->IsBound());
6660 uint32_t literal_offset = literal->GetLabel()->Position();
6661 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6662 target_method.dex_file,
6663 target_method.dex_method_index));
6664 }
6665 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6666 uint32_t literal_offset = info.label.Position();
6667 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6668 info.target_method.dex_file,
6669 info.target_method.dex_method_index));
6670 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006671 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6672 const DexFile& dex_file = info.target_dex_file;
6673 size_t base_element_offset = info.offset_or_index;
6674 DCHECK(info.add_pc_label.IsBound());
6675 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006676 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006677 DCHECK(info.movw_label.IsBound());
6678 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006679 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6680 &dex_file,
6681 add_pc_offset,
6682 base_element_offset));
6683 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006684 DCHECK(info.movt_label.IsBound());
6685 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006686 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6687 &dex_file,
6688 add_pc_offset,
6689 base_element_offset));
6690 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006691 for (const auto& entry : boot_image_string_patches_) {
6692 const StringReference& target_string = entry.first;
6693 Literal* literal = entry.second;
6694 DCHECK(literal->GetLabel()->IsBound());
6695 uint32_t literal_offset = literal->GetLabel()->Position();
6696 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6697 target_string.dex_file,
6698 target_string.string_index));
6699 }
6700 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6701 const DexFile& dex_file = info.target_dex_file;
6702 uint32_t string_index = info.offset_or_index;
6703 DCHECK(info.add_pc_label.IsBound());
6704 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6705 // Add MOVW patch.
6706 DCHECK(info.movw_label.IsBound());
6707 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6708 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6709 &dex_file,
6710 add_pc_offset,
6711 string_index));
6712 // Add MOVT patch.
6713 DCHECK(info.movt_label.IsBound());
6714 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6715 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6716 &dex_file,
6717 add_pc_offset,
6718 string_index));
6719 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006720 for (const auto& entry : boot_image_type_patches_) {
6721 const TypeReference& target_type = entry.first;
6722 Literal* literal = entry.second;
6723 DCHECK(literal->GetLabel()->IsBound());
6724 uint32_t literal_offset = literal->GetLabel()->Position();
6725 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6726 target_type.dex_file,
6727 target_type.type_index));
6728 }
6729 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6730 const DexFile& dex_file = info.target_dex_file;
6731 uint32_t type_index = info.offset_or_index;
6732 DCHECK(info.add_pc_label.IsBound());
6733 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6734 // Add MOVW patch.
6735 DCHECK(info.movw_label.IsBound());
6736 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6737 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6738 &dex_file,
6739 add_pc_offset,
6740 type_index));
6741 // Add MOVT patch.
6742 DCHECK(info.movt_label.IsBound());
6743 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6744 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6745 &dex_file,
6746 add_pc_offset,
6747 type_index));
6748 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006749 for (const auto& entry : boot_image_address_patches_) {
6750 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6751 Literal* literal = entry.second;
6752 DCHECK(literal->GetLabel()->IsBound());
6753 uint32_t literal_offset = literal->GetLabel()->Position();
6754 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6755 }
6756}
6757
6758Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6759 return map->GetOrCreate(
6760 value,
6761 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006762}
6763
6764Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6765 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006766 return map->GetOrCreate(
6767 target_method,
6768 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006769}
6770
6771Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6772 return DeduplicateMethodLiteral(target_method, &method_patches_);
6773}
6774
6775Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6776 return DeduplicateMethodLiteral(target_method, &call_patches_);
6777}
6778
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006779void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6780 LocationSummary* locations =
6781 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6782 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6783 Location::RequiresRegister());
6784 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6785 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6787}
6788
6789void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6790 LocationSummary* locations = instr->GetLocations();
6791 Register res = locations->Out().AsRegister<Register>();
6792 Register accumulator =
6793 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6794 Register mul_left =
6795 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6796 Register mul_right =
6797 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6798
6799 if (instr->GetOpKind() == HInstruction::kAdd) {
6800 __ mla(res, mul_left, mul_right, accumulator);
6801 } else {
6802 __ mls(res, mul_left, mul_right, accumulator);
6803 }
6804}
6805
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006806void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006807 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006808 LOG(FATAL) << "Unreachable";
6809}
6810
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006811void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006812 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006813 LOG(FATAL) << "Unreachable";
6814}
6815
Mark Mendellfe57faa2015-09-18 09:26:15 -04006816// Simple implementation of packed switch - generate cascaded compare/jumps.
6817void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6818 LocationSummary* locations =
6819 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6820 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006821 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006822 codegen_->GetAssembler()->IsThumb()) {
6823 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6824 if (switch_instr->GetStartValue() != 0) {
6825 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6826 }
6827 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006828}
6829
6830void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6831 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006832 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006833 LocationSummary* locations = switch_instr->GetLocations();
6834 Register value_reg = locations->InAt(0).AsRegister<Register>();
6835 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6836
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006837 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006838 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006839 Register temp_reg = IP;
6840 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6841 // the immediate, because IP is used as the destination register. For the other
6842 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6843 // and they can be encoded in the instruction without making use of IP register.
6844 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6845
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006846 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006847 // Jump to successors[0] if value == lower_bound.
6848 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6849 int32_t last_index = 0;
6850 for (; num_entries - last_index > 2; last_index += 2) {
6851 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6852 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6853 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6854 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6855 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6856 }
6857 if (num_entries - last_index == 2) {
6858 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006859 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006860 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006861 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006862
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006863 // And the default for any other value.
6864 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6865 __ b(codegen_->GetLabelOf(default_block));
6866 }
6867 } else {
6868 // Create a table lookup.
6869 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6870
6871 // Materialize a pointer to the switch table
6872 std::vector<Label*> labels(num_entries);
6873 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6874 for (uint32_t i = 0; i < num_entries; i++) {
6875 labels[i] = codegen_->GetLabelOf(successors[i]);
6876 }
6877 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6878
6879 // Remove the bias.
6880 Register key_reg;
6881 if (lower_bound != 0) {
6882 key_reg = locations->GetTemp(1).AsRegister<Register>();
6883 __ AddConstant(key_reg, value_reg, -lower_bound);
6884 } else {
6885 key_reg = value_reg;
6886 }
6887
6888 // Check whether the value is in the table, jump to default block if not.
6889 __ CmpConstant(key_reg, num_entries - 1);
6890 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6891
6892 // Load the displacement from the table.
6893 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6894
6895 // Dispatch is a direct add to the PC (for Thumb2).
6896 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006897 }
6898}
6899
Vladimir Markob4536b72015-11-24 13:45:23 +00006900void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6902 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00006903}
6904
6905void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6906 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006907 CodeGeneratorARM::PcRelativePatchInfo* labels =
6908 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00006909 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006910 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00006911 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006912 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00006913 __ BindTrackedLabel(&labels->add_pc_label);
6914 __ add(base_reg, base_reg, ShifterOperand(PC));
6915}
6916
Andreas Gampe85b62f22015-09-09 13:15:38 -07006917void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6918 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006919 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006920 return;
6921 }
6922
6923 DCHECK_NE(type, Primitive::kPrimVoid);
6924
6925 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6926 if (return_loc.Equals(trg)) {
6927 return;
6928 }
6929
6930 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6931 // with the last branch.
6932 if (type == Primitive::kPrimLong) {
6933 HParallelMove parallel_move(GetGraph()->GetArena());
6934 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6935 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6936 GetMoveResolver()->EmitNativeCode(&parallel_move);
6937 } else if (type == Primitive::kPrimDouble) {
6938 HParallelMove parallel_move(GetGraph()->GetArena());
6939 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6940 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6941 GetMoveResolver()->EmitNativeCode(&parallel_move);
6942 } else {
6943 // Let the parallel move resolver take care of all of this.
6944 HParallelMove parallel_move(GetGraph()->GetArena());
6945 parallel_move.AddMove(return_loc, trg, type, nullptr);
6946 GetMoveResolver()->EmitNativeCode(&parallel_move);
6947 }
6948}
6949
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006950void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
6951 LocationSummary* locations =
6952 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6953 locations->SetInAt(0, Location::RequiresRegister());
6954 locations->SetOut(Location::RequiresRegister());
6955}
6956
6957void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
6958 LocationSummary* locations = instruction->GetLocations();
6959 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006960 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006961 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6962 instruction->GetIndex(), kArmPointerSize).SizeValue();
6963 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03006964 __ LoadFromOffset(kLoadWord, locations->Out().AsRegister<Register>(),
6965 locations->InAt(0).AsRegister<Register>(),
6966 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
6967 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Nicolas Geoffrayd4ceecc2016-06-29 08:39:47 +00006968 instruction->GetIndex() % ImTable::kSize, kArmPointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006969 }
6970 __ LoadFromOffset(kLoadWord,
6971 locations->Out().AsRegister<Register>(),
6972 locations->InAt(0).AsRegister<Register>(),
6973 method_offset);
6974}
6975
Roland Levillain4d027112015-07-01 15:41:14 +01006976#undef __
6977#undef QUICK_ENTRY_POINT
6978
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006979} // namespace arm
6980} // namespace art