blob: 395db32f93a768478b7e54a0efb274023835dbd5 [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 Levillain02b75802016-07-13 11:54:35 +0100440 // Save live registers before the runtime call, and in particular
441 // R0 (if it is live), as it is clobbered by functions
442 // art_quick_read_barrier_mark_regX.
Roland Levillainc9285912015-12-18 10:38:42 +0000443 SaveLiveRegisters(codegen, locations);
444
445 InvokeRuntimeCallingConvention calling_convention;
446 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100447 DCHECK_NE(reg, SP);
448 DCHECK_NE(reg, LR);
449 DCHECK_NE(reg, PC);
450 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
451 // "Compact" slow path, saving two moves.
452 //
453 // Instead of using the standard runtime calling convention (input
454 // and output in R0):
455 //
456 // R0 <- obj
457 // R0 <- ReadBarrierMark(R0)
458 // obj <- R0
459 //
460 // we just use rX (the register holding `obj`) as input and output
461 // of a dedicated entrypoint:
462 //
463 // rX <- ReadBarrierMarkRegX(rX)
464 //
465 int32_t entry_point_offset =
466 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmWordSize>(reg);
467 // TODO: Do not emit a stack map for this runtime call.
468 arm_codegen->InvokeRuntime(entry_point_offset,
Roland Levillainc9285912015-12-18 10:38:42 +0000469 instruction_,
470 instruction_->GetDexPc(),
471 this);
Roland Levillainc9285912015-12-18 10:38:42 +0000472
473 RestoreLiveRegisters(codegen, locations);
474 __ b(GetExitLabel());
475 }
476
477 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000478 const Location obj_;
479
480 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
481};
482
Roland Levillain3b359c72015-11-17 19:35:12 +0000483// Slow path generating a read barrier for a heap reference.
484class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
485 public:
486 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
487 Location out,
488 Location ref,
489 Location obj,
490 uint32_t offset,
491 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000492 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000493 out_(out),
494 ref_(ref),
495 obj_(obj),
496 offset_(offset),
497 index_(index) {
498 DCHECK(kEmitCompilerReadBarrier);
499 // If `obj` is equal to `out` or `ref`, it means the initial object
500 // has been overwritten by (or after) the heap object reference load
501 // to be instrumented, e.g.:
502 //
503 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000504 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000505 //
506 // In that case, we have lost the information about the original
507 // object, and the emitted read barrier cannot work properly.
508 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
509 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
510 }
511
512 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
513 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
514 LocationSummary* locations = instruction_->GetLocations();
515 Register reg_out = out_.AsRegister<Register>();
516 DCHECK(locations->CanCall());
517 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100518 DCHECK(instruction_->IsInstanceFieldGet() ||
519 instruction_->IsStaticFieldGet() ||
520 instruction_->IsArrayGet() ||
521 instruction_->IsInstanceOf() ||
522 instruction_->IsCheckCast() ||
523 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillainc9285912015-12-18 10:38:42 +0000524 instruction_->GetLocations()->Intrinsified()))
525 << "Unexpected instruction in read barrier for heap reference slow path: "
526 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000527
528 __ Bind(GetEntryLabel());
529 SaveLiveRegisters(codegen, locations);
530
531 // We may have to change the index's value, but as `index_` is a
532 // constant member (like other "inputs" of this slow path),
533 // introduce a copy of it, `index`.
534 Location index = index_;
535 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100536 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000537 if (instruction_->IsArrayGet()) {
538 // Compute the actual memory offset and store it in `index`.
539 Register index_reg = index_.AsRegister<Register>();
540 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
541 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
542 // We are about to change the value of `index_reg` (see the
543 // calls to art::arm::Thumb2Assembler::Lsl and
544 // art::arm::Thumb2Assembler::AddConstant below), but it has
545 // not been saved by the previous call to
546 // art::SlowPathCode::SaveLiveRegisters, as it is a
547 // callee-save register --
548 // art::SlowPathCode::SaveLiveRegisters does not consider
549 // callee-save registers, as it has been designed with the
550 // assumption that callee-save registers are supposed to be
551 // handled by the called function. So, as a callee-save
552 // register, `index_reg` _would_ eventually be saved onto
553 // the stack, but it would be too late: we would have
554 // changed its value earlier. Therefore, we manually save
555 // it here into another freely available register,
556 // `free_reg`, chosen of course among the caller-save
557 // registers (as a callee-save `free_reg` register would
558 // exhibit the same problem).
559 //
560 // Note we could have requested a temporary register from
561 // the register allocator instead; but we prefer not to, as
562 // this is a slow path, and we know we can find a
563 // caller-save register that is available.
564 Register free_reg = FindAvailableCallerSaveRegister(codegen);
565 __ Mov(free_reg, index_reg);
566 index_reg = free_reg;
567 index = Location::RegisterLocation(index_reg);
568 } else {
569 // The initial register stored in `index_` has already been
570 // saved in the call to art::SlowPathCode::SaveLiveRegisters
571 // (as it is not a callee-save register), so we can freely
572 // use it.
573 }
574 // Shifting the index value contained in `index_reg` by the scale
575 // factor (2) cannot overflow in practice, as the runtime is
576 // unable to allocate object arrays with a size larger than
577 // 2^26 - 1 (that is, 2^28 - 4 bytes).
578 __ Lsl(index_reg, index_reg, TIMES_4);
579 static_assert(
580 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
581 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
582 __ AddConstant(index_reg, index_reg, offset_);
583 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100584 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
585 // intrinsics, `index_` is not shifted by a scale factor of 2
586 // (as in the case of ArrayGet), as it is actually an offset
587 // to an object field within an object.
588 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000589 DCHECK(instruction_->GetLocations()->Intrinsified());
590 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
591 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
592 << instruction_->AsInvoke()->GetIntrinsic();
593 DCHECK_EQ(offset_, 0U);
594 DCHECK(index_.IsRegisterPair());
595 // UnsafeGet's offset location is a register pair, the low
596 // part contains the correct offset.
597 index = index_.ToLow();
598 }
599 }
600
601 // We're moving two or three locations to locations that could
602 // overlap, so we need a parallel move resolver.
603 InvokeRuntimeCallingConvention calling_convention;
604 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
605 parallel_move.AddMove(ref_,
606 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
607 Primitive::kPrimNot,
608 nullptr);
609 parallel_move.AddMove(obj_,
610 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
611 Primitive::kPrimNot,
612 nullptr);
613 if (index.IsValid()) {
614 parallel_move.AddMove(index,
615 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
616 Primitive::kPrimInt,
617 nullptr);
618 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
619 } else {
620 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
621 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
622 }
623 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
624 instruction_,
625 instruction_->GetDexPc(),
626 this);
627 CheckEntrypointTypes<
628 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
629 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
630
631 RestoreLiveRegisters(codegen, locations);
632 __ b(GetExitLabel());
633 }
634
635 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
636
637 private:
638 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
639 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
640 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
641 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
642 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
643 return static_cast<Register>(i);
644 }
645 }
646 // We shall never fail to find a free caller-save register, as
647 // there are more than two core caller-save registers on ARM
648 // (meaning it is possible to find one which is different from
649 // `ref` and `obj`).
650 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
651 LOG(FATAL) << "Could not find a free caller-save register";
652 UNREACHABLE();
653 }
654
Roland Levillain3b359c72015-11-17 19:35:12 +0000655 const Location out_;
656 const Location ref_;
657 const Location obj_;
658 const uint32_t offset_;
659 // An additional location containing an index to an array.
660 // Only used for HArrayGet and the UnsafeGetObject &
661 // UnsafeGetObjectVolatile intrinsics.
662 const Location index_;
663
664 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
665};
666
667// Slow path generating a read barrier for a GC root.
668class ReadBarrierForRootSlowPathARM : public SlowPathCode {
669 public:
670 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000671 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000672 DCHECK(kEmitCompilerReadBarrier);
673 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000674
675 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
676 LocationSummary* locations = instruction_->GetLocations();
677 Register reg_out = out_.AsRegister<Register>();
678 DCHECK(locations->CanCall());
679 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000680 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
681 << "Unexpected instruction in read barrier for GC root slow path: "
682 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000683
684 __ Bind(GetEntryLabel());
685 SaveLiveRegisters(codegen, locations);
686
687 InvokeRuntimeCallingConvention calling_convention;
688 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
689 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
690 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
691 instruction_,
692 instruction_->GetDexPc(),
693 this);
694 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
695 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
696
697 RestoreLiveRegisters(codegen, locations);
698 __ b(GetExitLabel());
699 }
700
701 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
702
703 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000704 const Location out_;
705 const Location root_;
706
707 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
708};
709
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000710#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700711// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
712#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700713
Aart Bike9f37602015-10-09 11:15:55 -0700714inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700715 switch (cond) {
716 case kCondEQ: return EQ;
717 case kCondNE: return NE;
718 case kCondLT: return LT;
719 case kCondLE: return LE;
720 case kCondGT: return GT;
721 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700722 case kCondB: return LO;
723 case kCondBE: return LS;
724 case kCondA: return HI;
725 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700726 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727 LOG(FATAL) << "Unreachable";
728 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700729}
730
Aart Bike9f37602015-10-09 11:15:55 -0700731// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100732inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700733 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100734 case kCondEQ: return EQ;
735 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700736 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100737 case kCondLT: return LO;
738 case kCondLE: return LS;
739 case kCondGT: return HI;
740 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700741 // Unsigned remain unchanged.
742 case kCondB: return LO;
743 case kCondBE: return LS;
744 case kCondA: return HI;
745 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700746 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100747 LOG(FATAL) << "Unreachable";
748 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700749}
750
Vladimir Markod6e069b2016-01-18 11:11:01 +0000751inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
752 // The ARM condition codes can express all the necessary branches, see the
753 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
754 // There is no dex instruction or HIR that would need the missing conditions
755 // "equal or unordered" or "not equal".
756 switch (cond) {
757 case kCondEQ: return EQ;
758 case kCondNE: return NE /* unordered */;
759 case kCondLT: return gt_bias ? CC : LT /* unordered */;
760 case kCondLE: return gt_bias ? LS : LE /* unordered */;
761 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
762 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
763 default:
764 LOG(FATAL) << "UNREACHABLE";
765 UNREACHABLE();
766 }
767}
768
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100769void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100770 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100771}
772
773void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100774 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100775}
776
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100777size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
778 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
779 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100780}
781
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100782size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
783 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
784 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100785}
786
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000787size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
788 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
789 return kArmWordSize;
790}
791
792size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
793 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
794 return kArmWordSize;
795}
796
Calin Juravle34166012014-12-19 17:22:29 +0000797CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000798 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100799 const CompilerOptions& compiler_options,
800 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000801 : CodeGenerator(graph,
802 kNumberOfCoreRegisters,
803 kNumberOfSRegisters,
804 kNumberOfRegisterPairs,
805 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
806 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000807 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
808 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100809 compiler_options,
810 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100811 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100812 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100813 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100814 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100815 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000816 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000817 uint32_literals_(std::less<uint32_t>(),
818 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100819 method_patches_(MethodReferenceComparator(),
820 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
821 call_patches_(MethodReferenceComparator(),
822 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000823 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000824 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
825 boot_image_string_patches_(StringReferenceValueComparator(),
826 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
827 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100828 boot_image_type_patches_(TypeReferenceValueComparator(),
829 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
830 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000831 boot_image_address_patches_(std::less<uint32_t>(),
832 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700833 // Always save the LR register to mimic Quick.
834 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100835}
836
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000837void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
838 // Ensure that we fix up branches and literal loads and emit the literal pool.
839 __ FinalizeCode();
840
841 // Adjust native pc offsets in stack maps.
842 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
843 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
844 uint32_t new_position = __ GetAdjustedPosition(old_position);
845 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
846 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100847 // Adjust pc offsets for the disassembly information.
848 if (disasm_info_ != nullptr) {
849 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
850 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
851 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
852 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
853 it.second.start = __ GetAdjustedPosition(it.second.start);
854 it.second.end = __ GetAdjustedPosition(it.second.end);
855 }
856 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
857 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
858 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
859 }
860 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000861
862 CodeGenerator::Finalize(allocator);
863}
864
David Brazdil58282f42016-01-14 12:45:10 +0000865void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100867 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868
869 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100870 blocked_core_registers_[SP] = true;
871 blocked_core_registers_[LR] = true;
872 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100873
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100874 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100875 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100876
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100877 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100878 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100879
David Brazdil58282f42016-01-14 12:45:10 +0000880 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100881 // Stubs do not save callee-save floating point registers. If the graph
882 // is debuggable, we need to deal with these registers differently. For
883 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000884 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
885 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
886 }
887 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100888
889 UpdateBlockedPairRegisters();
890}
891
892void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
893 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
894 ArmManagedRegister current =
895 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
896 if (blocked_core_registers_[current.AsRegisterPairLow()]
897 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
898 blocked_register_pairs_[i] = true;
899 }
900 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100901}
902
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100903InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800904 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100905 assembler_(codegen->GetAssembler()),
906 codegen_(codegen) {}
907
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000908void CodeGeneratorARM::ComputeSpillMask() {
909 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
910 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000911 // There is no easy instruction to restore just the PC on thumb2. We spill and
912 // restore another arbitrary register.
913 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000914 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
915 // We use vpush and vpop for saving and restoring floating point registers, which take
916 // a SRegister and the number of registers to save/restore after that SRegister. We
917 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
918 // but in the range.
919 if (fpu_spill_mask_ != 0) {
920 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
921 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
922 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
923 fpu_spill_mask_ |= (1 << i);
924 }
925 }
926}
927
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100928static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100929 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930}
931
932static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100933 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100934}
935
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000936void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000937 bool skip_overflow_check =
938 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000939 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000940 __ Bind(&frame_entry_label_);
941
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000942 if (HasEmptyFrame()) {
943 return;
944 }
945
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100946 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000947 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
948 __ LoadFromOffset(kLoadWord, IP, IP, 0);
949 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100950 }
951
Andreas Gampe501fd632015-09-10 16:11:06 -0700952 __ PushList(core_spill_mask_);
953 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
954 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000955 if (fpu_spill_mask_ != 0) {
956 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
957 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100958 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100959 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000960 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100961 int adjust = GetFrameSize() - FrameEntrySpillSize();
962 __ AddConstant(SP, -adjust);
963 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100964 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000965}
966
967void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000968 if (HasEmptyFrame()) {
969 __ bx(LR);
970 return;
971 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100972 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100973 int adjust = GetFrameSize() - FrameEntrySpillSize();
974 __ AddConstant(SP, adjust);
975 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000976 if (fpu_spill_mask_ != 0) {
977 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
978 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100979 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
980 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000981 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700982 // Pop LR into PC to return.
983 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
984 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
985 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100986 __ cfi().RestoreState();
987 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000988}
989
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100990void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700991 Label* label = GetLabelOf(block);
992 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000993}
994
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100995Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100996 switch (type) {
997 case Primitive::kPrimBoolean:
998 case Primitive::kPrimByte:
999 case Primitive::kPrimChar:
1000 case Primitive::kPrimShort:
1001 case Primitive::kPrimInt:
1002 case Primitive::kPrimNot: {
1003 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001004 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001006 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001008 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001009 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001010 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001011
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001012 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001014 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001015 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001016 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001017 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001018 if (calling_convention.GetRegisterAt(index) == R1) {
1019 // Skip R1, and use R2_R3 instead.
1020 gp_index_++;
1021 index++;
1022 }
1023 }
1024 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1025 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001026 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001027
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001028 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001029 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001030 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001031 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1032 }
1033 }
1034
1035 case Primitive::kPrimFloat: {
1036 uint32_t stack_index = stack_index_++;
1037 if (float_index_ % 2 == 0) {
1038 float_index_ = std::max(double_index_, float_index_);
1039 }
1040 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1041 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1042 } else {
1043 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1044 }
1045 }
1046
1047 case Primitive::kPrimDouble: {
1048 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1049 uint32_t stack_index = stack_index_;
1050 stack_index_ += 2;
1051 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1052 uint32_t index = double_index_;
1053 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001054 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001055 calling_convention.GetFpuRegisterAt(index),
1056 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001057 DCHECK(ExpectedPairLayout(result));
1058 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001059 } else {
1060 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001061 }
1062 }
1063
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001064 case Primitive::kPrimVoid:
1065 LOG(FATAL) << "Unexpected parameter type " << type;
1066 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001067 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001068 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001069}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001070
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001071Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001072 switch (type) {
1073 case Primitive::kPrimBoolean:
1074 case Primitive::kPrimByte:
1075 case Primitive::kPrimChar:
1076 case Primitive::kPrimShort:
1077 case Primitive::kPrimInt:
1078 case Primitive::kPrimNot: {
1079 return Location::RegisterLocation(R0);
1080 }
1081
1082 case Primitive::kPrimFloat: {
1083 return Location::FpuRegisterLocation(S0);
1084 }
1085
1086 case Primitive::kPrimLong: {
1087 return Location::RegisterPairLocation(R0, R1);
1088 }
1089
1090 case Primitive::kPrimDouble: {
1091 return Location::FpuRegisterPairLocation(S0, S1);
1092 }
1093
1094 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001095 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001096 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001097
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001098 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001099}
1100
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001101Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1102 return Location::RegisterLocation(kMethodRegisterArgument);
1103}
1104
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105void CodeGeneratorARM::Move32(Location destination, Location source) {
1106 if (source.Equals(destination)) {
1107 return;
1108 }
1109 if (destination.IsRegister()) {
1110 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001113 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001115 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001116 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 } else if (destination.IsFpuRegister()) {
1118 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001119 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001120 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001121 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001123 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001124 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001126 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001128 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001129 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001130 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001131 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001132 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001133 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1134 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 }
1136 }
1137}
1138
1139void CodeGeneratorARM::Move64(Location destination, Location source) {
1140 if (source.Equals(destination)) {
1141 return;
1142 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001143 if (destination.IsRegisterPair()) {
1144 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001145 EmitParallelMoves(
1146 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1147 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001148 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001149 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001150 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1151 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001152 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001153 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001154 } else if (source.IsFpuRegisterPair()) {
1155 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1156 destination.AsRegisterPairHigh<Register>(),
1157 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 } else {
1159 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001160 DCHECK(ExpectedPairLayout(destination));
1161 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1162 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001163 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001164 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001166 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1167 SP,
1168 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001169 } else if (source.IsRegisterPair()) {
1170 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1171 source.AsRegisterPairLow<Register>(),
1172 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001173 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001174 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001175 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001176 } else {
1177 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001179 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 if (source.AsRegisterPairLow<Register>() == R1) {
1181 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1183 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001184 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001185 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001186 SP, destination.GetStackIndex());
1187 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001188 } else if (source.IsFpuRegisterPair()) {
1189 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1190 SP,
1191 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001192 } else {
1193 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001194 EmitParallelMoves(
1195 Location::StackSlot(source.GetStackIndex()),
1196 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001197 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001198 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001199 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1200 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001201 }
1202 }
1203}
1204
Calin Juravle175dc732015-08-25 15:42:32 +01001205void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1206 DCHECK(location.IsRegister());
1207 __ LoadImmediate(location.AsRegister<Register>(), value);
1208}
1209
Calin Juravlee460d1d2015-09-29 04:52:17 +01001210void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001211 HParallelMove move(GetGraph()->GetArena());
1212 move.AddMove(src, dst, dst_type, nullptr);
1213 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001214}
1215
1216void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1217 if (location.IsRegister()) {
1218 locations->AddTemp(location);
1219 } else if (location.IsRegisterPair()) {
1220 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1221 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1222 } else {
1223 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1224 }
1225}
1226
Calin Juravle175dc732015-08-25 15:42:32 +01001227void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1228 HInstruction* instruction,
1229 uint32_t dex_pc,
1230 SlowPathCode* slow_path) {
1231 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1232 instruction,
1233 dex_pc,
1234 slow_path);
1235}
1236
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001237void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1238 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001239 uint32_t dex_pc,
1240 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001241 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001242 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1243 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001244 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001245}
1246
David Brazdilfc6a86a2015-06-26 10:33:45 +00001247void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001248 DCHECK(!successor->IsExitBlock());
1249
1250 HBasicBlock* block = got->GetBlock();
1251 HInstruction* previous = got->GetPrevious();
1252
1253 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001254 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001255 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1256 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1257 return;
1258 }
1259
1260 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1261 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1262 }
1263 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001264 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001265 }
1266}
1267
David Brazdilfc6a86a2015-06-26 10:33:45 +00001268void LocationsBuilderARM::VisitGoto(HGoto* got) {
1269 got->SetLocations(nullptr);
1270}
1271
1272void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1273 HandleGoto(got, got->GetSuccessor());
1274}
1275
1276void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1277 try_boundary->SetLocations(nullptr);
1278}
1279
1280void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1281 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1282 if (!successor->IsExitBlock()) {
1283 HandleGoto(try_boundary, successor);
1284 }
1285}
1286
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001287void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001288 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001289}
1290
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001291void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001292}
1293
Roland Levillain4fa13f62015-07-06 18:11:54 +01001294void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1295 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001296 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001297 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001298 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001299}
1300
1301void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1302 Label* true_label,
1303 Label* false_label) {
1304 LocationSummary* locations = cond->GetLocations();
1305 Location left = locations->InAt(0);
1306 Location right = locations->InAt(1);
1307 IfCondition if_cond = cond->GetCondition();
1308
1309 Register left_high = left.AsRegisterPairHigh<Register>();
1310 Register left_low = left.AsRegisterPairLow<Register>();
1311 IfCondition true_high_cond = if_cond;
1312 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001313 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001314
1315 // Set the conditions for the test, remembering that == needs to be
1316 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001317 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001318 switch (if_cond) {
1319 case kCondEQ:
1320 case kCondNE:
1321 // Nothing to do.
1322 break;
1323 case kCondLT:
1324 false_high_cond = kCondGT;
1325 break;
1326 case kCondLE:
1327 true_high_cond = kCondLT;
1328 break;
1329 case kCondGT:
1330 false_high_cond = kCondLT;
1331 break;
1332 case kCondGE:
1333 true_high_cond = kCondGT;
1334 break;
Aart Bike9f37602015-10-09 11:15:55 -07001335 case kCondB:
1336 false_high_cond = kCondA;
1337 break;
1338 case kCondBE:
1339 true_high_cond = kCondB;
1340 break;
1341 case kCondA:
1342 false_high_cond = kCondB;
1343 break;
1344 case kCondAE:
1345 true_high_cond = kCondA;
1346 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001347 }
1348 if (right.IsConstant()) {
1349 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1350 int32_t val_low = Low32Bits(value);
1351 int32_t val_high = High32Bits(value);
1352
Vladimir Markoac6ac102015-12-17 12:14:00 +00001353 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001355 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001356 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001357 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001358 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001359 __ b(true_label, ARMCondition(true_high_cond));
1360 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001361 }
1362 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001363 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001364 } else {
1365 Register right_high = right.AsRegisterPairHigh<Register>();
1366 Register right_low = right.AsRegisterPairLow<Register>();
1367
1368 __ cmp(left_high, ShifterOperand(right_high));
1369 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001370 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001371 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001372 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001373 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001374 __ b(true_label, ARMCondition(true_high_cond));
1375 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001376 }
1377 // Must be equal high, so compare the lows.
1378 __ cmp(left_low, ShifterOperand(right_low));
1379 }
1380 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001381 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001382 __ b(true_label, final_condition);
1383}
1384
David Brazdil0debae72015-11-12 18:37:00 +00001385void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1386 Label* true_target_in,
1387 Label* false_target_in) {
1388 // Generated branching requires both targets to be explicit. If either of the
1389 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1390 Label fallthrough_target;
1391 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1392 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1393
Roland Levillain4fa13f62015-07-06 18:11:54 +01001394 LocationSummary* locations = condition->GetLocations();
1395 Location left = locations->InAt(0);
1396 Location right = locations->InAt(1);
1397
Roland Levillain4fa13f62015-07-06 18:11:54 +01001398 Primitive::Type type = condition->InputAt(0)->GetType();
1399 switch (type) {
1400 case Primitive::kPrimLong:
1401 GenerateLongComparesAndJumps(condition, true_target, false_target);
1402 break;
1403 case Primitive::kPrimFloat:
1404 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1405 GenerateFPJumps(condition, true_target, false_target);
1406 break;
1407 case Primitive::kPrimDouble:
1408 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1409 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1410 GenerateFPJumps(condition, true_target, false_target);
1411 break;
1412 default:
1413 LOG(FATAL) << "Unexpected compare type " << type;
1414 }
1415
David Brazdil0debae72015-11-12 18:37:00 +00001416 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001417 __ b(false_target);
1418 }
David Brazdil0debae72015-11-12 18:37:00 +00001419
1420 if (fallthrough_target.IsLinked()) {
1421 __ Bind(&fallthrough_target);
1422 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001423}
1424
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001425void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001426 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001427 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001428 Label* false_target) {
1429 HInstruction* cond = instruction->InputAt(condition_input_index);
1430
1431 if (true_target == nullptr && false_target == nullptr) {
1432 // Nothing to do. The code always falls through.
1433 return;
1434 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001435 // Constant condition, statically compared against "true" (integer value 1).
1436 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001437 if (true_target != nullptr) {
1438 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001439 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001440 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001441 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001442 if (false_target != nullptr) {
1443 __ b(false_target);
1444 }
1445 }
1446 return;
1447 }
1448
1449 // The following code generates these patterns:
1450 // (1) true_target == nullptr && false_target != nullptr
1451 // - opposite condition true => branch to false_target
1452 // (2) true_target != nullptr && false_target == nullptr
1453 // - condition true => branch to true_target
1454 // (3) true_target != nullptr && false_target != nullptr
1455 // - condition true => branch to true_target
1456 // - branch to false_target
1457 if (IsBooleanValueOrMaterializedCondition(cond)) {
1458 // Condition has been materialized, compare the output to 0.
1459 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1460 DCHECK(cond_val.IsRegister());
1461 if (true_target == nullptr) {
1462 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1463 } else {
1464 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001465 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001466 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001467 // Condition has not been materialized. Use its inputs as the comparison and
1468 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001469 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001470
1471 // If this is a long or FP comparison that has been folded into
1472 // the HCondition, generate the comparison directly.
1473 Primitive::Type type = condition->InputAt(0)->GetType();
1474 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1475 GenerateCompareTestAndBranch(condition, true_target, false_target);
1476 return;
1477 }
1478
1479 LocationSummary* locations = cond->GetLocations();
1480 DCHECK(locations->InAt(0).IsRegister());
1481 Register left = locations->InAt(0).AsRegister<Register>();
1482 Location right = locations->InAt(1);
1483 if (right.IsRegister()) {
1484 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001485 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001486 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001487 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001488 }
1489 if (true_target == nullptr) {
1490 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1491 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001492 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001493 }
Dave Allison20dfc792014-06-16 20:44:29 -07001494 }
David Brazdil0debae72015-11-12 18:37:00 +00001495
1496 // If neither branch falls through (case 3), the conditional branch to `true_target`
1497 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1498 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001499 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001500 }
1501}
1502
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001503void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001504 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1505 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001506 locations->SetInAt(0, Location::RequiresRegister());
1507 }
1508}
1509
1510void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001511 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1512 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1513 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1514 nullptr : codegen_->GetLabelOf(true_successor);
1515 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1516 nullptr : codegen_->GetLabelOf(false_successor);
1517 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001518}
1519
1520void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1521 LocationSummary* locations = new (GetGraph()->GetArena())
1522 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001523 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001524 locations->SetInAt(0, Location::RequiresRegister());
1525 }
1526}
1527
1528void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001529 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001530 GenerateTestAndBranch(deoptimize,
1531 /* condition_input_index */ 0,
1532 slow_path->GetEntryLabel(),
1533 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001534}
Dave Allison20dfc792014-06-16 20:44:29 -07001535
David Brazdil74eb1b22015-12-14 11:44:01 +00001536void LocationsBuilderARM::VisitSelect(HSelect* select) {
1537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1538 if (Primitive::IsFloatingPointType(select->GetType())) {
1539 locations->SetInAt(0, Location::RequiresFpuRegister());
1540 locations->SetInAt(1, Location::RequiresFpuRegister());
1541 } else {
1542 locations->SetInAt(0, Location::RequiresRegister());
1543 locations->SetInAt(1, Location::RequiresRegister());
1544 }
1545 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1546 locations->SetInAt(2, Location::RequiresRegister());
1547 }
1548 locations->SetOut(Location::SameAsFirstInput());
1549}
1550
1551void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1552 LocationSummary* locations = select->GetLocations();
1553 Label false_target;
1554 GenerateTestAndBranch(select,
1555 /* condition_input_index */ 2,
1556 /* true_target */ nullptr,
1557 &false_target);
1558 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1559 __ Bind(&false_target);
1560}
1561
David Srbecky0cf44932015-12-09 14:09:59 +00001562void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1563 new (GetGraph()->GetArena()) LocationSummary(info);
1564}
1565
David Srbeckyd28f4a02016-03-14 17:14:24 +00001566void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1567 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001568}
1569
1570void CodeGeneratorARM::GenerateNop() {
1571 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001572}
1573
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001574void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001575 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001576 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001577 // Handle the long/FP comparisons made in instruction simplification.
1578 switch (cond->InputAt(0)->GetType()) {
1579 case Primitive::kPrimLong:
1580 locations->SetInAt(0, Location::RequiresRegister());
1581 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001582 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001583 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1584 }
1585 break;
1586
1587 case Primitive::kPrimFloat:
1588 case Primitive::kPrimDouble:
1589 locations->SetInAt(0, Location::RequiresFpuRegister());
1590 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001591 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1593 }
1594 break;
1595
1596 default:
1597 locations->SetInAt(0, Location::RequiresRegister());
1598 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001599 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1601 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001602 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001603}
1604
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001605void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001606 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001607 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001608 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001609
1610 LocationSummary* locations = cond->GetLocations();
1611 Location left = locations->InAt(0);
1612 Location right = locations->InAt(1);
1613 Register out = locations->Out().AsRegister<Register>();
1614 Label true_label, false_label;
1615
1616 switch (cond->InputAt(0)->GetType()) {
1617 default: {
1618 // Integer case.
1619 if (right.IsRegister()) {
1620 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1621 } else {
1622 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001623 __ CmpConstant(left.AsRegister<Register>(),
1624 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001625 }
Aart Bike9f37602015-10-09 11:15:55 -07001626 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001627 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001628 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001629 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001630 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001631 return;
1632 }
1633 case Primitive::kPrimLong:
1634 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1635 break;
1636 case Primitive::kPrimFloat:
1637 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1638 GenerateFPJumps(cond, &true_label, &false_label);
1639 break;
1640 case Primitive::kPrimDouble:
1641 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1642 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1643 GenerateFPJumps(cond, &true_label, &false_label);
1644 break;
1645 }
1646
1647 // Convert the jumps into the result.
1648 Label done_label;
1649
1650 // False case: result = 0.
1651 __ Bind(&false_label);
1652 __ LoadImmediate(out, 0);
1653 __ b(&done_label);
1654
1655 // True case: result = 1.
1656 __ Bind(&true_label);
1657 __ LoadImmediate(out, 1);
1658 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001659}
1660
1661void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001662 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001663}
1664
1665void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001666 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001667}
1668
1669void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001670 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001671}
1672
1673void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001674 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001675}
1676
1677void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001678 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001679}
1680
1681void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001682 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001683}
1684
1685void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001686 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001687}
1688
1689void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001690 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001691}
1692
1693void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001694 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001695}
1696
1697void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001698 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001699}
1700
1701void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001703}
1704
1705void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001706 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001707}
1708
Aart Bike9f37602015-10-09 11:15:55 -07001709void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001710 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001711}
1712
1713void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001715}
1716
1717void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001719}
1720
1721void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001723}
1724
1725void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001727}
1728
1729void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001731}
1732
1733void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001735}
1736
1737void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001739}
1740
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001741void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001742 LocationSummary* locations =
1743 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001744 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001745}
1746
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001747void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001748 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001749}
1750
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001751void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1752 LocationSummary* locations =
1753 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1754 locations->SetOut(Location::ConstantLocation(constant));
1755}
1756
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001757void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001758 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001759}
1760
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001761void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001762 LocationSummary* locations =
1763 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001764 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001765}
1766
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001767void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001768 // Will be generated at use site.
1769}
1770
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001771void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1772 LocationSummary* locations =
1773 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1774 locations->SetOut(Location::ConstantLocation(constant));
1775}
1776
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001777void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001778 // Will be generated at use site.
1779}
1780
1781void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1782 LocationSummary* locations =
1783 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1784 locations->SetOut(Location::ConstantLocation(constant));
1785}
1786
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001787void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001788 // Will be generated at use site.
1789}
1790
Calin Juravle27df7582015-04-17 19:12:31 +01001791void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1792 memory_barrier->SetLocations(nullptr);
1793}
1794
1795void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001796 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001797}
1798
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001799void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001800 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801}
1802
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001803void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001804 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001805}
1806
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001808 LocationSummary* locations =
1809 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001810 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001811}
1812
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001813void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001814 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001815}
1816
Calin Juravle175dc732015-08-25 15:42:32 +01001817void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1818 // The trampoline uses the same calling convention as dex calling conventions,
1819 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1820 // the method_idx.
1821 HandleInvoke(invoke);
1822}
1823
1824void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1825 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1826}
1827
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001828void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001829 // Explicit clinit checks triggered by static invokes must have been pruned by
1830 // art::PrepareForRegisterAllocation.
1831 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001832
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001833 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001834 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001835 codegen_->GetInstructionSetFeatures());
1836 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001837 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1838 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1839 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001840 return;
1841 }
1842
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001843 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001844
1845 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1846 if (invoke->HasPcRelativeDexCache()) {
1847 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1848 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001849}
1850
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001851static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1852 if (invoke->GetLocations()->Intrinsified()) {
1853 IntrinsicCodeGeneratorARM intrinsic(codegen);
1854 intrinsic.Dispatch(invoke);
1855 return true;
1856 }
1857 return false;
1858}
1859
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001860void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001861 // Explicit clinit checks triggered by static invokes must have been pruned by
1862 // art::PrepareForRegisterAllocation.
1863 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001864
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001865 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1866 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001867 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001868
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001869 LocationSummary* locations = invoke->GetLocations();
1870 codegen_->GenerateStaticOrDirectCall(
1871 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001872 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001873}
1874
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001875void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001876 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001877 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001878}
1879
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001880void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001881 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001882 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001883 codegen_->GetInstructionSetFeatures());
1884 if (intrinsic.TryDispatch(invoke)) {
1885 return;
1886 }
1887
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001888 HandleInvoke(invoke);
1889}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001890
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001891void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001892 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1893 return;
1894 }
1895
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001896 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001897 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001898 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001899}
1900
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001901void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1902 HandleInvoke(invoke);
1903 // Add the hidden argument.
1904 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1905}
1906
1907void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1908 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001909 LocationSummary* locations = invoke->GetLocations();
1910 Register temp = locations->GetTemp(0).AsRegister<Register>();
1911 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001912 Location receiver = locations->InAt(0);
1913 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1914
Roland Levillain3b359c72015-11-17 19:35:12 +00001915 // Set the hidden argument. This is safe to do this here, as R12
1916 // won't be modified thereafter, before the `blx` (call) instruction.
1917 DCHECK_EQ(R12, hidden_reg);
1918 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001919
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001920 if (receiver.IsStackSlot()) {
1921 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001922 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001923 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1924 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001925 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001926 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001927 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001928 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001929 // Instead of simply (possibly) unpoisoning `temp` here, we should
1930 // emit a read barrier for the previous class reference load.
1931 // However this is not required in practice, as this is an
1932 // intermediate/temporary reference and because the current
1933 // concurrent copying collector keeps the from-space memory
1934 // intact/accessible until the end of the marking phase (the
1935 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001936 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001937 __ LoadFromOffset(kLoadWord, temp, temp,
1938 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1939 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
1940 invoke->GetImtIndex() % ImTable::kSize, kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001941 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001942 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001943 uint32_t entry_point =
1944 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001945 // LR = temp->GetEntryPoint();
1946 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1947 // LR();
1948 __ blx(LR);
1949 DCHECK(!codegen_->IsLeafMethod());
1950 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1951}
1952
Roland Levillain88cb1752014-10-20 16:36:47 +01001953void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1954 LocationSummary* locations =
1955 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1956 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001957 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001958 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1960 break;
1961 }
1962 case Primitive::kPrimLong: {
1963 locations->SetInAt(0, Location::RequiresRegister());
1964 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001965 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001966 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001967
Roland Levillain88cb1752014-10-20 16:36:47 +01001968 case Primitive::kPrimFloat:
1969 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001970 locations->SetInAt(0, Location::RequiresFpuRegister());
1971 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001972 break;
1973
1974 default:
1975 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1976 }
1977}
1978
1979void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1980 LocationSummary* locations = neg->GetLocations();
1981 Location out = locations->Out();
1982 Location in = locations->InAt(0);
1983 switch (neg->GetResultType()) {
1984 case Primitive::kPrimInt:
1985 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001986 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001987 break;
1988
1989 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001990 DCHECK(in.IsRegisterPair());
1991 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1992 __ rsbs(out.AsRegisterPairLow<Register>(),
1993 in.AsRegisterPairLow<Register>(),
1994 ShifterOperand(0));
1995 // We cannot emit an RSC (Reverse Subtract with Carry)
1996 // instruction here, as it does not exist in the Thumb-2
1997 // instruction set. We use the following approach
1998 // using SBC and SUB instead.
1999 //
2000 // out.hi = -C
2001 __ sbc(out.AsRegisterPairHigh<Register>(),
2002 out.AsRegisterPairHigh<Register>(),
2003 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2004 // out.hi = out.hi - in.hi
2005 __ sub(out.AsRegisterPairHigh<Register>(),
2006 out.AsRegisterPairHigh<Register>(),
2007 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2008 break;
2009
Roland Levillain88cb1752014-10-20 16:36:47 +01002010 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002011 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002013 break;
2014
Roland Levillain88cb1752014-10-20 16:36:47 +01002015 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002016 DCHECK(in.IsFpuRegisterPair());
2017 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2018 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002019 break;
2020
2021 default:
2022 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2023 }
2024}
2025
Roland Levillaindff1f282014-11-05 14:15:05 +00002026void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002027 Primitive::Type result_type = conversion->GetResultType();
2028 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002029 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002030
Roland Levillain5b3ee562015-04-14 16:02:41 +01002031 // The float-to-long, double-to-long and long-to-float type conversions
2032 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002033 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002034 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2035 && result_type == Primitive::kPrimLong)
2036 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002037 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002038 : LocationSummary::kNoCall;
2039 LocationSummary* locations =
2040 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2041
David Brazdilb2bd1c52015-03-25 11:17:37 +00002042 // The Java language does not allow treating boolean as an integral type but
2043 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002044
Roland Levillaindff1f282014-11-05 14:15:05 +00002045 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002046 case Primitive::kPrimByte:
2047 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002048 case Primitive::kPrimLong:
2049 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002050 case Primitive::kPrimBoolean:
2051 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002052 case Primitive::kPrimShort:
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002055 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002056 locations->SetInAt(0, Location::RequiresRegister());
2057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2058 break;
2059
2060 default:
2061 LOG(FATAL) << "Unexpected type conversion from " << input_type
2062 << " to " << result_type;
2063 }
2064 break;
2065
Roland Levillain01a8d712014-11-14 16:27:39 +00002066 case Primitive::kPrimShort:
2067 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002068 case Primitive::kPrimLong:
2069 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002070 case Primitive::kPrimBoolean:
2071 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002072 case Primitive::kPrimByte:
2073 case Primitive::kPrimInt:
2074 case Primitive::kPrimChar:
2075 // Processing a Dex `int-to-short' instruction.
2076 locations->SetInAt(0, Location::RequiresRegister());
2077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2078 break;
2079
2080 default:
2081 LOG(FATAL) << "Unexpected type conversion from " << input_type
2082 << " to " << result_type;
2083 }
2084 break;
2085
Roland Levillain946e1432014-11-11 17:35:19 +00002086 case Primitive::kPrimInt:
2087 switch (input_type) {
2088 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002089 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002090 locations->SetInAt(0, Location::Any());
2091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2092 break;
2093
2094 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002095 // Processing a Dex `float-to-int' instruction.
2096 locations->SetInAt(0, Location::RequiresFpuRegister());
2097 locations->SetOut(Location::RequiresRegister());
2098 locations->AddTemp(Location::RequiresFpuRegister());
2099 break;
2100
Roland Levillain946e1432014-11-11 17:35:19 +00002101 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002102 // Processing a Dex `double-to-int' instruction.
2103 locations->SetInAt(0, Location::RequiresFpuRegister());
2104 locations->SetOut(Location::RequiresRegister());
2105 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002106 break;
2107
2108 default:
2109 LOG(FATAL) << "Unexpected type conversion from " << input_type
2110 << " to " << result_type;
2111 }
2112 break;
2113
Roland Levillaindff1f282014-11-05 14:15:05 +00002114 case Primitive::kPrimLong:
2115 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002116 case Primitive::kPrimBoolean:
2117 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002118 case Primitive::kPrimByte:
2119 case Primitive::kPrimShort:
2120 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002121 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002122 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002123 locations->SetInAt(0, Location::RequiresRegister());
2124 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2125 break;
2126
Roland Levillain624279f2014-12-04 11:54:28 +00002127 case Primitive::kPrimFloat: {
2128 // Processing a Dex `float-to-long' instruction.
2129 InvokeRuntimeCallingConvention calling_convention;
2130 locations->SetInAt(0, Location::FpuRegisterLocation(
2131 calling_convention.GetFpuRegisterAt(0)));
2132 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2133 break;
2134 }
2135
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002136 case Primitive::kPrimDouble: {
2137 // Processing a Dex `double-to-long' instruction.
2138 InvokeRuntimeCallingConvention calling_convention;
2139 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2140 calling_convention.GetFpuRegisterAt(0),
2141 calling_convention.GetFpuRegisterAt(1)));
2142 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002143 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002144 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002145
2146 default:
2147 LOG(FATAL) << "Unexpected type conversion from " << input_type
2148 << " to " << result_type;
2149 }
2150 break;
2151
Roland Levillain981e4542014-11-14 11:47:14 +00002152 case Primitive::kPrimChar:
2153 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002154 case Primitive::kPrimLong:
2155 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002156 case Primitive::kPrimBoolean:
2157 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002158 case Primitive::kPrimByte:
2159 case Primitive::kPrimShort:
2160 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002161 // Processing a Dex `int-to-char' instruction.
2162 locations->SetInAt(0, Location::RequiresRegister());
2163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2164 break;
2165
2166 default:
2167 LOG(FATAL) << "Unexpected type conversion from " << input_type
2168 << " to " << result_type;
2169 }
2170 break;
2171
Roland Levillaindff1f282014-11-05 14:15:05 +00002172 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002173 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002174 case Primitive::kPrimBoolean:
2175 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002176 case Primitive::kPrimByte:
2177 case Primitive::kPrimShort:
2178 case Primitive::kPrimInt:
2179 case Primitive::kPrimChar:
2180 // Processing a Dex `int-to-float' instruction.
2181 locations->SetInAt(0, Location::RequiresRegister());
2182 locations->SetOut(Location::RequiresFpuRegister());
2183 break;
2184
Roland Levillain5b3ee562015-04-14 16:02:41 +01002185 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002186 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002187 InvokeRuntimeCallingConvention calling_convention;
2188 locations->SetInAt(0, Location::RegisterPairLocation(
2189 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2190 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002191 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002192 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002193
Roland Levillaincff13742014-11-17 14:32:17 +00002194 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002195 // Processing a Dex `double-to-float' instruction.
2196 locations->SetInAt(0, Location::RequiresFpuRegister());
2197 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 };
2204 break;
2205
Roland Levillaindff1f282014-11-05 14:15:05 +00002206 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002207 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002208 case Primitive::kPrimBoolean:
2209 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002210 case Primitive::kPrimByte:
2211 case Primitive::kPrimShort:
2212 case Primitive::kPrimInt:
2213 case Primitive::kPrimChar:
2214 // Processing a Dex `int-to-double' instruction.
2215 locations->SetInAt(0, Location::RequiresRegister());
2216 locations->SetOut(Location::RequiresFpuRegister());
2217 break;
2218
2219 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002220 // Processing a Dex `long-to-double' instruction.
2221 locations->SetInAt(0, Location::RequiresRegister());
2222 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002223 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002224 locations->AddTemp(Location::RequiresFpuRegister());
2225 break;
2226
Roland Levillaincff13742014-11-17 14:32:17 +00002227 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002228 // Processing a Dex `float-to-double' instruction.
2229 locations->SetInAt(0, Location::RequiresFpuRegister());
2230 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002231 break;
2232
2233 default:
2234 LOG(FATAL) << "Unexpected type conversion from " << input_type
2235 << " to " << result_type;
2236 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002237 break;
2238
2239 default:
2240 LOG(FATAL) << "Unexpected type conversion from " << input_type
2241 << " to " << result_type;
2242 }
2243}
2244
2245void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2246 LocationSummary* locations = conversion->GetLocations();
2247 Location out = locations->Out();
2248 Location in = locations->InAt(0);
2249 Primitive::Type result_type = conversion->GetResultType();
2250 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002251 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002252 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002253 case Primitive::kPrimByte:
2254 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002255 case Primitive::kPrimLong:
2256 // Type conversion from long to byte is a result of code transformations.
2257 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2258 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002259 case Primitive::kPrimBoolean:
2260 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002261 case Primitive::kPrimShort:
2262 case Primitive::kPrimInt:
2263 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002264 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002265 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002266 break;
2267
2268 default:
2269 LOG(FATAL) << "Unexpected type conversion from " << input_type
2270 << " to " << result_type;
2271 }
2272 break;
2273
Roland Levillain01a8d712014-11-14 16:27:39 +00002274 case Primitive::kPrimShort:
2275 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002276 case Primitive::kPrimLong:
2277 // Type conversion from long to short is a result of code transformations.
2278 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2279 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002280 case Primitive::kPrimBoolean:
2281 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002282 case Primitive::kPrimByte:
2283 case Primitive::kPrimInt:
2284 case Primitive::kPrimChar:
2285 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002286 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002287 break;
2288
2289 default:
2290 LOG(FATAL) << "Unexpected type conversion from " << input_type
2291 << " to " << result_type;
2292 }
2293 break;
2294
Roland Levillain946e1432014-11-11 17:35:19 +00002295 case Primitive::kPrimInt:
2296 switch (input_type) {
2297 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002298 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002299 DCHECK(out.IsRegister());
2300 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002301 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002302 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002303 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002304 } else {
2305 DCHECK(in.IsConstant());
2306 DCHECK(in.GetConstant()->IsLongConstant());
2307 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002309 }
2310 break;
2311
Roland Levillain3f8f9362014-12-02 17:45:01 +00002312 case Primitive::kPrimFloat: {
2313 // Processing a Dex `float-to-int' instruction.
2314 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002315 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002316 __ vmovrs(out.AsRegister<Register>(), temp);
2317 break;
2318 }
2319
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002320 case Primitive::kPrimDouble: {
2321 // Processing a Dex `double-to-int' instruction.
2322 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002323 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002324 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002325 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002326 }
Roland Levillain946e1432014-11-11 17:35:19 +00002327
2328 default:
2329 LOG(FATAL) << "Unexpected type conversion from " << input_type
2330 << " to " << result_type;
2331 }
2332 break;
2333
Roland Levillaindff1f282014-11-05 14:15:05 +00002334 case Primitive::kPrimLong:
2335 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002336 case Primitive::kPrimBoolean:
2337 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002338 case Primitive::kPrimByte:
2339 case Primitive::kPrimShort:
2340 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002341 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002342 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002343 DCHECK(out.IsRegisterPair());
2344 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002345 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002346 // Sign extension.
2347 __ Asr(out.AsRegisterPairHigh<Register>(),
2348 out.AsRegisterPairLow<Register>(),
2349 31);
2350 break;
2351
2352 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002353 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002354 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2355 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002356 conversion->GetDexPc(),
2357 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002358 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002359 break;
2360
Roland Levillaindff1f282014-11-05 14:15:05 +00002361 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002362 // Processing a Dex `double-to-long' instruction.
2363 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2364 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002365 conversion->GetDexPc(),
2366 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002367 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002368 break;
2369
2370 default:
2371 LOG(FATAL) << "Unexpected type conversion from " << input_type
2372 << " to " << result_type;
2373 }
2374 break;
2375
Roland Levillain981e4542014-11-14 11:47:14 +00002376 case Primitive::kPrimChar:
2377 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002378 case Primitive::kPrimLong:
2379 // Type conversion from long to char is a result of code transformations.
2380 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2381 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002382 case Primitive::kPrimBoolean:
2383 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002384 case Primitive::kPrimByte:
2385 case Primitive::kPrimShort:
2386 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002387 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002388 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002389 break;
2390
2391 default:
2392 LOG(FATAL) << "Unexpected type conversion from " << input_type
2393 << " to " << result_type;
2394 }
2395 break;
2396
Roland Levillaindff1f282014-11-05 14:15:05 +00002397 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002398 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002399 case Primitive::kPrimBoolean:
2400 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002401 case Primitive::kPrimByte:
2402 case Primitive::kPrimShort:
2403 case Primitive::kPrimInt:
2404 case Primitive::kPrimChar: {
2405 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002406 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2407 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002408 break;
2409 }
2410
Roland Levillain5b3ee562015-04-14 16:02:41 +01002411 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002412 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002413 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2414 conversion,
2415 conversion->GetDexPc(),
2416 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002417 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002418 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002419
Roland Levillaincff13742014-11-17 14:32:17 +00002420 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002421 // Processing a Dex `double-to-float' instruction.
2422 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2423 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002424 break;
2425
2426 default:
2427 LOG(FATAL) << "Unexpected type conversion from " << input_type
2428 << " to " << result_type;
2429 };
2430 break;
2431
Roland Levillaindff1f282014-11-05 14:15:05 +00002432 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002433 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002434 case Primitive::kPrimBoolean:
2435 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002436 case Primitive::kPrimByte:
2437 case Primitive::kPrimShort:
2438 case Primitive::kPrimInt:
2439 case Primitive::kPrimChar: {
2440 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002442 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2443 out.AsFpuRegisterPairLow<SRegister>());
2444 break;
2445 }
2446
Roland Levillain647b9ed2014-11-27 12:06:00 +00002447 case Primitive::kPrimLong: {
2448 // Processing a Dex `long-to-double' instruction.
2449 Register low = in.AsRegisterPairLow<Register>();
2450 Register high = in.AsRegisterPairHigh<Register>();
2451 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2452 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002453 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002454 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002455 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2456 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002457
Roland Levillain682393c2015-04-14 15:57:52 +01002458 // temp_d = int-to-double(high)
2459 __ vmovsr(temp_s, high);
2460 __ vcvtdi(temp_d, temp_s);
2461 // constant_d = k2Pow32EncodingForDouble
2462 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2463 // out_d = unsigned-to-double(low)
2464 __ vmovsr(out_s, low);
2465 __ vcvtdu(out_d, out_s);
2466 // out_d += temp_d * constant_d
2467 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002468 break;
2469 }
2470
Roland Levillaincff13742014-11-17 14:32:17 +00002471 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002472 // Processing a Dex `float-to-double' instruction.
2473 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2474 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002475 break;
2476
2477 default:
2478 LOG(FATAL) << "Unexpected type conversion from " << input_type
2479 << " to " << result_type;
2480 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002481 break;
2482
2483 default:
2484 LOG(FATAL) << "Unexpected type conversion from " << input_type
2485 << " to " << result_type;
2486 }
2487}
2488
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002489void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002490 LocationSummary* locations =
2491 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002492 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002493 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002494 locations->SetInAt(0, Location::RequiresRegister());
2495 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002496 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2497 break;
2498 }
2499
2500 case Primitive::kPrimLong: {
2501 locations->SetInAt(0, Location::RequiresRegister());
2502 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002503 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002504 break;
2505 }
2506
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002507 case Primitive::kPrimFloat:
2508 case Primitive::kPrimDouble: {
2509 locations->SetInAt(0, Location::RequiresFpuRegister());
2510 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002511 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002512 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002513 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002514
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002515 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002516 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002517 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002518}
2519
2520void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2521 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002522 Location out = locations->Out();
2523 Location first = locations->InAt(0);
2524 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002525 switch (add->GetResultType()) {
2526 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002527 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002528 __ add(out.AsRegister<Register>(),
2529 first.AsRegister<Register>(),
2530 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002531 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002532 __ AddConstant(out.AsRegister<Register>(),
2533 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002534 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002535 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002536 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002537
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002538 case Primitive::kPrimLong: {
2539 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002540 __ adds(out.AsRegisterPairLow<Register>(),
2541 first.AsRegisterPairLow<Register>(),
2542 ShifterOperand(second.AsRegisterPairLow<Register>()));
2543 __ adc(out.AsRegisterPairHigh<Register>(),
2544 first.AsRegisterPairHigh<Register>(),
2545 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002546 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002547 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002548
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002549 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002550 __ vadds(out.AsFpuRegister<SRegister>(),
2551 first.AsFpuRegister<SRegister>(),
2552 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002553 break;
2554
2555 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002556 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2557 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2558 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002559 break;
2560
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002561 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002562 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002563 }
2564}
2565
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002566void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002567 LocationSummary* locations =
2568 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002569 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002570 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002571 locations->SetInAt(0, Location::RequiresRegister());
2572 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2574 break;
2575 }
2576
2577 case Primitive::kPrimLong: {
2578 locations->SetInAt(0, Location::RequiresRegister());
2579 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002581 break;
2582 }
Calin Juravle11351682014-10-23 15:38:15 +01002583 case Primitive::kPrimFloat:
2584 case Primitive::kPrimDouble: {
2585 locations->SetInAt(0, Location::RequiresFpuRegister());
2586 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002587 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002588 break;
Calin Juravle11351682014-10-23 15:38:15 +01002589 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002590 default:
Calin Juravle11351682014-10-23 15:38:15 +01002591 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002592 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002593}
2594
2595void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2596 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002597 Location out = locations->Out();
2598 Location first = locations->InAt(0);
2599 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002600 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002601 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002602 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002603 __ sub(out.AsRegister<Register>(),
2604 first.AsRegister<Register>(),
2605 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002606 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002607 __ AddConstant(out.AsRegister<Register>(),
2608 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002609 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002610 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002611 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002612 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002613
Calin Juravle11351682014-10-23 15:38:15 +01002614 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002615 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002616 __ subs(out.AsRegisterPairLow<Register>(),
2617 first.AsRegisterPairLow<Register>(),
2618 ShifterOperand(second.AsRegisterPairLow<Register>()));
2619 __ sbc(out.AsRegisterPairHigh<Register>(),
2620 first.AsRegisterPairHigh<Register>(),
2621 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002622 break;
Calin Juravle11351682014-10-23 15:38:15 +01002623 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002624
Calin Juravle11351682014-10-23 15:38:15 +01002625 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002626 __ vsubs(out.AsFpuRegister<SRegister>(),
2627 first.AsFpuRegister<SRegister>(),
2628 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002629 break;
Calin Juravle11351682014-10-23 15:38:15 +01002630 }
2631
2632 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002633 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2634 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2635 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002636 break;
2637 }
2638
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002639
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002640 default:
Calin Juravle11351682014-10-23 15:38:15 +01002641 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002642 }
2643}
2644
Calin Juravle34bacdf2014-10-07 20:23:36 +01002645void LocationsBuilderARM::VisitMul(HMul* mul) {
2646 LocationSummary* locations =
2647 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2648 switch (mul->GetResultType()) {
2649 case Primitive::kPrimInt:
2650 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002651 locations->SetInAt(0, Location::RequiresRegister());
2652 locations->SetInAt(1, Location::RequiresRegister());
2653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002654 break;
2655 }
2656
Calin Juravleb5bfa962014-10-21 18:02:24 +01002657 case Primitive::kPrimFloat:
2658 case Primitive::kPrimDouble: {
2659 locations->SetInAt(0, Location::RequiresFpuRegister());
2660 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002661 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002662 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002663 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002664
2665 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002666 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002667 }
2668}
2669
2670void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2671 LocationSummary* locations = mul->GetLocations();
2672 Location out = locations->Out();
2673 Location first = locations->InAt(0);
2674 Location second = locations->InAt(1);
2675 switch (mul->GetResultType()) {
2676 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002677 __ mul(out.AsRegister<Register>(),
2678 first.AsRegister<Register>(),
2679 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002680 break;
2681 }
2682 case Primitive::kPrimLong: {
2683 Register out_hi = out.AsRegisterPairHigh<Register>();
2684 Register out_lo = out.AsRegisterPairLow<Register>();
2685 Register in1_hi = first.AsRegisterPairHigh<Register>();
2686 Register in1_lo = first.AsRegisterPairLow<Register>();
2687 Register in2_hi = second.AsRegisterPairHigh<Register>();
2688 Register in2_lo = second.AsRegisterPairLow<Register>();
2689
2690 // Extra checks to protect caused by the existence of R1_R2.
2691 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2692 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2693 DCHECK_NE(out_hi, in1_lo);
2694 DCHECK_NE(out_hi, in2_lo);
2695
2696 // input: in1 - 64 bits, in2 - 64 bits
2697 // output: out
2698 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2699 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2700 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2701
2702 // IP <- in1.lo * in2.hi
2703 __ mul(IP, in1_lo, in2_hi);
2704 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2705 __ mla(out_hi, in1_hi, in2_lo, IP);
2706 // out.lo <- (in1.lo * in2.lo)[31:0];
2707 __ umull(out_lo, IP, in1_lo, in2_lo);
2708 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2709 __ add(out_hi, out_hi, ShifterOperand(IP));
2710 break;
2711 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002712
2713 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002714 __ vmuls(out.AsFpuRegister<SRegister>(),
2715 first.AsFpuRegister<SRegister>(),
2716 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002717 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002718 }
2719
2720 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002721 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2722 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2723 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002724 break;
2725 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002726
2727 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002728 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002729 }
2730}
2731
Zheng Xuc6667102015-05-15 16:08:45 +08002732void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2733 DCHECK(instruction->IsDiv() || instruction->IsRem());
2734 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2735
2736 LocationSummary* locations = instruction->GetLocations();
2737 Location second = locations->InAt(1);
2738 DCHECK(second.IsConstant());
2739
2740 Register out = locations->Out().AsRegister<Register>();
2741 Register dividend = locations->InAt(0).AsRegister<Register>();
2742 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2743 DCHECK(imm == 1 || imm == -1);
2744
2745 if (instruction->IsRem()) {
2746 __ LoadImmediate(out, 0);
2747 } else {
2748 if (imm == 1) {
2749 __ Mov(out, dividend);
2750 } else {
2751 __ rsb(out, dividend, ShifterOperand(0));
2752 }
2753 }
2754}
2755
2756void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2757 DCHECK(instruction->IsDiv() || instruction->IsRem());
2758 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2759
2760 LocationSummary* locations = instruction->GetLocations();
2761 Location second = locations->InAt(1);
2762 DCHECK(second.IsConstant());
2763
2764 Register out = locations->Out().AsRegister<Register>();
2765 Register dividend = locations->InAt(0).AsRegister<Register>();
2766 Register temp = locations->GetTemp(0).AsRegister<Register>();
2767 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002768 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002769 int ctz_imm = CTZ(abs_imm);
2770
2771 if (ctz_imm == 1) {
2772 __ Lsr(temp, dividend, 32 - ctz_imm);
2773 } else {
2774 __ Asr(temp, dividend, 31);
2775 __ Lsr(temp, temp, 32 - ctz_imm);
2776 }
2777 __ add(out, temp, ShifterOperand(dividend));
2778
2779 if (instruction->IsDiv()) {
2780 __ Asr(out, out, ctz_imm);
2781 if (imm < 0) {
2782 __ rsb(out, out, ShifterOperand(0));
2783 }
2784 } else {
2785 __ ubfx(out, out, 0, ctz_imm);
2786 __ sub(out, out, ShifterOperand(temp));
2787 }
2788}
2789
2790void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2791 DCHECK(instruction->IsDiv() || instruction->IsRem());
2792 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2793
2794 LocationSummary* locations = instruction->GetLocations();
2795 Location second = locations->InAt(1);
2796 DCHECK(second.IsConstant());
2797
2798 Register out = locations->Out().AsRegister<Register>();
2799 Register dividend = locations->InAt(0).AsRegister<Register>();
2800 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2801 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2802 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2803
2804 int64_t magic;
2805 int shift;
2806 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2807
2808 __ LoadImmediate(temp1, magic);
2809 __ smull(temp2, temp1, dividend, temp1);
2810
2811 if (imm > 0 && magic < 0) {
2812 __ add(temp1, temp1, ShifterOperand(dividend));
2813 } else if (imm < 0 && magic > 0) {
2814 __ sub(temp1, temp1, ShifterOperand(dividend));
2815 }
2816
2817 if (shift != 0) {
2818 __ Asr(temp1, temp1, shift);
2819 }
2820
2821 if (instruction->IsDiv()) {
2822 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2823 } else {
2824 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2825 // TODO: Strength reduction for mls.
2826 __ LoadImmediate(temp2, imm);
2827 __ mls(out, temp1, temp2, dividend);
2828 }
2829}
2830
2831void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2832 DCHECK(instruction->IsDiv() || instruction->IsRem());
2833 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2834
2835 LocationSummary* locations = instruction->GetLocations();
2836 Location second = locations->InAt(1);
2837 DCHECK(second.IsConstant());
2838
2839 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2840 if (imm == 0) {
2841 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2842 } else if (imm == 1 || imm == -1) {
2843 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002844 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002845 DivRemByPowerOfTwo(instruction);
2846 } else {
2847 DCHECK(imm <= -2 || imm >= 2);
2848 GenerateDivRemWithAnyConstant(instruction);
2849 }
2850}
2851
Calin Juravle7c4954d2014-10-28 16:57:40 +00002852void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002853 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2854 if (div->GetResultType() == Primitive::kPrimLong) {
2855 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002856 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002857 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2858 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002859 } else if (div->GetResultType() == Primitive::kPrimInt &&
2860 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2861 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002862 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002863 }
2864
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2866
Calin Juravle7c4954d2014-10-28 16:57:40 +00002867 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002868 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002869 if (div->InputAt(1)->IsConstant()) {
2870 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002871 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002872 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002873 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2874 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002875 // No temp register required.
2876 } else {
2877 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002878 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002879 locations->AddTemp(Location::RequiresRegister());
2880 }
2881 }
2882 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002883 locations->SetInAt(0, Location::RequiresRegister());
2884 locations->SetInAt(1, Location::RequiresRegister());
2885 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2886 } else {
2887 InvokeRuntimeCallingConvention calling_convention;
2888 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2889 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2890 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2891 // we only need the former.
2892 locations->SetOut(Location::RegisterLocation(R0));
2893 }
Calin Juravled0d48522014-11-04 16:40:20 +00002894 break;
2895 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002896 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002897 InvokeRuntimeCallingConvention calling_convention;
2898 locations->SetInAt(0, Location::RegisterPairLocation(
2899 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2900 locations->SetInAt(1, Location::RegisterPairLocation(
2901 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002902 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002903 break;
2904 }
2905 case Primitive::kPrimFloat:
2906 case Primitive::kPrimDouble: {
2907 locations->SetInAt(0, Location::RequiresFpuRegister());
2908 locations->SetInAt(1, Location::RequiresFpuRegister());
2909 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2910 break;
2911 }
2912
2913 default:
2914 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2915 }
2916}
2917
2918void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2919 LocationSummary* locations = div->GetLocations();
2920 Location out = locations->Out();
2921 Location first = locations->InAt(0);
2922 Location second = locations->InAt(1);
2923
2924 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002925 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002926 if (second.IsConstant()) {
2927 GenerateDivRemConstantIntegral(div);
2928 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002929 __ sdiv(out.AsRegister<Register>(),
2930 first.AsRegister<Register>(),
2931 second.AsRegister<Register>());
2932 } else {
2933 InvokeRuntimeCallingConvention calling_convention;
2934 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2935 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2936 DCHECK_EQ(R0, out.AsRegister<Register>());
2937
2938 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002939 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002940 }
Calin Juravled0d48522014-11-04 16:40:20 +00002941 break;
2942 }
2943
Calin Juravle7c4954d2014-10-28 16:57:40 +00002944 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002945 InvokeRuntimeCallingConvention calling_convention;
2946 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2947 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2948 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2949 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2950 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002951 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002952
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002953 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002954 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002955 break;
2956 }
2957
2958 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002959 __ vdivs(out.AsFpuRegister<SRegister>(),
2960 first.AsFpuRegister<SRegister>(),
2961 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002962 break;
2963 }
2964
2965 case Primitive::kPrimDouble: {
2966 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2967 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2968 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2969 break;
2970 }
2971
2972 default:
2973 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2974 }
2975}
2976
Calin Juravlebacfec32014-11-14 15:54:36 +00002977void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002978 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002979
2980 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002981 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002982 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2983 // sdiv will be replaced by other instruction sequence.
2984 call_kind = LocationSummary::kNoCall;
2985 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2986 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002987 // Have hardware divide instruction for int, do it with three instructions.
2988 call_kind = LocationSummary::kNoCall;
2989 }
2990
Calin Juravlebacfec32014-11-14 15:54:36 +00002991 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2992
Calin Juravled2ec87d2014-12-08 14:24:46 +00002993 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002994 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002995 if (rem->InputAt(1)->IsConstant()) {
2996 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002997 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002998 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002999 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3000 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003001 // No temp register required.
3002 } else {
3003 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003004 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003005 locations->AddTemp(Location::RequiresRegister());
3006 }
3007 }
3008 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003009 locations->SetInAt(0, Location::RequiresRegister());
3010 locations->SetInAt(1, Location::RequiresRegister());
3011 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3012 locations->AddTemp(Location::RequiresRegister());
3013 } else {
3014 InvokeRuntimeCallingConvention calling_convention;
3015 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3016 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3017 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3018 // we only need the latter.
3019 locations->SetOut(Location::RegisterLocation(R1));
3020 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003021 break;
3022 }
3023 case Primitive::kPrimLong: {
3024 InvokeRuntimeCallingConvention calling_convention;
3025 locations->SetInAt(0, Location::RegisterPairLocation(
3026 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3027 locations->SetInAt(1, Location::RegisterPairLocation(
3028 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3029 // The runtime helper puts the output in R2,R3.
3030 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3031 break;
3032 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003033 case Primitive::kPrimFloat: {
3034 InvokeRuntimeCallingConvention calling_convention;
3035 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3036 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3037 locations->SetOut(Location::FpuRegisterLocation(S0));
3038 break;
3039 }
3040
Calin Juravlebacfec32014-11-14 15:54:36 +00003041 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003042 InvokeRuntimeCallingConvention calling_convention;
3043 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3044 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3045 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3046 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3047 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003048 break;
3049 }
3050
3051 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003052 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003053 }
3054}
3055
3056void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3057 LocationSummary* locations = rem->GetLocations();
3058 Location out = locations->Out();
3059 Location first = locations->InAt(0);
3060 Location second = locations->InAt(1);
3061
Calin Juravled2ec87d2014-12-08 14:24:46 +00003062 Primitive::Type type = rem->GetResultType();
3063 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003064 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003065 if (second.IsConstant()) {
3066 GenerateDivRemConstantIntegral(rem);
3067 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003068 Register reg1 = first.AsRegister<Register>();
3069 Register reg2 = second.AsRegister<Register>();
3070 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003071
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003072 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003073 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003074 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003075 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003076 } else {
3077 InvokeRuntimeCallingConvention calling_convention;
3078 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3079 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3080 DCHECK_EQ(R1, out.AsRegister<Register>());
3081
3082 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003083 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003084 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003085 break;
3086 }
3087
3088 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003089 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003090 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003091 break;
3092 }
3093
Calin Juravled2ec87d2014-12-08 14:24:46 +00003094 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003095 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003096 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003097 break;
3098 }
3099
Calin Juravlebacfec32014-11-14 15:54:36 +00003100 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003101 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003102 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003103 break;
3104 }
3105
3106 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003107 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003108 }
3109}
3110
Calin Juravled0d48522014-11-04 16:40:20 +00003111void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003112 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3113 ? LocationSummary::kCallOnSlowPath
3114 : LocationSummary::kNoCall;
3115 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003116 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003117 if (instruction->HasUses()) {
3118 locations->SetOut(Location::SameAsFirstInput());
3119 }
3120}
3121
3122void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003123 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003124 codegen_->AddSlowPath(slow_path);
3125
3126 LocationSummary* locations = instruction->GetLocations();
3127 Location value = locations->InAt(0);
3128
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003129 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003130 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003131 case Primitive::kPrimByte:
3132 case Primitive::kPrimChar:
3133 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003134 case Primitive::kPrimInt: {
3135 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003136 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003137 } else {
3138 DCHECK(value.IsConstant()) << value;
3139 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3140 __ b(slow_path->GetEntryLabel());
3141 }
3142 }
3143 break;
3144 }
3145 case Primitive::kPrimLong: {
3146 if (value.IsRegisterPair()) {
3147 __ orrs(IP,
3148 value.AsRegisterPairLow<Register>(),
3149 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3150 __ b(slow_path->GetEntryLabel(), EQ);
3151 } else {
3152 DCHECK(value.IsConstant()) << value;
3153 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3154 __ b(slow_path->GetEntryLabel());
3155 }
3156 }
3157 break;
3158 default:
3159 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3160 }
3161 }
Calin Juravled0d48522014-11-04 16:40:20 +00003162}
3163
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003164void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3165 Register in = locations->InAt(0).AsRegister<Register>();
3166 Location rhs = locations->InAt(1);
3167 Register out = locations->Out().AsRegister<Register>();
3168
3169 if (rhs.IsConstant()) {
3170 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3171 // so map all rotations to a +ve. equivalent in that range.
3172 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3173 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3174 if (rot) {
3175 // Rotate, mapping left rotations to right equivalents if necessary.
3176 // (e.g. left by 2 bits == right by 30.)
3177 __ Ror(out, in, rot);
3178 } else if (out != in) {
3179 __ Mov(out, in);
3180 }
3181 } else {
3182 __ Ror(out, in, rhs.AsRegister<Register>());
3183 }
3184}
3185
3186// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3187// rotates by swapping input regs (effectively rotating by the first 32-bits of
3188// a larger rotation) or flipping direction (thus treating larger right/left
3189// rotations as sub-word sized rotations in the other direction) as appropriate.
3190void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3191 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3192 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3193 Location rhs = locations->InAt(1);
3194 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3195 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3196
3197 if (rhs.IsConstant()) {
3198 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3199 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003200 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003201 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3202 // logic below to a simple pair of binary orr.
3203 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3204 if (rot >= kArmBitsPerWord) {
3205 rot -= kArmBitsPerWord;
3206 std::swap(in_reg_hi, in_reg_lo);
3207 }
3208 // Rotate, or mov to out for zero or word size rotations.
3209 if (rot != 0u) {
3210 __ Lsr(out_reg_hi, in_reg_hi, rot);
3211 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3212 __ Lsr(out_reg_lo, in_reg_lo, rot);
3213 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3214 } else {
3215 __ Mov(out_reg_lo, in_reg_lo);
3216 __ Mov(out_reg_hi, in_reg_hi);
3217 }
3218 } else {
3219 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3220 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3221 Label end;
3222 Label shift_by_32_plus_shift_right;
3223
3224 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3225 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3226 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3227 __ b(&shift_by_32_plus_shift_right, CC);
3228
3229 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3230 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3231 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3232 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3233 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3234 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3235 __ Lsr(shift_left, in_reg_hi, shift_right);
3236 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3237 __ b(&end);
3238
3239 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3240 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3241 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3242 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3243 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3244 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3245 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3246 __ Lsl(shift_right, in_reg_hi, shift_left);
3247 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3248
3249 __ Bind(&end);
3250 }
3251}
Roland Levillain22c49222016-03-18 14:04:28 +00003252
3253void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003254 LocationSummary* locations =
3255 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3256 switch (ror->GetResultType()) {
3257 case Primitive::kPrimInt: {
3258 locations->SetInAt(0, Location::RequiresRegister());
3259 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3261 break;
3262 }
3263 case Primitive::kPrimLong: {
3264 locations->SetInAt(0, Location::RequiresRegister());
3265 if (ror->InputAt(1)->IsConstant()) {
3266 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3267 } else {
3268 locations->SetInAt(1, Location::RequiresRegister());
3269 locations->AddTemp(Location::RequiresRegister());
3270 locations->AddTemp(Location::RequiresRegister());
3271 }
3272 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3273 break;
3274 }
3275 default:
3276 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3277 }
3278}
3279
Roland Levillain22c49222016-03-18 14:04:28 +00003280void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003281 LocationSummary* locations = ror->GetLocations();
3282 Primitive::Type type = ror->GetResultType();
3283 switch (type) {
3284 case Primitive::kPrimInt: {
3285 HandleIntegerRotate(locations);
3286 break;
3287 }
3288 case Primitive::kPrimLong: {
3289 HandleLongRotate(locations);
3290 break;
3291 }
3292 default:
3293 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003294 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003295 }
3296}
3297
Calin Juravle9aec02f2014-11-18 23:06:35 +00003298void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3299 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3300
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003301 LocationSummary* locations =
3302 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003303
3304 switch (op->GetResultType()) {
3305 case Primitive::kPrimInt: {
3306 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003307 if (op->InputAt(1)->IsConstant()) {
3308 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3310 } else {
3311 locations->SetInAt(1, Location::RequiresRegister());
3312 // Make the output overlap, as it will be used to hold the masked
3313 // second input.
3314 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3315 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003316 break;
3317 }
3318 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003319 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003320 if (op->InputAt(1)->IsConstant()) {
3321 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3322 // For simplicity, use kOutputOverlap even though we only require that low registers
3323 // don't clash with high registers which the register allocator currently guarantees.
3324 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3325 } else {
3326 locations->SetInAt(1, Location::RequiresRegister());
3327 locations->AddTemp(Location::RequiresRegister());
3328 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3329 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003330 break;
3331 }
3332 default:
3333 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3334 }
3335}
3336
3337void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3338 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3339
3340 LocationSummary* locations = op->GetLocations();
3341 Location out = locations->Out();
3342 Location first = locations->InAt(0);
3343 Location second = locations->InAt(1);
3344
3345 Primitive::Type type = op->GetResultType();
3346 switch (type) {
3347 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003348 Register out_reg = out.AsRegister<Register>();
3349 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003350 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003352 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003353 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003354 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003355 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003356 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003357 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003358 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003359 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003360 }
3361 } else {
3362 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003363 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003364 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003365 __ Mov(out_reg, first_reg);
3366 } else if (op->IsShl()) {
3367 __ Lsl(out_reg, first_reg, shift_value);
3368 } else if (op->IsShr()) {
3369 __ Asr(out_reg, first_reg, shift_value);
3370 } else {
3371 __ Lsr(out_reg, first_reg, shift_value);
3372 }
3373 }
3374 break;
3375 }
3376 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003377 Register o_h = out.AsRegisterPairHigh<Register>();
3378 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003379
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003380 Register high = first.AsRegisterPairHigh<Register>();
3381 Register low = first.AsRegisterPairLow<Register>();
3382
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003383 if (second.IsRegister()) {
3384 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003385
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003386 Register second_reg = second.AsRegister<Register>();
3387
3388 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003389 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003390 // Shift the high part
3391 __ Lsl(o_h, high, o_l);
3392 // Shift the low part and `or` what overflew on the high part
3393 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3394 __ Lsr(temp, low, temp);
3395 __ orr(o_h, o_h, ShifterOperand(temp));
3396 // If the shift is > 32 bits, override the high part
3397 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3398 __ it(PL);
3399 __ Lsl(o_h, low, temp, PL);
3400 // Shift the low part
3401 __ Lsl(o_l, low, o_l);
3402 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003403 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003404 // Shift the low part
3405 __ Lsr(o_l, low, o_h);
3406 // Shift the high part and `or` what underflew on the low part
3407 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3408 __ Lsl(temp, high, temp);
3409 __ orr(o_l, o_l, ShifterOperand(temp));
3410 // If the shift is > 32 bits, override the low part
3411 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3412 __ it(PL);
3413 __ Asr(o_l, high, temp, PL);
3414 // Shift the high part
3415 __ Asr(o_h, high, o_h);
3416 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003417 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003418 // same as Shr except we use `Lsr`s and not `Asr`s
3419 __ Lsr(o_l, low, o_h);
3420 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3421 __ Lsl(temp, high, temp);
3422 __ orr(o_l, o_l, ShifterOperand(temp));
3423 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3424 __ it(PL);
3425 __ Lsr(o_l, high, temp, PL);
3426 __ Lsr(o_h, high, o_h);
3427 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003428 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003429 // Register allocator doesn't create partial overlap.
3430 DCHECK_NE(o_l, high);
3431 DCHECK_NE(o_h, low);
3432 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003433 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003434 if (shift_value > 32) {
3435 if (op->IsShl()) {
3436 __ Lsl(o_h, low, shift_value - 32);
3437 __ LoadImmediate(o_l, 0);
3438 } else if (op->IsShr()) {
3439 __ Asr(o_l, high, shift_value - 32);
3440 __ Asr(o_h, high, 31);
3441 } else {
3442 __ Lsr(o_l, high, shift_value - 32);
3443 __ LoadImmediate(o_h, 0);
3444 }
3445 } else if (shift_value == 32) {
3446 if (op->IsShl()) {
3447 __ mov(o_h, ShifterOperand(low));
3448 __ LoadImmediate(o_l, 0);
3449 } else if (op->IsShr()) {
3450 __ mov(o_l, ShifterOperand(high));
3451 __ Asr(o_h, high, 31);
3452 } else {
3453 __ mov(o_l, ShifterOperand(high));
3454 __ LoadImmediate(o_h, 0);
3455 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003456 } else if (shift_value == 1) {
3457 if (op->IsShl()) {
3458 __ Lsls(o_l, low, 1);
3459 __ adc(o_h, high, ShifterOperand(high));
3460 } else if (op->IsShr()) {
3461 __ Asrs(o_h, high, 1);
3462 __ Rrx(o_l, low);
3463 } else {
3464 __ Lsrs(o_h, high, 1);
3465 __ Rrx(o_l, low);
3466 }
3467 } else {
3468 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003469 if (op->IsShl()) {
3470 __ Lsl(o_h, high, shift_value);
3471 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3472 __ Lsl(o_l, low, shift_value);
3473 } else if (op->IsShr()) {
3474 __ Lsr(o_l, low, shift_value);
3475 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3476 __ Asr(o_h, high, shift_value);
3477 } else {
3478 __ Lsr(o_l, low, shift_value);
3479 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3480 __ Lsr(o_h, high, shift_value);
3481 }
3482 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003483 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003484 break;
3485 }
3486 default:
3487 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003488 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003489 }
3490}
3491
3492void LocationsBuilderARM::VisitShl(HShl* shl) {
3493 HandleShift(shl);
3494}
3495
3496void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3497 HandleShift(shl);
3498}
3499
3500void LocationsBuilderARM::VisitShr(HShr* shr) {
3501 HandleShift(shr);
3502}
3503
3504void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3505 HandleShift(shr);
3506}
3507
3508void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3509 HandleShift(ushr);
3510}
3511
3512void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3513 HandleShift(ushr);
3514}
3515
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003516void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003517 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003518 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003519 if (instruction->IsStringAlloc()) {
3520 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3521 } else {
3522 InvokeRuntimeCallingConvention calling_convention;
3523 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3524 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3525 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003526 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003527}
3528
3529void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003530 // Note: if heap poisoning is enabled, the entry point takes cares
3531 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003532 if (instruction->IsStringAlloc()) {
3533 // String is allocated through StringFactory. Call NewEmptyString entry point.
3534 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3535 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3536 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3537 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3538 __ blx(LR);
3539 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3540 } else {
3541 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3542 instruction,
3543 instruction->GetDexPc(),
3544 nullptr);
3545 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3546 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003547}
3548
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003549void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3550 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003552 InvokeRuntimeCallingConvention calling_convention;
3553 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003554 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003555 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003556 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003557}
3558
3559void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3560 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003561 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003562 // Note: if heap poisoning is enabled, the entry point takes cares
3563 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003564 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003565 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003566 instruction->GetDexPc(),
3567 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003568 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003569}
3570
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003571void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003572 LocationSummary* locations =
3573 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003574 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3575 if (location.IsStackSlot()) {
3576 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3577 } else if (location.IsDoubleStackSlot()) {
3578 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003579 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003580 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003581}
3582
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003583void InstructionCodeGeneratorARM::VisitParameterValue(
3584 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003585 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003586}
3587
3588void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3589 LocationSummary* locations =
3590 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3591 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3592}
3593
3594void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3595 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003596}
3597
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003598void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003599 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003600 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003601 locations->SetInAt(0, Location::RequiresRegister());
3602 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003603}
3604
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003605void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3606 LocationSummary* locations = not_->GetLocations();
3607 Location out = locations->Out();
3608 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003609 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003610 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003611 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003612 break;
3613
3614 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003615 __ mvn(out.AsRegisterPairLow<Register>(),
3616 ShifterOperand(in.AsRegisterPairLow<Register>()));
3617 __ mvn(out.AsRegisterPairHigh<Register>(),
3618 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003619 break;
3620
3621 default:
3622 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3623 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003624}
3625
David Brazdil66d126e2015-04-03 16:02:44 +01003626void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3627 LocationSummary* locations =
3628 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3629 locations->SetInAt(0, Location::RequiresRegister());
3630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3631}
3632
3633void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003634 LocationSummary* locations = bool_not->GetLocations();
3635 Location out = locations->Out();
3636 Location in = locations->InAt(0);
3637 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3638}
3639
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003640void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003641 LocationSummary* locations =
3642 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003643 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003644 case Primitive::kPrimBoolean:
3645 case Primitive::kPrimByte:
3646 case Primitive::kPrimShort:
3647 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003648 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003649 case Primitive::kPrimLong: {
3650 locations->SetInAt(0, Location::RequiresRegister());
3651 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003652 // Output overlaps because it is written before doing the low comparison.
3653 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003654 break;
3655 }
3656 case Primitive::kPrimFloat:
3657 case Primitive::kPrimDouble: {
3658 locations->SetInAt(0, Location::RequiresFpuRegister());
3659 locations->SetInAt(1, Location::RequiresFpuRegister());
3660 locations->SetOut(Location::RequiresRegister());
3661 break;
3662 }
3663 default:
3664 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3665 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003666}
3667
3668void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003669 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003670 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003671 Location left = locations->InAt(0);
3672 Location right = locations->InAt(1);
3673
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003674 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003675 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003676 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003677 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003678 case Primitive::kPrimBoolean:
3679 case Primitive::kPrimByte:
3680 case Primitive::kPrimShort:
3681 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003682 case Primitive::kPrimInt: {
3683 __ LoadImmediate(out, 0);
3684 __ cmp(left.AsRegister<Register>(),
3685 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3686 less_cond = LT;
3687 break;
3688 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003689 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003690 __ cmp(left.AsRegisterPairHigh<Register>(),
3691 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003692 __ b(&less, LT);
3693 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003694 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003695 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003696 __ cmp(left.AsRegisterPairLow<Register>(),
3697 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003698 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003699 break;
3700 }
3701 case Primitive::kPrimFloat:
3702 case Primitive::kPrimDouble: {
3703 __ LoadImmediate(out, 0);
3704 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003705 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003706 } else {
3707 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3708 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3709 }
3710 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003711 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003712 break;
3713 }
3714 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003715 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003716 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003717 }
Aart Bika19616e2016-02-01 18:57:58 -08003718
Calin Juravleddb7df22014-11-25 20:56:51 +00003719 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003720 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003721
3722 __ Bind(&greater);
3723 __ LoadImmediate(out, 1);
3724 __ b(&done);
3725
3726 __ Bind(&less);
3727 __ LoadImmediate(out, -1);
3728
3729 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003730}
3731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003732void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003733 LocationSummary* locations =
3734 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003735 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003736 locations->SetInAt(i, Location::Any());
3737 }
3738 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003739}
3740
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003741void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003742 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003743}
3744
Roland Levillainc9285912015-12-18 10:38:42 +00003745void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3746 // TODO (ported from quick): revisit ARM barrier kinds.
3747 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003748 switch (kind) {
3749 case MemBarrierKind::kAnyStore:
3750 case MemBarrierKind::kLoadAny:
3751 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003752 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003753 break;
3754 }
3755 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003756 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003757 break;
3758 }
3759 default:
3760 LOG(FATAL) << "Unexpected memory barrier " << kind;
3761 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003762 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003763}
3764
3765void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3766 uint32_t offset,
3767 Register out_lo,
3768 Register out_hi) {
3769 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003770 // Ensure `out_lo` is different from `addr`, so that loading
3771 // `offset` into `out_lo` does not clutter `addr`.
3772 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003773 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003774 __ add(IP, addr, ShifterOperand(out_lo));
3775 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003776 }
3777 __ ldrexd(out_lo, out_hi, addr);
3778}
3779
3780void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3781 uint32_t offset,
3782 Register value_lo,
3783 Register value_hi,
3784 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003785 Register temp2,
3786 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003787 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003788 if (offset != 0) {
3789 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003790 __ add(IP, addr, ShifterOperand(temp1));
3791 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003792 }
3793 __ Bind(&fail);
3794 // We need a load followed by store. (The address used in a STREX instruction must
3795 // be the same as the address in the most recently executed LDREX instruction.)
3796 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003797 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003798 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003799 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003800}
3801
3802void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3803 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3804
Nicolas Geoffray39468442014-09-02 15:17:15 +01003805 LocationSummary* locations =
3806 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003807 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003808
Calin Juravle52c48962014-12-16 17:02:57 +00003809 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003810 if (Primitive::IsFloatingPointType(field_type)) {
3811 locations->SetInAt(1, Location::RequiresFpuRegister());
3812 } else {
3813 locations->SetInAt(1, Location::RequiresRegister());
3814 }
3815
Calin Juravle52c48962014-12-16 17:02:57 +00003816 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003817 bool generate_volatile = field_info.IsVolatile()
3818 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003819 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003820 bool needs_write_barrier =
3821 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003822 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003823 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003824 if (needs_write_barrier) {
3825 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003826 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003827 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003828 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003829 // - registers need to be consecutive
3830 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003831 // We don't test for ARM yet, and the assertion makes sure that we
3832 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003833 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3834
3835 locations->AddTemp(Location::RequiresRegister());
3836 locations->AddTemp(Location::RequiresRegister());
3837 if (field_type == Primitive::kPrimDouble) {
3838 // For doubles we need two more registers to copy the value.
3839 locations->AddTemp(Location::RegisterLocation(R2));
3840 locations->AddTemp(Location::RegisterLocation(R3));
3841 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003842 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003843}
3844
Calin Juravle52c48962014-12-16 17:02:57 +00003845void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003846 const FieldInfo& field_info,
3847 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003848 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3849
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003850 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003851 Register base = locations->InAt(0).AsRegister<Register>();
3852 Location value = locations->InAt(1);
3853
3854 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003855 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003856 Primitive::Type field_type = field_info.GetFieldType();
3857 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003858 bool needs_write_barrier =
3859 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003860
3861 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003862 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003863 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003864
3865 switch (field_type) {
3866 case Primitive::kPrimBoolean:
3867 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003868 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003869 break;
3870 }
3871
3872 case Primitive::kPrimShort:
3873 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003874 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003875 break;
3876 }
3877
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003878 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003879 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003880 if (kPoisonHeapReferences && needs_write_barrier) {
3881 // Note that in the case where `value` is a null reference,
3882 // we do not enter this block, as a null reference does not
3883 // need poisoning.
3884 DCHECK_EQ(field_type, Primitive::kPrimNot);
3885 Register temp = locations->GetTemp(0).AsRegister<Register>();
3886 __ Mov(temp, value.AsRegister<Register>());
3887 __ PoisonHeapReference(temp);
3888 __ StoreToOffset(kStoreWord, temp, base, offset);
3889 } else {
3890 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3891 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003892 break;
3893 }
3894
3895 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003896 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003897 GenerateWideAtomicStore(base, offset,
3898 value.AsRegisterPairLow<Register>(),
3899 value.AsRegisterPairHigh<Register>(),
3900 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003901 locations->GetTemp(1).AsRegister<Register>(),
3902 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003903 } else {
3904 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003905 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003906 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003907 break;
3908 }
3909
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003910 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003911 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003912 break;
3913 }
3914
3915 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003916 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003917 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003918 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3919 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3920
3921 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3922
3923 GenerateWideAtomicStore(base, offset,
3924 value_reg_lo,
3925 value_reg_hi,
3926 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003927 locations->GetTemp(3).AsRegister<Register>(),
3928 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003929 } else {
3930 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003931 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003932 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003933 break;
3934 }
3935
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003936 case Primitive::kPrimVoid:
3937 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003938 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003939 }
Calin Juravle52c48962014-12-16 17:02:57 +00003940
Calin Juravle77520bc2015-01-12 18:45:46 +00003941 // Longs and doubles are handled in the switch.
3942 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3943 codegen_->MaybeRecordImplicitNullCheck(instruction);
3944 }
3945
3946 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3947 Register temp = locations->GetTemp(0).AsRegister<Register>();
3948 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003949 codegen_->MarkGCCard(
3950 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003951 }
3952
Calin Juravle52c48962014-12-16 17:02:57 +00003953 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003954 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003955 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003956}
3957
Calin Juravle52c48962014-12-16 17:02:57 +00003958void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3959 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003960
3961 bool object_field_get_with_read_barrier =
3962 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003963 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003964 new (GetGraph()->GetArena()) LocationSummary(instruction,
3965 object_field_get_with_read_barrier ?
3966 LocationSummary::kCallOnSlowPath :
3967 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003968 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003969
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003970 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003971 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003972 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003973 // The output overlaps in case of volatile long: we don't want the
3974 // code generated by GenerateWideAtomicLoad to overwrite the
3975 // object's location. Likewise, in the case of an object field get
3976 // with read barriers enabled, we do not want the load to overwrite
3977 // the object's location, as we need it to emit the read barrier.
3978 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3979 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003980
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003981 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3982 locations->SetOut(Location::RequiresFpuRegister());
3983 } else {
3984 locations->SetOut(Location::RequiresRegister(),
3985 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3986 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003987 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00003988 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003989 // - registers need to be consecutive
3990 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003991 // We don't test for ARM yet, and the assertion makes sure that we
3992 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003993 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3994 locations->AddTemp(Location::RequiresRegister());
3995 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00003996 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3997 // We need a temporary register for the read barrier marking slow
3998 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3999 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004000 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004001}
4002
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004003Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4004 Opcode opcode) {
4005 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4006 if (constant->IsConstant() &&
4007 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4008 return Location::ConstantLocation(constant->AsConstant());
4009 }
4010 return Location::RequiresRegister();
4011}
4012
4013bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4014 Opcode opcode) {
4015 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4016 if (Primitive::Is64BitType(input_cst->GetType())) {
4017 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4018 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4019 } else {
4020 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4021 }
4022}
4023
4024bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4025 ShifterOperand so;
4026 ArmAssembler* assembler = codegen_->GetAssembler();
4027 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4028 return true;
4029 }
4030 Opcode neg_opcode = kNoOperand;
4031 switch (opcode) {
4032 case AND:
4033 neg_opcode = BIC;
4034 break;
4035 case ORR:
4036 neg_opcode = ORN;
4037 break;
4038 default:
4039 return false;
4040 }
4041 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4042}
4043
Calin Juravle52c48962014-12-16 17:02:57 +00004044void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4045 const FieldInfo& field_info) {
4046 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004047
Calin Juravle52c48962014-12-16 17:02:57 +00004048 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004049 Location base_loc = locations->InAt(0);
4050 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004051 Location out = locations->Out();
4052 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004053 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004054 Primitive::Type field_type = field_info.GetFieldType();
4055 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4056
4057 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004058 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004059 __ LoadFromOffset(kLoadUnsignedByte, 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::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004063 __ LoadFromOffset(kLoadSignedByte, 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::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004067 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004068 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004069
Roland Levillainc9285912015-12-18 10:38:42 +00004070 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004071 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004072 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004073
4074 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004075 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004076 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004077
4078 case Primitive::kPrimNot: {
4079 // /* HeapReference<Object> */ out = *(base + offset)
4080 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4081 Location temp_loc = locations->GetTemp(0);
4082 // Note that a potential implicit null check is handled in this
4083 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4084 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4085 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4086 if (is_volatile) {
4087 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4088 }
4089 } else {
4090 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4091 codegen_->MaybeRecordImplicitNullCheck(instruction);
4092 if (is_volatile) {
4093 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4094 }
4095 // If read barriers are enabled, emit read barriers other than
4096 // Baker's using a slow path (and also unpoison the loaded
4097 // reference, if heap poisoning is enabled).
4098 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4099 }
4100 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004101 }
4102
Roland Levillainc9285912015-12-18 10:38:42 +00004103 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004104 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004105 GenerateWideAtomicLoad(base, offset,
4106 out.AsRegisterPairLow<Register>(),
4107 out.AsRegisterPairHigh<Register>());
4108 } else {
4109 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4110 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004112
Roland Levillainc9285912015-12-18 10:38:42 +00004113 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004114 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004115 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004116
4117 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004118 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004119 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004120 Register lo = locations->GetTemp(0).AsRegister<Register>();
4121 Register hi = locations->GetTemp(1).AsRegister<Register>();
4122 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004123 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004124 __ vmovdrr(out_reg, lo, hi);
4125 } else {
4126 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004127 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004128 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004129 break;
4130 }
4131
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004132 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004133 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004134 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004135 }
Calin Juravle52c48962014-12-16 17:02:57 +00004136
Roland Levillainc9285912015-12-18 10:38:42 +00004137 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4138 // Potential implicit null checks, in the case of reference or
4139 // double fields, are handled in the previous switch statement.
4140 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004141 codegen_->MaybeRecordImplicitNullCheck(instruction);
4142 }
4143
Calin Juravle52c48962014-12-16 17:02:57 +00004144 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004145 if (field_type == Primitive::kPrimNot) {
4146 // Memory barriers, in the case of references, are also handled
4147 // in the previous switch statement.
4148 } else {
4149 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4150 }
Roland Levillain4d027112015-07-01 15:41:14 +01004151 }
Calin Juravle52c48962014-12-16 17:02:57 +00004152}
4153
4154void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4155 HandleFieldSet(instruction, instruction->GetFieldInfo());
4156}
4157
4158void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004159 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004160}
4161
4162void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4163 HandleFieldGet(instruction, instruction->GetFieldInfo());
4164}
4165
4166void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4167 HandleFieldGet(instruction, instruction->GetFieldInfo());
4168}
4169
4170void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4171 HandleFieldGet(instruction, instruction->GetFieldInfo());
4172}
4173
4174void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4175 HandleFieldGet(instruction, instruction->GetFieldInfo());
4176}
4177
4178void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4179 HandleFieldSet(instruction, instruction->GetFieldInfo());
4180}
4181
4182void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004183 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004184}
4185
Calin Juravlee460d1d2015-09-29 04:52:17 +01004186void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4187 HUnresolvedInstanceFieldGet* instruction) {
4188 FieldAccessCallingConventionARM calling_convention;
4189 codegen_->CreateUnresolvedFieldLocationSummary(
4190 instruction, instruction->GetFieldType(), calling_convention);
4191}
4192
4193void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4194 HUnresolvedInstanceFieldGet* instruction) {
4195 FieldAccessCallingConventionARM calling_convention;
4196 codegen_->GenerateUnresolvedFieldAccess(instruction,
4197 instruction->GetFieldType(),
4198 instruction->GetFieldIndex(),
4199 instruction->GetDexPc(),
4200 calling_convention);
4201}
4202
4203void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4204 HUnresolvedInstanceFieldSet* instruction) {
4205 FieldAccessCallingConventionARM calling_convention;
4206 codegen_->CreateUnresolvedFieldLocationSummary(
4207 instruction, instruction->GetFieldType(), calling_convention);
4208}
4209
4210void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4211 HUnresolvedInstanceFieldSet* instruction) {
4212 FieldAccessCallingConventionARM calling_convention;
4213 codegen_->GenerateUnresolvedFieldAccess(instruction,
4214 instruction->GetFieldType(),
4215 instruction->GetFieldIndex(),
4216 instruction->GetDexPc(),
4217 calling_convention);
4218}
4219
4220void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4221 HUnresolvedStaticFieldGet* instruction) {
4222 FieldAccessCallingConventionARM calling_convention;
4223 codegen_->CreateUnresolvedFieldLocationSummary(
4224 instruction, instruction->GetFieldType(), calling_convention);
4225}
4226
4227void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4228 HUnresolvedStaticFieldGet* instruction) {
4229 FieldAccessCallingConventionARM calling_convention;
4230 codegen_->GenerateUnresolvedFieldAccess(instruction,
4231 instruction->GetFieldType(),
4232 instruction->GetFieldIndex(),
4233 instruction->GetDexPc(),
4234 calling_convention);
4235}
4236
4237void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4238 HUnresolvedStaticFieldSet* instruction) {
4239 FieldAccessCallingConventionARM calling_convention;
4240 codegen_->CreateUnresolvedFieldLocationSummary(
4241 instruction, instruction->GetFieldType(), calling_convention);
4242}
4243
4244void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4245 HUnresolvedStaticFieldSet* instruction) {
4246 FieldAccessCallingConventionARM calling_convention;
4247 codegen_->GenerateUnresolvedFieldAccess(instruction,
4248 instruction->GetFieldType(),
4249 instruction->GetFieldIndex(),
4250 instruction->GetDexPc(),
4251 calling_convention);
4252}
4253
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004255 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4256 ? LocationSummary::kCallOnSlowPath
4257 : LocationSummary::kNoCall;
4258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004259 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004260 if (instruction->HasUses()) {
4261 locations->SetOut(Location::SameAsFirstInput());
4262 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004263}
4264
Calin Juravle2ae48182016-03-16 14:05:09 +00004265void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4266 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004267 return;
4268 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004269 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004270
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004271 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004272 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004273}
4274
Calin Juravle2ae48182016-03-16 14:05:09 +00004275void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004276 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004277 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004278
4279 LocationSummary* locations = instruction->GetLocations();
4280 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004281
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004282 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283}
4284
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004285void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004286 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004287}
4288
Artem Serov6c916792016-07-11 14:02:34 +01004289static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4290 switch (type) {
4291 case Primitive::kPrimNot:
4292 return kLoadWord;
4293 case Primitive::kPrimBoolean:
4294 return kLoadUnsignedByte;
4295 case Primitive::kPrimByte:
4296 return kLoadSignedByte;
4297 case Primitive::kPrimChar:
4298 return kLoadUnsignedHalfword;
4299 case Primitive::kPrimShort:
4300 return kLoadSignedHalfword;
4301 case Primitive::kPrimInt:
4302 return kLoadWord;
4303 case Primitive::kPrimLong:
4304 return kLoadWordPair;
4305 case Primitive::kPrimFloat:
4306 return kLoadSWord;
4307 case Primitive::kPrimDouble:
4308 return kLoadDWord;
4309 default:
4310 LOG(FATAL) << "Unreachable type " << type;
4311 UNREACHABLE();
4312 }
4313}
4314
4315static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4316 switch (type) {
4317 case Primitive::kPrimNot:
4318 return kStoreWord;
4319 case Primitive::kPrimBoolean:
4320 case Primitive::kPrimByte:
4321 return kStoreByte;
4322 case Primitive::kPrimChar:
4323 case Primitive::kPrimShort:
4324 return kStoreHalfword;
4325 case Primitive::kPrimInt:
4326 return kStoreWord;
4327 case Primitive::kPrimLong:
4328 return kStoreWordPair;
4329 case Primitive::kPrimFloat:
4330 return kStoreSWord;
4331 case Primitive::kPrimDouble:
4332 return kStoreDWord;
4333 default:
4334 LOG(FATAL) << "Unreachable type " << type;
4335 UNREACHABLE();
4336 }
4337}
4338
4339void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4340 Location out_loc,
4341 Register base,
4342 Register reg_offset,
4343 Condition cond) {
4344 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4345 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4346
4347 switch (type) {
4348 case Primitive::kPrimByte:
4349 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4350 break;
4351 case Primitive::kPrimBoolean:
4352 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4353 break;
4354 case Primitive::kPrimShort:
4355 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4356 break;
4357 case Primitive::kPrimChar:
4358 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4359 break;
4360 case Primitive::kPrimNot:
4361 case Primitive::kPrimInt:
4362 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4363 break;
4364 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4365 case Primitive::kPrimLong:
4366 case Primitive::kPrimFloat:
4367 case Primitive::kPrimDouble:
4368 default:
4369 LOG(FATAL) << "Unreachable type " << type;
4370 UNREACHABLE();
4371 }
4372}
4373
4374void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4375 Location loc,
4376 Register base,
4377 Register reg_offset,
4378 Condition cond) {
4379 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4380 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4381
4382 switch (type) {
4383 case Primitive::kPrimByte:
4384 case Primitive::kPrimBoolean:
4385 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4386 break;
4387 case Primitive::kPrimShort:
4388 case Primitive::kPrimChar:
4389 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4390 break;
4391 case Primitive::kPrimNot:
4392 case Primitive::kPrimInt:
4393 __ str(loc.AsRegister<Register>(), mem_address, cond);
4394 break;
4395 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4396 case Primitive::kPrimLong:
4397 case Primitive::kPrimFloat:
4398 case Primitive::kPrimDouble:
4399 default:
4400 LOG(FATAL) << "Unreachable type " << type;
4401 UNREACHABLE();
4402 }
4403}
4404
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004405void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004406 bool object_array_get_with_read_barrier =
4407 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004408 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004409 new (GetGraph()->GetArena()) LocationSummary(instruction,
4410 object_array_get_with_read_barrier ?
4411 LocationSummary::kCallOnSlowPath :
4412 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004413 locations->SetInAt(0, Location::RequiresRegister());
4414 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004415 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4416 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4417 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004418 // The output overlaps in the case of an object array get with
4419 // read barriers enabled: we do not want the move to overwrite the
4420 // array's location, as we need it to emit the read barrier.
4421 locations->SetOut(
4422 Location::RequiresRegister(),
4423 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004424 }
Roland Levillainc9285912015-12-18 10:38:42 +00004425 // We need a temporary register for the read barrier marking slow
4426 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4427 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4428 locations->AddTemp(Location::RequiresRegister());
4429 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430}
4431
4432void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4433 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004434 Location obj_loc = locations->InAt(0);
4435 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004437 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004438 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004439 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004440 HInstruction* array_instr = instruction->GetArray();
4441 bool has_intermediate_address = array_instr->IsIntermediateAddress();
4442 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4443 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004444
Roland Levillain4d027112015-07-01 15:41:14 +01004445 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004446 case Primitive::kPrimBoolean:
4447 case Primitive::kPrimByte:
4448 case Primitive::kPrimShort:
4449 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004450 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004451 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004452 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4453 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4454
4455 LoadOperandType load_type = GetLoadOperandType(type);
4456 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004457 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004458 Register temp = IP;
4459
4460 if (has_intermediate_address) {
4461 // We do not need to compute the intermediate address from the array: the
4462 // input instruction has done it already. See the comment in
4463 // `TryExtractArrayAccessAddress()`.
4464 if (kIsDebugBuild) {
4465 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4466 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4467 }
4468 temp = obj;
4469 } else {
4470 __ add(temp, obj, ShifterOperand(data_offset));
4471 }
4472 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004473 }
4474 break;
4475 }
4476
Roland Levillainc9285912015-12-18 10:38:42 +00004477 case Primitive::kPrimNot: {
4478 static_assert(
4479 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4480 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004481 // /* HeapReference<Object> */ out =
4482 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4483 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4484 Location temp = locations->GetTemp(0);
4485 // Note that a potential implicit null check is handled in this
4486 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4487 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4488 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4489 } else {
4490 Register out = out_loc.AsRegister<Register>();
4491 if (index.IsConstant()) {
4492 size_t offset =
4493 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4494 __ LoadFromOffset(kLoadWord, out, obj, offset);
4495 codegen_->MaybeRecordImplicitNullCheck(instruction);
4496 // If read barriers are enabled, emit read barriers other than
4497 // Baker's using a slow path (and also unpoison the loaded
4498 // reference, if heap poisoning is enabled).
4499 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4500 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004501 Register temp = IP;
4502
4503 if (has_intermediate_address) {
4504 // We do not need to compute the intermediate address from the array: the
4505 // input instruction has done it already. See the comment in
4506 // `TryExtractArrayAccessAddress()`.
4507 if (kIsDebugBuild) {
4508 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4509 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4510 }
4511 temp = obj;
4512 } else {
4513 __ add(temp, obj, ShifterOperand(data_offset));
4514 }
4515 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004516
Roland Levillainc9285912015-12-18 10:38:42 +00004517 codegen_->MaybeRecordImplicitNullCheck(instruction);
4518 // If read barriers are enabled, emit read barriers other than
4519 // Baker's using a slow path (and also unpoison the loaded
4520 // reference, if heap poisoning is enabled).
4521 codegen_->MaybeGenerateReadBarrierSlow(
4522 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4523 }
4524 }
4525 break;
4526 }
4527
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004528 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004530 size_t offset =
4531 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004532 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004533 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004534 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004535 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004536 }
4537 break;
4538 }
4539
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004540 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004541 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004542 if (index.IsConstant()) {
4543 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004544 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004545 } else {
4546 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004547 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004548 }
4549 break;
4550 }
4551
4552 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004553 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004554 if (index.IsConstant()) {
4555 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004556 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004557 } else {
4558 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004559 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004560 }
4561 break;
4562 }
4563
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004564 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004565 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004566 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004567 }
Roland Levillain4d027112015-07-01 15:41:14 +01004568
4569 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004570 // Potential implicit null checks, in the case of reference
4571 // arrays, are handled in the previous switch statement.
4572 } else {
4573 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004574 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004575}
4576
4577void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004578 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004579
4580 bool needs_write_barrier =
4581 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004582 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4583 bool object_array_set_with_read_barrier =
4584 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004585
Nicolas Geoffray39468442014-09-02 15:17:15 +01004586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004587 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004588 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4589 LocationSummary::kCallOnSlowPath :
4590 LocationSummary::kNoCall);
4591
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004592 locations->SetInAt(0, Location::RequiresRegister());
4593 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4594 if (Primitive::IsFloatingPointType(value_type)) {
4595 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004596 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004597 locations->SetInAt(2, Location::RequiresRegister());
4598 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004599 if (needs_write_barrier) {
4600 // Temporary registers for the write barrier.
4601 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004602 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604}
4605
4606void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4607 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004608 Location array_loc = locations->InAt(0);
4609 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004611 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004612 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004613 bool needs_write_barrier =
4614 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004615 uint32_t data_offset =
4616 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4617 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004618 HInstruction* array_instr = instruction->GetArray();
4619 bool has_intermediate_address = array_instr->IsIntermediateAddress();
4620 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4621 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622
4623 switch (value_type) {
4624 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004625 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004627 case Primitive::kPrimChar:
4628 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004629 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004630 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4631 uint32_t full_offset =
4632 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4633 StoreOperandType store_type = GetStoreOperandType(value_type);
4634 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004635 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004636 Register temp = IP;
4637
4638 if (has_intermediate_address) {
4639 // We do not need to compute the intermediate address from the array: the
4640 // input instruction has done it already. See the comment in
4641 // `TryExtractArrayAccessAddress()`.
4642 if (kIsDebugBuild) {
4643 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4644 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4645 }
4646 temp = array;
4647 } else {
4648 __ add(temp, array, ShifterOperand(data_offset));
4649 }
Artem Serov6c916792016-07-11 14:02:34 +01004650 codegen_->StoreToShiftedRegOffset(value_type,
4651 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004652 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004653 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004654 }
4655 break;
4656 }
4657
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004658 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004659 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004660 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4661 // See the comment in instruction_simplifier_shared.cc.
4662 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004663
4664 if (instruction->InputAt(2)->IsNullConstant()) {
4665 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004666 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004667 size_t offset =
4668 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004669 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004670 } else {
4671 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004672 __ add(IP, array, ShifterOperand(data_offset));
4673 codegen_->StoreToShiftedRegOffset(value_type,
4674 value_loc,
4675 IP,
4676 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004677 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004678 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004679 DCHECK(!needs_write_barrier);
4680 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004681 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004682 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004683
4684 DCHECK(needs_write_barrier);
4685 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4686 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4687 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4688 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4689 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4690 Label done;
4691 SlowPathCode* slow_path = nullptr;
4692
Roland Levillain3b359c72015-11-17 19:35:12 +00004693 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004694 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4695 codegen_->AddSlowPath(slow_path);
4696 if (instruction->GetValueCanBeNull()) {
4697 Label non_zero;
4698 __ CompareAndBranchIfNonZero(value, &non_zero);
4699 if (index.IsConstant()) {
4700 size_t offset =
4701 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4702 __ StoreToOffset(kStoreWord, value, array, offset);
4703 } else {
4704 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004705 __ add(IP, array, ShifterOperand(data_offset));
4706 codegen_->StoreToShiftedRegOffset(value_type,
4707 value_loc,
4708 IP,
4709 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004710 }
4711 codegen_->MaybeRecordImplicitNullCheck(instruction);
4712 __ b(&done);
4713 __ Bind(&non_zero);
4714 }
4715
Roland Levillain3b359c72015-11-17 19:35:12 +00004716 if (kEmitCompilerReadBarrier) {
4717 // When read barriers are enabled, the type checking
4718 // instrumentation requires two read barriers:
4719 //
4720 // __ Mov(temp2, temp1);
4721 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4722 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004723 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004724 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4725 //
4726 // // /* HeapReference<Class> */ temp2 = value->klass_
4727 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004728 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004729 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4730 //
4731 // __ cmp(temp1, ShifterOperand(temp2));
4732 //
4733 // However, the second read barrier may trash `temp`, as it
4734 // is a temporary register, and as such would not be saved
4735 // along with live registers before calling the runtime (nor
4736 // restored afterwards). So in this case, we bail out and
4737 // delegate the work to the array set slow path.
4738 //
4739 // TODO: Extend the register allocator to support a new
4740 // "(locally) live temp" location so as to avoid always
4741 // going into the slow path when read barriers are enabled.
4742 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004743 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004744 // /* HeapReference<Class> */ temp1 = array->klass_
4745 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4746 codegen_->MaybeRecordImplicitNullCheck(instruction);
4747 __ MaybeUnpoisonHeapReference(temp1);
4748
4749 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4750 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4751 // /* HeapReference<Class> */ temp2 = value->klass_
4752 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4753 // If heap poisoning is enabled, no need to unpoison `temp1`
4754 // nor `temp2`, as we are comparing two poisoned references.
4755 __ cmp(temp1, ShifterOperand(temp2));
4756
4757 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4758 Label do_put;
4759 __ b(&do_put, EQ);
4760 // If heap poisoning is enabled, the `temp1` reference has
4761 // not been unpoisoned yet; unpoison it now.
4762 __ MaybeUnpoisonHeapReference(temp1);
4763
4764 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4765 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4766 // If heap poisoning is enabled, no need to unpoison
4767 // `temp1`, as we are comparing against null below.
4768 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4769 __ Bind(&do_put);
4770 } else {
4771 __ b(slow_path->GetEntryLabel(), NE);
4772 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 }
4774 }
4775
Artem Serov6c916792016-07-11 14:02:34 +01004776 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004777 if (kPoisonHeapReferences) {
4778 // Note that in the case where `value` is a null reference,
4779 // we do not enter this block, as a null reference does not
4780 // need poisoning.
4781 DCHECK_EQ(value_type, Primitive::kPrimNot);
4782 __ Mov(temp1, value);
4783 __ PoisonHeapReference(temp1);
4784 source = temp1;
4785 }
4786
4787 if (index.IsConstant()) {
4788 size_t offset =
4789 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4790 __ StoreToOffset(kStoreWord, source, array, offset);
4791 } else {
4792 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004793
4794 __ add(IP, array, ShifterOperand(data_offset));
4795 codegen_->StoreToShiftedRegOffset(value_type,
4796 Location::RegisterLocation(source),
4797 IP,
4798 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 }
4800
Roland Levillain3b359c72015-11-17 19:35:12 +00004801 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004802 codegen_->MaybeRecordImplicitNullCheck(instruction);
4803 }
4804
4805 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4806
4807 if (done.IsLinked()) {
4808 __ Bind(&done);
4809 }
4810
4811 if (slow_path != nullptr) {
4812 __ Bind(slow_path->GetExitLabel());
4813 }
4814
4815 break;
4816 }
4817
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004819 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004820 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004821 size_t offset =
4822 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004823 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004824 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004825 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004826 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827 }
4828 break;
4829 }
4830
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004831 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004832 Location value = locations->InAt(2);
4833 DCHECK(value.IsFpuRegister());
4834 if (index.IsConstant()) {
4835 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004836 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004837 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004838 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004839 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4840 }
4841 break;
4842 }
4843
4844 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004845 Location value = locations->InAt(2);
4846 DCHECK(value.IsFpuRegisterPair());
4847 if (index.IsConstant()) {
4848 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004849 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004850 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004851 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004852 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4853 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004854
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004855 break;
4856 }
4857
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004858 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004859 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004860 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004862
Roland Levillain80e67092016-01-08 16:04:55 +00004863 // Objects are handled in the switch.
4864 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004865 codegen_->MaybeRecordImplicitNullCheck(instruction);
4866 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004867}
4868
4869void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004870 LocationSummary* locations =
4871 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004872 locations->SetInAt(0, Location::RequiresRegister());
4873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004874}
4875
4876void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4877 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004878 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004879 Register obj = locations->InAt(0).AsRegister<Register>();
4880 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004882 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004883}
4884
Artem Serov328429f2016-07-06 16:23:04 +01004885void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4886 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4887 DCHECK(!kEmitCompilerReadBarrier);
4888 LocationSummary* locations =
4889 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4890
4891 locations->SetInAt(0, Location::RequiresRegister());
4892 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4894}
4895
4896void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4897 LocationSummary* locations = instruction->GetLocations();
4898 Location out = locations->Out();
4899 Location first = locations->InAt(0);
4900 Location second = locations->InAt(1);
4901
4902 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4903 DCHECK(!kEmitCompilerReadBarrier);
4904
4905 if (second.IsRegister()) {
4906 __ add(out.AsRegister<Register>(),
4907 first.AsRegister<Register>(),
4908 ShifterOperand(second.AsRegister<Register>()));
4909 } else {
4910 __ AddConstant(out.AsRegister<Register>(),
4911 first.AsRegister<Register>(),
4912 second.GetConstant()->AsIntConstant()->GetValue());
4913 }
4914}
4915
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004916void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004917 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4918 ? LocationSummary::kCallOnSlowPath
4919 : LocationSummary::kNoCall;
4920 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004921 locations->SetInAt(0, Location::RequiresRegister());
4922 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004923 if (instruction->HasUses()) {
4924 locations->SetOut(Location::SameAsFirstInput());
4925 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926}
4927
4928void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4929 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004930 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004931 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004932 codegen_->AddSlowPath(slow_path);
4933
Roland Levillain271ab9c2014-11-27 15:23:57 +00004934 Register index = locations->InAt(0).AsRegister<Register>();
4935 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004936
4937 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004938 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004939}
4940
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004941void CodeGeneratorARM::MarkGCCard(Register temp,
4942 Register card,
4943 Register object,
4944 Register value,
4945 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004946 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004947 if (can_be_null) {
4948 __ CompareAndBranchIfZero(value, &is_null);
4949 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004950 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4951 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4952 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004953 if (can_be_null) {
4954 __ Bind(&is_null);
4955 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004956}
4957
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004958void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004959 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004960}
4961
4962void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004963 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4964}
4965
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004966void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4968}
4969
4970void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004971 HBasicBlock* block = instruction->GetBlock();
4972 if (block->GetLoopInformation() != nullptr) {
4973 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4974 // The back edge will generate the suspend check.
4975 return;
4976 }
4977 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4978 // The goto will generate the suspend check.
4979 return;
4980 }
4981 GenerateSuspendCheck(instruction, nullptr);
4982}
4983
4984void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4985 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004986 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004987 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4988 if (slow_path == nullptr) {
4989 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4990 instruction->SetSlowPath(slow_path);
4991 codegen_->AddSlowPath(slow_path);
4992 if (successor != nullptr) {
4993 DCHECK(successor->IsLoopHeader());
4994 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4995 }
4996 } else {
4997 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4998 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004999
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005000 __ LoadFromOffset(
5001 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005002 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005003 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005004 __ Bind(slow_path->GetReturnLabel());
5005 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005006 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005007 __ b(slow_path->GetEntryLabel());
5008 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005009}
5010
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005011ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5012 return codegen_->GetAssembler();
5013}
5014
5015void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005016 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005017 Location source = move->GetSource();
5018 Location destination = move->GetDestination();
5019
5020 if (source.IsRegister()) {
5021 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005022 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005023 } else if (destination.IsFpuRegister()) {
5024 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005025 } else {
5026 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005027 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005028 SP, destination.GetStackIndex());
5029 }
5030 } else if (source.IsStackSlot()) {
5031 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005032 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005033 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005034 } else if (destination.IsFpuRegister()) {
5035 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005036 } else {
5037 DCHECK(destination.IsStackSlot());
5038 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5039 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5040 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005041 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005042 if (destination.IsRegister()) {
5043 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5044 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005045 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005046 } else {
5047 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005048 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5049 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005050 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005051 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005052 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5053 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005054 } else if (destination.IsRegisterPair()) {
5055 DCHECK(ExpectedPairLayout(destination));
5056 __ LoadFromOffset(
5057 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5058 } else {
5059 DCHECK(destination.IsFpuRegisterPair()) << destination;
5060 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5061 SP,
5062 source.GetStackIndex());
5063 }
5064 } else if (source.IsRegisterPair()) {
5065 if (destination.IsRegisterPair()) {
5066 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5067 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005068 } else if (destination.IsFpuRegisterPair()) {
5069 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5070 source.AsRegisterPairLow<Register>(),
5071 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005072 } else {
5073 DCHECK(destination.IsDoubleStackSlot()) << destination;
5074 DCHECK(ExpectedPairLayout(source));
5075 __ StoreToOffset(
5076 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5077 }
5078 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005079 if (destination.IsRegisterPair()) {
5080 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5081 destination.AsRegisterPairHigh<Register>(),
5082 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5083 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005084 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5085 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5086 } else {
5087 DCHECK(destination.IsDoubleStackSlot()) << destination;
5088 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5089 SP,
5090 destination.GetStackIndex());
5091 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005092 } else {
5093 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005094 HConstant* constant = source.GetConstant();
5095 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5096 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005097 if (destination.IsRegister()) {
5098 __ LoadImmediate(destination.AsRegister<Register>(), value);
5099 } else {
5100 DCHECK(destination.IsStackSlot());
5101 __ LoadImmediate(IP, value);
5102 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5103 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005104 } else if (constant->IsLongConstant()) {
5105 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005106 if (destination.IsRegisterPair()) {
5107 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5108 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005109 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005110 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005111 __ LoadImmediate(IP, Low32Bits(value));
5112 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5113 __ LoadImmediate(IP, High32Bits(value));
5114 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5115 }
5116 } else if (constant->IsDoubleConstant()) {
5117 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005118 if (destination.IsFpuRegisterPair()) {
5119 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005120 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005121 DCHECK(destination.IsDoubleStackSlot()) << destination;
5122 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005123 __ LoadImmediate(IP, Low32Bits(int_value));
5124 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5125 __ LoadImmediate(IP, High32Bits(int_value));
5126 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5127 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005128 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005129 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005130 float value = constant->AsFloatConstant()->GetValue();
5131 if (destination.IsFpuRegister()) {
5132 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5133 } else {
5134 DCHECK(destination.IsStackSlot());
5135 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5136 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5137 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005138 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005139 }
5140}
5141
5142void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5143 __ Mov(IP, reg);
5144 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5145 __ StoreToOffset(kStoreWord, IP, SP, mem);
5146}
5147
5148void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5149 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5150 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5151 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5152 SP, mem1 + stack_offset);
5153 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5154 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5155 SP, mem2 + stack_offset);
5156 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5157}
5158
5159void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005160 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005161 Location source = move->GetSource();
5162 Location destination = move->GetDestination();
5163
5164 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005165 DCHECK_NE(source.AsRegister<Register>(), IP);
5166 DCHECK_NE(destination.AsRegister<Register>(), IP);
5167 __ Mov(IP, source.AsRegister<Register>());
5168 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5169 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005170 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005171 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005172 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005173 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005174 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5175 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005176 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005177 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005178 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005179 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005180 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005181 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005182 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005183 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005184 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5185 destination.AsRegisterPairHigh<Register>(),
5186 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005187 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005188 Register low_reg = source.IsRegisterPair()
5189 ? source.AsRegisterPairLow<Register>()
5190 : destination.AsRegisterPairLow<Register>();
5191 int mem = source.IsRegisterPair()
5192 ? destination.GetStackIndex()
5193 : source.GetStackIndex();
5194 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005195 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005196 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005197 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005198 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005199 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5200 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005201 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005202 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005203 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005204 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5205 DRegister reg = source.IsFpuRegisterPair()
5206 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5207 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5208 int mem = source.IsFpuRegisterPair()
5209 ? destination.GetStackIndex()
5210 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005211 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005212 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005213 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005214 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5215 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5216 : destination.AsFpuRegister<SRegister>();
5217 int mem = source.IsFpuRegister()
5218 ? destination.GetStackIndex()
5219 : source.GetStackIndex();
5220
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005221 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005222 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005223 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005224 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005225 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5226 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005227 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005228 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005229 }
5230}
5231
5232void ParallelMoveResolverARM::SpillScratch(int reg) {
5233 __ Push(static_cast<Register>(reg));
5234}
5235
5236void ParallelMoveResolverARM::RestoreScratch(int reg) {
5237 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005238}
5239
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005240HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5241 HLoadClass::LoadKind desired_class_load_kind) {
5242 if (kEmitCompilerReadBarrier) {
5243 switch (desired_class_load_kind) {
5244 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5245 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5246 case HLoadClass::LoadKind::kBootImageAddress:
5247 // TODO: Implement for read barrier.
5248 return HLoadClass::LoadKind::kDexCacheViaMethod;
5249 default:
5250 break;
5251 }
5252 }
5253 switch (desired_class_load_kind) {
5254 case HLoadClass::LoadKind::kReferrersClass:
5255 break;
5256 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5257 DCHECK(!GetCompilerOptions().GetCompilePic());
5258 break;
5259 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5260 DCHECK(GetCompilerOptions().GetCompilePic());
5261 break;
5262 case HLoadClass::LoadKind::kBootImageAddress:
5263 break;
5264 case HLoadClass::LoadKind::kDexCacheAddress:
5265 DCHECK(Runtime::Current()->UseJitCompilation());
5266 break;
5267 case HLoadClass::LoadKind::kDexCachePcRelative:
5268 DCHECK(!Runtime::Current()->UseJitCompilation());
5269 // We disable pc-relative load when there is an irreducible loop, as the optimization
5270 // is incompatible with it.
5271 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5272 // with irreducible loops.
5273 if (GetGraph()->HasIrreducibleLoops()) {
5274 return HLoadClass::LoadKind::kDexCacheViaMethod;
5275 }
5276 break;
5277 case HLoadClass::LoadKind::kDexCacheViaMethod:
5278 break;
5279 }
5280 return desired_class_load_kind;
5281}
5282
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005283void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005284 if (cls->NeedsAccessCheck()) {
5285 InvokeRuntimeCallingConvention calling_convention;
5286 CodeGenerator::CreateLoadClassLocationSummary(
5287 cls,
5288 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5289 Location::RegisterLocation(R0),
5290 /* code_generator_supports_read_barrier */ true);
5291 return;
5292 }
5293
5294 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5295 ? LocationSummary::kCallOnSlowPath
5296 : LocationSummary::kNoCall;
5297 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5298 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5299 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5300 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5301 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5302 locations->SetInAt(0, Location::RequiresRegister());
5303 }
5304 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005305}
5306
5307void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005308 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005309 if (cls->NeedsAccessCheck()) {
5310 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5311 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5312 cls,
5313 cls->GetDexPc(),
5314 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005315 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005316 return;
5317 }
5318
Roland Levillain3b359c72015-11-17 19:35:12 +00005319 Location out_loc = locations->Out();
5320 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005321
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005322 bool generate_null_check = false;
5323 switch (cls->GetLoadKind()) {
5324 case HLoadClass::LoadKind::kReferrersClass: {
5325 DCHECK(!cls->CanCallRuntime());
5326 DCHECK(!cls->MustGenerateClinitCheck());
5327 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5328 Register current_method = locations->InAt(0).AsRegister<Register>();
5329 GenerateGcRootFieldLoad(
5330 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5331 break;
5332 }
5333 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5334 DCHECK(!kEmitCompilerReadBarrier);
5335 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5336 cls->GetTypeIndex()));
5337 break;
5338 }
5339 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5340 DCHECK(!kEmitCompilerReadBarrier);
5341 CodeGeneratorARM::PcRelativePatchInfo* labels =
5342 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5343 __ BindTrackedLabel(&labels->movw_label);
5344 __ movw(out, /* placeholder */ 0u);
5345 __ BindTrackedLabel(&labels->movt_label);
5346 __ movt(out, /* placeholder */ 0u);
5347 __ BindTrackedLabel(&labels->add_pc_label);
5348 __ add(out, out, ShifterOperand(PC));
5349 break;
5350 }
5351 case HLoadClass::LoadKind::kBootImageAddress: {
5352 DCHECK(!kEmitCompilerReadBarrier);
5353 DCHECK_NE(cls->GetAddress(), 0u);
5354 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5355 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5356 break;
5357 }
5358 case HLoadClass::LoadKind::kDexCacheAddress: {
5359 DCHECK_NE(cls->GetAddress(), 0u);
5360 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5361 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5362 // a 128B range. To try and reduce the number of literals if we load multiple types,
5363 // simply split the dex cache address to a 128B aligned base loaded from a literal
5364 // and the remaining offset embedded in the load.
5365 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5366 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5367 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5368 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5369 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5370 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5371 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5372 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5373 generate_null_check = !cls->IsInDexCache();
5374 break;
5375 }
5376 case HLoadClass::LoadKind::kDexCachePcRelative: {
5377 Register base_reg = locations->InAt(0).AsRegister<Register>();
5378 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5379 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5380 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5381 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5382 generate_null_check = !cls->IsInDexCache();
5383 break;
5384 }
5385 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5386 // /* GcRoot<mirror::Class>[] */ out =
5387 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5388 Register current_method = locations->InAt(0).AsRegister<Register>();
5389 __ LoadFromOffset(kLoadWord,
5390 out,
5391 current_method,
5392 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5393 // /* GcRoot<mirror::Class> */ out = out[type_index]
5394 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5395 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5396 generate_null_check = !cls->IsInDexCache();
5397 }
5398 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005399
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005400 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5401 DCHECK(cls->CanCallRuntime());
5402 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5403 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5404 codegen_->AddSlowPath(slow_path);
5405 if (generate_null_check) {
5406 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5407 }
5408 if (cls->MustGenerateClinitCheck()) {
5409 GenerateClassInitializationCheck(slow_path, out);
5410 } else {
5411 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005412 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005413 }
5414}
5415
5416void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5417 LocationSummary* locations =
5418 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5419 locations->SetInAt(0, Location::RequiresRegister());
5420 if (check->HasUses()) {
5421 locations->SetOut(Location::SameAsFirstInput());
5422 }
5423}
5424
5425void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005426 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005427 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005428 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005429 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005430 GenerateClassInitializationCheck(slow_path,
5431 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005432}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005433
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005434void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005435 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005436 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5437 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5438 __ b(slow_path->GetEntryLabel(), LT);
5439 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5440 // properly. Therefore, we do a memory fence.
5441 __ dmb(ISH);
5442 __ Bind(slow_path->GetExitLabel());
5443}
5444
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005445HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5446 HLoadString::LoadKind desired_string_load_kind) {
5447 if (kEmitCompilerReadBarrier) {
5448 switch (desired_string_load_kind) {
5449 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5450 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5451 case HLoadString::LoadKind::kBootImageAddress:
5452 // TODO: Implement for read barrier.
5453 return HLoadString::LoadKind::kDexCacheViaMethod;
5454 default:
5455 break;
5456 }
5457 }
5458 switch (desired_string_load_kind) {
5459 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5460 DCHECK(!GetCompilerOptions().GetCompilePic());
5461 break;
5462 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5463 DCHECK(GetCompilerOptions().GetCompilePic());
5464 break;
5465 case HLoadString::LoadKind::kBootImageAddress:
5466 break;
5467 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005468 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005469 break;
5470 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005471 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005472 // We disable pc-relative load when there is an irreducible loop, as the optimization
5473 // is incompatible with it.
5474 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5475 // with irreducible loops.
5476 if (GetGraph()->HasIrreducibleLoops()) {
5477 return HLoadString::LoadKind::kDexCacheViaMethod;
5478 }
5479 break;
5480 case HLoadString::LoadKind::kDexCacheViaMethod:
5481 break;
5482 }
5483 return desired_string_load_kind;
5484}
5485
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005486void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005487 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005488 ? LocationSummary::kCallOnSlowPath
5489 : LocationSummary::kNoCall;
5490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005491 HLoadString::LoadKind load_kind = load->GetLoadKind();
5492 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5493 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5494 locations->SetInAt(0, Location::RequiresRegister());
5495 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005496 locations->SetOut(Location::RequiresRegister());
5497}
5498
5499void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005500 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005501 Location out_loc = locations->Out();
5502 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005503
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005504 switch (load->GetLoadKind()) {
5505 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5506 DCHECK(!kEmitCompilerReadBarrier);
5507 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5508 load->GetStringIndex()));
5509 return; // No dex cache slow path.
5510 }
5511 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5512 DCHECK(!kEmitCompilerReadBarrier);
5513 CodeGeneratorARM::PcRelativePatchInfo* labels =
5514 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5515 __ BindTrackedLabel(&labels->movw_label);
5516 __ movw(out, /* placeholder */ 0u);
5517 __ BindTrackedLabel(&labels->movt_label);
5518 __ movt(out, /* placeholder */ 0u);
5519 __ BindTrackedLabel(&labels->add_pc_label);
5520 __ add(out, out, ShifterOperand(PC));
5521 return; // No dex cache slow path.
5522 }
5523 case HLoadString::LoadKind::kBootImageAddress: {
5524 DCHECK(!kEmitCompilerReadBarrier);
5525 DCHECK_NE(load->GetAddress(), 0u);
5526 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5527 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5528 return; // No dex cache slow path.
5529 }
5530 case HLoadString::LoadKind::kDexCacheAddress: {
5531 DCHECK_NE(load->GetAddress(), 0u);
5532 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5533 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5534 // a 128B range. To try and reduce the number of literals if we load multiple strings,
5535 // simply split the dex cache address to a 128B aligned base loaded from a literal
5536 // and the remaining offset embedded in the load.
5537 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
5538 DCHECK_ALIGNED(load->GetAddress(), 4u);
5539 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5540 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5541 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5542 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005543 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005544 GenerateGcRootFieldLoad(load, out_loc, out, offset);
5545 break;
5546 }
5547 case HLoadString::LoadKind::kDexCachePcRelative: {
5548 Register base_reg = locations->InAt(0).AsRegister<Register>();
5549 HArmDexCacheArraysBase* base = load->InputAt(0)->AsArmDexCacheArraysBase();
5550 int32_t offset = load->GetDexCacheElementOffset() - base->GetElementOffset();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551 // /* GcRoot<mirror::String> */ out = *(dex_cache_arrays_base + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005552 GenerateGcRootFieldLoad(load, out_loc, base_reg, offset);
5553 break;
5554 }
5555 case HLoadString::LoadKind::kDexCacheViaMethod: {
5556 Register current_method = locations->InAt(0).AsRegister<Register>();
5557
5558 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5559 GenerateGcRootFieldLoad(
5560 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5561 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5562 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
5563 // /* GcRoot<mirror::String> */ out = out[string_index]
5564 GenerateGcRootFieldLoad(
5565 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
5566 break;
5567 }
5568 default:
5569 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5570 UNREACHABLE();
5571 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005572
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005573 if (!load->IsInDexCache()) {
5574 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5575 codegen_->AddSlowPath(slow_path);
5576 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5577 __ Bind(slow_path->GetExitLabel());
5578 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005579}
5580
David Brazdilcb1c0552015-08-04 16:22:25 +01005581static int32_t GetExceptionTlsOffset() {
5582 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5583}
5584
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005585void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5586 LocationSummary* locations =
5587 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5588 locations->SetOut(Location::RequiresRegister());
5589}
5590
5591void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005592 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005593 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5594}
5595
5596void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5597 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5598}
5599
5600void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005601 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005602 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005603}
5604
5605void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5606 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005607 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005608 InvokeRuntimeCallingConvention calling_convention;
5609 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5610}
5611
5612void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5613 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005614 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005615 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005616}
5617
Roland Levillainc9285912015-12-18 10:38:42 +00005618static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5619 return kEmitCompilerReadBarrier &&
5620 (kUseBakerReadBarrier ||
5621 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5622 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5623 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5624}
5625
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005626void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005627 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005628 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5629 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005630 case TypeCheckKind::kExactCheck:
5631 case TypeCheckKind::kAbstractClassCheck:
5632 case TypeCheckKind::kClassHierarchyCheck:
5633 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005634 call_kind =
5635 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005636 break;
5637 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005638 case TypeCheckKind::kUnresolvedCheck:
5639 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005640 call_kind = LocationSummary::kCallOnSlowPath;
5641 break;
5642 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005643
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005644 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005645 locations->SetInAt(0, Location::RequiresRegister());
5646 locations->SetInAt(1, Location::RequiresRegister());
5647 // The "out" register is used as a temporary, so it overlaps with the inputs.
5648 // Note that TypeCheckSlowPathARM uses this register too.
5649 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5650 // When read barriers are enabled, we need a temporary register for
5651 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005652 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005653 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005654 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005655}
5656
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005657void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005658 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005659 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005660 Location obj_loc = locations->InAt(0);
5661 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005662 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005663 Location out_loc = locations->Out();
5664 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005665 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005666 locations->GetTemp(0) :
5667 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005668 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005669 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5670 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5671 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005672 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005673 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005674
5675 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005676 // avoid null check if we know obj is not null.
5677 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005678 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005679 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005680
Roland Levillain3b359c72015-11-17 19:35:12 +00005681 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005682 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683
Roland Levillainc9285912015-12-18 10:38:42 +00005684 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005685 case TypeCheckKind::kExactCheck: {
5686 __ cmp(out, ShifterOperand(cls));
5687 // Classes must be equal for the instanceof to succeed.
5688 __ b(&zero, NE);
5689 __ LoadImmediate(out, 1);
5690 __ b(&done);
5691 break;
5692 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005693
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005694 case TypeCheckKind::kAbstractClassCheck: {
5695 // If the class is abstract, we eagerly fetch the super class of the
5696 // object to avoid doing a comparison we know will fail.
5697 Label loop;
5698 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005699 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005700 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005701 // If `out` is null, we use it for the result, and jump to `done`.
5702 __ CompareAndBranchIfZero(out, &done);
5703 __ cmp(out, ShifterOperand(cls));
5704 __ b(&loop, NE);
5705 __ LoadImmediate(out, 1);
5706 if (zero.IsLinked()) {
5707 __ b(&done);
5708 }
5709 break;
5710 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005711
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005712 case TypeCheckKind::kClassHierarchyCheck: {
5713 // Walk over the class hierarchy to find a match.
5714 Label loop, success;
5715 __ Bind(&loop);
5716 __ cmp(out, ShifterOperand(cls));
5717 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005718 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005719 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 __ CompareAndBranchIfNonZero(out, &loop);
5721 // If `out` is null, we use it for the result, and jump to `done`.
5722 __ b(&done);
5723 __ Bind(&success);
5724 __ LoadImmediate(out, 1);
5725 if (zero.IsLinked()) {
5726 __ b(&done);
5727 }
5728 break;
5729 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005730
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005731 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005732 // Do an exact check.
5733 Label exact_check;
5734 __ cmp(out, ShifterOperand(cls));
5735 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005736 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005737 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005738 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005739 // If `out` is null, we use it for the result, and jump to `done`.
5740 __ CompareAndBranchIfZero(out, &done);
5741 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5742 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5743 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005744 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005745 __ LoadImmediate(out, 1);
5746 __ b(&done);
5747 break;
5748 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005750 case TypeCheckKind::kArrayCheck: {
5751 __ cmp(out, ShifterOperand(cls));
5752 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005753 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5754 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005755 codegen_->AddSlowPath(slow_path);
5756 __ b(slow_path->GetEntryLabel(), NE);
5757 __ LoadImmediate(out, 1);
5758 if (zero.IsLinked()) {
5759 __ b(&done);
5760 }
5761 break;
5762 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005763
Calin Juravle98893e12015-10-02 21:05:03 +01005764 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005765 case TypeCheckKind::kInterfaceCheck: {
5766 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005767 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005768 // cases.
5769 //
5770 // We cannot directly call the InstanceofNonTrivial runtime
5771 // entry point without resorting to a type checking slow path
5772 // here (i.e. by calling InvokeRuntime directly), as it would
5773 // require to assign fixed registers for the inputs of this
5774 // HInstanceOf instruction (following the runtime calling
5775 // convention), which might be cluttered by the potential first
5776 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005777 //
5778 // TODO: Introduce a new runtime entry point taking the object
5779 // to test (instead of its class) as argument, and let it deal
5780 // with the read barrier issues. This will let us refactor this
5781 // case of the `switch` code as it was previously (with a direct
5782 // call to the runtime not using a type checking slow path).
5783 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005784 DCHECK(locations->OnlyCallsOnSlowPath());
5785 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5786 /* is_fatal */ false);
5787 codegen_->AddSlowPath(slow_path);
5788 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005789 if (zero.IsLinked()) {
5790 __ b(&done);
5791 }
5792 break;
5793 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005794 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005795
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005796 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005797 __ Bind(&zero);
5798 __ LoadImmediate(out, 0);
5799 }
5800
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005801 if (done.IsLinked()) {
5802 __ Bind(&done);
5803 }
5804
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005805 if (slow_path != nullptr) {
5806 __ Bind(slow_path->GetExitLabel());
5807 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005808}
5809
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005810void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005811 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5812 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5813
Roland Levillain3b359c72015-11-17 19:35:12 +00005814 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5815 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005816 case TypeCheckKind::kExactCheck:
5817 case TypeCheckKind::kAbstractClassCheck:
5818 case TypeCheckKind::kClassHierarchyCheck:
5819 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005820 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5821 LocationSummary::kCallOnSlowPath :
5822 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005823 break;
5824 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005825 case TypeCheckKind::kUnresolvedCheck:
5826 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005827 call_kind = LocationSummary::kCallOnSlowPath;
5828 break;
5829 }
5830
Roland Levillain3b359c72015-11-17 19:35:12 +00005831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5832 locations->SetInAt(0, Location::RequiresRegister());
5833 locations->SetInAt(1, Location::RequiresRegister());
5834 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5835 locations->AddTemp(Location::RequiresRegister());
5836 // When read barriers are enabled, we need an additional temporary
5837 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005838 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005839 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005841}
5842
5843void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005844 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005845 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005846 Location obj_loc = locations->InAt(0);
5847 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005848 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005849 Location temp_loc = locations->GetTemp(0);
5850 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005851 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005852 locations->GetTemp(1) :
5853 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005854 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005855 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5856 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5857 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005858
Roland Levillain3b359c72015-11-17 19:35:12 +00005859 bool is_type_check_slow_path_fatal =
5860 (type_check_kind == TypeCheckKind::kExactCheck ||
5861 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5862 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5863 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5864 !instruction->CanThrowIntoCatchBlock();
5865 SlowPathCode* type_check_slow_path =
5866 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5867 is_type_check_slow_path_fatal);
5868 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005869
5870 Label done;
5871 // Avoid null check if we know obj is not null.
5872 if (instruction->MustDoNullCheck()) {
5873 __ CompareAndBranchIfZero(obj, &done);
5874 }
5875
Roland Levillain3b359c72015-11-17 19:35:12 +00005876 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005877 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005878
Roland Levillain3b359c72015-11-17 19:35:12 +00005879 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005880 case TypeCheckKind::kExactCheck:
5881 case TypeCheckKind::kArrayCheck: {
5882 __ cmp(temp, ShifterOperand(cls));
5883 // Jump to slow path for throwing the exception or doing a
5884 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005885 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005886 break;
5887 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005888
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 case TypeCheckKind::kAbstractClassCheck: {
5890 // If the class is abstract, we eagerly fetch the super class of the
5891 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005892 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005893 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005894 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005895 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005896
5897 // If the class reference currently in `temp` is not null, jump
5898 // to the `compare_classes` label to compare it with the checked
5899 // class.
5900 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5901 // Otherwise, jump to the slow path to throw the exception.
5902 //
5903 // But before, move back the object's class into `temp` before
5904 // going into the slow path, as it has been overwritten in the
5905 // meantime.
5906 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005907 GenerateReferenceLoadTwoRegisters(
5908 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005909 __ b(type_check_slow_path->GetEntryLabel());
5910
5911 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 __ cmp(temp, ShifterOperand(cls));
5913 __ b(&loop, NE);
5914 break;
5915 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005916
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 case TypeCheckKind::kClassHierarchyCheck: {
5918 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005919 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005920 __ Bind(&loop);
5921 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005922 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005923
Roland Levillain3b359c72015-11-17 19:35:12 +00005924 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005925 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005926
5927 // If the class reference currently in `temp` is not null, jump
5928 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005930 // Otherwise, jump to the slow path to throw the exception.
5931 //
5932 // But before, move back the object's class into `temp` before
5933 // going into the slow path, as it has been overwritten in the
5934 // meantime.
5935 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005936 GenerateReferenceLoadTwoRegisters(
5937 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005938 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005939 break;
5940 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005941
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005942 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005943 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005944 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005945 __ cmp(temp, ShifterOperand(cls));
5946 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005947
5948 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005949 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005950 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005951
5952 // If the component type is not null (i.e. the object is indeed
5953 // an array), jump to label `check_non_primitive_component_type`
5954 // to further check that this component type is not a primitive
5955 // type.
5956 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5957 // Otherwise, jump to the slow path to throw the exception.
5958 //
5959 // But before, move back the object's class into `temp` before
5960 // going into the slow path, as it has been overwritten in the
5961 // meantime.
5962 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005963 GenerateReferenceLoadTwoRegisters(
5964 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005965 __ b(type_check_slow_path->GetEntryLabel());
5966
5967 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005969 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5970 __ CompareAndBranchIfZero(temp, &done);
5971 // Same comment as above regarding `temp` and the slow path.
5972 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005973 GenerateReferenceLoadTwoRegisters(
5974 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005975 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005976 break;
5977 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005978
Calin Juravle98893e12015-10-02 21:05:03 +01005979 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005981 // We always go into the type check slow path for the unresolved
5982 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005983 //
5984 // We cannot directly call the CheckCast runtime entry point
5985 // without resorting to a type checking slow path here (i.e. by
5986 // calling InvokeRuntime directly), as it would require to
5987 // assign fixed registers for the inputs of this HInstanceOf
5988 // instruction (following the runtime calling convention), which
5989 // might be cluttered by the potential first read barrier
5990 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005991 //
5992 // TODO: Introduce a new runtime entry point taking the object
5993 // to test (instead of its class) as argument, and let it deal
5994 // with the read barrier issues. This will let us refactor this
5995 // case of the `switch` code as it was previously (with a direct
5996 // call to the runtime not using a type checking slow path).
5997 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005998 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999 break;
6000 }
6001 __ Bind(&done);
6002
Roland Levillain3b359c72015-11-17 19:35:12 +00006003 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006004}
6005
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006006void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6007 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006009 InvokeRuntimeCallingConvention calling_convention;
6010 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6011}
6012
6013void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6014 codegen_->InvokeRuntime(instruction->IsEnter()
6015 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
6016 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00006017 instruction->GetDexPc(),
6018 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006019 if (instruction->IsEnter()) {
6020 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6021 } else {
6022 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6023 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006024}
6025
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006026void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6027void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6028void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006029
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006030void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006031 LocationSummary* locations =
6032 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6033 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6034 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006035 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006036 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006037 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006038 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006039}
6040
6041void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6042 HandleBitwiseOperation(instruction);
6043}
6044
6045void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6046 HandleBitwiseOperation(instruction);
6047}
6048
6049void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6050 HandleBitwiseOperation(instruction);
6051}
6052
Artem Serov7fc63502016-02-09 17:15:29 +00006053
6054void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6055 LocationSummary* locations =
6056 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6057 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6058 || instruction->GetResultType() == Primitive::kPrimLong);
6059
6060 locations->SetInAt(0, Location::RequiresRegister());
6061 locations->SetInAt(1, Location::RequiresRegister());
6062 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6063}
6064
6065void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6066 LocationSummary* locations = instruction->GetLocations();
6067 Location first = locations->InAt(0);
6068 Location second = locations->InAt(1);
6069 Location out = locations->Out();
6070
6071 if (instruction->GetResultType() == Primitive::kPrimInt) {
6072 Register first_reg = first.AsRegister<Register>();
6073 ShifterOperand second_reg(second.AsRegister<Register>());
6074 Register out_reg = out.AsRegister<Register>();
6075
6076 switch (instruction->GetOpKind()) {
6077 case HInstruction::kAnd:
6078 __ bic(out_reg, first_reg, second_reg);
6079 break;
6080 case HInstruction::kOr:
6081 __ orn(out_reg, first_reg, second_reg);
6082 break;
6083 // There is no EON on arm.
6084 case HInstruction::kXor:
6085 default:
6086 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6087 UNREACHABLE();
6088 }
6089 return;
6090
6091 } else {
6092 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6093 Register first_low = first.AsRegisterPairLow<Register>();
6094 Register first_high = first.AsRegisterPairHigh<Register>();
6095 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6096 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6097 Register out_low = out.AsRegisterPairLow<Register>();
6098 Register out_high = out.AsRegisterPairHigh<Register>();
6099
6100 switch (instruction->GetOpKind()) {
6101 case HInstruction::kAnd:
6102 __ bic(out_low, first_low, second_low);
6103 __ bic(out_high, first_high, second_high);
6104 break;
6105 case HInstruction::kOr:
6106 __ orn(out_low, first_low, second_low);
6107 __ orn(out_high, first_high, second_high);
6108 break;
6109 // There is no EON on arm.
6110 case HInstruction::kXor:
6111 default:
6112 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6113 UNREACHABLE();
6114 }
6115 }
6116}
6117
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006118void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6119 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6120 if (value == 0xffffffffu) {
6121 if (out != first) {
6122 __ mov(out, ShifterOperand(first));
6123 }
6124 return;
6125 }
6126 if (value == 0u) {
6127 __ mov(out, ShifterOperand(0));
6128 return;
6129 }
6130 ShifterOperand so;
6131 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6132 __ and_(out, first, so);
6133 } else {
6134 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6135 __ bic(out, first, ShifterOperand(~value));
6136 }
6137}
6138
6139void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6140 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6141 if (value == 0u) {
6142 if (out != first) {
6143 __ mov(out, ShifterOperand(first));
6144 }
6145 return;
6146 }
6147 if (value == 0xffffffffu) {
6148 __ mvn(out, ShifterOperand(0));
6149 return;
6150 }
6151 ShifterOperand so;
6152 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6153 __ orr(out, first, so);
6154 } else {
6155 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6156 __ orn(out, first, ShifterOperand(~value));
6157 }
6158}
6159
6160void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6161 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6162 if (value == 0u) {
6163 if (out != first) {
6164 __ mov(out, ShifterOperand(first));
6165 }
6166 return;
6167 }
6168 __ eor(out, first, ShifterOperand(value));
6169}
6170
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006171void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6172 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006173 Location first = locations->InAt(0);
6174 Location second = locations->InAt(1);
6175 Location out = locations->Out();
6176
6177 if (second.IsConstant()) {
6178 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6179 uint32_t value_low = Low32Bits(value);
6180 if (instruction->GetResultType() == Primitive::kPrimInt) {
6181 Register first_reg = first.AsRegister<Register>();
6182 Register out_reg = out.AsRegister<Register>();
6183 if (instruction->IsAnd()) {
6184 GenerateAndConst(out_reg, first_reg, value_low);
6185 } else if (instruction->IsOr()) {
6186 GenerateOrrConst(out_reg, first_reg, value_low);
6187 } else {
6188 DCHECK(instruction->IsXor());
6189 GenerateEorConst(out_reg, first_reg, value_low);
6190 }
6191 } else {
6192 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6193 uint32_t value_high = High32Bits(value);
6194 Register first_low = first.AsRegisterPairLow<Register>();
6195 Register first_high = first.AsRegisterPairHigh<Register>();
6196 Register out_low = out.AsRegisterPairLow<Register>();
6197 Register out_high = out.AsRegisterPairHigh<Register>();
6198 if (instruction->IsAnd()) {
6199 GenerateAndConst(out_low, first_low, value_low);
6200 GenerateAndConst(out_high, first_high, value_high);
6201 } else if (instruction->IsOr()) {
6202 GenerateOrrConst(out_low, first_low, value_low);
6203 GenerateOrrConst(out_high, first_high, value_high);
6204 } else {
6205 DCHECK(instruction->IsXor());
6206 GenerateEorConst(out_low, first_low, value_low);
6207 GenerateEorConst(out_high, first_high, value_high);
6208 }
6209 }
6210 return;
6211 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006212
6213 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006214 Register first_reg = first.AsRegister<Register>();
6215 ShifterOperand second_reg(second.AsRegister<Register>());
6216 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006217 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006218 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006219 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006220 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006221 } else {
6222 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006223 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006224 }
6225 } else {
6226 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006227 Register first_low = first.AsRegisterPairLow<Register>();
6228 Register first_high = first.AsRegisterPairHigh<Register>();
6229 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6230 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6231 Register out_low = out.AsRegisterPairLow<Register>();
6232 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006233 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006234 __ and_(out_low, first_low, second_low);
6235 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006236 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006237 __ orr(out_low, first_low, second_low);
6238 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006239 } else {
6240 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006241 __ eor(out_low, first_low, second_low);
6242 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006243 }
6244 }
6245}
6246
Roland Levillainc9285912015-12-18 10:38:42 +00006247void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6248 Location out,
6249 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006250 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006251 Register out_reg = out.AsRegister<Register>();
6252 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006253 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006254 if (kUseBakerReadBarrier) {
6255 // Load with fast path based Baker's read barrier.
6256 // /* HeapReference<Object> */ out = *(out + offset)
6257 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006258 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006259 } else {
6260 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006261 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006262 // in the following move operation, as we will need it for the
6263 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006264 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006265 // /* HeapReference<Object> */ out = *(out + offset)
6266 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006267 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006268 }
6269 } else {
6270 // Plain load with no read barrier.
6271 // /* HeapReference<Object> */ out = *(out + offset)
6272 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6273 __ MaybeUnpoisonHeapReference(out_reg);
6274 }
6275}
6276
6277void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6278 Location out,
6279 Location obj,
6280 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006281 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006282 Register out_reg = out.AsRegister<Register>();
6283 Register obj_reg = obj.AsRegister<Register>();
6284 if (kEmitCompilerReadBarrier) {
6285 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006286 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006287 // Load with fast path based Baker's read barrier.
6288 // /* HeapReference<Object> */ out = *(obj + offset)
6289 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006290 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006291 } else {
6292 // Load with slow path based read barrier.
6293 // /* HeapReference<Object> */ out = *(obj + offset)
6294 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6295 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6296 }
6297 } else {
6298 // Plain load with no read barrier.
6299 // /* HeapReference<Object> */ out = *(obj + offset)
6300 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6301 __ MaybeUnpoisonHeapReference(out_reg);
6302 }
6303}
6304
6305void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6306 Location root,
6307 Register obj,
6308 uint32_t offset) {
6309 Register root_reg = root.AsRegister<Register>();
6310 if (kEmitCompilerReadBarrier) {
6311 if (kUseBakerReadBarrier) {
6312 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6313 // Baker's read barrier are used:
6314 //
6315 // root = obj.field;
6316 // if (Thread::Current()->GetIsGcMarking()) {
6317 // root = ReadBarrier::Mark(root)
6318 // }
6319
6320 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6321 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6322 static_assert(
6323 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6324 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6325 "have different sizes.");
6326 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6327 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6328 "have different sizes.");
6329
6330 // Slow path used to mark the GC root `root`.
6331 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006332 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006333 codegen_->AddSlowPath(slow_path);
6334
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006335 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006336 __ LoadFromOffset(
6337 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6338 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6339 __ Bind(slow_path->GetExitLabel());
6340 } else {
6341 // GC root loaded through a slow path for read barriers other
6342 // than Baker's.
6343 // /* GcRoot<mirror::Object>* */ root = obj + offset
6344 __ AddConstant(root_reg, obj, offset);
6345 // /* mirror::Object* */ root = root->Read()
6346 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6347 }
6348 } else {
6349 // Plain GC root load with no read barrier.
6350 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6351 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6352 // Note that GC roots are not affected by heap poisoning, thus we
6353 // do not have to unpoison `root_reg` here.
6354 }
6355}
6356
6357void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6358 Location ref,
6359 Register obj,
6360 uint32_t offset,
6361 Location temp,
6362 bool needs_null_check) {
6363 DCHECK(kEmitCompilerReadBarrier);
6364 DCHECK(kUseBakerReadBarrier);
6365
6366 // /* HeapReference<Object> */ ref = *(obj + offset)
6367 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006368 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006369 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006370 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006371}
6372
6373void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6374 Location ref,
6375 Register obj,
6376 uint32_t data_offset,
6377 Location index,
6378 Location temp,
6379 bool needs_null_check) {
6380 DCHECK(kEmitCompilerReadBarrier);
6381 DCHECK(kUseBakerReadBarrier);
6382
Roland Levillainbfea3352016-06-23 13:48:47 +01006383 static_assert(
6384 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6385 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006386 // /* HeapReference<Object> */ ref =
6387 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006388 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006389 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006390 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006391}
6392
6393void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6394 Location ref,
6395 Register obj,
6396 uint32_t offset,
6397 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006398 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006399 Location temp,
6400 bool needs_null_check) {
6401 DCHECK(kEmitCompilerReadBarrier);
6402 DCHECK(kUseBakerReadBarrier);
6403
6404 // In slow path based read barriers, the read barrier call is
6405 // inserted after the original load. However, in fast path based
6406 // Baker's read barriers, we need to perform the load of
6407 // mirror::Object::monitor_ *before* the original reference load.
6408 // This load-load ordering is required by the read barrier.
6409 // The fast path/slow path (for Baker's algorithm) should look like:
6410 //
6411 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6412 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6413 // HeapReference<Object> ref = *src; // Original reference load.
6414 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6415 // if (is_gray) {
6416 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6417 // }
6418 //
6419 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006420 // slightly more complex as it performs additional checks that we do
6421 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006422
6423 Register ref_reg = ref.AsRegister<Register>();
6424 Register temp_reg = temp.AsRegister<Register>();
6425 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6426
6427 // /* int32_t */ monitor = obj->monitor_
6428 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6429 if (needs_null_check) {
6430 MaybeRecordImplicitNullCheck(instruction);
6431 }
6432 // /* LockWord */ lock_word = LockWord(monitor)
6433 static_assert(sizeof(LockWord) == sizeof(int32_t),
6434 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006435
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006436 // Introduce a dependency on the lock_word including the rb_state,
6437 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006438 // a memory barrier (which would be more expensive).
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006439 // obj is unchanged by this operation, but its value now depends on temp_reg.
6440 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006441
6442 // The actual reference load.
6443 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006444 // Load types involving an "index": ArrayGet and
6445 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6446 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006447 if (index.IsConstant()) {
6448 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006449 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006450 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6451 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006452 // Handle the special case of the
6453 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6454 // a register pair as index ("long offset"), of which only the low
6455 // part contains data.
6456 Register index_reg = index.IsRegisterPair()
6457 ? index.AsRegisterPairLow<Register>()
6458 : index.AsRegister<Register>();
6459 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006460 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6461 }
6462 } else {
6463 // /* HeapReference<Object> */ ref = *(obj + offset)
6464 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6465 }
6466
6467 // Object* ref = ref_addr->AsMirrorPtr()
6468 __ MaybeUnpoisonHeapReference(ref_reg);
6469
6470 // Slow path used to mark the object `ref` when it is gray.
6471 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006472 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006473 AddSlowPath(slow_path);
6474
6475 // if (rb_state == ReadBarrier::gray_ptr_)
6476 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006477 // Given the numeric representation, it's enough to check the low bit of the
6478 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6479 // which can be a 16-bit instruction unlike the TST immediate.
6480 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6481 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6482 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6483 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6484 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006485 __ Bind(slow_path->GetExitLabel());
6486}
6487
6488void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6489 Location out,
6490 Location ref,
6491 Location obj,
6492 uint32_t offset,
6493 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006494 DCHECK(kEmitCompilerReadBarrier);
6495
Roland Levillainc9285912015-12-18 10:38:42 +00006496 // Insert a slow path based read barrier *after* the reference load.
6497 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006498 // If heap poisoning is enabled, the unpoisoning of the loaded
6499 // reference will be carried out by the runtime within the slow
6500 // path.
6501 //
6502 // Note that `ref` currently does not get unpoisoned (when heap
6503 // poisoning is enabled), which is alright as the `ref` argument is
6504 // not used by the artReadBarrierSlow entry point.
6505 //
6506 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6507 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6508 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6509 AddSlowPath(slow_path);
6510
Roland Levillain3b359c72015-11-17 19:35:12 +00006511 __ b(slow_path->GetEntryLabel());
6512 __ Bind(slow_path->GetExitLabel());
6513}
6514
Roland Levillainc9285912015-12-18 10:38:42 +00006515void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6516 Location out,
6517 Location ref,
6518 Location obj,
6519 uint32_t offset,
6520 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006521 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006522 // Baker's read barriers shall be handled by the fast path
6523 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6524 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006525 // If heap poisoning is enabled, unpoisoning will be taken care of
6526 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006527 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006528 } else if (kPoisonHeapReferences) {
6529 __ UnpoisonHeapReference(out.AsRegister<Register>());
6530 }
6531}
6532
Roland Levillainc9285912015-12-18 10:38:42 +00006533void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6534 Location out,
6535 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006536 DCHECK(kEmitCompilerReadBarrier);
6537
Roland Levillainc9285912015-12-18 10:38:42 +00006538 // Insert a slow path based read barrier *after* the GC root load.
6539 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006540 // Note that GC roots are not affected by heap poisoning, so we do
6541 // not need to do anything special for this here.
6542 SlowPathCode* slow_path =
6543 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6544 AddSlowPath(slow_path);
6545
Roland Levillain3b359c72015-11-17 19:35:12 +00006546 __ b(slow_path->GetEntryLabel());
6547 __ Bind(slow_path->GetExitLabel());
6548}
6549
Vladimir Markodc151b22015-10-15 18:02:30 +01006550HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6551 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6552 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006553 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6554 // We disable pc-relative load when there is an irreducible loop, as the optimization
6555 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006556 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6557 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006558 if (GetGraph()->HasIrreducibleLoops() &&
6559 (dispatch_info.method_load_kind ==
6560 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6561 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6562 }
6563
6564 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006565 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6566 if (&outer_dex_file != target_method.dex_file) {
6567 // Calls across dex files are more likely to exceed the available BL range,
6568 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6569 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6570 (desired_dispatch_info.method_load_kind ==
6571 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6572 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6573 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6574 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006575 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006576 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006577 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006578 0u
6579 };
6580 }
6581 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006582 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006583}
6584
Vladimir Markob4536b72015-11-24 13:45:23 +00006585Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6586 Register temp) {
6587 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6588 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6589 if (!invoke->GetLocations()->Intrinsified()) {
6590 return location.AsRegister<Register>();
6591 }
6592 // For intrinsics we allow any location, so it may be on the stack.
6593 if (!location.IsRegister()) {
6594 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6595 return temp;
6596 }
6597 // For register locations, check if the register was saved. If so, get it from the stack.
6598 // Note: There is a chance that the register was saved but not overwritten, so we could
6599 // save one load. However, since this is just an intrinsic slow path we prefer this
6600 // simple and more robust approach rather that trying to determine if that's the case.
6601 SlowPathCode* slow_path = GetCurrentSlowPath();
6602 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6603 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6604 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6605 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6606 return temp;
6607 }
6608 return location.AsRegister<Register>();
6609}
6610
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006611void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006612 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006613 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006614 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6615 // LR = code address from literal pool with link-time patch.
6616 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006617 break;
6618 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6619 // LR = invoke->GetDirectCodePtr();
6620 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006621 break;
6622 default:
6623 break;
6624 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006625
Vladimir Marko58155012015-08-19 12:49:41 +00006626 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6627 switch (invoke->GetMethodLoadKind()) {
6628 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6629 // temp = thread->string_init_entrypoint
6630 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6631 break;
6632 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006633 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006634 break;
6635 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6636 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6637 break;
6638 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6639 __ LoadLiteral(temp.AsRegister<Register>(),
6640 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6641 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006642 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6643 HArmDexCacheArraysBase* base =
6644 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6645 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6646 temp.AsRegister<Register>());
6647 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6648 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6649 break;
6650 }
Vladimir Marko58155012015-08-19 12:49:41 +00006651 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006652 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006653 Register method_reg;
6654 Register reg = temp.AsRegister<Register>();
6655 if (current_method.IsRegister()) {
6656 method_reg = current_method.AsRegister<Register>();
6657 } else {
6658 DCHECK(invoke->GetLocations()->Intrinsified());
6659 DCHECK(!current_method.IsValid());
6660 method_reg = reg;
6661 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6662 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006663 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6664 __ LoadFromOffset(kLoadWord,
6665 reg,
6666 method_reg,
6667 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006668 // temp = temp[index_in_cache];
6669 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6670 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006671 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6672 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006673 }
Vladimir Marko58155012015-08-19 12:49:41 +00006674 }
6675
6676 switch (invoke->GetCodePtrLocation()) {
6677 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6678 __ bl(GetFrameEntryLabel());
6679 break;
6680 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006681 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006682 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006683 // Arbitrarily branch to the BL itself, override at link time.
6684 __ bl(&relative_call_patches_.back().label);
6685 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006686 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6687 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6688 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006689 // LR()
6690 __ blx(LR);
6691 break;
6692 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6693 // LR = callee_method->entry_point_from_quick_compiled_code_
6694 __ LoadFromOffset(
6695 kLoadWord, LR, callee_method.AsRegister<Register>(),
6696 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6697 // LR()
6698 __ blx(LR);
6699 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006700 }
6701
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006702 DCHECK(!IsLeafMethod());
6703}
6704
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006705void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6706 Register temp = temp_location.AsRegister<Register>();
6707 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6708 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006709
6710 // Use the calling convention instead of the location of the receiver, as
6711 // intrinsics may have put the receiver in a different register. In the intrinsics
6712 // slow path, the arguments have been moved to the right place, so here we are
6713 // guaranteed that the receiver is the first register of the calling convention.
6714 InvokeDexCallingConvention calling_convention;
6715 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006716 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006717 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006718 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006719 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006720 // Instead of simply (possibly) unpoisoning `temp` here, we should
6721 // emit a read barrier for the previous class reference load.
6722 // However this is not required in practice, as this is an
6723 // intermediate/temporary reference and because the current
6724 // concurrent copying collector keeps the from-space memory
6725 // intact/accessible until the end of the marking phase (the
6726 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006727 __ MaybeUnpoisonHeapReference(temp);
6728 // temp = temp->GetMethodAt(method_offset);
6729 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6730 kArmWordSize).Int32Value();
6731 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6732 // LR = temp->GetEntryPoint();
6733 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6734 // LR();
6735 __ blx(LR);
6736}
6737
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006738CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6739 const DexFile& dex_file, uint32_t string_index) {
6740 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6741}
6742
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006743CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6744 const DexFile& dex_file, uint32_t type_index) {
6745 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6746}
6747
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006748CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6749 const DexFile& dex_file, uint32_t element_offset) {
6750 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6751}
6752
6753CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6754 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6755 patches->emplace_back(dex_file, offset_or_index);
6756 return &patches->back();
6757}
6758
6759Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6760 uint32_t string_index) {
6761 return boot_image_string_patches_.GetOrCreate(
6762 StringReference(&dex_file, string_index),
6763 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6764}
6765
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006766Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6767 uint32_t type_index) {
6768 return boot_image_type_patches_.GetOrCreate(
6769 TypeReference(&dex_file, type_index),
6770 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6771}
6772
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006773Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6774 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6775 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6776 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6777}
6778
6779Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6780 return DeduplicateUint32Literal(address, &uint32_literals_);
6781}
6782
Vladimir Marko58155012015-08-19 12:49:41 +00006783void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6784 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006785 size_t size =
6786 method_patches_.size() +
6787 call_patches_.size() +
6788 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006789 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6790 boot_image_string_patches_.size() +
6791 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006792 boot_image_type_patches_.size() +
6793 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006794 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006795 linker_patches->reserve(size);
6796 for (const auto& entry : method_patches_) {
6797 const MethodReference& target_method = entry.first;
6798 Literal* literal = entry.second;
6799 DCHECK(literal->GetLabel()->IsBound());
6800 uint32_t literal_offset = literal->GetLabel()->Position();
6801 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6802 target_method.dex_file,
6803 target_method.dex_method_index));
6804 }
6805 for (const auto& entry : call_patches_) {
6806 const MethodReference& target_method = entry.first;
6807 Literal* literal = entry.second;
6808 DCHECK(literal->GetLabel()->IsBound());
6809 uint32_t literal_offset = literal->GetLabel()->Position();
6810 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6811 target_method.dex_file,
6812 target_method.dex_method_index));
6813 }
6814 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6815 uint32_t literal_offset = info.label.Position();
6816 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6817 info.target_method.dex_file,
6818 info.target_method.dex_method_index));
6819 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006820 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6821 const DexFile& dex_file = info.target_dex_file;
6822 size_t base_element_offset = info.offset_or_index;
6823 DCHECK(info.add_pc_label.IsBound());
6824 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006825 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006826 DCHECK(info.movw_label.IsBound());
6827 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006828 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6829 &dex_file,
6830 add_pc_offset,
6831 base_element_offset));
6832 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006833 DCHECK(info.movt_label.IsBound());
6834 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006835 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6836 &dex_file,
6837 add_pc_offset,
6838 base_element_offset));
6839 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006840 for (const auto& entry : boot_image_string_patches_) {
6841 const StringReference& target_string = entry.first;
6842 Literal* literal = entry.second;
6843 DCHECK(literal->GetLabel()->IsBound());
6844 uint32_t literal_offset = literal->GetLabel()->Position();
6845 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6846 target_string.dex_file,
6847 target_string.string_index));
6848 }
6849 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6850 const DexFile& dex_file = info.target_dex_file;
6851 uint32_t string_index = info.offset_or_index;
6852 DCHECK(info.add_pc_label.IsBound());
6853 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6854 // Add MOVW patch.
6855 DCHECK(info.movw_label.IsBound());
6856 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6857 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6858 &dex_file,
6859 add_pc_offset,
6860 string_index));
6861 // Add MOVT patch.
6862 DCHECK(info.movt_label.IsBound());
6863 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6864 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6865 &dex_file,
6866 add_pc_offset,
6867 string_index));
6868 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006869 for (const auto& entry : boot_image_type_patches_) {
6870 const TypeReference& target_type = entry.first;
6871 Literal* literal = entry.second;
6872 DCHECK(literal->GetLabel()->IsBound());
6873 uint32_t literal_offset = literal->GetLabel()->Position();
6874 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6875 target_type.dex_file,
6876 target_type.type_index));
6877 }
6878 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6879 const DexFile& dex_file = info.target_dex_file;
6880 uint32_t type_index = info.offset_or_index;
6881 DCHECK(info.add_pc_label.IsBound());
6882 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6883 // Add MOVW patch.
6884 DCHECK(info.movw_label.IsBound());
6885 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6886 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6887 &dex_file,
6888 add_pc_offset,
6889 type_index));
6890 // Add MOVT patch.
6891 DCHECK(info.movt_label.IsBound());
6892 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6893 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6894 &dex_file,
6895 add_pc_offset,
6896 type_index));
6897 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006898 for (const auto& entry : boot_image_address_patches_) {
6899 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6900 Literal* literal = entry.second;
6901 DCHECK(literal->GetLabel()->IsBound());
6902 uint32_t literal_offset = literal->GetLabel()->Position();
6903 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6904 }
6905}
6906
6907Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6908 return map->GetOrCreate(
6909 value,
6910 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006911}
6912
6913Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6914 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006915 return map->GetOrCreate(
6916 target_method,
6917 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006918}
6919
6920Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6921 return DeduplicateMethodLiteral(target_method, &method_patches_);
6922}
6923
6924Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6925 return DeduplicateMethodLiteral(target_method, &call_patches_);
6926}
6927
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006928void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6929 LocationSummary* locations =
6930 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6931 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6932 Location::RequiresRegister());
6933 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6934 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6935 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6936}
6937
6938void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6939 LocationSummary* locations = instr->GetLocations();
6940 Register res = locations->Out().AsRegister<Register>();
6941 Register accumulator =
6942 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6943 Register mul_left =
6944 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6945 Register mul_right =
6946 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6947
6948 if (instr->GetOpKind() == HInstruction::kAdd) {
6949 __ mla(res, mul_left, mul_right, accumulator);
6950 } else {
6951 __ mls(res, mul_left, mul_right, accumulator);
6952 }
6953}
6954
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006955void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006956 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006957 LOG(FATAL) << "Unreachable";
6958}
6959
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006960void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006961 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006962 LOG(FATAL) << "Unreachable";
6963}
6964
Mark Mendellfe57faa2015-09-18 09:26:15 -04006965// Simple implementation of packed switch - generate cascaded compare/jumps.
6966void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6967 LocationSummary* locations =
6968 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6969 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006970 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006971 codegen_->GetAssembler()->IsThumb()) {
6972 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6973 if (switch_instr->GetStartValue() != 0) {
6974 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6975 }
6976 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006977}
6978
6979void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6980 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006981 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006982 LocationSummary* locations = switch_instr->GetLocations();
6983 Register value_reg = locations->InAt(0).AsRegister<Register>();
6984 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6985
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006986 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006987 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006988 Register temp_reg = IP;
6989 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6990 // the immediate, because IP is used as the destination register. For the other
6991 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6992 // and they can be encoded in the instruction without making use of IP register.
6993 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6994
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006995 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006996 // Jump to successors[0] if value == lower_bound.
6997 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6998 int32_t last_index = 0;
6999 for (; num_entries - last_index > 2; last_index += 2) {
7000 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7001 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7002 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7003 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7004 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7005 }
7006 if (num_entries - last_index == 2) {
7007 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007008 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007009 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007010 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007011
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007012 // And the default for any other value.
7013 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7014 __ b(codegen_->GetLabelOf(default_block));
7015 }
7016 } else {
7017 // Create a table lookup.
7018 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7019
7020 // Materialize a pointer to the switch table
7021 std::vector<Label*> labels(num_entries);
7022 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7023 for (uint32_t i = 0; i < num_entries; i++) {
7024 labels[i] = codegen_->GetLabelOf(successors[i]);
7025 }
7026 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7027
7028 // Remove the bias.
7029 Register key_reg;
7030 if (lower_bound != 0) {
7031 key_reg = locations->GetTemp(1).AsRegister<Register>();
7032 __ AddConstant(key_reg, value_reg, -lower_bound);
7033 } else {
7034 key_reg = value_reg;
7035 }
7036
7037 // Check whether the value is in the table, jump to default block if not.
7038 __ CmpConstant(key_reg, num_entries - 1);
7039 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7040
7041 // Load the displacement from the table.
7042 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7043
7044 // Dispatch is a direct add to the PC (for Thumb2).
7045 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007046 }
7047}
7048
Vladimir Markob4536b72015-11-24 13:45:23 +00007049void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7050 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7051 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007052}
7053
7054void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7055 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007056 CodeGeneratorARM::PcRelativePatchInfo* labels =
7057 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007058 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007059 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007060 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007061 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007062 __ BindTrackedLabel(&labels->add_pc_label);
7063 __ add(base_reg, base_reg, ShifterOperand(PC));
7064}
7065
Andreas Gampe85b62f22015-09-09 13:15:38 -07007066void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7067 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007068 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007069 return;
7070 }
7071
7072 DCHECK_NE(type, Primitive::kPrimVoid);
7073
7074 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7075 if (return_loc.Equals(trg)) {
7076 return;
7077 }
7078
7079 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7080 // with the last branch.
7081 if (type == Primitive::kPrimLong) {
7082 HParallelMove parallel_move(GetGraph()->GetArena());
7083 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7084 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7085 GetMoveResolver()->EmitNativeCode(&parallel_move);
7086 } else if (type == Primitive::kPrimDouble) {
7087 HParallelMove parallel_move(GetGraph()->GetArena());
7088 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7089 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7090 GetMoveResolver()->EmitNativeCode(&parallel_move);
7091 } else {
7092 // Let the parallel move resolver take care of all of this.
7093 HParallelMove parallel_move(GetGraph()->GetArena());
7094 parallel_move.AddMove(return_loc, trg, type, nullptr);
7095 GetMoveResolver()->EmitNativeCode(&parallel_move);
7096 }
7097}
7098
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007099void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7100 LocationSummary* locations =
7101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7102 locations->SetInAt(0, Location::RequiresRegister());
7103 locations->SetOut(Location::RequiresRegister());
7104}
7105
7106void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7107 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007108 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007109 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007110 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007111 __ LoadFromOffset(kLoadWord,
7112 locations->Out().AsRegister<Register>(),
7113 locations->InAt(0).AsRegister<Register>(),
7114 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007115 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007116 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007117 instruction->GetIndex() % ImTable::kSize, kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007118 __ LoadFromOffset(kLoadWord,
7119 locations->Out().AsRegister<Register>(),
7120 locations->InAt(0).AsRegister<Register>(),
7121 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7122 __ LoadFromOffset(kLoadWord,
7123 locations->Out().AsRegister<Register>(),
7124 locations->Out().AsRegister<Register>(),
7125 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007126 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007127}
7128
Roland Levillain4d027112015-07-01 15:41:14 +01007129#undef __
7130#undef QUICK_ENTRY_POINT
7131
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007132} // namespace arm
7133} // namespace art