blob: 474e9d540add361eb4644501526e0a81452da478 [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:
Roland Levillain02b75802016-07-13 11:54:35 +0100415 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
416 : SlowPathCode(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000417 DCHECK(kEmitCompilerReadBarrier);
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
421
422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100424 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000425 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100426 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000427 DCHECK(instruction_->IsInstanceFieldGet() ||
428 instruction_->IsStaticFieldGet() ||
429 instruction_->IsArrayGet() ||
430 instruction_->IsLoadClass() ||
431 instruction_->IsLoadString() ||
432 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100433 instruction_->IsCheckCast() ||
434 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
435 instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000436 << "Unexpected instruction in read barrier marking slow path: "
437 << instruction_->DebugName();
438
439 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100440 // No need to save live registers; it's taken care of by the
441 // entrypoint. Also, there is no need to update the stack mask,
442 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000443 InvokeRuntimeCallingConvention calling_convention;
444 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100445 DCHECK_NE(reg, SP);
446 DCHECK_NE(reg, LR);
447 DCHECK_NE(reg, PC);
448 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
449 // "Compact" slow path, saving two moves.
450 //
451 // Instead of using the standard runtime calling convention (input
452 // and output in R0):
453 //
454 // R0 <- obj
455 // R0 <- ReadBarrierMark(R0)
456 // obj <- R0
457 //
458 // we just use rX (the register holding `obj`) as input and output
459 // of a dedicated entrypoint:
460 //
461 // rX <- ReadBarrierMarkRegX(rX)
462 //
463 int32_t entry_point_offset =
464 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmWordSize>(reg);
465 // TODO: Do not emit a stack map for this runtime call.
466 arm_codegen->InvokeRuntime(entry_point_offset,
Roland Levillainc9285912015-12-18 10:38:42 +0000467 instruction_,
468 instruction_->GetDexPc(),
469 this);
Roland Levillainc9285912015-12-18 10:38:42 +0000470 __ b(GetExitLabel());
471 }
472
473 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000474 const Location obj_;
475
476 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
477};
478
Roland Levillain3b359c72015-11-17 19:35:12 +0000479// Slow path generating a read barrier for a heap reference.
480class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
481 public:
482 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
483 Location out,
484 Location ref,
485 Location obj,
486 uint32_t offset,
487 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000488 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000489 out_(out),
490 ref_(ref),
491 obj_(obj),
492 offset_(offset),
493 index_(index) {
494 DCHECK(kEmitCompilerReadBarrier);
495 // If `obj` is equal to `out` or `ref`, it means the initial object
496 // has been overwritten by (or after) the heap object reference load
497 // to be instrumented, e.g.:
498 //
499 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000500 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000501 //
502 // In that case, we have lost the information about the original
503 // object, and the emitted read barrier cannot work properly.
504 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
505 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
506 }
507
508 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
509 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
510 LocationSummary* locations = instruction_->GetLocations();
511 Register reg_out = out_.AsRegister<Register>();
512 DCHECK(locations->CanCall());
513 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100514 DCHECK(instruction_->IsInstanceFieldGet() ||
515 instruction_->IsStaticFieldGet() ||
516 instruction_->IsArrayGet() ||
517 instruction_->IsInstanceOf() ||
518 instruction_->IsCheckCast() ||
519 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillainc9285912015-12-18 10:38:42 +0000520 instruction_->GetLocations()->Intrinsified()))
521 << "Unexpected instruction in read barrier for heap reference slow path: "
522 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000523
524 __ Bind(GetEntryLabel());
525 SaveLiveRegisters(codegen, locations);
526
527 // We may have to change the index's value, but as `index_` is a
528 // constant member (like other "inputs" of this slow path),
529 // introduce a copy of it, `index`.
530 Location index = index_;
531 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100532 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000533 if (instruction_->IsArrayGet()) {
534 // Compute the actual memory offset and store it in `index`.
535 Register index_reg = index_.AsRegister<Register>();
536 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
537 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
538 // We are about to change the value of `index_reg` (see the
539 // calls to art::arm::Thumb2Assembler::Lsl and
540 // art::arm::Thumb2Assembler::AddConstant below), but it has
541 // not been saved by the previous call to
542 // art::SlowPathCode::SaveLiveRegisters, as it is a
543 // callee-save register --
544 // art::SlowPathCode::SaveLiveRegisters does not consider
545 // callee-save registers, as it has been designed with the
546 // assumption that callee-save registers are supposed to be
547 // handled by the called function. So, as a callee-save
548 // register, `index_reg` _would_ eventually be saved onto
549 // the stack, but it would be too late: we would have
550 // changed its value earlier. Therefore, we manually save
551 // it here into another freely available register,
552 // `free_reg`, chosen of course among the caller-save
553 // registers (as a callee-save `free_reg` register would
554 // exhibit the same problem).
555 //
556 // Note we could have requested a temporary register from
557 // the register allocator instead; but we prefer not to, as
558 // this is a slow path, and we know we can find a
559 // caller-save register that is available.
560 Register free_reg = FindAvailableCallerSaveRegister(codegen);
561 __ Mov(free_reg, index_reg);
562 index_reg = free_reg;
563 index = Location::RegisterLocation(index_reg);
564 } else {
565 // The initial register stored in `index_` has already been
566 // saved in the call to art::SlowPathCode::SaveLiveRegisters
567 // (as it is not a callee-save register), so we can freely
568 // use it.
569 }
570 // Shifting the index value contained in `index_reg` by the scale
571 // factor (2) cannot overflow in practice, as the runtime is
572 // unable to allocate object arrays with a size larger than
573 // 2^26 - 1 (that is, 2^28 - 4 bytes).
574 __ Lsl(index_reg, index_reg, TIMES_4);
575 static_assert(
576 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
577 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
578 __ AddConstant(index_reg, index_reg, offset_);
579 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100580 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
581 // intrinsics, `index_` is not shifted by a scale factor of 2
582 // (as in the case of ArrayGet), as it is actually an offset
583 // to an object field within an object.
584 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000585 DCHECK(instruction_->GetLocations()->Intrinsified());
586 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
587 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
588 << instruction_->AsInvoke()->GetIntrinsic();
589 DCHECK_EQ(offset_, 0U);
590 DCHECK(index_.IsRegisterPair());
591 // UnsafeGet's offset location is a register pair, the low
592 // part contains the correct offset.
593 index = index_.ToLow();
594 }
595 }
596
597 // We're moving two or three locations to locations that could
598 // overlap, so we need a parallel move resolver.
599 InvokeRuntimeCallingConvention calling_convention;
600 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
601 parallel_move.AddMove(ref_,
602 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
603 Primitive::kPrimNot,
604 nullptr);
605 parallel_move.AddMove(obj_,
606 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
607 Primitive::kPrimNot,
608 nullptr);
609 if (index.IsValid()) {
610 parallel_move.AddMove(index,
611 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
612 Primitive::kPrimInt,
613 nullptr);
614 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
615 } else {
616 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
617 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
618 }
619 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
620 instruction_,
621 instruction_->GetDexPc(),
622 this);
623 CheckEntrypointTypes<
624 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
625 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
626
627 RestoreLiveRegisters(codegen, locations);
628 __ b(GetExitLabel());
629 }
630
631 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
632
633 private:
634 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
635 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
636 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
637 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
638 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
639 return static_cast<Register>(i);
640 }
641 }
642 // We shall never fail to find a free caller-save register, as
643 // there are more than two core caller-save registers on ARM
644 // (meaning it is possible to find one which is different from
645 // `ref` and `obj`).
646 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
647 LOG(FATAL) << "Could not find a free caller-save register";
648 UNREACHABLE();
649 }
650
Roland Levillain3b359c72015-11-17 19:35:12 +0000651 const Location out_;
652 const Location ref_;
653 const Location obj_;
654 const uint32_t offset_;
655 // An additional location containing an index to an array.
656 // Only used for HArrayGet and the UnsafeGetObject &
657 // UnsafeGetObjectVolatile intrinsics.
658 const Location index_;
659
660 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
661};
662
663// Slow path generating a read barrier for a GC root.
664class ReadBarrierForRootSlowPathARM : public SlowPathCode {
665 public:
666 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000667 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000668 DCHECK(kEmitCompilerReadBarrier);
669 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000670
671 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
672 LocationSummary* locations = instruction_->GetLocations();
673 Register reg_out = out_.AsRegister<Register>();
674 DCHECK(locations->CanCall());
675 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000676 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
677 << "Unexpected instruction in read barrier for GC root slow path: "
678 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000679
680 __ Bind(GetEntryLabel());
681 SaveLiveRegisters(codegen, locations);
682
683 InvokeRuntimeCallingConvention calling_convention;
684 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
685 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
686 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
687 instruction_,
688 instruction_->GetDexPc(),
689 this);
690 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
691 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
692
693 RestoreLiveRegisters(codegen, locations);
694 __ b(GetExitLabel());
695 }
696
697 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
698
699 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000700 const Location out_;
701 const Location root_;
702
703 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
704};
705
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000706#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700707// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
708#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700709
Aart Bike9f37602015-10-09 11:15:55 -0700710inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700711 switch (cond) {
712 case kCondEQ: return EQ;
713 case kCondNE: return NE;
714 case kCondLT: return LT;
715 case kCondLE: return LE;
716 case kCondGT: return GT;
717 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700718 case kCondB: return LO;
719 case kCondBE: return LS;
720 case kCondA: return HI;
721 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700722 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100723 LOG(FATAL) << "Unreachable";
724 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700725}
726
Aart Bike9f37602015-10-09 11:15:55 -0700727// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100728inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700729 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730 case kCondEQ: return EQ;
731 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700732 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100733 case kCondLT: return LO;
734 case kCondLE: return LS;
735 case kCondGT: return HI;
736 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700737 // Unsigned remain unchanged.
738 case kCondB: return LO;
739 case kCondBE: return LS;
740 case kCondA: return HI;
741 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700742 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100743 LOG(FATAL) << "Unreachable";
744 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700745}
746
Vladimir Markod6e069b2016-01-18 11:11:01 +0000747inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
748 // The ARM condition codes can express all the necessary branches, see the
749 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
750 // There is no dex instruction or HIR that would need the missing conditions
751 // "equal or unordered" or "not equal".
752 switch (cond) {
753 case kCondEQ: return EQ;
754 case kCondNE: return NE /* unordered */;
755 case kCondLT: return gt_bias ? CC : LT /* unordered */;
756 case kCondLE: return gt_bias ? LS : LE /* unordered */;
757 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
758 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
759 default:
760 LOG(FATAL) << "UNREACHABLE";
761 UNREACHABLE();
762 }
763}
764
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100765void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100766 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100767}
768
769void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100770 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100771}
772
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100773size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
774 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
775 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100776}
777
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100778size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
779 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
780 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100781}
782
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000783size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
784 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
785 return kArmWordSize;
786}
787
788size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
789 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
790 return kArmWordSize;
791}
792
Calin Juravle34166012014-12-19 17:22:29 +0000793CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000794 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100795 const CompilerOptions& compiler_options,
796 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000797 : CodeGenerator(graph,
798 kNumberOfCoreRegisters,
799 kNumberOfSRegisters,
800 kNumberOfRegisterPairs,
801 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
802 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000803 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
804 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100805 compiler_options,
806 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100807 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100809 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100810 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100811 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000812 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000813 uint32_literals_(std::less<uint32_t>(),
814 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100815 method_patches_(MethodReferenceComparator(),
816 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
817 call_patches_(MethodReferenceComparator(),
818 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000819 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000820 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
821 boot_image_string_patches_(StringReferenceValueComparator(),
822 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
823 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100824 boot_image_type_patches_(TypeReferenceValueComparator(),
825 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
826 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000827 boot_image_address_patches_(std::less<uint32_t>(),
828 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700829 // Always save the LR register to mimic Quick.
830 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100831}
832
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000833void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
834 // Ensure that we fix up branches and literal loads and emit the literal pool.
835 __ FinalizeCode();
836
837 // Adjust native pc offsets in stack maps.
838 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
839 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
840 uint32_t new_position = __ GetAdjustedPosition(old_position);
841 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
842 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100843 // Adjust pc offsets for the disassembly information.
844 if (disasm_info_ != nullptr) {
845 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
846 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
847 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
848 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
849 it.second.start = __ GetAdjustedPosition(it.second.start);
850 it.second.end = __ GetAdjustedPosition(it.second.end);
851 }
852 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
853 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
854 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
855 }
856 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000857
858 CodeGenerator::Finalize(allocator);
859}
860
David Brazdil58282f42016-01-14 12:45:10 +0000861void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100862 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100863 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100864
865 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100866 blocked_core_registers_[SP] = true;
867 blocked_core_registers_[LR] = true;
868 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100869
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100871 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100872
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100873 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100874 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100875
David Brazdil58282f42016-01-14 12:45:10 +0000876 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100877 // Stubs do not save callee-save floating point registers. If the graph
878 // is debuggable, we need to deal with these registers differently. For
879 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000880 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
881 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
882 }
883 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100884
885 UpdateBlockedPairRegisters();
886}
887
888void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
889 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
890 ArmManagedRegister current =
891 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
892 if (blocked_core_registers_[current.AsRegisterPairLow()]
893 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
894 blocked_register_pairs_[i] = true;
895 }
896 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100897}
898
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100899InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800900 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100901 assembler_(codegen->GetAssembler()),
902 codegen_(codegen) {}
903
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000904void CodeGeneratorARM::ComputeSpillMask() {
905 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
906 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000907 // There is no easy instruction to restore just the PC on thumb2. We spill and
908 // restore another arbitrary register.
909 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000910 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
911 // We use vpush and vpop for saving and restoring floating point registers, which take
912 // a SRegister and the number of registers to save/restore after that SRegister. We
913 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
914 // but in the range.
915 if (fpu_spill_mask_ != 0) {
916 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
917 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
918 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
919 fpu_spill_mask_ |= (1 << i);
920 }
921 }
922}
923
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100924static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100925 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100926}
927
928static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100929 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930}
931
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000932void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000933 bool skip_overflow_check =
934 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000935 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000936 __ Bind(&frame_entry_label_);
937
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000938 if (HasEmptyFrame()) {
939 return;
940 }
941
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100942 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000943 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
944 __ LoadFromOffset(kLoadWord, IP, IP, 0);
945 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100946 }
947
Andreas Gampe501fd632015-09-10 16:11:06 -0700948 __ PushList(core_spill_mask_);
949 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
950 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000951 if (fpu_spill_mask_ != 0) {
952 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
953 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100954 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100955 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000956 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100957 int adjust = GetFrameSize() - FrameEntrySpillSize();
958 __ AddConstant(SP, -adjust);
959 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100960 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000961}
962
963void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000964 if (HasEmptyFrame()) {
965 __ bx(LR);
966 return;
967 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100968 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100969 int adjust = GetFrameSize() - FrameEntrySpillSize();
970 __ AddConstant(SP, adjust);
971 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000972 if (fpu_spill_mask_ != 0) {
973 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
974 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100975 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
976 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000977 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700978 // Pop LR into PC to return.
979 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
980 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
981 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100982 __ cfi().RestoreState();
983 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000984}
985
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100986void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700987 Label* label = GetLabelOf(block);
988 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000989}
990
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100991Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100992 switch (type) {
993 case Primitive::kPrimBoolean:
994 case Primitive::kPrimByte:
995 case Primitive::kPrimChar:
996 case Primitive::kPrimShort:
997 case Primitive::kPrimInt:
998 case Primitive::kPrimNot: {
999 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001000 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001001 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001002 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001003 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001004 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001005 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001006 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001008 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001009 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001010 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001011 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001012 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001014 if (calling_convention.GetRegisterAt(index) == R1) {
1015 // Skip R1, and use R2_R3 instead.
1016 gp_index_++;
1017 index++;
1018 }
1019 }
1020 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1021 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001022 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001023
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001024 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001025 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001026 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001027 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1028 }
1029 }
1030
1031 case Primitive::kPrimFloat: {
1032 uint32_t stack_index = stack_index_++;
1033 if (float_index_ % 2 == 0) {
1034 float_index_ = std::max(double_index_, float_index_);
1035 }
1036 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1037 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1038 } else {
1039 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1040 }
1041 }
1042
1043 case Primitive::kPrimDouble: {
1044 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1045 uint32_t stack_index = stack_index_;
1046 stack_index_ += 2;
1047 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1048 uint32_t index = double_index_;
1049 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001050 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001051 calling_convention.GetFpuRegisterAt(index),
1052 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001053 DCHECK(ExpectedPairLayout(result));
1054 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001055 } else {
1056 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001057 }
1058 }
1059
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001060 case Primitive::kPrimVoid:
1061 LOG(FATAL) << "Unexpected parameter type " << type;
1062 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001063 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001064 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001065}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001066
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001067Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001068 switch (type) {
1069 case Primitive::kPrimBoolean:
1070 case Primitive::kPrimByte:
1071 case Primitive::kPrimChar:
1072 case Primitive::kPrimShort:
1073 case Primitive::kPrimInt:
1074 case Primitive::kPrimNot: {
1075 return Location::RegisterLocation(R0);
1076 }
1077
1078 case Primitive::kPrimFloat: {
1079 return Location::FpuRegisterLocation(S0);
1080 }
1081
1082 case Primitive::kPrimLong: {
1083 return Location::RegisterPairLocation(R0, R1);
1084 }
1085
1086 case Primitive::kPrimDouble: {
1087 return Location::FpuRegisterPairLocation(S0, S1);
1088 }
1089
1090 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001091 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001092 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001093
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001094 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001095}
1096
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001097Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1098 return Location::RegisterLocation(kMethodRegisterArgument);
1099}
1100
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101void CodeGeneratorARM::Move32(Location destination, Location source) {
1102 if (source.Equals(destination)) {
1103 return;
1104 }
1105 if (destination.IsRegister()) {
1106 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001108 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 } else if (destination.IsFpuRegister()) {
1114 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001115 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001117 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001119 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001120 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001121 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001122 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001124 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001125 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001126 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001128 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001129 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1130 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001131 }
1132 }
1133}
1134
1135void CodeGeneratorARM::Move64(Location destination, Location source) {
1136 if (source.Equals(destination)) {
1137 return;
1138 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001139 if (destination.IsRegisterPair()) {
1140 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001141 EmitParallelMoves(
1142 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1143 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001144 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001145 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001146 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1147 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001149 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001150 } else if (source.IsFpuRegisterPair()) {
1151 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1152 destination.AsRegisterPairHigh<Register>(),
1153 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 } else {
1155 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001156 DCHECK(ExpectedPairLayout(destination));
1157 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1158 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001160 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001161 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001162 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1163 SP,
1164 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001165 } else if (source.IsRegisterPair()) {
1166 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1167 source.AsRegisterPairLow<Register>(),
1168 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001169 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001170 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 } else {
1173 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001175 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001176 if (source.AsRegisterPairLow<Register>() == R1) {
1177 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1179 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001181 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 SP, destination.GetStackIndex());
1183 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001184 } else if (source.IsFpuRegisterPair()) {
1185 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1186 SP,
1187 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188 } else {
1189 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001190 EmitParallelMoves(
1191 Location::StackSlot(source.GetStackIndex()),
1192 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001193 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001194 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001195 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1196 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197 }
1198 }
1199}
1200
Calin Juravle175dc732015-08-25 15:42:32 +01001201void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1202 DCHECK(location.IsRegister());
1203 __ LoadImmediate(location.AsRegister<Register>(), value);
1204}
1205
Calin Juravlee460d1d2015-09-29 04:52:17 +01001206void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001207 HParallelMove move(GetGraph()->GetArena());
1208 move.AddMove(src, dst, dst_type, nullptr);
1209 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001210}
1211
1212void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1213 if (location.IsRegister()) {
1214 locations->AddTemp(location);
1215 } else if (location.IsRegisterPair()) {
1216 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1217 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1218 } else {
1219 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1220 }
1221}
1222
Calin Juravle175dc732015-08-25 15:42:32 +01001223void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1224 HInstruction* instruction,
1225 uint32_t dex_pc,
1226 SlowPathCode* slow_path) {
1227 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1228 instruction,
1229 dex_pc,
1230 slow_path);
1231}
1232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001233void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1234 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001235 uint32_t dex_pc,
1236 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001237 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001238 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1239 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001240 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001241}
1242
David Brazdilfc6a86a2015-06-26 10:33:45 +00001243void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001244 DCHECK(!successor->IsExitBlock());
1245
1246 HBasicBlock* block = got->GetBlock();
1247 HInstruction* previous = got->GetPrevious();
1248
1249 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001250 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001251 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1252 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1253 return;
1254 }
1255
1256 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1257 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1258 }
1259 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001260 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001261 }
1262}
1263
David Brazdilfc6a86a2015-06-26 10:33:45 +00001264void LocationsBuilderARM::VisitGoto(HGoto* got) {
1265 got->SetLocations(nullptr);
1266}
1267
1268void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1269 HandleGoto(got, got->GetSuccessor());
1270}
1271
1272void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1273 try_boundary->SetLocations(nullptr);
1274}
1275
1276void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1277 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1278 if (!successor->IsExitBlock()) {
1279 HandleGoto(try_boundary, successor);
1280 }
1281}
1282
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001283void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001284 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001285}
1286
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001287void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001288}
1289
Roland Levillain4fa13f62015-07-06 18:11:54 +01001290void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1291 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001292 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001293 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001294 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001295}
1296
1297void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1298 Label* true_label,
1299 Label* false_label) {
1300 LocationSummary* locations = cond->GetLocations();
1301 Location left = locations->InAt(0);
1302 Location right = locations->InAt(1);
1303 IfCondition if_cond = cond->GetCondition();
1304
1305 Register left_high = left.AsRegisterPairHigh<Register>();
1306 Register left_low = left.AsRegisterPairLow<Register>();
1307 IfCondition true_high_cond = if_cond;
1308 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001309 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001310
1311 // Set the conditions for the test, remembering that == needs to be
1312 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001313 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001314 switch (if_cond) {
1315 case kCondEQ:
1316 case kCondNE:
1317 // Nothing to do.
1318 break;
1319 case kCondLT:
1320 false_high_cond = kCondGT;
1321 break;
1322 case kCondLE:
1323 true_high_cond = kCondLT;
1324 break;
1325 case kCondGT:
1326 false_high_cond = kCondLT;
1327 break;
1328 case kCondGE:
1329 true_high_cond = kCondGT;
1330 break;
Aart Bike9f37602015-10-09 11:15:55 -07001331 case kCondB:
1332 false_high_cond = kCondA;
1333 break;
1334 case kCondBE:
1335 true_high_cond = kCondB;
1336 break;
1337 case kCondA:
1338 false_high_cond = kCondB;
1339 break;
1340 case kCondAE:
1341 true_high_cond = kCondA;
1342 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343 }
1344 if (right.IsConstant()) {
1345 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1346 int32_t val_low = Low32Bits(value);
1347 int32_t val_high = High32Bits(value);
1348
Vladimir Markoac6ac102015-12-17 12:14:00 +00001349 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001350 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001351 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001352 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001353 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001355 __ b(true_label, ARMCondition(true_high_cond));
1356 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001357 }
1358 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001359 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001360 } else {
1361 Register right_high = right.AsRegisterPairHigh<Register>();
1362 Register right_low = right.AsRegisterPairLow<Register>();
1363
1364 __ cmp(left_high, ShifterOperand(right_high));
1365 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001366 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001367 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001368 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001369 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001370 __ b(true_label, ARMCondition(true_high_cond));
1371 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001372 }
1373 // Must be equal high, so compare the lows.
1374 __ cmp(left_low, ShifterOperand(right_low));
1375 }
1376 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001377 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001378 __ b(true_label, final_condition);
1379}
1380
David Brazdil0debae72015-11-12 18:37:00 +00001381void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1382 Label* true_target_in,
1383 Label* false_target_in) {
1384 // Generated branching requires both targets to be explicit. If either of the
1385 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1386 Label fallthrough_target;
1387 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1388 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1389
Roland Levillain4fa13f62015-07-06 18:11:54 +01001390 LocationSummary* locations = condition->GetLocations();
1391 Location left = locations->InAt(0);
1392 Location right = locations->InAt(1);
1393
Roland Levillain4fa13f62015-07-06 18:11:54 +01001394 Primitive::Type type = condition->InputAt(0)->GetType();
1395 switch (type) {
1396 case Primitive::kPrimLong:
1397 GenerateLongComparesAndJumps(condition, true_target, false_target);
1398 break;
1399 case Primitive::kPrimFloat:
1400 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1401 GenerateFPJumps(condition, true_target, false_target);
1402 break;
1403 case Primitive::kPrimDouble:
1404 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1405 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1406 GenerateFPJumps(condition, true_target, false_target);
1407 break;
1408 default:
1409 LOG(FATAL) << "Unexpected compare type " << type;
1410 }
1411
David Brazdil0debae72015-11-12 18:37:00 +00001412 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001413 __ b(false_target);
1414 }
David Brazdil0debae72015-11-12 18:37:00 +00001415
1416 if (fallthrough_target.IsLinked()) {
1417 __ Bind(&fallthrough_target);
1418 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001419}
1420
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001421void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001422 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001423 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001424 Label* false_target) {
1425 HInstruction* cond = instruction->InputAt(condition_input_index);
1426
1427 if (true_target == nullptr && false_target == nullptr) {
1428 // Nothing to do. The code always falls through.
1429 return;
1430 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001431 // Constant condition, statically compared against "true" (integer value 1).
1432 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001433 if (true_target != nullptr) {
1434 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001435 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001436 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001437 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001438 if (false_target != nullptr) {
1439 __ b(false_target);
1440 }
1441 }
1442 return;
1443 }
1444
1445 // The following code generates these patterns:
1446 // (1) true_target == nullptr && false_target != nullptr
1447 // - opposite condition true => branch to false_target
1448 // (2) true_target != nullptr && false_target == nullptr
1449 // - condition true => branch to true_target
1450 // (3) true_target != nullptr && false_target != nullptr
1451 // - condition true => branch to true_target
1452 // - branch to false_target
1453 if (IsBooleanValueOrMaterializedCondition(cond)) {
1454 // Condition has been materialized, compare the output to 0.
1455 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1456 DCHECK(cond_val.IsRegister());
1457 if (true_target == nullptr) {
1458 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1459 } else {
1460 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001461 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001462 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001463 // Condition has not been materialized. Use its inputs as the comparison and
1464 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001465 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001466
1467 // If this is a long or FP comparison that has been folded into
1468 // the HCondition, generate the comparison directly.
1469 Primitive::Type type = condition->InputAt(0)->GetType();
1470 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1471 GenerateCompareTestAndBranch(condition, true_target, false_target);
1472 return;
1473 }
1474
1475 LocationSummary* locations = cond->GetLocations();
1476 DCHECK(locations->InAt(0).IsRegister());
1477 Register left = locations->InAt(0).AsRegister<Register>();
1478 Location right = locations->InAt(1);
1479 if (right.IsRegister()) {
1480 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001481 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001482 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001483 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001484 }
1485 if (true_target == nullptr) {
1486 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1487 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001488 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001489 }
Dave Allison20dfc792014-06-16 20:44:29 -07001490 }
David Brazdil0debae72015-11-12 18:37:00 +00001491
1492 // If neither branch falls through (case 3), the conditional branch to `true_target`
1493 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1494 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001495 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001496 }
1497}
1498
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001499void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1501 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001502 locations->SetInAt(0, Location::RequiresRegister());
1503 }
1504}
1505
1506void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001507 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1508 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1509 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1510 nullptr : codegen_->GetLabelOf(true_successor);
1511 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1512 nullptr : codegen_->GetLabelOf(false_successor);
1513 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001514}
1515
1516void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1517 LocationSummary* locations = new (GetGraph()->GetArena())
1518 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001519 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001520 locations->SetInAt(0, Location::RequiresRegister());
1521 }
1522}
1523
1524void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001525 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001526 GenerateTestAndBranch(deoptimize,
1527 /* condition_input_index */ 0,
1528 slow_path->GetEntryLabel(),
1529 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001530}
Dave Allison20dfc792014-06-16 20:44:29 -07001531
David Brazdil74eb1b22015-12-14 11:44:01 +00001532void LocationsBuilderARM::VisitSelect(HSelect* select) {
1533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1534 if (Primitive::IsFloatingPointType(select->GetType())) {
1535 locations->SetInAt(0, Location::RequiresFpuRegister());
1536 locations->SetInAt(1, Location::RequiresFpuRegister());
1537 } else {
1538 locations->SetInAt(0, Location::RequiresRegister());
1539 locations->SetInAt(1, Location::RequiresRegister());
1540 }
1541 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1542 locations->SetInAt(2, Location::RequiresRegister());
1543 }
1544 locations->SetOut(Location::SameAsFirstInput());
1545}
1546
1547void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1548 LocationSummary* locations = select->GetLocations();
1549 Label false_target;
1550 GenerateTestAndBranch(select,
1551 /* condition_input_index */ 2,
1552 /* true_target */ nullptr,
1553 &false_target);
1554 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1555 __ Bind(&false_target);
1556}
1557
David Srbecky0cf44932015-12-09 14:09:59 +00001558void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1559 new (GetGraph()->GetArena()) LocationSummary(info);
1560}
1561
David Srbeckyd28f4a02016-03-14 17:14:24 +00001562void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1563 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001564}
1565
1566void CodeGeneratorARM::GenerateNop() {
1567 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001568}
1569
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001570void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001571 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001572 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001573 // Handle the long/FP comparisons made in instruction simplification.
1574 switch (cond->InputAt(0)->GetType()) {
1575 case Primitive::kPrimLong:
1576 locations->SetInAt(0, Location::RequiresRegister());
1577 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001578 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001579 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1580 }
1581 break;
1582
1583 case Primitive::kPrimFloat:
1584 case Primitive::kPrimDouble:
1585 locations->SetInAt(0, Location::RequiresFpuRegister());
1586 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001587 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1589 }
1590 break;
1591
1592 default:
1593 locations->SetInAt(0, Location::RequiresRegister());
1594 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001595 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1597 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001598 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001599}
1600
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001601void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001602 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001603 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001604 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001605
1606 LocationSummary* locations = cond->GetLocations();
1607 Location left = locations->InAt(0);
1608 Location right = locations->InAt(1);
1609 Register out = locations->Out().AsRegister<Register>();
1610 Label true_label, false_label;
1611
1612 switch (cond->InputAt(0)->GetType()) {
1613 default: {
1614 // Integer case.
1615 if (right.IsRegister()) {
1616 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1617 } else {
1618 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001619 __ CmpConstant(left.AsRegister<Register>(),
1620 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001621 }
Aart Bike9f37602015-10-09 11:15:55 -07001622 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001623 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001624 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001625 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001626 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001627 return;
1628 }
1629 case Primitive::kPrimLong:
1630 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1631 break;
1632 case Primitive::kPrimFloat:
1633 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1634 GenerateFPJumps(cond, &true_label, &false_label);
1635 break;
1636 case Primitive::kPrimDouble:
1637 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1638 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1639 GenerateFPJumps(cond, &true_label, &false_label);
1640 break;
1641 }
1642
1643 // Convert the jumps into the result.
1644 Label done_label;
1645
1646 // False case: result = 0.
1647 __ Bind(&false_label);
1648 __ LoadImmediate(out, 0);
1649 __ b(&done_label);
1650
1651 // True case: result = 1.
1652 __ Bind(&true_label);
1653 __ LoadImmediate(out, 1);
1654 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001655}
1656
1657void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001658 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001659}
1660
1661void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001662 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001663}
1664
1665void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001666 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001667}
1668
1669void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001670 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001671}
1672
1673void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001674 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001675}
1676
1677void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001678 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001679}
1680
1681void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001682 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001683}
1684
1685void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001686 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001687}
1688
1689void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001690 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001691}
1692
1693void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001694 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001695}
1696
1697void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001698 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001699}
1700
1701void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001703}
1704
Aart Bike9f37602015-10-09 11:15:55 -07001705void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001706 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001707}
1708
1709void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001710 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001711}
1712
1713void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001715}
1716
1717void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001719}
1720
1721void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001723}
1724
1725void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001727}
1728
1729void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001731}
1732
1733void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001735}
1736
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001737void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001738 LocationSummary* locations =
1739 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001740 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001741}
1742
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001743void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001744 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001745}
1746
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001747void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1748 LocationSummary* locations =
1749 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1750 locations->SetOut(Location::ConstantLocation(constant));
1751}
1752
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001753void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001754 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001755}
1756
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001757void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001758 LocationSummary* locations =
1759 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001760 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001761}
1762
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001763void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001764 // Will be generated at use site.
1765}
1766
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001767void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1768 LocationSummary* locations =
1769 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1770 locations->SetOut(Location::ConstantLocation(constant));
1771}
1772
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001773void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001774 // Will be generated at use site.
1775}
1776
1777void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1778 LocationSummary* locations =
1779 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1780 locations->SetOut(Location::ConstantLocation(constant));
1781}
1782
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001783void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001784 // Will be generated at use site.
1785}
1786
Calin Juravle27df7582015-04-17 19:12:31 +01001787void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1788 memory_barrier->SetLocations(nullptr);
1789}
1790
1791void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001792 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001793}
1794
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001795void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001796 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001797}
1798
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001799void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001800 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001801}
1802
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001803void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001804 LocationSummary* locations =
1805 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001806 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807}
1808
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001809void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001810 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001811}
1812
Calin Juravle175dc732015-08-25 15:42:32 +01001813void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1814 // The trampoline uses the same calling convention as dex calling conventions,
1815 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1816 // the method_idx.
1817 HandleInvoke(invoke);
1818}
1819
1820void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1821 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1822}
1823
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001824void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001825 // Explicit clinit checks triggered by static invokes must have been pruned by
1826 // art::PrepareForRegisterAllocation.
1827 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001828
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001829 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001830 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001831 codegen_->GetInstructionSetFeatures());
1832 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001833 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1834 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1835 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001836 return;
1837 }
1838
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001839 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001840
1841 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1842 if (invoke->HasPcRelativeDexCache()) {
1843 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1844 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001845}
1846
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001847static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1848 if (invoke->GetLocations()->Intrinsified()) {
1849 IntrinsicCodeGeneratorARM intrinsic(codegen);
1850 intrinsic.Dispatch(invoke);
1851 return true;
1852 }
1853 return false;
1854}
1855
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001856void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001857 // Explicit clinit checks triggered by static invokes must have been pruned by
1858 // art::PrepareForRegisterAllocation.
1859 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001860
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001861 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1862 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001863 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001864
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001865 LocationSummary* locations = invoke->GetLocations();
1866 codegen_->GenerateStaticOrDirectCall(
1867 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001868 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001869}
1870
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001871void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001872 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001873 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001874}
1875
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001876void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001877 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001878 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001879 codegen_->GetInstructionSetFeatures());
1880 if (intrinsic.TryDispatch(invoke)) {
1881 return;
1882 }
1883
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001884 HandleInvoke(invoke);
1885}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001886
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001887void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001888 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1889 return;
1890 }
1891
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001892 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001893 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001894 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001895}
1896
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001897void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1898 HandleInvoke(invoke);
1899 // Add the hidden argument.
1900 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1901}
1902
1903void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1904 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001905 LocationSummary* locations = invoke->GetLocations();
1906 Register temp = locations->GetTemp(0).AsRegister<Register>();
1907 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001908 Location receiver = locations->InAt(0);
1909 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1910
Roland Levillain3b359c72015-11-17 19:35:12 +00001911 // Set the hidden argument. This is safe to do this here, as R12
1912 // won't be modified thereafter, before the `blx` (call) instruction.
1913 DCHECK_EQ(R12, hidden_reg);
1914 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001915
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001916 if (receiver.IsStackSlot()) {
1917 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001918 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001919 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1920 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001921 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001923 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001924 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001925 // Instead of simply (possibly) unpoisoning `temp` here, we should
1926 // emit a read barrier for the previous class reference load.
1927 // However this is not required in practice, as this is an
1928 // intermediate/temporary reference and because the current
1929 // concurrent copying collector keeps the from-space memory
1930 // intact/accessible until the end of the marking phase (the
1931 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001932 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001933 __ LoadFromOffset(kLoadWord, temp, temp,
1934 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1935 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
1936 invoke->GetImtIndex() % ImTable::kSize, kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001937 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001938 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001939 uint32_t entry_point =
1940 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001941 // LR = temp->GetEntryPoint();
1942 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1943 // LR();
1944 __ blx(LR);
1945 DCHECK(!codegen_->IsLeafMethod());
1946 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1947}
1948
Roland Levillain88cb1752014-10-20 16:36:47 +01001949void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1950 LocationSummary* locations =
1951 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1952 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001953 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001954 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1956 break;
1957 }
1958 case Primitive::kPrimLong: {
1959 locations->SetInAt(0, Location::RequiresRegister());
1960 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001961 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001962 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001963
Roland Levillain88cb1752014-10-20 16:36:47 +01001964 case Primitive::kPrimFloat:
1965 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001966 locations->SetInAt(0, Location::RequiresFpuRegister());
1967 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001968 break;
1969
1970 default:
1971 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1972 }
1973}
1974
1975void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1976 LocationSummary* locations = neg->GetLocations();
1977 Location out = locations->Out();
1978 Location in = locations->InAt(0);
1979 switch (neg->GetResultType()) {
1980 case Primitive::kPrimInt:
1981 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001982 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001983 break;
1984
1985 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001986 DCHECK(in.IsRegisterPair());
1987 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1988 __ rsbs(out.AsRegisterPairLow<Register>(),
1989 in.AsRegisterPairLow<Register>(),
1990 ShifterOperand(0));
1991 // We cannot emit an RSC (Reverse Subtract with Carry)
1992 // instruction here, as it does not exist in the Thumb-2
1993 // instruction set. We use the following approach
1994 // using SBC and SUB instead.
1995 //
1996 // out.hi = -C
1997 __ sbc(out.AsRegisterPairHigh<Register>(),
1998 out.AsRegisterPairHigh<Register>(),
1999 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2000 // out.hi = out.hi - in.hi
2001 __ sub(out.AsRegisterPairHigh<Register>(),
2002 out.AsRegisterPairHigh<Register>(),
2003 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2004 break;
2005
Roland Levillain88cb1752014-10-20 16:36:47 +01002006 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002007 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002008 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002009 break;
2010
Roland Levillain88cb1752014-10-20 16:36:47 +01002011 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002012 DCHECK(in.IsFpuRegisterPair());
2013 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2014 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002015 break;
2016
2017 default:
2018 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2019 }
2020}
2021
Roland Levillaindff1f282014-11-05 14:15:05 +00002022void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002023 Primitive::Type result_type = conversion->GetResultType();
2024 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002025 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002026
Roland Levillain5b3ee562015-04-14 16:02:41 +01002027 // The float-to-long, double-to-long and long-to-float type conversions
2028 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002029 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002030 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2031 && result_type == Primitive::kPrimLong)
2032 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002033 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002034 : LocationSummary::kNoCall;
2035 LocationSummary* locations =
2036 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2037
David Brazdilb2bd1c52015-03-25 11:17:37 +00002038 // The Java language does not allow treating boolean as an integral type but
2039 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002040
Roland Levillaindff1f282014-11-05 14:15:05 +00002041 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002042 case Primitive::kPrimByte:
2043 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002044 case Primitive::kPrimLong:
2045 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002046 case Primitive::kPrimBoolean:
2047 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002048 case Primitive::kPrimShort:
2049 case Primitive::kPrimInt:
2050 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002051 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002052 locations->SetInAt(0, Location::RequiresRegister());
2053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected type conversion from " << input_type
2058 << " to " << result_type;
2059 }
2060 break;
2061
Roland Levillain01a8d712014-11-14 16:27:39 +00002062 case Primitive::kPrimShort:
2063 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002064 case Primitive::kPrimLong:
2065 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002066 case Primitive::kPrimBoolean:
2067 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002068 case Primitive::kPrimByte:
2069 case Primitive::kPrimInt:
2070 case Primitive::kPrimChar:
2071 // Processing a Dex `int-to-short' instruction.
2072 locations->SetInAt(0, Location::RequiresRegister());
2073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2074 break;
2075
2076 default:
2077 LOG(FATAL) << "Unexpected type conversion from " << input_type
2078 << " to " << result_type;
2079 }
2080 break;
2081
Roland Levillain946e1432014-11-11 17:35:19 +00002082 case Primitive::kPrimInt:
2083 switch (input_type) {
2084 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002085 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002086 locations->SetInAt(0, Location::Any());
2087 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2088 break;
2089
2090 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002091 // Processing a Dex `float-to-int' instruction.
2092 locations->SetInAt(0, Location::RequiresFpuRegister());
2093 locations->SetOut(Location::RequiresRegister());
2094 locations->AddTemp(Location::RequiresFpuRegister());
2095 break;
2096
Roland Levillain946e1432014-11-11 17:35:19 +00002097 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002098 // Processing a Dex `double-to-int' instruction.
2099 locations->SetInAt(0, Location::RequiresFpuRegister());
2100 locations->SetOut(Location::RequiresRegister());
2101 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002102 break;
2103
2104 default:
2105 LOG(FATAL) << "Unexpected type conversion from " << input_type
2106 << " to " << result_type;
2107 }
2108 break;
2109
Roland Levillaindff1f282014-11-05 14:15:05 +00002110 case Primitive::kPrimLong:
2111 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002112 case Primitive::kPrimBoolean:
2113 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002114 case Primitive::kPrimByte:
2115 case Primitive::kPrimShort:
2116 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002117 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002118 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002119 locations->SetInAt(0, Location::RequiresRegister());
2120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2121 break;
2122
Roland Levillain624279f2014-12-04 11:54:28 +00002123 case Primitive::kPrimFloat: {
2124 // Processing a Dex `float-to-long' instruction.
2125 InvokeRuntimeCallingConvention calling_convention;
2126 locations->SetInAt(0, Location::FpuRegisterLocation(
2127 calling_convention.GetFpuRegisterAt(0)));
2128 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2129 break;
2130 }
2131
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002132 case Primitive::kPrimDouble: {
2133 // Processing a Dex `double-to-long' instruction.
2134 InvokeRuntimeCallingConvention calling_convention;
2135 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2136 calling_convention.GetFpuRegisterAt(0),
2137 calling_convention.GetFpuRegisterAt(1)));
2138 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002139 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002140 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002141
2142 default:
2143 LOG(FATAL) << "Unexpected type conversion from " << input_type
2144 << " to " << result_type;
2145 }
2146 break;
2147
Roland Levillain981e4542014-11-14 11:47:14 +00002148 case Primitive::kPrimChar:
2149 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002150 case Primitive::kPrimLong:
2151 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002152 case Primitive::kPrimBoolean:
2153 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002154 case Primitive::kPrimByte:
2155 case Primitive::kPrimShort:
2156 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002157 // Processing a Dex `int-to-char' instruction.
2158 locations->SetInAt(0, Location::RequiresRegister());
2159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2160 break;
2161
2162 default:
2163 LOG(FATAL) << "Unexpected type conversion from " << input_type
2164 << " to " << result_type;
2165 }
2166 break;
2167
Roland Levillaindff1f282014-11-05 14:15:05 +00002168 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002169 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002170 case Primitive::kPrimBoolean:
2171 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002172 case Primitive::kPrimByte:
2173 case Primitive::kPrimShort:
2174 case Primitive::kPrimInt:
2175 case Primitive::kPrimChar:
2176 // Processing a Dex `int-to-float' instruction.
2177 locations->SetInAt(0, Location::RequiresRegister());
2178 locations->SetOut(Location::RequiresFpuRegister());
2179 break;
2180
Roland Levillain5b3ee562015-04-14 16:02:41 +01002181 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002182 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002183 InvokeRuntimeCallingConvention calling_convention;
2184 locations->SetInAt(0, Location::RegisterPairLocation(
2185 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2186 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002187 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002188 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002189
Roland Levillaincff13742014-11-17 14:32:17 +00002190 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002191 // Processing a Dex `double-to-float' instruction.
2192 locations->SetInAt(0, Location::RequiresFpuRegister());
2193 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002194 break;
2195
2196 default:
2197 LOG(FATAL) << "Unexpected type conversion from " << input_type
2198 << " to " << result_type;
2199 };
2200 break;
2201
Roland Levillaindff1f282014-11-05 14:15:05 +00002202 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002203 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002204 case Primitive::kPrimBoolean:
2205 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002206 case Primitive::kPrimByte:
2207 case Primitive::kPrimShort:
2208 case Primitive::kPrimInt:
2209 case Primitive::kPrimChar:
2210 // Processing a Dex `int-to-double' instruction.
2211 locations->SetInAt(0, Location::RequiresRegister());
2212 locations->SetOut(Location::RequiresFpuRegister());
2213 break;
2214
2215 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002216 // Processing a Dex `long-to-double' instruction.
2217 locations->SetInAt(0, Location::RequiresRegister());
2218 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002219 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002220 locations->AddTemp(Location::RequiresFpuRegister());
2221 break;
2222
Roland Levillaincff13742014-11-17 14:32:17 +00002223 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002224 // Processing a Dex `float-to-double' instruction.
2225 locations->SetInAt(0, Location::RequiresFpuRegister());
2226 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002227 break;
2228
2229 default:
2230 LOG(FATAL) << "Unexpected type conversion from " << input_type
2231 << " to " << result_type;
2232 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002233 break;
2234
2235 default:
2236 LOG(FATAL) << "Unexpected type conversion from " << input_type
2237 << " to " << result_type;
2238 }
2239}
2240
2241void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2242 LocationSummary* locations = conversion->GetLocations();
2243 Location out = locations->Out();
2244 Location in = locations->InAt(0);
2245 Primitive::Type result_type = conversion->GetResultType();
2246 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002247 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002248 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002249 case Primitive::kPrimByte:
2250 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002251 case Primitive::kPrimLong:
2252 // Type conversion from long to byte is a result of code transformations.
2253 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2254 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002255 case Primitive::kPrimBoolean:
2256 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002257 case Primitive::kPrimShort:
2258 case Primitive::kPrimInt:
2259 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002260 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002262 break;
2263
2264 default:
2265 LOG(FATAL) << "Unexpected type conversion from " << input_type
2266 << " to " << result_type;
2267 }
2268 break;
2269
Roland Levillain01a8d712014-11-14 16:27:39 +00002270 case Primitive::kPrimShort:
2271 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002272 case Primitive::kPrimLong:
2273 // Type conversion from long to short is a result of code transformations.
2274 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2275 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002276 case Primitive::kPrimBoolean:
2277 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002278 case Primitive::kPrimByte:
2279 case Primitive::kPrimInt:
2280 case Primitive::kPrimChar:
2281 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002282 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002283 break;
2284
2285 default:
2286 LOG(FATAL) << "Unexpected type conversion from " << input_type
2287 << " to " << result_type;
2288 }
2289 break;
2290
Roland Levillain946e1432014-11-11 17:35:19 +00002291 case Primitive::kPrimInt:
2292 switch (input_type) {
2293 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002294 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002295 DCHECK(out.IsRegister());
2296 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002297 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002298 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002299 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002300 } else {
2301 DCHECK(in.IsConstant());
2302 DCHECK(in.GetConstant()->IsLongConstant());
2303 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002304 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002305 }
2306 break;
2307
Roland Levillain3f8f9362014-12-02 17:45:01 +00002308 case Primitive::kPrimFloat: {
2309 // Processing a Dex `float-to-int' instruction.
2310 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002311 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002312 __ vmovrs(out.AsRegister<Register>(), temp);
2313 break;
2314 }
2315
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002316 case Primitive::kPrimDouble: {
2317 // Processing a Dex `double-to-int' instruction.
2318 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002319 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002320 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002321 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002322 }
Roland Levillain946e1432014-11-11 17:35:19 +00002323
2324 default:
2325 LOG(FATAL) << "Unexpected type conversion from " << input_type
2326 << " to " << result_type;
2327 }
2328 break;
2329
Roland Levillaindff1f282014-11-05 14:15:05 +00002330 case Primitive::kPrimLong:
2331 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002332 case Primitive::kPrimBoolean:
2333 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002334 case Primitive::kPrimByte:
2335 case Primitive::kPrimShort:
2336 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002337 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002338 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002339 DCHECK(out.IsRegisterPair());
2340 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002341 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002342 // Sign extension.
2343 __ Asr(out.AsRegisterPairHigh<Register>(),
2344 out.AsRegisterPairLow<Register>(),
2345 31);
2346 break;
2347
2348 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002349 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002350 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2351 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002352 conversion->GetDexPc(),
2353 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002354 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002355 break;
2356
Roland Levillaindff1f282014-11-05 14:15:05 +00002357 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002358 // Processing a Dex `double-to-long' instruction.
2359 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2360 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002361 conversion->GetDexPc(),
2362 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002363 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 break;
2365
2366 default:
2367 LOG(FATAL) << "Unexpected type conversion from " << input_type
2368 << " to " << result_type;
2369 }
2370 break;
2371
Roland Levillain981e4542014-11-14 11:47:14 +00002372 case Primitive::kPrimChar:
2373 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002374 case Primitive::kPrimLong:
2375 // Type conversion from long to char is a result of code transformations.
2376 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2377 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002378 case Primitive::kPrimBoolean:
2379 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002380 case Primitive::kPrimByte:
2381 case Primitive::kPrimShort:
2382 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002383 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002384 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002385 break;
2386
2387 default:
2388 LOG(FATAL) << "Unexpected type conversion from " << input_type
2389 << " to " << result_type;
2390 }
2391 break;
2392
Roland Levillaindff1f282014-11-05 14:15:05 +00002393 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002394 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002395 case Primitive::kPrimBoolean:
2396 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002397 case Primitive::kPrimByte:
2398 case Primitive::kPrimShort:
2399 case Primitive::kPrimInt:
2400 case Primitive::kPrimChar: {
2401 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002402 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2403 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002404 break;
2405 }
2406
Roland Levillain5b3ee562015-04-14 16:02:41 +01002407 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002408 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002409 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2410 conversion,
2411 conversion->GetDexPc(),
2412 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002413 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002414 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002415
Roland Levillaincff13742014-11-17 14:32:17 +00002416 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002417 // Processing a Dex `double-to-float' instruction.
2418 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2419 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 };
2426 break;
2427
Roland Levillaindff1f282014-11-05 14:15:05 +00002428 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002429 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002430 case Primitive::kPrimBoolean:
2431 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002432 case Primitive::kPrimByte:
2433 case Primitive::kPrimShort:
2434 case Primitive::kPrimInt:
2435 case Primitive::kPrimChar: {
2436 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002438 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2439 out.AsFpuRegisterPairLow<SRegister>());
2440 break;
2441 }
2442
Roland Levillain647b9ed2014-11-27 12:06:00 +00002443 case Primitive::kPrimLong: {
2444 // Processing a Dex `long-to-double' instruction.
2445 Register low = in.AsRegisterPairLow<Register>();
2446 Register high = in.AsRegisterPairHigh<Register>();
2447 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2448 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002449 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002450 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002451 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2452 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002453
Roland Levillain682393c2015-04-14 15:57:52 +01002454 // temp_d = int-to-double(high)
2455 __ vmovsr(temp_s, high);
2456 __ vcvtdi(temp_d, temp_s);
2457 // constant_d = k2Pow32EncodingForDouble
2458 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2459 // out_d = unsigned-to-double(low)
2460 __ vmovsr(out_s, low);
2461 __ vcvtdu(out_d, out_s);
2462 // out_d += temp_d * constant_d
2463 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002464 break;
2465 }
2466
Roland Levillaincff13742014-11-17 14:32:17 +00002467 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002468 // Processing a Dex `float-to-double' instruction.
2469 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2470 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002471 break;
2472
2473 default:
2474 LOG(FATAL) << "Unexpected type conversion from " << input_type
2475 << " to " << result_type;
2476 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002477 break;
2478
2479 default:
2480 LOG(FATAL) << "Unexpected type conversion from " << input_type
2481 << " to " << result_type;
2482 }
2483}
2484
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002485void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002486 LocationSummary* locations =
2487 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002488 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002489 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002490 locations->SetInAt(0, Location::RequiresRegister());
2491 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2493 break;
2494 }
2495
2496 case Primitive::kPrimLong: {
2497 locations->SetInAt(0, Location::RequiresRegister());
2498 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002500 break;
2501 }
2502
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002503 case Primitive::kPrimFloat:
2504 case Primitive::kPrimDouble: {
2505 locations->SetInAt(0, Location::RequiresFpuRegister());
2506 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002507 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002508 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002509 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002510
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002511 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002512 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002514}
2515
2516void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2517 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002518 Location out = locations->Out();
2519 Location first = locations->InAt(0);
2520 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002521 switch (add->GetResultType()) {
2522 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002523 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002524 __ add(out.AsRegister<Register>(),
2525 first.AsRegister<Register>(),
2526 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002527 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002528 __ AddConstant(out.AsRegister<Register>(),
2529 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002531 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002532 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002533
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002534 case Primitive::kPrimLong: {
2535 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002536 __ adds(out.AsRegisterPairLow<Register>(),
2537 first.AsRegisterPairLow<Register>(),
2538 ShifterOperand(second.AsRegisterPairLow<Register>()));
2539 __ adc(out.AsRegisterPairHigh<Register>(),
2540 first.AsRegisterPairHigh<Register>(),
2541 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002542 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002543 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002544
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002545 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002546 __ vadds(out.AsFpuRegister<SRegister>(),
2547 first.AsFpuRegister<SRegister>(),
2548 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002549 break;
2550
2551 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002552 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2553 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2554 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002555 break;
2556
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002557 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002558 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002559 }
2560}
2561
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002562void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002565 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002566 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002567 locations->SetInAt(0, Location::RequiresRegister());
2568 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2570 break;
2571 }
2572
2573 case Primitive::kPrimLong: {
2574 locations->SetInAt(0, Location::RequiresRegister());
2575 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002577 break;
2578 }
Calin Juravle11351682014-10-23 15:38:15 +01002579 case Primitive::kPrimFloat:
2580 case Primitive::kPrimDouble: {
2581 locations->SetInAt(0, Location::RequiresFpuRegister());
2582 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002583 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002584 break;
Calin Juravle11351682014-10-23 15:38:15 +01002585 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002586 default:
Calin Juravle11351682014-10-23 15:38:15 +01002587 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002588 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002589}
2590
2591void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2592 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002593 Location out = locations->Out();
2594 Location first = locations->InAt(0);
2595 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002596 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002597 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002598 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002599 __ sub(out.AsRegister<Register>(),
2600 first.AsRegister<Register>(),
2601 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002602 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ AddConstant(out.AsRegister<Register>(),
2604 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002605 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002606 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002607 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002608 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002609
Calin Juravle11351682014-10-23 15:38:15 +01002610 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002611 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002612 __ subs(out.AsRegisterPairLow<Register>(),
2613 first.AsRegisterPairLow<Register>(),
2614 ShifterOperand(second.AsRegisterPairLow<Register>()));
2615 __ sbc(out.AsRegisterPairHigh<Register>(),
2616 first.AsRegisterPairHigh<Register>(),
2617 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002618 break;
Calin Juravle11351682014-10-23 15:38:15 +01002619 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002620
Calin Juravle11351682014-10-23 15:38:15 +01002621 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002622 __ vsubs(out.AsFpuRegister<SRegister>(),
2623 first.AsFpuRegister<SRegister>(),
2624 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002625 break;
Calin Juravle11351682014-10-23 15:38:15 +01002626 }
2627
2628 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002629 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2630 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2631 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002632 break;
2633 }
2634
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002635
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002636 default:
Calin Juravle11351682014-10-23 15:38:15 +01002637 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002638 }
2639}
2640
Calin Juravle34bacdf2014-10-07 20:23:36 +01002641void LocationsBuilderARM::VisitMul(HMul* mul) {
2642 LocationSummary* locations =
2643 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2644 switch (mul->GetResultType()) {
2645 case Primitive::kPrimInt:
2646 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetInAt(1, Location::RequiresRegister());
2649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002650 break;
2651 }
2652
Calin Juravleb5bfa962014-10-21 18:02:24 +01002653 case Primitive::kPrimFloat:
2654 case Primitive::kPrimDouble: {
2655 locations->SetInAt(0, Location::RequiresFpuRegister());
2656 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002657 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002658 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002659 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002660
2661 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002662 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002663 }
2664}
2665
2666void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2667 LocationSummary* locations = mul->GetLocations();
2668 Location out = locations->Out();
2669 Location first = locations->InAt(0);
2670 Location second = locations->InAt(1);
2671 switch (mul->GetResultType()) {
2672 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002673 __ mul(out.AsRegister<Register>(),
2674 first.AsRegister<Register>(),
2675 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002676 break;
2677 }
2678 case Primitive::kPrimLong: {
2679 Register out_hi = out.AsRegisterPairHigh<Register>();
2680 Register out_lo = out.AsRegisterPairLow<Register>();
2681 Register in1_hi = first.AsRegisterPairHigh<Register>();
2682 Register in1_lo = first.AsRegisterPairLow<Register>();
2683 Register in2_hi = second.AsRegisterPairHigh<Register>();
2684 Register in2_lo = second.AsRegisterPairLow<Register>();
2685
2686 // Extra checks to protect caused by the existence of R1_R2.
2687 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2688 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2689 DCHECK_NE(out_hi, in1_lo);
2690 DCHECK_NE(out_hi, in2_lo);
2691
2692 // input: in1 - 64 bits, in2 - 64 bits
2693 // output: out
2694 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2695 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2696 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2697
2698 // IP <- in1.lo * in2.hi
2699 __ mul(IP, in1_lo, in2_hi);
2700 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2701 __ mla(out_hi, in1_hi, in2_lo, IP);
2702 // out.lo <- (in1.lo * in2.lo)[31:0];
2703 __ umull(out_lo, IP, in1_lo, in2_lo);
2704 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2705 __ add(out_hi, out_hi, ShifterOperand(IP));
2706 break;
2707 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002708
2709 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002710 __ vmuls(out.AsFpuRegister<SRegister>(),
2711 first.AsFpuRegister<SRegister>(),
2712 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002713 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002714 }
2715
2716 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002717 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2718 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2719 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002720 break;
2721 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002722
2723 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002724 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002725 }
2726}
2727
Zheng Xuc6667102015-05-15 16:08:45 +08002728void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2729 DCHECK(instruction->IsDiv() || instruction->IsRem());
2730 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2731
2732 LocationSummary* locations = instruction->GetLocations();
2733 Location second = locations->InAt(1);
2734 DCHECK(second.IsConstant());
2735
2736 Register out = locations->Out().AsRegister<Register>();
2737 Register dividend = locations->InAt(0).AsRegister<Register>();
2738 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2739 DCHECK(imm == 1 || imm == -1);
2740
2741 if (instruction->IsRem()) {
2742 __ LoadImmediate(out, 0);
2743 } else {
2744 if (imm == 1) {
2745 __ Mov(out, dividend);
2746 } else {
2747 __ rsb(out, dividend, ShifterOperand(0));
2748 }
2749 }
2750}
2751
2752void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2753 DCHECK(instruction->IsDiv() || instruction->IsRem());
2754 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2755
2756 LocationSummary* locations = instruction->GetLocations();
2757 Location second = locations->InAt(1);
2758 DCHECK(second.IsConstant());
2759
2760 Register out = locations->Out().AsRegister<Register>();
2761 Register dividend = locations->InAt(0).AsRegister<Register>();
2762 Register temp = locations->GetTemp(0).AsRegister<Register>();
2763 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002764 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002765 int ctz_imm = CTZ(abs_imm);
2766
2767 if (ctz_imm == 1) {
2768 __ Lsr(temp, dividend, 32 - ctz_imm);
2769 } else {
2770 __ Asr(temp, dividend, 31);
2771 __ Lsr(temp, temp, 32 - ctz_imm);
2772 }
2773 __ add(out, temp, ShifterOperand(dividend));
2774
2775 if (instruction->IsDiv()) {
2776 __ Asr(out, out, ctz_imm);
2777 if (imm < 0) {
2778 __ rsb(out, out, ShifterOperand(0));
2779 }
2780 } else {
2781 __ ubfx(out, out, 0, ctz_imm);
2782 __ sub(out, out, ShifterOperand(temp));
2783 }
2784}
2785
2786void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2787 DCHECK(instruction->IsDiv() || instruction->IsRem());
2788 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2789
2790 LocationSummary* locations = instruction->GetLocations();
2791 Location second = locations->InAt(1);
2792 DCHECK(second.IsConstant());
2793
2794 Register out = locations->Out().AsRegister<Register>();
2795 Register dividend = locations->InAt(0).AsRegister<Register>();
2796 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2797 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2798 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2799
2800 int64_t magic;
2801 int shift;
2802 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2803
2804 __ LoadImmediate(temp1, magic);
2805 __ smull(temp2, temp1, dividend, temp1);
2806
2807 if (imm > 0 && magic < 0) {
2808 __ add(temp1, temp1, ShifterOperand(dividend));
2809 } else if (imm < 0 && magic > 0) {
2810 __ sub(temp1, temp1, ShifterOperand(dividend));
2811 }
2812
2813 if (shift != 0) {
2814 __ Asr(temp1, temp1, shift);
2815 }
2816
2817 if (instruction->IsDiv()) {
2818 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2819 } else {
2820 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2821 // TODO: Strength reduction for mls.
2822 __ LoadImmediate(temp2, imm);
2823 __ mls(out, temp1, temp2, dividend);
2824 }
2825}
2826
2827void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2828 DCHECK(instruction->IsDiv() || instruction->IsRem());
2829 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2830
2831 LocationSummary* locations = instruction->GetLocations();
2832 Location second = locations->InAt(1);
2833 DCHECK(second.IsConstant());
2834
2835 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2836 if (imm == 0) {
2837 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2838 } else if (imm == 1 || imm == -1) {
2839 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002840 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002841 DivRemByPowerOfTwo(instruction);
2842 } else {
2843 DCHECK(imm <= -2 || imm >= 2);
2844 GenerateDivRemWithAnyConstant(instruction);
2845 }
2846}
2847
Calin Juravle7c4954d2014-10-28 16:57:40 +00002848void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002849 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2850 if (div->GetResultType() == Primitive::kPrimLong) {
2851 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002852 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002853 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2854 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002855 } else if (div->GetResultType() == Primitive::kPrimInt &&
2856 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2857 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002858 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002859 }
2860
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2862
Calin Juravle7c4954d2014-10-28 16:57:40 +00002863 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002864 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002865 if (div->InputAt(1)->IsConstant()) {
2866 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002867 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002869 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2870 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002871 // No temp register required.
2872 } else {
2873 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002874 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002875 locations->AddTemp(Location::RequiresRegister());
2876 }
2877 }
2878 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002879 locations->SetInAt(0, Location::RequiresRegister());
2880 locations->SetInAt(1, Location::RequiresRegister());
2881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2882 } else {
2883 InvokeRuntimeCallingConvention calling_convention;
2884 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2885 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2886 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2887 // we only need the former.
2888 locations->SetOut(Location::RegisterLocation(R0));
2889 }
Calin Juravled0d48522014-11-04 16:40:20 +00002890 break;
2891 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002892 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002893 InvokeRuntimeCallingConvention calling_convention;
2894 locations->SetInAt(0, Location::RegisterPairLocation(
2895 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2896 locations->SetInAt(1, Location::RegisterPairLocation(
2897 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002898 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002899 break;
2900 }
2901 case Primitive::kPrimFloat:
2902 case Primitive::kPrimDouble: {
2903 locations->SetInAt(0, Location::RequiresFpuRegister());
2904 locations->SetInAt(1, Location::RequiresFpuRegister());
2905 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2906 break;
2907 }
2908
2909 default:
2910 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2911 }
2912}
2913
2914void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2915 LocationSummary* locations = div->GetLocations();
2916 Location out = locations->Out();
2917 Location first = locations->InAt(0);
2918 Location second = locations->InAt(1);
2919
2920 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002921 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002922 if (second.IsConstant()) {
2923 GenerateDivRemConstantIntegral(div);
2924 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002925 __ sdiv(out.AsRegister<Register>(),
2926 first.AsRegister<Register>(),
2927 second.AsRegister<Register>());
2928 } else {
2929 InvokeRuntimeCallingConvention calling_convention;
2930 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2931 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2932 DCHECK_EQ(R0, out.AsRegister<Register>());
2933
2934 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002935 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002936 }
Calin Juravled0d48522014-11-04 16:40:20 +00002937 break;
2938 }
2939
Calin Juravle7c4954d2014-10-28 16:57:40 +00002940 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002941 InvokeRuntimeCallingConvention calling_convention;
2942 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2943 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2944 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2945 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2946 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002947 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002948
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002949 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002950 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002951 break;
2952 }
2953
2954 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002955 __ vdivs(out.AsFpuRegister<SRegister>(),
2956 first.AsFpuRegister<SRegister>(),
2957 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002958 break;
2959 }
2960
2961 case Primitive::kPrimDouble: {
2962 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2963 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2964 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2965 break;
2966 }
2967
2968 default:
2969 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2970 }
2971}
2972
Calin Juravlebacfec32014-11-14 15:54:36 +00002973void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002974 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002975
2976 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002977 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002978 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2979 // sdiv will be replaced by other instruction sequence.
2980 call_kind = LocationSummary::kNoCall;
2981 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2982 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002983 // Have hardware divide instruction for int, do it with three instructions.
2984 call_kind = LocationSummary::kNoCall;
2985 }
2986
Calin Juravlebacfec32014-11-14 15:54:36 +00002987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2988
Calin Juravled2ec87d2014-12-08 14:24:46 +00002989 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002990 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002991 if (rem->InputAt(1)->IsConstant()) {
2992 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002993 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002995 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
2996 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002997 // No temp register required.
2998 } else {
2999 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003000 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003001 locations->AddTemp(Location::RequiresRegister());
3002 }
3003 }
3004 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003005 locations->SetInAt(0, Location::RequiresRegister());
3006 locations->SetInAt(1, Location::RequiresRegister());
3007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3008 locations->AddTemp(Location::RequiresRegister());
3009 } else {
3010 InvokeRuntimeCallingConvention calling_convention;
3011 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3012 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3013 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3014 // we only need the latter.
3015 locations->SetOut(Location::RegisterLocation(R1));
3016 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003017 break;
3018 }
3019 case Primitive::kPrimLong: {
3020 InvokeRuntimeCallingConvention calling_convention;
3021 locations->SetInAt(0, Location::RegisterPairLocation(
3022 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3023 locations->SetInAt(1, Location::RegisterPairLocation(
3024 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3025 // The runtime helper puts the output in R2,R3.
3026 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3027 break;
3028 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003029 case Primitive::kPrimFloat: {
3030 InvokeRuntimeCallingConvention calling_convention;
3031 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3032 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3033 locations->SetOut(Location::FpuRegisterLocation(S0));
3034 break;
3035 }
3036
Calin Juravlebacfec32014-11-14 15:54:36 +00003037 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003038 InvokeRuntimeCallingConvention calling_convention;
3039 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3040 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3041 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3042 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3043 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003044 break;
3045 }
3046
3047 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003048 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003049 }
3050}
3051
3052void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3053 LocationSummary* locations = rem->GetLocations();
3054 Location out = locations->Out();
3055 Location first = locations->InAt(0);
3056 Location second = locations->InAt(1);
3057
Calin Juravled2ec87d2014-12-08 14:24:46 +00003058 Primitive::Type type = rem->GetResultType();
3059 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003060 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003061 if (second.IsConstant()) {
3062 GenerateDivRemConstantIntegral(rem);
3063 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003064 Register reg1 = first.AsRegister<Register>();
3065 Register reg2 = second.AsRegister<Register>();
3066 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003067
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003068 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003069 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003070 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003071 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003072 } else {
3073 InvokeRuntimeCallingConvention calling_convention;
3074 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3075 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3076 DCHECK_EQ(R1, out.AsRegister<Register>());
3077
3078 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003079 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003080 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003081 break;
3082 }
3083
3084 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003085 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003086 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003087 break;
3088 }
3089
Calin Juravled2ec87d2014-12-08 14:24:46 +00003090 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003091 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003092 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003093 break;
3094 }
3095
Calin Juravlebacfec32014-11-14 15:54:36 +00003096 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003097 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003098 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003099 break;
3100 }
3101
3102 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003103 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003104 }
3105}
3106
Calin Juravled0d48522014-11-04 16:40:20 +00003107void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003108 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3109 ? LocationSummary::kCallOnSlowPath
3110 : LocationSummary::kNoCall;
3111 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003112 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003113 if (instruction->HasUses()) {
3114 locations->SetOut(Location::SameAsFirstInput());
3115 }
3116}
3117
3118void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003119 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003120 codegen_->AddSlowPath(slow_path);
3121
3122 LocationSummary* locations = instruction->GetLocations();
3123 Location value = locations->InAt(0);
3124
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003125 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003126 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003127 case Primitive::kPrimByte:
3128 case Primitive::kPrimChar:
3129 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003130 case Primitive::kPrimInt: {
3131 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003132 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003133 } else {
3134 DCHECK(value.IsConstant()) << value;
3135 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3136 __ b(slow_path->GetEntryLabel());
3137 }
3138 }
3139 break;
3140 }
3141 case Primitive::kPrimLong: {
3142 if (value.IsRegisterPair()) {
3143 __ orrs(IP,
3144 value.AsRegisterPairLow<Register>(),
3145 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3146 __ b(slow_path->GetEntryLabel(), EQ);
3147 } else {
3148 DCHECK(value.IsConstant()) << value;
3149 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3150 __ b(slow_path->GetEntryLabel());
3151 }
3152 }
3153 break;
3154 default:
3155 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3156 }
3157 }
Calin Juravled0d48522014-11-04 16:40:20 +00003158}
3159
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003160void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3161 Register in = locations->InAt(0).AsRegister<Register>();
3162 Location rhs = locations->InAt(1);
3163 Register out = locations->Out().AsRegister<Register>();
3164
3165 if (rhs.IsConstant()) {
3166 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3167 // so map all rotations to a +ve. equivalent in that range.
3168 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3169 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3170 if (rot) {
3171 // Rotate, mapping left rotations to right equivalents if necessary.
3172 // (e.g. left by 2 bits == right by 30.)
3173 __ Ror(out, in, rot);
3174 } else if (out != in) {
3175 __ Mov(out, in);
3176 }
3177 } else {
3178 __ Ror(out, in, rhs.AsRegister<Register>());
3179 }
3180}
3181
3182// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3183// rotates by swapping input regs (effectively rotating by the first 32-bits of
3184// a larger rotation) or flipping direction (thus treating larger right/left
3185// rotations as sub-word sized rotations in the other direction) as appropriate.
3186void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3187 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3188 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3189 Location rhs = locations->InAt(1);
3190 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3191 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3192
3193 if (rhs.IsConstant()) {
3194 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3195 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003196 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003197 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3198 // logic below to a simple pair of binary orr.
3199 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3200 if (rot >= kArmBitsPerWord) {
3201 rot -= kArmBitsPerWord;
3202 std::swap(in_reg_hi, in_reg_lo);
3203 }
3204 // Rotate, or mov to out for zero or word size rotations.
3205 if (rot != 0u) {
3206 __ Lsr(out_reg_hi, in_reg_hi, rot);
3207 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3208 __ Lsr(out_reg_lo, in_reg_lo, rot);
3209 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3210 } else {
3211 __ Mov(out_reg_lo, in_reg_lo);
3212 __ Mov(out_reg_hi, in_reg_hi);
3213 }
3214 } else {
3215 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3216 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3217 Label end;
3218 Label shift_by_32_plus_shift_right;
3219
3220 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3221 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3222 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3223 __ b(&shift_by_32_plus_shift_right, CC);
3224
3225 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3226 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3227 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3228 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3229 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3230 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3231 __ Lsr(shift_left, in_reg_hi, shift_right);
3232 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3233 __ b(&end);
3234
3235 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3236 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3237 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3238 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3239 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3240 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3241 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3242 __ Lsl(shift_right, in_reg_hi, shift_left);
3243 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3244
3245 __ Bind(&end);
3246 }
3247}
Roland Levillain22c49222016-03-18 14:04:28 +00003248
3249void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003250 LocationSummary* locations =
3251 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3252 switch (ror->GetResultType()) {
3253 case Primitive::kPrimInt: {
3254 locations->SetInAt(0, Location::RequiresRegister());
3255 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3256 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3257 break;
3258 }
3259 case Primitive::kPrimLong: {
3260 locations->SetInAt(0, Location::RequiresRegister());
3261 if (ror->InputAt(1)->IsConstant()) {
3262 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3263 } else {
3264 locations->SetInAt(1, Location::RequiresRegister());
3265 locations->AddTemp(Location::RequiresRegister());
3266 locations->AddTemp(Location::RequiresRegister());
3267 }
3268 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3269 break;
3270 }
3271 default:
3272 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3273 }
3274}
3275
Roland Levillain22c49222016-03-18 14:04:28 +00003276void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003277 LocationSummary* locations = ror->GetLocations();
3278 Primitive::Type type = ror->GetResultType();
3279 switch (type) {
3280 case Primitive::kPrimInt: {
3281 HandleIntegerRotate(locations);
3282 break;
3283 }
3284 case Primitive::kPrimLong: {
3285 HandleLongRotate(locations);
3286 break;
3287 }
3288 default:
3289 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003290 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003291 }
3292}
3293
Calin Juravle9aec02f2014-11-18 23:06:35 +00003294void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3295 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3296
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003297 LocationSummary* locations =
3298 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003299
3300 switch (op->GetResultType()) {
3301 case Primitive::kPrimInt: {
3302 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003303 if (op->InputAt(1)->IsConstant()) {
3304 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3306 } else {
3307 locations->SetInAt(1, Location::RequiresRegister());
3308 // Make the output overlap, as it will be used to hold the masked
3309 // second input.
3310 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3311 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003312 break;
3313 }
3314 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003315 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003316 if (op->InputAt(1)->IsConstant()) {
3317 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3318 // For simplicity, use kOutputOverlap even though we only require that low registers
3319 // don't clash with high registers which the register allocator currently guarantees.
3320 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3321 } else {
3322 locations->SetInAt(1, Location::RequiresRegister());
3323 locations->AddTemp(Location::RequiresRegister());
3324 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3325 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003326 break;
3327 }
3328 default:
3329 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3330 }
3331}
3332
3333void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3334 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3335
3336 LocationSummary* locations = op->GetLocations();
3337 Location out = locations->Out();
3338 Location first = locations->InAt(0);
3339 Location second = locations->InAt(1);
3340
3341 Primitive::Type type = op->GetResultType();
3342 switch (type) {
3343 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 Register out_reg = out.AsRegister<Register>();
3345 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003346 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003348 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003349 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003350 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003351 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003352 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003353 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003354 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003355 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003356 }
3357 } else {
3358 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003359 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003360 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003361 __ Mov(out_reg, first_reg);
3362 } else if (op->IsShl()) {
3363 __ Lsl(out_reg, first_reg, shift_value);
3364 } else if (op->IsShr()) {
3365 __ Asr(out_reg, first_reg, shift_value);
3366 } else {
3367 __ Lsr(out_reg, first_reg, shift_value);
3368 }
3369 }
3370 break;
3371 }
3372 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003373 Register o_h = out.AsRegisterPairHigh<Register>();
3374 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003375
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003376 Register high = first.AsRegisterPairHigh<Register>();
3377 Register low = first.AsRegisterPairLow<Register>();
3378
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003379 if (second.IsRegister()) {
3380 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003381
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003382 Register second_reg = second.AsRegister<Register>();
3383
3384 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003385 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003386 // Shift the high part
3387 __ Lsl(o_h, high, o_l);
3388 // Shift the low part and `or` what overflew on the high part
3389 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3390 __ Lsr(temp, low, temp);
3391 __ orr(o_h, o_h, ShifterOperand(temp));
3392 // If the shift is > 32 bits, override the high part
3393 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3394 __ it(PL);
3395 __ Lsl(o_h, low, temp, PL);
3396 // Shift the low part
3397 __ Lsl(o_l, low, o_l);
3398 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003399 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003400 // Shift the low part
3401 __ Lsr(o_l, low, o_h);
3402 // Shift the high part and `or` what underflew on the low part
3403 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3404 __ Lsl(temp, high, temp);
3405 __ orr(o_l, o_l, ShifterOperand(temp));
3406 // If the shift is > 32 bits, override the low part
3407 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3408 __ it(PL);
3409 __ Asr(o_l, high, temp, PL);
3410 // Shift the high part
3411 __ Asr(o_h, high, o_h);
3412 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003413 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003414 // same as Shr except we use `Lsr`s and not `Asr`s
3415 __ Lsr(o_l, low, o_h);
3416 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3417 __ Lsl(temp, high, temp);
3418 __ orr(o_l, o_l, ShifterOperand(temp));
3419 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3420 __ it(PL);
3421 __ Lsr(o_l, high, temp, PL);
3422 __ Lsr(o_h, high, o_h);
3423 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003424 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003425 // Register allocator doesn't create partial overlap.
3426 DCHECK_NE(o_l, high);
3427 DCHECK_NE(o_h, low);
3428 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003429 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003430 if (shift_value > 32) {
3431 if (op->IsShl()) {
3432 __ Lsl(o_h, low, shift_value - 32);
3433 __ LoadImmediate(o_l, 0);
3434 } else if (op->IsShr()) {
3435 __ Asr(o_l, high, shift_value - 32);
3436 __ Asr(o_h, high, 31);
3437 } else {
3438 __ Lsr(o_l, high, shift_value - 32);
3439 __ LoadImmediate(o_h, 0);
3440 }
3441 } else if (shift_value == 32) {
3442 if (op->IsShl()) {
3443 __ mov(o_h, ShifterOperand(low));
3444 __ LoadImmediate(o_l, 0);
3445 } else if (op->IsShr()) {
3446 __ mov(o_l, ShifterOperand(high));
3447 __ Asr(o_h, high, 31);
3448 } else {
3449 __ mov(o_l, ShifterOperand(high));
3450 __ LoadImmediate(o_h, 0);
3451 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003452 } else if (shift_value == 1) {
3453 if (op->IsShl()) {
3454 __ Lsls(o_l, low, 1);
3455 __ adc(o_h, high, ShifterOperand(high));
3456 } else if (op->IsShr()) {
3457 __ Asrs(o_h, high, 1);
3458 __ Rrx(o_l, low);
3459 } else {
3460 __ Lsrs(o_h, high, 1);
3461 __ Rrx(o_l, low);
3462 }
3463 } else {
3464 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003465 if (op->IsShl()) {
3466 __ Lsl(o_h, high, shift_value);
3467 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3468 __ Lsl(o_l, low, shift_value);
3469 } else if (op->IsShr()) {
3470 __ Lsr(o_l, low, shift_value);
3471 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3472 __ Asr(o_h, high, shift_value);
3473 } else {
3474 __ Lsr(o_l, low, shift_value);
3475 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3476 __ Lsr(o_h, high, shift_value);
3477 }
3478 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003479 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003480 break;
3481 }
3482 default:
3483 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003484 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003485 }
3486}
3487
3488void LocationsBuilderARM::VisitShl(HShl* shl) {
3489 HandleShift(shl);
3490}
3491
3492void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3493 HandleShift(shl);
3494}
3495
3496void LocationsBuilderARM::VisitShr(HShr* shr) {
3497 HandleShift(shr);
3498}
3499
3500void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3501 HandleShift(shr);
3502}
3503
3504void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3505 HandleShift(ushr);
3506}
3507
3508void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3509 HandleShift(ushr);
3510}
3511
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003512void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003513 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003514 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003515 if (instruction->IsStringAlloc()) {
3516 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3517 } else {
3518 InvokeRuntimeCallingConvention calling_convention;
3519 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3520 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3521 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003522 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003523}
3524
3525void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003526 // Note: if heap poisoning is enabled, the entry point takes cares
3527 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003528 if (instruction->IsStringAlloc()) {
3529 // String is allocated through StringFactory. Call NewEmptyString entry point.
3530 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3531 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3532 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3533 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3534 __ blx(LR);
3535 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3536 } else {
3537 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3538 instruction,
3539 instruction->GetDexPc(),
3540 nullptr);
3541 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3542 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003543}
3544
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003545void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3546 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003548 InvokeRuntimeCallingConvention calling_convention;
3549 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003550 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003551 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003552 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003553}
3554
3555void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3556 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003557 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003558 // Note: if heap poisoning is enabled, the entry point takes cares
3559 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003560 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003561 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003562 instruction->GetDexPc(),
3563 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003564 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003565}
3566
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003567void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003570 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3571 if (location.IsStackSlot()) {
3572 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3573 } else if (location.IsDoubleStackSlot()) {
3574 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003575 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003576 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003577}
3578
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003579void InstructionCodeGeneratorARM::VisitParameterValue(
3580 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003581 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003582}
3583
3584void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3585 LocationSummary* locations =
3586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3587 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3588}
3589
3590void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3591 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003592}
3593
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003594void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003595 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003596 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003597 locations->SetInAt(0, Location::RequiresRegister());
3598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003599}
3600
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003601void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3602 LocationSummary* locations = not_->GetLocations();
3603 Location out = locations->Out();
3604 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003605 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003606 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003608 break;
3609
3610 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003611 __ mvn(out.AsRegisterPairLow<Register>(),
3612 ShifterOperand(in.AsRegisterPairLow<Register>()));
3613 __ mvn(out.AsRegisterPairHigh<Register>(),
3614 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003615 break;
3616
3617 default:
3618 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3619 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003620}
3621
David Brazdil66d126e2015-04-03 16:02:44 +01003622void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3623 LocationSummary* locations =
3624 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3625 locations->SetInAt(0, Location::RequiresRegister());
3626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3627}
3628
3629void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003630 LocationSummary* locations = bool_not->GetLocations();
3631 Location out = locations->Out();
3632 Location in = locations->InAt(0);
3633 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3634}
3635
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003636void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003637 LocationSummary* locations =
3638 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003639 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003640 case Primitive::kPrimBoolean:
3641 case Primitive::kPrimByte:
3642 case Primitive::kPrimShort:
3643 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003644 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003645 case Primitive::kPrimLong: {
3646 locations->SetInAt(0, Location::RequiresRegister());
3647 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003648 // Output overlaps because it is written before doing the low comparison.
3649 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003650 break;
3651 }
3652 case Primitive::kPrimFloat:
3653 case Primitive::kPrimDouble: {
3654 locations->SetInAt(0, Location::RequiresFpuRegister());
3655 locations->SetInAt(1, Location::RequiresFpuRegister());
3656 locations->SetOut(Location::RequiresRegister());
3657 break;
3658 }
3659 default:
3660 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3661 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003662}
3663
3664void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003665 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003666 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003667 Location left = locations->InAt(0);
3668 Location right = locations->InAt(1);
3669
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003670 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003671 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003672 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003673 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003674 case Primitive::kPrimBoolean:
3675 case Primitive::kPrimByte:
3676 case Primitive::kPrimShort:
3677 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003678 case Primitive::kPrimInt: {
3679 __ LoadImmediate(out, 0);
3680 __ cmp(left.AsRegister<Register>(),
3681 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3682 less_cond = LT;
3683 break;
3684 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003685 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003686 __ cmp(left.AsRegisterPairHigh<Register>(),
3687 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003688 __ b(&less, LT);
3689 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003690 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003691 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003692 __ cmp(left.AsRegisterPairLow<Register>(),
3693 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003694 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003695 break;
3696 }
3697 case Primitive::kPrimFloat:
3698 case Primitive::kPrimDouble: {
3699 __ LoadImmediate(out, 0);
3700 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003701 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003702 } else {
3703 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3704 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3705 }
3706 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003707 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003708 break;
3709 }
3710 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003711 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003712 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003713 }
Aart Bika19616e2016-02-01 18:57:58 -08003714
Calin Juravleddb7df22014-11-25 20:56:51 +00003715 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003716 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003717
3718 __ Bind(&greater);
3719 __ LoadImmediate(out, 1);
3720 __ b(&done);
3721
3722 __ Bind(&less);
3723 __ LoadImmediate(out, -1);
3724
3725 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003726}
3727
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003728void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003729 LocationSummary* locations =
3730 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003731 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003732 locations->SetInAt(i, Location::Any());
3733 }
3734 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003735}
3736
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003737void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003738 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003739}
3740
Roland Levillainc9285912015-12-18 10:38:42 +00003741void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3742 // TODO (ported from quick): revisit ARM barrier kinds.
3743 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003744 switch (kind) {
3745 case MemBarrierKind::kAnyStore:
3746 case MemBarrierKind::kLoadAny:
3747 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003748 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003749 break;
3750 }
3751 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003752 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003753 break;
3754 }
3755 default:
3756 LOG(FATAL) << "Unexpected memory barrier " << kind;
3757 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003758 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003759}
3760
3761void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3762 uint32_t offset,
3763 Register out_lo,
3764 Register out_hi) {
3765 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003766 // Ensure `out_lo` is different from `addr`, so that loading
3767 // `offset` into `out_lo` does not clutter `addr`.
3768 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003769 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003770 __ add(IP, addr, ShifterOperand(out_lo));
3771 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003772 }
3773 __ ldrexd(out_lo, out_hi, addr);
3774}
3775
3776void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3777 uint32_t offset,
3778 Register value_lo,
3779 Register value_hi,
3780 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003781 Register temp2,
3782 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003783 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003784 if (offset != 0) {
3785 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003786 __ add(IP, addr, ShifterOperand(temp1));
3787 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003788 }
3789 __ Bind(&fail);
3790 // We need a load followed by store. (The address used in a STREX instruction must
3791 // be the same as the address in the most recently executed LDREX instruction.)
3792 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003793 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003794 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003795 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003796}
3797
3798void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3799 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3800
Nicolas Geoffray39468442014-09-02 15:17:15 +01003801 LocationSummary* locations =
3802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003803 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003804
Calin Juravle52c48962014-12-16 17:02:57 +00003805 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003806 if (Primitive::IsFloatingPointType(field_type)) {
3807 locations->SetInAt(1, Location::RequiresFpuRegister());
3808 } else {
3809 locations->SetInAt(1, Location::RequiresRegister());
3810 }
3811
Calin Juravle52c48962014-12-16 17:02:57 +00003812 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003813 bool generate_volatile = field_info.IsVolatile()
3814 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003815 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003816 bool needs_write_barrier =
3817 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003818 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003819 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003820 if (needs_write_barrier) {
3821 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003822 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003823 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003824 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003825 // - registers need to be consecutive
3826 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003827 // We don't test for ARM yet, and the assertion makes sure that we
3828 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003829 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3830
3831 locations->AddTemp(Location::RequiresRegister());
3832 locations->AddTemp(Location::RequiresRegister());
3833 if (field_type == Primitive::kPrimDouble) {
3834 // For doubles we need two more registers to copy the value.
3835 locations->AddTemp(Location::RegisterLocation(R2));
3836 locations->AddTemp(Location::RegisterLocation(R3));
3837 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003838 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839}
3840
Calin Juravle52c48962014-12-16 17:02:57 +00003841void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003842 const FieldInfo& field_info,
3843 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003844 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3845
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003846 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003847 Register base = locations->InAt(0).AsRegister<Register>();
3848 Location value = locations->InAt(1);
3849
3850 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003851 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003852 Primitive::Type field_type = field_info.GetFieldType();
3853 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003854 bool needs_write_barrier =
3855 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003856
3857 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003858 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003859 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003860
3861 switch (field_type) {
3862 case Primitive::kPrimBoolean:
3863 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003864 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003865 break;
3866 }
3867
3868 case Primitive::kPrimShort:
3869 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003870 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003871 break;
3872 }
3873
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003874 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003875 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003876 if (kPoisonHeapReferences && needs_write_barrier) {
3877 // Note that in the case where `value` is a null reference,
3878 // we do not enter this block, as a null reference does not
3879 // need poisoning.
3880 DCHECK_EQ(field_type, Primitive::kPrimNot);
3881 Register temp = locations->GetTemp(0).AsRegister<Register>();
3882 __ Mov(temp, value.AsRegister<Register>());
3883 __ PoisonHeapReference(temp);
3884 __ StoreToOffset(kStoreWord, temp, base, offset);
3885 } else {
3886 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3887 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003888 break;
3889 }
3890
3891 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003892 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003893 GenerateWideAtomicStore(base, offset,
3894 value.AsRegisterPairLow<Register>(),
3895 value.AsRegisterPairHigh<Register>(),
3896 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003897 locations->GetTemp(1).AsRegister<Register>(),
3898 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003899 } else {
3900 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003901 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003902 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003903 break;
3904 }
3905
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003906 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003907 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003908 break;
3909 }
3910
3911 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003912 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003913 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003914 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3915 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3916
3917 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3918
3919 GenerateWideAtomicStore(base, offset,
3920 value_reg_lo,
3921 value_reg_hi,
3922 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003923 locations->GetTemp(3).AsRegister<Register>(),
3924 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003925 } else {
3926 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003927 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003928 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003929 break;
3930 }
3931
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003932 case Primitive::kPrimVoid:
3933 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003934 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003935 }
Calin Juravle52c48962014-12-16 17:02:57 +00003936
Calin Juravle77520bc2015-01-12 18:45:46 +00003937 // Longs and doubles are handled in the switch.
3938 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3939 codegen_->MaybeRecordImplicitNullCheck(instruction);
3940 }
3941
3942 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3943 Register temp = locations->GetTemp(0).AsRegister<Register>();
3944 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003945 codegen_->MarkGCCard(
3946 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003947 }
3948
Calin Juravle52c48962014-12-16 17:02:57 +00003949 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003950 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003951 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003952}
3953
Calin Juravle52c48962014-12-16 17:02:57 +00003954void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3955 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003956
3957 bool object_field_get_with_read_barrier =
3958 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003959 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003960 new (GetGraph()->GetArena()) LocationSummary(instruction,
3961 object_field_get_with_read_barrier ?
3962 LocationSummary::kCallOnSlowPath :
3963 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003964 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003965
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003966 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003967 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003968 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003969 // The output overlaps in case of volatile long: we don't want the
3970 // code generated by GenerateWideAtomicLoad to overwrite the
3971 // object's location. Likewise, in the case of an object field get
3972 // with read barriers enabled, we do not want the load to overwrite
3973 // the object's location, as we need it to emit the read barrier.
3974 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3975 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003976
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003977 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3978 locations->SetOut(Location::RequiresFpuRegister());
3979 } else {
3980 locations->SetOut(Location::RequiresRegister(),
3981 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3982 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003983 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00003984 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003985 // - registers need to be consecutive
3986 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003987 // We don't test for ARM yet, and the assertion makes sure that we
3988 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003989 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3990 locations->AddTemp(Location::RequiresRegister());
3991 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00003992 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3993 // We need a temporary register for the read barrier marking slow
3994 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3995 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003996 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003997}
3998
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003999Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4000 Opcode opcode) {
4001 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4002 if (constant->IsConstant() &&
4003 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4004 return Location::ConstantLocation(constant->AsConstant());
4005 }
4006 return Location::RequiresRegister();
4007}
4008
4009bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4010 Opcode opcode) {
4011 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4012 if (Primitive::Is64BitType(input_cst->GetType())) {
4013 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4014 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4015 } else {
4016 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4017 }
4018}
4019
4020bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4021 ShifterOperand so;
4022 ArmAssembler* assembler = codegen_->GetAssembler();
4023 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4024 return true;
4025 }
4026 Opcode neg_opcode = kNoOperand;
4027 switch (opcode) {
4028 case AND:
4029 neg_opcode = BIC;
4030 break;
4031 case ORR:
4032 neg_opcode = ORN;
4033 break;
4034 default:
4035 return false;
4036 }
4037 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4038}
4039
Calin Juravle52c48962014-12-16 17:02:57 +00004040void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4041 const FieldInfo& field_info) {
4042 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004043
Calin Juravle52c48962014-12-16 17:02:57 +00004044 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004045 Location base_loc = locations->InAt(0);
4046 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004047 Location out = locations->Out();
4048 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004049 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004050 Primitive::Type field_type = field_info.GetFieldType();
4051 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4052
4053 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004054 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004055 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004056 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004057
Roland Levillainc9285912015-12-18 10:38:42 +00004058 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004059 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004060 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004061
Roland Levillainc9285912015-12-18 10:38:42 +00004062 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004063 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004064 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004065
Roland Levillainc9285912015-12-18 10:38:42 +00004066 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004067 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004068 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004069
4070 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004071 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004072 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004073
4074 case Primitive::kPrimNot: {
4075 // /* HeapReference<Object> */ out = *(base + offset)
4076 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4077 Location temp_loc = locations->GetTemp(0);
4078 // Note that a potential implicit null check is handled in this
4079 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4080 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4081 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4082 if (is_volatile) {
4083 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4084 }
4085 } else {
4086 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4087 codegen_->MaybeRecordImplicitNullCheck(instruction);
4088 if (is_volatile) {
4089 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4090 }
4091 // If read barriers are enabled, emit read barriers other than
4092 // Baker's using a slow path (and also unpoison the loaded
4093 // reference, if heap poisoning is enabled).
4094 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4095 }
4096 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004097 }
4098
Roland Levillainc9285912015-12-18 10:38:42 +00004099 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004100 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004101 GenerateWideAtomicLoad(base, offset,
4102 out.AsRegisterPairLow<Register>(),
4103 out.AsRegisterPairHigh<Register>());
4104 } else {
4105 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4106 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004108
Roland Levillainc9285912015-12-18 10:38:42 +00004109 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004110 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004111 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004112
4113 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004114 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004115 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004116 Register lo = locations->GetTemp(0).AsRegister<Register>();
4117 Register hi = locations->GetTemp(1).AsRegister<Register>();
4118 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004119 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004120 __ vmovdrr(out_reg, lo, hi);
4121 } else {
4122 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004123 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004124 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004125 break;
4126 }
4127
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004128 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004129 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004130 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004131 }
Calin Juravle52c48962014-12-16 17:02:57 +00004132
Roland Levillainc9285912015-12-18 10:38:42 +00004133 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4134 // Potential implicit null checks, in the case of reference or
4135 // double fields, are handled in the previous switch statement.
4136 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004137 codegen_->MaybeRecordImplicitNullCheck(instruction);
4138 }
4139
Calin Juravle52c48962014-12-16 17:02:57 +00004140 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004141 if (field_type == Primitive::kPrimNot) {
4142 // Memory barriers, in the case of references, are also handled
4143 // in the previous switch statement.
4144 } else {
4145 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4146 }
Roland Levillain4d027112015-07-01 15:41:14 +01004147 }
Calin Juravle52c48962014-12-16 17:02:57 +00004148}
4149
4150void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4151 HandleFieldSet(instruction, instruction->GetFieldInfo());
4152}
4153
4154void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004155 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004156}
4157
4158void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4159 HandleFieldGet(instruction, instruction->GetFieldInfo());
4160}
4161
4162void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4163 HandleFieldGet(instruction, instruction->GetFieldInfo());
4164}
4165
4166void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4167 HandleFieldGet(instruction, instruction->GetFieldInfo());
4168}
4169
4170void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4171 HandleFieldGet(instruction, instruction->GetFieldInfo());
4172}
4173
4174void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4175 HandleFieldSet(instruction, instruction->GetFieldInfo());
4176}
4177
4178void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004179 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004180}
4181
Calin Juravlee460d1d2015-09-29 04:52:17 +01004182void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4183 HUnresolvedInstanceFieldGet* instruction) {
4184 FieldAccessCallingConventionARM calling_convention;
4185 codegen_->CreateUnresolvedFieldLocationSummary(
4186 instruction, instruction->GetFieldType(), calling_convention);
4187}
4188
4189void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4190 HUnresolvedInstanceFieldGet* instruction) {
4191 FieldAccessCallingConventionARM calling_convention;
4192 codegen_->GenerateUnresolvedFieldAccess(instruction,
4193 instruction->GetFieldType(),
4194 instruction->GetFieldIndex(),
4195 instruction->GetDexPc(),
4196 calling_convention);
4197}
4198
4199void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4200 HUnresolvedInstanceFieldSet* instruction) {
4201 FieldAccessCallingConventionARM calling_convention;
4202 codegen_->CreateUnresolvedFieldLocationSummary(
4203 instruction, instruction->GetFieldType(), calling_convention);
4204}
4205
4206void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4207 HUnresolvedInstanceFieldSet* instruction) {
4208 FieldAccessCallingConventionARM calling_convention;
4209 codegen_->GenerateUnresolvedFieldAccess(instruction,
4210 instruction->GetFieldType(),
4211 instruction->GetFieldIndex(),
4212 instruction->GetDexPc(),
4213 calling_convention);
4214}
4215
4216void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4217 HUnresolvedStaticFieldGet* instruction) {
4218 FieldAccessCallingConventionARM calling_convention;
4219 codegen_->CreateUnresolvedFieldLocationSummary(
4220 instruction, instruction->GetFieldType(), calling_convention);
4221}
4222
4223void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4224 HUnresolvedStaticFieldGet* instruction) {
4225 FieldAccessCallingConventionARM calling_convention;
4226 codegen_->GenerateUnresolvedFieldAccess(instruction,
4227 instruction->GetFieldType(),
4228 instruction->GetFieldIndex(),
4229 instruction->GetDexPc(),
4230 calling_convention);
4231}
4232
4233void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4234 HUnresolvedStaticFieldSet* instruction) {
4235 FieldAccessCallingConventionARM calling_convention;
4236 codegen_->CreateUnresolvedFieldLocationSummary(
4237 instruction, instruction->GetFieldType(), calling_convention);
4238}
4239
4240void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4241 HUnresolvedStaticFieldSet* instruction) {
4242 FieldAccessCallingConventionARM calling_convention;
4243 codegen_->GenerateUnresolvedFieldAccess(instruction,
4244 instruction->GetFieldType(),
4245 instruction->GetFieldIndex(),
4246 instruction->GetDexPc(),
4247 calling_convention);
4248}
4249
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004250void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004251 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4252 ? LocationSummary::kCallOnSlowPath
4253 : LocationSummary::kNoCall;
4254 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004255 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004256 if (instruction->HasUses()) {
4257 locations->SetOut(Location::SameAsFirstInput());
4258 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004259}
4260
Calin Juravle2ae48182016-03-16 14:05:09 +00004261void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4262 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004263 return;
4264 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004265 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004266
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004267 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004268 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004269}
4270
Calin Juravle2ae48182016-03-16 14:05:09 +00004271void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004272 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004273 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274
4275 LocationSummary* locations = instruction->GetLocations();
4276 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004278 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279}
4280
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004281void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004282 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004283}
4284
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004285void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004286 bool object_array_get_with_read_barrier =
4287 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004288 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004289 new (GetGraph()->GetArena()) LocationSummary(instruction,
4290 object_array_get_with_read_barrier ?
4291 LocationSummary::kCallOnSlowPath :
4292 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004293 locations->SetInAt(0, Location::RequiresRegister());
4294 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004295 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4296 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4297 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004298 // The output overlaps in the case of an object array get with
4299 // read barriers enabled: we do not want the move to overwrite the
4300 // array's location, as we need it to emit the read barrier.
4301 locations->SetOut(
4302 Location::RequiresRegister(),
4303 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004304 }
Roland Levillainc9285912015-12-18 10:38:42 +00004305 // We need a temporary register for the read barrier marking slow
4306 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4307 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4308 locations->AddTemp(Location::RequiresRegister());
4309 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004310}
4311
4312void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4313 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004314 Location obj_loc = locations->InAt(0);
4315 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004316 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004317 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004318 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004319
Roland Levillainc9285912015-12-18 10:38:42 +00004320 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004321 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004322 case Primitive::kPrimBoolean: {
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(kLoadUnsignedByte, 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(kLoadUnsignedByte, out, IP, data_offset);
4331 }
4332 break;
4333 }
4334
4335 case Primitive::kPrimByte: {
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_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4341 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004342 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004343 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4344 }
4345 break;
4346 }
4347
4348 case Primitive::kPrimShort: {
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(kLoadSignedHalfword, 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(kLoadSignedHalfword, out, IP, data_offset);
4357 }
4358 break;
4359 }
4360
4361 case Primitive::kPrimChar: {
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_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004366 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4367 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004369 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4370 }
4371 break;
4372 }
4373
Roland Levillainc9285912015-12-18 10:38:42 +00004374 case Primitive::kPrimInt: {
Roland Levillainc9285912015-12-18 10:38:42 +00004375 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004376 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004377 size_t offset =
4378 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004379 __ LoadFromOffset(kLoadWord, out, obj, offset);
4380 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004381 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004382 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4383 }
4384 break;
4385 }
4386
Roland Levillainc9285912015-12-18 10:38:42 +00004387 case Primitive::kPrimNot: {
4388 static_assert(
4389 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4390 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004391 // /* HeapReference<Object> */ out =
4392 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4393 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4394 Location temp = locations->GetTemp(0);
4395 // Note that a potential implicit null check is handled in this
4396 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4397 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4398 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4399 } else {
4400 Register out = out_loc.AsRegister<Register>();
4401 if (index.IsConstant()) {
4402 size_t offset =
4403 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4404 __ LoadFromOffset(kLoadWord, out, obj, offset);
4405 codegen_->MaybeRecordImplicitNullCheck(instruction);
4406 // If read barriers are enabled, emit read barriers other than
4407 // Baker's using a slow path (and also unpoison the loaded
4408 // reference, if heap poisoning is enabled).
4409 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4410 } else {
4411 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4412 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4413 codegen_->MaybeRecordImplicitNullCheck(instruction);
4414 // If read barriers are enabled, emit read barriers other than
4415 // Baker's using a slow path (and also unpoison the loaded
4416 // reference, if heap poisoning is enabled).
4417 codegen_->MaybeGenerateReadBarrierSlow(
4418 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4419 }
4420 }
4421 break;
4422 }
4423
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004424 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004426 size_t offset =
4427 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004428 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004429 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004430 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004431 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432 }
4433 break;
4434 }
4435
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004436 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004437 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004438 if (index.IsConstant()) {
4439 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004440 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004441 } else {
4442 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004443 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004444 }
4445 break;
4446 }
4447
4448 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004449 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004450 if (index.IsConstant()) {
4451 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004452 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004453 } else {
4454 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004455 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004456 }
4457 break;
4458 }
4459
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004460 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004461 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004462 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004463 }
Roland Levillain4d027112015-07-01 15:41:14 +01004464
4465 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004466 // Potential implicit null checks, in the case of reference
4467 // arrays, are handled in the previous switch statement.
4468 } else {
4469 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004470 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004471}
4472
4473void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004474 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004475
4476 bool needs_write_barrier =
4477 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004478 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4479 bool object_array_set_with_read_barrier =
4480 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004481
Nicolas Geoffray39468442014-09-02 15:17:15 +01004482 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004483 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004484 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4485 LocationSummary::kCallOnSlowPath :
4486 LocationSummary::kNoCall);
4487
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004488 locations->SetInAt(0, Location::RequiresRegister());
4489 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4490 if (Primitive::IsFloatingPointType(value_type)) {
4491 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004492 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004493 locations->SetInAt(2, Location::RequiresRegister());
4494 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004495 if (needs_write_barrier) {
4496 // Temporary registers for the write barrier.
4497 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004498 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004499 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004500}
4501
4502void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4503 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004504 Location array_loc = locations->InAt(0);
4505 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004506 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004507 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004508 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004509 bool needs_write_barrier =
4510 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004511
4512 switch (value_type) {
4513 case Primitive::kPrimBoolean:
4514 case Primitive::kPrimByte: {
4515 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004516 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004518 size_t offset =
4519 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004520 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004521 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004522 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4524 }
4525 break;
4526 }
4527
4528 case Primitive::kPrimShort:
4529 case Primitive::kPrimChar: {
4530 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004531 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004533 size_t offset =
4534 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004535 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004536 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004537 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4539 }
4540 break;
4541 }
4542
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004543 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004544 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004545 Location value_loc = locations->InAt(2);
4546 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004547 Register source = value;
4548
4549 if (instruction->InputAt(2)->IsNullConstant()) {
4550 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004551 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004552 size_t offset =
4553 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004554 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004555 } else {
4556 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004557 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004558 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004559 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004560 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004561 DCHECK(!needs_write_barrier);
4562 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004563 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004564 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004565
4566 DCHECK(needs_write_barrier);
4567 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4568 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4569 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4570 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4571 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4572 Label done;
4573 SlowPathCode* slow_path = nullptr;
4574
Roland Levillain3b359c72015-11-17 19:35:12 +00004575 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004576 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4577 codegen_->AddSlowPath(slow_path);
4578 if (instruction->GetValueCanBeNull()) {
4579 Label non_zero;
4580 __ CompareAndBranchIfNonZero(value, &non_zero);
4581 if (index.IsConstant()) {
4582 size_t offset =
4583 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4584 __ StoreToOffset(kStoreWord, value, array, offset);
4585 } else {
4586 DCHECK(index.IsRegister()) << index;
4587 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4588 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4589 }
4590 codegen_->MaybeRecordImplicitNullCheck(instruction);
4591 __ b(&done);
4592 __ Bind(&non_zero);
4593 }
4594
Roland Levillain3b359c72015-11-17 19:35:12 +00004595 if (kEmitCompilerReadBarrier) {
4596 // When read barriers are enabled, the type checking
4597 // instrumentation requires two read barriers:
4598 //
4599 // __ Mov(temp2, temp1);
4600 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4601 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004602 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004603 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4604 //
4605 // // /* HeapReference<Class> */ temp2 = value->klass_
4606 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004607 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004608 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4609 //
4610 // __ cmp(temp1, ShifterOperand(temp2));
4611 //
4612 // However, the second read barrier may trash `temp`, as it
4613 // is a temporary register, and as such would not be saved
4614 // along with live registers before calling the runtime (nor
4615 // restored afterwards). So in this case, we bail out and
4616 // delegate the work to the array set slow path.
4617 //
4618 // TODO: Extend the register allocator to support a new
4619 // "(locally) live temp" location so as to avoid always
4620 // going into the slow path when read barriers are enabled.
4621 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004622 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004623 // /* HeapReference<Class> */ temp1 = array->klass_
4624 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4625 codegen_->MaybeRecordImplicitNullCheck(instruction);
4626 __ MaybeUnpoisonHeapReference(temp1);
4627
4628 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4629 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4630 // /* HeapReference<Class> */ temp2 = value->klass_
4631 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4632 // If heap poisoning is enabled, no need to unpoison `temp1`
4633 // nor `temp2`, as we are comparing two poisoned references.
4634 __ cmp(temp1, ShifterOperand(temp2));
4635
4636 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4637 Label do_put;
4638 __ b(&do_put, EQ);
4639 // If heap poisoning is enabled, the `temp1` reference has
4640 // not been unpoisoned yet; unpoison it now.
4641 __ MaybeUnpoisonHeapReference(temp1);
4642
4643 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4644 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4645 // If heap poisoning is enabled, no need to unpoison
4646 // `temp1`, as we are comparing against null below.
4647 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4648 __ Bind(&do_put);
4649 } else {
4650 __ b(slow_path->GetEntryLabel(), NE);
4651 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004652 }
4653 }
4654
4655 if (kPoisonHeapReferences) {
4656 // Note that in the case where `value` is a null reference,
4657 // we do not enter this block, as a null reference does not
4658 // need poisoning.
4659 DCHECK_EQ(value_type, Primitive::kPrimNot);
4660 __ Mov(temp1, value);
4661 __ PoisonHeapReference(temp1);
4662 source = temp1;
4663 }
4664
4665 if (index.IsConstant()) {
4666 size_t offset =
4667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4668 __ StoreToOffset(kStoreWord, source, array, offset);
4669 } else {
4670 DCHECK(index.IsRegister()) << index;
4671 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4672 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4673 }
4674
Roland Levillain3b359c72015-11-17 19:35:12 +00004675 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004676 codegen_->MaybeRecordImplicitNullCheck(instruction);
4677 }
4678
4679 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4680
4681 if (done.IsLinked()) {
4682 __ Bind(&done);
4683 }
4684
4685 if (slow_path != nullptr) {
4686 __ Bind(slow_path->GetExitLabel());
4687 }
4688
4689 break;
4690 }
4691
4692 case Primitive::kPrimInt: {
4693 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4694 Register value = locations->InAt(2).AsRegister<Register>();
4695 if (index.IsConstant()) {
4696 size_t offset =
4697 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4698 __ StoreToOffset(kStoreWord, value, array, offset);
4699 } else {
4700 DCHECK(index.IsRegister()) << index;
4701 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4702 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4703 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704 break;
4705 }
4706
4707 case Primitive::kPrimLong: {
4708 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004709 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004711 size_t offset =
4712 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004713 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004714 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004715 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004716 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004717 }
4718 break;
4719 }
4720
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004721 case Primitive::kPrimFloat: {
4722 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4723 Location value = locations->InAt(2);
4724 DCHECK(value.IsFpuRegister());
4725 if (index.IsConstant()) {
4726 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004727 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004728 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004729 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004730 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4731 }
4732 break;
4733 }
4734
4735 case Primitive::kPrimDouble: {
4736 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4737 Location value = locations->InAt(2);
4738 DCHECK(value.IsFpuRegisterPair());
4739 if (index.IsConstant()) {
4740 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004741 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004742 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004743 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004744 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4745 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004746
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004747 break;
4748 }
4749
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004751 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004752 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004753 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004754
Roland Levillain80e67092016-01-08 16:04:55 +00004755 // Objects are handled in the switch.
4756 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004757 codegen_->MaybeRecordImplicitNullCheck(instruction);
4758 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759}
4760
4761void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004762 LocationSummary* locations =
4763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004764 locations->SetInAt(0, Location::RequiresRegister());
4765 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766}
4767
4768void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4769 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004770 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004771 Register obj = locations->InAt(0).AsRegister<Register>();
4772 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004774 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004775}
4776
4777void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004778 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4779 ? LocationSummary::kCallOnSlowPath
4780 : LocationSummary::kNoCall;
4781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782 locations->SetInAt(0, Location::RequiresRegister());
4783 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004784 if (instruction->HasUses()) {
4785 locations->SetOut(Location::SameAsFirstInput());
4786 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787}
4788
4789void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4790 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004791 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004792 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004793 codegen_->AddSlowPath(slow_path);
4794
Roland Levillain271ab9c2014-11-27 15:23:57 +00004795 Register index = locations->InAt(0).AsRegister<Register>();
4796 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004797
4798 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004799 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800}
4801
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004802void CodeGeneratorARM::MarkGCCard(Register temp,
4803 Register card,
4804 Register object,
4805 Register value,
4806 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004807 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004808 if (can_be_null) {
4809 __ CompareAndBranchIfZero(value, &is_null);
4810 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004811 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4812 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4813 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004814 if (can_be_null) {
4815 __ Bind(&is_null);
4816 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817}
4818
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004819void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004820 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004821}
4822
4823void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004824 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4825}
4826
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004827void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4828 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4829}
4830
4831void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004832 HBasicBlock* block = instruction->GetBlock();
4833 if (block->GetLoopInformation() != nullptr) {
4834 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4835 // The back edge will generate the suspend check.
4836 return;
4837 }
4838 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4839 // The goto will generate the suspend check.
4840 return;
4841 }
4842 GenerateSuspendCheck(instruction, nullptr);
4843}
4844
4845void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4846 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004847 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004848 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4849 if (slow_path == nullptr) {
4850 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4851 instruction->SetSlowPath(slow_path);
4852 codegen_->AddSlowPath(slow_path);
4853 if (successor != nullptr) {
4854 DCHECK(successor->IsLoopHeader());
4855 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4856 }
4857 } else {
4858 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4859 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004860
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004861 __ LoadFromOffset(
4862 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004863 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004864 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004865 __ Bind(slow_path->GetReturnLabel());
4866 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004867 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004868 __ b(slow_path->GetEntryLabel());
4869 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004870}
4871
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004872ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4873 return codegen_->GetAssembler();
4874}
4875
4876void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004877 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004878 Location source = move->GetSource();
4879 Location destination = move->GetDestination();
4880
4881 if (source.IsRegister()) {
4882 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004883 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004884 } else if (destination.IsFpuRegister()) {
4885 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004886 } else {
4887 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004888 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004889 SP, destination.GetStackIndex());
4890 }
4891 } else if (source.IsStackSlot()) {
4892 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004893 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004894 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004895 } else if (destination.IsFpuRegister()) {
4896 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004897 } else {
4898 DCHECK(destination.IsStackSlot());
4899 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4900 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4901 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004902 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004903 if (destination.IsRegister()) {
4904 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
4905 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004906 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004907 } else {
4908 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004909 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4910 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004911 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004912 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004913 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4914 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004915 } else if (destination.IsRegisterPair()) {
4916 DCHECK(ExpectedPairLayout(destination));
4917 __ LoadFromOffset(
4918 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4919 } else {
4920 DCHECK(destination.IsFpuRegisterPair()) << destination;
4921 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4922 SP,
4923 source.GetStackIndex());
4924 }
4925 } else if (source.IsRegisterPair()) {
4926 if (destination.IsRegisterPair()) {
4927 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4928 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004929 } else if (destination.IsFpuRegisterPair()) {
4930 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4931 source.AsRegisterPairLow<Register>(),
4932 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004933 } else {
4934 DCHECK(destination.IsDoubleStackSlot()) << destination;
4935 DCHECK(ExpectedPairLayout(source));
4936 __ StoreToOffset(
4937 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4938 }
4939 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004940 if (destination.IsRegisterPair()) {
4941 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4942 destination.AsRegisterPairHigh<Register>(),
4943 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4944 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004945 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4946 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4947 } else {
4948 DCHECK(destination.IsDoubleStackSlot()) << destination;
4949 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4950 SP,
4951 destination.GetStackIndex());
4952 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004953 } else {
4954 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004955 HConstant* constant = source.GetConstant();
4956 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4957 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004958 if (destination.IsRegister()) {
4959 __ LoadImmediate(destination.AsRegister<Register>(), value);
4960 } else {
4961 DCHECK(destination.IsStackSlot());
4962 __ LoadImmediate(IP, value);
4963 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4964 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004965 } else if (constant->IsLongConstant()) {
4966 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004967 if (destination.IsRegisterPair()) {
4968 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4969 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004970 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004971 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004972 __ LoadImmediate(IP, Low32Bits(value));
4973 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4974 __ LoadImmediate(IP, High32Bits(value));
4975 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4976 }
4977 } else if (constant->IsDoubleConstant()) {
4978 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004979 if (destination.IsFpuRegisterPair()) {
4980 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004981 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004982 DCHECK(destination.IsDoubleStackSlot()) << destination;
4983 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004984 __ LoadImmediate(IP, Low32Bits(int_value));
4985 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4986 __ LoadImmediate(IP, High32Bits(int_value));
4987 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4988 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004989 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004990 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004991 float value = constant->AsFloatConstant()->GetValue();
4992 if (destination.IsFpuRegister()) {
4993 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4994 } else {
4995 DCHECK(destination.IsStackSlot());
4996 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4997 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4998 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004999 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005000 }
5001}
5002
5003void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5004 __ Mov(IP, reg);
5005 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5006 __ StoreToOffset(kStoreWord, IP, SP, mem);
5007}
5008
5009void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5010 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5011 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5012 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5013 SP, mem1 + stack_offset);
5014 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5015 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5016 SP, mem2 + stack_offset);
5017 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5018}
5019
5020void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005021 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005022 Location source = move->GetSource();
5023 Location destination = move->GetDestination();
5024
5025 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005026 DCHECK_NE(source.AsRegister<Register>(), IP);
5027 DCHECK_NE(destination.AsRegister<Register>(), IP);
5028 __ Mov(IP, source.AsRegister<Register>());
5029 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5030 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005031 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005032 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005033 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005034 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005035 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5036 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005037 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005038 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005039 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005040 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005041 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005042 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005043 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005044 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005045 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5046 destination.AsRegisterPairHigh<Register>(),
5047 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005048 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005049 Register low_reg = source.IsRegisterPair()
5050 ? source.AsRegisterPairLow<Register>()
5051 : destination.AsRegisterPairLow<Register>();
5052 int mem = source.IsRegisterPair()
5053 ? destination.GetStackIndex()
5054 : source.GetStackIndex();
5055 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005056 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005057 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005058 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005059 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005060 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5061 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005062 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005063 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005064 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005065 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5066 DRegister reg = source.IsFpuRegisterPair()
5067 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5068 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5069 int mem = source.IsFpuRegisterPair()
5070 ? destination.GetStackIndex()
5071 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005072 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005073 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005074 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005075 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5076 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5077 : destination.AsFpuRegister<SRegister>();
5078 int mem = source.IsFpuRegister()
5079 ? destination.GetStackIndex()
5080 : source.GetStackIndex();
5081
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005082 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005083 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005084 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005085 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005086 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5087 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005088 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005089 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005090 }
5091}
5092
5093void ParallelMoveResolverARM::SpillScratch(int reg) {
5094 __ Push(static_cast<Register>(reg));
5095}
5096
5097void ParallelMoveResolverARM::RestoreScratch(int reg) {
5098 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005099}
5100
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005101HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5102 HLoadClass::LoadKind desired_class_load_kind) {
5103 if (kEmitCompilerReadBarrier) {
5104 switch (desired_class_load_kind) {
5105 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5106 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5107 case HLoadClass::LoadKind::kBootImageAddress:
5108 // TODO: Implement for read barrier.
5109 return HLoadClass::LoadKind::kDexCacheViaMethod;
5110 default:
5111 break;
5112 }
5113 }
5114 switch (desired_class_load_kind) {
5115 case HLoadClass::LoadKind::kReferrersClass:
5116 break;
5117 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5118 DCHECK(!GetCompilerOptions().GetCompilePic());
5119 break;
5120 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5121 DCHECK(GetCompilerOptions().GetCompilePic());
5122 break;
5123 case HLoadClass::LoadKind::kBootImageAddress:
5124 break;
5125 case HLoadClass::LoadKind::kDexCacheAddress:
5126 DCHECK(Runtime::Current()->UseJitCompilation());
5127 break;
5128 case HLoadClass::LoadKind::kDexCachePcRelative:
5129 DCHECK(!Runtime::Current()->UseJitCompilation());
5130 // We disable pc-relative load when there is an irreducible loop, as the optimization
5131 // is incompatible with it.
5132 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5133 // with irreducible loops.
5134 if (GetGraph()->HasIrreducibleLoops()) {
5135 return HLoadClass::LoadKind::kDexCacheViaMethod;
5136 }
5137 break;
5138 case HLoadClass::LoadKind::kDexCacheViaMethod:
5139 break;
5140 }
5141 return desired_class_load_kind;
5142}
5143
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005144void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005145 if (cls->NeedsAccessCheck()) {
5146 InvokeRuntimeCallingConvention calling_convention;
5147 CodeGenerator::CreateLoadClassLocationSummary(
5148 cls,
5149 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5150 Location::RegisterLocation(R0),
5151 /* code_generator_supports_read_barrier */ true);
5152 return;
5153 }
5154
5155 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5156 ? LocationSummary::kCallOnSlowPath
5157 : LocationSummary::kNoCall;
5158 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5159 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5160 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5161 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5162 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5163 locations->SetInAt(0, Location::RequiresRegister());
5164 }
5165 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005166}
5167
5168void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005169 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005170 if (cls->NeedsAccessCheck()) {
5171 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5172 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5173 cls,
5174 cls->GetDexPc(),
5175 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005176 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005177 return;
5178 }
5179
Roland Levillain3b359c72015-11-17 19:35:12 +00005180 Location out_loc = locations->Out();
5181 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005182
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005183 bool generate_null_check = false;
5184 switch (cls->GetLoadKind()) {
5185 case HLoadClass::LoadKind::kReferrersClass: {
5186 DCHECK(!cls->CanCallRuntime());
5187 DCHECK(!cls->MustGenerateClinitCheck());
5188 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5189 Register current_method = locations->InAt(0).AsRegister<Register>();
5190 GenerateGcRootFieldLoad(
5191 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5192 break;
5193 }
5194 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5195 DCHECK(!kEmitCompilerReadBarrier);
5196 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5197 cls->GetTypeIndex()));
5198 break;
5199 }
5200 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5201 DCHECK(!kEmitCompilerReadBarrier);
5202 CodeGeneratorARM::PcRelativePatchInfo* labels =
5203 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5204 __ BindTrackedLabel(&labels->movw_label);
5205 __ movw(out, /* placeholder */ 0u);
5206 __ BindTrackedLabel(&labels->movt_label);
5207 __ movt(out, /* placeholder */ 0u);
5208 __ BindTrackedLabel(&labels->add_pc_label);
5209 __ add(out, out, ShifterOperand(PC));
5210 break;
5211 }
5212 case HLoadClass::LoadKind::kBootImageAddress: {
5213 DCHECK(!kEmitCompilerReadBarrier);
5214 DCHECK_NE(cls->GetAddress(), 0u);
5215 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5216 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5217 break;
5218 }
5219 case HLoadClass::LoadKind::kDexCacheAddress: {
5220 DCHECK_NE(cls->GetAddress(), 0u);
5221 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5222 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5223 // a 128B range. To try and reduce the number of literals if we load multiple types,
5224 // simply split the dex cache address to a 128B aligned base loaded from a literal
5225 // and the remaining offset embedded in the load.
5226 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5227 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5228 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5229 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5230 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5231 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5232 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5233 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5234 generate_null_check = !cls->IsInDexCache();
5235 break;
5236 }
5237 case HLoadClass::LoadKind::kDexCachePcRelative: {
5238 Register base_reg = locations->InAt(0).AsRegister<Register>();
5239 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5240 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5241 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5242 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5243 generate_null_check = !cls->IsInDexCache();
5244 break;
5245 }
5246 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5247 // /* GcRoot<mirror::Class>[] */ out =
5248 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5249 Register current_method = locations->InAt(0).AsRegister<Register>();
5250 __ LoadFromOffset(kLoadWord,
5251 out,
5252 current_method,
5253 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5254 // /* GcRoot<mirror::Class> */ out = out[type_index]
5255 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5256 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5257 generate_null_check = !cls->IsInDexCache();
5258 }
5259 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005260
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005261 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5262 DCHECK(cls->CanCallRuntime());
5263 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5264 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5265 codegen_->AddSlowPath(slow_path);
5266 if (generate_null_check) {
5267 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5268 }
5269 if (cls->MustGenerateClinitCheck()) {
5270 GenerateClassInitializationCheck(slow_path, out);
5271 } else {
5272 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005273 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005274 }
5275}
5276
5277void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5278 LocationSummary* locations =
5279 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5280 locations->SetInAt(0, Location::RequiresRegister());
5281 if (check->HasUses()) {
5282 locations->SetOut(Location::SameAsFirstInput());
5283 }
5284}
5285
5286void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005287 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005288 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005289 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005290 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005291 GenerateClassInitializationCheck(slow_path,
5292 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005293}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005294
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005295void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005296 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005297 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5298 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5299 __ b(slow_path->GetEntryLabel(), LT);
5300 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5301 // properly. Therefore, we do a memory fence.
5302 __ dmb(ISH);
5303 __ Bind(slow_path->GetExitLabel());
5304}
5305
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005306HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5307 HLoadString::LoadKind desired_string_load_kind) {
5308 if (kEmitCompilerReadBarrier) {
5309 switch (desired_string_load_kind) {
5310 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5311 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5312 case HLoadString::LoadKind::kBootImageAddress:
5313 // TODO: Implement for read barrier.
5314 return HLoadString::LoadKind::kDexCacheViaMethod;
5315 default:
5316 break;
5317 }
5318 }
5319 switch (desired_string_load_kind) {
5320 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5321 DCHECK(!GetCompilerOptions().GetCompilePic());
5322 break;
5323 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5324 DCHECK(GetCompilerOptions().GetCompilePic());
5325 break;
5326 case HLoadString::LoadKind::kBootImageAddress:
5327 break;
5328 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005329 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005330 break;
5331 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005332 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005333 // We disable pc-relative load when there is an irreducible loop, as the optimization
5334 // is incompatible with it.
5335 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5336 // with irreducible loops.
5337 if (GetGraph()->HasIrreducibleLoops()) {
5338 return HLoadString::LoadKind::kDexCacheViaMethod;
5339 }
5340 break;
5341 case HLoadString::LoadKind::kDexCacheViaMethod:
5342 break;
5343 }
5344 return desired_string_load_kind;
5345}
5346
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005347void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005348 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005349 ? LocationSummary::kCallOnSlowPath
5350 : LocationSummary::kNoCall;
5351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005352 HLoadString::LoadKind load_kind = load->GetLoadKind();
5353 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5354 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5355 locations->SetInAt(0, Location::RequiresRegister());
5356 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005357 locations->SetOut(Location::RequiresRegister());
5358}
5359
5360void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005361 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005362 Location out_loc = locations->Out();
5363 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005364
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005365 switch (load->GetLoadKind()) {
5366 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5367 DCHECK(!kEmitCompilerReadBarrier);
5368 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5369 load->GetStringIndex()));
5370 return; // No dex cache slow path.
5371 }
5372 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5373 DCHECK(!kEmitCompilerReadBarrier);
5374 CodeGeneratorARM::PcRelativePatchInfo* labels =
5375 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5376 __ BindTrackedLabel(&labels->movw_label);
5377 __ movw(out, /* placeholder */ 0u);
5378 __ BindTrackedLabel(&labels->movt_label);
5379 __ movt(out, /* placeholder */ 0u);
5380 __ BindTrackedLabel(&labels->add_pc_label);
5381 __ add(out, out, ShifterOperand(PC));
5382 return; // No dex cache slow path.
5383 }
5384 case HLoadString::LoadKind::kBootImageAddress: {
5385 DCHECK(!kEmitCompilerReadBarrier);
5386 DCHECK_NE(load->GetAddress(), 0u);
5387 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5388 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5389 return; // No dex cache slow path.
5390 }
5391 case HLoadString::LoadKind::kDexCacheAddress: {
5392 DCHECK_NE(load->GetAddress(), 0u);
5393 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5394 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5395 // a 128B range. To try and reduce the number of literals if we load multiple strings,
5396 // simply split the dex cache address to a 128B aligned base loaded from a literal
5397 // and the remaining offset embedded in the load.
5398 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
5399 DCHECK_ALIGNED(load->GetAddress(), 4u);
5400 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5401 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5402 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5403 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005404 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005405 GenerateGcRootFieldLoad(load, out_loc, out, offset);
5406 break;
5407 }
5408 case HLoadString::LoadKind::kDexCachePcRelative: {
5409 Register base_reg = locations->InAt(0).AsRegister<Register>();
5410 HArmDexCacheArraysBase* base = load->InputAt(0)->AsArmDexCacheArraysBase();
5411 int32_t offset = load->GetDexCacheElementOffset() - base->GetElementOffset();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005412 // /* GcRoot<mirror::String> */ out = *(dex_cache_arrays_base + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005413 GenerateGcRootFieldLoad(load, out_loc, base_reg, offset);
5414 break;
5415 }
5416 case HLoadString::LoadKind::kDexCacheViaMethod: {
5417 Register current_method = locations->InAt(0).AsRegister<Register>();
5418
5419 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5420 GenerateGcRootFieldLoad(
5421 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5422 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5423 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
5424 // /* GcRoot<mirror::String> */ out = out[string_index]
5425 GenerateGcRootFieldLoad(
5426 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
5427 break;
5428 }
5429 default:
5430 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5431 UNREACHABLE();
5432 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005433
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005434 if (!load->IsInDexCache()) {
5435 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5436 codegen_->AddSlowPath(slow_path);
5437 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5438 __ Bind(slow_path->GetExitLabel());
5439 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005440}
5441
David Brazdilcb1c0552015-08-04 16:22:25 +01005442static int32_t GetExceptionTlsOffset() {
5443 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5444}
5445
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005446void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5447 LocationSummary* locations =
5448 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5449 locations->SetOut(Location::RequiresRegister());
5450}
5451
5452void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005453 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005454 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5455}
5456
5457void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5458 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5459}
5460
5461void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005462 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005463 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005464}
5465
5466void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5467 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005468 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005469 InvokeRuntimeCallingConvention calling_convention;
5470 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5471}
5472
5473void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5474 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005475 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005476 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005477}
5478
Roland Levillainc9285912015-12-18 10:38:42 +00005479static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5480 return kEmitCompilerReadBarrier &&
5481 (kUseBakerReadBarrier ||
5482 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5483 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5484 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5485}
5486
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005487void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005488 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005489 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5490 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005491 case TypeCheckKind::kExactCheck:
5492 case TypeCheckKind::kAbstractClassCheck:
5493 case TypeCheckKind::kClassHierarchyCheck:
5494 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005495 call_kind =
5496 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005497 break;
5498 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005499 case TypeCheckKind::kUnresolvedCheck:
5500 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005501 call_kind = LocationSummary::kCallOnSlowPath;
5502 break;
5503 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005504
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005505 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005506 locations->SetInAt(0, Location::RequiresRegister());
5507 locations->SetInAt(1, Location::RequiresRegister());
5508 // The "out" register is used as a temporary, so it overlaps with the inputs.
5509 // Note that TypeCheckSlowPathARM uses this register too.
5510 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5511 // When read barriers are enabled, we need a temporary register for
5512 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005513 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005514 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005515 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005516}
5517
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005518void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005519 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005520 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005521 Location obj_loc = locations->InAt(0);
5522 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005523 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005524 Location out_loc = locations->Out();
5525 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005526 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005527 locations->GetTemp(0) :
5528 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005529 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005530 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5531 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5532 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005533 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005534 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005535
5536 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005537 // avoid null check if we know obj is not null.
5538 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005539 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005540 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005541
Roland Levillain3b359c72015-11-17 19:35:12 +00005542 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005543 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005544
Roland Levillainc9285912015-12-18 10:38:42 +00005545 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005546 case TypeCheckKind::kExactCheck: {
5547 __ cmp(out, ShifterOperand(cls));
5548 // Classes must be equal for the instanceof to succeed.
5549 __ b(&zero, NE);
5550 __ LoadImmediate(out, 1);
5551 __ b(&done);
5552 break;
5553 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005554
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005555 case TypeCheckKind::kAbstractClassCheck: {
5556 // If the class is abstract, we eagerly fetch the super class of the
5557 // object to avoid doing a comparison we know will fail.
5558 Label loop;
5559 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005560 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005561 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005562 // If `out` is null, we use it for the result, and jump to `done`.
5563 __ CompareAndBranchIfZero(out, &done);
5564 __ cmp(out, ShifterOperand(cls));
5565 __ b(&loop, NE);
5566 __ LoadImmediate(out, 1);
5567 if (zero.IsLinked()) {
5568 __ b(&done);
5569 }
5570 break;
5571 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005572
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005573 case TypeCheckKind::kClassHierarchyCheck: {
5574 // Walk over the class hierarchy to find a match.
5575 Label loop, success;
5576 __ Bind(&loop);
5577 __ cmp(out, ShifterOperand(cls));
5578 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005579 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005580 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005581 __ CompareAndBranchIfNonZero(out, &loop);
5582 // If `out` is null, we use it for the result, and jump to `done`.
5583 __ b(&done);
5584 __ Bind(&success);
5585 __ LoadImmediate(out, 1);
5586 if (zero.IsLinked()) {
5587 __ b(&done);
5588 }
5589 break;
5590 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005591
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005592 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005593 // Do an exact check.
5594 Label exact_check;
5595 __ cmp(out, ShifterOperand(cls));
5596 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005597 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005598 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005599 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005600 // If `out` is null, we use it for the result, and jump to `done`.
5601 __ CompareAndBranchIfZero(out, &done);
5602 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5603 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5604 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005605 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005606 __ LoadImmediate(out, 1);
5607 __ b(&done);
5608 break;
5609 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005610
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005611 case TypeCheckKind::kArrayCheck: {
5612 __ cmp(out, ShifterOperand(cls));
5613 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005614 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5615 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005616 codegen_->AddSlowPath(slow_path);
5617 __ b(slow_path->GetEntryLabel(), NE);
5618 __ LoadImmediate(out, 1);
5619 if (zero.IsLinked()) {
5620 __ b(&done);
5621 }
5622 break;
5623 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005624
Calin Juravle98893e12015-10-02 21:05:03 +01005625 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005626 case TypeCheckKind::kInterfaceCheck: {
5627 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005628 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005629 // cases.
5630 //
5631 // We cannot directly call the InstanceofNonTrivial runtime
5632 // entry point without resorting to a type checking slow path
5633 // here (i.e. by calling InvokeRuntime directly), as it would
5634 // require to assign fixed registers for the inputs of this
5635 // HInstanceOf instruction (following the runtime calling
5636 // convention), which might be cluttered by the potential first
5637 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005638 //
5639 // TODO: Introduce a new runtime entry point taking the object
5640 // to test (instead of its class) as argument, and let it deal
5641 // with the read barrier issues. This will let us refactor this
5642 // case of the `switch` code as it was previously (with a direct
5643 // call to the runtime not using a type checking slow path).
5644 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005645 DCHECK(locations->OnlyCallsOnSlowPath());
5646 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5647 /* is_fatal */ false);
5648 codegen_->AddSlowPath(slow_path);
5649 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005650 if (zero.IsLinked()) {
5651 __ b(&done);
5652 }
5653 break;
5654 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005655 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005656
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005657 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005658 __ Bind(&zero);
5659 __ LoadImmediate(out, 0);
5660 }
5661
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005662 if (done.IsLinked()) {
5663 __ Bind(&done);
5664 }
5665
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005666 if (slow_path != nullptr) {
5667 __ Bind(slow_path->GetExitLabel());
5668 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005669}
5670
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005671void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005672 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5673 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5674
Roland Levillain3b359c72015-11-17 19:35:12 +00005675 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5676 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005677 case TypeCheckKind::kExactCheck:
5678 case TypeCheckKind::kAbstractClassCheck:
5679 case TypeCheckKind::kClassHierarchyCheck:
5680 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005681 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5682 LocationSummary::kCallOnSlowPath :
5683 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 break;
5685 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005686 case TypeCheckKind::kUnresolvedCheck:
5687 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005688 call_kind = LocationSummary::kCallOnSlowPath;
5689 break;
5690 }
5691
Roland Levillain3b359c72015-11-17 19:35:12 +00005692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5693 locations->SetInAt(0, Location::RequiresRegister());
5694 locations->SetInAt(1, Location::RequiresRegister());
5695 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5696 locations->AddTemp(Location::RequiresRegister());
5697 // When read barriers are enabled, we need an additional temporary
5698 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005699 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005700 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005701 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005702}
5703
5704void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005705 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005706 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005707 Location obj_loc = locations->InAt(0);
5708 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005709 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005710 Location temp_loc = locations->GetTemp(0);
5711 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005712 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005713 locations->GetTemp(1) :
5714 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005715 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005716 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5717 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5718 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005719
Roland Levillain3b359c72015-11-17 19:35:12 +00005720 bool is_type_check_slow_path_fatal =
5721 (type_check_kind == TypeCheckKind::kExactCheck ||
5722 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5723 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5724 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5725 !instruction->CanThrowIntoCatchBlock();
5726 SlowPathCode* type_check_slow_path =
5727 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5728 is_type_check_slow_path_fatal);
5729 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005730
5731 Label done;
5732 // Avoid null check if we know obj is not null.
5733 if (instruction->MustDoNullCheck()) {
5734 __ CompareAndBranchIfZero(obj, &done);
5735 }
5736
Roland Levillain3b359c72015-11-17 19:35:12 +00005737 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005738 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005739
Roland Levillain3b359c72015-11-17 19:35:12 +00005740 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005741 case TypeCheckKind::kExactCheck:
5742 case TypeCheckKind::kArrayCheck: {
5743 __ cmp(temp, ShifterOperand(cls));
5744 // Jump to slow path for throwing the exception or doing a
5745 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005746 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005747 break;
5748 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005750 case TypeCheckKind::kAbstractClassCheck: {
5751 // If the class is abstract, we eagerly fetch the super class of the
5752 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005753 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005755 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005756 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005757
5758 // If the class reference currently in `temp` is not null, jump
5759 // to the `compare_classes` label to compare it with the checked
5760 // class.
5761 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5762 // Otherwise, jump to the slow path to throw the exception.
5763 //
5764 // But before, move back the object's class into `temp` before
5765 // going into the slow path, as it has been overwritten in the
5766 // meantime.
5767 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005768 GenerateReferenceLoadTwoRegisters(
5769 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005770 __ b(type_check_slow_path->GetEntryLabel());
5771
5772 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 __ cmp(temp, ShifterOperand(cls));
5774 __ b(&loop, NE);
5775 break;
5776 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005777
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005778 case TypeCheckKind::kClassHierarchyCheck: {
5779 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005780 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005781 __ Bind(&loop);
5782 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005783 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005784
Roland Levillain3b359c72015-11-17 19:35:12 +00005785 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005786 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005787
5788 // If the class reference currently in `temp` is not null, jump
5789 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005791 // Otherwise, jump to the slow path to throw the exception.
5792 //
5793 // But before, move back the object's class into `temp` before
5794 // going into the slow path, as it has been overwritten in the
5795 // meantime.
5796 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005797 GenerateReferenceLoadTwoRegisters(
5798 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005799 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005800 break;
5801 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005802
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005803 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005804 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005805 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005806 __ cmp(temp, ShifterOperand(cls));
5807 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005808
5809 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005810 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005811 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005812
5813 // If the component type is not null (i.e. the object is indeed
5814 // an array), jump to label `check_non_primitive_component_type`
5815 // to further check that this component type is not a primitive
5816 // type.
5817 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5818 // Otherwise, jump to the slow path to throw the exception.
5819 //
5820 // But before, move back the object's class into `temp` before
5821 // going into the slow path, as it has been overwritten in the
5822 // meantime.
5823 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005824 GenerateReferenceLoadTwoRegisters(
5825 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005826 __ b(type_check_slow_path->GetEntryLabel());
5827
5828 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005830 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5831 __ CompareAndBranchIfZero(temp, &done);
5832 // Same comment as above regarding `temp` and the slow path.
5833 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005834 GenerateReferenceLoadTwoRegisters(
5835 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005836 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005837 break;
5838 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005839
Calin Juravle98893e12015-10-02 21:05:03 +01005840 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005842 // We always go into the type check slow path for the unresolved
5843 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005844 //
5845 // We cannot directly call the CheckCast runtime entry point
5846 // without resorting to a type checking slow path here (i.e. by
5847 // calling InvokeRuntime directly), as it would require to
5848 // assign fixed registers for the inputs of this HInstanceOf
5849 // instruction (following the runtime calling convention), which
5850 // might be cluttered by the potential first read barrier
5851 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005852 //
5853 // TODO: Introduce a new runtime entry point taking the object
5854 // to test (instead of its class) as argument, and let it deal
5855 // with the read barrier issues. This will let us refactor this
5856 // case of the `switch` code as it was previously (with a direct
5857 // call to the runtime not using a type checking slow path).
5858 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005859 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 break;
5861 }
5862 __ Bind(&done);
5863
Roland Levillain3b359c72015-11-17 19:35:12 +00005864 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005865}
5866
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005867void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5868 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005869 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005870 InvokeRuntimeCallingConvention calling_convention;
5871 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5872}
5873
5874void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5875 codegen_->InvokeRuntime(instruction->IsEnter()
5876 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5877 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005878 instruction->GetDexPc(),
5879 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005880 if (instruction->IsEnter()) {
5881 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5882 } else {
5883 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5884 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005885}
5886
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005887void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5888void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5889void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005890
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005891void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005892 LocationSummary* locations =
5893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5894 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5895 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005896 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005897 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005898 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005900}
5901
5902void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5903 HandleBitwiseOperation(instruction);
5904}
5905
5906void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5907 HandleBitwiseOperation(instruction);
5908}
5909
5910void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5911 HandleBitwiseOperation(instruction);
5912}
5913
Artem Serov7fc63502016-02-09 17:15:29 +00005914
5915void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5916 LocationSummary* locations =
5917 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5918 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5919 || instruction->GetResultType() == Primitive::kPrimLong);
5920
5921 locations->SetInAt(0, Location::RequiresRegister());
5922 locations->SetInAt(1, Location::RequiresRegister());
5923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5924}
5925
5926void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5927 LocationSummary* locations = instruction->GetLocations();
5928 Location first = locations->InAt(0);
5929 Location second = locations->InAt(1);
5930 Location out = locations->Out();
5931
5932 if (instruction->GetResultType() == Primitive::kPrimInt) {
5933 Register first_reg = first.AsRegister<Register>();
5934 ShifterOperand second_reg(second.AsRegister<Register>());
5935 Register out_reg = out.AsRegister<Register>();
5936
5937 switch (instruction->GetOpKind()) {
5938 case HInstruction::kAnd:
5939 __ bic(out_reg, first_reg, second_reg);
5940 break;
5941 case HInstruction::kOr:
5942 __ orn(out_reg, first_reg, second_reg);
5943 break;
5944 // There is no EON on arm.
5945 case HInstruction::kXor:
5946 default:
5947 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
5948 UNREACHABLE();
5949 }
5950 return;
5951
5952 } else {
5953 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5954 Register first_low = first.AsRegisterPairLow<Register>();
5955 Register first_high = first.AsRegisterPairHigh<Register>();
5956 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5957 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5958 Register out_low = out.AsRegisterPairLow<Register>();
5959 Register out_high = out.AsRegisterPairHigh<Register>();
5960
5961 switch (instruction->GetOpKind()) {
5962 case HInstruction::kAnd:
5963 __ bic(out_low, first_low, second_low);
5964 __ bic(out_high, first_high, second_high);
5965 break;
5966 case HInstruction::kOr:
5967 __ orn(out_low, first_low, second_low);
5968 __ orn(out_high, first_high, second_high);
5969 break;
5970 // There is no EON on arm.
5971 case HInstruction::kXor:
5972 default:
5973 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
5974 UNREACHABLE();
5975 }
5976 }
5977}
5978
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005979void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5980 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5981 if (value == 0xffffffffu) {
5982 if (out != first) {
5983 __ mov(out, ShifterOperand(first));
5984 }
5985 return;
5986 }
5987 if (value == 0u) {
5988 __ mov(out, ShifterOperand(0));
5989 return;
5990 }
5991 ShifterOperand so;
5992 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5993 __ and_(out, first, so);
5994 } else {
5995 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5996 __ bic(out, first, ShifterOperand(~value));
5997 }
5998}
5999
6000void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6001 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6002 if (value == 0u) {
6003 if (out != first) {
6004 __ mov(out, ShifterOperand(first));
6005 }
6006 return;
6007 }
6008 if (value == 0xffffffffu) {
6009 __ mvn(out, ShifterOperand(0));
6010 return;
6011 }
6012 ShifterOperand so;
6013 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6014 __ orr(out, first, so);
6015 } else {
6016 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6017 __ orn(out, first, ShifterOperand(~value));
6018 }
6019}
6020
6021void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6022 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6023 if (value == 0u) {
6024 if (out != first) {
6025 __ mov(out, ShifterOperand(first));
6026 }
6027 return;
6028 }
6029 __ eor(out, first, ShifterOperand(value));
6030}
6031
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006032void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6033 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006034 Location first = locations->InAt(0);
6035 Location second = locations->InAt(1);
6036 Location out = locations->Out();
6037
6038 if (second.IsConstant()) {
6039 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6040 uint32_t value_low = Low32Bits(value);
6041 if (instruction->GetResultType() == Primitive::kPrimInt) {
6042 Register first_reg = first.AsRegister<Register>();
6043 Register out_reg = out.AsRegister<Register>();
6044 if (instruction->IsAnd()) {
6045 GenerateAndConst(out_reg, first_reg, value_low);
6046 } else if (instruction->IsOr()) {
6047 GenerateOrrConst(out_reg, first_reg, value_low);
6048 } else {
6049 DCHECK(instruction->IsXor());
6050 GenerateEorConst(out_reg, first_reg, value_low);
6051 }
6052 } else {
6053 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6054 uint32_t value_high = High32Bits(value);
6055 Register first_low = first.AsRegisterPairLow<Register>();
6056 Register first_high = first.AsRegisterPairHigh<Register>();
6057 Register out_low = out.AsRegisterPairLow<Register>();
6058 Register out_high = out.AsRegisterPairHigh<Register>();
6059 if (instruction->IsAnd()) {
6060 GenerateAndConst(out_low, first_low, value_low);
6061 GenerateAndConst(out_high, first_high, value_high);
6062 } else if (instruction->IsOr()) {
6063 GenerateOrrConst(out_low, first_low, value_low);
6064 GenerateOrrConst(out_high, first_high, value_high);
6065 } else {
6066 DCHECK(instruction->IsXor());
6067 GenerateEorConst(out_low, first_low, value_low);
6068 GenerateEorConst(out_high, first_high, value_high);
6069 }
6070 }
6071 return;
6072 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006073
6074 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006075 Register first_reg = first.AsRegister<Register>();
6076 ShifterOperand second_reg(second.AsRegister<Register>());
6077 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006078 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006079 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006080 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006081 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006082 } else {
6083 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006084 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006085 }
6086 } else {
6087 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006088 Register first_low = first.AsRegisterPairLow<Register>();
6089 Register first_high = first.AsRegisterPairHigh<Register>();
6090 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6091 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6092 Register out_low = out.AsRegisterPairLow<Register>();
6093 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006094 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006095 __ and_(out_low, first_low, second_low);
6096 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006097 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006098 __ orr(out_low, first_low, second_low);
6099 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006100 } else {
6101 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006102 __ eor(out_low, first_low, second_low);
6103 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006104 }
6105 }
6106}
6107
Roland Levillainc9285912015-12-18 10:38:42 +00006108void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6109 Location out,
6110 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006111 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006112 Register out_reg = out.AsRegister<Register>();
6113 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006114 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006115 if (kUseBakerReadBarrier) {
6116 // Load with fast path based Baker's read barrier.
6117 // /* HeapReference<Object> */ out = *(out + offset)
6118 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006119 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006120 } else {
6121 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006122 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006123 // in the following move operation, as we will need it for the
6124 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006125 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006126 // /* HeapReference<Object> */ out = *(out + offset)
6127 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006128 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006129 }
6130 } else {
6131 // Plain load with no read barrier.
6132 // /* HeapReference<Object> */ out = *(out + offset)
6133 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6134 __ MaybeUnpoisonHeapReference(out_reg);
6135 }
6136}
6137
6138void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6139 Location out,
6140 Location obj,
6141 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006142 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006143 Register out_reg = out.AsRegister<Register>();
6144 Register obj_reg = obj.AsRegister<Register>();
6145 if (kEmitCompilerReadBarrier) {
6146 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006147 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006148 // Load with fast path based Baker's read barrier.
6149 // /* HeapReference<Object> */ out = *(obj + offset)
6150 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006151 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006152 } else {
6153 // Load with slow path based read barrier.
6154 // /* HeapReference<Object> */ out = *(obj + offset)
6155 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6156 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6157 }
6158 } else {
6159 // Plain load with no read barrier.
6160 // /* HeapReference<Object> */ out = *(obj + offset)
6161 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6162 __ MaybeUnpoisonHeapReference(out_reg);
6163 }
6164}
6165
6166void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6167 Location root,
6168 Register obj,
6169 uint32_t offset) {
6170 Register root_reg = root.AsRegister<Register>();
6171 if (kEmitCompilerReadBarrier) {
6172 if (kUseBakerReadBarrier) {
6173 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6174 // Baker's read barrier are used:
6175 //
6176 // root = obj.field;
6177 // if (Thread::Current()->GetIsGcMarking()) {
6178 // root = ReadBarrier::Mark(root)
6179 // }
6180
6181 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6182 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6183 static_assert(
6184 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6185 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6186 "have different sizes.");
6187 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6188 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6189 "have different sizes.");
6190
6191 // Slow path used to mark the GC root `root`.
6192 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006193 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006194 codegen_->AddSlowPath(slow_path);
6195
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006196 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006197 __ LoadFromOffset(
6198 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6199 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6200 __ Bind(slow_path->GetExitLabel());
6201 } else {
6202 // GC root loaded through a slow path for read barriers other
6203 // than Baker's.
6204 // /* GcRoot<mirror::Object>* */ root = obj + offset
6205 __ AddConstant(root_reg, obj, offset);
6206 // /* mirror::Object* */ root = root->Read()
6207 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6208 }
6209 } else {
6210 // Plain GC root load with no read barrier.
6211 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6212 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6213 // Note that GC roots are not affected by heap poisoning, thus we
6214 // do not have to unpoison `root_reg` here.
6215 }
6216}
6217
6218void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6219 Location ref,
6220 Register obj,
6221 uint32_t offset,
6222 Location temp,
6223 bool needs_null_check) {
6224 DCHECK(kEmitCompilerReadBarrier);
6225 DCHECK(kUseBakerReadBarrier);
6226
6227 // /* HeapReference<Object> */ ref = *(obj + offset)
6228 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006229 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006230 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006231 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006232}
6233
6234void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6235 Location ref,
6236 Register obj,
6237 uint32_t data_offset,
6238 Location index,
6239 Location temp,
6240 bool needs_null_check) {
6241 DCHECK(kEmitCompilerReadBarrier);
6242 DCHECK(kUseBakerReadBarrier);
6243
Roland Levillainbfea3352016-06-23 13:48:47 +01006244 static_assert(
6245 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6246 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006247 // /* HeapReference<Object> */ ref =
6248 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006249 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006250 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006251 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006252}
6253
6254void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6255 Location ref,
6256 Register obj,
6257 uint32_t offset,
6258 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006259 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006260 Location temp,
6261 bool needs_null_check) {
6262 DCHECK(kEmitCompilerReadBarrier);
6263 DCHECK(kUseBakerReadBarrier);
6264
6265 // In slow path based read barriers, the read barrier call is
6266 // inserted after the original load. However, in fast path based
6267 // Baker's read barriers, we need to perform the load of
6268 // mirror::Object::monitor_ *before* the original reference load.
6269 // This load-load ordering is required by the read barrier.
6270 // The fast path/slow path (for Baker's algorithm) should look like:
6271 //
6272 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6273 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6274 // HeapReference<Object> ref = *src; // Original reference load.
6275 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6276 // if (is_gray) {
6277 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6278 // }
6279 //
6280 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006281 // slightly more complex as it performs additional checks that we do
6282 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006283
6284 Register ref_reg = ref.AsRegister<Register>();
6285 Register temp_reg = temp.AsRegister<Register>();
6286 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6287
6288 // /* int32_t */ monitor = obj->monitor_
6289 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6290 if (needs_null_check) {
6291 MaybeRecordImplicitNullCheck(instruction);
6292 }
6293 // /* LockWord */ lock_word = LockWord(monitor)
6294 static_assert(sizeof(LockWord) == sizeof(int32_t),
6295 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006296
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006297 // Introduce a dependency on the lock_word including the rb_state,
6298 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006299 // a memory barrier (which would be more expensive).
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006300 // obj is unchanged by this operation, but its value now depends on temp_reg.
6301 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006302
6303 // The actual reference load.
6304 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006305 // Load types involving an "index": ArrayGet and
6306 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6307 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006308 if (index.IsConstant()) {
6309 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006310 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006311 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6312 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006313 // Handle the special case of the
6314 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6315 // a register pair as index ("long offset"), of which only the low
6316 // part contains data.
6317 Register index_reg = index.IsRegisterPair()
6318 ? index.AsRegisterPairLow<Register>()
6319 : index.AsRegister<Register>();
6320 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006321 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6322 }
6323 } else {
6324 // /* HeapReference<Object> */ ref = *(obj + offset)
6325 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6326 }
6327
6328 // Object* ref = ref_addr->AsMirrorPtr()
6329 __ MaybeUnpoisonHeapReference(ref_reg);
6330
6331 // Slow path used to mark the object `ref` when it is gray.
6332 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006333 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006334 AddSlowPath(slow_path);
6335
6336 // if (rb_state == ReadBarrier::gray_ptr_)
6337 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006338 // Given the numeric representation, it's enough to check the low bit of the
6339 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6340 // which can be a 16-bit instruction unlike the TST immediate.
6341 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6342 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6343 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6344 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6345 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006346 __ Bind(slow_path->GetExitLabel());
6347}
6348
6349void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6350 Location out,
6351 Location ref,
6352 Location obj,
6353 uint32_t offset,
6354 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006355 DCHECK(kEmitCompilerReadBarrier);
6356
Roland Levillainc9285912015-12-18 10:38:42 +00006357 // Insert a slow path based read barrier *after* the reference load.
6358 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006359 // If heap poisoning is enabled, the unpoisoning of the loaded
6360 // reference will be carried out by the runtime within the slow
6361 // path.
6362 //
6363 // Note that `ref` currently does not get unpoisoned (when heap
6364 // poisoning is enabled), which is alright as the `ref` argument is
6365 // not used by the artReadBarrierSlow entry point.
6366 //
6367 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6368 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6369 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6370 AddSlowPath(slow_path);
6371
Roland Levillain3b359c72015-11-17 19:35:12 +00006372 __ b(slow_path->GetEntryLabel());
6373 __ Bind(slow_path->GetExitLabel());
6374}
6375
Roland Levillainc9285912015-12-18 10:38:42 +00006376void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6377 Location out,
6378 Location ref,
6379 Location obj,
6380 uint32_t offset,
6381 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006382 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006383 // Baker's read barriers shall be handled by the fast path
6384 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6385 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006386 // If heap poisoning is enabled, unpoisoning will be taken care of
6387 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006388 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006389 } else if (kPoisonHeapReferences) {
6390 __ UnpoisonHeapReference(out.AsRegister<Register>());
6391 }
6392}
6393
Roland Levillainc9285912015-12-18 10:38:42 +00006394void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6395 Location out,
6396 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006397 DCHECK(kEmitCompilerReadBarrier);
6398
Roland Levillainc9285912015-12-18 10:38:42 +00006399 // Insert a slow path based read barrier *after* the GC root load.
6400 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006401 // Note that GC roots are not affected by heap poisoning, so we do
6402 // not need to do anything special for this here.
6403 SlowPathCode* slow_path =
6404 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6405 AddSlowPath(slow_path);
6406
Roland Levillain3b359c72015-11-17 19:35:12 +00006407 __ b(slow_path->GetEntryLabel());
6408 __ Bind(slow_path->GetExitLabel());
6409}
6410
Vladimir Markodc151b22015-10-15 18:02:30 +01006411HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6412 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6413 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006414 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6415 // We disable pc-relative load when there is an irreducible loop, as the optimization
6416 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006417 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6418 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006419 if (GetGraph()->HasIrreducibleLoops() &&
6420 (dispatch_info.method_load_kind ==
6421 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6422 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6423 }
6424
6425 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006426 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6427 if (&outer_dex_file != target_method.dex_file) {
6428 // Calls across dex files are more likely to exceed the available BL range,
6429 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6430 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6431 (desired_dispatch_info.method_load_kind ==
6432 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6433 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6434 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6435 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006436 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006437 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006438 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006439 0u
6440 };
6441 }
6442 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006443 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006444}
6445
Vladimir Markob4536b72015-11-24 13:45:23 +00006446Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6447 Register temp) {
6448 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6449 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6450 if (!invoke->GetLocations()->Intrinsified()) {
6451 return location.AsRegister<Register>();
6452 }
6453 // For intrinsics we allow any location, so it may be on the stack.
6454 if (!location.IsRegister()) {
6455 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6456 return temp;
6457 }
6458 // For register locations, check if the register was saved. If so, get it from the stack.
6459 // Note: There is a chance that the register was saved but not overwritten, so we could
6460 // save one load. However, since this is just an intrinsic slow path we prefer this
6461 // simple and more robust approach rather that trying to determine if that's the case.
6462 SlowPathCode* slow_path = GetCurrentSlowPath();
6463 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6464 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6465 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6466 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6467 return temp;
6468 }
6469 return location.AsRegister<Register>();
6470}
6471
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006472void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006473 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006474 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006475 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6476 // LR = code address from literal pool with link-time patch.
6477 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006478 break;
6479 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6480 // LR = invoke->GetDirectCodePtr();
6481 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006482 break;
6483 default:
6484 break;
6485 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006486
Vladimir Marko58155012015-08-19 12:49:41 +00006487 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6488 switch (invoke->GetMethodLoadKind()) {
6489 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6490 // temp = thread->string_init_entrypoint
6491 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6492 break;
6493 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006494 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006495 break;
6496 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6497 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6498 break;
6499 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6500 __ LoadLiteral(temp.AsRegister<Register>(),
6501 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6502 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006503 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6504 HArmDexCacheArraysBase* base =
6505 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6506 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6507 temp.AsRegister<Register>());
6508 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6509 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6510 break;
6511 }
Vladimir Marko58155012015-08-19 12:49:41 +00006512 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006513 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006514 Register method_reg;
6515 Register reg = temp.AsRegister<Register>();
6516 if (current_method.IsRegister()) {
6517 method_reg = current_method.AsRegister<Register>();
6518 } else {
6519 DCHECK(invoke->GetLocations()->Intrinsified());
6520 DCHECK(!current_method.IsValid());
6521 method_reg = reg;
6522 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6523 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006524 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6525 __ LoadFromOffset(kLoadWord,
6526 reg,
6527 method_reg,
6528 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006529 // temp = temp[index_in_cache];
6530 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6531 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006532 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6533 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006534 }
Vladimir Marko58155012015-08-19 12:49:41 +00006535 }
6536
6537 switch (invoke->GetCodePtrLocation()) {
6538 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6539 __ bl(GetFrameEntryLabel());
6540 break;
6541 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006542 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006543 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006544 // Arbitrarily branch to the BL itself, override at link time.
6545 __ bl(&relative_call_patches_.back().label);
6546 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006547 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6548 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6549 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006550 // LR()
6551 __ blx(LR);
6552 break;
6553 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6554 // LR = callee_method->entry_point_from_quick_compiled_code_
6555 __ LoadFromOffset(
6556 kLoadWord, LR, callee_method.AsRegister<Register>(),
6557 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6558 // LR()
6559 __ blx(LR);
6560 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006561 }
6562
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006563 DCHECK(!IsLeafMethod());
6564}
6565
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006566void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6567 Register temp = temp_location.AsRegister<Register>();
6568 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6569 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006570
6571 // Use the calling convention instead of the location of the receiver, as
6572 // intrinsics may have put the receiver in a different register. In the intrinsics
6573 // slow path, the arguments have been moved to the right place, so here we are
6574 // guaranteed that the receiver is the first register of the calling convention.
6575 InvokeDexCallingConvention calling_convention;
6576 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006577 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006578 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006579 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006580 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006581 // Instead of simply (possibly) unpoisoning `temp` here, we should
6582 // emit a read barrier for the previous class reference load.
6583 // However this is not required in practice, as this is an
6584 // intermediate/temporary reference and because the current
6585 // concurrent copying collector keeps the from-space memory
6586 // intact/accessible until the end of the marking phase (the
6587 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006588 __ MaybeUnpoisonHeapReference(temp);
6589 // temp = temp->GetMethodAt(method_offset);
6590 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6591 kArmWordSize).Int32Value();
6592 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6593 // LR = temp->GetEntryPoint();
6594 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6595 // LR();
6596 __ blx(LR);
6597}
6598
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006599CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6600 const DexFile& dex_file, uint32_t string_index) {
6601 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6602}
6603
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006604CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6605 const DexFile& dex_file, uint32_t type_index) {
6606 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6607}
6608
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006609CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6610 const DexFile& dex_file, uint32_t element_offset) {
6611 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6612}
6613
6614CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6615 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6616 patches->emplace_back(dex_file, offset_or_index);
6617 return &patches->back();
6618}
6619
6620Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6621 uint32_t string_index) {
6622 return boot_image_string_patches_.GetOrCreate(
6623 StringReference(&dex_file, string_index),
6624 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6625}
6626
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006627Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6628 uint32_t type_index) {
6629 return boot_image_type_patches_.GetOrCreate(
6630 TypeReference(&dex_file, type_index),
6631 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6632}
6633
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006634Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6635 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6636 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6637 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6638}
6639
6640Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6641 return DeduplicateUint32Literal(address, &uint32_literals_);
6642}
6643
Vladimir Marko58155012015-08-19 12:49:41 +00006644void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6645 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006646 size_t size =
6647 method_patches_.size() +
6648 call_patches_.size() +
6649 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006650 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6651 boot_image_string_patches_.size() +
6652 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006653 boot_image_type_patches_.size() +
6654 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006655 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006656 linker_patches->reserve(size);
6657 for (const auto& entry : method_patches_) {
6658 const MethodReference& target_method = entry.first;
6659 Literal* literal = entry.second;
6660 DCHECK(literal->GetLabel()->IsBound());
6661 uint32_t literal_offset = literal->GetLabel()->Position();
6662 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6663 target_method.dex_file,
6664 target_method.dex_method_index));
6665 }
6666 for (const auto& entry : call_patches_) {
6667 const MethodReference& target_method = entry.first;
6668 Literal* literal = entry.second;
6669 DCHECK(literal->GetLabel()->IsBound());
6670 uint32_t literal_offset = literal->GetLabel()->Position();
6671 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6672 target_method.dex_file,
6673 target_method.dex_method_index));
6674 }
6675 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6676 uint32_t literal_offset = info.label.Position();
6677 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6678 info.target_method.dex_file,
6679 info.target_method.dex_method_index));
6680 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006681 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6682 const DexFile& dex_file = info.target_dex_file;
6683 size_t base_element_offset = info.offset_or_index;
6684 DCHECK(info.add_pc_label.IsBound());
6685 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006686 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006687 DCHECK(info.movw_label.IsBound());
6688 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006689 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6690 &dex_file,
6691 add_pc_offset,
6692 base_element_offset));
6693 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006694 DCHECK(info.movt_label.IsBound());
6695 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006696 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6697 &dex_file,
6698 add_pc_offset,
6699 base_element_offset));
6700 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006701 for (const auto& entry : boot_image_string_patches_) {
6702 const StringReference& target_string = entry.first;
6703 Literal* literal = entry.second;
6704 DCHECK(literal->GetLabel()->IsBound());
6705 uint32_t literal_offset = literal->GetLabel()->Position();
6706 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6707 target_string.dex_file,
6708 target_string.string_index));
6709 }
6710 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6711 const DexFile& dex_file = info.target_dex_file;
6712 uint32_t string_index = info.offset_or_index;
6713 DCHECK(info.add_pc_label.IsBound());
6714 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6715 // Add MOVW patch.
6716 DCHECK(info.movw_label.IsBound());
6717 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6718 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6719 &dex_file,
6720 add_pc_offset,
6721 string_index));
6722 // Add MOVT patch.
6723 DCHECK(info.movt_label.IsBound());
6724 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6725 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6726 &dex_file,
6727 add_pc_offset,
6728 string_index));
6729 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006730 for (const auto& entry : boot_image_type_patches_) {
6731 const TypeReference& target_type = entry.first;
6732 Literal* literal = entry.second;
6733 DCHECK(literal->GetLabel()->IsBound());
6734 uint32_t literal_offset = literal->GetLabel()->Position();
6735 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6736 target_type.dex_file,
6737 target_type.type_index));
6738 }
6739 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6740 const DexFile& dex_file = info.target_dex_file;
6741 uint32_t type_index = info.offset_or_index;
6742 DCHECK(info.add_pc_label.IsBound());
6743 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6744 // Add MOVW patch.
6745 DCHECK(info.movw_label.IsBound());
6746 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6747 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6748 &dex_file,
6749 add_pc_offset,
6750 type_index));
6751 // Add MOVT patch.
6752 DCHECK(info.movt_label.IsBound());
6753 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6754 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6755 &dex_file,
6756 add_pc_offset,
6757 type_index));
6758 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006759 for (const auto& entry : boot_image_address_patches_) {
6760 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6761 Literal* literal = entry.second;
6762 DCHECK(literal->GetLabel()->IsBound());
6763 uint32_t literal_offset = literal->GetLabel()->Position();
6764 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6765 }
6766}
6767
6768Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6769 return map->GetOrCreate(
6770 value,
6771 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006772}
6773
6774Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6775 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006776 return map->GetOrCreate(
6777 target_method,
6778 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006779}
6780
6781Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6782 return DeduplicateMethodLiteral(target_method, &method_patches_);
6783}
6784
6785Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6786 return DeduplicateMethodLiteral(target_method, &call_patches_);
6787}
6788
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006789void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6790 LocationSummary* locations =
6791 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6792 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6793 Location::RequiresRegister());
6794 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6795 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6796 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6797}
6798
6799void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6800 LocationSummary* locations = instr->GetLocations();
6801 Register res = locations->Out().AsRegister<Register>();
6802 Register accumulator =
6803 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6804 Register mul_left =
6805 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6806 Register mul_right =
6807 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6808
6809 if (instr->GetOpKind() == HInstruction::kAdd) {
6810 __ mla(res, mul_left, mul_right, accumulator);
6811 } else {
6812 __ mls(res, mul_left, mul_right, accumulator);
6813 }
6814}
6815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006816void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006817 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006818 LOG(FATAL) << "Unreachable";
6819}
6820
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006821void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006822 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006823 LOG(FATAL) << "Unreachable";
6824}
6825
Mark Mendellfe57faa2015-09-18 09:26:15 -04006826// Simple implementation of packed switch - generate cascaded compare/jumps.
6827void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6828 LocationSummary* locations =
6829 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6830 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006831 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006832 codegen_->GetAssembler()->IsThumb()) {
6833 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6834 if (switch_instr->GetStartValue() != 0) {
6835 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6836 }
6837 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006838}
6839
6840void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6841 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006842 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006843 LocationSummary* locations = switch_instr->GetLocations();
6844 Register value_reg = locations->InAt(0).AsRegister<Register>();
6845 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6846
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006847 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006848 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006849 Register temp_reg = IP;
6850 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6851 // the immediate, because IP is used as the destination register. For the other
6852 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6853 // and they can be encoded in the instruction without making use of IP register.
6854 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6855
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006856 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006857 // Jump to successors[0] if value == lower_bound.
6858 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6859 int32_t last_index = 0;
6860 for (; num_entries - last_index > 2; last_index += 2) {
6861 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6862 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6863 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6864 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6865 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6866 }
6867 if (num_entries - last_index == 2) {
6868 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006869 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006870 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006871 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006872
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006873 // And the default for any other value.
6874 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6875 __ b(codegen_->GetLabelOf(default_block));
6876 }
6877 } else {
6878 // Create a table lookup.
6879 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6880
6881 // Materialize a pointer to the switch table
6882 std::vector<Label*> labels(num_entries);
6883 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6884 for (uint32_t i = 0; i < num_entries; i++) {
6885 labels[i] = codegen_->GetLabelOf(successors[i]);
6886 }
6887 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6888
6889 // Remove the bias.
6890 Register key_reg;
6891 if (lower_bound != 0) {
6892 key_reg = locations->GetTemp(1).AsRegister<Register>();
6893 __ AddConstant(key_reg, value_reg, -lower_bound);
6894 } else {
6895 key_reg = value_reg;
6896 }
6897
6898 // Check whether the value is in the table, jump to default block if not.
6899 __ CmpConstant(key_reg, num_entries - 1);
6900 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6901
6902 // Load the displacement from the table.
6903 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6904
6905 // Dispatch is a direct add to the PC (for Thumb2).
6906 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006907 }
6908}
6909
Vladimir Markob4536b72015-11-24 13:45:23 +00006910void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6911 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6912 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00006913}
6914
6915void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6916 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006917 CodeGeneratorARM::PcRelativePatchInfo* labels =
6918 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00006919 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006920 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00006921 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006922 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00006923 __ BindTrackedLabel(&labels->add_pc_label);
6924 __ add(base_reg, base_reg, ShifterOperand(PC));
6925}
6926
Andreas Gampe85b62f22015-09-09 13:15:38 -07006927void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6928 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006929 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006930 return;
6931 }
6932
6933 DCHECK_NE(type, Primitive::kPrimVoid);
6934
6935 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6936 if (return_loc.Equals(trg)) {
6937 return;
6938 }
6939
6940 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6941 // with the last branch.
6942 if (type == Primitive::kPrimLong) {
6943 HParallelMove parallel_move(GetGraph()->GetArena());
6944 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6945 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6946 GetMoveResolver()->EmitNativeCode(&parallel_move);
6947 } else if (type == Primitive::kPrimDouble) {
6948 HParallelMove parallel_move(GetGraph()->GetArena());
6949 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6950 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6951 GetMoveResolver()->EmitNativeCode(&parallel_move);
6952 } else {
6953 // Let the parallel move resolver take care of all of this.
6954 HParallelMove parallel_move(GetGraph()->GetArena());
6955 parallel_move.AddMove(return_loc, trg, type, nullptr);
6956 GetMoveResolver()->EmitNativeCode(&parallel_move);
6957 }
6958}
6959
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006960void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
6961 LocationSummary* locations =
6962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6963 locations->SetInAt(0, Location::RequiresRegister());
6964 locations->SetOut(Location::RequiresRegister());
6965}
6966
6967void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
6968 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006969 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006970 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006971 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006972 __ LoadFromOffset(kLoadWord,
6973 locations->Out().AsRegister<Register>(),
6974 locations->InAt(0).AsRegister<Register>(),
6975 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006976 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006977 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006978 instruction->GetIndex() % ImTable::kSize, kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006979 __ LoadFromOffset(kLoadWord,
6980 locations->Out().AsRegister<Register>(),
6981 locations->InAt(0).AsRegister<Register>(),
6982 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
6983 __ LoadFromOffset(kLoadWord,
6984 locations->Out().AsRegister<Register>(),
6985 locations->Out().AsRegister<Register>(),
6986 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006987 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006988}
6989
Roland Levillain4d027112015-07-01 15:41:14 +01006990#undef __
6991#undef QUICK_ENTRY_POINT
6992
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006993} // namespace arm
6994} // namespace art