blob: 775d3a4ae3a59317e6e58e7cd6bf55be62d08669 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070062// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Andreas Gampe85b62f22015-09-09 13:15:38 -070066class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000068 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069
Alexandre Rames67555f72014-11-18 10:55:16 +000070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010071 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000073 if (instruction_->CanThrowIntoCatchBlock()) {
74 // Live registers will be restored in the catch block if caught.
75 SaveLiveRegisters(codegen, instruction_->GetLocations());
76 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010077 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000078 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000079 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 }
81
Alexandre Rames8158f282015-08-07 10:26:17 +010082 bool IsFatal() const OVERRIDE { return true; }
83
Alexandre Rames9931f312015-06-19 14:47:01 +010084 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
85
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
88};
89
Andreas Gampe85b62f22015-09-09 13:15:38 -070090class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000091 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000092 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000093
Alexandre Rames67555f72014-11-18 10:55:16 +000094 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000095 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
96 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000097 if (instruction_->CanThrowIntoCatchBlock()) {
98 // Live registers will be restored in the catch block if caught.
99 SaveLiveRegisters(codegen, instruction_->GetLocations());
100 }
Calin Juravled0d48522014-11-04 16:40:20 +0000101 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000102 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000103 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000104 }
105
Alexandre Rames8158f282015-08-07 10:26:17 +0100106 bool IsFatal() const OVERRIDE { return true; }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000111 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
112};
113
Andreas Gampe85b62f22015-09-09 13:15:38 -0700114class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000115 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000116 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000117 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118
Alexandre Rames67555f72014-11-18 10:55:16 +0000119 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100120 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000122 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100123 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000124 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000125 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000126 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 if (successor_ == nullptr) {
128 __ b(GetReturnLabel());
129 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 }
133
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 Label* GetReturnLabel() {
135 DCHECK(successor_ == nullptr);
136 return &return_label_;
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100139 HBasicBlock* GetSuccessor() const {
140 return successor_;
141 }
142
Alexandre Rames9931f312015-06-19 14:47:01 +0100143 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
144
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100146 // If not null, the block to branch to after the suspend check.
147 HBasicBlock* const successor_;
148
149 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 Label return_label_;
151
152 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
153};
154
Andreas Gampe85b62f22015-09-09 13:15:38 -0700155class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100157 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000158 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159
Alexandre Rames67555f72014-11-18 10:55:16 +0000160 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100161 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100162 LocationSummary* locations = instruction_->GetLocations();
163
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000165 if (instruction_->CanThrowIntoCatchBlock()) {
166 // Live registers will be restored in the catch block if caught.
167 SaveLiveRegisters(codegen, instruction_->GetLocations());
168 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000169 // We're moving two locations to locations that could overlap, so we need a parallel
170 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000172 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100173 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100175 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100176 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100177 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
178 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100179 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
180 ? QUICK_ENTRY_POINT(pThrowStringBounds)
181 : QUICK_ENTRY_POINT(pThrowArrayBounds);
182 arm_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
183 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000184 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 }
186
Alexandre Rames8158f282015-08-07 10:26:17 +0100187 bool IsFatal() const OVERRIDE { return true; }
188
Alexandre Rames9931f312015-06-19 14:47:01 +0100189 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
190
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
193};
194
Andreas Gampe85b62f22015-09-09 13:15:38 -0700195class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 LoadClassSlowPathARM(HLoadClass* cls,
198 HInstruction* at,
199 uint32_t dex_pc,
200 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204
Alexandre Rames67555f72014-11-18 10:55:16 +0000205 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LocationSummary* locations = at_->GetLocations();
207
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100208 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
209 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 int32_t entry_point_offset = do_clinit_
215 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
216 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000217 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000218 if (do_clinit_) {
219 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
220 } else {
221 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
222 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223
224 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000225 Location out = locations->Out();
226 if (out.IsValid()) {
227 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
229 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231 __ b(GetExitLabel());
232 }
233
Alexandre Rames9931f312015-06-19 14:47:01 +0100234 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
235
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100236 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 // The class this slow path will load.
238 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 // The instruction where this slow path is happening.
241 // (Might be the load class or an initialization check).
242 HInstruction* const at_;
243
244 // The dex PC of `at_`.
245 const uint32_t dex_pc_;
246
247 // Whether to initialize the class.
248 const bool do_clinit_;
249
250 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100251};
252
Andreas Gampe85b62f22015-09-09 13:15:38 -0700253class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000255 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258 LocationSummary* locations = instruction_->GetLocations();
259 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
260
261 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000264
265 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000266 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
267 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000268 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000270 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
272
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 __ b(GetExitLabel());
275 }
276
Alexandre Rames9931f312015-06-19 14:47:01 +0100277 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
278
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000279 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000280 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
281};
282
Andreas Gampe85b62f22015-09-09 13:15:38 -0700283class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000285 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
Alexandre Rames67555f72014-11-18 10:55:16 +0000288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100290 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
291 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 DCHECK(instruction_->IsCheckCast()
293 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
295 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
296 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000297
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000298 if (!is_fatal_) {
299 SaveLiveRegisters(codegen, locations);
300 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 // We're moving two locations to locations that could overlap, so we need a parallel
303 // move resolver.
304 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100309 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100310 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
311 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100314 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
315 instruction_,
316 instruction_->GetDexPc(),
317 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000318 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700319 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
321 } else {
322 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
324 instruction_,
325 instruction_->GetDexPc(),
326 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000327 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 if (!is_fatal_) {
331 RestoreLiveRegisters(codegen, locations);
332 __ b(GetExitLabel());
333 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 }
335
Alexandre Rames9931f312015-06-19 14:47:01 +0100336 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
337
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000338 bool IsFatal() const OVERRIDE { return is_fatal_; }
339
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000341 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342
343 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
344};
345
Andreas Gampe85b62f22015-09-09 13:15:38 -0700346class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700347 public:
Aart Bik42249c32016-01-07 15:33:50 -0800348 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800352 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800355 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
356 instruction_,
357 instruction_->GetDexPc(),
358 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000359 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700360 }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
363
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700364 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
366};
367
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100368class ArraySetSlowPathARM : public SlowPathCode {
369 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000370 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100371
372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
373 LocationSummary* locations = instruction_->GetLocations();
374 __ Bind(GetEntryLabel());
375 SaveLiveRegisters(codegen, locations);
376
377 InvokeRuntimeCallingConvention calling_convention;
378 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
379 parallel_move.AddMove(
380 locations->InAt(0),
381 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
382 Primitive::kPrimNot,
383 nullptr);
384 parallel_move.AddMove(
385 locations->InAt(1),
386 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
387 Primitive::kPrimInt,
388 nullptr);
389 parallel_move.AddMove(
390 locations->InAt(2),
391 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
392 Primitive::kPrimNot,
393 nullptr);
394 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
395
396 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
397 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
398 instruction_,
399 instruction_->GetDexPc(),
400 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402 RestoreLiveRegisters(codegen, locations);
403 __ b(GetExitLabel());
404 }
405
406 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
407
408 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100409 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
410};
411
Roland Levillainc9285912015-12-18 10:38:42 +0000412// Slow path marking an object during a read barrier.
413class ReadBarrierMarkSlowPathARM : public SlowPathCode {
414 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100415 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
416 : SlowPathCode(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000417 DCHECK(kEmitCompilerReadBarrier);
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
421
422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100424 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000425 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100426 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000427 DCHECK(instruction_->IsInstanceFieldGet() ||
428 instruction_->IsStaticFieldGet() ||
429 instruction_->IsArrayGet() ||
430 instruction_->IsLoadClass() ||
431 instruction_->IsLoadString() ||
432 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100433 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100434 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000435 << "Unexpected instruction in read barrier marking slow path: "
436 << instruction_->DebugName();
437
438 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100439 // No need to save live registers; it's taken care of by the
440 // entrypoint. Also, there is no need to update the stack mask,
441 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000442 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100443 DCHECK_NE(reg, SP);
444 DCHECK_NE(reg, LR);
445 DCHECK_NE(reg, PC);
446 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
447 // "Compact" slow path, saving two moves.
448 //
449 // Instead of using the standard runtime calling convention (input
450 // and output in R0):
451 //
452 // R0 <- obj
453 // R0 <- ReadBarrierMark(R0)
454 // obj <- R0
455 //
456 // we just use rX (the register holding `obj`) as input and output
457 // of a dedicated entrypoint:
458 //
459 // rX <- ReadBarrierMarkRegX(rX)
460 //
461 int32_t entry_point_offset =
462 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmWordSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100463 // This runtime call does not require a stack map.
464 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000465 __ b(GetExitLabel());
466 }
467
468 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000469 const Location obj_;
470
471 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
472};
473
Roland Levillain3b359c72015-11-17 19:35:12 +0000474// Slow path generating a read barrier for a heap reference.
475class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
476 public:
477 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
478 Location out,
479 Location ref,
480 Location obj,
481 uint32_t offset,
482 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000483 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000484 out_(out),
485 ref_(ref),
486 obj_(obj),
487 offset_(offset),
488 index_(index) {
489 DCHECK(kEmitCompilerReadBarrier);
490 // If `obj` is equal to `out` or `ref`, it means the initial object
491 // has been overwritten by (or after) the heap object reference load
492 // to be instrumented, e.g.:
493 //
494 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000495 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000496 //
497 // In that case, we have lost the information about the original
498 // object, and the emitted read barrier cannot work properly.
499 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
500 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
501 }
502
503 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
504 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
505 LocationSummary* locations = instruction_->GetLocations();
506 Register reg_out = out_.AsRegister<Register>();
507 DCHECK(locations->CanCall());
508 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100509 DCHECK(instruction_->IsInstanceFieldGet() ||
510 instruction_->IsStaticFieldGet() ||
511 instruction_->IsArrayGet() ||
512 instruction_->IsInstanceOf() ||
513 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100514 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000515 << "Unexpected instruction in read barrier for heap reference slow path: "
516 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000517
518 __ Bind(GetEntryLabel());
519 SaveLiveRegisters(codegen, locations);
520
521 // We may have to change the index's value, but as `index_` is a
522 // constant member (like other "inputs" of this slow path),
523 // introduce a copy of it, `index`.
524 Location index = index_;
525 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100526 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000527 if (instruction_->IsArrayGet()) {
528 // Compute the actual memory offset and store it in `index`.
529 Register index_reg = index_.AsRegister<Register>();
530 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
531 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
532 // We are about to change the value of `index_reg` (see the
533 // calls to art::arm::Thumb2Assembler::Lsl and
534 // art::arm::Thumb2Assembler::AddConstant below), but it has
535 // not been saved by the previous call to
536 // art::SlowPathCode::SaveLiveRegisters, as it is a
537 // callee-save register --
538 // art::SlowPathCode::SaveLiveRegisters does not consider
539 // callee-save registers, as it has been designed with the
540 // assumption that callee-save registers are supposed to be
541 // handled by the called function. So, as a callee-save
542 // register, `index_reg` _would_ eventually be saved onto
543 // the stack, but it would be too late: we would have
544 // changed its value earlier. Therefore, we manually save
545 // it here into another freely available register,
546 // `free_reg`, chosen of course among the caller-save
547 // registers (as a callee-save `free_reg` register would
548 // exhibit the same problem).
549 //
550 // Note we could have requested a temporary register from
551 // the register allocator instead; but we prefer not to, as
552 // this is a slow path, and we know we can find a
553 // caller-save register that is available.
554 Register free_reg = FindAvailableCallerSaveRegister(codegen);
555 __ Mov(free_reg, index_reg);
556 index_reg = free_reg;
557 index = Location::RegisterLocation(index_reg);
558 } else {
559 // The initial register stored in `index_` has already been
560 // saved in the call to art::SlowPathCode::SaveLiveRegisters
561 // (as it is not a callee-save register), so we can freely
562 // use it.
563 }
564 // Shifting the index value contained in `index_reg` by the scale
565 // factor (2) cannot overflow in practice, as the runtime is
566 // unable to allocate object arrays with a size larger than
567 // 2^26 - 1 (that is, 2^28 - 4 bytes).
568 __ Lsl(index_reg, index_reg, TIMES_4);
569 static_assert(
570 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
571 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
572 __ AddConstant(index_reg, index_reg, offset_);
573 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100574 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
575 // intrinsics, `index_` is not shifted by a scale factor of 2
576 // (as in the case of ArrayGet), as it is actually an offset
577 // to an object field within an object.
578 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000579 DCHECK(instruction_->GetLocations()->Intrinsified());
580 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
581 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
582 << instruction_->AsInvoke()->GetIntrinsic();
583 DCHECK_EQ(offset_, 0U);
584 DCHECK(index_.IsRegisterPair());
585 // UnsafeGet's offset location is a register pair, the low
586 // part contains the correct offset.
587 index = index_.ToLow();
588 }
589 }
590
591 // We're moving two or three locations to locations that could
592 // overlap, so we need a parallel move resolver.
593 InvokeRuntimeCallingConvention calling_convention;
594 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
595 parallel_move.AddMove(ref_,
596 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
597 Primitive::kPrimNot,
598 nullptr);
599 parallel_move.AddMove(obj_,
600 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
601 Primitive::kPrimNot,
602 nullptr);
603 if (index.IsValid()) {
604 parallel_move.AddMove(index,
605 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
606 Primitive::kPrimInt,
607 nullptr);
608 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
609 } else {
610 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
611 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
612 }
613 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
614 instruction_,
615 instruction_->GetDexPc(),
616 this);
617 CheckEntrypointTypes<
618 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
619 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
620
621 RestoreLiveRegisters(codegen, locations);
622 __ b(GetExitLabel());
623 }
624
625 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
626
627 private:
628 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
629 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
630 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
631 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
632 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
633 return static_cast<Register>(i);
634 }
635 }
636 // We shall never fail to find a free caller-save register, as
637 // there are more than two core caller-save registers on ARM
638 // (meaning it is possible to find one which is different from
639 // `ref` and `obj`).
640 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
641 LOG(FATAL) << "Could not find a free caller-save register";
642 UNREACHABLE();
643 }
644
Roland Levillain3b359c72015-11-17 19:35:12 +0000645 const Location out_;
646 const Location ref_;
647 const Location obj_;
648 const uint32_t offset_;
649 // An additional location containing an index to an array.
650 // Only used for HArrayGet and the UnsafeGetObject &
651 // UnsafeGetObjectVolatile intrinsics.
652 const Location index_;
653
654 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
655};
656
657// Slow path generating a read barrier for a GC root.
658class ReadBarrierForRootSlowPathARM : public SlowPathCode {
659 public:
660 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000661 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000662 DCHECK(kEmitCompilerReadBarrier);
663 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000664
665 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
666 LocationSummary* locations = instruction_->GetLocations();
667 Register reg_out = out_.AsRegister<Register>();
668 DCHECK(locations->CanCall());
669 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000670 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
671 << "Unexpected instruction in read barrier for GC root slow path: "
672 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000673
674 __ Bind(GetEntryLabel());
675 SaveLiveRegisters(codegen, locations);
676
677 InvokeRuntimeCallingConvention calling_convention;
678 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
679 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
680 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
681 instruction_,
682 instruction_->GetDexPc(),
683 this);
684 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
685 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
686
687 RestoreLiveRegisters(codegen, locations);
688 __ b(GetExitLabel());
689 }
690
691 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
692
693 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000694 const Location out_;
695 const Location root_;
696
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
698};
699
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000700#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700701// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
702#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700703
Aart Bike9f37602015-10-09 11:15:55 -0700704inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700705 switch (cond) {
706 case kCondEQ: return EQ;
707 case kCondNE: return NE;
708 case kCondLT: return LT;
709 case kCondLE: return LE;
710 case kCondGT: return GT;
711 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700712 case kCondB: return LO;
713 case kCondBE: return LS;
714 case kCondA: return HI;
715 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700716 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100717 LOG(FATAL) << "Unreachable";
718 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700719}
720
Aart Bike9f37602015-10-09 11:15:55 -0700721// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100722inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700723 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100724 case kCondEQ: return EQ;
725 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700726 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727 case kCondLT: return LO;
728 case kCondLE: return LS;
729 case kCondGT: return HI;
730 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700731 // Unsigned remain unchanged.
732 case kCondB: return LO;
733 case kCondBE: return LS;
734 case kCondA: return HI;
735 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700736 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100737 LOG(FATAL) << "Unreachable";
738 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700739}
740
Vladimir Markod6e069b2016-01-18 11:11:01 +0000741inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
742 // The ARM condition codes can express all the necessary branches, see the
743 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
744 // There is no dex instruction or HIR that would need the missing conditions
745 // "equal or unordered" or "not equal".
746 switch (cond) {
747 case kCondEQ: return EQ;
748 case kCondNE: return NE /* unordered */;
749 case kCondLT: return gt_bias ? CC : LT /* unordered */;
750 case kCondLE: return gt_bias ? LS : LE /* unordered */;
751 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
752 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
753 default:
754 LOG(FATAL) << "UNREACHABLE";
755 UNREACHABLE();
756 }
757}
758
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100759void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100760 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100761}
762
763void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100764 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100765}
766
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100767size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
768 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
769 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100770}
771
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100772size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
773 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
774 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100775}
776
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000777size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
778 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
779 return kArmWordSize;
780}
781
782size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
783 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
784 return kArmWordSize;
785}
786
Calin Juravle34166012014-12-19 17:22:29 +0000787CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000788 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100789 const CompilerOptions& compiler_options,
790 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000791 : CodeGenerator(graph,
792 kNumberOfCoreRegisters,
793 kNumberOfSRegisters,
794 kNumberOfRegisterPairs,
795 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
796 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000797 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
798 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100799 compiler_options,
800 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100801 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100802 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100803 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100804 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100805 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000806 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000807 uint32_literals_(std::less<uint32_t>(),
808 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100809 method_patches_(MethodReferenceComparator(),
810 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
811 call_patches_(MethodReferenceComparator(),
812 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000813 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000814 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
815 boot_image_string_patches_(StringReferenceValueComparator(),
816 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
817 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100818 boot_image_type_patches_(TypeReferenceValueComparator(),
819 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
820 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000821 boot_image_address_patches_(std::less<uint32_t>(),
822 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700823 // Always save the LR register to mimic Quick.
824 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100825}
826
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000827void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
828 // Ensure that we fix up branches and literal loads and emit the literal pool.
829 __ FinalizeCode();
830
831 // Adjust native pc offsets in stack maps.
832 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
833 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
834 uint32_t new_position = __ GetAdjustedPosition(old_position);
835 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
836 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100837 // Adjust pc offsets for the disassembly information.
838 if (disasm_info_ != nullptr) {
839 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
840 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
841 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
842 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
843 it.second.start = __ GetAdjustedPosition(it.second.start);
844 it.second.end = __ GetAdjustedPosition(it.second.end);
845 }
846 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
847 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
848 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
849 }
850 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000851
852 CodeGenerator::Finalize(allocator);
853}
854
David Brazdil58282f42016-01-14 12:45:10 +0000855void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100856 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100857 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100858
859 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100860 blocked_core_registers_[SP] = true;
861 blocked_core_registers_[LR] = true;
862 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100863
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100864 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100865 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100867 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100868 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100869
David Brazdil58282f42016-01-14 12:45:10 +0000870 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100871 // Stubs do not save callee-save floating point registers. If the graph
872 // is debuggable, we need to deal with these registers differently. For
873 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000874 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
875 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
876 }
877 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100878
879 UpdateBlockedPairRegisters();
880}
881
882void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
883 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
884 ArmManagedRegister current =
885 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
886 if (blocked_core_registers_[current.AsRegisterPairLow()]
887 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
888 blocked_register_pairs_[i] = true;
889 }
890 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100891}
892
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100893InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800894 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100895 assembler_(codegen->GetAssembler()),
896 codegen_(codegen) {}
897
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000898void CodeGeneratorARM::ComputeSpillMask() {
899 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
900 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000901 // There is no easy instruction to restore just the PC on thumb2. We spill and
902 // restore another arbitrary register.
903 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000904 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
905 // We use vpush and vpop for saving and restoring floating point registers, which take
906 // a SRegister and the number of registers to save/restore after that SRegister. We
907 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
908 // but in the range.
909 if (fpu_spill_mask_ != 0) {
910 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
911 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
912 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
913 fpu_spill_mask_ |= (1 << i);
914 }
915 }
916}
917
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100918static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100919 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100920}
921
922static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100923 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100924}
925
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000926void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000927 bool skip_overflow_check =
928 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000929 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000930 __ Bind(&frame_entry_label_);
931
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000932 if (HasEmptyFrame()) {
933 return;
934 }
935
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100936 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000937 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
938 __ LoadFromOffset(kLoadWord, IP, IP, 0);
939 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100940 }
941
Andreas Gampe501fd632015-09-10 16:11:06 -0700942 __ PushList(core_spill_mask_);
943 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
944 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000945 if (fpu_spill_mask_ != 0) {
946 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
947 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100948 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100949 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000950 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100951 int adjust = GetFrameSize() - FrameEntrySpillSize();
952 __ AddConstant(SP, -adjust);
953 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100954 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000955}
956
957void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000958 if (HasEmptyFrame()) {
959 __ bx(LR);
960 return;
961 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100962 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100963 int adjust = GetFrameSize() - FrameEntrySpillSize();
964 __ AddConstant(SP, adjust);
965 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000966 if (fpu_spill_mask_ != 0) {
967 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
968 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100969 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
970 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000971 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700972 // Pop LR into PC to return.
973 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
974 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
975 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100976 __ cfi().RestoreState();
977 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000978}
979
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100980void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700981 Label* label = GetLabelOf(block);
982 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000983}
984
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100985Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100986 switch (type) {
987 case Primitive::kPrimBoolean:
988 case Primitive::kPrimByte:
989 case Primitive::kPrimChar:
990 case Primitive::kPrimShort:
991 case Primitive::kPrimInt:
992 case Primitive::kPrimNot: {
993 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000994 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100995 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100996 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100997 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000998 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100999 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001000 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001001
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001002 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001003 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 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001006 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001007 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001008 if (calling_convention.GetRegisterAt(index) == R1) {
1009 // Skip R1, and use R2_R3 instead.
1010 gp_index_++;
1011 index++;
1012 }
1013 }
1014 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1015 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001016 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001017
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001018 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001019 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001020 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001021 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1022 }
1023 }
1024
1025 case Primitive::kPrimFloat: {
1026 uint32_t stack_index = stack_index_++;
1027 if (float_index_ % 2 == 0) {
1028 float_index_ = std::max(double_index_, float_index_);
1029 }
1030 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1031 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1032 } else {
1033 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1034 }
1035 }
1036
1037 case Primitive::kPrimDouble: {
1038 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1039 uint32_t stack_index = stack_index_;
1040 stack_index_ += 2;
1041 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1042 uint32_t index = double_index_;
1043 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001044 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001045 calling_convention.GetFpuRegisterAt(index),
1046 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001047 DCHECK(ExpectedPairLayout(result));
1048 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001049 } else {
1050 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001051 }
1052 }
1053
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001054 case Primitive::kPrimVoid:
1055 LOG(FATAL) << "Unexpected parameter type " << type;
1056 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001057 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001058 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001059}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001060
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001061Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001062 switch (type) {
1063 case Primitive::kPrimBoolean:
1064 case Primitive::kPrimByte:
1065 case Primitive::kPrimChar:
1066 case Primitive::kPrimShort:
1067 case Primitive::kPrimInt:
1068 case Primitive::kPrimNot: {
1069 return Location::RegisterLocation(R0);
1070 }
1071
1072 case Primitive::kPrimFloat: {
1073 return Location::FpuRegisterLocation(S0);
1074 }
1075
1076 case Primitive::kPrimLong: {
1077 return Location::RegisterPairLocation(R0, R1);
1078 }
1079
1080 case Primitive::kPrimDouble: {
1081 return Location::FpuRegisterPairLocation(S0, S1);
1082 }
1083
1084 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001085 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001086 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001087
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001088 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001089}
1090
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001091Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1092 return Location::RegisterLocation(kMethodRegisterArgument);
1093}
1094
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095void CodeGeneratorARM::Move32(Location destination, Location source) {
1096 if (source.Equals(destination)) {
1097 return;
1098 }
1099 if (destination.IsRegister()) {
1100 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001101 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001103 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001107 } else if (destination.IsFpuRegister()) {
1108 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001113 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001114 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001116 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001118 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001119 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001120 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001121 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001122 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001123 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1124 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125 }
1126 }
1127}
1128
1129void CodeGeneratorARM::Move64(Location destination, Location source) {
1130 if (source.Equals(destination)) {
1131 return;
1132 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 if (destination.IsRegisterPair()) {
1134 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001135 EmitParallelMoves(
1136 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1137 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001138 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001139 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001140 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1141 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001142 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001143 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001144 } else if (source.IsFpuRegisterPair()) {
1145 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1146 destination.AsRegisterPairHigh<Register>(),
1147 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 } else {
1149 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001150 DCHECK(ExpectedPairLayout(destination));
1151 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1152 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001154 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001155 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001156 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1157 SP,
1158 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001159 } else if (source.IsRegisterPair()) {
1160 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1161 source.AsRegisterPairLow<Register>(),
1162 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001163 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001164 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 } else {
1167 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001168 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001169 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 if (source.AsRegisterPairLow<Register>() == R1) {
1171 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1173 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001174 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001175 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001176 SP, destination.GetStackIndex());
1177 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001178 } else if (source.IsFpuRegisterPair()) {
1179 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1180 SP,
1181 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 } else {
1183 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001184 EmitParallelMoves(
1185 Location::StackSlot(source.GetStackIndex()),
1186 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001187 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001188 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001189 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1190 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 }
1192 }
1193}
1194
Calin Juravle175dc732015-08-25 15:42:32 +01001195void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1196 DCHECK(location.IsRegister());
1197 __ LoadImmediate(location.AsRegister<Register>(), value);
1198}
1199
Calin Juravlee460d1d2015-09-29 04:52:17 +01001200void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001201 HParallelMove move(GetGraph()->GetArena());
1202 move.AddMove(src, dst, dst_type, nullptr);
1203 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001204}
1205
1206void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1207 if (location.IsRegister()) {
1208 locations->AddTemp(location);
1209 } else if (location.IsRegisterPair()) {
1210 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1211 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1212 } else {
1213 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1214 }
1215}
1216
Calin Juravle175dc732015-08-25 15:42:32 +01001217void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1218 HInstruction* instruction,
1219 uint32_t dex_pc,
1220 SlowPathCode* slow_path) {
1221 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1222 instruction,
1223 dex_pc,
1224 slow_path);
1225}
1226
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001227void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1228 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001229 uint32_t dex_pc,
1230 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001231 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001232 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1233 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001234 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001235}
1236
Roland Levillaindec8f632016-07-22 17:10:06 +01001237void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1238 HInstruction* instruction,
1239 SlowPathCode* slow_path) {
1240 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1241 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1242 __ blx(LR);
1243}
1244
David Brazdilfc6a86a2015-06-26 10:33:45 +00001245void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001246 DCHECK(!successor->IsExitBlock());
1247
1248 HBasicBlock* block = got->GetBlock();
1249 HInstruction* previous = got->GetPrevious();
1250
1251 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001252 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001253 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1254 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1255 return;
1256 }
1257
1258 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1259 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1260 }
1261 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001262 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001263 }
1264}
1265
David Brazdilfc6a86a2015-06-26 10:33:45 +00001266void LocationsBuilderARM::VisitGoto(HGoto* got) {
1267 got->SetLocations(nullptr);
1268}
1269
1270void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1271 HandleGoto(got, got->GetSuccessor());
1272}
1273
1274void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1275 try_boundary->SetLocations(nullptr);
1276}
1277
1278void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1279 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1280 if (!successor->IsExitBlock()) {
1281 HandleGoto(try_boundary, successor);
1282 }
1283}
1284
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001285void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001286 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001287}
1288
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001289void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001290}
1291
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001292void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1293 Primitive::Type type = instruction->InputAt(0)->GetType();
1294 Location lhs_loc = instruction->GetLocations()->InAt(0);
1295 Location rhs_loc = instruction->GetLocations()->InAt(1);
1296 if (rhs_loc.IsConstant()) {
1297 // 0.0 is the only immediate that can be encoded directly in
1298 // a VCMP instruction.
1299 //
1300 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1301 // specify that in a floating-point comparison, positive zero
1302 // and negative zero are considered equal, so we can use the
1303 // literal 0.0 for both cases here.
1304 //
1305 // Note however that some methods (Float.equal, Float.compare,
1306 // Float.compareTo, Double.equal, Double.compare,
1307 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1308 // StrictMath.min) consider 0.0 to be (strictly) greater than
1309 // -0.0. So if we ever translate calls to these methods into a
1310 // HCompare instruction, we must handle the -0.0 case with
1311 // care here.
1312 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1313 if (type == Primitive::kPrimFloat) {
1314 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1315 } else {
1316 DCHECK_EQ(type, Primitive::kPrimDouble);
1317 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1318 }
1319 } else {
1320 if (type == Primitive::kPrimFloat) {
1321 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1322 } else {
1323 DCHECK_EQ(type, Primitive::kPrimDouble);
1324 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1325 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1326 }
1327 }
1328}
1329
Roland Levillain4fa13f62015-07-06 18:11:54 +01001330void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1331 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001332 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001333 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001334 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001335}
1336
1337void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1338 Label* true_label,
1339 Label* false_label) {
1340 LocationSummary* locations = cond->GetLocations();
1341 Location left = locations->InAt(0);
1342 Location right = locations->InAt(1);
1343 IfCondition if_cond = cond->GetCondition();
1344
1345 Register left_high = left.AsRegisterPairHigh<Register>();
1346 Register left_low = left.AsRegisterPairLow<Register>();
1347 IfCondition true_high_cond = if_cond;
1348 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001349 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001350
1351 // Set the conditions for the test, remembering that == needs to be
1352 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001353 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 switch (if_cond) {
1355 case kCondEQ:
1356 case kCondNE:
1357 // Nothing to do.
1358 break;
1359 case kCondLT:
1360 false_high_cond = kCondGT;
1361 break;
1362 case kCondLE:
1363 true_high_cond = kCondLT;
1364 break;
1365 case kCondGT:
1366 false_high_cond = kCondLT;
1367 break;
1368 case kCondGE:
1369 true_high_cond = kCondGT;
1370 break;
Aart Bike9f37602015-10-09 11:15:55 -07001371 case kCondB:
1372 false_high_cond = kCondA;
1373 break;
1374 case kCondBE:
1375 true_high_cond = kCondB;
1376 break;
1377 case kCondA:
1378 false_high_cond = kCondB;
1379 break;
1380 case kCondAE:
1381 true_high_cond = kCondA;
1382 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001383 }
1384 if (right.IsConstant()) {
1385 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1386 int32_t val_low = Low32Bits(value);
1387 int32_t val_high = High32Bits(value);
1388
Vladimir Markoac6ac102015-12-17 12:14:00 +00001389 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001390 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001391 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001392 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001393 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001394 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001395 __ b(true_label, ARMCondition(true_high_cond));
1396 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397 }
1398 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001399 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001400 } else {
1401 Register right_high = right.AsRegisterPairHigh<Register>();
1402 Register right_low = right.AsRegisterPairLow<Register>();
1403
1404 __ cmp(left_high, ShifterOperand(right_high));
1405 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001406 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001408 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001409 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001410 __ b(true_label, ARMCondition(true_high_cond));
1411 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 }
1413 // Must be equal high, so compare the lows.
1414 __ cmp(left_low, ShifterOperand(right_low));
1415 }
1416 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001417 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001418 __ b(true_label, final_condition);
1419}
1420
David Brazdil0debae72015-11-12 18:37:00 +00001421void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1422 Label* true_target_in,
1423 Label* false_target_in) {
1424 // Generated branching requires both targets to be explicit. If either of the
1425 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1426 Label fallthrough_target;
1427 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1428 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1429
Roland Levillain4fa13f62015-07-06 18:11:54 +01001430 Primitive::Type type = condition->InputAt(0)->GetType();
1431 switch (type) {
1432 case Primitive::kPrimLong:
1433 GenerateLongComparesAndJumps(condition, true_target, false_target);
1434 break;
1435 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001436 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001437 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001438 GenerateFPJumps(condition, true_target, false_target);
1439 break;
1440 default:
1441 LOG(FATAL) << "Unexpected compare type " << type;
1442 }
1443
David Brazdil0debae72015-11-12 18:37:00 +00001444 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001445 __ b(false_target);
1446 }
David Brazdil0debae72015-11-12 18:37:00 +00001447
1448 if (fallthrough_target.IsLinked()) {
1449 __ Bind(&fallthrough_target);
1450 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001451}
1452
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001453void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001454 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001455 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001456 Label* false_target) {
1457 HInstruction* cond = instruction->InputAt(condition_input_index);
1458
1459 if (true_target == nullptr && false_target == nullptr) {
1460 // Nothing to do. The code always falls through.
1461 return;
1462 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001463 // Constant condition, statically compared against "true" (integer value 1).
1464 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001465 if (true_target != nullptr) {
1466 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001467 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001468 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001469 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001470 if (false_target != nullptr) {
1471 __ b(false_target);
1472 }
1473 }
1474 return;
1475 }
1476
1477 // The following code generates these patterns:
1478 // (1) true_target == nullptr && false_target != nullptr
1479 // - opposite condition true => branch to false_target
1480 // (2) true_target != nullptr && false_target == nullptr
1481 // - condition true => branch to true_target
1482 // (3) true_target != nullptr && false_target != nullptr
1483 // - condition true => branch to true_target
1484 // - branch to false_target
1485 if (IsBooleanValueOrMaterializedCondition(cond)) {
1486 // Condition has been materialized, compare the output to 0.
1487 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1488 DCHECK(cond_val.IsRegister());
1489 if (true_target == nullptr) {
1490 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1491 } else {
1492 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001493 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001494 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001495 // Condition has not been materialized. Use its inputs as the comparison and
1496 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001497 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001498
1499 // If this is a long or FP comparison that has been folded into
1500 // the HCondition, generate the comparison directly.
1501 Primitive::Type type = condition->InputAt(0)->GetType();
1502 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1503 GenerateCompareTestAndBranch(condition, true_target, false_target);
1504 return;
1505 }
1506
1507 LocationSummary* locations = cond->GetLocations();
1508 DCHECK(locations->InAt(0).IsRegister());
1509 Register left = locations->InAt(0).AsRegister<Register>();
1510 Location right = locations->InAt(1);
1511 if (right.IsRegister()) {
1512 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001513 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001514 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001515 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001516 }
1517 if (true_target == nullptr) {
1518 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1519 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001520 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001521 }
Dave Allison20dfc792014-06-16 20:44:29 -07001522 }
David Brazdil0debae72015-11-12 18:37:00 +00001523
1524 // If neither branch falls through (case 3), the conditional branch to `true_target`
1525 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1526 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001527 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001528 }
1529}
1530
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001531void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1533 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001534 locations->SetInAt(0, Location::RequiresRegister());
1535 }
1536}
1537
1538void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001539 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1540 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1541 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1542 nullptr : codegen_->GetLabelOf(true_successor);
1543 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1544 nullptr : codegen_->GetLabelOf(false_successor);
1545 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001546}
1547
1548void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1549 LocationSummary* locations = new (GetGraph()->GetArena())
1550 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001551 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001552 locations->SetInAt(0, Location::RequiresRegister());
1553 }
1554}
1555
1556void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001557 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001558 GenerateTestAndBranch(deoptimize,
1559 /* condition_input_index */ 0,
1560 slow_path->GetEntryLabel(),
1561 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001562}
Dave Allison20dfc792014-06-16 20:44:29 -07001563
David Brazdil74eb1b22015-12-14 11:44:01 +00001564void LocationsBuilderARM::VisitSelect(HSelect* select) {
1565 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1566 if (Primitive::IsFloatingPointType(select->GetType())) {
1567 locations->SetInAt(0, Location::RequiresFpuRegister());
1568 locations->SetInAt(1, Location::RequiresFpuRegister());
1569 } else {
1570 locations->SetInAt(0, Location::RequiresRegister());
1571 locations->SetInAt(1, Location::RequiresRegister());
1572 }
1573 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1574 locations->SetInAt(2, Location::RequiresRegister());
1575 }
1576 locations->SetOut(Location::SameAsFirstInput());
1577}
1578
1579void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1580 LocationSummary* locations = select->GetLocations();
1581 Label false_target;
1582 GenerateTestAndBranch(select,
1583 /* condition_input_index */ 2,
1584 /* true_target */ nullptr,
1585 &false_target);
1586 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1587 __ Bind(&false_target);
1588}
1589
David Srbecky0cf44932015-12-09 14:09:59 +00001590void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1591 new (GetGraph()->GetArena()) LocationSummary(info);
1592}
1593
David Srbeckyd28f4a02016-03-14 17:14:24 +00001594void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1595 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001596}
1597
1598void CodeGeneratorARM::GenerateNop() {
1599 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001600}
1601
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001602void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001603 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001604 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001605 // Handle the long/FP comparisons made in instruction simplification.
1606 switch (cond->InputAt(0)->GetType()) {
1607 case Primitive::kPrimLong:
1608 locations->SetInAt(0, Location::RequiresRegister());
1609 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001610 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001611 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1612 }
1613 break;
1614
1615 case Primitive::kPrimFloat:
1616 case Primitive::kPrimDouble:
1617 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001618 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001619 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1621 }
1622 break;
1623
1624 default:
1625 locations->SetInAt(0, Location::RequiresRegister());
1626 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001627 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1629 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001630 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001631}
1632
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001633void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001634 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001635 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001636 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001637
1638 LocationSummary* locations = cond->GetLocations();
1639 Location left = locations->InAt(0);
1640 Location right = locations->InAt(1);
1641 Register out = locations->Out().AsRegister<Register>();
1642 Label true_label, false_label;
1643
1644 switch (cond->InputAt(0)->GetType()) {
1645 default: {
1646 // Integer case.
1647 if (right.IsRegister()) {
1648 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1649 } else {
1650 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001651 __ CmpConstant(left.AsRegister<Register>(),
1652 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001653 }
Aart Bike9f37602015-10-09 11:15:55 -07001654 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001655 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001656 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001657 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001658 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001659 return;
1660 }
1661 case Primitive::kPrimLong:
1662 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1663 break;
1664 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001665 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001666 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001667 GenerateFPJumps(cond, &true_label, &false_label);
1668 break;
1669 }
1670
1671 // Convert the jumps into the result.
1672 Label done_label;
1673
1674 // False case: result = 0.
1675 __ Bind(&false_label);
1676 __ LoadImmediate(out, 0);
1677 __ b(&done_label);
1678
1679 // True case: result = 1.
1680 __ Bind(&true_label);
1681 __ LoadImmediate(out, 1);
1682 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001683}
1684
1685void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001686 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001687}
1688
1689void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001690 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001691}
1692
1693void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001694 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001695}
1696
1697void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001698 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001699}
1700
1701void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001703}
1704
1705void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001706 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001707}
1708
1709void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001710 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001711}
1712
1713void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001715}
1716
1717void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001719}
1720
1721void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001723}
1724
1725void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001727}
1728
1729void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001731}
1732
Aart Bike9f37602015-10-09 11:15:55 -07001733void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001735}
1736
1737void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001739}
1740
1741void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001743}
1744
1745void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001747}
1748
1749void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001751}
1752
1753void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001755}
1756
1757void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001759}
1760
1761void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001763}
1764
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001765void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001766 LocationSummary* locations =
1767 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001768 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001769}
1770
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001771void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001772 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001773}
1774
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001775void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1776 LocationSummary* locations =
1777 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1778 locations->SetOut(Location::ConstantLocation(constant));
1779}
1780
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001781void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001782 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001783}
1784
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001785void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001786 LocationSummary* locations =
1787 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001788 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001789}
1790
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001791void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001792 // Will be generated at use site.
1793}
1794
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001795void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1796 LocationSummary* locations =
1797 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1798 locations->SetOut(Location::ConstantLocation(constant));
1799}
1800
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001801void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001802 // Will be generated at use site.
1803}
1804
1805void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1806 LocationSummary* locations =
1807 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1808 locations->SetOut(Location::ConstantLocation(constant));
1809}
1810
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001811void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001812 // Will be generated at use site.
1813}
1814
Calin Juravle27df7582015-04-17 19:12:31 +01001815void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1816 memory_barrier->SetLocations(nullptr);
1817}
1818
1819void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001820 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001821}
1822
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001823void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001824 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001825}
1826
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001827void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001828 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001829}
1830
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001831void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001832 LocationSummary* locations =
1833 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001834 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835}
1836
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001837void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001838 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001839}
1840
Calin Juravle175dc732015-08-25 15:42:32 +01001841void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1842 // The trampoline uses the same calling convention as dex calling conventions,
1843 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1844 // the method_idx.
1845 HandleInvoke(invoke);
1846}
1847
1848void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1849 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1850}
1851
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001852void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001853 // Explicit clinit checks triggered by static invokes must have been pruned by
1854 // art::PrepareForRegisterAllocation.
1855 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001856
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001857 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001858 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001859 codegen_->GetInstructionSetFeatures());
1860 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001861 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1862 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1863 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001864 return;
1865 }
1866
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001867 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001868
1869 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1870 if (invoke->HasPcRelativeDexCache()) {
1871 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1872 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001873}
1874
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001875static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1876 if (invoke->GetLocations()->Intrinsified()) {
1877 IntrinsicCodeGeneratorARM intrinsic(codegen);
1878 intrinsic.Dispatch(invoke);
1879 return true;
1880 }
1881 return false;
1882}
1883
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001884void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001885 // Explicit clinit checks triggered by static invokes must have been pruned by
1886 // art::PrepareForRegisterAllocation.
1887 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001888
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001889 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1890 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001891 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001892
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001893 LocationSummary* locations = invoke->GetLocations();
1894 codegen_->GenerateStaticOrDirectCall(
1895 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001896 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001897}
1898
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001899void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001900 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001901 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001902}
1903
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001904void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001905 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001906 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001907 codegen_->GetInstructionSetFeatures());
1908 if (intrinsic.TryDispatch(invoke)) {
1909 return;
1910 }
1911
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001912 HandleInvoke(invoke);
1913}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001914
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001915void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001916 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1917 return;
1918 }
1919
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001920 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001921 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001922 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001923}
1924
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001925void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1926 HandleInvoke(invoke);
1927 // Add the hidden argument.
1928 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1929}
1930
1931void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1932 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001933 LocationSummary* locations = invoke->GetLocations();
1934 Register temp = locations->GetTemp(0).AsRegister<Register>();
1935 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001936 Location receiver = locations->InAt(0);
1937 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1938
Roland Levillain3b359c72015-11-17 19:35:12 +00001939 // Set the hidden argument. This is safe to do this here, as R12
1940 // won't be modified thereafter, before the `blx` (call) instruction.
1941 DCHECK_EQ(R12, hidden_reg);
1942 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001943
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001944 if (receiver.IsStackSlot()) {
1945 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001946 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001947 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1948 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001949 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001950 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001951 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001952 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001953 // Instead of simply (possibly) unpoisoning `temp` here, we should
1954 // emit a read barrier for the previous class reference load.
1955 // However this is not required in practice, as this is an
1956 // intermediate/temporary reference and because the current
1957 // concurrent copying collector keeps the from-space memory
1958 // intact/accessible until the end of the marking phase (the
1959 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001960 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001961 __ LoadFromOffset(kLoadWord, temp, temp,
1962 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1963 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00001964 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001965 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001966 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001967 uint32_t entry_point =
1968 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001969 // LR = temp->GetEntryPoint();
1970 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1971 // LR();
1972 __ blx(LR);
1973 DCHECK(!codegen_->IsLeafMethod());
1974 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1975}
1976
Roland Levillain88cb1752014-10-20 16:36:47 +01001977void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1978 LocationSummary* locations =
1979 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1980 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001981 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001982 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1984 break;
1985 }
1986 case Primitive::kPrimLong: {
1987 locations->SetInAt(0, Location::RequiresRegister());
1988 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001989 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001990 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001991
Roland Levillain88cb1752014-10-20 16:36:47 +01001992 case Primitive::kPrimFloat:
1993 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001994 locations->SetInAt(0, Location::RequiresFpuRegister());
1995 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001996 break;
1997
1998 default:
1999 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2000 }
2001}
2002
2003void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2004 LocationSummary* locations = neg->GetLocations();
2005 Location out = locations->Out();
2006 Location in = locations->InAt(0);
2007 switch (neg->GetResultType()) {
2008 case Primitive::kPrimInt:
2009 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002010 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002011 break;
2012
2013 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002014 DCHECK(in.IsRegisterPair());
2015 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2016 __ rsbs(out.AsRegisterPairLow<Register>(),
2017 in.AsRegisterPairLow<Register>(),
2018 ShifterOperand(0));
2019 // We cannot emit an RSC (Reverse Subtract with Carry)
2020 // instruction here, as it does not exist in the Thumb-2
2021 // instruction set. We use the following approach
2022 // using SBC and SUB instead.
2023 //
2024 // out.hi = -C
2025 __ sbc(out.AsRegisterPairHigh<Register>(),
2026 out.AsRegisterPairHigh<Register>(),
2027 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2028 // out.hi = out.hi - in.hi
2029 __ sub(out.AsRegisterPairHigh<Register>(),
2030 out.AsRegisterPairHigh<Register>(),
2031 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2032 break;
2033
Roland Levillain88cb1752014-10-20 16:36:47 +01002034 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002035 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002036 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002037 break;
2038
Roland Levillain88cb1752014-10-20 16:36:47 +01002039 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002040 DCHECK(in.IsFpuRegisterPair());
2041 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2042 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002043 break;
2044
2045 default:
2046 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2047 }
2048}
2049
Roland Levillaindff1f282014-11-05 14:15:05 +00002050void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002051 Primitive::Type result_type = conversion->GetResultType();
2052 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002053 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002054
Roland Levillain5b3ee562015-04-14 16:02:41 +01002055 // The float-to-long, double-to-long and long-to-float type conversions
2056 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002057 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002058 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2059 && result_type == Primitive::kPrimLong)
2060 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002061 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002062 : LocationSummary::kNoCall;
2063 LocationSummary* locations =
2064 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2065
David Brazdilb2bd1c52015-03-25 11:17:37 +00002066 // The Java language does not allow treating boolean as an integral type but
2067 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002068
Roland Levillaindff1f282014-11-05 14:15:05 +00002069 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002070 case Primitive::kPrimByte:
2071 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002072 case Primitive::kPrimLong:
2073 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002074 case Primitive::kPrimBoolean:
2075 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002076 case Primitive::kPrimShort:
2077 case Primitive::kPrimInt:
2078 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002079 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002080 locations->SetInAt(0, Location::RequiresRegister());
2081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2082 break;
2083
2084 default:
2085 LOG(FATAL) << "Unexpected type conversion from " << input_type
2086 << " to " << result_type;
2087 }
2088 break;
2089
Roland Levillain01a8d712014-11-14 16:27:39 +00002090 case Primitive::kPrimShort:
2091 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002092 case Primitive::kPrimLong:
2093 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002094 case Primitive::kPrimBoolean:
2095 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002096 case Primitive::kPrimByte:
2097 case Primitive::kPrimInt:
2098 case Primitive::kPrimChar:
2099 // Processing a Dex `int-to-short' instruction.
2100 locations->SetInAt(0, Location::RequiresRegister());
2101 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2102 break;
2103
2104 default:
2105 LOG(FATAL) << "Unexpected type conversion from " << input_type
2106 << " to " << result_type;
2107 }
2108 break;
2109
Roland Levillain946e1432014-11-11 17:35:19 +00002110 case Primitive::kPrimInt:
2111 switch (input_type) {
2112 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002113 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002114 locations->SetInAt(0, Location::Any());
2115 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2116 break;
2117
2118 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002119 // Processing a Dex `float-to-int' instruction.
2120 locations->SetInAt(0, Location::RequiresFpuRegister());
2121 locations->SetOut(Location::RequiresRegister());
2122 locations->AddTemp(Location::RequiresFpuRegister());
2123 break;
2124
Roland Levillain946e1432014-11-11 17:35:19 +00002125 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002126 // Processing a Dex `double-to-int' instruction.
2127 locations->SetInAt(0, Location::RequiresFpuRegister());
2128 locations->SetOut(Location::RequiresRegister());
2129 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002130 break;
2131
2132 default:
2133 LOG(FATAL) << "Unexpected type conversion from " << input_type
2134 << " to " << result_type;
2135 }
2136 break;
2137
Roland Levillaindff1f282014-11-05 14:15:05 +00002138 case Primitive::kPrimLong:
2139 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002140 case Primitive::kPrimBoolean:
2141 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002142 case Primitive::kPrimByte:
2143 case Primitive::kPrimShort:
2144 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002145 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002146 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002147 locations->SetInAt(0, Location::RequiresRegister());
2148 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2149 break;
2150
Roland Levillain624279f2014-12-04 11:54:28 +00002151 case Primitive::kPrimFloat: {
2152 // Processing a Dex `float-to-long' instruction.
2153 InvokeRuntimeCallingConvention calling_convention;
2154 locations->SetInAt(0, Location::FpuRegisterLocation(
2155 calling_convention.GetFpuRegisterAt(0)));
2156 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2157 break;
2158 }
2159
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002160 case Primitive::kPrimDouble: {
2161 // Processing a Dex `double-to-long' instruction.
2162 InvokeRuntimeCallingConvention calling_convention;
2163 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2164 calling_convention.GetFpuRegisterAt(0),
2165 calling_convention.GetFpuRegisterAt(1)));
2166 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002167 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002168 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002169
2170 default:
2171 LOG(FATAL) << "Unexpected type conversion from " << input_type
2172 << " to " << result_type;
2173 }
2174 break;
2175
Roland Levillain981e4542014-11-14 11:47:14 +00002176 case Primitive::kPrimChar:
2177 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002178 case Primitive::kPrimLong:
2179 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002180 case Primitive::kPrimBoolean:
2181 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002182 case Primitive::kPrimByte:
2183 case Primitive::kPrimShort:
2184 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002185 // Processing a Dex `int-to-char' instruction.
2186 locations->SetInAt(0, Location::RequiresRegister());
2187 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2188 break;
2189
2190 default:
2191 LOG(FATAL) << "Unexpected type conversion from " << input_type
2192 << " to " << result_type;
2193 }
2194 break;
2195
Roland Levillaindff1f282014-11-05 14:15:05 +00002196 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002197 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002198 case Primitive::kPrimBoolean:
2199 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002200 case Primitive::kPrimByte:
2201 case Primitive::kPrimShort:
2202 case Primitive::kPrimInt:
2203 case Primitive::kPrimChar:
2204 // Processing a Dex `int-to-float' instruction.
2205 locations->SetInAt(0, Location::RequiresRegister());
2206 locations->SetOut(Location::RequiresFpuRegister());
2207 break;
2208
Roland Levillain5b3ee562015-04-14 16:02:41 +01002209 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002210 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002211 InvokeRuntimeCallingConvention calling_convention;
2212 locations->SetInAt(0, Location::RegisterPairLocation(
2213 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2214 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002215 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002216 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002217
Roland Levillaincff13742014-11-17 14:32:17 +00002218 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002219 // Processing a Dex `double-to-float' instruction.
2220 locations->SetInAt(0, Location::RequiresFpuRegister());
2221 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002222 break;
2223
2224 default:
2225 LOG(FATAL) << "Unexpected type conversion from " << input_type
2226 << " to " << result_type;
2227 };
2228 break;
2229
Roland Levillaindff1f282014-11-05 14:15:05 +00002230 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002231 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002232 case Primitive::kPrimBoolean:
2233 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002234 case Primitive::kPrimByte:
2235 case Primitive::kPrimShort:
2236 case Primitive::kPrimInt:
2237 case Primitive::kPrimChar:
2238 // Processing a Dex `int-to-double' instruction.
2239 locations->SetInAt(0, Location::RequiresRegister());
2240 locations->SetOut(Location::RequiresFpuRegister());
2241 break;
2242
2243 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002244 // Processing a Dex `long-to-double' instruction.
2245 locations->SetInAt(0, Location::RequiresRegister());
2246 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002247 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002248 locations->AddTemp(Location::RequiresFpuRegister());
2249 break;
2250
Roland Levillaincff13742014-11-17 14:32:17 +00002251 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002252 // Processing a Dex `float-to-double' instruction.
2253 locations->SetInAt(0, Location::RequiresFpuRegister());
2254 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002255 break;
2256
2257 default:
2258 LOG(FATAL) << "Unexpected type conversion from " << input_type
2259 << " to " << result_type;
2260 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002261 break;
2262
2263 default:
2264 LOG(FATAL) << "Unexpected type conversion from " << input_type
2265 << " to " << result_type;
2266 }
2267}
2268
2269void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2270 LocationSummary* locations = conversion->GetLocations();
2271 Location out = locations->Out();
2272 Location in = locations->InAt(0);
2273 Primitive::Type result_type = conversion->GetResultType();
2274 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002275 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002276 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002277 case Primitive::kPrimByte:
2278 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002279 case Primitive::kPrimLong:
2280 // Type conversion from long to byte is a result of code transformations.
2281 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2282 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002283 case Primitive::kPrimBoolean:
2284 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002285 case Primitive::kPrimShort:
2286 case Primitive::kPrimInt:
2287 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002288 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002289 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002290 break;
2291
2292 default:
2293 LOG(FATAL) << "Unexpected type conversion from " << input_type
2294 << " to " << result_type;
2295 }
2296 break;
2297
Roland Levillain01a8d712014-11-14 16:27:39 +00002298 case Primitive::kPrimShort:
2299 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002300 case Primitive::kPrimLong:
2301 // Type conversion from long to short is a result of code transformations.
2302 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2303 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002304 case Primitive::kPrimBoolean:
2305 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002306 case Primitive::kPrimByte:
2307 case Primitive::kPrimInt:
2308 case Primitive::kPrimChar:
2309 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002310 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002311 break;
2312
2313 default:
2314 LOG(FATAL) << "Unexpected type conversion from " << input_type
2315 << " to " << result_type;
2316 }
2317 break;
2318
Roland Levillain946e1432014-11-11 17:35:19 +00002319 case Primitive::kPrimInt:
2320 switch (input_type) {
2321 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002322 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002323 DCHECK(out.IsRegister());
2324 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002325 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002326 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002328 } else {
2329 DCHECK(in.IsConstant());
2330 DCHECK(in.GetConstant()->IsLongConstant());
2331 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002333 }
2334 break;
2335
Roland Levillain3f8f9362014-12-02 17:45:01 +00002336 case Primitive::kPrimFloat: {
2337 // Processing a Dex `float-to-int' instruction.
2338 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002339 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002340 __ vmovrs(out.AsRegister<Register>(), temp);
2341 break;
2342 }
2343
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002344 case Primitive::kPrimDouble: {
2345 // Processing a Dex `double-to-int' instruction.
2346 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002347 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002348 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002349 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002350 }
Roland Levillain946e1432014-11-11 17:35:19 +00002351
2352 default:
2353 LOG(FATAL) << "Unexpected type conversion from " << input_type
2354 << " to " << result_type;
2355 }
2356 break;
2357
Roland Levillaindff1f282014-11-05 14:15:05 +00002358 case Primitive::kPrimLong:
2359 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002360 case Primitive::kPrimBoolean:
2361 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002362 case Primitive::kPrimByte:
2363 case Primitive::kPrimShort:
2364 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002365 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002366 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002367 DCHECK(out.IsRegisterPair());
2368 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002370 // Sign extension.
2371 __ Asr(out.AsRegisterPairHigh<Register>(),
2372 out.AsRegisterPairLow<Register>(),
2373 31);
2374 break;
2375
2376 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002377 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002378 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2379 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002380 conversion->GetDexPc(),
2381 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002382 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002383 break;
2384
Roland Levillaindff1f282014-11-05 14:15:05 +00002385 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002386 // Processing a Dex `double-to-long' instruction.
2387 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2388 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002389 conversion->GetDexPc(),
2390 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002391 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002392 break;
2393
2394 default:
2395 LOG(FATAL) << "Unexpected type conversion from " << input_type
2396 << " to " << result_type;
2397 }
2398 break;
2399
Roland Levillain981e4542014-11-14 11:47:14 +00002400 case Primitive::kPrimChar:
2401 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002402 case Primitive::kPrimLong:
2403 // Type conversion from long to char is a result of code transformations.
2404 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2405 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002406 case Primitive::kPrimBoolean:
2407 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002408 case Primitive::kPrimByte:
2409 case Primitive::kPrimShort:
2410 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002411 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002412 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002413 break;
2414
2415 default:
2416 LOG(FATAL) << "Unexpected type conversion from " << input_type
2417 << " to " << result_type;
2418 }
2419 break;
2420
Roland Levillaindff1f282014-11-05 14:15:05 +00002421 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002422 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002423 case Primitive::kPrimBoolean:
2424 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002425 case Primitive::kPrimByte:
2426 case Primitive::kPrimShort:
2427 case Primitive::kPrimInt:
2428 case Primitive::kPrimChar: {
2429 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002430 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2431 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002432 break;
2433 }
2434
Roland Levillain5b3ee562015-04-14 16:02:41 +01002435 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002436 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002437 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2438 conversion,
2439 conversion->GetDexPc(),
2440 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002441 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002442 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002443
Roland Levillaincff13742014-11-17 14:32:17 +00002444 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002445 // Processing a Dex `double-to-float' instruction.
2446 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2447 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002448 break;
2449
2450 default:
2451 LOG(FATAL) << "Unexpected type conversion from " << input_type
2452 << " to " << result_type;
2453 };
2454 break;
2455
Roland Levillaindff1f282014-11-05 14:15:05 +00002456 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002457 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002458 case Primitive::kPrimBoolean:
2459 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002460 case Primitive::kPrimByte:
2461 case Primitive::kPrimShort:
2462 case Primitive::kPrimInt:
2463 case Primitive::kPrimChar: {
2464 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002465 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002466 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2467 out.AsFpuRegisterPairLow<SRegister>());
2468 break;
2469 }
2470
Roland Levillain647b9ed2014-11-27 12:06:00 +00002471 case Primitive::kPrimLong: {
2472 // Processing a Dex `long-to-double' instruction.
2473 Register low = in.AsRegisterPairLow<Register>();
2474 Register high = in.AsRegisterPairHigh<Register>();
2475 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2476 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002477 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002478 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002479 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2480 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002481
Roland Levillain682393c2015-04-14 15:57:52 +01002482 // temp_d = int-to-double(high)
2483 __ vmovsr(temp_s, high);
2484 __ vcvtdi(temp_d, temp_s);
2485 // constant_d = k2Pow32EncodingForDouble
2486 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2487 // out_d = unsigned-to-double(low)
2488 __ vmovsr(out_s, low);
2489 __ vcvtdu(out_d, out_s);
2490 // out_d += temp_d * constant_d
2491 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002492 break;
2493 }
2494
Roland Levillaincff13742014-11-17 14:32:17 +00002495 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002496 // Processing a Dex `float-to-double' instruction.
2497 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2498 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002499 break;
2500
2501 default:
2502 LOG(FATAL) << "Unexpected type conversion from " << input_type
2503 << " to " << result_type;
2504 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002505 break;
2506
2507 default:
2508 LOG(FATAL) << "Unexpected type conversion from " << input_type
2509 << " to " << result_type;
2510 }
2511}
2512
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002513void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002514 LocationSummary* locations =
2515 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002516 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002517 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002518 locations->SetInAt(0, Location::RequiresRegister());
2519 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2521 break;
2522 }
2523
2524 case Primitive::kPrimLong: {
2525 locations->SetInAt(0, Location::RequiresRegister());
2526 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002527 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002528 break;
2529 }
2530
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002531 case Primitive::kPrimFloat:
2532 case Primitive::kPrimDouble: {
2533 locations->SetInAt(0, Location::RequiresFpuRegister());
2534 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002535 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002536 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002537 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002538
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002539 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002540 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002541 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002542}
2543
2544void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2545 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002546 Location out = locations->Out();
2547 Location first = locations->InAt(0);
2548 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002549 switch (add->GetResultType()) {
2550 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002551 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002552 __ add(out.AsRegister<Register>(),
2553 first.AsRegister<Register>(),
2554 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002555 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002556 __ AddConstant(out.AsRegister<Register>(),
2557 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002558 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002559 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002560 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002561
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002562 case Primitive::kPrimLong: {
2563 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002564 __ adds(out.AsRegisterPairLow<Register>(),
2565 first.AsRegisterPairLow<Register>(),
2566 ShifterOperand(second.AsRegisterPairLow<Register>()));
2567 __ adc(out.AsRegisterPairHigh<Register>(),
2568 first.AsRegisterPairHigh<Register>(),
2569 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002570 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002571 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002572
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002573 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002574 __ vadds(out.AsFpuRegister<SRegister>(),
2575 first.AsFpuRegister<SRegister>(),
2576 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002577 break;
2578
2579 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002580 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2581 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2582 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002583 break;
2584
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002585 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002586 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002587 }
2588}
2589
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002590void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002591 LocationSummary* locations =
2592 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002593 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002594 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002595 locations->SetInAt(0, Location::RequiresRegister());
2596 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002597 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2598 break;
2599 }
2600
2601 case Primitive::kPrimLong: {
2602 locations->SetInAt(0, Location::RequiresRegister());
2603 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002605 break;
2606 }
Calin Juravle11351682014-10-23 15:38:15 +01002607 case Primitive::kPrimFloat:
2608 case Primitive::kPrimDouble: {
2609 locations->SetInAt(0, Location::RequiresFpuRegister());
2610 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002611 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002612 break;
Calin Juravle11351682014-10-23 15:38:15 +01002613 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002614 default:
Calin Juravle11351682014-10-23 15:38:15 +01002615 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002616 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002617}
2618
2619void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2620 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002621 Location out = locations->Out();
2622 Location first = locations->InAt(0);
2623 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002624 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002625 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002626 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002627 __ sub(out.AsRegister<Register>(),
2628 first.AsRegister<Register>(),
2629 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002630 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002631 __ AddConstant(out.AsRegister<Register>(),
2632 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002633 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002634 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002635 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002636 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002637
Calin Juravle11351682014-10-23 15:38:15 +01002638 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002639 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002640 __ subs(out.AsRegisterPairLow<Register>(),
2641 first.AsRegisterPairLow<Register>(),
2642 ShifterOperand(second.AsRegisterPairLow<Register>()));
2643 __ sbc(out.AsRegisterPairHigh<Register>(),
2644 first.AsRegisterPairHigh<Register>(),
2645 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002646 break;
Calin Juravle11351682014-10-23 15:38:15 +01002647 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002648
Calin Juravle11351682014-10-23 15:38:15 +01002649 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002650 __ vsubs(out.AsFpuRegister<SRegister>(),
2651 first.AsFpuRegister<SRegister>(),
2652 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002653 break;
Calin Juravle11351682014-10-23 15:38:15 +01002654 }
2655
2656 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002657 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2658 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2659 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002660 break;
2661 }
2662
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002663
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002664 default:
Calin Juravle11351682014-10-23 15:38:15 +01002665 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002666 }
2667}
2668
Calin Juravle34bacdf2014-10-07 20:23:36 +01002669void LocationsBuilderARM::VisitMul(HMul* mul) {
2670 LocationSummary* locations =
2671 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2672 switch (mul->GetResultType()) {
2673 case Primitive::kPrimInt:
2674 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002675 locations->SetInAt(0, Location::RequiresRegister());
2676 locations->SetInAt(1, Location::RequiresRegister());
2677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002678 break;
2679 }
2680
Calin Juravleb5bfa962014-10-21 18:02:24 +01002681 case Primitive::kPrimFloat:
2682 case Primitive::kPrimDouble: {
2683 locations->SetInAt(0, Location::RequiresFpuRegister());
2684 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002685 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002686 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002687 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002688
2689 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002690 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002691 }
2692}
2693
2694void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2695 LocationSummary* locations = mul->GetLocations();
2696 Location out = locations->Out();
2697 Location first = locations->InAt(0);
2698 Location second = locations->InAt(1);
2699 switch (mul->GetResultType()) {
2700 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002701 __ mul(out.AsRegister<Register>(),
2702 first.AsRegister<Register>(),
2703 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002704 break;
2705 }
2706 case Primitive::kPrimLong: {
2707 Register out_hi = out.AsRegisterPairHigh<Register>();
2708 Register out_lo = out.AsRegisterPairLow<Register>();
2709 Register in1_hi = first.AsRegisterPairHigh<Register>();
2710 Register in1_lo = first.AsRegisterPairLow<Register>();
2711 Register in2_hi = second.AsRegisterPairHigh<Register>();
2712 Register in2_lo = second.AsRegisterPairLow<Register>();
2713
2714 // Extra checks to protect caused by the existence of R1_R2.
2715 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2716 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2717 DCHECK_NE(out_hi, in1_lo);
2718 DCHECK_NE(out_hi, in2_lo);
2719
2720 // input: in1 - 64 bits, in2 - 64 bits
2721 // output: out
2722 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2723 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2724 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2725
2726 // IP <- in1.lo * in2.hi
2727 __ mul(IP, in1_lo, in2_hi);
2728 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2729 __ mla(out_hi, in1_hi, in2_lo, IP);
2730 // out.lo <- (in1.lo * in2.lo)[31:0];
2731 __ umull(out_lo, IP, in1_lo, in2_lo);
2732 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2733 __ add(out_hi, out_hi, ShifterOperand(IP));
2734 break;
2735 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002736
2737 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002738 __ vmuls(out.AsFpuRegister<SRegister>(),
2739 first.AsFpuRegister<SRegister>(),
2740 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002741 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002742 }
2743
2744 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002745 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2746 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2747 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002748 break;
2749 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002750
2751 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002752 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002753 }
2754}
2755
Zheng Xuc6667102015-05-15 16:08:45 +08002756void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(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 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2767 DCHECK(imm == 1 || imm == -1);
2768
2769 if (instruction->IsRem()) {
2770 __ LoadImmediate(out, 0);
2771 } else {
2772 if (imm == 1) {
2773 __ Mov(out, dividend);
2774 } else {
2775 __ rsb(out, dividend, ShifterOperand(0));
2776 }
2777 }
2778}
2779
2780void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2781 DCHECK(instruction->IsDiv() || instruction->IsRem());
2782 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2783
2784 LocationSummary* locations = instruction->GetLocations();
2785 Location second = locations->InAt(1);
2786 DCHECK(second.IsConstant());
2787
2788 Register out = locations->Out().AsRegister<Register>();
2789 Register dividend = locations->InAt(0).AsRegister<Register>();
2790 Register temp = locations->GetTemp(0).AsRegister<Register>();
2791 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002792 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002793 int ctz_imm = CTZ(abs_imm);
2794
2795 if (ctz_imm == 1) {
2796 __ Lsr(temp, dividend, 32 - ctz_imm);
2797 } else {
2798 __ Asr(temp, dividend, 31);
2799 __ Lsr(temp, temp, 32 - ctz_imm);
2800 }
2801 __ add(out, temp, ShifterOperand(dividend));
2802
2803 if (instruction->IsDiv()) {
2804 __ Asr(out, out, ctz_imm);
2805 if (imm < 0) {
2806 __ rsb(out, out, ShifterOperand(0));
2807 }
2808 } else {
2809 __ ubfx(out, out, 0, ctz_imm);
2810 __ sub(out, out, ShifterOperand(temp));
2811 }
2812}
2813
2814void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2815 DCHECK(instruction->IsDiv() || instruction->IsRem());
2816 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2817
2818 LocationSummary* locations = instruction->GetLocations();
2819 Location second = locations->InAt(1);
2820 DCHECK(second.IsConstant());
2821
2822 Register out = locations->Out().AsRegister<Register>();
2823 Register dividend = locations->InAt(0).AsRegister<Register>();
2824 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2825 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2826 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2827
2828 int64_t magic;
2829 int shift;
2830 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2831
2832 __ LoadImmediate(temp1, magic);
2833 __ smull(temp2, temp1, dividend, temp1);
2834
2835 if (imm > 0 && magic < 0) {
2836 __ add(temp1, temp1, ShifterOperand(dividend));
2837 } else if (imm < 0 && magic > 0) {
2838 __ sub(temp1, temp1, ShifterOperand(dividend));
2839 }
2840
2841 if (shift != 0) {
2842 __ Asr(temp1, temp1, shift);
2843 }
2844
2845 if (instruction->IsDiv()) {
2846 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2847 } else {
2848 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2849 // TODO: Strength reduction for mls.
2850 __ LoadImmediate(temp2, imm);
2851 __ mls(out, temp1, temp2, dividend);
2852 }
2853}
2854
2855void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2856 DCHECK(instruction->IsDiv() || instruction->IsRem());
2857 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2858
2859 LocationSummary* locations = instruction->GetLocations();
2860 Location second = locations->InAt(1);
2861 DCHECK(second.IsConstant());
2862
2863 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2864 if (imm == 0) {
2865 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2866 } else if (imm == 1 || imm == -1) {
2867 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002868 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002869 DivRemByPowerOfTwo(instruction);
2870 } else {
2871 DCHECK(imm <= -2 || imm >= 2);
2872 GenerateDivRemWithAnyConstant(instruction);
2873 }
2874}
2875
Calin Juravle7c4954d2014-10-28 16:57:40 +00002876void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002877 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2878 if (div->GetResultType() == Primitive::kPrimLong) {
2879 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002880 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08002881 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2882 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002883 } else if (div->GetResultType() == Primitive::kPrimInt &&
2884 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2885 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002886 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002887 }
2888
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2890
Calin Juravle7c4954d2014-10-28 16:57:40 +00002891 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002892 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002893 if (div->InputAt(1)->IsConstant()) {
2894 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002895 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002896 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002897 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2898 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002899 // No temp register required.
2900 } else {
2901 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002902 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002903 locations->AddTemp(Location::RequiresRegister());
2904 }
2905 }
2906 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002907 locations->SetInAt(0, Location::RequiresRegister());
2908 locations->SetInAt(1, Location::RequiresRegister());
2909 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2910 } else {
2911 InvokeRuntimeCallingConvention calling_convention;
2912 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2913 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2914 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2915 // we only need the former.
2916 locations->SetOut(Location::RegisterLocation(R0));
2917 }
Calin Juravled0d48522014-11-04 16:40:20 +00002918 break;
2919 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002920 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002921 InvokeRuntimeCallingConvention calling_convention;
2922 locations->SetInAt(0, Location::RegisterPairLocation(
2923 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2924 locations->SetInAt(1, Location::RegisterPairLocation(
2925 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002926 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002927 break;
2928 }
2929 case Primitive::kPrimFloat:
2930 case Primitive::kPrimDouble: {
2931 locations->SetInAt(0, Location::RequiresFpuRegister());
2932 locations->SetInAt(1, Location::RequiresFpuRegister());
2933 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2934 break;
2935 }
2936
2937 default:
2938 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2939 }
2940}
2941
2942void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2943 LocationSummary* locations = div->GetLocations();
2944 Location out = locations->Out();
2945 Location first = locations->InAt(0);
2946 Location second = locations->InAt(1);
2947
2948 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002949 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002950 if (second.IsConstant()) {
2951 GenerateDivRemConstantIntegral(div);
2952 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002953 __ sdiv(out.AsRegister<Register>(),
2954 first.AsRegister<Register>(),
2955 second.AsRegister<Register>());
2956 } else {
2957 InvokeRuntimeCallingConvention calling_convention;
2958 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2959 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2960 DCHECK_EQ(R0, out.AsRegister<Register>());
2961
2962 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002963 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002964 }
Calin Juravled0d48522014-11-04 16:40:20 +00002965 break;
2966 }
2967
Calin Juravle7c4954d2014-10-28 16:57:40 +00002968 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002969 InvokeRuntimeCallingConvention calling_convention;
2970 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2971 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2972 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2973 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2974 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002975 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002976
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002977 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002978 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002979 break;
2980 }
2981
2982 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002983 __ vdivs(out.AsFpuRegister<SRegister>(),
2984 first.AsFpuRegister<SRegister>(),
2985 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002986 break;
2987 }
2988
2989 case Primitive::kPrimDouble: {
2990 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2991 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2992 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2993 break;
2994 }
2995
2996 default:
2997 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2998 }
2999}
3000
Calin Juravlebacfec32014-11-14 15:54:36 +00003001void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003002 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003003
3004 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003005 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003006 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3007 // sdiv will be replaced by other instruction sequence.
3008 call_kind = LocationSummary::kNoCall;
3009 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3010 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003011 // Have hardware divide instruction for int, do it with three instructions.
3012 call_kind = LocationSummary::kNoCall;
3013 }
3014
Calin Juravlebacfec32014-11-14 15:54:36 +00003015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3016
Calin Juravled2ec87d2014-12-08 14:24:46 +00003017 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003018 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003019 if (rem->InputAt(1)->IsConstant()) {
3020 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003021 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003022 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003023 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3024 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003025 // No temp register required.
3026 } else {
3027 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003028 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003029 locations->AddTemp(Location::RequiresRegister());
3030 }
3031 }
3032 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003033 locations->SetInAt(0, Location::RequiresRegister());
3034 locations->SetInAt(1, Location::RequiresRegister());
3035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3036 locations->AddTemp(Location::RequiresRegister());
3037 } else {
3038 InvokeRuntimeCallingConvention calling_convention;
3039 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3040 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3041 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3042 // we only need the latter.
3043 locations->SetOut(Location::RegisterLocation(R1));
3044 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003045 break;
3046 }
3047 case Primitive::kPrimLong: {
3048 InvokeRuntimeCallingConvention calling_convention;
3049 locations->SetInAt(0, Location::RegisterPairLocation(
3050 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3051 locations->SetInAt(1, Location::RegisterPairLocation(
3052 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3053 // The runtime helper puts the output in R2,R3.
3054 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3055 break;
3056 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003057 case Primitive::kPrimFloat: {
3058 InvokeRuntimeCallingConvention calling_convention;
3059 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3060 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3061 locations->SetOut(Location::FpuRegisterLocation(S0));
3062 break;
3063 }
3064
Calin Juravlebacfec32014-11-14 15:54:36 +00003065 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003066 InvokeRuntimeCallingConvention calling_convention;
3067 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3068 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3069 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3070 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3071 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003072 break;
3073 }
3074
3075 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003076 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003077 }
3078}
3079
3080void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3081 LocationSummary* locations = rem->GetLocations();
3082 Location out = locations->Out();
3083 Location first = locations->InAt(0);
3084 Location second = locations->InAt(1);
3085
Calin Juravled2ec87d2014-12-08 14:24:46 +00003086 Primitive::Type type = rem->GetResultType();
3087 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003088 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003089 if (second.IsConstant()) {
3090 GenerateDivRemConstantIntegral(rem);
3091 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003092 Register reg1 = first.AsRegister<Register>();
3093 Register reg2 = second.AsRegister<Register>();
3094 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003095
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003096 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003097 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003098 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003099 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003100 } else {
3101 InvokeRuntimeCallingConvention calling_convention;
3102 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3103 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3104 DCHECK_EQ(R1, out.AsRegister<Register>());
3105
3106 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003107 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003108 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003109 break;
3110 }
3111
3112 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003113 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003114 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003115 break;
3116 }
3117
Calin Juravled2ec87d2014-12-08 14:24:46 +00003118 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003119 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003120 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003121 break;
3122 }
3123
Calin Juravlebacfec32014-11-14 15:54:36 +00003124 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003125 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003126 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003127 break;
3128 }
3129
3130 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003131 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003132 }
3133}
3134
Calin Juravled0d48522014-11-04 16:40:20 +00003135void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003136 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3137 ? LocationSummary::kCallOnSlowPath
3138 : LocationSummary::kNoCall;
3139 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003140 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003141 if (instruction->HasUses()) {
3142 locations->SetOut(Location::SameAsFirstInput());
3143 }
3144}
3145
3146void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003147 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003148 codegen_->AddSlowPath(slow_path);
3149
3150 LocationSummary* locations = instruction->GetLocations();
3151 Location value = locations->InAt(0);
3152
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003153 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003154 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003155 case Primitive::kPrimByte:
3156 case Primitive::kPrimChar:
3157 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003158 case Primitive::kPrimInt: {
3159 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003160 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003161 } else {
3162 DCHECK(value.IsConstant()) << value;
3163 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3164 __ b(slow_path->GetEntryLabel());
3165 }
3166 }
3167 break;
3168 }
3169 case Primitive::kPrimLong: {
3170 if (value.IsRegisterPair()) {
3171 __ orrs(IP,
3172 value.AsRegisterPairLow<Register>(),
3173 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3174 __ b(slow_path->GetEntryLabel(), EQ);
3175 } else {
3176 DCHECK(value.IsConstant()) << value;
3177 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3178 __ b(slow_path->GetEntryLabel());
3179 }
3180 }
3181 break;
3182 default:
3183 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3184 }
3185 }
Calin Juravled0d48522014-11-04 16:40:20 +00003186}
3187
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003188void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3189 Register in = locations->InAt(0).AsRegister<Register>();
3190 Location rhs = locations->InAt(1);
3191 Register out = locations->Out().AsRegister<Register>();
3192
3193 if (rhs.IsConstant()) {
3194 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3195 // so map all rotations to a +ve. equivalent in that range.
3196 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3197 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3198 if (rot) {
3199 // Rotate, mapping left rotations to right equivalents if necessary.
3200 // (e.g. left by 2 bits == right by 30.)
3201 __ Ror(out, in, rot);
3202 } else if (out != in) {
3203 __ Mov(out, in);
3204 }
3205 } else {
3206 __ Ror(out, in, rhs.AsRegister<Register>());
3207 }
3208}
3209
3210// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3211// rotates by swapping input regs (effectively rotating by the first 32-bits of
3212// a larger rotation) or flipping direction (thus treating larger right/left
3213// rotations as sub-word sized rotations in the other direction) as appropriate.
3214void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3215 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3216 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3217 Location rhs = locations->InAt(1);
3218 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3219 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3220
3221 if (rhs.IsConstant()) {
3222 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3223 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003224 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003225 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3226 // logic below to a simple pair of binary orr.
3227 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3228 if (rot >= kArmBitsPerWord) {
3229 rot -= kArmBitsPerWord;
3230 std::swap(in_reg_hi, in_reg_lo);
3231 }
3232 // Rotate, or mov to out for zero or word size rotations.
3233 if (rot != 0u) {
3234 __ Lsr(out_reg_hi, in_reg_hi, rot);
3235 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3236 __ Lsr(out_reg_lo, in_reg_lo, rot);
3237 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3238 } else {
3239 __ Mov(out_reg_lo, in_reg_lo);
3240 __ Mov(out_reg_hi, in_reg_hi);
3241 }
3242 } else {
3243 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3244 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3245 Label end;
3246 Label shift_by_32_plus_shift_right;
3247
3248 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3249 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3250 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3251 __ b(&shift_by_32_plus_shift_right, CC);
3252
3253 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3254 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3255 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3256 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3257 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3258 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3259 __ Lsr(shift_left, in_reg_hi, shift_right);
3260 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3261 __ b(&end);
3262
3263 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3264 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3265 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3266 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3267 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3268 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3269 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3270 __ Lsl(shift_right, in_reg_hi, shift_left);
3271 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3272
3273 __ Bind(&end);
3274 }
3275}
Roland Levillain22c49222016-03-18 14:04:28 +00003276
3277void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003278 LocationSummary* locations =
3279 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3280 switch (ror->GetResultType()) {
3281 case Primitive::kPrimInt: {
3282 locations->SetInAt(0, Location::RequiresRegister());
3283 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3285 break;
3286 }
3287 case Primitive::kPrimLong: {
3288 locations->SetInAt(0, Location::RequiresRegister());
3289 if (ror->InputAt(1)->IsConstant()) {
3290 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3291 } else {
3292 locations->SetInAt(1, Location::RequiresRegister());
3293 locations->AddTemp(Location::RequiresRegister());
3294 locations->AddTemp(Location::RequiresRegister());
3295 }
3296 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3297 break;
3298 }
3299 default:
3300 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3301 }
3302}
3303
Roland Levillain22c49222016-03-18 14:04:28 +00003304void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003305 LocationSummary* locations = ror->GetLocations();
3306 Primitive::Type type = ror->GetResultType();
3307 switch (type) {
3308 case Primitive::kPrimInt: {
3309 HandleIntegerRotate(locations);
3310 break;
3311 }
3312 case Primitive::kPrimLong: {
3313 HandleLongRotate(locations);
3314 break;
3315 }
3316 default:
3317 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003318 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003319 }
3320}
3321
Calin Juravle9aec02f2014-11-18 23:06:35 +00003322void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3323 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3324
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003325 LocationSummary* locations =
3326 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003327
3328 switch (op->GetResultType()) {
3329 case Primitive::kPrimInt: {
3330 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003331 if (op->InputAt(1)->IsConstant()) {
3332 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3334 } else {
3335 locations->SetInAt(1, Location::RequiresRegister());
3336 // Make the output overlap, as it will be used to hold the masked
3337 // second input.
3338 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3339 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003340 break;
3341 }
3342 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003343 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003344 if (op->InputAt(1)->IsConstant()) {
3345 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3346 // For simplicity, use kOutputOverlap even though we only require that low registers
3347 // don't clash with high registers which the register allocator currently guarantees.
3348 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3349 } else {
3350 locations->SetInAt(1, Location::RequiresRegister());
3351 locations->AddTemp(Location::RequiresRegister());
3352 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3353 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003354 break;
3355 }
3356 default:
3357 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3358 }
3359}
3360
3361void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3362 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3363
3364 LocationSummary* locations = op->GetLocations();
3365 Location out = locations->Out();
3366 Location first = locations->InAt(0);
3367 Location second = locations->InAt(1);
3368
3369 Primitive::Type type = op->GetResultType();
3370 switch (type) {
3371 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003372 Register out_reg = out.AsRegister<Register>();
3373 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003374 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003375 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003376 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003377 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003378 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003379 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003380 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003381 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003382 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003383 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003384 }
3385 } else {
3386 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003387 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003388 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003389 __ Mov(out_reg, first_reg);
3390 } else if (op->IsShl()) {
3391 __ Lsl(out_reg, first_reg, shift_value);
3392 } else if (op->IsShr()) {
3393 __ Asr(out_reg, first_reg, shift_value);
3394 } else {
3395 __ Lsr(out_reg, first_reg, shift_value);
3396 }
3397 }
3398 break;
3399 }
3400 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003401 Register o_h = out.AsRegisterPairHigh<Register>();
3402 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003403
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003404 Register high = first.AsRegisterPairHigh<Register>();
3405 Register low = first.AsRegisterPairLow<Register>();
3406
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003407 if (second.IsRegister()) {
3408 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003409
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003410 Register second_reg = second.AsRegister<Register>();
3411
3412 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003413 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003414 // Shift the high part
3415 __ Lsl(o_h, high, o_l);
3416 // Shift the low part and `or` what overflew on the high part
3417 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3418 __ Lsr(temp, low, temp);
3419 __ orr(o_h, o_h, ShifterOperand(temp));
3420 // If the shift is > 32 bits, override the high part
3421 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3422 __ it(PL);
3423 __ Lsl(o_h, low, temp, PL);
3424 // Shift the low part
3425 __ Lsl(o_l, low, o_l);
3426 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003427 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003428 // Shift the low part
3429 __ Lsr(o_l, low, o_h);
3430 // Shift the high part and `or` what underflew on the low part
3431 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3432 __ Lsl(temp, high, temp);
3433 __ orr(o_l, o_l, ShifterOperand(temp));
3434 // If the shift is > 32 bits, override the low part
3435 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3436 __ it(PL);
3437 __ Asr(o_l, high, temp, PL);
3438 // Shift the high part
3439 __ Asr(o_h, high, o_h);
3440 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003441 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003442 // same as Shr except we use `Lsr`s and not `Asr`s
3443 __ Lsr(o_l, low, o_h);
3444 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3445 __ Lsl(temp, high, temp);
3446 __ orr(o_l, o_l, ShifterOperand(temp));
3447 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3448 __ it(PL);
3449 __ Lsr(o_l, high, temp, PL);
3450 __ Lsr(o_h, high, o_h);
3451 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003452 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003453 // Register allocator doesn't create partial overlap.
3454 DCHECK_NE(o_l, high);
3455 DCHECK_NE(o_h, low);
3456 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003457 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003458 if (shift_value > 32) {
3459 if (op->IsShl()) {
3460 __ Lsl(o_h, low, shift_value - 32);
3461 __ LoadImmediate(o_l, 0);
3462 } else if (op->IsShr()) {
3463 __ Asr(o_l, high, shift_value - 32);
3464 __ Asr(o_h, high, 31);
3465 } else {
3466 __ Lsr(o_l, high, shift_value - 32);
3467 __ LoadImmediate(o_h, 0);
3468 }
3469 } else if (shift_value == 32) {
3470 if (op->IsShl()) {
3471 __ mov(o_h, ShifterOperand(low));
3472 __ LoadImmediate(o_l, 0);
3473 } else if (op->IsShr()) {
3474 __ mov(o_l, ShifterOperand(high));
3475 __ Asr(o_h, high, 31);
3476 } else {
3477 __ mov(o_l, ShifterOperand(high));
3478 __ LoadImmediate(o_h, 0);
3479 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003480 } else if (shift_value == 1) {
3481 if (op->IsShl()) {
3482 __ Lsls(o_l, low, 1);
3483 __ adc(o_h, high, ShifterOperand(high));
3484 } else if (op->IsShr()) {
3485 __ Asrs(o_h, high, 1);
3486 __ Rrx(o_l, low);
3487 } else {
3488 __ Lsrs(o_h, high, 1);
3489 __ Rrx(o_l, low);
3490 }
3491 } else {
3492 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003493 if (op->IsShl()) {
3494 __ Lsl(o_h, high, shift_value);
3495 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3496 __ Lsl(o_l, low, shift_value);
3497 } else if (op->IsShr()) {
3498 __ Lsr(o_l, low, shift_value);
3499 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3500 __ Asr(o_h, high, shift_value);
3501 } else {
3502 __ Lsr(o_l, low, shift_value);
3503 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3504 __ Lsr(o_h, high, shift_value);
3505 }
3506 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003507 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003508 break;
3509 }
3510 default:
3511 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003512 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003513 }
3514}
3515
3516void LocationsBuilderARM::VisitShl(HShl* shl) {
3517 HandleShift(shl);
3518}
3519
3520void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3521 HandleShift(shl);
3522}
3523
3524void LocationsBuilderARM::VisitShr(HShr* shr) {
3525 HandleShift(shr);
3526}
3527
3528void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3529 HandleShift(shr);
3530}
3531
3532void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3533 HandleShift(ushr);
3534}
3535
3536void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3537 HandleShift(ushr);
3538}
3539
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003540void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003541 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003542 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003543 if (instruction->IsStringAlloc()) {
3544 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3545 } else {
3546 InvokeRuntimeCallingConvention calling_convention;
3547 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3548 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3549 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003550 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003551}
3552
3553void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003554 // Note: if heap poisoning is enabled, the entry point takes cares
3555 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003556 if (instruction->IsStringAlloc()) {
3557 // String is allocated through StringFactory. Call NewEmptyString entry point.
3558 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3559 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3560 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3561 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3562 __ blx(LR);
3563 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3564 } else {
3565 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3566 instruction,
3567 instruction->GetDexPc(),
3568 nullptr);
3569 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3570 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003571}
3572
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003573void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3574 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003575 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003576 InvokeRuntimeCallingConvention calling_convention;
3577 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003578 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003579 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003580 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003581}
3582
3583void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3584 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003585 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003586 // Note: if heap poisoning is enabled, the entry point takes cares
3587 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003588 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003589 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003590 instruction->GetDexPc(),
3591 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003592 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003593}
3594
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003595void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003596 LocationSummary* locations =
3597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003598 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3599 if (location.IsStackSlot()) {
3600 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3601 } else if (location.IsDoubleStackSlot()) {
3602 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003603 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003604 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003605}
3606
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003607void InstructionCodeGeneratorARM::VisitParameterValue(
3608 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003609 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003610}
3611
3612void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3613 LocationSummary* locations =
3614 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3615 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3616}
3617
3618void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3619 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003620}
3621
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003622void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003623 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003624 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003625 locations->SetInAt(0, Location::RequiresRegister());
3626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003627}
3628
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003629void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3630 LocationSummary* locations = not_->GetLocations();
3631 Location out = locations->Out();
3632 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003633 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003634 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003635 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003636 break;
3637
3638 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003639 __ mvn(out.AsRegisterPairLow<Register>(),
3640 ShifterOperand(in.AsRegisterPairLow<Register>()));
3641 __ mvn(out.AsRegisterPairHigh<Register>(),
3642 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003643 break;
3644
3645 default:
3646 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3647 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003648}
3649
David Brazdil66d126e2015-04-03 16:02:44 +01003650void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3651 LocationSummary* locations =
3652 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3653 locations->SetInAt(0, Location::RequiresRegister());
3654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3655}
3656
3657void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003658 LocationSummary* locations = bool_not->GetLocations();
3659 Location out = locations->Out();
3660 Location in = locations->InAt(0);
3661 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3662}
3663
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003664void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003665 LocationSummary* locations =
3666 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003667 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003668 case Primitive::kPrimBoolean:
3669 case Primitive::kPrimByte:
3670 case Primitive::kPrimShort:
3671 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003672 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003673 case Primitive::kPrimLong: {
3674 locations->SetInAt(0, Location::RequiresRegister());
3675 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003676 // Output overlaps because it is written before doing the low comparison.
3677 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003678 break;
3679 }
3680 case Primitive::kPrimFloat:
3681 case Primitive::kPrimDouble: {
3682 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003683 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003684 locations->SetOut(Location::RequiresRegister());
3685 break;
3686 }
3687 default:
3688 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3689 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003690}
3691
3692void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003693 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003694 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003695 Location left = locations->InAt(0);
3696 Location right = locations->InAt(1);
3697
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003698 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003699 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003700 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003701 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003702 case Primitive::kPrimBoolean:
3703 case Primitive::kPrimByte:
3704 case Primitive::kPrimShort:
3705 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003706 case Primitive::kPrimInt: {
3707 __ LoadImmediate(out, 0);
3708 __ cmp(left.AsRegister<Register>(),
3709 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3710 less_cond = LT;
3711 break;
3712 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003713 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003714 __ cmp(left.AsRegisterPairHigh<Register>(),
3715 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003716 __ b(&less, LT);
3717 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003718 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003719 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003720 __ cmp(left.AsRegisterPairLow<Register>(),
3721 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003722 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003723 break;
3724 }
3725 case Primitive::kPrimFloat:
3726 case Primitive::kPrimDouble: {
3727 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003728 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003729 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003730 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003731 break;
3732 }
3733 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003734 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003735 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003736 }
Aart Bika19616e2016-02-01 18:57:58 -08003737
Calin Juravleddb7df22014-11-25 20:56:51 +00003738 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003739 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003740
3741 __ Bind(&greater);
3742 __ LoadImmediate(out, 1);
3743 __ b(&done);
3744
3745 __ Bind(&less);
3746 __ LoadImmediate(out, -1);
3747
3748 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003749}
3750
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003751void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003752 LocationSummary* locations =
3753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003754 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003755 locations->SetInAt(i, Location::Any());
3756 }
3757 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003758}
3759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003760void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003761 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003762}
3763
Roland Levillainc9285912015-12-18 10:38:42 +00003764void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3765 // TODO (ported from quick): revisit ARM barrier kinds.
3766 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003767 switch (kind) {
3768 case MemBarrierKind::kAnyStore:
3769 case MemBarrierKind::kLoadAny:
3770 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003771 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003772 break;
3773 }
3774 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003775 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003776 break;
3777 }
3778 default:
3779 LOG(FATAL) << "Unexpected memory barrier " << kind;
3780 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003781 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003782}
3783
3784void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3785 uint32_t offset,
3786 Register out_lo,
3787 Register out_hi) {
3788 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003789 // Ensure `out_lo` is different from `addr`, so that loading
3790 // `offset` into `out_lo` does not clutter `addr`.
3791 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003792 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003793 __ add(IP, addr, ShifterOperand(out_lo));
3794 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003795 }
3796 __ ldrexd(out_lo, out_hi, addr);
3797}
3798
3799void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3800 uint32_t offset,
3801 Register value_lo,
3802 Register value_hi,
3803 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003804 Register temp2,
3805 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003806 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003807 if (offset != 0) {
3808 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003809 __ add(IP, addr, ShifterOperand(temp1));
3810 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003811 }
3812 __ Bind(&fail);
3813 // We need a load followed by store. (The address used in a STREX instruction must
3814 // be the same as the address in the most recently executed LDREX instruction.)
3815 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003816 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003817 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003818 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003819}
3820
3821void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3822 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3823
Nicolas Geoffray39468442014-09-02 15:17:15 +01003824 LocationSummary* locations =
3825 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003826 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003827
Calin Juravle52c48962014-12-16 17:02:57 +00003828 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003829 if (Primitive::IsFloatingPointType(field_type)) {
3830 locations->SetInAt(1, Location::RequiresFpuRegister());
3831 } else {
3832 locations->SetInAt(1, Location::RequiresRegister());
3833 }
3834
Calin Juravle52c48962014-12-16 17:02:57 +00003835 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003836 bool generate_volatile = field_info.IsVolatile()
3837 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003838 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003839 bool needs_write_barrier =
3840 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003841 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003842 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003843 if (needs_write_barrier) {
3844 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003845 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003846 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003847 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003848 // - registers need to be consecutive
3849 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003850 // We don't test for ARM yet, and the assertion makes sure that we
3851 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003852 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3853
3854 locations->AddTemp(Location::RequiresRegister());
3855 locations->AddTemp(Location::RequiresRegister());
3856 if (field_type == Primitive::kPrimDouble) {
3857 // For doubles we need two more registers to copy the value.
3858 locations->AddTemp(Location::RegisterLocation(R2));
3859 locations->AddTemp(Location::RegisterLocation(R3));
3860 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003861 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862}
3863
Calin Juravle52c48962014-12-16 17:02:57 +00003864void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003865 const FieldInfo& field_info,
3866 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003867 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3868
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003869 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003870 Register base = locations->InAt(0).AsRegister<Register>();
3871 Location value = locations->InAt(1);
3872
3873 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003874 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003875 Primitive::Type field_type = field_info.GetFieldType();
3876 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003877 bool needs_write_barrier =
3878 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003879
3880 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003881 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003882 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003883
3884 switch (field_type) {
3885 case Primitive::kPrimBoolean:
3886 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003887 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003888 break;
3889 }
3890
3891 case Primitive::kPrimShort:
3892 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003893 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003894 break;
3895 }
3896
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003897 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003898 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003899 if (kPoisonHeapReferences && needs_write_barrier) {
3900 // Note that in the case where `value` is a null reference,
3901 // we do not enter this block, as a null reference does not
3902 // need poisoning.
3903 DCHECK_EQ(field_type, Primitive::kPrimNot);
3904 Register temp = locations->GetTemp(0).AsRegister<Register>();
3905 __ Mov(temp, value.AsRegister<Register>());
3906 __ PoisonHeapReference(temp);
3907 __ StoreToOffset(kStoreWord, temp, base, offset);
3908 } else {
3909 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3910 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003911 break;
3912 }
3913
3914 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003915 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003916 GenerateWideAtomicStore(base, offset,
3917 value.AsRegisterPairLow<Register>(),
3918 value.AsRegisterPairHigh<Register>(),
3919 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003920 locations->GetTemp(1).AsRegister<Register>(),
3921 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003922 } else {
3923 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003924 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003925 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003926 break;
3927 }
3928
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003929 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003930 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003931 break;
3932 }
3933
3934 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003935 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003936 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003937 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3938 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3939
3940 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3941
3942 GenerateWideAtomicStore(base, offset,
3943 value_reg_lo,
3944 value_reg_hi,
3945 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003946 locations->GetTemp(3).AsRegister<Register>(),
3947 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003948 } else {
3949 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003950 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003951 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003952 break;
3953 }
3954
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003955 case Primitive::kPrimVoid:
3956 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003957 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003958 }
Calin Juravle52c48962014-12-16 17:02:57 +00003959
Calin Juravle77520bc2015-01-12 18:45:46 +00003960 // Longs and doubles are handled in the switch.
3961 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3962 codegen_->MaybeRecordImplicitNullCheck(instruction);
3963 }
3964
3965 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3966 Register temp = locations->GetTemp(0).AsRegister<Register>();
3967 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003968 codegen_->MarkGCCard(
3969 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003970 }
3971
Calin Juravle52c48962014-12-16 17:02:57 +00003972 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003973 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003974 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003975}
3976
Calin Juravle52c48962014-12-16 17:02:57 +00003977void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3978 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003979
3980 bool object_field_get_with_read_barrier =
3981 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003982 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003983 new (GetGraph()->GetArena()) LocationSummary(instruction,
3984 object_field_get_with_read_barrier ?
3985 LocationSummary::kCallOnSlowPath :
3986 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003987 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003988
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003989 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003990 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003991 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003992 // The output overlaps in case of volatile long: we don't want the
3993 // code generated by GenerateWideAtomicLoad to overwrite the
3994 // object's location. Likewise, in the case of an object field get
3995 // with read barriers enabled, we do not want the load to overwrite
3996 // the object's location, as we need it to emit the read barrier.
3997 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3998 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003999
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004000 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4001 locations->SetOut(Location::RequiresFpuRegister());
4002 } else {
4003 locations->SetOut(Location::RequiresRegister(),
4004 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4005 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004006 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004007 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004008 // - registers need to be consecutive
4009 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004010 // We don't test for ARM yet, and the assertion makes sure that we
4011 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004012 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4013 locations->AddTemp(Location::RequiresRegister());
4014 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004015 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4016 // We need a temporary register for the read barrier marking slow
4017 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4018 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004019 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004020}
4021
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004022Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4023 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4024 << input->GetType();
4025 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4026 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4027 return Location::ConstantLocation(input->AsConstant());
4028 } else {
4029 return Location::RequiresFpuRegister();
4030 }
4031}
4032
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004033Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4034 Opcode opcode) {
4035 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4036 if (constant->IsConstant() &&
4037 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4038 return Location::ConstantLocation(constant->AsConstant());
4039 }
4040 return Location::RequiresRegister();
4041}
4042
4043bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4044 Opcode opcode) {
4045 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4046 if (Primitive::Is64BitType(input_cst->GetType())) {
4047 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4048 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4049 } else {
4050 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4051 }
4052}
4053
4054bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4055 ShifterOperand so;
4056 ArmAssembler* assembler = codegen_->GetAssembler();
4057 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4058 return true;
4059 }
4060 Opcode neg_opcode = kNoOperand;
4061 switch (opcode) {
4062 case AND:
4063 neg_opcode = BIC;
4064 break;
4065 case ORR:
4066 neg_opcode = ORN;
4067 break;
4068 default:
4069 return false;
4070 }
4071 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4072}
4073
Calin Juravle52c48962014-12-16 17:02:57 +00004074void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4075 const FieldInfo& field_info) {
4076 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004077
Calin Juravle52c48962014-12-16 17:02:57 +00004078 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004079 Location base_loc = locations->InAt(0);
4080 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004081 Location out = locations->Out();
4082 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004083 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004084 Primitive::Type field_type = field_info.GetFieldType();
4085 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4086
4087 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004088 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004089 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004090 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004091
Roland Levillainc9285912015-12-18 10:38:42 +00004092 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004093 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004094 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004095
Roland Levillainc9285912015-12-18 10:38:42 +00004096 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004097 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004098 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004099
Roland Levillainc9285912015-12-18 10:38:42 +00004100 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004101 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004103
4104 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004105 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004106 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004107
4108 case Primitive::kPrimNot: {
4109 // /* HeapReference<Object> */ out = *(base + offset)
4110 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4111 Location temp_loc = locations->GetTemp(0);
4112 // Note that a potential implicit null check is handled in this
4113 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4114 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4115 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4116 if (is_volatile) {
4117 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4118 }
4119 } else {
4120 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4121 codegen_->MaybeRecordImplicitNullCheck(instruction);
4122 if (is_volatile) {
4123 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4124 }
4125 // If read barriers are enabled, emit read barriers other than
4126 // Baker's using a slow path (and also unpoison the loaded
4127 // reference, if heap poisoning is enabled).
4128 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4129 }
4130 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004131 }
4132
Roland Levillainc9285912015-12-18 10:38:42 +00004133 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004134 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004135 GenerateWideAtomicLoad(base, offset,
4136 out.AsRegisterPairLow<Register>(),
4137 out.AsRegisterPairHigh<Register>());
4138 } else {
4139 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4140 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004141 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004142
Roland Levillainc9285912015-12-18 10:38:42 +00004143 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004144 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004145 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004146
4147 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004148 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004149 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004150 Register lo = locations->GetTemp(0).AsRegister<Register>();
4151 Register hi = locations->GetTemp(1).AsRegister<Register>();
4152 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004153 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004154 __ vmovdrr(out_reg, lo, hi);
4155 } else {
4156 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004157 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004158 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004159 break;
4160 }
4161
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004162 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004163 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004164 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165 }
Calin Juravle52c48962014-12-16 17:02:57 +00004166
Roland Levillainc9285912015-12-18 10:38:42 +00004167 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4168 // Potential implicit null checks, in the case of reference or
4169 // double fields, are handled in the previous switch statement.
4170 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004171 codegen_->MaybeRecordImplicitNullCheck(instruction);
4172 }
4173
Calin Juravle52c48962014-12-16 17:02:57 +00004174 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004175 if (field_type == Primitive::kPrimNot) {
4176 // Memory barriers, in the case of references, are also handled
4177 // in the previous switch statement.
4178 } else {
4179 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4180 }
Roland Levillain4d027112015-07-01 15:41:14 +01004181 }
Calin Juravle52c48962014-12-16 17:02:57 +00004182}
4183
4184void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4185 HandleFieldSet(instruction, instruction->GetFieldInfo());
4186}
4187
4188void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004189 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004190}
4191
4192void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4193 HandleFieldGet(instruction, instruction->GetFieldInfo());
4194}
4195
4196void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4197 HandleFieldGet(instruction, instruction->GetFieldInfo());
4198}
4199
4200void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4201 HandleFieldGet(instruction, instruction->GetFieldInfo());
4202}
4203
4204void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4205 HandleFieldGet(instruction, instruction->GetFieldInfo());
4206}
4207
4208void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4209 HandleFieldSet(instruction, instruction->GetFieldInfo());
4210}
4211
4212void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004213 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004214}
4215
Calin Juravlee460d1d2015-09-29 04:52:17 +01004216void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4217 HUnresolvedInstanceFieldGet* instruction) {
4218 FieldAccessCallingConventionARM calling_convention;
4219 codegen_->CreateUnresolvedFieldLocationSummary(
4220 instruction, instruction->GetFieldType(), calling_convention);
4221}
4222
4223void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4224 HUnresolvedInstanceFieldGet* instruction) {
4225 FieldAccessCallingConventionARM calling_convention;
4226 codegen_->GenerateUnresolvedFieldAccess(instruction,
4227 instruction->GetFieldType(),
4228 instruction->GetFieldIndex(),
4229 instruction->GetDexPc(),
4230 calling_convention);
4231}
4232
4233void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4234 HUnresolvedInstanceFieldSet* instruction) {
4235 FieldAccessCallingConventionARM calling_convention;
4236 codegen_->CreateUnresolvedFieldLocationSummary(
4237 instruction, instruction->GetFieldType(), calling_convention);
4238}
4239
4240void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4241 HUnresolvedInstanceFieldSet* instruction) {
4242 FieldAccessCallingConventionARM calling_convention;
4243 codegen_->GenerateUnresolvedFieldAccess(instruction,
4244 instruction->GetFieldType(),
4245 instruction->GetFieldIndex(),
4246 instruction->GetDexPc(),
4247 calling_convention);
4248}
4249
4250void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4251 HUnresolvedStaticFieldGet* instruction) {
4252 FieldAccessCallingConventionARM calling_convention;
4253 codegen_->CreateUnresolvedFieldLocationSummary(
4254 instruction, instruction->GetFieldType(), calling_convention);
4255}
4256
4257void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4258 HUnresolvedStaticFieldGet* instruction) {
4259 FieldAccessCallingConventionARM calling_convention;
4260 codegen_->GenerateUnresolvedFieldAccess(instruction,
4261 instruction->GetFieldType(),
4262 instruction->GetFieldIndex(),
4263 instruction->GetDexPc(),
4264 calling_convention);
4265}
4266
4267void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4268 HUnresolvedStaticFieldSet* instruction) {
4269 FieldAccessCallingConventionARM calling_convention;
4270 codegen_->CreateUnresolvedFieldLocationSummary(
4271 instruction, instruction->GetFieldType(), calling_convention);
4272}
4273
4274void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4275 HUnresolvedStaticFieldSet* instruction) {
4276 FieldAccessCallingConventionARM calling_convention;
4277 codegen_->GenerateUnresolvedFieldAccess(instruction,
4278 instruction->GetFieldType(),
4279 instruction->GetFieldIndex(),
4280 instruction->GetDexPc(),
4281 calling_convention);
4282}
4283
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004284void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004285 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4286 ? LocationSummary::kCallOnSlowPath
4287 : LocationSummary::kNoCall;
4288 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004289 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004290 if (instruction->HasUses()) {
4291 locations->SetOut(Location::SameAsFirstInput());
4292 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004293}
4294
Calin Juravle2ae48182016-03-16 14:05:09 +00004295void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4296 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004297 return;
4298 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004299 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004300
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004301 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004302 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004303}
4304
Calin Juravle2ae48182016-03-16 14:05:09 +00004305void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004306 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004307 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004308
4309 LocationSummary* locations = instruction->GetLocations();
4310 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004311
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004312 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313}
4314
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004315void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004316 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004317}
4318
Artem Serov6c916792016-07-11 14:02:34 +01004319static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4320 switch (type) {
4321 case Primitive::kPrimNot:
4322 return kLoadWord;
4323 case Primitive::kPrimBoolean:
4324 return kLoadUnsignedByte;
4325 case Primitive::kPrimByte:
4326 return kLoadSignedByte;
4327 case Primitive::kPrimChar:
4328 return kLoadUnsignedHalfword;
4329 case Primitive::kPrimShort:
4330 return kLoadSignedHalfword;
4331 case Primitive::kPrimInt:
4332 return kLoadWord;
4333 case Primitive::kPrimLong:
4334 return kLoadWordPair;
4335 case Primitive::kPrimFloat:
4336 return kLoadSWord;
4337 case Primitive::kPrimDouble:
4338 return kLoadDWord;
4339 default:
4340 LOG(FATAL) << "Unreachable type " << type;
4341 UNREACHABLE();
4342 }
4343}
4344
4345static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4346 switch (type) {
4347 case Primitive::kPrimNot:
4348 return kStoreWord;
4349 case Primitive::kPrimBoolean:
4350 case Primitive::kPrimByte:
4351 return kStoreByte;
4352 case Primitive::kPrimChar:
4353 case Primitive::kPrimShort:
4354 return kStoreHalfword;
4355 case Primitive::kPrimInt:
4356 return kStoreWord;
4357 case Primitive::kPrimLong:
4358 return kStoreWordPair;
4359 case Primitive::kPrimFloat:
4360 return kStoreSWord;
4361 case Primitive::kPrimDouble:
4362 return kStoreDWord;
4363 default:
4364 LOG(FATAL) << "Unreachable type " << type;
4365 UNREACHABLE();
4366 }
4367}
4368
4369void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4370 Location out_loc,
4371 Register base,
4372 Register reg_offset,
4373 Condition cond) {
4374 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4375 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4376
4377 switch (type) {
4378 case Primitive::kPrimByte:
4379 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4380 break;
4381 case Primitive::kPrimBoolean:
4382 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4383 break;
4384 case Primitive::kPrimShort:
4385 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4386 break;
4387 case Primitive::kPrimChar:
4388 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4389 break;
4390 case Primitive::kPrimNot:
4391 case Primitive::kPrimInt:
4392 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4393 break;
4394 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4395 case Primitive::kPrimLong:
4396 case Primitive::kPrimFloat:
4397 case Primitive::kPrimDouble:
4398 default:
4399 LOG(FATAL) << "Unreachable type " << type;
4400 UNREACHABLE();
4401 }
4402}
4403
4404void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4405 Location loc,
4406 Register base,
4407 Register reg_offset,
4408 Condition cond) {
4409 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4410 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4411
4412 switch (type) {
4413 case Primitive::kPrimByte:
4414 case Primitive::kPrimBoolean:
4415 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4416 break;
4417 case Primitive::kPrimShort:
4418 case Primitive::kPrimChar:
4419 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4420 break;
4421 case Primitive::kPrimNot:
4422 case Primitive::kPrimInt:
4423 __ str(loc.AsRegister<Register>(), mem_address, cond);
4424 break;
4425 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4426 case Primitive::kPrimLong:
4427 case Primitive::kPrimFloat:
4428 case Primitive::kPrimDouble:
4429 default:
4430 LOG(FATAL) << "Unreachable type " << type;
4431 UNREACHABLE();
4432 }
4433}
4434
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004435void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004436 bool object_array_get_with_read_barrier =
4437 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004438 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004439 new (GetGraph()->GetArena()) LocationSummary(instruction,
4440 object_array_get_with_read_barrier ?
4441 LocationSummary::kCallOnSlowPath :
4442 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004443 locations->SetInAt(0, Location::RequiresRegister());
4444 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004445 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4446 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4447 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004448 // The output overlaps in the case of an object array get with
4449 // read barriers enabled: we do not want the move to overwrite the
4450 // array's location, as we need it to emit the read barrier.
4451 locations->SetOut(
4452 Location::RequiresRegister(),
4453 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004454 }
Roland Levillainc9285912015-12-18 10:38:42 +00004455 // We need a temporary register for the read barrier marking slow
4456 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4457 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4458 locations->AddTemp(Location::RequiresRegister());
4459 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004460}
4461
4462void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4463 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004464 Location obj_loc = locations->InAt(0);
4465 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004466 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004467 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004468 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004469 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004470 HInstruction* array_instr = instruction->GetArray();
4471 bool has_intermediate_address = array_instr->IsIntermediateAddress();
4472 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4473 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004474
Roland Levillain4d027112015-07-01 15:41:14 +01004475 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004476 case Primitive::kPrimBoolean:
4477 case Primitive::kPrimByte:
4478 case Primitive::kPrimShort:
4479 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004480 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004481 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004482 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4483 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4484
4485 LoadOperandType load_type = GetLoadOperandType(type);
4486 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004488 Register temp = IP;
4489
4490 if (has_intermediate_address) {
4491 // We do not need to compute the intermediate address from the array: the
4492 // input instruction has done it already. See the comment in
4493 // `TryExtractArrayAccessAddress()`.
4494 if (kIsDebugBuild) {
4495 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4496 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4497 }
4498 temp = obj;
4499 } else {
4500 __ add(temp, obj, ShifterOperand(data_offset));
4501 }
4502 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004503 }
4504 break;
4505 }
4506
Roland Levillainc9285912015-12-18 10:38:42 +00004507 case Primitive::kPrimNot: {
4508 static_assert(
4509 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4510 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004511 // /* HeapReference<Object> */ out =
4512 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4513 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4514 Location temp = locations->GetTemp(0);
4515 // Note that a potential implicit null check is handled in this
4516 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4517 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4518 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4519 } else {
4520 Register out = out_loc.AsRegister<Register>();
4521 if (index.IsConstant()) {
4522 size_t offset =
4523 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4524 __ LoadFromOffset(kLoadWord, out, obj, offset);
4525 codegen_->MaybeRecordImplicitNullCheck(instruction);
4526 // If read barriers are enabled, emit read barriers other than
4527 // Baker's using a slow path (and also unpoison the loaded
4528 // reference, if heap poisoning is enabled).
4529 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4530 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004531 Register temp = IP;
4532
4533 if (has_intermediate_address) {
4534 // We do not need to compute the intermediate address from the array: the
4535 // input instruction has done it already. See the comment in
4536 // `TryExtractArrayAccessAddress()`.
4537 if (kIsDebugBuild) {
4538 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4539 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4540 }
4541 temp = obj;
4542 } else {
4543 __ add(temp, obj, ShifterOperand(data_offset));
4544 }
4545 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004546
Roland Levillainc9285912015-12-18 10:38:42 +00004547 codegen_->MaybeRecordImplicitNullCheck(instruction);
4548 // If read barriers are enabled, emit read barriers other than
4549 // Baker's using a slow path (and also unpoison the loaded
4550 // reference, if heap poisoning is enabled).
4551 codegen_->MaybeGenerateReadBarrierSlow(
4552 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4553 }
4554 }
4555 break;
4556 }
4557
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004558 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004560 size_t offset =
4561 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004562 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004563 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004564 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004565 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004566 }
4567 break;
4568 }
4569
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004570 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004571 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004572 if (index.IsConstant()) {
4573 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004574 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004575 } else {
4576 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004577 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004578 }
4579 break;
4580 }
4581
4582 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004583 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004584 if (index.IsConstant()) {
4585 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004586 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004587 } else {
4588 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004589 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004590 }
4591 break;
4592 }
4593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004595 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004596 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004597 }
Roland Levillain4d027112015-07-01 15:41:14 +01004598
4599 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004600 // Potential implicit null checks, in the case of reference
4601 // arrays, are handled in the previous switch statement.
4602 } else {
4603 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004604 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605}
4606
4607void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004608 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004609
4610 bool needs_write_barrier =
4611 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004612 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4613 bool object_array_set_with_read_barrier =
4614 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004615
Nicolas Geoffray39468442014-09-02 15:17:15 +01004616 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004617 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004618 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4619 LocationSummary::kCallOnSlowPath :
4620 LocationSummary::kNoCall);
4621
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004622 locations->SetInAt(0, Location::RequiresRegister());
4623 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4624 if (Primitive::IsFloatingPointType(value_type)) {
4625 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004626 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004627 locations->SetInAt(2, Location::RequiresRegister());
4628 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004629 if (needs_write_barrier) {
4630 // Temporary registers for the write barrier.
4631 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004632 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004633 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004634}
4635
4636void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4637 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004638 Location array_loc = locations->InAt(0);
4639 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004641 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004642 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004643 bool needs_write_barrier =
4644 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004645 uint32_t data_offset =
4646 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4647 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004648 HInstruction* array_instr = instruction->GetArray();
4649 bool has_intermediate_address = array_instr->IsIntermediateAddress();
4650 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4651 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004652
4653 switch (value_type) {
4654 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004655 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004656 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004657 case Primitive::kPrimChar:
4658 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004659 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004660 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4661 uint32_t full_offset =
4662 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4663 StoreOperandType store_type = GetStoreOperandType(value_type);
4664 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004665 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004666 Register temp = IP;
4667
4668 if (has_intermediate_address) {
4669 // We do not need to compute the intermediate address from the array: the
4670 // input instruction has done it already. See the comment in
4671 // `TryExtractArrayAccessAddress()`.
4672 if (kIsDebugBuild) {
4673 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4674 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4675 }
4676 temp = array;
4677 } else {
4678 __ add(temp, array, ShifterOperand(data_offset));
4679 }
Artem Serov6c916792016-07-11 14:02:34 +01004680 codegen_->StoreToShiftedRegOffset(value_type,
4681 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004682 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004683 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004684 }
4685 break;
4686 }
4687
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004688 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004689 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004690 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4691 // See the comment in instruction_simplifier_shared.cc.
4692 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004693
4694 if (instruction->InputAt(2)->IsNullConstant()) {
4695 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004696 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004697 size_t offset =
4698 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004699 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004700 } else {
4701 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004702 __ add(IP, array, ShifterOperand(data_offset));
4703 codegen_->StoreToShiftedRegOffset(value_type,
4704 value_loc,
4705 IP,
4706 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004707 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004709 DCHECK(!needs_write_barrier);
4710 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004711 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004712 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004713
4714 DCHECK(needs_write_barrier);
4715 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4716 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4717 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4718 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4719 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4720 Label done;
4721 SlowPathCode* slow_path = nullptr;
4722
Roland Levillain3b359c72015-11-17 19:35:12 +00004723 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004724 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4725 codegen_->AddSlowPath(slow_path);
4726 if (instruction->GetValueCanBeNull()) {
4727 Label non_zero;
4728 __ CompareAndBranchIfNonZero(value, &non_zero);
4729 if (index.IsConstant()) {
4730 size_t offset =
4731 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4732 __ StoreToOffset(kStoreWord, value, array, offset);
4733 } else {
4734 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004735 __ add(IP, array, ShifterOperand(data_offset));
4736 codegen_->StoreToShiftedRegOffset(value_type,
4737 value_loc,
4738 IP,
4739 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004740 }
4741 codegen_->MaybeRecordImplicitNullCheck(instruction);
4742 __ b(&done);
4743 __ Bind(&non_zero);
4744 }
4745
Roland Levillain3b359c72015-11-17 19:35:12 +00004746 if (kEmitCompilerReadBarrier) {
4747 // When read barriers are enabled, the type checking
4748 // instrumentation requires two read barriers:
4749 //
4750 // __ Mov(temp2, temp1);
4751 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4752 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004753 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004754 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4755 //
4756 // // /* HeapReference<Class> */ temp2 = value->klass_
4757 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004758 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004759 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4760 //
4761 // __ cmp(temp1, ShifterOperand(temp2));
4762 //
4763 // However, the second read barrier may trash `temp`, as it
4764 // is a temporary register, and as such would not be saved
4765 // along with live registers before calling the runtime (nor
4766 // restored afterwards). So in this case, we bail out and
4767 // delegate the work to the array set slow path.
4768 //
4769 // TODO: Extend the register allocator to support a new
4770 // "(locally) live temp" location so as to avoid always
4771 // going into the slow path when read barriers are enabled.
4772 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004774 // /* HeapReference<Class> */ temp1 = array->klass_
4775 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4776 codegen_->MaybeRecordImplicitNullCheck(instruction);
4777 __ MaybeUnpoisonHeapReference(temp1);
4778
4779 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4780 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4781 // /* HeapReference<Class> */ temp2 = value->klass_
4782 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4783 // If heap poisoning is enabled, no need to unpoison `temp1`
4784 // nor `temp2`, as we are comparing two poisoned references.
4785 __ cmp(temp1, ShifterOperand(temp2));
4786
4787 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4788 Label do_put;
4789 __ b(&do_put, EQ);
4790 // If heap poisoning is enabled, the `temp1` reference has
4791 // not been unpoisoned yet; unpoison it now.
4792 __ MaybeUnpoisonHeapReference(temp1);
4793
4794 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4795 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4796 // If heap poisoning is enabled, no need to unpoison
4797 // `temp1`, as we are comparing against null below.
4798 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4799 __ Bind(&do_put);
4800 } else {
4801 __ b(slow_path->GetEntryLabel(), NE);
4802 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004803 }
4804 }
4805
Artem Serov6c916792016-07-11 14:02:34 +01004806 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004807 if (kPoisonHeapReferences) {
4808 // Note that in the case where `value` is a null reference,
4809 // we do not enter this block, as a null reference does not
4810 // need poisoning.
4811 DCHECK_EQ(value_type, Primitive::kPrimNot);
4812 __ Mov(temp1, value);
4813 __ PoisonHeapReference(temp1);
4814 source = temp1;
4815 }
4816
4817 if (index.IsConstant()) {
4818 size_t offset =
4819 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4820 __ StoreToOffset(kStoreWord, source, array, offset);
4821 } else {
4822 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004823
4824 __ add(IP, array, ShifterOperand(data_offset));
4825 codegen_->StoreToShiftedRegOffset(value_type,
4826 Location::RegisterLocation(source),
4827 IP,
4828 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004829 }
4830
Roland Levillain3b359c72015-11-17 19:35:12 +00004831 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 codegen_->MaybeRecordImplicitNullCheck(instruction);
4833 }
4834
4835 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4836
4837 if (done.IsLinked()) {
4838 __ Bind(&done);
4839 }
4840
4841 if (slow_path != nullptr) {
4842 __ Bind(slow_path->GetExitLabel());
4843 }
4844
4845 break;
4846 }
4847
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004849 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004850 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004851 size_t offset =
4852 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004854 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004855 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004856 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857 }
4858 break;
4859 }
4860
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004861 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004862 Location value = locations->InAt(2);
4863 DCHECK(value.IsFpuRegister());
4864 if (index.IsConstant()) {
4865 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004866 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004867 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004868 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004869 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4870 }
4871 break;
4872 }
4873
4874 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004875 Location value = locations->InAt(2);
4876 DCHECK(value.IsFpuRegisterPair());
4877 if (index.IsConstant()) {
4878 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004879 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004880 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004881 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004882 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4883 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004884
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004885 break;
4886 }
4887
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004889 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004890 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004891 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004892
Roland Levillain80e67092016-01-08 16:04:55 +00004893 // Objects are handled in the switch.
4894 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004895 codegen_->MaybeRecordImplicitNullCheck(instruction);
4896 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004897}
4898
4899void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004900 LocationSummary* locations =
4901 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004902 locations->SetInAt(0, Location::RequiresRegister());
4903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004904}
4905
4906void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4907 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004908 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004909 Register obj = locations->InAt(0).AsRegister<Register>();
4910 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004911 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004912 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004913}
4914
Artem Serov328429f2016-07-06 16:23:04 +01004915void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4916 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4917 DCHECK(!kEmitCompilerReadBarrier);
4918 LocationSummary* locations =
4919 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4920
4921 locations->SetInAt(0, Location::RequiresRegister());
4922 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
4923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4924}
4925
4926void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
4927 LocationSummary* locations = instruction->GetLocations();
4928 Location out = locations->Out();
4929 Location first = locations->InAt(0);
4930 Location second = locations->InAt(1);
4931
4932 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4933 DCHECK(!kEmitCompilerReadBarrier);
4934
4935 if (second.IsRegister()) {
4936 __ add(out.AsRegister<Register>(),
4937 first.AsRegister<Register>(),
4938 ShifterOperand(second.AsRegister<Register>()));
4939 } else {
4940 __ AddConstant(out.AsRegister<Register>(),
4941 first.AsRegister<Register>(),
4942 second.GetConstant()->AsIntConstant()->GetValue());
4943 }
4944}
4945
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004947 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4948 ? LocationSummary::kCallOnSlowPath
4949 : LocationSummary::kNoCall;
4950 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004951 locations->SetInAt(0, Location::RequiresRegister());
4952 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004953 if (instruction->HasUses()) {
4954 locations->SetOut(Location::SameAsFirstInput());
4955 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004956}
4957
4958void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4959 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004960 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004961 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004962 codegen_->AddSlowPath(slow_path);
4963
Roland Levillain271ab9c2014-11-27 15:23:57 +00004964 Register index = locations->InAt(0).AsRegister<Register>();
4965 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004966
4967 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004968 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004969}
4970
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004971void CodeGeneratorARM::MarkGCCard(Register temp,
4972 Register card,
4973 Register object,
4974 Register value,
4975 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004976 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004977 if (can_be_null) {
4978 __ CompareAndBranchIfZero(value, &is_null);
4979 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004980 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4981 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4982 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004983 if (can_be_null) {
4984 __ Bind(&is_null);
4985 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004986}
4987
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004988void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004989 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004990}
4991
4992void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004993 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4994}
4995
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004996void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4998}
4999
5000void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005001 HBasicBlock* block = instruction->GetBlock();
5002 if (block->GetLoopInformation() != nullptr) {
5003 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5004 // The back edge will generate the suspend check.
5005 return;
5006 }
5007 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5008 // The goto will generate the suspend check.
5009 return;
5010 }
5011 GenerateSuspendCheck(instruction, nullptr);
5012}
5013
5014void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5015 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005016 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005017 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5018 if (slow_path == nullptr) {
5019 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5020 instruction->SetSlowPath(slow_path);
5021 codegen_->AddSlowPath(slow_path);
5022 if (successor != nullptr) {
5023 DCHECK(successor->IsLoopHeader());
5024 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5025 }
5026 } else {
5027 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5028 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005029
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005030 __ LoadFromOffset(
5031 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005032 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005033 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005034 __ Bind(slow_path->GetReturnLabel());
5035 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005036 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005037 __ b(slow_path->GetEntryLabel());
5038 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005039}
5040
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005041ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5042 return codegen_->GetAssembler();
5043}
5044
5045void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005046 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005047 Location source = move->GetSource();
5048 Location destination = move->GetDestination();
5049
5050 if (source.IsRegister()) {
5051 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005052 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005053 } else if (destination.IsFpuRegister()) {
5054 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005055 } else {
5056 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005057 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005058 SP, destination.GetStackIndex());
5059 }
5060 } else if (source.IsStackSlot()) {
5061 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005062 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005063 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005064 } else if (destination.IsFpuRegister()) {
5065 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005066 } else {
5067 DCHECK(destination.IsStackSlot());
5068 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5069 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5070 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005071 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005072 if (destination.IsRegister()) {
5073 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5074 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005075 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005076 } else {
5077 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005078 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5079 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005080 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005081 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005082 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5083 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005084 } else if (destination.IsRegisterPair()) {
5085 DCHECK(ExpectedPairLayout(destination));
5086 __ LoadFromOffset(
5087 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5088 } else {
5089 DCHECK(destination.IsFpuRegisterPair()) << destination;
5090 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5091 SP,
5092 source.GetStackIndex());
5093 }
5094 } else if (source.IsRegisterPair()) {
5095 if (destination.IsRegisterPair()) {
5096 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5097 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005098 } else if (destination.IsFpuRegisterPair()) {
5099 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5100 source.AsRegisterPairLow<Register>(),
5101 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005102 } else {
5103 DCHECK(destination.IsDoubleStackSlot()) << destination;
5104 DCHECK(ExpectedPairLayout(source));
5105 __ StoreToOffset(
5106 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5107 }
5108 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005109 if (destination.IsRegisterPair()) {
5110 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5111 destination.AsRegisterPairHigh<Register>(),
5112 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5113 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005114 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5115 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5116 } else {
5117 DCHECK(destination.IsDoubleStackSlot()) << destination;
5118 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5119 SP,
5120 destination.GetStackIndex());
5121 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005122 } else {
5123 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005124 HConstant* constant = source.GetConstant();
5125 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5126 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005127 if (destination.IsRegister()) {
5128 __ LoadImmediate(destination.AsRegister<Register>(), value);
5129 } else {
5130 DCHECK(destination.IsStackSlot());
5131 __ LoadImmediate(IP, value);
5132 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5133 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005134 } else if (constant->IsLongConstant()) {
5135 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005136 if (destination.IsRegisterPair()) {
5137 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5138 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005139 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005140 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005141 __ LoadImmediate(IP, Low32Bits(value));
5142 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5143 __ LoadImmediate(IP, High32Bits(value));
5144 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5145 }
5146 } else if (constant->IsDoubleConstant()) {
5147 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005148 if (destination.IsFpuRegisterPair()) {
5149 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005150 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005151 DCHECK(destination.IsDoubleStackSlot()) << destination;
5152 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005153 __ LoadImmediate(IP, Low32Bits(int_value));
5154 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5155 __ LoadImmediate(IP, High32Bits(int_value));
5156 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5157 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005158 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005159 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005160 float value = constant->AsFloatConstant()->GetValue();
5161 if (destination.IsFpuRegister()) {
5162 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5163 } else {
5164 DCHECK(destination.IsStackSlot());
5165 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5166 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5167 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005168 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005169 }
5170}
5171
5172void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5173 __ Mov(IP, reg);
5174 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5175 __ StoreToOffset(kStoreWord, IP, SP, mem);
5176}
5177
5178void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5179 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5180 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5181 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5182 SP, mem1 + stack_offset);
5183 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5184 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5185 SP, mem2 + stack_offset);
5186 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5187}
5188
5189void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005190 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005191 Location source = move->GetSource();
5192 Location destination = move->GetDestination();
5193
5194 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005195 DCHECK_NE(source.AsRegister<Register>(), IP);
5196 DCHECK_NE(destination.AsRegister<Register>(), IP);
5197 __ Mov(IP, source.AsRegister<Register>());
5198 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5199 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005200 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005201 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005202 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005203 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005204 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5205 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005206 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005207 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005208 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005209 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005210 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005211 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005212 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005213 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005214 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5215 destination.AsRegisterPairHigh<Register>(),
5216 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005217 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005218 Register low_reg = source.IsRegisterPair()
5219 ? source.AsRegisterPairLow<Register>()
5220 : destination.AsRegisterPairLow<Register>();
5221 int mem = source.IsRegisterPair()
5222 ? destination.GetStackIndex()
5223 : source.GetStackIndex();
5224 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005225 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005226 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005227 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005228 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005229 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5230 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005231 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005232 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005233 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005234 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5235 DRegister reg = source.IsFpuRegisterPair()
5236 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5237 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5238 int mem = source.IsFpuRegisterPair()
5239 ? destination.GetStackIndex()
5240 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005241 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005242 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005243 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005244 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5245 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5246 : destination.AsFpuRegister<SRegister>();
5247 int mem = source.IsFpuRegister()
5248 ? destination.GetStackIndex()
5249 : source.GetStackIndex();
5250
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005251 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005252 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005253 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005254 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005255 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5256 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005257 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005258 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005259 }
5260}
5261
5262void ParallelMoveResolverARM::SpillScratch(int reg) {
5263 __ Push(static_cast<Register>(reg));
5264}
5265
5266void ParallelMoveResolverARM::RestoreScratch(int reg) {
5267 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005268}
5269
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005270HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5271 HLoadClass::LoadKind desired_class_load_kind) {
5272 if (kEmitCompilerReadBarrier) {
5273 switch (desired_class_load_kind) {
5274 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5275 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5276 case HLoadClass::LoadKind::kBootImageAddress:
5277 // TODO: Implement for read barrier.
5278 return HLoadClass::LoadKind::kDexCacheViaMethod;
5279 default:
5280 break;
5281 }
5282 }
5283 switch (desired_class_load_kind) {
5284 case HLoadClass::LoadKind::kReferrersClass:
5285 break;
5286 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5287 DCHECK(!GetCompilerOptions().GetCompilePic());
5288 break;
5289 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5290 DCHECK(GetCompilerOptions().GetCompilePic());
5291 break;
5292 case HLoadClass::LoadKind::kBootImageAddress:
5293 break;
5294 case HLoadClass::LoadKind::kDexCacheAddress:
5295 DCHECK(Runtime::Current()->UseJitCompilation());
5296 break;
5297 case HLoadClass::LoadKind::kDexCachePcRelative:
5298 DCHECK(!Runtime::Current()->UseJitCompilation());
5299 // We disable pc-relative load when there is an irreducible loop, as the optimization
5300 // is incompatible with it.
5301 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5302 // with irreducible loops.
5303 if (GetGraph()->HasIrreducibleLoops()) {
5304 return HLoadClass::LoadKind::kDexCacheViaMethod;
5305 }
5306 break;
5307 case HLoadClass::LoadKind::kDexCacheViaMethod:
5308 break;
5309 }
5310 return desired_class_load_kind;
5311}
5312
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005313void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005314 if (cls->NeedsAccessCheck()) {
5315 InvokeRuntimeCallingConvention calling_convention;
5316 CodeGenerator::CreateLoadClassLocationSummary(
5317 cls,
5318 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5319 Location::RegisterLocation(R0),
5320 /* code_generator_supports_read_barrier */ true);
5321 return;
5322 }
5323
5324 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5325 ? LocationSummary::kCallOnSlowPath
5326 : LocationSummary::kNoCall;
5327 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5328 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5329 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5330 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5331 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5332 locations->SetInAt(0, Location::RequiresRegister());
5333 }
5334 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005335}
5336
5337void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005338 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005339 if (cls->NeedsAccessCheck()) {
5340 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5341 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5342 cls,
5343 cls->GetDexPc(),
5344 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005345 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005346 return;
5347 }
5348
Roland Levillain3b359c72015-11-17 19:35:12 +00005349 Location out_loc = locations->Out();
5350 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005351
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005352 bool generate_null_check = false;
5353 switch (cls->GetLoadKind()) {
5354 case HLoadClass::LoadKind::kReferrersClass: {
5355 DCHECK(!cls->CanCallRuntime());
5356 DCHECK(!cls->MustGenerateClinitCheck());
5357 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5358 Register current_method = locations->InAt(0).AsRegister<Register>();
5359 GenerateGcRootFieldLoad(
5360 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5361 break;
5362 }
5363 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5364 DCHECK(!kEmitCompilerReadBarrier);
5365 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5366 cls->GetTypeIndex()));
5367 break;
5368 }
5369 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5370 DCHECK(!kEmitCompilerReadBarrier);
5371 CodeGeneratorARM::PcRelativePatchInfo* labels =
5372 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5373 __ BindTrackedLabel(&labels->movw_label);
5374 __ movw(out, /* placeholder */ 0u);
5375 __ BindTrackedLabel(&labels->movt_label);
5376 __ movt(out, /* placeholder */ 0u);
5377 __ BindTrackedLabel(&labels->add_pc_label);
5378 __ add(out, out, ShifterOperand(PC));
5379 break;
5380 }
5381 case HLoadClass::LoadKind::kBootImageAddress: {
5382 DCHECK(!kEmitCompilerReadBarrier);
5383 DCHECK_NE(cls->GetAddress(), 0u);
5384 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5385 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5386 break;
5387 }
5388 case HLoadClass::LoadKind::kDexCacheAddress: {
5389 DCHECK_NE(cls->GetAddress(), 0u);
5390 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5391 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5392 // a 128B range. To try and reduce the number of literals if we load multiple types,
5393 // simply split the dex cache address to a 128B aligned base loaded from a literal
5394 // and the remaining offset embedded in the load.
5395 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5396 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5397 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5398 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5399 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5400 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5401 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5402 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5403 generate_null_check = !cls->IsInDexCache();
5404 break;
5405 }
5406 case HLoadClass::LoadKind::kDexCachePcRelative: {
5407 Register base_reg = locations->InAt(0).AsRegister<Register>();
5408 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5409 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5410 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5411 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset);
5412 generate_null_check = !cls->IsInDexCache();
5413 break;
5414 }
5415 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5416 // /* GcRoot<mirror::Class>[] */ out =
5417 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5418 Register current_method = locations->InAt(0).AsRegister<Register>();
5419 __ LoadFromOffset(kLoadWord,
5420 out,
5421 current_method,
5422 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5423 // /* GcRoot<mirror::Class> */ out = out[type_index]
5424 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5425 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5426 generate_null_check = !cls->IsInDexCache();
5427 }
5428 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005429
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005430 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5431 DCHECK(cls->CanCallRuntime());
5432 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5433 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5434 codegen_->AddSlowPath(slow_path);
5435 if (generate_null_check) {
5436 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5437 }
5438 if (cls->MustGenerateClinitCheck()) {
5439 GenerateClassInitializationCheck(slow_path, out);
5440 } else {
5441 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005442 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005443 }
5444}
5445
5446void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5447 LocationSummary* locations =
5448 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5449 locations->SetInAt(0, Location::RequiresRegister());
5450 if (check->HasUses()) {
5451 locations->SetOut(Location::SameAsFirstInput());
5452 }
5453}
5454
5455void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005456 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005457 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005458 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005459 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005460 GenerateClassInitializationCheck(slow_path,
5461 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005462}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005463
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005464void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005465 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005466 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5467 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5468 __ b(slow_path->GetEntryLabel(), LT);
5469 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5470 // properly. Therefore, we do a memory fence.
5471 __ dmb(ISH);
5472 __ Bind(slow_path->GetExitLabel());
5473}
5474
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005475HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5476 HLoadString::LoadKind desired_string_load_kind) {
5477 if (kEmitCompilerReadBarrier) {
5478 switch (desired_string_load_kind) {
5479 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5480 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5481 case HLoadString::LoadKind::kBootImageAddress:
5482 // TODO: Implement for read barrier.
5483 return HLoadString::LoadKind::kDexCacheViaMethod;
5484 default:
5485 break;
5486 }
5487 }
5488 switch (desired_string_load_kind) {
5489 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5490 DCHECK(!GetCompilerOptions().GetCompilePic());
5491 break;
5492 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5493 DCHECK(GetCompilerOptions().GetCompilePic());
5494 break;
5495 case HLoadString::LoadKind::kBootImageAddress:
5496 break;
5497 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005498 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005499 break;
5500 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005501 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005502 // We disable pc-relative load when there is an irreducible loop, as the optimization
5503 // is incompatible with it.
5504 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5505 // with irreducible loops.
5506 if (GetGraph()->HasIrreducibleLoops()) {
5507 return HLoadString::LoadKind::kDexCacheViaMethod;
5508 }
5509 break;
5510 case HLoadString::LoadKind::kDexCacheViaMethod:
5511 break;
5512 }
5513 return desired_string_load_kind;
5514}
5515
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005516void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005517 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005518 ? LocationSummary::kCallOnSlowPath
5519 : LocationSummary::kNoCall;
5520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005521 HLoadString::LoadKind load_kind = load->GetLoadKind();
5522 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5523 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5524 locations->SetInAt(0, Location::RequiresRegister());
5525 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005526 locations->SetOut(Location::RequiresRegister());
5527}
5528
5529void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005530 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005531 Location out_loc = locations->Out();
5532 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005533
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005534 switch (load->GetLoadKind()) {
5535 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5536 DCHECK(!kEmitCompilerReadBarrier);
5537 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5538 load->GetStringIndex()));
5539 return; // No dex cache slow path.
5540 }
5541 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5542 DCHECK(!kEmitCompilerReadBarrier);
5543 CodeGeneratorARM::PcRelativePatchInfo* labels =
5544 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5545 __ BindTrackedLabel(&labels->movw_label);
5546 __ movw(out, /* placeholder */ 0u);
5547 __ BindTrackedLabel(&labels->movt_label);
5548 __ movt(out, /* placeholder */ 0u);
5549 __ BindTrackedLabel(&labels->add_pc_label);
5550 __ add(out, out, ShifterOperand(PC));
5551 return; // No dex cache slow path.
5552 }
5553 case HLoadString::LoadKind::kBootImageAddress: {
5554 DCHECK(!kEmitCompilerReadBarrier);
5555 DCHECK_NE(load->GetAddress(), 0u);
5556 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5557 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5558 return; // No dex cache slow path.
5559 }
5560 case HLoadString::LoadKind::kDexCacheAddress: {
5561 DCHECK_NE(load->GetAddress(), 0u);
5562 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5563 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5564 // a 128B range. To try and reduce the number of literals if we load multiple strings,
5565 // simply split the dex cache address to a 128B aligned base loaded from a literal
5566 // and the remaining offset embedded in the load.
5567 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
5568 DCHECK_ALIGNED(load->GetAddress(), 4u);
5569 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5570 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5571 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5572 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005573 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005574 GenerateGcRootFieldLoad(load, out_loc, out, offset);
5575 break;
5576 }
5577 case HLoadString::LoadKind::kDexCachePcRelative: {
5578 Register base_reg = locations->InAt(0).AsRegister<Register>();
5579 HArmDexCacheArraysBase* base = load->InputAt(0)->AsArmDexCacheArraysBase();
5580 int32_t offset = load->GetDexCacheElementOffset() - base->GetElementOffset();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005581 // /* GcRoot<mirror::String> */ out = *(dex_cache_arrays_base + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005582 GenerateGcRootFieldLoad(load, out_loc, base_reg, offset);
5583 break;
5584 }
5585 case HLoadString::LoadKind::kDexCacheViaMethod: {
5586 Register current_method = locations->InAt(0).AsRegister<Register>();
5587
5588 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5589 GenerateGcRootFieldLoad(
5590 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5591 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5592 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
5593 // /* GcRoot<mirror::String> */ out = out[string_index]
5594 GenerateGcRootFieldLoad(
5595 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
5596 break;
5597 }
5598 default:
5599 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5600 UNREACHABLE();
5601 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005602
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005603 if (!load->IsInDexCache()) {
5604 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5605 codegen_->AddSlowPath(slow_path);
5606 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5607 __ Bind(slow_path->GetExitLabel());
5608 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005609}
5610
David Brazdilcb1c0552015-08-04 16:22:25 +01005611static int32_t GetExceptionTlsOffset() {
5612 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5613}
5614
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005615void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5616 LocationSummary* locations =
5617 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5618 locations->SetOut(Location::RequiresRegister());
5619}
5620
5621void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005622 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005623 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5624}
5625
5626void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5627 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5628}
5629
5630void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005631 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005632 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005633}
5634
5635void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5636 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005637 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005638 InvokeRuntimeCallingConvention calling_convention;
5639 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5640}
5641
5642void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5643 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005644 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005645 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005646}
5647
Roland Levillainc9285912015-12-18 10:38:42 +00005648static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5649 return kEmitCompilerReadBarrier &&
5650 (kUseBakerReadBarrier ||
5651 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5652 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5653 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5654}
5655
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005656void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005657 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005658 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5659 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005660 case TypeCheckKind::kExactCheck:
5661 case TypeCheckKind::kAbstractClassCheck:
5662 case TypeCheckKind::kClassHierarchyCheck:
5663 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005664 call_kind =
5665 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005666 break;
5667 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005668 case TypeCheckKind::kUnresolvedCheck:
5669 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005670 call_kind = LocationSummary::kCallOnSlowPath;
5671 break;
5672 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005673
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005675 locations->SetInAt(0, Location::RequiresRegister());
5676 locations->SetInAt(1, Location::RequiresRegister());
5677 // The "out" register is used as a temporary, so it overlaps with the inputs.
5678 // Note that TypeCheckSlowPathARM uses this register too.
5679 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5680 // When read barriers are enabled, we need a temporary register for
5681 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005682 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005683 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005685}
5686
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005687void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005688 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005689 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005690 Location obj_loc = locations->InAt(0);
5691 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005692 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005693 Location out_loc = locations->Out();
5694 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005695 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005696 locations->GetTemp(0) :
5697 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005698 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005699 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5700 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5701 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005702 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005703 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005704
5705 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005706 // avoid null check if we know obj is not null.
5707 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005708 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005709 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005710
Roland Levillain3b359c72015-11-17 19:35:12 +00005711 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005712 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713
Roland Levillainc9285912015-12-18 10:38:42 +00005714 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005715 case TypeCheckKind::kExactCheck: {
5716 __ cmp(out, ShifterOperand(cls));
5717 // Classes must be equal for the instanceof to succeed.
5718 __ b(&zero, NE);
5719 __ LoadImmediate(out, 1);
5720 __ b(&done);
5721 break;
5722 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005723
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724 case TypeCheckKind::kAbstractClassCheck: {
5725 // If the class is abstract, we eagerly fetch the super class of the
5726 // object to avoid doing a comparison we know will fail.
5727 Label loop;
5728 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005729 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005730 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005731 // If `out` is null, we use it for the result, and jump to `done`.
5732 __ CompareAndBranchIfZero(out, &done);
5733 __ cmp(out, ShifterOperand(cls));
5734 __ b(&loop, NE);
5735 __ LoadImmediate(out, 1);
5736 if (zero.IsLinked()) {
5737 __ b(&done);
5738 }
5739 break;
5740 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005741
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 case TypeCheckKind::kClassHierarchyCheck: {
5743 // Walk over the class hierarchy to find a match.
5744 Label loop, success;
5745 __ Bind(&loop);
5746 __ cmp(out, ShifterOperand(cls));
5747 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005748 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005749 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005750 __ CompareAndBranchIfNonZero(out, &loop);
5751 // If `out` is null, we use it for the result, and jump to `done`.
5752 __ b(&done);
5753 __ Bind(&success);
5754 __ LoadImmediate(out, 1);
5755 if (zero.IsLinked()) {
5756 __ b(&done);
5757 }
5758 break;
5759 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005760
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005762 // Do an exact check.
5763 Label exact_check;
5764 __ cmp(out, ShifterOperand(cls));
5765 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005766 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005767 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005768 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005769 // If `out` is null, we use it for the result, and jump to `done`.
5770 __ CompareAndBranchIfZero(out, &done);
5771 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5772 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5773 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005774 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 __ LoadImmediate(out, 1);
5776 __ b(&done);
5777 break;
5778 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005779
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005780 case TypeCheckKind::kArrayCheck: {
5781 __ cmp(out, ShifterOperand(cls));
5782 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005783 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5784 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005785 codegen_->AddSlowPath(slow_path);
5786 __ b(slow_path->GetEntryLabel(), NE);
5787 __ LoadImmediate(out, 1);
5788 if (zero.IsLinked()) {
5789 __ b(&done);
5790 }
5791 break;
5792 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005793
Calin Juravle98893e12015-10-02 21:05:03 +01005794 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005795 case TypeCheckKind::kInterfaceCheck: {
5796 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005797 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005798 // cases.
5799 //
5800 // We cannot directly call the InstanceofNonTrivial runtime
5801 // entry point without resorting to a type checking slow path
5802 // here (i.e. by calling InvokeRuntime directly), as it would
5803 // require to assign fixed registers for the inputs of this
5804 // HInstanceOf instruction (following the runtime calling
5805 // convention), which might be cluttered by the potential first
5806 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005807 //
5808 // TODO: Introduce a new runtime entry point taking the object
5809 // to test (instead of its class) as argument, and let it deal
5810 // with the read barrier issues. This will let us refactor this
5811 // case of the `switch` code as it was previously (with a direct
5812 // call to the runtime not using a type checking slow path).
5813 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005814 DCHECK(locations->OnlyCallsOnSlowPath());
5815 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5816 /* is_fatal */ false);
5817 codegen_->AddSlowPath(slow_path);
5818 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 if (zero.IsLinked()) {
5820 __ b(&done);
5821 }
5822 break;
5823 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005824 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005825
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005827 __ Bind(&zero);
5828 __ LoadImmediate(out, 0);
5829 }
5830
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 if (done.IsLinked()) {
5832 __ Bind(&done);
5833 }
5834
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005835 if (slow_path != nullptr) {
5836 __ Bind(slow_path->GetExitLabel());
5837 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005838}
5839
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005840void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005841 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5842 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5843
Roland Levillain3b359c72015-11-17 19:35:12 +00005844 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5845 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005846 case TypeCheckKind::kExactCheck:
5847 case TypeCheckKind::kAbstractClassCheck:
5848 case TypeCheckKind::kClassHierarchyCheck:
5849 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005850 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5851 LocationSummary::kCallOnSlowPath :
5852 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005853 break;
5854 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005855 case TypeCheckKind::kUnresolvedCheck:
5856 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005857 call_kind = LocationSummary::kCallOnSlowPath;
5858 break;
5859 }
5860
Roland Levillain3b359c72015-11-17 19:35:12 +00005861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5862 locations->SetInAt(0, Location::RequiresRegister());
5863 locations->SetInAt(1, Location::RequiresRegister());
5864 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5865 locations->AddTemp(Location::RequiresRegister());
5866 // When read barriers are enabled, we need an additional temporary
5867 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005868 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005869 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005870 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005871}
5872
5873void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005874 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005875 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005876 Location obj_loc = locations->InAt(0);
5877 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005878 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005879 Location temp_loc = locations->GetTemp(0);
5880 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005881 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005882 locations->GetTemp(1) :
5883 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005884 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5886 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5887 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005888
Roland Levillain3b359c72015-11-17 19:35:12 +00005889 bool is_type_check_slow_path_fatal =
5890 (type_check_kind == TypeCheckKind::kExactCheck ||
5891 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5892 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5893 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5894 !instruction->CanThrowIntoCatchBlock();
5895 SlowPathCode* type_check_slow_path =
5896 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5897 is_type_check_slow_path_fatal);
5898 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005899
5900 Label done;
5901 // Avoid null check if we know obj is not null.
5902 if (instruction->MustDoNullCheck()) {
5903 __ CompareAndBranchIfZero(obj, &done);
5904 }
5905
Roland Levillain3b359c72015-11-17 19:35:12 +00005906 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005907 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908
Roland Levillain3b359c72015-11-17 19:35:12 +00005909 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 case TypeCheckKind::kExactCheck:
5911 case TypeCheckKind::kArrayCheck: {
5912 __ cmp(temp, ShifterOperand(cls));
5913 // Jump to slow path for throwing the exception or doing a
5914 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005915 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 break;
5917 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005918
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005919 case TypeCheckKind::kAbstractClassCheck: {
5920 // If the class is abstract, we eagerly fetch the super class of the
5921 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005922 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005923 __ Bind(&loop);
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 // to the `compare_classes` label to compare it with the checked
5929 // class.
5930 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5931 // Otherwise, jump to the slow path to throw the exception.
5932 //
5933 // But before, move back the object's class into `temp` before
5934 // going into the slow path, as it has been overwritten in the
5935 // meantime.
5936 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005937 GenerateReferenceLoadTwoRegisters(
5938 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005939 __ b(type_check_slow_path->GetEntryLabel());
5940
5941 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005942 __ cmp(temp, ShifterOperand(cls));
5943 __ b(&loop, NE);
5944 break;
5945 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005946
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005947 case TypeCheckKind::kClassHierarchyCheck: {
5948 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005949 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 __ Bind(&loop);
5951 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005952 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005953
Roland Levillain3b359c72015-11-17 19:35:12 +00005954 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005955 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005956
5957 // If the class reference currently in `temp` is not null, jump
5958 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005960 // Otherwise, jump to the slow path to throw the exception.
5961 //
5962 // But before, move back the object's class into `temp` before
5963 // going into the slow path, as it has been overwritten in the
5964 // meantime.
5965 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005966 GenerateReferenceLoadTwoRegisters(
5967 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005968 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005969 break;
5970 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005971
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005973 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005974 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005975 __ cmp(temp, ShifterOperand(cls));
5976 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005977
5978 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005979 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005980 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005981
5982 // If the component type is not null (i.e. the object is indeed
5983 // an array), jump to label `check_non_primitive_component_type`
5984 // to further check that this component type is not a primitive
5985 // type.
5986 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5987 // Otherwise, jump to the slow path to throw the exception.
5988 //
5989 // But before, move back the object's class into `temp` before
5990 // going into the slow path, as it has been overwritten in the
5991 // meantime.
5992 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005993 GenerateReferenceLoadTwoRegisters(
5994 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005995 __ b(type_check_slow_path->GetEntryLabel());
5996
5997 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005998 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005999 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6000 __ CompareAndBranchIfZero(temp, &done);
6001 // Same comment as above regarding `temp` and the slow path.
6002 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006003 GenerateReferenceLoadTwoRegisters(
6004 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006005 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 break;
6007 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006008
Calin Juravle98893e12015-10-02 21:05:03 +01006009 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006010 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006011 // We always go into the type check slow path for the unresolved
6012 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006013 //
6014 // We cannot directly call the CheckCast runtime entry point
6015 // without resorting to a type checking slow path here (i.e. by
6016 // calling InvokeRuntime directly), as it would require to
6017 // assign fixed registers for the inputs of this HInstanceOf
6018 // instruction (following the runtime calling convention), which
6019 // might be cluttered by the potential first read barrier
6020 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006021 //
6022 // TODO: Introduce a new runtime entry point taking the object
6023 // to test (instead of its class) as argument, and let it deal
6024 // with the read barrier issues. This will let us refactor this
6025 // case of the `switch` code as it was previously (with a direct
6026 // call to the runtime not using a type checking slow path).
6027 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006028 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006029 break;
6030 }
6031 __ Bind(&done);
6032
Roland Levillain3b359c72015-11-17 19:35:12 +00006033 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006034}
6035
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006036void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6037 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006038 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006039 InvokeRuntimeCallingConvention calling_convention;
6040 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6041}
6042
6043void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6044 codegen_->InvokeRuntime(instruction->IsEnter()
6045 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
6046 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00006047 instruction->GetDexPc(),
6048 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006049 if (instruction->IsEnter()) {
6050 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6051 } else {
6052 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6053 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006054}
6055
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006056void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6057void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6058void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006059
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006060void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006061 LocationSummary* locations =
6062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6063 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6064 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006065 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006066 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006067 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006068 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006069}
6070
6071void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6072 HandleBitwiseOperation(instruction);
6073}
6074
6075void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6076 HandleBitwiseOperation(instruction);
6077}
6078
6079void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6080 HandleBitwiseOperation(instruction);
6081}
6082
Artem Serov7fc63502016-02-09 17:15:29 +00006083
6084void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6085 LocationSummary* locations =
6086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6087 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6088 || instruction->GetResultType() == Primitive::kPrimLong);
6089
6090 locations->SetInAt(0, Location::RequiresRegister());
6091 locations->SetInAt(1, Location::RequiresRegister());
6092 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6093}
6094
6095void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6096 LocationSummary* locations = instruction->GetLocations();
6097 Location first = locations->InAt(0);
6098 Location second = locations->InAt(1);
6099 Location out = locations->Out();
6100
6101 if (instruction->GetResultType() == Primitive::kPrimInt) {
6102 Register first_reg = first.AsRegister<Register>();
6103 ShifterOperand second_reg(second.AsRegister<Register>());
6104 Register out_reg = out.AsRegister<Register>();
6105
6106 switch (instruction->GetOpKind()) {
6107 case HInstruction::kAnd:
6108 __ bic(out_reg, first_reg, second_reg);
6109 break;
6110 case HInstruction::kOr:
6111 __ orn(out_reg, first_reg, second_reg);
6112 break;
6113 // There is no EON on arm.
6114 case HInstruction::kXor:
6115 default:
6116 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6117 UNREACHABLE();
6118 }
6119 return;
6120
6121 } else {
6122 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6123 Register first_low = first.AsRegisterPairLow<Register>();
6124 Register first_high = first.AsRegisterPairHigh<Register>();
6125 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6126 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6127 Register out_low = out.AsRegisterPairLow<Register>();
6128 Register out_high = out.AsRegisterPairHigh<Register>();
6129
6130 switch (instruction->GetOpKind()) {
6131 case HInstruction::kAnd:
6132 __ bic(out_low, first_low, second_low);
6133 __ bic(out_high, first_high, second_high);
6134 break;
6135 case HInstruction::kOr:
6136 __ orn(out_low, first_low, second_low);
6137 __ orn(out_high, first_high, second_high);
6138 break;
6139 // There is no EON on arm.
6140 case HInstruction::kXor:
6141 default:
6142 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6143 UNREACHABLE();
6144 }
6145 }
6146}
6147
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006148void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6149 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6150 if (value == 0xffffffffu) {
6151 if (out != first) {
6152 __ mov(out, ShifterOperand(first));
6153 }
6154 return;
6155 }
6156 if (value == 0u) {
6157 __ mov(out, ShifterOperand(0));
6158 return;
6159 }
6160 ShifterOperand so;
6161 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6162 __ and_(out, first, so);
6163 } else {
6164 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6165 __ bic(out, first, ShifterOperand(~value));
6166 }
6167}
6168
6169void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6170 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6171 if (value == 0u) {
6172 if (out != first) {
6173 __ mov(out, ShifterOperand(first));
6174 }
6175 return;
6176 }
6177 if (value == 0xffffffffu) {
6178 __ mvn(out, ShifterOperand(0));
6179 return;
6180 }
6181 ShifterOperand so;
6182 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6183 __ orr(out, first, so);
6184 } else {
6185 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6186 __ orn(out, first, ShifterOperand(~value));
6187 }
6188}
6189
6190void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6191 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6192 if (value == 0u) {
6193 if (out != first) {
6194 __ mov(out, ShifterOperand(first));
6195 }
6196 return;
6197 }
6198 __ eor(out, first, ShifterOperand(value));
6199}
6200
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006201void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6202 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006203 Location first = locations->InAt(0);
6204 Location second = locations->InAt(1);
6205 Location out = locations->Out();
6206
6207 if (second.IsConstant()) {
6208 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6209 uint32_t value_low = Low32Bits(value);
6210 if (instruction->GetResultType() == Primitive::kPrimInt) {
6211 Register first_reg = first.AsRegister<Register>();
6212 Register out_reg = out.AsRegister<Register>();
6213 if (instruction->IsAnd()) {
6214 GenerateAndConst(out_reg, first_reg, value_low);
6215 } else if (instruction->IsOr()) {
6216 GenerateOrrConst(out_reg, first_reg, value_low);
6217 } else {
6218 DCHECK(instruction->IsXor());
6219 GenerateEorConst(out_reg, first_reg, value_low);
6220 }
6221 } else {
6222 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6223 uint32_t value_high = High32Bits(value);
6224 Register first_low = first.AsRegisterPairLow<Register>();
6225 Register first_high = first.AsRegisterPairHigh<Register>();
6226 Register out_low = out.AsRegisterPairLow<Register>();
6227 Register out_high = out.AsRegisterPairHigh<Register>();
6228 if (instruction->IsAnd()) {
6229 GenerateAndConst(out_low, first_low, value_low);
6230 GenerateAndConst(out_high, first_high, value_high);
6231 } else if (instruction->IsOr()) {
6232 GenerateOrrConst(out_low, first_low, value_low);
6233 GenerateOrrConst(out_high, first_high, value_high);
6234 } else {
6235 DCHECK(instruction->IsXor());
6236 GenerateEorConst(out_low, first_low, value_low);
6237 GenerateEorConst(out_high, first_high, value_high);
6238 }
6239 }
6240 return;
6241 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006242
6243 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006244 Register first_reg = first.AsRegister<Register>();
6245 ShifterOperand second_reg(second.AsRegister<Register>());
6246 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006247 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006248 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006249 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006250 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006251 } else {
6252 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006253 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006254 }
6255 } else {
6256 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006257 Register first_low = first.AsRegisterPairLow<Register>();
6258 Register first_high = first.AsRegisterPairHigh<Register>();
6259 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6260 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6261 Register out_low = out.AsRegisterPairLow<Register>();
6262 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006263 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006264 __ and_(out_low, first_low, second_low);
6265 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006266 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006267 __ orr(out_low, first_low, second_low);
6268 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006269 } else {
6270 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006271 __ eor(out_low, first_low, second_low);
6272 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006273 }
6274 }
6275}
6276
Roland Levillainc9285912015-12-18 10:38:42 +00006277void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6278 Location out,
6279 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006280 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006281 Register out_reg = out.AsRegister<Register>();
6282 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006283 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006284 if (kUseBakerReadBarrier) {
6285 // Load with fast path based Baker's read barrier.
6286 // /* HeapReference<Object> */ out = *(out + offset)
6287 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006288 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006289 } else {
6290 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006291 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006292 // in the following move operation, as we will need it for the
6293 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006294 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006295 // /* HeapReference<Object> */ out = *(out + offset)
6296 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006297 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006298 }
6299 } else {
6300 // Plain load with no read barrier.
6301 // /* HeapReference<Object> */ out = *(out + offset)
6302 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6303 __ MaybeUnpoisonHeapReference(out_reg);
6304 }
6305}
6306
6307void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6308 Location out,
6309 Location obj,
6310 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006311 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006312 Register out_reg = out.AsRegister<Register>();
6313 Register obj_reg = obj.AsRegister<Register>();
6314 if (kEmitCompilerReadBarrier) {
6315 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006316 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006317 // Load with fast path based Baker's read barrier.
6318 // /* HeapReference<Object> */ out = *(obj + offset)
6319 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006320 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006321 } else {
6322 // Load with slow path based read barrier.
6323 // /* HeapReference<Object> */ out = *(obj + offset)
6324 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6325 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6326 }
6327 } else {
6328 // Plain load with no read barrier.
6329 // /* HeapReference<Object> */ out = *(obj + offset)
6330 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6331 __ MaybeUnpoisonHeapReference(out_reg);
6332 }
6333}
6334
6335void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6336 Location root,
6337 Register obj,
6338 uint32_t offset) {
6339 Register root_reg = root.AsRegister<Register>();
6340 if (kEmitCompilerReadBarrier) {
6341 if (kUseBakerReadBarrier) {
6342 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6343 // Baker's read barrier are used:
6344 //
6345 // root = obj.field;
6346 // if (Thread::Current()->GetIsGcMarking()) {
6347 // root = ReadBarrier::Mark(root)
6348 // }
6349
6350 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6351 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6352 static_assert(
6353 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6354 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6355 "have different sizes.");
6356 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6357 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6358 "have different sizes.");
6359
6360 // Slow path used to mark the GC root `root`.
6361 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006362 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006363 codegen_->AddSlowPath(slow_path);
6364
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006365 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006366 __ LoadFromOffset(
6367 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6368 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6369 __ Bind(slow_path->GetExitLabel());
6370 } else {
6371 // GC root loaded through a slow path for read barriers other
6372 // than Baker's.
6373 // /* GcRoot<mirror::Object>* */ root = obj + offset
6374 __ AddConstant(root_reg, obj, offset);
6375 // /* mirror::Object* */ root = root->Read()
6376 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6377 }
6378 } else {
6379 // Plain GC root load with no read barrier.
6380 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6381 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6382 // Note that GC roots are not affected by heap poisoning, thus we
6383 // do not have to unpoison `root_reg` here.
6384 }
6385}
6386
6387void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6388 Location ref,
6389 Register obj,
6390 uint32_t offset,
6391 Location temp,
6392 bool needs_null_check) {
6393 DCHECK(kEmitCompilerReadBarrier);
6394 DCHECK(kUseBakerReadBarrier);
6395
6396 // /* HeapReference<Object> */ ref = *(obj + offset)
6397 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006398 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006399 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006400 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006401}
6402
6403void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6404 Location ref,
6405 Register obj,
6406 uint32_t data_offset,
6407 Location index,
6408 Location temp,
6409 bool needs_null_check) {
6410 DCHECK(kEmitCompilerReadBarrier);
6411 DCHECK(kUseBakerReadBarrier);
6412
Roland Levillainbfea3352016-06-23 13:48:47 +01006413 static_assert(
6414 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6415 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006416 // /* HeapReference<Object> */ ref =
6417 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006418 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006419 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006420 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006421}
6422
6423void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6424 Location ref,
6425 Register obj,
6426 uint32_t offset,
6427 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006428 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006429 Location temp,
6430 bool needs_null_check) {
6431 DCHECK(kEmitCompilerReadBarrier);
6432 DCHECK(kUseBakerReadBarrier);
6433
6434 // In slow path based read barriers, the read barrier call is
6435 // inserted after the original load. However, in fast path based
6436 // Baker's read barriers, we need to perform the load of
6437 // mirror::Object::monitor_ *before* the original reference load.
6438 // This load-load ordering is required by the read barrier.
6439 // The fast path/slow path (for Baker's algorithm) should look like:
6440 //
6441 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6442 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6443 // HeapReference<Object> ref = *src; // Original reference load.
6444 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6445 // if (is_gray) {
6446 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6447 // }
6448 //
6449 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006450 // slightly more complex as it performs additional checks that we do
6451 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006452
6453 Register ref_reg = ref.AsRegister<Register>();
6454 Register temp_reg = temp.AsRegister<Register>();
6455 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6456
6457 // /* int32_t */ monitor = obj->monitor_
6458 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6459 if (needs_null_check) {
6460 MaybeRecordImplicitNullCheck(instruction);
6461 }
6462 // /* LockWord */ lock_word = LockWord(monitor)
6463 static_assert(sizeof(LockWord) == sizeof(int32_t),
6464 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006465
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006466 // Introduce a dependency on the lock_word including the rb_state,
6467 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006468 // a memory barrier (which would be more expensive).
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006469 // obj is unchanged by this operation, but its value now depends on temp_reg.
6470 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006471
6472 // The actual reference load.
6473 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006474 // Load types involving an "index": ArrayGet and
6475 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6476 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006477 if (index.IsConstant()) {
6478 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006479 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006480 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6481 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006482 // Handle the special case of the
6483 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6484 // a register pair as index ("long offset"), of which only the low
6485 // part contains data.
6486 Register index_reg = index.IsRegisterPair()
6487 ? index.AsRegisterPairLow<Register>()
6488 : index.AsRegister<Register>();
6489 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006490 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6491 }
6492 } else {
6493 // /* HeapReference<Object> */ ref = *(obj + offset)
6494 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6495 }
6496
6497 // Object* ref = ref_addr->AsMirrorPtr()
6498 __ MaybeUnpoisonHeapReference(ref_reg);
6499
6500 // Slow path used to mark the object `ref` when it is gray.
6501 SlowPathCode* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006502 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006503 AddSlowPath(slow_path);
6504
6505 // if (rb_state == ReadBarrier::gray_ptr_)
6506 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006507 // Given the numeric representation, it's enough to check the low bit of the
6508 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6509 // which can be a 16-bit instruction unlike the TST immediate.
6510 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6511 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6512 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6513 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6514 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006515 __ Bind(slow_path->GetExitLabel());
6516}
6517
6518void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6519 Location out,
6520 Location ref,
6521 Location obj,
6522 uint32_t offset,
6523 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006524 DCHECK(kEmitCompilerReadBarrier);
6525
Roland Levillainc9285912015-12-18 10:38:42 +00006526 // Insert a slow path based read barrier *after* the reference load.
6527 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006528 // If heap poisoning is enabled, the unpoisoning of the loaded
6529 // reference will be carried out by the runtime within the slow
6530 // path.
6531 //
6532 // Note that `ref` currently does not get unpoisoned (when heap
6533 // poisoning is enabled), which is alright as the `ref` argument is
6534 // not used by the artReadBarrierSlow entry point.
6535 //
6536 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6537 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6538 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6539 AddSlowPath(slow_path);
6540
Roland Levillain3b359c72015-11-17 19:35:12 +00006541 __ b(slow_path->GetEntryLabel());
6542 __ Bind(slow_path->GetExitLabel());
6543}
6544
Roland Levillainc9285912015-12-18 10:38:42 +00006545void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6546 Location out,
6547 Location ref,
6548 Location obj,
6549 uint32_t offset,
6550 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006551 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006552 // Baker's read barriers shall be handled by the fast path
6553 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6554 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006555 // If heap poisoning is enabled, unpoisoning will be taken care of
6556 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006557 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006558 } else if (kPoisonHeapReferences) {
6559 __ UnpoisonHeapReference(out.AsRegister<Register>());
6560 }
6561}
6562
Roland Levillainc9285912015-12-18 10:38:42 +00006563void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6564 Location out,
6565 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006566 DCHECK(kEmitCompilerReadBarrier);
6567
Roland Levillainc9285912015-12-18 10:38:42 +00006568 // Insert a slow path based read barrier *after* the GC root load.
6569 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006570 // Note that GC roots are not affected by heap poisoning, so we do
6571 // not need to do anything special for this here.
6572 SlowPathCode* slow_path =
6573 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6574 AddSlowPath(slow_path);
6575
Roland Levillain3b359c72015-11-17 19:35:12 +00006576 __ b(slow_path->GetEntryLabel());
6577 __ Bind(slow_path->GetExitLabel());
6578}
6579
Vladimir Markodc151b22015-10-15 18:02:30 +01006580HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6581 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6582 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006583 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6584 // We disable pc-relative load when there is an irreducible loop, as the optimization
6585 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006586 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6587 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006588 if (GetGraph()->HasIrreducibleLoops() &&
6589 (dispatch_info.method_load_kind ==
6590 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6591 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6592 }
6593
6594 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006595 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6596 if (&outer_dex_file != target_method.dex_file) {
6597 // Calls across dex files are more likely to exceed the available BL range,
6598 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6599 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6600 (desired_dispatch_info.method_load_kind ==
6601 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6602 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6603 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6604 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006605 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006606 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006607 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006608 0u
6609 };
6610 }
6611 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006612 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006613}
6614
Vladimir Markob4536b72015-11-24 13:45:23 +00006615Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6616 Register temp) {
6617 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6618 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6619 if (!invoke->GetLocations()->Intrinsified()) {
6620 return location.AsRegister<Register>();
6621 }
6622 // For intrinsics we allow any location, so it may be on the stack.
6623 if (!location.IsRegister()) {
6624 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6625 return temp;
6626 }
6627 // For register locations, check if the register was saved. If so, get it from the stack.
6628 // Note: There is a chance that the register was saved but not overwritten, so we could
6629 // save one load. However, since this is just an intrinsic slow path we prefer this
6630 // simple and more robust approach rather that trying to determine if that's the case.
6631 SlowPathCode* slow_path = GetCurrentSlowPath();
6632 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6633 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6634 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6635 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6636 return temp;
6637 }
6638 return location.AsRegister<Register>();
6639}
6640
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006641void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006642 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006643 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006644 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6645 // LR = code address from literal pool with link-time patch.
6646 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006647 break;
6648 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6649 // LR = invoke->GetDirectCodePtr();
6650 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006651 break;
6652 default:
6653 break;
6654 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006655
Vladimir Marko58155012015-08-19 12:49:41 +00006656 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6657 switch (invoke->GetMethodLoadKind()) {
6658 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6659 // temp = thread->string_init_entrypoint
6660 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6661 break;
6662 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006663 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006664 break;
6665 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6666 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6667 break;
6668 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6669 __ LoadLiteral(temp.AsRegister<Register>(),
6670 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6671 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006672 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6673 HArmDexCacheArraysBase* base =
6674 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6675 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6676 temp.AsRegister<Register>());
6677 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6678 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6679 break;
6680 }
Vladimir Marko58155012015-08-19 12:49:41 +00006681 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006682 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006683 Register method_reg;
6684 Register reg = temp.AsRegister<Register>();
6685 if (current_method.IsRegister()) {
6686 method_reg = current_method.AsRegister<Register>();
6687 } else {
6688 DCHECK(invoke->GetLocations()->Intrinsified());
6689 DCHECK(!current_method.IsValid());
6690 method_reg = reg;
6691 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6692 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006693 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6694 __ LoadFromOffset(kLoadWord,
6695 reg,
6696 method_reg,
6697 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006698 // temp = temp[index_in_cache];
6699 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6700 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006701 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6702 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006703 }
Vladimir Marko58155012015-08-19 12:49:41 +00006704 }
6705
6706 switch (invoke->GetCodePtrLocation()) {
6707 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6708 __ bl(GetFrameEntryLabel());
6709 break;
6710 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006711 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006712 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006713 // Arbitrarily branch to the BL itself, override at link time.
6714 __ bl(&relative_call_patches_.back().label);
6715 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006716 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6717 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6718 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006719 // LR()
6720 __ blx(LR);
6721 break;
6722 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6723 // LR = callee_method->entry_point_from_quick_compiled_code_
6724 __ LoadFromOffset(
6725 kLoadWord, LR, callee_method.AsRegister<Register>(),
6726 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6727 // LR()
6728 __ blx(LR);
6729 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006730 }
6731
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006732 DCHECK(!IsLeafMethod());
6733}
6734
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006735void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6736 Register temp = temp_location.AsRegister<Register>();
6737 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6738 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006739
6740 // Use the calling convention instead of the location of the receiver, as
6741 // intrinsics may have put the receiver in a different register. In the intrinsics
6742 // slow path, the arguments have been moved to the right place, so here we are
6743 // guaranteed that the receiver is the first register of the calling convention.
6744 InvokeDexCallingConvention calling_convention;
6745 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006746 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006747 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006748 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006749 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006750 // Instead of simply (possibly) unpoisoning `temp` here, we should
6751 // emit a read barrier for the previous class reference load.
6752 // However this is not required in practice, as this is an
6753 // intermediate/temporary reference and because the current
6754 // concurrent copying collector keeps the from-space memory
6755 // intact/accessible until the end of the marking phase (the
6756 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006757 __ MaybeUnpoisonHeapReference(temp);
6758 // temp = temp->GetMethodAt(method_offset);
6759 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6760 kArmWordSize).Int32Value();
6761 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6762 // LR = temp->GetEntryPoint();
6763 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6764 // LR();
6765 __ blx(LR);
6766}
6767
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006768CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6769 const DexFile& dex_file, uint32_t string_index) {
6770 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6771}
6772
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006773CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6774 const DexFile& dex_file, uint32_t type_index) {
6775 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6776}
6777
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006778CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6779 const DexFile& dex_file, uint32_t element_offset) {
6780 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6781}
6782
6783CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6784 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6785 patches->emplace_back(dex_file, offset_or_index);
6786 return &patches->back();
6787}
6788
6789Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6790 uint32_t string_index) {
6791 return boot_image_string_patches_.GetOrCreate(
6792 StringReference(&dex_file, string_index),
6793 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6794}
6795
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006796Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6797 uint32_t type_index) {
6798 return boot_image_type_patches_.GetOrCreate(
6799 TypeReference(&dex_file, type_index),
6800 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6801}
6802
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006803Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6804 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6805 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6806 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6807}
6808
6809Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6810 return DeduplicateUint32Literal(address, &uint32_literals_);
6811}
6812
Vladimir Marko58155012015-08-19 12:49:41 +00006813void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6814 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006815 size_t size =
6816 method_patches_.size() +
6817 call_patches_.size() +
6818 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006819 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6820 boot_image_string_patches_.size() +
6821 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006822 boot_image_type_patches_.size() +
6823 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006824 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006825 linker_patches->reserve(size);
6826 for (const auto& entry : method_patches_) {
6827 const MethodReference& target_method = entry.first;
6828 Literal* literal = entry.second;
6829 DCHECK(literal->GetLabel()->IsBound());
6830 uint32_t literal_offset = literal->GetLabel()->Position();
6831 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6832 target_method.dex_file,
6833 target_method.dex_method_index));
6834 }
6835 for (const auto& entry : call_patches_) {
6836 const MethodReference& target_method = entry.first;
6837 Literal* literal = entry.second;
6838 DCHECK(literal->GetLabel()->IsBound());
6839 uint32_t literal_offset = literal->GetLabel()->Position();
6840 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6841 target_method.dex_file,
6842 target_method.dex_method_index));
6843 }
6844 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6845 uint32_t literal_offset = info.label.Position();
6846 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6847 info.target_method.dex_file,
6848 info.target_method.dex_method_index));
6849 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006850 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6851 const DexFile& dex_file = info.target_dex_file;
6852 size_t base_element_offset = info.offset_or_index;
6853 DCHECK(info.add_pc_label.IsBound());
6854 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006855 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006856 DCHECK(info.movw_label.IsBound());
6857 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006858 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6859 &dex_file,
6860 add_pc_offset,
6861 base_element_offset));
6862 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006863 DCHECK(info.movt_label.IsBound());
6864 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006865 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6866 &dex_file,
6867 add_pc_offset,
6868 base_element_offset));
6869 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006870 for (const auto& entry : boot_image_string_patches_) {
6871 const StringReference& target_string = entry.first;
6872 Literal* literal = entry.second;
6873 DCHECK(literal->GetLabel()->IsBound());
6874 uint32_t literal_offset = literal->GetLabel()->Position();
6875 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6876 target_string.dex_file,
6877 target_string.string_index));
6878 }
6879 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6880 const DexFile& dex_file = info.target_dex_file;
6881 uint32_t string_index = info.offset_or_index;
6882 DCHECK(info.add_pc_label.IsBound());
6883 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6884 // Add MOVW patch.
6885 DCHECK(info.movw_label.IsBound());
6886 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6887 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6888 &dex_file,
6889 add_pc_offset,
6890 string_index));
6891 // Add MOVT patch.
6892 DCHECK(info.movt_label.IsBound());
6893 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6894 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6895 &dex_file,
6896 add_pc_offset,
6897 string_index));
6898 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006899 for (const auto& entry : boot_image_type_patches_) {
6900 const TypeReference& target_type = entry.first;
6901 Literal* literal = entry.second;
6902 DCHECK(literal->GetLabel()->IsBound());
6903 uint32_t literal_offset = literal->GetLabel()->Position();
6904 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
6905 target_type.dex_file,
6906 target_type.type_index));
6907 }
6908 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
6909 const DexFile& dex_file = info.target_dex_file;
6910 uint32_t type_index = info.offset_or_index;
6911 DCHECK(info.add_pc_label.IsBound());
6912 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6913 // Add MOVW patch.
6914 DCHECK(info.movw_label.IsBound());
6915 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6916 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
6917 &dex_file,
6918 add_pc_offset,
6919 type_index));
6920 // Add MOVT patch.
6921 DCHECK(info.movt_label.IsBound());
6922 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6923 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
6924 &dex_file,
6925 add_pc_offset,
6926 type_index));
6927 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006928 for (const auto& entry : boot_image_address_patches_) {
6929 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6930 Literal* literal = entry.second;
6931 DCHECK(literal->GetLabel()->IsBound());
6932 uint32_t literal_offset = literal->GetLabel()->Position();
6933 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6934 }
6935}
6936
6937Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6938 return map->GetOrCreate(
6939 value,
6940 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006941}
6942
6943Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6944 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006945 return map->GetOrCreate(
6946 target_method,
6947 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006948}
6949
6950Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6951 return DeduplicateMethodLiteral(target_method, &method_patches_);
6952}
6953
6954Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6955 return DeduplicateMethodLiteral(target_method, &call_patches_);
6956}
6957
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006958void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6959 LocationSummary* locations =
6960 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6961 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6962 Location::RequiresRegister());
6963 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6964 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6966}
6967
6968void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6969 LocationSummary* locations = instr->GetLocations();
6970 Register res = locations->Out().AsRegister<Register>();
6971 Register accumulator =
6972 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6973 Register mul_left =
6974 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6975 Register mul_right =
6976 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6977
6978 if (instr->GetOpKind() == HInstruction::kAdd) {
6979 __ mla(res, mul_left, mul_right, accumulator);
6980 } else {
6981 __ mls(res, mul_left, mul_right, accumulator);
6982 }
6983}
6984
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006985void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006986 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006987 LOG(FATAL) << "Unreachable";
6988}
6989
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006990void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006991 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006992 LOG(FATAL) << "Unreachable";
6993}
6994
Mark Mendellfe57faa2015-09-18 09:26:15 -04006995// Simple implementation of packed switch - generate cascaded compare/jumps.
6996void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6997 LocationSummary* locations =
6998 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6999 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007000 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007001 codegen_->GetAssembler()->IsThumb()) {
7002 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7003 if (switch_instr->GetStartValue() != 0) {
7004 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7005 }
7006 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007007}
7008
7009void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7010 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007011 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007012 LocationSummary* locations = switch_instr->GetLocations();
7013 Register value_reg = locations->InAt(0).AsRegister<Register>();
7014 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7015
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007016 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007017 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007018 Register temp_reg = IP;
7019 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7020 // the immediate, because IP is used as the destination register. For the other
7021 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7022 // and they can be encoded in the instruction without making use of IP register.
7023 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7024
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007025 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007026 // Jump to successors[0] if value == lower_bound.
7027 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7028 int32_t last_index = 0;
7029 for (; num_entries - last_index > 2; last_index += 2) {
7030 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7031 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7032 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7033 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7034 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7035 }
7036 if (num_entries - last_index == 2) {
7037 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007038 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007039 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007040 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007041
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007042 // And the default for any other value.
7043 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7044 __ b(codegen_->GetLabelOf(default_block));
7045 }
7046 } else {
7047 // Create a table lookup.
7048 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7049
7050 // Materialize a pointer to the switch table
7051 std::vector<Label*> labels(num_entries);
7052 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7053 for (uint32_t i = 0; i < num_entries; i++) {
7054 labels[i] = codegen_->GetLabelOf(successors[i]);
7055 }
7056 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7057
7058 // Remove the bias.
7059 Register key_reg;
7060 if (lower_bound != 0) {
7061 key_reg = locations->GetTemp(1).AsRegister<Register>();
7062 __ AddConstant(key_reg, value_reg, -lower_bound);
7063 } else {
7064 key_reg = value_reg;
7065 }
7066
7067 // Check whether the value is in the table, jump to default block if not.
7068 __ CmpConstant(key_reg, num_entries - 1);
7069 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7070
7071 // Load the displacement from the table.
7072 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7073
7074 // Dispatch is a direct add to the PC (for Thumb2).
7075 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007076 }
7077}
7078
Vladimir Markob4536b72015-11-24 13:45:23 +00007079void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7081 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007082}
7083
7084void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7085 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007086 CodeGeneratorARM::PcRelativePatchInfo* labels =
7087 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007088 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007089 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007090 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007091 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007092 __ BindTrackedLabel(&labels->add_pc_label);
7093 __ add(base_reg, base_reg, ShifterOperand(PC));
7094}
7095
Andreas Gampe85b62f22015-09-09 13:15:38 -07007096void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7097 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007098 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007099 return;
7100 }
7101
7102 DCHECK_NE(type, Primitive::kPrimVoid);
7103
7104 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7105 if (return_loc.Equals(trg)) {
7106 return;
7107 }
7108
7109 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7110 // with the last branch.
7111 if (type == Primitive::kPrimLong) {
7112 HParallelMove parallel_move(GetGraph()->GetArena());
7113 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7114 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7115 GetMoveResolver()->EmitNativeCode(&parallel_move);
7116 } else if (type == Primitive::kPrimDouble) {
7117 HParallelMove parallel_move(GetGraph()->GetArena());
7118 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7119 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7120 GetMoveResolver()->EmitNativeCode(&parallel_move);
7121 } else {
7122 // Let the parallel move resolver take care of all of this.
7123 HParallelMove parallel_move(GetGraph()->GetArena());
7124 parallel_move.AddMove(return_loc, trg, type, nullptr);
7125 GetMoveResolver()->EmitNativeCode(&parallel_move);
7126 }
7127}
7128
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007129void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7130 LocationSummary* locations =
7131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7132 locations->SetInAt(0, Location::RequiresRegister());
7133 locations->SetOut(Location::RequiresRegister());
7134}
7135
7136void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7137 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007138 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007139 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007140 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007141 __ LoadFromOffset(kLoadWord,
7142 locations->Out().AsRegister<Register>(),
7143 locations->InAt(0).AsRegister<Register>(),
7144 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007145 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007146 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007147 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007148 __ LoadFromOffset(kLoadWord,
7149 locations->Out().AsRegister<Register>(),
7150 locations->InAt(0).AsRegister<Register>(),
7151 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7152 __ LoadFromOffset(kLoadWord,
7153 locations->Out().AsRegister<Register>(),
7154 locations->Out().AsRegister<Register>(),
7155 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007156 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007157}
7158
Roland Levillain4d027112015-07-01 15:41:14 +01007159#undef __
7160#undef QUICK_ENTRY_POINT
7161
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007162} // namespace arm
7163} // namespace art