blob: e629a8e7a54665fcccfcd42a95c09852b9e66bcb [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070062// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Andreas Gampe85b62f22015-09-09 13:15:38 -070066class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000068 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069
Alexandre Rames67555f72014-11-18 10:55:16 +000070 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010071 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000073 if (instruction_->CanThrowIntoCatchBlock()) {
74 // Live registers will be restored in the catch block if caught.
75 SaveLiveRegisters(codegen, instruction_->GetLocations());
76 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010077 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000078 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000079 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 }
81
Alexandre Rames8158f282015-08-07 10:26:17 +010082 bool IsFatal() const OVERRIDE { return true; }
83
Alexandre Rames9931f312015-06-19 14:47:01 +010084 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
85
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
88};
89
Andreas Gampe85b62f22015-09-09 13:15:38 -070090class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000091 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000092 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000093
Alexandre Rames67555f72014-11-18 10:55:16 +000094 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000095 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
96 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000097 if (instruction_->CanThrowIntoCatchBlock()) {
98 // Live registers will be restored in the catch block if caught.
99 SaveLiveRegisters(codegen, instruction_->GetLocations());
100 }
Calin Juravled0d48522014-11-04 16:40:20 +0000101 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000102 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000103 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000104 }
105
Alexandre Rames8158f282015-08-07 10:26:17 +0100106 bool IsFatal() const OVERRIDE { return true; }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000111 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
112};
113
Andreas Gampe85b62f22015-09-09 13:15:38 -0700114class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000115 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000116 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000117 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118
Alexandre Rames67555f72014-11-18 10:55:16 +0000119 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100120 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000122 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100123 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000124 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000125 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000126 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100127 if (successor_ == nullptr) {
128 __ b(GetReturnLabel());
129 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 }
133
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 Label* GetReturnLabel() {
135 DCHECK(successor_ == nullptr);
136 return &return_label_;
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100139 HBasicBlock* GetSuccessor() const {
140 return successor_;
141 }
142
Alexandre Rames9931f312015-06-19 14:47:01 +0100143 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
144
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000145 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100146 // If not null, the block to branch to after the suspend check.
147 HBasicBlock* const successor_;
148
149 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 Label return_label_;
151
152 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
153};
154
Andreas Gampe85b62f22015-09-09 13:15:38 -0700155class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100157 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000158 : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159
Alexandre Rames67555f72014-11-18 10:55:16 +0000160 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100161 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100162 LocationSummary* locations = instruction_->GetLocations();
163
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000165 if (instruction_->CanThrowIntoCatchBlock()) {
166 // Live registers will be restored in the catch block if caught.
167 SaveLiveRegisters(codegen, instruction_->GetLocations());
168 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000169 // We're moving two locations to locations that could overlap, so we need a parallel
170 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100171 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000172 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100173 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100175 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100176 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100177 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
178 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100179 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
180 ? QUICK_ENTRY_POINT(pThrowStringBounds)
181 : QUICK_ENTRY_POINT(pThrowArrayBounds);
182 arm_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
183 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000184 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 }
186
Alexandre Rames8158f282015-08-07 10:26:17 +0100187 bool IsFatal() const OVERRIDE { return true; }
188
Alexandre Rames9931f312015-06-19 14:47:01 +0100189 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
190
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
193};
194
Andreas Gampe85b62f22015-09-09 13:15:38 -0700195class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100196 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000197 LoadClassSlowPathARM(HLoadClass* cls,
198 HInstruction* at,
199 uint32_t dex_pc,
200 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
203 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204
Alexandre Rames67555f72014-11-18 10:55:16 +0000205 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LocationSummary* locations = at_->GetLocations();
207
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100208 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
209 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000210 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 int32_t entry_point_offset = do_clinit_
215 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
216 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000217 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000218 if (do_clinit_) {
219 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
220 } else {
221 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
222 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223
224 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000225 Location out = locations->Out();
226 if (out.IsValid()) {
227 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
229 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231 __ b(GetExitLabel());
232 }
233
Alexandre Rames9931f312015-06-19 14:47:01 +0100234 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
235
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100236 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 // The class this slow path will load.
238 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 // The instruction where this slow path is happening.
241 // (Might be the load class or an initialization check).
242 HInstruction* const at_;
243
244 // The dex PC of `at_`.
245 const uint32_t dex_pc_;
246
247 // Whether to initialize the class.
248 const bool do_clinit_;
249
250 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100251};
252
Andreas Gampe85b62f22015-09-09 13:15:38 -0700253class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000255 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256
Alexandre Rames67555f72014-11-18 10:55:16 +0000257 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258 LocationSummary* locations = instruction_->GetLocations();
259 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
260
261 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
262 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000263 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000264
265 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000266 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
267 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000268 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000270 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
272
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 __ b(GetExitLabel());
275 }
276
Alexandre Rames9931f312015-06-19 14:47:01 +0100277 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
278
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000279 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000280 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
281};
282
Andreas Gampe85b62f22015-09-09 13:15:38 -0700283class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000285 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000286 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
Alexandre Rames67555f72014-11-18 10:55:16 +0000288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100290 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
291 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 DCHECK(instruction_->IsCheckCast()
293 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
295 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
296 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000297
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000298 if (!is_fatal_) {
299 SaveLiveRegisters(codegen, locations);
300 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 // We're moving two locations to locations that could overlap, so we need a parallel
303 // move resolver.
304 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100309 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100310 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
311 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100314 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
315 instruction_,
316 instruction_->GetDexPc(),
317 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000318 CheckEntrypointTypes<
319 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
321 } else {
322 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
324 instruction_,
325 instruction_->GetDexPc(),
326 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000327 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000330 if (!is_fatal_) {
331 RestoreLiveRegisters(codegen, locations);
332 __ b(GetExitLabel());
333 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334 }
335
Alexandre Rames9931f312015-06-19 14:47:01 +0100336 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
337
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000338 bool IsFatal() const OVERRIDE { return is_fatal_; }
339
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000341 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342
343 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
344};
345
Andreas Gampe85b62f22015-09-09 13:15:38 -0700346class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700347 public:
Aart Bik42249c32016-01-07 15:33:50 -0800348 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800352 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800355 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
356 instruction_,
357 instruction_->GetDexPc(),
358 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000359 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700360 }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
363
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700364 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
366};
367
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100368class ArraySetSlowPathARM : public SlowPathCode {
369 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000370 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100371
372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
373 LocationSummary* locations = instruction_->GetLocations();
374 __ Bind(GetEntryLabel());
375 SaveLiveRegisters(codegen, locations);
376
377 InvokeRuntimeCallingConvention calling_convention;
378 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
379 parallel_move.AddMove(
380 locations->InAt(0),
381 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
382 Primitive::kPrimNot,
383 nullptr);
384 parallel_move.AddMove(
385 locations->InAt(1),
386 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
387 Primitive::kPrimInt,
388 nullptr);
389 parallel_move.AddMove(
390 locations->InAt(2),
391 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
392 Primitive::kPrimNot,
393 nullptr);
394 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
395
396 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
397 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
398 instruction_,
399 instruction_->GetDexPc(),
400 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402 RestoreLiveRegisters(codegen, locations);
403 __ b(GetExitLabel());
404 }
405
406 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
407
408 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100409 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
410};
411
Roland Levillainc9285912015-12-18 10:38:42 +0000412// Slow path marking an object during a read barrier.
413class ReadBarrierMarkSlowPathARM : public SlowPathCode {
414 public:
415 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000417 DCHECK(kEmitCompilerReadBarrier);
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
421
422 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
423 LocationSummary* locations = instruction_->GetLocations();
424 Register reg_out = out_.AsRegister<Register>();
425 DCHECK(locations->CanCall());
426 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
427 DCHECK(instruction_->IsInstanceFieldGet() ||
428 instruction_->IsStaticFieldGet() ||
429 instruction_->IsArrayGet() ||
430 instruction_->IsLoadClass() ||
431 instruction_->IsLoadString() ||
432 instruction_->IsInstanceOf() ||
433 instruction_->IsCheckCast())
434 << "Unexpected instruction in read barrier marking slow path: "
435 << instruction_->DebugName();
436
437 __ Bind(GetEntryLabel());
438 SaveLiveRegisters(codegen, locations);
439
440 InvokeRuntimeCallingConvention calling_convention;
441 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
442 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
443 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
444 instruction_,
445 instruction_->GetDexPc(),
446 this);
447 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
448 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
449
450 RestoreLiveRegisters(codegen, locations);
451 __ b(GetExitLabel());
452 }
453
454 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000455 const Location out_;
456 const Location obj_;
457
458 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
459};
460
Roland Levillain3b359c72015-11-17 19:35:12 +0000461// Slow path generating a read barrier for a heap reference.
462class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
463 public:
464 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
465 Location out,
466 Location ref,
467 Location obj,
468 uint32_t offset,
469 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000470 : SlowPathCode(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000471 out_(out),
472 ref_(ref),
473 obj_(obj),
474 offset_(offset),
475 index_(index) {
476 DCHECK(kEmitCompilerReadBarrier);
477 // If `obj` is equal to `out` or `ref`, it means the initial object
478 // has been overwritten by (or after) the heap object reference load
479 // to be instrumented, e.g.:
480 //
481 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000482 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000483 //
484 // In that case, we have lost the information about the original
485 // object, and the emitted read barrier cannot work properly.
486 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
487 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
488 }
489
490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
491 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
492 LocationSummary* locations = instruction_->GetLocations();
493 Register reg_out = out_.AsRegister<Register>();
494 DCHECK(locations->CanCall());
495 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
496 DCHECK(!instruction_->IsInvoke() ||
497 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillainc9285912015-12-18 10:38:42 +0000498 instruction_->GetLocations()->Intrinsified()))
499 << "Unexpected instruction in read barrier for heap reference slow path: "
500 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000501
502 __ Bind(GetEntryLabel());
503 SaveLiveRegisters(codegen, locations);
504
505 // We may have to change the index's value, but as `index_` is a
506 // constant member (like other "inputs" of this slow path),
507 // introduce a copy of it, `index`.
508 Location index = index_;
509 if (index_.IsValid()) {
510 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
511 if (instruction_->IsArrayGet()) {
512 // Compute the actual memory offset and store it in `index`.
513 Register index_reg = index_.AsRegister<Register>();
514 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
515 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
516 // We are about to change the value of `index_reg` (see the
517 // calls to art::arm::Thumb2Assembler::Lsl and
518 // art::arm::Thumb2Assembler::AddConstant below), but it has
519 // not been saved by the previous call to
520 // art::SlowPathCode::SaveLiveRegisters, as it is a
521 // callee-save register --
522 // art::SlowPathCode::SaveLiveRegisters does not consider
523 // callee-save registers, as it has been designed with the
524 // assumption that callee-save registers are supposed to be
525 // handled by the called function. So, as a callee-save
526 // register, `index_reg` _would_ eventually be saved onto
527 // the stack, but it would be too late: we would have
528 // changed its value earlier. Therefore, we manually save
529 // it here into another freely available register,
530 // `free_reg`, chosen of course among the caller-save
531 // registers (as a callee-save `free_reg` register would
532 // exhibit the same problem).
533 //
534 // Note we could have requested a temporary register from
535 // the register allocator instead; but we prefer not to, as
536 // this is a slow path, and we know we can find a
537 // caller-save register that is available.
538 Register free_reg = FindAvailableCallerSaveRegister(codegen);
539 __ Mov(free_reg, index_reg);
540 index_reg = free_reg;
541 index = Location::RegisterLocation(index_reg);
542 } else {
543 // The initial register stored in `index_` has already been
544 // saved in the call to art::SlowPathCode::SaveLiveRegisters
545 // (as it is not a callee-save register), so we can freely
546 // use it.
547 }
548 // Shifting the index value contained in `index_reg` by the scale
549 // factor (2) cannot overflow in practice, as the runtime is
550 // unable to allocate object arrays with a size larger than
551 // 2^26 - 1 (that is, 2^28 - 4 bytes).
552 __ Lsl(index_reg, index_reg, TIMES_4);
553 static_assert(
554 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
555 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
556 __ AddConstant(index_reg, index_reg, offset_);
557 } else {
558 DCHECK(instruction_->IsInvoke());
559 DCHECK(instruction_->GetLocations()->Intrinsified());
560 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
561 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
562 << instruction_->AsInvoke()->GetIntrinsic();
563 DCHECK_EQ(offset_, 0U);
564 DCHECK(index_.IsRegisterPair());
565 // UnsafeGet's offset location is a register pair, the low
566 // part contains the correct offset.
567 index = index_.ToLow();
568 }
569 }
570
571 // We're moving two or three locations to locations that could
572 // overlap, so we need a parallel move resolver.
573 InvokeRuntimeCallingConvention calling_convention;
574 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
575 parallel_move.AddMove(ref_,
576 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
577 Primitive::kPrimNot,
578 nullptr);
579 parallel_move.AddMove(obj_,
580 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
581 Primitive::kPrimNot,
582 nullptr);
583 if (index.IsValid()) {
584 parallel_move.AddMove(index,
585 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
586 Primitive::kPrimInt,
587 nullptr);
588 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
589 } else {
590 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
591 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
592 }
593 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
594 instruction_,
595 instruction_->GetDexPc(),
596 this);
597 CheckEntrypointTypes<
598 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
599 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
600
601 RestoreLiveRegisters(codegen, locations);
602 __ b(GetExitLabel());
603 }
604
605 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
606
607 private:
608 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
609 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
610 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
611 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
612 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
613 return static_cast<Register>(i);
614 }
615 }
616 // We shall never fail to find a free caller-save register, as
617 // there are more than two core caller-save registers on ARM
618 // (meaning it is possible to find one which is different from
619 // `ref` and `obj`).
620 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
621 LOG(FATAL) << "Could not find a free caller-save register";
622 UNREACHABLE();
623 }
624
Roland Levillain3b359c72015-11-17 19:35:12 +0000625 const Location out_;
626 const Location ref_;
627 const Location obj_;
628 const uint32_t offset_;
629 // An additional location containing an index to an array.
630 // Only used for HArrayGet and the UnsafeGetObject &
631 // UnsafeGetObjectVolatile intrinsics.
632 const Location index_;
633
634 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
635};
636
637// Slow path generating a read barrier for a GC root.
638class ReadBarrierForRootSlowPathARM : public SlowPathCode {
639 public:
640 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000641 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000642 DCHECK(kEmitCompilerReadBarrier);
643 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000644
645 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
646 LocationSummary* locations = instruction_->GetLocations();
647 Register reg_out = out_.AsRegister<Register>();
648 DCHECK(locations->CanCall());
649 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000650 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
651 << "Unexpected instruction in read barrier for GC root slow path: "
652 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000653
654 __ Bind(GetEntryLabel());
655 SaveLiveRegisters(codegen, locations);
656
657 InvokeRuntimeCallingConvention calling_convention;
658 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
659 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
660 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
661 instruction_,
662 instruction_->GetDexPc(),
663 this);
664 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
665 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
666
667 RestoreLiveRegisters(codegen, locations);
668 __ b(GetExitLabel());
669 }
670
671 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
672
673 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000674 const Location out_;
675 const Location root_;
676
677 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
678};
679
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000680#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700681// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
682#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700683
Aart Bike9f37602015-10-09 11:15:55 -0700684inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700685 switch (cond) {
686 case kCondEQ: return EQ;
687 case kCondNE: return NE;
688 case kCondLT: return LT;
689 case kCondLE: return LE;
690 case kCondGT: return GT;
691 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700692 case kCondB: return LO;
693 case kCondBE: return LS;
694 case kCondA: return HI;
695 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700696 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100697 LOG(FATAL) << "Unreachable";
698 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700699}
700
Aart Bike9f37602015-10-09 11:15:55 -0700701// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100702inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700703 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100704 case kCondEQ: return EQ;
705 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700706 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100707 case kCondLT: return LO;
708 case kCondLE: return LS;
709 case kCondGT: return HI;
710 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700711 // Unsigned remain unchanged.
712 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
Vladimir Markod6e069b2016-01-18 11:11:01 +0000721inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
722 // The ARM condition codes can express all the necessary branches, see the
723 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
724 // There is no dex instruction or HIR that would need the missing conditions
725 // "equal or unordered" or "not equal".
726 switch (cond) {
727 case kCondEQ: return EQ;
728 case kCondNE: return NE /* unordered */;
729 case kCondLT: return gt_bias ? CC : LT /* unordered */;
730 case kCondLE: return gt_bias ? LS : LE /* unordered */;
731 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
732 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
733 default:
734 LOG(FATAL) << "UNREACHABLE";
735 UNREACHABLE();
736 }
737}
738
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100739void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100740 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100741}
742
743void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100744 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100745}
746
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100747size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
748 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
749 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100750}
751
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100752size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
753 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
754 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100755}
756
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000757size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
758 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
759 return kArmWordSize;
760}
761
762size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
763 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
764 return kArmWordSize;
765}
766
Calin Juravle34166012014-12-19 17:22:29 +0000767CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000768 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100769 const CompilerOptions& compiler_options,
770 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000771 : CodeGenerator(graph,
772 kNumberOfCoreRegisters,
773 kNumberOfSRegisters,
774 kNumberOfRegisterPairs,
775 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
776 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000777 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
778 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100779 compiler_options,
780 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100781 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100782 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100783 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100784 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100785 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000786 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000787 uint32_literals_(std::less<uint32_t>(),
788 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100789 method_patches_(MethodReferenceComparator(),
790 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
791 call_patches_(MethodReferenceComparator(),
792 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000793 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000794 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
795 boot_image_string_patches_(StringReferenceValueComparator(),
796 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
797 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
798 boot_image_address_patches_(std::less<uint32_t>(),
799 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700800 // Always save the LR register to mimic Quick.
801 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100802}
803
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000804void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
805 // Ensure that we fix up branches and literal loads and emit the literal pool.
806 __ FinalizeCode();
807
808 // Adjust native pc offsets in stack maps.
809 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
810 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
811 uint32_t new_position = __ GetAdjustedPosition(old_position);
812 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
813 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100814 // Adjust pc offsets for the disassembly information.
815 if (disasm_info_ != nullptr) {
816 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
817 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
818 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
819 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
820 it.second.start = __ GetAdjustedPosition(it.second.start);
821 it.second.end = __ GetAdjustedPosition(it.second.end);
822 }
823 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
824 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
825 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
826 }
827 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000828
829 CodeGenerator::Finalize(allocator);
830}
831
David Brazdil58282f42016-01-14 12:45:10 +0000832void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100833 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100834 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100835
836 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100837 blocked_core_registers_[SP] = true;
838 blocked_core_registers_[LR] = true;
839 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100840
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100841 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100842 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100844 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100845 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100846
David Brazdil58282f42016-01-14 12:45:10 +0000847 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100848 // Stubs do not save callee-save floating point registers. If the graph
849 // is debuggable, we need to deal with these registers differently. For
850 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000851 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
852 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
853 }
854 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100855
856 UpdateBlockedPairRegisters();
857}
858
859void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
860 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
861 ArmManagedRegister current =
862 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
863 if (blocked_core_registers_[current.AsRegisterPairLow()]
864 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
865 blocked_register_pairs_[i] = true;
866 }
867 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868}
869
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100870InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800871 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100872 assembler_(codegen->GetAssembler()),
873 codegen_(codegen) {}
874
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000875void CodeGeneratorARM::ComputeSpillMask() {
876 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
877 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000878 // There is no easy instruction to restore just the PC on thumb2. We spill and
879 // restore another arbitrary register.
880 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000881 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
882 // We use vpush and vpop for saving and restoring floating point registers, which take
883 // a SRegister and the number of registers to save/restore after that SRegister. We
884 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
885 // but in the range.
886 if (fpu_spill_mask_ != 0) {
887 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
888 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
889 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
890 fpu_spill_mask_ |= (1 << i);
891 }
892 }
893}
894
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100895static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100896 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100897}
898
899static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100900 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100901}
902
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000903void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000904 bool skip_overflow_check =
905 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000906 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000907 __ Bind(&frame_entry_label_);
908
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000909 if (HasEmptyFrame()) {
910 return;
911 }
912
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100913 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000914 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
915 __ LoadFromOffset(kLoadWord, IP, IP, 0);
916 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100917 }
918
Andreas Gampe501fd632015-09-10 16:11:06 -0700919 __ PushList(core_spill_mask_);
920 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
921 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000922 if (fpu_spill_mask_ != 0) {
923 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
924 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100925 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100926 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000927 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100928 int adjust = GetFrameSize() - FrameEntrySpillSize();
929 __ AddConstant(SP, -adjust);
930 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100931 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000932}
933
934void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000935 if (HasEmptyFrame()) {
936 __ bx(LR);
937 return;
938 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100939 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100940 int adjust = GetFrameSize() - FrameEntrySpillSize();
941 __ AddConstant(SP, adjust);
942 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000943 if (fpu_spill_mask_ != 0) {
944 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
945 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100946 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
947 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000948 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700949 // Pop LR into PC to return.
950 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
951 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
952 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100953 __ cfi().RestoreState();
954 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000955}
956
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100957void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700958 Label* label = GetLabelOf(block);
959 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000960}
961
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100962Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100963 switch (type) {
964 case Primitive::kPrimBoolean:
965 case Primitive::kPrimByte:
966 case Primitive::kPrimChar:
967 case Primitive::kPrimShort:
968 case Primitive::kPrimInt:
969 case Primitive::kPrimNot: {
970 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000971 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000975 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100976 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100977 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100978
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000979 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100980 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000981 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100982 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000983 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100984 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000985 if (calling_convention.GetRegisterAt(index) == R1) {
986 // Skip R1, and use R2_R3 instead.
987 gp_index_++;
988 index++;
989 }
990 }
991 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
992 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000993 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100994
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000995 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000996 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100997 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000998 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
999 }
1000 }
1001
1002 case Primitive::kPrimFloat: {
1003 uint32_t stack_index = stack_index_++;
1004 if (float_index_ % 2 == 0) {
1005 float_index_ = std::max(double_index_, float_index_);
1006 }
1007 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1008 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1009 } else {
1010 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1011 }
1012 }
1013
1014 case Primitive::kPrimDouble: {
1015 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1016 uint32_t stack_index = stack_index_;
1017 stack_index_ += 2;
1018 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1019 uint32_t index = double_index_;
1020 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001021 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001022 calling_convention.GetFpuRegisterAt(index),
1023 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001024 DCHECK(ExpectedPairLayout(result));
1025 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001026 } else {
1027 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001028 }
1029 }
1030
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001031 case Primitive::kPrimVoid:
1032 LOG(FATAL) << "Unexpected parameter type " << type;
1033 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001034 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001035 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001036}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001037
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001038Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001039 switch (type) {
1040 case Primitive::kPrimBoolean:
1041 case Primitive::kPrimByte:
1042 case Primitive::kPrimChar:
1043 case Primitive::kPrimShort:
1044 case Primitive::kPrimInt:
1045 case Primitive::kPrimNot: {
1046 return Location::RegisterLocation(R0);
1047 }
1048
1049 case Primitive::kPrimFloat: {
1050 return Location::FpuRegisterLocation(S0);
1051 }
1052
1053 case Primitive::kPrimLong: {
1054 return Location::RegisterPairLocation(R0, R1);
1055 }
1056
1057 case Primitive::kPrimDouble: {
1058 return Location::FpuRegisterPairLocation(S0, S1);
1059 }
1060
1061 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001062 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001063 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001064
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001065 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001066}
1067
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001068Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1069 return Location::RegisterLocation(kMethodRegisterArgument);
1070}
1071
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072void CodeGeneratorARM::Move32(Location destination, Location source) {
1073 if (source.Equals(destination)) {
1074 return;
1075 }
1076 if (destination.IsRegister()) {
1077 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001078 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001080 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001082 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 } else if (destination.IsFpuRegister()) {
1085 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001086 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001087 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001088 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001090 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001093 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001095 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001096 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001097 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001099 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001100 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1101 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 }
1103 }
1104}
1105
1106void CodeGeneratorARM::Move64(Location destination, Location source) {
1107 if (source.Equals(destination)) {
1108 return;
1109 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001110 if (destination.IsRegisterPair()) {
1111 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001112 EmitParallelMoves(
1113 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1114 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001115 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001116 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001117 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1118 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001119 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001120 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001121 } else if (source.IsFpuRegisterPair()) {
1122 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1123 destination.AsRegisterPairHigh<Register>(),
1124 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001125 } else {
1126 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001127 DCHECK(ExpectedPairLayout(destination));
1128 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1129 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001131 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001133 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1134 SP,
1135 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001136 } else if (source.IsRegisterPair()) {
1137 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1138 source.AsRegisterPairLow<Register>(),
1139 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001140 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001141 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001142 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 } else {
1144 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001145 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001146 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 if (source.AsRegisterPairLow<Register>() == R1) {
1148 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001149 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1150 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 SP, destination.GetStackIndex());
1154 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001155 } else if (source.IsFpuRegisterPair()) {
1156 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1157 SP,
1158 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 } else {
1160 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001161 EmitParallelMoves(
1162 Location::StackSlot(source.GetStackIndex()),
1163 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001164 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001165 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001166 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1167 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 }
1169 }
1170}
1171
Calin Juravle175dc732015-08-25 15:42:32 +01001172void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1173 DCHECK(location.IsRegister());
1174 __ LoadImmediate(location.AsRegister<Register>(), value);
1175}
1176
Calin Juravlee460d1d2015-09-29 04:52:17 +01001177void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001178 HParallelMove move(GetGraph()->GetArena());
1179 move.AddMove(src, dst, dst_type, nullptr);
1180 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001181}
1182
1183void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1184 if (location.IsRegister()) {
1185 locations->AddTemp(location);
1186 } else if (location.IsRegisterPair()) {
1187 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1188 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1189 } else {
1190 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1191 }
1192}
1193
Calin Juravle175dc732015-08-25 15:42:32 +01001194void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1195 HInstruction* instruction,
1196 uint32_t dex_pc,
1197 SlowPathCode* slow_path) {
1198 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1199 instruction,
1200 dex_pc,
1201 slow_path);
1202}
1203
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001204void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1205 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001206 uint32_t dex_pc,
1207 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001208 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001209 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1210 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001211 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001212}
1213
David Brazdilfc6a86a2015-06-26 10:33:45 +00001214void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001215 DCHECK(!successor->IsExitBlock());
1216
1217 HBasicBlock* block = got->GetBlock();
1218 HInstruction* previous = got->GetPrevious();
1219
1220 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001221 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001222 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1223 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1224 return;
1225 }
1226
1227 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1228 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1229 }
1230 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001231 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001232 }
1233}
1234
David Brazdilfc6a86a2015-06-26 10:33:45 +00001235void LocationsBuilderARM::VisitGoto(HGoto* got) {
1236 got->SetLocations(nullptr);
1237}
1238
1239void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1240 HandleGoto(got, got->GetSuccessor());
1241}
1242
1243void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1244 try_boundary->SetLocations(nullptr);
1245}
1246
1247void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1248 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1249 if (!successor->IsExitBlock()) {
1250 HandleGoto(try_boundary, successor);
1251 }
1252}
1253
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001254void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001255 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001256}
1257
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001258void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001259}
1260
Roland Levillain4fa13f62015-07-06 18:11:54 +01001261void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1262 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001263 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001264 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001265 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001266}
1267
1268void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1269 Label* true_label,
1270 Label* false_label) {
1271 LocationSummary* locations = cond->GetLocations();
1272 Location left = locations->InAt(0);
1273 Location right = locations->InAt(1);
1274 IfCondition if_cond = cond->GetCondition();
1275
1276 Register left_high = left.AsRegisterPairHigh<Register>();
1277 Register left_low = left.AsRegisterPairLow<Register>();
1278 IfCondition true_high_cond = if_cond;
1279 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001280 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001281
1282 // Set the conditions for the test, remembering that == needs to be
1283 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001284 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001285 switch (if_cond) {
1286 case kCondEQ:
1287 case kCondNE:
1288 // Nothing to do.
1289 break;
1290 case kCondLT:
1291 false_high_cond = kCondGT;
1292 break;
1293 case kCondLE:
1294 true_high_cond = kCondLT;
1295 break;
1296 case kCondGT:
1297 false_high_cond = kCondLT;
1298 break;
1299 case kCondGE:
1300 true_high_cond = kCondGT;
1301 break;
Aart Bike9f37602015-10-09 11:15:55 -07001302 case kCondB:
1303 false_high_cond = kCondA;
1304 break;
1305 case kCondBE:
1306 true_high_cond = kCondB;
1307 break;
1308 case kCondA:
1309 false_high_cond = kCondB;
1310 break;
1311 case kCondAE:
1312 true_high_cond = kCondA;
1313 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001314 }
1315 if (right.IsConstant()) {
1316 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1317 int32_t val_low = Low32Bits(value);
1318 int32_t val_high = High32Bits(value);
1319
Vladimir Markoac6ac102015-12-17 12:14:00 +00001320 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001321 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001322 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001323 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001324 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001325 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001326 __ b(true_label, ARMCondition(true_high_cond));
1327 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001328 }
1329 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001330 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001331 } else {
1332 Register right_high = right.AsRegisterPairHigh<Register>();
1333 Register right_low = right.AsRegisterPairLow<Register>();
1334
1335 __ cmp(left_high, ShifterOperand(right_high));
1336 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001337 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001338 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001339 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001340 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001341 __ b(true_label, ARMCondition(true_high_cond));
1342 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343 }
1344 // Must be equal high, so compare the lows.
1345 __ cmp(left_low, ShifterOperand(right_low));
1346 }
1347 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001348 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001349 __ b(true_label, final_condition);
1350}
1351
David Brazdil0debae72015-11-12 18:37:00 +00001352void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1353 Label* true_target_in,
1354 Label* false_target_in) {
1355 // Generated branching requires both targets to be explicit. If either of the
1356 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1357 Label fallthrough_target;
1358 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1359 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1360
Roland Levillain4fa13f62015-07-06 18:11:54 +01001361 LocationSummary* locations = condition->GetLocations();
1362 Location left = locations->InAt(0);
1363 Location right = locations->InAt(1);
1364
Roland Levillain4fa13f62015-07-06 18:11:54 +01001365 Primitive::Type type = condition->InputAt(0)->GetType();
1366 switch (type) {
1367 case Primitive::kPrimLong:
1368 GenerateLongComparesAndJumps(condition, true_target, false_target);
1369 break;
1370 case Primitive::kPrimFloat:
1371 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1372 GenerateFPJumps(condition, true_target, false_target);
1373 break;
1374 case Primitive::kPrimDouble:
1375 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1376 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1377 GenerateFPJumps(condition, true_target, false_target);
1378 break;
1379 default:
1380 LOG(FATAL) << "Unexpected compare type " << type;
1381 }
1382
David Brazdil0debae72015-11-12 18:37:00 +00001383 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001384 __ b(false_target);
1385 }
David Brazdil0debae72015-11-12 18:37:00 +00001386
1387 if (fallthrough_target.IsLinked()) {
1388 __ Bind(&fallthrough_target);
1389 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001390}
1391
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001392void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001393 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001394 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001395 Label* false_target) {
1396 HInstruction* cond = instruction->InputAt(condition_input_index);
1397
1398 if (true_target == nullptr && false_target == nullptr) {
1399 // Nothing to do. The code always falls through.
1400 return;
1401 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001402 // Constant condition, statically compared against "true" (integer value 1).
1403 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001404 if (true_target != nullptr) {
1405 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001406 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001407 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001408 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001409 if (false_target != nullptr) {
1410 __ b(false_target);
1411 }
1412 }
1413 return;
1414 }
1415
1416 // The following code generates these patterns:
1417 // (1) true_target == nullptr && false_target != nullptr
1418 // - opposite condition true => branch to false_target
1419 // (2) true_target != nullptr && false_target == nullptr
1420 // - condition true => branch to true_target
1421 // (3) true_target != nullptr && false_target != nullptr
1422 // - condition true => branch to true_target
1423 // - branch to false_target
1424 if (IsBooleanValueOrMaterializedCondition(cond)) {
1425 // Condition has been materialized, compare the output to 0.
1426 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1427 DCHECK(cond_val.IsRegister());
1428 if (true_target == nullptr) {
1429 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1430 } else {
1431 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001432 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001433 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001434 // Condition has not been materialized. Use its inputs as the comparison and
1435 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001436 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001437
1438 // If this is a long or FP comparison that has been folded into
1439 // the HCondition, generate the comparison directly.
1440 Primitive::Type type = condition->InputAt(0)->GetType();
1441 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1442 GenerateCompareTestAndBranch(condition, true_target, false_target);
1443 return;
1444 }
1445
1446 LocationSummary* locations = cond->GetLocations();
1447 DCHECK(locations->InAt(0).IsRegister());
1448 Register left = locations->InAt(0).AsRegister<Register>();
1449 Location right = locations->InAt(1);
1450 if (right.IsRegister()) {
1451 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001452 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001453 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001454 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001455 }
1456 if (true_target == nullptr) {
1457 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1458 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001459 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001460 }
Dave Allison20dfc792014-06-16 20:44:29 -07001461 }
David Brazdil0debae72015-11-12 18:37:00 +00001462
1463 // If neither branch falls through (case 3), the conditional branch to `true_target`
1464 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1465 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001466 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001467 }
1468}
1469
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001470void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1472 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001473 locations->SetInAt(0, Location::RequiresRegister());
1474 }
1475}
1476
1477void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001478 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1479 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1480 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1481 nullptr : codegen_->GetLabelOf(true_successor);
1482 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1483 nullptr : codegen_->GetLabelOf(false_successor);
1484 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001485}
1486
1487void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1488 LocationSummary* locations = new (GetGraph()->GetArena())
1489 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001490 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001491 locations->SetInAt(0, Location::RequiresRegister());
1492 }
1493}
1494
1495void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001496 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001497 GenerateTestAndBranch(deoptimize,
1498 /* condition_input_index */ 0,
1499 slow_path->GetEntryLabel(),
1500 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001501}
Dave Allison20dfc792014-06-16 20:44:29 -07001502
David Brazdil74eb1b22015-12-14 11:44:01 +00001503void LocationsBuilderARM::VisitSelect(HSelect* select) {
1504 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1505 if (Primitive::IsFloatingPointType(select->GetType())) {
1506 locations->SetInAt(0, Location::RequiresFpuRegister());
1507 locations->SetInAt(1, Location::RequiresFpuRegister());
1508 } else {
1509 locations->SetInAt(0, Location::RequiresRegister());
1510 locations->SetInAt(1, Location::RequiresRegister());
1511 }
1512 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1513 locations->SetInAt(2, Location::RequiresRegister());
1514 }
1515 locations->SetOut(Location::SameAsFirstInput());
1516}
1517
1518void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1519 LocationSummary* locations = select->GetLocations();
1520 Label false_target;
1521 GenerateTestAndBranch(select,
1522 /* condition_input_index */ 2,
1523 /* true_target */ nullptr,
1524 &false_target);
1525 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1526 __ Bind(&false_target);
1527}
1528
David Srbecky0cf44932015-12-09 14:09:59 +00001529void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1530 new (GetGraph()->GetArena()) LocationSummary(info);
1531}
1532
David Srbeckyd28f4a02016-03-14 17:14:24 +00001533void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1534 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001535}
1536
1537void CodeGeneratorARM::GenerateNop() {
1538 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001539}
1540
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001541void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001542 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001543 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001544 // Handle the long/FP comparisons made in instruction simplification.
1545 switch (cond->InputAt(0)->GetType()) {
1546 case Primitive::kPrimLong:
1547 locations->SetInAt(0, Location::RequiresRegister());
1548 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001549 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001550 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1551 }
1552 break;
1553
1554 case Primitive::kPrimFloat:
1555 case Primitive::kPrimDouble:
1556 locations->SetInAt(0, Location::RequiresFpuRegister());
1557 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001558 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001559 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1560 }
1561 break;
1562
1563 default:
1564 locations->SetInAt(0, Location::RequiresRegister());
1565 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001566 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001567 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1568 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001569 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001570}
1571
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001572void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001573 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001574 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001575 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001576
1577 LocationSummary* locations = cond->GetLocations();
1578 Location left = locations->InAt(0);
1579 Location right = locations->InAt(1);
1580 Register out = locations->Out().AsRegister<Register>();
1581 Label true_label, false_label;
1582
1583 switch (cond->InputAt(0)->GetType()) {
1584 default: {
1585 // Integer case.
1586 if (right.IsRegister()) {
1587 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1588 } else {
1589 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001590 __ CmpConstant(left.AsRegister<Register>(),
1591 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001592 }
Aart Bike9f37602015-10-09 11:15:55 -07001593 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001594 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001595 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001596 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001597 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001598 return;
1599 }
1600 case Primitive::kPrimLong:
1601 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1602 break;
1603 case Primitive::kPrimFloat:
1604 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1605 GenerateFPJumps(cond, &true_label, &false_label);
1606 break;
1607 case Primitive::kPrimDouble:
1608 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1609 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1610 GenerateFPJumps(cond, &true_label, &false_label);
1611 break;
1612 }
1613
1614 // Convert the jumps into the result.
1615 Label done_label;
1616
1617 // False case: result = 0.
1618 __ Bind(&false_label);
1619 __ LoadImmediate(out, 0);
1620 __ b(&done_label);
1621
1622 // True case: result = 1.
1623 __ Bind(&true_label);
1624 __ LoadImmediate(out, 1);
1625 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001626}
1627
1628void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001629 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001630}
1631
1632void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001633 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001634}
1635
1636void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001637 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001638}
1639
1640void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001641 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001642}
1643
1644void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001645 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001646}
1647
1648void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001649 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001650}
1651
1652void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001653 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001654}
1655
1656void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001657 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001658}
1659
1660void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001661 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001662}
1663
1664void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001665 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001666}
1667
1668void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001669 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001670}
1671
1672void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001673 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001674}
1675
Aart Bike9f37602015-10-09 11:15:55 -07001676void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001677 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001678}
1679
1680void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001681 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001682}
1683
1684void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001685 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001686}
1687
1688void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001689 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001690}
1691
1692void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001693 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001694}
1695
1696void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001697 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001698}
1699
1700void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001701 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001702}
1703
1704void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001705 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001706}
1707
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001708void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001709 LocationSummary* locations =
1710 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001711 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001712}
1713
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001714void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001715 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001716}
1717
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001718void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1719 LocationSummary* locations =
1720 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1721 locations->SetOut(Location::ConstantLocation(constant));
1722}
1723
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001724void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001725 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001726}
1727
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001728void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001729 LocationSummary* locations =
1730 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001731 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001732}
1733
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001734void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001735 // Will be generated at use site.
1736}
1737
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001738void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1739 LocationSummary* locations =
1740 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1741 locations->SetOut(Location::ConstantLocation(constant));
1742}
1743
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001744void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001745 // Will be generated at use site.
1746}
1747
1748void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1749 LocationSummary* locations =
1750 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1751 locations->SetOut(Location::ConstantLocation(constant));
1752}
1753
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001754void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001755 // Will be generated at use site.
1756}
1757
Calin Juravle27df7582015-04-17 19:12:31 +01001758void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1759 memory_barrier->SetLocations(nullptr);
1760}
1761
1762void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001763 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001764}
1765
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001766void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001767 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001768}
1769
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001770void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001771 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001772}
1773
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001774void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001775 LocationSummary* locations =
1776 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001777 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778}
1779
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001780void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001781 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001782}
1783
Calin Juravle175dc732015-08-25 15:42:32 +01001784void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1785 // The trampoline uses the same calling convention as dex calling conventions,
1786 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1787 // the method_idx.
1788 HandleInvoke(invoke);
1789}
1790
1791void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1792 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1793}
1794
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001795void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001796 // Explicit clinit checks triggered by static invokes must have been pruned by
1797 // art::PrepareForRegisterAllocation.
1798 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001799
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001800 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001801 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001802 codegen_->GetInstructionSetFeatures());
1803 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001804 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1805 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1806 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001807 return;
1808 }
1809
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001810 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001811
1812 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1813 if (invoke->HasPcRelativeDexCache()) {
1814 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1815 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001816}
1817
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001818static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1819 if (invoke->GetLocations()->Intrinsified()) {
1820 IntrinsicCodeGeneratorARM intrinsic(codegen);
1821 intrinsic.Dispatch(invoke);
1822 return true;
1823 }
1824 return false;
1825}
1826
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001827void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001828 // Explicit clinit checks triggered by static invokes must have been pruned by
1829 // art::PrepareForRegisterAllocation.
1830 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001831
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001832 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1833 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001834 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001835
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001836 LocationSummary* locations = invoke->GetLocations();
1837 codegen_->GenerateStaticOrDirectCall(
1838 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001839 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001840}
1841
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001842void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001843 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001844 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001845}
1846
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001847void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001848 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001849 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001850 codegen_->GetInstructionSetFeatures());
1851 if (intrinsic.TryDispatch(invoke)) {
1852 return;
1853 }
1854
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001855 HandleInvoke(invoke);
1856}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001857
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001858void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001859 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1860 return;
1861 }
1862
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001863 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001864 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001865 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001866}
1867
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001868void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1869 HandleInvoke(invoke);
1870 // Add the hidden argument.
1871 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1872}
1873
1874void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1875 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001876 LocationSummary* locations = invoke->GetLocations();
1877 Register temp = locations->GetTemp(0).AsRegister<Register>();
1878 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001879 Location receiver = locations->InAt(0);
1880 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1881
Roland Levillain3b359c72015-11-17 19:35:12 +00001882 // Set the hidden argument. This is safe to do this here, as R12
1883 // won't be modified thereafter, before the `blx` (call) instruction.
1884 DCHECK_EQ(R12, hidden_reg);
1885 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001886
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001887 if (receiver.IsStackSlot()) {
1888 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001889 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001890 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1891 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001892 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001893 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001894 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001895 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001896 // Instead of simply (possibly) unpoisoning `temp` here, we should
1897 // emit a read barrier for the previous class reference load.
1898 // However this is not required in practice, as this is an
1899 // intermediate/temporary reference and because the current
1900 // concurrent copying collector keeps the from-space memory
1901 // intact/accessible until the end of the marking phase (the
1902 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001903 __ MaybeUnpoisonHeapReference(temp);
Nelli Kimbadee982016-05-13 13:08:53 +03001904 __ LoadFromOffset(kLoadWord, temp, temp,
1905 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
1906 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07001907 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001908 // temp = temp->GetImtEntryAt(method_offset);
Nelli Kimbadee982016-05-13 13:08:53 +03001909 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001910 uint32_t entry_point =
1911 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001912 // LR = temp->GetEntryPoint();
1913 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1914 // LR();
1915 __ blx(LR);
1916 DCHECK(!codegen_->IsLeafMethod());
1917 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1918}
1919
Roland Levillain88cb1752014-10-20 16:36:47 +01001920void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1921 LocationSummary* locations =
1922 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1923 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001924 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001925 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1927 break;
1928 }
1929 case Primitive::kPrimLong: {
1930 locations->SetInAt(0, Location::RequiresRegister());
1931 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001932 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001933 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001934
Roland Levillain88cb1752014-10-20 16:36:47 +01001935 case Primitive::kPrimFloat:
1936 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001937 locations->SetInAt(0, Location::RequiresFpuRegister());
1938 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001939 break;
1940
1941 default:
1942 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1943 }
1944}
1945
1946void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1947 LocationSummary* locations = neg->GetLocations();
1948 Location out = locations->Out();
1949 Location in = locations->InAt(0);
1950 switch (neg->GetResultType()) {
1951 case Primitive::kPrimInt:
1952 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001953 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001954 break;
1955
1956 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001957 DCHECK(in.IsRegisterPair());
1958 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1959 __ rsbs(out.AsRegisterPairLow<Register>(),
1960 in.AsRegisterPairLow<Register>(),
1961 ShifterOperand(0));
1962 // We cannot emit an RSC (Reverse Subtract with Carry)
1963 // instruction here, as it does not exist in the Thumb-2
1964 // instruction set. We use the following approach
1965 // using SBC and SUB instead.
1966 //
1967 // out.hi = -C
1968 __ sbc(out.AsRegisterPairHigh<Register>(),
1969 out.AsRegisterPairHigh<Register>(),
1970 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1971 // out.hi = out.hi - in.hi
1972 __ sub(out.AsRegisterPairHigh<Register>(),
1973 out.AsRegisterPairHigh<Register>(),
1974 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1975 break;
1976
Roland Levillain88cb1752014-10-20 16:36:47 +01001977 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001978 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001979 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001980 break;
1981
Roland Levillain88cb1752014-10-20 16:36:47 +01001982 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001983 DCHECK(in.IsFpuRegisterPair());
1984 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1985 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001986 break;
1987
1988 default:
1989 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1990 }
1991}
1992
Roland Levillaindff1f282014-11-05 14:15:05 +00001993void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001994 Primitive::Type result_type = conversion->GetResultType();
1995 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001996 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001997
Roland Levillain5b3ee562015-04-14 16:02:41 +01001998 // The float-to-long, double-to-long and long-to-float type conversions
1999 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002000 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002001 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2002 && result_type == Primitive::kPrimLong)
2003 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002004 ? LocationSummary::kCall
2005 : LocationSummary::kNoCall;
2006 LocationSummary* locations =
2007 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2008
David Brazdilb2bd1c52015-03-25 11:17:37 +00002009 // The Java language does not allow treating boolean as an integral type but
2010 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002011
Roland Levillaindff1f282014-11-05 14:15:05 +00002012 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002013 case Primitive::kPrimByte:
2014 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002015 case Primitive::kPrimLong:
2016 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002017 case Primitive::kPrimBoolean:
2018 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002019 case Primitive::kPrimShort:
2020 case Primitive::kPrimInt:
2021 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002022 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002023 locations->SetInAt(0, Location::RequiresRegister());
2024 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2025 break;
2026
2027 default:
2028 LOG(FATAL) << "Unexpected type conversion from " << input_type
2029 << " to " << result_type;
2030 }
2031 break;
2032
Roland Levillain01a8d712014-11-14 16:27:39 +00002033 case Primitive::kPrimShort:
2034 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002035 case Primitive::kPrimLong:
2036 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002037 case Primitive::kPrimBoolean:
2038 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002039 case Primitive::kPrimByte:
2040 case Primitive::kPrimInt:
2041 case Primitive::kPrimChar:
2042 // Processing a Dex `int-to-short' instruction.
2043 locations->SetInAt(0, Location::RequiresRegister());
2044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2045 break;
2046
2047 default:
2048 LOG(FATAL) << "Unexpected type conversion from " << input_type
2049 << " to " << result_type;
2050 }
2051 break;
2052
Roland Levillain946e1432014-11-11 17:35:19 +00002053 case Primitive::kPrimInt:
2054 switch (input_type) {
2055 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002056 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002057 locations->SetInAt(0, Location::Any());
2058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2059 break;
2060
2061 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002062 // Processing a Dex `float-to-int' instruction.
2063 locations->SetInAt(0, Location::RequiresFpuRegister());
2064 locations->SetOut(Location::RequiresRegister());
2065 locations->AddTemp(Location::RequiresFpuRegister());
2066 break;
2067
Roland Levillain946e1432014-11-11 17:35:19 +00002068 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002069 // Processing a Dex `double-to-int' instruction.
2070 locations->SetInAt(0, Location::RequiresFpuRegister());
2071 locations->SetOut(Location::RequiresRegister());
2072 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002073 break;
2074
2075 default:
2076 LOG(FATAL) << "Unexpected type conversion from " << input_type
2077 << " to " << result_type;
2078 }
2079 break;
2080
Roland Levillaindff1f282014-11-05 14:15:05 +00002081 case Primitive::kPrimLong:
2082 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002083 case Primitive::kPrimBoolean:
2084 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002085 case Primitive::kPrimByte:
2086 case Primitive::kPrimShort:
2087 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002088 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002089 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002090 locations->SetInAt(0, Location::RequiresRegister());
2091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2092 break;
2093
Roland Levillain624279f2014-12-04 11:54:28 +00002094 case Primitive::kPrimFloat: {
2095 // Processing a Dex `float-to-long' instruction.
2096 InvokeRuntimeCallingConvention calling_convention;
2097 locations->SetInAt(0, Location::FpuRegisterLocation(
2098 calling_convention.GetFpuRegisterAt(0)));
2099 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2100 break;
2101 }
2102
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002103 case Primitive::kPrimDouble: {
2104 // Processing a Dex `double-to-long' instruction.
2105 InvokeRuntimeCallingConvention calling_convention;
2106 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2107 calling_convention.GetFpuRegisterAt(0),
2108 calling_convention.GetFpuRegisterAt(1)));
2109 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002110 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002111 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002112
2113 default:
2114 LOG(FATAL) << "Unexpected type conversion from " << input_type
2115 << " to " << result_type;
2116 }
2117 break;
2118
Roland Levillain981e4542014-11-14 11:47:14 +00002119 case Primitive::kPrimChar:
2120 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002121 case Primitive::kPrimLong:
2122 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002123 case Primitive::kPrimBoolean:
2124 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002125 case Primitive::kPrimByte:
2126 case Primitive::kPrimShort:
2127 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002128 // Processing a Dex `int-to-char' instruction.
2129 locations->SetInAt(0, Location::RequiresRegister());
2130 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2131 break;
2132
2133 default:
2134 LOG(FATAL) << "Unexpected type conversion from " << input_type
2135 << " to " << result_type;
2136 }
2137 break;
2138
Roland Levillaindff1f282014-11-05 14:15:05 +00002139 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002140 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002141 case Primitive::kPrimBoolean:
2142 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002143 case Primitive::kPrimByte:
2144 case Primitive::kPrimShort:
2145 case Primitive::kPrimInt:
2146 case Primitive::kPrimChar:
2147 // Processing a Dex `int-to-float' instruction.
2148 locations->SetInAt(0, Location::RequiresRegister());
2149 locations->SetOut(Location::RequiresFpuRegister());
2150 break;
2151
Roland Levillain5b3ee562015-04-14 16:02:41 +01002152 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002153 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002154 InvokeRuntimeCallingConvention calling_convention;
2155 locations->SetInAt(0, Location::RegisterPairLocation(
2156 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2157 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002158 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002159 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002160
Roland Levillaincff13742014-11-17 14:32:17 +00002161 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002162 // Processing a Dex `double-to-float' instruction.
2163 locations->SetInAt(0, Location::RequiresFpuRegister());
2164 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002165 break;
2166
2167 default:
2168 LOG(FATAL) << "Unexpected type conversion from " << input_type
2169 << " to " << result_type;
2170 };
2171 break;
2172
Roland Levillaindff1f282014-11-05 14:15:05 +00002173 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002174 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002175 case Primitive::kPrimBoolean:
2176 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002177 case Primitive::kPrimByte:
2178 case Primitive::kPrimShort:
2179 case Primitive::kPrimInt:
2180 case Primitive::kPrimChar:
2181 // Processing a Dex `int-to-double' instruction.
2182 locations->SetInAt(0, Location::RequiresRegister());
2183 locations->SetOut(Location::RequiresFpuRegister());
2184 break;
2185
2186 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002187 // Processing a Dex `long-to-double' instruction.
2188 locations->SetInAt(0, Location::RequiresRegister());
2189 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002190 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002191 locations->AddTemp(Location::RequiresFpuRegister());
2192 break;
2193
Roland Levillaincff13742014-11-17 14:32:17 +00002194 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002195 // Processing a Dex `float-to-double' instruction.
2196 locations->SetInAt(0, Location::RequiresFpuRegister());
2197 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002204 break;
2205
2206 default:
2207 LOG(FATAL) << "Unexpected type conversion from " << input_type
2208 << " to " << result_type;
2209 }
2210}
2211
2212void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2213 LocationSummary* locations = conversion->GetLocations();
2214 Location out = locations->Out();
2215 Location in = locations->InAt(0);
2216 Primitive::Type result_type = conversion->GetResultType();
2217 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002218 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002219 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002220 case Primitive::kPrimByte:
2221 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002222 case Primitive::kPrimLong:
2223 // Type conversion from long to byte is a result of code transformations.
2224 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2225 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002226 case Primitive::kPrimBoolean:
2227 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002228 case Primitive::kPrimShort:
2229 case Primitive::kPrimInt:
2230 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002231 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002232 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002233 break;
2234
2235 default:
2236 LOG(FATAL) << "Unexpected type conversion from " << input_type
2237 << " to " << result_type;
2238 }
2239 break;
2240
Roland Levillain01a8d712014-11-14 16:27:39 +00002241 case Primitive::kPrimShort:
2242 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002243 case Primitive::kPrimLong:
2244 // Type conversion from long to short is a result of code transformations.
2245 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2246 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002247 case Primitive::kPrimBoolean:
2248 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002249 case Primitive::kPrimByte:
2250 case Primitive::kPrimInt:
2251 case Primitive::kPrimChar:
2252 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002253 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002254 break;
2255
2256 default:
2257 LOG(FATAL) << "Unexpected type conversion from " << input_type
2258 << " to " << result_type;
2259 }
2260 break;
2261
Roland Levillain946e1432014-11-11 17:35:19 +00002262 case Primitive::kPrimInt:
2263 switch (input_type) {
2264 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002265 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002266 DCHECK(out.IsRegister());
2267 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002268 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002269 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002270 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002271 } else {
2272 DCHECK(in.IsConstant());
2273 DCHECK(in.GetConstant()->IsLongConstant());
2274 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002275 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002276 }
2277 break;
2278
Roland Levillain3f8f9362014-12-02 17:45:01 +00002279 case Primitive::kPrimFloat: {
2280 // Processing a Dex `float-to-int' instruction.
2281 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2282 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2283 __ vcvtis(temp, temp);
2284 __ vmovrs(out.AsRegister<Register>(), temp);
2285 break;
2286 }
2287
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002288 case Primitive::kPrimDouble: {
2289 // Processing a Dex `double-to-int' instruction.
2290 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2291 DRegister temp_d = FromLowSToD(temp_s);
2292 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2293 __ vcvtid(temp_s, temp_d);
2294 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002295 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002296 }
Roland Levillain946e1432014-11-11 17:35:19 +00002297
2298 default:
2299 LOG(FATAL) << "Unexpected type conversion from " << input_type
2300 << " to " << result_type;
2301 }
2302 break;
2303
Roland Levillaindff1f282014-11-05 14:15:05 +00002304 case Primitive::kPrimLong:
2305 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002306 case Primitive::kPrimBoolean:
2307 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002308 case Primitive::kPrimByte:
2309 case Primitive::kPrimShort:
2310 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002311 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002312 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002313 DCHECK(out.IsRegisterPair());
2314 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002315 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002316 // Sign extension.
2317 __ Asr(out.AsRegisterPairHigh<Register>(),
2318 out.AsRegisterPairLow<Register>(),
2319 31);
2320 break;
2321
2322 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002323 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002324 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2325 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002326 conversion->GetDexPc(),
2327 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002328 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002329 break;
2330
Roland Levillaindff1f282014-11-05 14:15:05 +00002331 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002332 // Processing a Dex `double-to-long' instruction.
2333 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2334 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002335 conversion->GetDexPc(),
2336 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002337 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002338 break;
2339
2340 default:
2341 LOG(FATAL) << "Unexpected type conversion from " << input_type
2342 << " to " << result_type;
2343 }
2344 break;
2345
Roland Levillain981e4542014-11-14 11:47:14 +00002346 case Primitive::kPrimChar:
2347 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002348 case Primitive::kPrimLong:
2349 // Type conversion from long to char is a result of code transformations.
2350 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2351 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002352 case Primitive::kPrimBoolean:
2353 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002354 case Primitive::kPrimByte:
2355 case Primitive::kPrimShort:
2356 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002357 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002359 break;
2360
2361 default:
2362 LOG(FATAL) << "Unexpected type conversion from " << input_type
2363 << " to " << result_type;
2364 }
2365 break;
2366
Roland Levillaindff1f282014-11-05 14:15:05 +00002367 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002368 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002369 case Primitive::kPrimBoolean:
2370 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002371 case Primitive::kPrimByte:
2372 case Primitive::kPrimShort:
2373 case Primitive::kPrimInt:
2374 case Primitive::kPrimChar: {
2375 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002376 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2377 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002378 break;
2379 }
2380
Roland Levillain5b3ee562015-04-14 16:02:41 +01002381 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002382 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002383 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2384 conversion,
2385 conversion->GetDexPc(),
2386 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002387 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002388 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002389
Roland Levillaincff13742014-11-17 14:32:17 +00002390 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002391 // Processing a Dex `double-to-float' instruction.
2392 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2393 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002394 break;
2395
2396 default:
2397 LOG(FATAL) << "Unexpected type conversion from " << input_type
2398 << " to " << result_type;
2399 };
2400 break;
2401
Roland Levillaindff1f282014-11-05 14:15:05 +00002402 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002403 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002404 case Primitive::kPrimBoolean:
2405 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002406 case Primitive::kPrimByte:
2407 case Primitive::kPrimShort:
2408 case Primitive::kPrimInt:
2409 case Primitive::kPrimChar: {
2410 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002411 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002412 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2413 out.AsFpuRegisterPairLow<SRegister>());
2414 break;
2415 }
2416
Roland Levillain647b9ed2014-11-27 12:06:00 +00002417 case Primitive::kPrimLong: {
2418 // Processing a Dex `long-to-double' instruction.
2419 Register low = in.AsRegisterPairLow<Register>();
2420 Register high = in.AsRegisterPairHigh<Register>();
2421 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2422 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002423 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002424 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002425 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2426 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002427
Roland Levillain682393c2015-04-14 15:57:52 +01002428 // temp_d = int-to-double(high)
2429 __ vmovsr(temp_s, high);
2430 __ vcvtdi(temp_d, temp_s);
2431 // constant_d = k2Pow32EncodingForDouble
2432 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2433 // out_d = unsigned-to-double(low)
2434 __ vmovsr(out_s, low);
2435 __ vcvtdu(out_d, out_s);
2436 // out_d += temp_d * constant_d
2437 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002438 break;
2439 }
2440
Roland Levillaincff13742014-11-17 14:32:17 +00002441 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002442 // Processing a Dex `float-to-double' instruction.
2443 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2444 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002445 break;
2446
2447 default:
2448 LOG(FATAL) << "Unexpected type conversion from " << input_type
2449 << " to " << result_type;
2450 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 }
2457}
2458
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002459void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002460 LocationSummary* locations =
2461 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002462 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002463 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002464 locations->SetInAt(0, Location::RequiresRegister());
2465 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2467 break;
2468 }
2469
2470 case Primitive::kPrimLong: {
2471 locations->SetInAt(0, Location::RequiresRegister());
2472 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002474 break;
2475 }
2476
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002477 case Primitive::kPrimFloat:
2478 case Primitive::kPrimDouble: {
2479 locations->SetInAt(0, Location::RequiresFpuRegister());
2480 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002481 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002482 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002483 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002484
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002485 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002486 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002487 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002488}
2489
2490void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2491 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002492 Location out = locations->Out();
2493 Location first = locations->InAt(0);
2494 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002495 switch (add->GetResultType()) {
2496 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002497 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002498 __ add(out.AsRegister<Register>(),
2499 first.AsRegister<Register>(),
2500 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002501 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002502 __ AddConstant(out.AsRegister<Register>(),
2503 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002504 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002505 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002506 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002507
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002508 case Primitive::kPrimLong: {
2509 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002510 __ adds(out.AsRegisterPairLow<Register>(),
2511 first.AsRegisterPairLow<Register>(),
2512 ShifterOperand(second.AsRegisterPairLow<Register>()));
2513 __ adc(out.AsRegisterPairHigh<Register>(),
2514 first.AsRegisterPairHigh<Register>(),
2515 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002516 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002517 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002518
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002519 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002520 __ vadds(out.AsFpuRegister<SRegister>(),
2521 first.AsFpuRegister<SRegister>(),
2522 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002523 break;
2524
2525 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002526 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2527 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2528 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002529 break;
2530
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002531 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002532 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002533 }
2534}
2535
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002536void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002537 LocationSummary* locations =
2538 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002539 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002540 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002541 locations->SetInAt(0, Location::RequiresRegister());
2542 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002543 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2544 break;
2545 }
2546
2547 case Primitive::kPrimLong: {
2548 locations->SetInAt(0, Location::RequiresRegister());
2549 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002550 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002551 break;
2552 }
Calin Juravle11351682014-10-23 15:38:15 +01002553 case Primitive::kPrimFloat:
2554 case Primitive::kPrimDouble: {
2555 locations->SetInAt(0, Location::RequiresFpuRegister());
2556 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002558 break;
Calin Juravle11351682014-10-23 15:38:15 +01002559 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002560 default:
Calin Juravle11351682014-10-23 15:38:15 +01002561 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002562 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002563}
2564
2565void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2566 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002567 Location out = locations->Out();
2568 Location first = locations->InAt(0);
2569 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002570 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002571 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002572 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002573 __ sub(out.AsRegister<Register>(),
2574 first.AsRegister<Register>(),
2575 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002576 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002577 __ AddConstant(out.AsRegister<Register>(),
2578 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002579 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002580 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002581 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002582 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002583
Calin Juravle11351682014-10-23 15:38:15 +01002584 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002585 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002586 __ subs(out.AsRegisterPairLow<Register>(),
2587 first.AsRegisterPairLow<Register>(),
2588 ShifterOperand(second.AsRegisterPairLow<Register>()));
2589 __ sbc(out.AsRegisterPairHigh<Register>(),
2590 first.AsRegisterPairHigh<Register>(),
2591 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002592 break;
Calin Juravle11351682014-10-23 15:38:15 +01002593 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002594
Calin Juravle11351682014-10-23 15:38:15 +01002595 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002596 __ vsubs(out.AsFpuRegister<SRegister>(),
2597 first.AsFpuRegister<SRegister>(),
2598 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002599 break;
Calin Juravle11351682014-10-23 15:38:15 +01002600 }
2601
2602 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002603 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2604 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2605 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002606 break;
2607 }
2608
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002609
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002610 default:
Calin Juravle11351682014-10-23 15:38:15 +01002611 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002612 }
2613}
2614
Calin Juravle34bacdf2014-10-07 20:23:36 +01002615void LocationsBuilderARM::VisitMul(HMul* mul) {
2616 LocationSummary* locations =
2617 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2618 switch (mul->GetResultType()) {
2619 case Primitive::kPrimInt:
2620 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002621 locations->SetInAt(0, Location::RequiresRegister());
2622 locations->SetInAt(1, Location::RequiresRegister());
2623 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002624 break;
2625 }
2626
Calin Juravleb5bfa962014-10-21 18:02:24 +01002627 case Primitive::kPrimFloat:
2628 case Primitive::kPrimDouble: {
2629 locations->SetInAt(0, Location::RequiresFpuRegister());
2630 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002631 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002632 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002633 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002634
2635 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002636 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002637 }
2638}
2639
2640void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2641 LocationSummary* locations = mul->GetLocations();
2642 Location out = locations->Out();
2643 Location first = locations->InAt(0);
2644 Location second = locations->InAt(1);
2645 switch (mul->GetResultType()) {
2646 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002647 __ mul(out.AsRegister<Register>(),
2648 first.AsRegister<Register>(),
2649 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002650 break;
2651 }
2652 case Primitive::kPrimLong: {
2653 Register out_hi = out.AsRegisterPairHigh<Register>();
2654 Register out_lo = out.AsRegisterPairLow<Register>();
2655 Register in1_hi = first.AsRegisterPairHigh<Register>();
2656 Register in1_lo = first.AsRegisterPairLow<Register>();
2657 Register in2_hi = second.AsRegisterPairHigh<Register>();
2658 Register in2_lo = second.AsRegisterPairLow<Register>();
2659
2660 // Extra checks to protect caused by the existence of R1_R2.
2661 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2662 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2663 DCHECK_NE(out_hi, in1_lo);
2664 DCHECK_NE(out_hi, in2_lo);
2665
2666 // input: in1 - 64 bits, in2 - 64 bits
2667 // output: out
2668 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2669 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2670 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2671
2672 // IP <- in1.lo * in2.hi
2673 __ mul(IP, in1_lo, in2_hi);
2674 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2675 __ mla(out_hi, in1_hi, in2_lo, IP);
2676 // out.lo <- (in1.lo * in2.lo)[31:0];
2677 __ umull(out_lo, IP, in1_lo, in2_lo);
2678 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2679 __ add(out_hi, out_hi, ShifterOperand(IP));
2680 break;
2681 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002682
2683 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002684 __ vmuls(out.AsFpuRegister<SRegister>(),
2685 first.AsFpuRegister<SRegister>(),
2686 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002687 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002688 }
2689
2690 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002691 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2692 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2693 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002694 break;
2695 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002696
2697 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002698 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002699 }
2700}
2701
Zheng Xuc6667102015-05-15 16:08:45 +08002702void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2703 DCHECK(instruction->IsDiv() || instruction->IsRem());
2704 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2705
2706 LocationSummary* locations = instruction->GetLocations();
2707 Location second = locations->InAt(1);
2708 DCHECK(second.IsConstant());
2709
2710 Register out = locations->Out().AsRegister<Register>();
2711 Register dividend = locations->InAt(0).AsRegister<Register>();
2712 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2713 DCHECK(imm == 1 || imm == -1);
2714
2715 if (instruction->IsRem()) {
2716 __ LoadImmediate(out, 0);
2717 } else {
2718 if (imm == 1) {
2719 __ Mov(out, dividend);
2720 } else {
2721 __ rsb(out, dividend, ShifterOperand(0));
2722 }
2723 }
2724}
2725
2726void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2727 DCHECK(instruction->IsDiv() || instruction->IsRem());
2728 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2729
2730 LocationSummary* locations = instruction->GetLocations();
2731 Location second = locations->InAt(1);
2732 DCHECK(second.IsConstant());
2733
2734 Register out = locations->Out().AsRegister<Register>();
2735 Register dividend = locations->InAt(0).AsRegister<Register>();
2736 Register temp = locations->GetTemp(0).AsRegister<Register>();
2737 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002738 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002739 int ctz_imm = CTZ(abs_imm);
2740
2741 if (ctz_imm == 1) {
2742 __ Lsr(temp, dividend, 32 - ctz_imm);
2743 } else {
2744 __ Asr(temp, dividend, 31);
2745 __ Lsr(temp, temp, 32 - ctz_imm);
2746 }
2747 __ add(out, temp, ShifterOperand(dividend));
2748
2749 if (instruction->IsDiv()) {
2750 __ Asr(out, out, ctz_imm);
2751 if (imm < 0) {
2752 __ rsb(out, out, ShifterOperand(0));
2753 }
2754 } else {
2755 __ ubfx(out, out, 0, ctz_imm);
2756 __ sub(out, out, ShifterOperand(temp));
2757 }
2758}
2759
2760void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2761 DCHECK(instruction->IsDiv() || instruction->IsRem());
2762 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2763
2764 LocationSummary* locations = instruction->GetLocations();
2765 Location second = locations->InAt(1);
2766 DCHECK(second.IsConstant());
2767
2768 Register out = locations->Out().AsRegister<Register>();
2769 Register dividend = locations->InAt(0).AsRegister<Register>();
2770 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2771 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2772 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2773
2774 int64_t magic;
2775 int shift;
2776 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2777
2778 __ LoadImmediate(temp1, magic);
2779 __ smull(temp2, temp1, dividend, temp1);
2780
2781 if (imm > 0 && magic < 0) {
2782 __ add(temp1, temp1, ShifterOperand(dividend));
2783 } else if (imm < 0 && magic > 0) {
2784 __ sub(temp1, temp1, ShifterOperand(dividend));
2785 }
2786
2787 if (shift != 0) {
2788 __ Asr(temp1, temp1, shift);
2789 }
2790
2791 if (instruction->IsDiv()) {
2792 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2793 } else {
2794 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2795 // TODO: Strength reduction for mls.
2796 __ LoadImmediate(temp2, imm);
2797 __ mls(out, temp1, temp2, dividend);
2798 }
2799}
2800
2801void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2802 DCHECK(instruction->IsDiv() || instruction->IsRem());
2803 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2804
2805 LocationSummary* locations = instruction->GetLocations();
2806 Location second = locations->InAt(1);
2807 DCHECK(second.IsConstant());
2808
2809 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2810 if (imm == 0) {
2811 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2812 } else if (imm == 1 || imm == -1) {
2813 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002814 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002815 DivRemByPowerOfTwo(instruction);
2816 } else {
2817 DCHECK(imm <= -2 || imm >= 2);
2818 GenerateDivRemWithAnyConstant(instruction);
2819 }
2820}
2821
Calin Juravle7c4954d2014-10-28 16:57:40 +00002822void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002823 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2824 if (div->GetResultType() == Primitive::kPrimLong) {
2825 // pLdiv runtime call.
2826 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002827 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2828 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002829 } else if (div->GetResultType() == Primitive::kPrimInt &&
2830 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2831 // pIdivmod runtime call.
2832 call_kind = LocationSummary::kCall;
2833 }
2834
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002835 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2836
Calin Juravle7c4954d2014-10-28 16:57:40 +00002837 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002838 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002839 if (div->InputAt(1)->IsConstant()) {
2840 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002841 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002843 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2844 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002845 // No temp register required.
2846 } else {
2847 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002848 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002849 locations->AddTemp(Location::RequiresRegister());
2850 }
2851 }
2852 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002853 locations->SetInAt(0, Location::RequiresRegister());
2854 locations->SetInAt(1, Location::RequiresRegister());
2855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2856 } else {
2857 InvokeRuntimeCallingConvention calling_convention;
2858 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2859 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2860 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2861 // we only need the former.
2862 locations->SetOut(Location::RegisterLocation(R0));
2863 }
Calin Juravled0d48522014-11-04 16:40:20 +00002864 break;
2865 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002866 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002867 InvokeRuntimeCallingConvention calling_convention;
2868 locations->SetInAt(0, Location::RegisterPairLocation(
2869 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2870 locations->SetInAt(1, Location::RegisterPairLocation(
2871 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002872 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002873 break;
2874 }
2875 case Primitive::kPrimFloat:
2876 case Primitive::kPrimDouble: {
2877 locations->SetInAt(0, Location::RequiresFpuRegister());
2878 locations->SetInAt(1, Location::RequiresFpuRegister());
2879 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2880 break;
2881 }
2882
2883 default:
2884 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2885 }
2886}
2887
2888void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2889 LocationSummary* locations = div->GetLocations();
2890 Location out = locations->Out();
2891 Location first = locations->InAt(0);
2892 Location second = locations->InAt(1);
2893
2894 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002895 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002896 if (second.IsConstant()) {
2897 GenerateDivRemConstantIntegral(div);
2898 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002899 __ sdiv(out.AsRegister<Register>(),
2900 first.AsRegister<Register>(),
2901 second.AsRegister<Register>());
2902 } else {
2903 InvokeRuntimeCallingConvention calling_convention;
2904 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2905 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2906 DCHECK_EQ(R0, out.AsRegister<Register>());
2907
2908 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002909 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002910 }
Calin Juravled0d48522014-11-04 16:40:20 +00002911 break;
2912 }
2913
Calin Juravle7c4954d2014-10-28 16:57:40 +00002914 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002915 InvokeRuntimeCallingConvention calling_convention;
2916 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2917 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2918 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2919 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2920 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002921 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002922
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002923 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002924 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002925 break;
2926 }
2927
2928 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002929 __ vdivs(out.AsFpuRegister<SRegister>(),
2930 first.AsFpuRegister<SRegister>(),
2931 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002932 break;
2933 }
2934
2935 case Primitive::kPrimDouble: {
2936 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2937 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2938 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2939 break;
2940 }
2941
2942 default:
2943 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2944 }
2945}
2946
Calin Juravlebacfec32014-11-14 15:54:36 +00002947void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002948 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002949
2950 // Most remainders are implemented in the runtime.
2951 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002952 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2953 // sdiv will be replaced by other instruction sequence.
2954 call_kind = LocationSummary::kNoCall;
2955 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2956 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002957 // Have hardware divide instruction for int, do it with three instructions.
2958 call_kind = LocationSummary::kNoCall;
2959 }
2960
Calin Juravlebacfec32014-11-14 15:54:36 +00002961 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2962
Calin Juravled2ec87d2014-12-08 14:24:46 +00002963 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002964 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002965 if (rem->InputAt(1)->IsConstant()) {
2966 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002967 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002969 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
2970 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002971 // No temp register required.
2972 } else {
2973 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002974 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002975 locations->AddTemp(Location::RequiresRegister());
2976 }
2977 }
2978 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002979 locations->SetInAt(0, Location::RequiresRegister());
2980 locations->SetInAt(1, Location::RequiresRegister());
2981 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2982 locations->AddTemp(Location::RequiresRegister());
2983 } else {
2984 InvokeRuntimeCallingConvention calling_convention;
2985 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2986 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2987 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2988 // we only need the latter.
2989 locations->SetOut(Location::RegisterLocation(R1));
2990 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002991 break;
2992 }
2993 case Primitive::kPrimLong: {
2994 InvokeRuntimeCallingConvention calling_convention;
2995 locations->SetInAt(0, Location::RegisterPairLocation(
2996 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2997 locations->SetInAt(1, Location::RegisterPairLocation(
2998 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2999 // The runtime helper puts the output in R2,R3.
3000 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3001 break;
3002 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003003 case Primitive::kPrimFloat: {
3004 InvokeRuntimeCallingConvention calling_convention;
3005 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3006 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3007 locations->SetOut(Location::FpuRegisterLocation(S0));
3008 break;
3009 }
3010
Calin Juravlebacfec32014-11-14 15:54:36 +00003011 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003012 InvokeRuntimeCallingConvention calling_convention;
3013 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3014 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3015 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3016 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3017 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003018 break;
3019 }
3020
3021 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003022 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003023 }
3024}
3025
3026void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3027 LocationSummary* locations = rem->GetLocations();
3028 Location out = locations->Out();
3029 Location first = locations->InAt(0);
3030 Location second = locations->InAt(1);
3031
Calin Juravled2ec87d2014-12-08 14:24:46 +00003032 Primitive::Type type = rem->GetResultType();
3033 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003034 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003035 if (second.IsConstant()) {
3036 GenerateDivRemConstantIntegral(rem);
3037 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003038 Register reg1 = first.AsRegister<Register>();
3039 Register reg2 = second.AsRegister<Register>();
3040 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003041
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003042 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003043 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003044 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003045 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003046 } else {
3047 InvokeRuntimeCallingConvention calling_convention;
3048 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3049 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3050 DCHECK_EQ(R1, out.AsRegister<Register>());
3051
3052 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003053 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003054 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003055 break;
3056 }
3057
3058 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003059 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003060 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003061 break;
3062 }
3063
Calin Juravled2ec87d2014-12-08 14:24:46 +00003064 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003065 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003066 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003067 break;
3068 }
3069
Calin Juravlebacfec32014-11-14 15:54:36 +00003070 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003071 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003072 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003073 break;
3074 }
3075
3076 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003077 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003078 }
3079}
3080
Calin Juravled0d48522014-11-04 16:40:20 +00003081void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003082 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3083 ? LocationSummary::kCallOnSlowPath
3084 : LocationSummary::kNoCall;
3085 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003086 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003087 if (instruction->HasUses()) {
3088 locations->SetOut(Location::SameAsFirstInput());
3089 }
3090}
3091
3092void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003093 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003094 codegen_->AddSlowPath(slow_path);
3095
3096 LocationSummary* locations = instruction->GetLocations();
3097 Location value = locations->InAt(0);
3098
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003099 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003100 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003101 case Primitive::kPrimByte:
3102 case Primitive::kPrimChar:
3103 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003104 case Primitive::kPrimInt: {
3105 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003106 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003107 } else {
3108 DCHECK(value.IsConstant()) << value;
3109 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3110 __ b(slow_path->GetEntryLabel());
3111 }
3112 }
3113 break;
3114 }
3115 case Primitive::kPrimLong: {
3116 if (value.IsRegisterPair()) {
3117 __ orrs(IP,
3118 value.AsRegisterPairLow<Register>(),
3119 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3120 __ b(slow_path->GetEntryLabel(), EQ);
3121 } else {
3122 DCHECK(value.IsConstant()) << value;
3123 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3124 __ b(slow_path->GetEntryLabel());
3125 }
3126 }
3127 break;
3128 default:
3129 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3130 }
3131 }
Calin Juravled0d48522014-11-04 16:40:20 +00003132}
3133
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003134void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3135 Register in = locations->InAt(0).AsRegister<Register>();
3136 Location rhs = locations->InAt(1);
3137 Register out = locations->Out().AsRegister<Register>();
3138
3139 if (rhs.IsConstant()) {
3140 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3141 // so map all rotations to a +ve. equivalent in that range.
3142 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3143 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3144 if (rot) {
3145 // Rotate, mapping left rotations to right equivalents if necessary.
3146 // (e.g. left by 2 bits == right by 30.)
3147 __ Ror(out, in, rot);
3148 } else if (out != in) {
3149 __ Mov(out, in);
3150 }
3151 } else {
3152 __ Ror(out, in, rhs.AsRegister<Register>());
3153 }
3154}
3155
3156// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3157// rotates by swapping input regs (effectively rotating by the first 32-bits of
3158// a larger rotation) or flipping direction (thus treating larger right/left
3159// rotations as sub-word sized rotations in the other direction) as appropriate.
3160void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3161 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3162 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3163 Location rhs = locations->InAt(1);
3164 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3165 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3166
3167 if (rhs.IsConstant()) {
3168 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3169 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003170 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003171 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3172 // logic below to a simple pair of binary orr.
3173 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3174 if (rot >= kArmBitsPerWord) {
3175 rot -= kArmBitsPerWord;
3176 std::swap(in_reg_hi, in_reg_lo);
3177 }
3178 // Rotate, or mov to out for zero or word size rotations.
3179 if (rot != 0u) {
3180 __ Lsr(out_reg_hi, in_reg_hi, rot);
3181 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3182 __ Lsr(out_reg_lo, in_reg_lo, rot);
3183 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3184 } else {
3185 __ Mov(out_reg_lo, in_reg_lo);
3186 __ Mov(out_reg_hi, in_reg_hi);
3187 }
3188 } else {
3189 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3190 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3191 Label end;
3192 Label shift_by_32_plus_shift_right;
3193
3194 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3195 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3196 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3197 __ b(&shift_by_32_plus_shift_right, CC);
3198
3199 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3200 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3201 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3202 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3203 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3204 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3205 __ Lsr(shift_left, in_reg_hi, shift_right);
3206 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3207 __ b(&end);
3208
3209 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3210 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3211 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3212 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3213 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3214 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3215 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3216 __ Lsl(shift_right, in_reg_hi, shift_left);
3217 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3218
3219 __ Bind(&end);
3220 }
3221}
Roland Levillain22c49222016-03-18 14:04:28 +00003222
3223void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003224 LocationSummary* locations =
3225 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3226 switch (ror->GetResultType()) {
3227 case Primitive::kPrimInt: {
3228 locations->SetInAt(0, Location::RequiresRegister());
3229 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3231 break;
3232 }
3233 case Primitive::kPrimLong: {
3234 locations->SetInAt(0, Location::RequiresRegister());
3235 if (ror->InputAt(1)->IsConstant()) {
3236 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3237 } else {
3238 locations->SetInAt(1, Location::RequiresRegister());
3239 locations->AddTemp(Location::RequiresRegister());
3240 locations->AddTemp(Location::RequiresRegister());
3241 }
3242 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3243 break;
3244 }
3245 default:
3246 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3247 }
3248}
3249
Roland Levillain22c49222016-03-18 14:04:28 +00003250void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003251 LocationSummary* locations = ror->GetLocations();
3252 Primitive::Type type = ror->GetResultType();
3253 switch (type) {
3254 case Primitive::kPrimInt: {
3255 HandleIntegerRotate(locations);
3256 break;
3257 }
3258 case Primitive::kPrimLong: {
3259 HandleLongRotate(locations);
3260 break;
3261 }
3262 default:
3263 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003264 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003265 }
3266}
3267
Calin Juravle9aec02f2014-11-18 23:06:35 +00003268void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3269 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3270
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003271 LocationSummary* locations =
3272 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003273
3274 switch (op->GetResultType()) {
3275 case Primitive::kPrimInt: {
3276 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003277 if (op->InputAt(1)->IsConstant()) {
3278 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3279 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3280 } else {
3281 locations->SetInAt(1, Location::RequiresRegister());
3282 // Make the output overlap, as it will be used to hold the masked
3283 // second input.
3284 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3285 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003286 break;
3287 }
3288 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003289 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003290 if (op->InputAt(1)->IsConstant()) {
3291 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3292 // For simplicity, use kOutputOverlap even though we only require that low registers
3293 // don't clash with high registers which the register allocator currently guarantees.
3294 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3295 } else {
3296 locations->SetInAt(1, Location::RequiresRegister());
3297 locations->AddTemp(Location::RequiresRegister());
3298 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3299 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003300 break;
3301 }
3302 default:
3303 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3304 }
3305}
3306
3307void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3308 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3309
3310 LocationSummary* locations = op->GetLocations();
3311 Location out = locations->Out();
3312 Location first = locations->InAt(0);
3313 Location second = locations->InAt(1);
3314
3315 Primitive::Type type = op->GetResultType();
3316 switch (type) {
3317 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003318 Register out_reg = out.AsRegister<Register>();
3319 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003320 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003321 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003322 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003323 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003324 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003325 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003326 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003327 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003328 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003329 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003330 }
3331 } else {
3332 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003333 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003334 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003335 __ Mov(out_reg, first_reg);
3336 } else if (op->IsShl()) {
3337 __ Lsl(out_reg, first_reg, shift_value);
3338 } else if (op->IsShr()) {
3339 __ Asr(out_reg, first_reg, shift_value);
3340 } else {
3341 __ Lsr(out_reg, first_reg, shift_value);
3342 }
3343 }
3344 break;
3345 }
3346 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003347 Register o_h = out.AsRegisterPairHigh<Register>();
3348 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003349
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003350 Register high = first.AsRegisterPairHigh<Register>();
3351 Register low = first.AsRegisterPairLow<Register>();
3352
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003353 if (second.IsRegister()) {
3354 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003355
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003356 Register second_reg = second.AsRegister<Register>();
3357
3358 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003359 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003360 // Shift the high part
3361 __ Lsl(o_h, high, o_l);
3362 // Shift the low part and `or` what overflew on the high part
3363 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3364 __ Lsr(temp, low, temp);
3365 __ orr(o_h, o_h, ShifterOperand(temp));
3366 // If the shift is > 32 bits, override the high part
3367 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3368 __ it(PL);
3369 __ Lsl(o_h, low, temp, PL);
3370 // Shift the low part
3371 __ Lsl(o_l, low, o_l);
3372 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003373 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003374 // Shift the low part
3375 __ Lsr(o_l, low, o_h);
3376 // Shift the high part and `or` what underflew on the low part
3377 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3378 __ Lsl(temp, high, temp);
3379 __ orr(o_l, o_l, ShifterOperand(temp));
3380 // If the shift is > 32 bits, override the low part
3381 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3382 __ it(PL);
3383 __ Asr(o_l, high, temp, PL);
3384 // Shift the high part
3385 __ Asr(o_h, high, o_h);
3386 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003387 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003388 // same as Shr except we use `Lsr`s and not `Asr`s
3389 __ Lsr(o_l, low, o_h);
3390 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3391 __ Lsl(temp, high, temp);
3392 __ orr(o_l, o_l, ShifterOperand(temp));
3393 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3394 __ it(PL);
3395 __ Lsr(o_l, high, temp, PL);
3396 __ Lsr(o_h, high, o_h);
3397 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003398 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003399 // Register allocator doesn't create partial overlap.
3400 DCHECK_NE(o_l, high);
3401 DCHECK_NE(o_h, low);
3402 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003403 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003404 if (shift_value > 32) {
3405 if (op->IsShl()) {
3406 __ Lsl(o_h, low, shift_value - 32);
3407 __ LoadImmediate(o_l, 0);
3408 } else if (op->IsShr()) {
3409 __ Asr(o_l, high, shift_value - 32);
3410 __ Asr(o_h, high, 31);
3411 } else {
3412 __ Lsr(o_l, high, shift_value - 32);
3413 __ LoadImmediate(o_h, 0);
3414 }
3415 } else if (shift_value == 32) {
3416 if (op->IsShl()) {
3417 __ mov(o_h, ShifterOperand(low));
3418 __ LoadImmediate(o_l, 0);
3419 } else if (op->IsShr()) {
3420 __ mov(o_l, ShifterOperand(high));
3421 __ Asr(o_h, high, 31);
3422 } else {
3423 __ mov(o_l, ShifterOperand(high));
3424 __ LoadImmediate(o_h, 0);
3425 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003426 } else if (shift_value == 1) {
3427 if (op->IsShl()) {
3428 __ Lsls(o_l, low, 1);
3429 __ adc(o_h, high, ShifterOperand(high));
3430 } else if (op->IsShr()) {
3431 __ Asrs(o_h, high, 1);
3432 __ Rrx(o_l, low);
3433 } else {
3434 __ Lsrs(o_h, high, 1);
3435 __ Rrx(o_l, low);
3436 }
3437 } else {
3438 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003439 if (op->IsShl()) {
3440 __ Lsl(o_h, high, shift_value);
3441 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3442 __ Lsl(o_l, low, shift_value);
3443 } else if (op->IsShr()) {
3444 __ Lsr(o_l, low, shift_value);
3445 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3446 __ Asr(o_h, high, shift_value);
3447 } else {
3448 __ Lsr(o_l, low, shift_value);
3449 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3450 __ Lsr(o_h, high, shift_value);
3451 }
3452 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003453 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003454 break;
3455 }
3456 default:
3457 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003458 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003459 }
3460}
3461
3462void LocationsBuilderARM::VisitShl(HShl* shl) {
3463 HandleShift(shl);
3464}
3465
3466void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3467 HandleShift(shl);
3468}
3469
3470void LocationsBuilderARM::VisitShr(HShr* shr) {
3471 HandleShift(shr);
3472}
3473
3474void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3475 HandleShift(shr);
3476}
3477
3478void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3479 HandleShift(ushr);
3480}
3481
3482void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3483 HandleShift(ushr);
3484}
3485
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003486void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003487 LocationSummary* locations =
3488 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
David Brazdil6de19382016-01-08 17:37:10 +00003489 if (instruction->IsStringAlloc()) {
3490 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3491 } else {
3492 InvokeRuntimeCallingConvention calling_convention;
3493 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3494 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3495 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003496 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003497}
3498
3499void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003500 // Note: if heap poisoning is enabled, the entry point takes cares
3501 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003502 if (instruction->IsStringAlloc()) {
3503 // String is allocated through StringFactory. Call NewEmptyString entry point.
3504 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3505 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3506 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3507 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3508 __ blx(LR);
3509 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3510 } else {
3511 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3512 instruction,
3513 instruction->GetDexPc(),
3514 nullptr);
3515 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3516 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003517}
3518
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003519void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3520 LocationSummary* locations =
3521 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3522 InvokeRuntimeCallingConvention calling_convention;
3523 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003524 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003525 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003526 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003527}
3528
3529void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3530 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003531 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003532 // Note: if heap poisoning is enabled, the entry point takes cares
3533 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003534 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003535 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003536 instruction->GetDexPc(),
3537 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003538 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003539}
3540
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003541void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003542 LocationSummary* locations =
3543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003544 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3545 if (location.IsStackSlot()) {
3546 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3547 } else if (location.IsDoubleStackSlot()) {
3548 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003549 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003550 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003551}
3552
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003553void InstructionCodeGeneratorARM::VisitParameterValue(
3554 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003555 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003556}
3557
3558void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3559 LocationSummary* locations =
3560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3561 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3562}
3563
3564void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3565 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003566}
3567
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003568void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003569 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003570 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003571 locations->SetInAt(0, Location::RequiresRegister());
3572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003573}
3574
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003575void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3576 LocationSummary* locations = not_->GetLocations();
3577 Location out = locations->Out();
3578 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003579 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003580 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003581 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003582 break;
3583
3584 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003585 __ mvn(out.AsRegisterPairLow<Register>(),
3586 ShifterOperand(in.AsRegisterPairLow<Register>()));
3587 __ mvn(out.AsRegisterPairHigh<Register>(),
3588 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003589 break;
3590
3591 default:
3592 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3593 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003594}
3595
David Brazdil66d126e2015-04-03 16:02:44 +01003596void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3599 locations->SetInAt(0, Location::RequiresRegister());
3600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3601}
3602
3603void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003604 LocationSummary* locations = bool_not->GetLocations();
3605 Location out = locations->Out();
3606 Location in = locations->InAt(0);
3607 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3608}
3609
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003610void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003611 LocationSummary* locations =
3612 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003613 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003614 case Primitive::kPrimBoolean:
3615 case Primitive::kPrimByte:
3616 case Primitive::kPrimShort:
3617 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003618 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003619 case Primitive::kPrimLong: {
3620 locations->SetInAt(0, Location::RequiresRegister());
3621 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003622 // Output overlaps because it is written before doing the low comparison.
3623 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003624 break;
3625 }
3626 case Primitive::kPrimFloat:
3627 case Primitive::kPrimDouble: {
3628 locations->SetInAt(0, Location::RequiresFpuRegister());
3629 locations->SetInAt(1, Location::RequiresFpuRegister());
3630 locations->SetOut(Location::RequiresRegister());
3631 break;
3632 }
3633 default:
3634 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3635 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003636}
3637
3638void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003639 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003640 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003641 Location left = locations->InAt(0);
3642 Location right = locations->InAt(1);
3643
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003644 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003645 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003646 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003647 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003648 case Primitive::kPrimBoolean:
3649 case Primitive::kPrimByte:
3650 case Primitive::kPrimShort:
3651 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003652 case Primitive::kPrimInt: {
3653 __ LoadImmediate(out, 0);
3654 __ cmp(left.AsRegister<Register>(),
3655 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3656 less_cond = LT;
3657 break;
3658 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003659 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003660 __ cmp(left.AsRegisterPairHigh<Register>(),
3661 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003662 __ b(&less, LT);
3663 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003664 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003665 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003666 __ cmp(left.AsRegisterPairLow<Register>(),
3667 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003668 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003669 break;
3670 }
3671 case Primitive::kPrimFloat:
3672 case Primitive::kPrimDouble: {
3673 __ LoadImmediate(out, 0);
3674 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003675 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003676 } else {
3677 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3678 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3679 }
3680 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003681 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003682 break;
3683 }
3684 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003685 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003686 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003687 }
Aart Bika19616e2016-02-01 18:57:58 -08003688
Calin Juravleddb7df22014-11-25 20:56:51 +00003689 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003690 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003691
3692 __ Bind(&greater);
3693 __ LoadImmediate(out, 1);
3694 __ b(&done);
3695
3696 __ Bind(&less);
3697 __ LoadImmediate(out, -1);
3698
3699 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003700}
3701
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003702void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003703 LocationSummary* locations =
3704 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003705 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003706 locations->SetInAt(i, Location::Any());
3707 }
3708 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003709}
3710
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003711void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003712 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003713}
3714
Roland Levillainc9285912015-12-18 10:38:42 +00003715void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3716 // TODO (ported from quick): revisit ARM barrier kinds.
3717 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003718 switch (kind) {
3719 case MemBarrierKind::kAnyStore:
3720 case MemBarrierKind::kLoadAny:
3721 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003722 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003723 break;
3724 }
3725 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003726 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003727 break;
3728 }
3729 default:
3730 LOG(FATAL) << "Unexpected memory barrier " << kind;
3731 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003732 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003733}
3734
3735void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3736 uint32_t offset,
3737 Register out_lo,
3738 Register out_hi) {
3739 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003740 // Ensure `out_lo` is different from `addr`, so that loading
3741 // `offset` into `out_lo` does not clutter `addr`.
3742 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003743 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003744 __ add(IP, addr, ShifterOperand(out_lo));
3745 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003746 }
3747 __ ldrexd(out_lo, out_hi, addr);
3748}
3749
3750void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3751 uint32_t offset,
3752 Register value_lo,
3753 Register value_hi,
3754 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003755 Register temp2,
3756 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003757 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003758 if (offset != 0) {
3759 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003760 __ add(IP, addr, ShifterOperand(temp1));
3761 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003762 }
3763 __ Bind(&fail);
3764 // We need a load followed by store. (The address used in a STREX instruction must
3765 // be the same as the address in the most recently executed LDREX instruction.)
3766 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003768 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003769 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003770}
3771
3772void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3773 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3774
Nicolas Geoffray39468442014-09-02 15:17:15 +01003775 LocationSummary* locations =
3776 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003777 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003778
Calin Juravle52c48962014-12-16 17:02:57 +00003779 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003780 if (Primitive::IsFloatingPointType(field_type)) {
3781 locations->SetInAt(1, Location::RequiresFpuRegister());
3782 } else {
3783 locations->SetInAt(1, Location::RequiresRegister());
3784 }
3785
Calin Juravle52c48962014-12-16 17:02:57 +00003786 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003787 bool generate_volatile = field_info.IsVolatile()
3788 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003789 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003790 bool needs_write_barrier =
3791 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003792 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003793 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003794 if (needs_write_barrier) {
3795 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003796 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003797 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003798 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003799 // - registers need to be consecutive
3800 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003801 // We don't test for ARM yet, and the assertion makes sure that we
3802 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003803 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3804
3805 locations->AddTemp(Location::RequiresRegister());
3806 locations->AddTemp(Location::RequiresRegister());
3807 if (field_type == Primitive::kPrimDouble) {
3808 // For doubles we need two more registers to copy the value.
3809 locations->AddTemp(Location::RegisterLocation(R2));
3810 locations->AddTemp(Location::RegisterLocation(R3));
3811 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003812 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003813}
3814
Calin Juravle52c48962014-12-16 17:02:57 +00003815void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003816 const FieldInfo& field_info,
3817 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003818 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3819
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003820 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003821 Register base = locations->InAt(0).AsRegister<Register>();
3822 Location value = locations->InAt(1);
3823
3824 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003825 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003826 Primitive::Type field_type = field_info.GetFieldType();
3827 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003828 bool needs_write_barrier =
3829 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003830
3831 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003832 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003833 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003834
3835 switch (field_type) {
3836 case Primitive::kPrimBoolean:
3837 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003838 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003839 break;
3840 }
3841
3842 case Primitive::kPrimShort:
3843 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003844 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003845 break;
3846 }
3847
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003848 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003849 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003850 if (kPoisonHeapReferences && needs_write_barrier) {
3851 // Note that in the case where `value` is a null reference,
3852 // we do not enter this block, as a null reference does not
3853 // need poisoning.
3854 DCHECK_EQ(field_type, Primitive::kPrimNot);
3855 Register temp = locations->GetTemp(0).AsRegister<Register>();
3856 __ Mov(temp, value.AsRegister<Register>());
3857 __ PoisonHeapReference(temp);
3858 __ StoreToOffset(kStoreWord, temp, base, offset);
3859 } else {
3860 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3861 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862 break;
3863 }
3864
3865 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003866 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003867 GenerateWideAtomicStore(base, offset,
3868 value.AsRegisterPairLow<Register>(),
3869 value.AsRegisterPairHigh<Register>(),
3870 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003871 locations->GetTemp(1).AsRegister<Register>(),
3872 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003873 } else {
3874 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003875 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003876 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003877 break;
3878 }
3879
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003880 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003881 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003882 break;
3883 }
3884
3885 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003886 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003887 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003888 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3889 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3890
3891 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3892
3893 GenerateWideAtomicStore(base, offset,
3894 value_reg_lo,
3895 value_reg_hi,
3896 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003897 locations->GetTemp(3).AsRegister<Register>(),
3898 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003899 } else {
3900 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003901 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003902 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003903 break;
3904 }
3905
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003906 case Primitive::kPrimVoid:
3907 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003908 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003909 }
Calin Juravle52c48962014-12-16 17:02:57 +00003910
Calin Juravle77520bc2015-01-12 18:45:46 +00003911 // Longs and doubles are handled in the switch.
3912 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3913 codegen_->MaybeRecordImplicitNullCheck(instruction);
3914 }
3915
3916 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3917 Register temp = locations->GetTemp(0).AsRegister<Register>();
3918 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003919 codegen_->MarkGCCard(
3920 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003921 }
3922
Calin Juravle52c48962014-12-16 17:02:57 +00003923 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003924 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003925 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003926}
3927
Calin Juravle52c48962014-12-16 17:02:57 +00003928void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3929 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003930
3931 bool object_field_get_with_read_barrier =
3932 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003933 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003934 new (GetGraph()->GetArena()) LocationSummary(instruction,
3935 object_field_get_with_read_barrier ?
3936 LocationSummary::kCallOnSlowPath :
3937 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003938 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003939
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003940 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003941 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003942 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003943 // The output overlaps in case of volatile long: we don't want the
3944 // code generated by GenerateWideAtomicLoad to overwrite the
3945 // object's location. Likewise, in the case of an object field get
3946 // with read barriers enabled, we do not want the load to overwrite
3947 // the object's location, as we need it to emit the read barrier.
3948 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3949 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003950
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003951 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3952 locations->SetOut(Location::RequiresFpuRegister());
3953 } else {
3954 locations->SetOut(Location::RequiresRegister(),
3955 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3956 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003957 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00003958 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003959 // - registers need to be consecutive
3960 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003961 // We don't test for ARM yet, and the assertion makes sure that we
3962 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003963 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3964 locations->AddTemp(Location::RequiresRegister());
3965 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00003966 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3967 // We need a temporary register for the read barrier marking slow
3968 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
3969 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003970 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971}
3972
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003973Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3974 Opcode opcode) {
3975 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3976 if (constant->IsConstant() &&
3977 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3978 return Location::ConstantLocation(constant->AsConstant());
3979 }
3980 return Location::RequiresRegister();
3981}
3982
3983bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3984 Opcode opcode) {
3985 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3986 if (Primitive::Is64BitType(input_cst->GetType())) {
3987 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3988 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3989 } else {
3990 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3991 }
3992}
3993
3994bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3995 ShifterOperand so;
3996 ArmAssembler* assembler = codegen_->GetAssembler();
3997 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3998 return true;
3999 }
4000 Opcode neg_opcode = kNoOperand;
4001 switch (opcode) {
4002 case AND:
4003 neg_opcode = BIC;
4004 break;
4005 case ORR:
4006 neg_opcode = ORN;
4007 break;
4008 default:
4009 return false;
4010 }
4011 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4012}
4013
Calin Juravle52c48962014-12-16 17:02:57 +00004014void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4015 const FieldInfo& field_info) {
4016 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004017
Calin Juravle52c48962014-12-16 17:02:57 +00004018 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004019 Location base_loc = locations->InAt(0);
4020 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004021 Location out = locations->Out();
4022 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004023 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004024 Primitive::Type field_type = field_info.GetFieldType();
4025 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4026
4027 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004028 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004029 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004030 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004031
Roland Levillainc9285912015-12-18 10:38:42 +00004032 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004033 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004034 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004035
Roland Levillainc9285912015-12-18 10:38:42 +00004036 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004037 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004038 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004039
Roland Levillainc9285912015-12-18 10:38:42 +00004040 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004041 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004042 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004043
4044 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004045 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004046 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004047
4048 case Primitive::kPrimNot: {
4049 // /* HeapReference<Object> */ out = *(base + offset)
4050 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4051 Location temp_loc = locations->GetTemp(0);
4052 // Note that a potential implicit null check is handled in this
4053 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4054 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4055 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4056 if (is_volatile) {
4057 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4058 }
4059 } else {
4060 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4061 codegen_->MaybeRecordImplicitNullCheck(instruction);
4062 if (is_volatile) {
4063 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4064 }
4065 // If read barriers are enabled, emit read barriers other than
4066 // Baker's using a slow path (and also unpoison the loaded
4067 // reference, if heap poisoning is enabled).
4068 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4069 }
4070 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004071 }
4072
Roland Levillainc9285912015-12-18 10:38:42 +00004073 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004074 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004075 GenerateWideAtomicLoad(base, offset,
4076 out.AsRegisterPairLow<Register>(),
4077 out.AsRegisterPairHigh<Register>());
4078 } else {
4079 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4080 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004081 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004082
Roland Levillainc9285912015-12-18 10:38:42 +00004083 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004084 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004085 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004086
4087 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004088 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004089 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004090 Register lo = locations->GetTemp(0).AsRegister<Register>();
4091 Register hi = locations->GetTemp(1).AsRegister<Register>();
4092 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004093 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004094 __ vmovdrr(out_reg, lo, hi);
4095 } else {
4096 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004097 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004098 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004099 break;
4100 }
4101
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004103 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004104 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004105 }
Calin Juravle52c48962014-12-16 17:02:57 +00004106
Roland Levillainc9285912015-12-18 10:38:42 +00004107 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4108 // Potential implicit null checks, in the case of reference or
4109 // double fields, are handled in the previous switch statement.
4110 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004111 codegen_->MaybeRecordImplicitNullCheck(instruction);
4112 }
4113
Calin Juravle52c48962014-12-16 17:02:57 +00004114 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004115 if (field_type == Primitive::kPrimNot) {
4116 // Memory barriers, in the case of references, are also handled
4117 // in the previous switch statement.
4118 } else {
4119 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4120 }
Roland Levillain4d027112015-07-01 15:41:14 +01004121 }
Calin Juravle52c48962014-12-16 17:02:57 +00004122}
4123
4124void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4125 HandleFieldSet(instruction, instruction->GetFieldInfo());
4126}
4127
4128void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004129 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004130}
4131
4132void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4133 HandleFieldGet(instruction, instruction->GetFieldInfo());
4134}
4135
4136void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4137 HandleFieldGet(instruction, instruction->GetFieldInfo());
4138}
4139
4140void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4141 HandleFieldGet(instruction, instruction->GetFieldInfo());
4142}
4143
4144void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4145 HandleFieldGet(instruction, instruction->GetFieldInfo());
4146}
4147
4148void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4149 HandleFieldSet(instruction, instruction->GetFieldInfo());
4150}
4151
4152void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004153 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004154}
4155
Calin Juravlee460d1d2015-09-29 04:52:17 +01004156void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4157 HUnresolvedInstanceFieldGet* instruction) {
4158 FieldAccessCallingConventionARM calling_convention;
4159 codegen_->CreateUnresolvedFieldLocationSummary(
4160 instruction, instruction->GetFieldType(), calling_convention);
4161}
4162
4163void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4164 HUnresolvedInstanceFieldGet* instruction) {
4165 FieldAccessCallingConventionARM calling_convention;
4166 codegen_->GenerateUnresolvedFieldAccess(instruction,
4167 instruction->GetFieldType(),
4168 instruction->GetFieldIndex(),
4169 instruction->GetDexPc(),
4170 calling_convention);
4171}
4172
4173void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4174 HUnresolvedInstanceFieldSet* instruction) {
4175 FieldAccessCallingConventionARM calling_convention;
4176 codegen_->CreateUnresolvedFieldLocationSummary(
4177 instruction, instruction->GetFieldType(), calling_convention);
4178}
4179
4180void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4181 HUnresolvedInstanceFieldSet* instruction) {
4182 FieldAccessCallingConventionARM calling_convention;
4183 codegen_->GenerateUnresolvedFieldAccess(instruction,
4184 instruction->GetFieldType(),
4185 instruction->GetFieldIndex(),
4186 instruction->GetDexPc(),
4187 calling_convention);
4188}
4189
4190void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4191 HUnresolvedStaticFieldGet* instruction) {
4192 FieldAccessCallingConventionARM calling_convention;
4193 codegen_->CreateUnresolvedFieldLocationSummary(
4194 instruction, instruction->GetFieldType(), calling_convention);
4195}
4196
4197void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4198 HUnresolvedStaticFieldGet* instruction) {
4199 FieldAccessCallingConventionARM calling_convention;
4200 codegen_->GenerateUnresolvedFieldAccess(instruction,
4201 instruction->GetFieldType(),
4202 instruction->GetFieldIndex(),
4203 instruction->GetDexPc(),
4204 calling_convention);
4205}
4206
4207void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4208 HUnresolvedStaticFieldSet* instruction) {
4209 FieldAccessCallingConventionARM calling_convention;
4210 codegen_->CreateUnresolvedFieldLocationSummary(
4211 instruction, instruction->GetFieldType(), calling_convention);
4212}
4213
4214void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4215 HUnresolvedStaticFieldSet* instruction) {
4216 FieldAccessCallingConventionARM calling_convention;
4217 codegen_->GenerateUnresolvedFieldAccess(instruction,
4218 instruction->GetFieldType(),
4219 instruction->GetFieldIndex(),
4220 instruction->GetDexPc(),
4221 calling_convention);
4222}
4223
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004224void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004225 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4226 ? LocationSummary::kCallOnSlowPath
4227 : LocationSummary::kNoCall;
4228 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004229 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004230 if (instruction->HasUses()) {
4231 locations->SetOut(Location::SameAsFirstInput());
4232 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233}
4234
Calin Juravle2ae48182016-03-16 14:05:09 +00004235void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4236 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004237 return;
4238 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004239 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004240
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004241 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004242 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004243}
4244
Calin Juravle2ae48182016-03-16 14:05:09 +00004245void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004246 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004247 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004248
4249 LocationSummary* locations = instruction->GetLocations();
4250 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004251
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004252 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004253}
4254
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004255void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004256 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004257}
4258
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004259void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004260 bool object_array_get_with_read_barrier =
4261 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004262 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004263 new (GetGraph()->GetArena()) LocationSummary(instruction,
4264 object_array_get_with_read_barrier ?
4265 LocationSummary::kCallOnSlowPath :
4266 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004267 locations->SetInAt(0, Location::RequiresRegister());
4268 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004269 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4270 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4271 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004272 // The output overlaps in the case of an object array get with
4273 // read barriers enabled: we do not want the move to overwrite the
4274 // array's location, as we need it to emit the read barrier.
4275 locations->SetOut(
4276 Location::RequiresRegister(),
4277 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004278 }
Roland Levillainc9285912015-12-18 10:38:42 +00004279 // We need a temporary register for the read barrier marking slow
4280 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4281 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4282 locations->AddTemp(Location::RequiresRegister());
4283 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004284}
4285
4286void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4287 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004288 Location obj_loc = locations->InAt(0);
4289 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004290 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004291 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004292 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004293
Roland Levillainc9285912015-12-18 10:38:42 +00004294 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004295 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004296 case Primitive::kPrimBoolean: {
Roland Levillainc9285912015-12-18 10:38:42 +00004297 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004298 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004299 size_t offset =
4300 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004301 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4302 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004303 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004304 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4305 }
4306 break;
4307 }
4308
4309 case Primitive::kPrimByte: {
Roland Levillainc9285912015-12-18 10:38:42 +00004310 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004312 size_t offset =
4313 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004314 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4315 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004316 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004317 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4318 }
4319 break;
4320 }
4321
4322 case Primitive::kPrimShort: {
Roland Levillainc9285912015-12-18 10:38:42 +00004323 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004324 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004325 size_t offset =
4326 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004327 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4328 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004329 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004330 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4331 }
4332 break;
4333 }
4334
4335 case Primitive::kPrimChar: {
Roland Levillainc9285912015-12-18 10:38:42 +00004336 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004337 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004338 size_t offset =
4339 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004340 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4341 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004342 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004343 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4344 }
4345 break;
4346 }
4347
Roland Levillainc9285912015-12-18 10:38:42 +00004348 case Primitive::kPrimInt: {
Roland Levillainc9285912015-12-18 10:38:42 +00004349 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004351 size_t offset =
4352 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004353 __ LoadFromOffset(kLoadWord, out, obj, offset);
4354 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004355 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004356 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4357 }
4358 break;
4359 }
4360
Roland Levillainc9285912015-12-18 10:38:42 +00004361 case Primitive::kPrimNot: {
4362 static_assert(
4363 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4364 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004365 // /* HeapReference<Object> */ out =
4366 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4367 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4368 Location temp = locations->GetTemp(0);
4369 // Note that a potential implicit null check is handled in this
4370 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4371 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4372 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4373 } else {
4374 Register out = out_loc.AsRegister<Register>();
4375 if (index.IsConstant()) {
4376 size_t offset =
4377 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4378 __ LoadFromOffset(kLoadWord, out, obj, offset);
4379 codegen_->MaybeRecordImplicitNullCheck(instruction);
4380 // If read barriers are enabled, emit read barriers other than
4381 // Baker's using a slow path (and also unpoison the loaded
4382 // reference, if heap poisoning is enabled).
4383 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4384 } else {
4385 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4386 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4387 codegen_->MaybeRecordImplicitNullCheck(instruction);
4388 // If read barriers are enabled, emit read barriers other than
4389 // Baker's using a slow path (and also unpoison the loaded
4390 // reference, if heap poisoning is enabled).
4391 codegen_->MaybeGenerateReadBarrierSlow(
4392 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4393 }
4394 }
4395 break;
4396 }
4397
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004398 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004399 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004400 size_t offset =
4401 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004402 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004404 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004405 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004406 }
4407 break;
4408 }
4409
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004410 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004411 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004412 if (index.IsConstant()) {
4413 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004414 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004415 } else {
4416 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004417 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004418 }
4419 break;
4420 }
4421
4422 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004423 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004424 if (index.IsConstant()) {
4425 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004426 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004427 } else {
4428 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004429 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004430 }
4431 break;
4432 }
4433
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004434 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004435 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004436 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004437 }
Roland Levillain4d027112015-07-01 15:41:14 +01004438
4439 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004440 // Potential implicit null checks, in the case of reference
4441 // arrays, are handled in the previous switch statement.
4442 } else {
4443 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004444 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004445}
4446
4447void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004448 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004449
4450 bool needs_write_barrier =
4451 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004452 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4453 bool object_array_set_with_read_barrier =
4454 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004455
Nicolas Geoffray39468442014-09-02 15:17:15 +01004456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004457 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004458 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4459 LocationSummary::kCallOnSlowPath :
4460 LocationSummary::kNoCall);
4461
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004462 locations->SetInAt(0, Location::RequiresRegister());
4463 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4464 if (Primitive::IsFloatingPointType(value_type)) {
4465 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004466 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004467 locations->SetInAt(2, Location::RequiresRegister());
4468 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004469 if (needs_write_barrier) {
4470 // Temporary registers for the write barrier.
4471 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004472 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004473 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004474}
4475
4476void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4477 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004478 Location array_loc = locations->InAt(0);
4479 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004480 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004481 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004482 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004483 bool needs_write_barrier =
4484 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004485
4486 switch (value_type) {
4487 case Primitive::kPrimBoolean:
4488 case Primitive::kPrimByte: {
4489 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004490 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004491 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004492 size_t offset =
4493 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004494 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004495 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004496 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004497 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4498 }
4499 break;
4500 }
4501
4502 case Primitive::kPrimShort:
4503 case Primitive::kPrimChar: {
4504 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004505 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004506 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004507 size_t offset =
4508 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004509 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004510 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004511 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004512 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4513 }
4514 break;
4515 }
4516
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004518 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004519 Location value_loc = locations->InAt(2);
4520 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004521 Register source = value;
4522
4523 if (instruction->InputAt(2)->IsNullConstant()) {
4524 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004525 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004526 size_t offset =
4527 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004528 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004529 } else {
4530 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004531 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004532 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004533 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004534 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004535 DCHECK(!needs_write_barrier);
4536 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004537 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004538 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004539
4540 DCHECK(needs_write_barrier);
4541 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4542 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4543 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4544 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4545 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4546 Label done;
4547 SlowPathCode* slow_path = nullptr;
4548
Roland Levillain3b359c72015-11-17 19:35:12 +00004549 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004550 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4551 codegen_->AddSlowPath(slow_path);
4552 if (instruction->GetValueCanBeNull()) {
4553 Label non_zero;
4554 __ CompareAndBranchIfNonZero(value, &non_zero);
4555 if (index.IsConstant()) {
4556 size_t offset =
4557 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4558 __ StoreToOffset(kStoreWord, value, array, offset);
4559 } else {
4560 DCHECK(index.IsRegister()) << index;
4561 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4562 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4563 }
4564 codegen_->MaybeRecordImplicitNullCheck(instruction);
4565 __ b(&done);
4566 __ Bind(&non_zero);
4567 }
4568
Roland Levillain3b359c72015-11-17 19:35:12 +00004569 if (kEmitCompilerReadBarrier) {
4570 // When read barriers are enabled, the type checking
4571 // instrumentation requires two read barriers:
4572 //
4573 // __ Mov(temp2, temp1);
4574 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4575 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004576 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004577 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4578 //
4579 // // /* HeapReference<Class> */ temp2 = value->klass_
4580 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004581 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004582 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4583 //
4584 // __ cmp(temp1, ShifterOperand(temp2));
4585 //
4586 // However, the second read barrier may trash `temp`, as it
4587 // is a temporary register, and as such would not be saved
4588 // along with live registers before calling the runtime (nor
4589 // restored afterwards). So in this case, we bail out and
4590 // delegate the work to the array set slow path.
4591 //
4592 // TODO: Extend the register allocator to support a new
4593 // "(locally) live temp" location so as to avoid always
4594 // going into the slow path when read barriers are enabled.
4595 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004596 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004597 // /* HeapReference<Class> */ temp1 = array->klass_
4598 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4599 codegen_->MaybeRecordImplicitNullCheck(instruction);
4600 __ MaybeUnpoisonHeapReference(temp1);
4601
4602 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4603 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4604 // /* HeapReference<Class> */ temp2 = value->klass_
4605 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4606 // If heap poisoning is enabled, no need to unpoison `temp1`
4607 // nor `temp2`, as we are comparing two poisoned references.
4608 __ cmp(temp1, ShifterOperand(temp2));
4609
4610 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4611 Label do_put;
4612 __ b(&do_put, EQ);
4613 // If heap poisoning is enabled, the `temp1` reference has
4614 // not been unpoisoned yet; unpoison it now.
4615 __ MaybeUnpoisonHeapReference(temp1);
4616
4617 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4618 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4619 // If heap poisoning is enabled, no need to unpoison
4620 // `temp1`, as we are comparing against null below.
4621 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4622 __ Bind(&do_put);
4623 } else {
4624 __ b(slow_path->GetEntryLabel(), NE);
4625 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004626 }
4627 }
4628
4629 if (kPoisonHeapReferences) {
4630 // Note that in the case where `value` is a null reference,
4631 // we do not enter this block, as a null reference does not
4632 // need poisoning.
4633 DCHECK_EQ(value_type, Primitive::kPrimNot);
4634 __ Mov(temp1, value);
4635 __ PoisonHeapReference(temp1);
4636 source = temp1;
4637 }
4638
4639 if (index.IsConstant()) {
4640 size_t offset =
4641 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4642 __ StoreToOffset(kStoreWord, source, array, offset);
4643 } else {
4644 DCHECK(index.IsRegister()) << index;
4645 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4646 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4647 }
4648
Roland Levillain3b359c72015-11-17 19:35:12 +00004649 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004650 codegen_->MaybeRecordImplicitNullCheck(instruction);
4651 }
4652
4653 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4654
4655 if (done.IsLinked()) {
4656 __ Bind(&done);
4657 }
4658
4659 if (slow_path != nullptr) {
4660 __ Bind(slow_path->GetExitLabel());
4661 }
4662
4663 break;
4664 }
4665
4666 case Primitive::kPrimInt: {
4667 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4668 Register value = locations->InAt(2).AsRegister<Register>();
4669 if (index.IsConstant()) {
4670 size_t offset =
4671 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4672 __ StoreToOffset(kStoreWord, value, array, offset);
4673 } else {
4674 DCHECK(index.IsRegister()) << index;
4675 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4676 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4677 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004678 break;
4679 }
4680
4681 case Primitive::kPrimLong: {
4682 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004683 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004684 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004685 size_t offset =
4686 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004687 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004688 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004689 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004690 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691 }
4692 break;
4693 }
4694
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004695 case Primitive::kPrimFloat: {
4696 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4697 Location value = locations->InAt(2);
4698 DCHECK(value.IsFpuRegister());
4699 if (index.IsConstant()) {
4700 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004701 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004702 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004703 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004704 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4705 }
4706 break;
4707 }
4708
4709 case Primitive::kPrimDouble: {
4710 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4711 Location value = locations->InAt(2);
4712 DCHECK(value.IsFpuRegisterPair());
4713 if (index.IsConstant()) {
4714 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004715 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004716 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004717 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004718 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4719 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004720
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004721 break;
4722 }
4723
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004724 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004725 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004726 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004727 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004728
Roland Levillain80e67092016-01-08 16:04:55 +00004729 // Objects are handled in the switch.
4730 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004731 codegen_->MaybeRecordImplicitNullCheck(instruction);
4732 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004733}
4734
4735void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004736 LocationSummary* locations =
4737 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004738 locations->SetInAt(0, Location::RequiresRegister());
4739 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004740}
4741
4742void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4743 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01004744 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004745 Register obj = locations->InAt(0).AsRegister<Register>();
4746 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004747 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004748 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004749}
4750
4751void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004752 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4753 ? LocationSummary::kCallOnSlowPath
4754 : LocationSummary::kNoCall;
4755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004756 locations->SetInAt(0, Location::RequiresRegister());
4757 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004758 if (instruction->HasUses()) {
4759 locations->SetOut(Location::SameAsFirstInput());
4760 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004761}
4762
4763void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4764 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004765 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004766 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004767 codegen_->AddSlowPath(slow_path);
4768
Roland Levillain271ab9c2014-11-27 15:23:57 +00004769 Register index = locations->InAt(0).AsRegister<Register>();
4770 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004771
4772 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004773 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004774}
4775
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004776void CodeGeneratorARM::MarkGCCard(Register temp,
4777 Register card,
4778 Register object,
4779 Register value,
4780 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004781 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004782 if (can_be_null) {
4783 __ CompareAndBranchIfZero(value, &is_null);
4784 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004785 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4786 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4787 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004788 if (can_be_null) {
4789 __ Bind(&is_null);
4790 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004791}
4792
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004793void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004794 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004795}
4796
4797void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004798 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4799}
4800
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004801void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4803}
4804
4805void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004806 HBasicBlock* block = instruction->GetBlock();
4807 if (block->GetLoopInformation() != nullptr) {
4808 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4809 // The back edge will generate the suspend check.
4810 return;
4811 }
4812 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4813 // The goto will generate the suspend check.
4814 return;
4815 }
4816 GenerateSuspendCheck(instruction, nullptr);
4817}
4818
4819void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4820 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004821 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004822 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4823 if (slow_path == nullptr) {
4824 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4825 instruction->SetSlowPath(slow_path);
4826 codegen_->AddSlowPath(slow_path);
4827 if (successor != nullptr) {
4828 DCHECK(successor->IsLoopHeader());
4829 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4830 }
4831 } else {
4832 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4833 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004834
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004835 __ LoadFromOffset(
4836 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004837 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004838 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004839 __ Bind(slow_path->GetReturnLabel());
4840 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004841 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004842 __ b(slow_path->GetEntryLabel());
4843 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004844}
4845
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004846ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4847 return codegen_->GetAssembler();
4848}
4849
4850void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004851 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004852 Location source = move->GetSource();
4853 Location destination = move->GetDestination();
4854
4855 if (source.IsRegister()) {
4856 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004857 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004858 } else if (destination.IsFpuRegister()) {
4859 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004860 } else {
4861 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004862 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004863 SP, destination.GetStackIndex());
4864 }
4865 } else if (source.IsStackSlot()) {
4866 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004867 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004868 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004869 } else if (destination.IsFpuRegister()) {
4870 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004871 } else {
4872 DCHECK(destination.IsStackSlot());
4873 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4874 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4875 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004876 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004877 if (destination.IsRegister()) {
4878 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
4879 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004880 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004881 } else {
4882 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004883 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4884 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004885 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004886 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004887 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4888 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004889 } else if (destination.IsRegisterPair()) {
4890 DCHECK(ExpectedPairLayout(destination));
4891 __ LoadFromOffset(
4892 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4893 } else {
4894 DCHECK(destination.IsFpuRegisterPair()) << destination;
4895 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4896 SP,
4897 source.GetStackIndex());
4898 }
4899 } else if (source.IsRegisterPair()) {
4900 if (destination.IsRegisterPair()) {
4901 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4902 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004903 } else if (destination.IsFpuRegisterPair()) {
4904 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4905 source.AsRegisterPairLow<Register>(),
4906 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004907 } else {
4908 DCHECK(destination.IsDoubleStackSlot()) << destination;
4909 DCHECK(ExpectedPairLayout(source));
4910 __ StoreToOffset(
4911 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4912 }
4913 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004914 if (destination.IsRegisterPair()) {
4915 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4916 destination.AsRegisterPairHigh<Register>(),
4917 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4918 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004919 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4920 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4921 } else {
4922 DCHECK(destination.IsDoubleStackSlot()) << destination;
4923 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4924 SP,
4925 destination.GetStackIndex());
4926 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004927 } else {
4928 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004929 HConstant* constant = source.GetConstant();
4930 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4931 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004932 if (destination.IsRegister()) {
4933 __ LoadImmediate(destination.AsRegister<Register>(), value);
4934 } else {
4935 DCHECK(destination.IsStackSlot());
4936 __ LoadImmediate(IP, value);
4937 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4938 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004939 } else if (constant->IsLongConstant()) {
4940 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004941 if (destination.IsRegisterPair()) {
4942 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4943 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004944 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004945 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004946 __ LoadImmediate(IP, Low32Bits(value));
4947 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4948 __ LoadImmediate(IP, High32Bits(value));
4949 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4950 }
4951 } else if (constant->IsDoubleConstant()) {
4952 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004953 if (destination.IsFpuRegisterPair()) {
4954 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004955 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004956 DCHECK(destination.IsDoubleStackSlot()) << destination;
4957 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004958 __ LoadImmediate(IP, Low32Bits(int_value));
4959 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4960 __ LoadImmediate(IP, High32Bits(int_value));
4961 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4962 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004963 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004964 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004965 float value = constant->AsFloatConstant()->GetValue();
4966 if (destination.IsFpuRegister()) {
4967 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4968 } else {
4969 DCHECK(destination.IsStackSlot());
4970 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4971 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4972 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004973 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004974 }
4975}
4976
4977void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4978 __ Mov(IP, reg);
4979 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4980 __ StoreToOffset(kStoreWord, IP, SP, mem);
4981}
4982
4983void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4984 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4985 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4986 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4987 SP, mem1 + stack_offset);
4988 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4989 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4990 SP, mem2 + stack_offset);
4991 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4992}
4993
4994void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004995 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004996 Location source = move->GetSource();
4997 Location destination = move->GetDestination();
4998
4999 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005000 DCHECK_NE(source.AsRegister<Register>(), IP);
5001 DCHECK_NE(destination.AsRegister<Register>(), IP);
5002 __ Mov(IP, source.AsRegister<Register>());
5003 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5004 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005005 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005006 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005007 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005008 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005009 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5010 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005011 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005012 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005013 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005014 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005015 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005016 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005017 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005018 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005019 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5020 destination.AsRegisterPairHigh<Register>(),
5021 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005022 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005023 Register low_reg = source.IsRegisterPair()
5024 ? source.AsRegisterPairLow<Register>()
5025 : destination.AsRegisterPairLow<Register>();
5026 int mem = source.IsRegisterPair()
5027 ? destination.GetStackIndex()
5028 : source.GetStackIndex();
5029 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005030 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005031 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005032 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005033 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005034 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5035 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005036 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005037 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005038 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005039 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5040 DRegister reg = source.IsFpuRegisterPair()
5041 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5042 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5043 int mem = source.IsFpuRegisterPair()
5044 ? destination.GetStackIndex()
5045 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005046 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005047 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005048 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005049 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5050 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5051 : destination.AsFpuRegister<SRegister>();
5052 int mem = source.IsFpuRegister()
5053 ? destination.GetStackIndex()
5054 : source.GetStackIndex();
5055
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005056 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005057 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005058 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005059 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005060 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5061 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005062 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005063 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005064 }
5065}
5066
5067void ParallelMoveResolverARM::SpillScratch(int reg) {
5068 __ Push(static_cast<Register>(reg));
5069}
5070
5071void ParallelMoveResolverARM::RestoreScratch(int reg) {
5072 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005073}
5074
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005075void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005076 InvokeRuntimeCallingConvention calling_convention;
5077 CodeGenerator::CreateLoadClassLocationSummary(
5078 cls,
5079 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005080 Location::RegisterLocation(R0),
5081 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005082}
5083
5084void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005085 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005086 if (cls->NeedsAccessCheck()) {
5087 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5088 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5089 cls,
5090 cls->GetDexPc(),
5091 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005092 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005093 return;
5094 }
5095
Roland Levillain3b359c72015-11-17 19:35:12 +00005096 Location out_loc = locations->Out();
5097 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005098 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005099
Calin Juravle580b6092015-10-06 17:35:58 +01005100 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005101 DCHECK(!cls->CanCallRuntime());
5102 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005103 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5104 GenerateGcRootFieldLoad(
5105 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005106 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005107 // /* GcRoot<mirror::Class>[] */ out =
5108 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005109 __ LoadFromOffset(kLoadWord,
5110 out,
5111 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005112 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005113 // /* GcRoot<mirror::Class> */ out = out[type_index]
5114 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005115
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005116 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5117 DCHECK(cls->CanCallRuntime());
5118 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5119 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5120 codegen_->AddSlowPath(slow_path);
5121 if (!cls->IsInDexCache()) {
5122 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5123 }
5124 if (cls->MustGenerateClinitCheck()) {
5125 GenerateClassInitializationCheck(slow_path, out);
5126 } else {
5127 __ Bind(slow_path->GetExitLabel());
5128 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005129 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005130 }
5131}
5132
5133void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5134 LocationSummary* locations =
5135 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5136 locations->SetInAt(0, Location::RequiresRegister());
5137 if (check->HasUses()) {
5138 locations->SetOut(Location::SameAsFirstInput());
5139 }
5140}
5141
5142void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005143 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005144 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005145 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005146 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005147 GenerateClassInitializationCheck(slow_path,
5148 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005149}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005150
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005151void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005152 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005153 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5154 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5155 __ b(slow_path->GetEntryLabel(), LT);
5156 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5157 // properly. Therefore, we do a memory fence.
5158 __ dmb(ISH);
5159 __ Bind(slow_path->GetExitLabel());
5160}
5161
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005162HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5163 HLoadString::LoadKind desired_string_load_kind) {
5164 if (kEmitCompilerReadBarrier) {
5165 switch (desired_string_load_kind) {
5166 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5167 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5168 case HLoadString::LoadKind::kBootImageAddress:
5169 // TODO: Implement for read barrier.
5170 return HLoadString::LoadKind::kDexCacheViaMethod;
5171 default:
5172 break;
5173 }
5174 }
5175 switch (desired_string_load_kind) {
5176 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5177 DCHECK(!GetCompilerOptions().GetCompilePic());
5178 break;
5179 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5180 DCHECK(GetCompilerOptions().GetCompilePic());
5181 break;
5182 case HLoadString::LoadKind::kBootImageAddress:
5183 break;
5184 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005185 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005186 break;
5187 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005188 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005189 // We disable pc-relative load when there is an irreducible loop, as the optimization
5190 // is incompatible with it.
5191 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5192 // with irreducible loops.
5193 if (GetGraph()->HasIrreducibleLoops()) {
5194 return HLoadString::LoadKind::kDexCacheViaMethod;
5195 }
5196 break;
5197 case HLoadString::LoadKind::kDexCacheViaMethod:
5198 break;
5199 }
5200 return desired_string_load_kind;
5201}
5202
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005203void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005204 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005205 ? LocationSummary::kCallOnSlowPath
5206 : LocationSummary::kNoCall;
5207 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005208 HLoadString::LoadKind load_kind = load->GetLoadKind();
5209 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
5210 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
5211 locations->SetInAt(0, Location::RequiresRegister());
5212 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005213 locations->SetOut(Location::RequiresRegister());
5214}
5215
5216void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005217 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005218 Location out_loc = locations->Out();
5219 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005220
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005221 switch (load->GetLoadKind()) {
5222 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
5223 DCHECK(!kEmitCompilerReadBarrier);
5224 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5225 load->GetStringIndex()));
5226 return; // No dex cache slow path.
5227 }
5228 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5229 DCHECK(!kEmitCompilerReadBarrier);
5230 CodeGeneratorARM::PcRelativePatchInfo* labels =
5231 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5232 __ BindTrackedLabel(&labels->movw_label);
5233 __ movw(out, /* placeholder */ 0u);
5234 __ BindTrackedLabel(&labels->movt_label);
5235 __ movt(out, /* placeholder */ 0u);
5236 __ BindTrackedLabel(&labels->add_pc_label);
5237 __ add(out, out, ShifterOperand(PC));
5238 return; // No dex cache slow path.
5239 }
5240 case HLoadString::LoadKind::kBootImageAddress: {
5241 DCHECK(!kEmitCompilerReadBarrier);
5242 DCHECK_NE(load->GetAddress(), 0u);
5243 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5244 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5245 return; // No dex cache slow path.
5246 }
5247 case HLoadString::LoadKind::kDexCacheAddress: {
5248 DCHECK_NE(load->GetAddress(), 0u);
5249 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5250 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5251 // a 128B range. To try and reduce the number of literals if we load multiple strings,
5252 // simply split the dex cache address to a 128B aligned base loaded from a literal
5253 // and the remaining offset embedded in the load.
5254 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
5255 DCHECK_ALIGNED(load->GetAddress(), 4u);
5256 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5257 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5258 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5259 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5260 GenerateGcRootFieldLoad(load, out_loc, out, offset);
5261 break;
5262 }
5263 case HLoadString::LoadKind::kDexCachePcRelative: {
5264 Register base_reg = locations->InAt(0).AsRegister<Register>();
5265 HArmDexCacheArraysBase* base = load->InputAt(0)->AsArmDexCacheArraysBase();
5266 int32_t offset = load->GetDexCacheElementOffset() - base->GetElementOffset();
5267 GenerateGcRootFieldLoad(load, out_loc, base_reg, offset);
5268 break;
5269 }
5270 case HLoadString::LoadKind::kDexCacheViaMethod: {
5271 Register current_method = locations->InAt(0).AsRegister<Register>();
5272
5273 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5274 GenerateGcRootFieldLoad(
5275 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
5276 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5277 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
5278 // /* GcRoot<mirror::String> */ out = out[string_index]
5279 GenerateGcRootFieldLoad(
5280 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
5281 break;
5282 }
5283 default:
5284 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
5285 UNREACHABLE();
5286 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005287
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005288 if (!load->IsInDexCache()) {
5289 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5290 codegen_->AddSlowPath(slow_path);
5291 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5292 __ Bind(slow_path->GetExitLabel());
5293 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005294}
5295
David Brazdilcb1c0552015-08-04 16:22:25 +01005296static int32_t GetExceptionTlsOffset() {
5297 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5298}
5299
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005300void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5301 LocationSummary* locations =
5302 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5303 locations->SetOut(Location::RequiresRegister());
5304}
5305
5306void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005307 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005308 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5309}
5310
5311void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5312 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5313}
5314
5315void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005316 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005317 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005318}
5319
5320void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5321 LocationSummary* locations =
5322 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5323 InvokeRuntimeCallingConvention calling_convention;
5324 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5325}
5326
5327void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5328 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005329 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005330 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005331}
5332
Roland Levillainc9285912015-12-18 10:38:42 +00005333static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5334 return kEmitCompilerReadBarrier &&
5335 (kUseBakerReadBarrier ||
5336 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5337 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5338 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5339}
5340
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005341void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005342 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005343 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5344 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005345 case TypeCheckKind::kExactCheck:
5346 case TypeCheckKind::kAbstractClassCheck:
5347 case TypeCheckKind::kClassHierarchyCheck:
5348 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005349 call_kind =
5350 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005351 break;
5352 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005353 case TypeCheckKind::kUnresolvedCheck:
5354 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005355 call_kind = LocationSummary::kCallOnSlowPath;
5356 break;
5357 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005358
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005360 locations->SetInAt(0, Location::RequiresRegister());
5361 locations->SetInAt(1, Location::RequiresRegister());
5362 // The "out" register is used as a temporary, so it overlaps with the inputs.
5363 // Note that TypeCheckSlowPathARM uses this register too.
5364 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5365 // When read barriers are enabled, we need a temporary register for
5366 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005367 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005368 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005369 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005370}
5371
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005372void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005373 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005374 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005375 Location obj_loc = locations->InAt(0);
5376 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005377 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005378 Location out_loc = locations->Out();
5379 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005380 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005381 locations->GetTemp(0) :
5382 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005383 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005384 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5385 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5386 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005387 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005388 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005389
5390 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005391 // avoid null check if we know obj is not null.
5392 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005393 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005394 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005395
Roland Levillain3b359c72015-11-17 19:35:12 +00005396 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005397 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005398
Roland Levillainc9285912015-12-18 10:38:42 +00005399 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005400 case TypeCheckKind::kExactCheck: {
5401 __ cmp(out, ShifterOperand(cls));
5402 // Classes must be equal for the instanceof to succeed.
5403 __ b(&zero, NE);
5404 __ LoadImmediate(out, 1);
5405 __ b(&done);
5406 break;
5407 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005408
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005409 case TypeCheckKind::kAbstractClassCheck: {
5410 // If the class is abstract, we eagerly fetch the super class of the
5411 // object to avoid doing a comparison we know will fail.
5412 Label loop;
5413 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005414 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005415 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005416 // If `out` is null, we use it for the result, and jump to `done`.
5417 __ CompareAndBranchIfZero(out, &done);
5418 __ cmp(out, ShifterOperand(cls));
5419 __ b(&loop, NE);
5420 __ LoadImmediate(out, 1);
5421 if (zero.IsLinked()) {
5422 __ b(&done);
5423 }
5424 break;
5425 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005426
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005427 case TypeCheckKind::kClassHierarchyCheck: {
5428 // Walk over the class hierarchy to find a match.
5429 Label loop, success;
5430 __ Bind(&loop);
5431 __ cmp(out, ShifterOperand(cls));
5432 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005433 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005434 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005435 __ CompareAndBranchIfNonZero(out, &loop);
5436 // If `out` is null, we use it for the result, and jump to `done`.
5437 __ b(&done);
5438 __ Bind(&success);
5439 __ LoadImmediate(out, 1);
5440 if (zero.IsLinked()) {
5441 __ b(&done);
5442 }
5443 break;
5444 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005445
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005446 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005447 // Do an exact check.
5448 Label exact_check;
5449 __ cmp(out, ShifterOperand(cls));
5450 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005451 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005452 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005453 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005454 // If `out` is null, we use it for the result, and jump to `done`.
5455 __ CompareAndBranchIfZero(out, &done);
5456 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5457 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5458 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005459 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005460 __ LoadImmediate(out, 1);
5461 __ b(&done);
5462 break;
5463 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005464
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005465 case TypeCheckKind::kArrayCheck: {
5466 __ cmp(out, ShifterOperand(cls));
5467 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005468 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5469 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005470 codegen_->AddSlowPath(slow_path);
5471 __ b(slow_path->GetEntryLabel(), NE);
5472 __ LoadImmediate(out, 1);
5473 if (zero.IsLinked()) {
5474 __ b(&done);
5475 }
5476 break;
5477 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005478
Calin Juravle98893e12015-10-02 21:05:03 +01005479 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005480 case TypeCheckKind::kInterfaceCheck: {
5481 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005482 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005483 // cases.
5484 //
5485 // We cannot directly call the InstanceofNonTrivial runtime
5486 // entry point without resorting to a type checking slow path
5487 // here (i.e. by calling InvokeRuntime directly), as it would
5488 // require to assign fixed registers for the inputs of this
5489 // HInstanceOf instruction (following the runtime calling
5490 // convention), which might be cluttered by the potential first
5491 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005492 //
5493 // TODO: Introduce a new runtime entry point taking the object
5494 // to test (instead of its class) as argument, and let it deal
5495 // with the read barrier issues. This will let us refactor this
5496 // case of the `switch` code as it was previously (with a direct
5497 // call to the runtime not using a type checking slow path).
5498 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005499 DCHECK(locations->OnlyCallsOnSlowPath());
5500 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5501 /* is_fatal */ false);
5502 codegen_->AddSlowPath(slow_path);
5503 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005504 if (zero.IsLinked()) {
5505 __ b(&done);
5506 }
5507 break;
5508 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005509 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005510
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005511 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005512 __ Bind(&zero);
5513 __ LoadImmediate(out, 0);
5514 }
5515
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005516 if (done.IsLinked()) {
5517 __ Bind(&done);
5518 }
5519
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005520 if (slow_path != nullptr) {
5521 __ Bind(slow_path->GetExitLabel());
5522 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005523}
5524
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005525void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005526 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5527 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5528
Roland Levillain3b359c72015-11-17 19:35:12 +00005529 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5530 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005531 case TypeCheckKind::kExactCheck:
5532 case TypeCheckKind::kAbstractClassCheck:
5533 case TypeCheckKind::kClassHierarchyCheck:
5534 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005535 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5536 LocationSummary::kCallOnSlowPath :
5537 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005538 break;
5539 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005540 case TypeCheckKind::kUnresolvedCheck:
5541 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005542 call_kind = LocationSummary::kCallOnSlowPath;
5543 break;
5544 }
5545
Roland Levillain3b359c72015-11-17 19:35:12 +00005546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5547 locations->SetInAt(0, Location::RequiresRegister());
5548 locations->SetInAt(1, Location::RequiresRegister());
5549 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5550 locations->AddTemp(Location::RequiresRegister());
5551 // When read barriers are enabled, we need an additional temporary
5552 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005553 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005554 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005555 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005556}
5557
5558void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005559 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005560 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005561 Location obj_loc = locations->InAt(0);
5562 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005563 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005564 Location temp_loc = locations->GetTemp(0);
5565 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005566 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005567 locations->GetTemp(1) :
5568 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005569 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005570 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5571 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5572 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005573
Roland Levillain3b359c72015-11-17 19:35:12 +00005574 bool is_type_check_slow_path_fatal =
5575 (type_check_kind == TypeCheckKind::kExactCheck ||
5576 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5577 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5578 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5579 !instruction->CanThrowIntoCatchBlock();
5580 SlowPathCode* type_check_slow_path =
5581 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5582 is_type_check_slow_path_fatal);
5583 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005584
5585 Label done;
5586 // Avoid null check if we know obj is not null.
5587 if (instruction->MustDoNullCheck()) {
5588 __ CompareAndBranchIfZero(obj, &done);
5589 }
5590
Roland Levillain3b359c72015-11-17 19:35:12 +00005591 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005592 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005593
Roland Levillain3b359c72015-11-17 19:35:12 +00005594 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005595 case TypeCheckKind::kExactCheck:
5596 case TypeCheckKind::kArrayCheck: {
5597 __ cmp(temp, ShifterOperand(cls));
5598 // Jump to slow path for throwing the exception or doing a
5599 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005600 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 break;
5602 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005603
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005604 case TypeCheckKind::kAbstractClassCheck: {
5605 // If the class is abstract, we eagerly fetch the super class of the
5606 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005607 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005608 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005609 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005610 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005611
5612 // If the class reference currently in `temp` is not null, jump
5613 // to the `compare_classes` label to compare it with the checked
5614 // class.
5615 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5616 // Otherwise, jump to the slow path to throw the exception.
5617 //
5618 // But before, move back the object's class into `temp` before
5619 // going into the slow path, as it has been overwritten in the
5620 // meantime.
5621 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005622 GenerateReferenceLoadTwoRegisters(
5623 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005624 __ b(type_check_slow_path->GetEntryLabel());
5625
5626 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005627 __ cmp(temp, ShifterOperand(cls));
5628 __ b(&loop, NE);
5629 break;
5630 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005631
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005632 case TypeCheckKind::kClassHierarchyCheck: {
5633 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005634 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005635 __ Bind(&loop);
5636 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005637 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005638
Roland Levillain3b359c72015-11-17 19:35:12 +00005639 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005640 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005641
5642 // If the class reference currently in `temp` is not null, jump
5643 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005645 // Otherwise, jump to the slow path to throw the exception.
5646 //
5647 // But before, move back the object's class into `temp` before
5648 // going into the slow path, as it has been overwritten in the
5649 // meantime.
5650 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005651 GenerateReferenceLoadTwoRegisters(
5652 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005653 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005654 break;
5655 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005656
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005657 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005658 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005659 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005660 __ cmp(temp, ShifterOperand(cls));
5661 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005662
5663 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005664 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005665 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005666
5667 // If the component type is not null (i.e. the object is indeed
5668 // an array), jump to label `check_non_primitive_component_type`
5669 // to further check that this component type is not a primitive
5670 // type.
5671 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5672 // Otherwise, jump to the slow path to throw the exception.
5673 //
5674 // But before, move back the object's class into `temp` before
5675 // going into the slow path, as it has been overwritten in the
5676 // meantime.
5677 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005678 GenerateReferenceLoadTwoRegisters(
5679 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005680 __ b(type_check_slow_path->GetEntryLabel());
5681
5682 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005683 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005684 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5685 __ CompareAndBranchIfZero(temp, &done);
5686 // Same comment as above regarding `temp` and the slow path.
5687 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005688 GenerateReferenceLoadTwoRegisters(
5689 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005690 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005691 break;
5692 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005693
Calin Juravle98893e12015-10-02 21:05:03 +01005694 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005695 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005696 // We always go into the type check slow path for the unresolved
5697 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005698 //
5699 // We cannot directly call the CheckCast runtime entry point
5700 // without resorting to a type checking slow path here (i.e. by
5701 // calling InvokeRuntime directly), as it would require to
5702 // assign fixed registers for the inputs of this HInstanceOf
5703 // instruction (following the runtime calling convention), which
5704 // might be cluttered by the potential first read barrier
5705 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005706 //
5707 // TODO: Introduce a new runtime entry point taking the object
5708 // to test (instead of its class) as argument, and let it deal
5709 // with the read barrier issues. This will let us refactor this
5710 // case of the `switch` code as it was previously (with a direct
5711 // call to the runtime not using a type checking slow path).
5712 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005713 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005714 break;
5715 }
5716 __ Bind(&done);
5717
Roland Levillain3b359c72015-11-17 19:35:12 +00005718 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005719}
5720
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005721void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5722 LocationSummary* locations =
5723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5724 InvokeRuntimeCallingConvention calling_convention;
5725 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5726}
5727
5728void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5729 codegen_->InvokeRuntime(instruction->IsEnter()
5730 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5731 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005732 instruction->GetDexPc(),
5733 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005734 if (instruction->IsEnter()) {
5735 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5736 } else {
5737 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5738 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005739}
5740
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005741void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5742void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5743void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005744
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005745void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005746 LocationSummary* locations =
5747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5748 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5749 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005750 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005751 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005752 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005754}
5755
5756void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5757 HandleBitwiseOperation(instruction);
5758}
5759
5760void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5761 HandleBitwiseOperation(instruction);
5762}
5763
5764void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5765 HandleBitwiseOperation(instruction);
5766}
5767
Artem Serov7fc63502016-02-09 17:15:29 +00005768
5769void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5770 LocationSummary* locations =
5771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5772 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5773 || instruction->GetResultType() == Primitive::kPrimLong);
5774
5775 locations->SetInAt(0, Location::RequiresRegister());
5776 locations->SetInAt(1, Location::RequiresRegister());
5777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5778}
5779
5780void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
5781 LocationSummary* locations = instruction->GetLocations();
5782 Location first = locations->InAt(0);
5783 Location second = locations->InAt(1);
5784 Location out = locations->Out();
5785
5786 if (instruction->GetResultType() == Primitive::kPrimInt) {
5787 Register first_reg = first.AsRegister<Register>();
5788 ShifterOperand second_reg(second.AsRegister<Register>());
5789 Register out_reg = out.AsRegister<Register>();
5790
5791 switch (instruction->GetOpKind()) {
5792 case HInstruction::kAnd:
5793 __ bic(out_reg, first_reg, second_reg);
5794 break;
5795 case HInstruction::kOr:
5796 __ orn(out_reg, first_reg, second_reg);
5797 break;
5798 // There is no EON on arm.
5799 case HInstruction::kXor:
5800 default:
5801 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
5802 UNREACHABLE();
5803 }
5804 return;
5805
5806 } else {
5807 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5808 Register first_low = first.AsRegisterPairLow<Register>();
5809 Register first_high = first.AsRegisterPairHigh<Register>();
5810 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5811 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5812 Register out_low = out.AsRegisterPairLow<Register>();
5813 Register out_high = out.AsRegisterPairHigh<Register>();
5814
5815 switch (instruction->GetOpKind()) {
5816 case HInstruction::kAnd:
5817 __ bic(out_low, first_low, second_low);
5818 __ bic(out_high, first_high, second_high);
5819 break;
5820 case HInstruction::kOr:
5821 __ orn(out_low, first_low, second_low);
5822 __ orn(out_high, first_high, second_high);
5823 break;
5824 // There is no EON on arm.
5825 case HInstruction::kXor:
5826 default:
5827 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
5828 UNREACHABLE();
5829 }
5830 }
5831}
5832
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005833void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5834 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5835 if (value == 0xffffffffu) {
5836 if (out != first) {
5837 __ mov(out, ShifterOperand(first));
5838 }
5839 return;
5840 }
5841 if (value == 0u) {
5842 __ mov(out, ShifterOperand(0));
5843 return;
5844 }
5845 ShifterOperand so;
5846 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5847 __ and_(out, first, so);
5848 } else {
5849 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5850 __ bic(out, first, ShifterOperand(~value));
5851 }
5852}
5853
5854void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5855 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5856 if (value == 0u) {
5857 if (out != first) {
5858 __ mov(out, ShifterOperand(first));
5859 }
5860 return;
5861 }
5862 if (value == 0xffffffffu) {
5863 __ mvn(out, ShifterOperand(0));
5864 return;
5865 }
5866 ShifterOperand so;
5867 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5868 __ orr(out, first, so);
5869 } else {
5870 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5871 __ orn(out, first, ShifterOperand(~value));
5872 }
5873}
5874
5875void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5876 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5877 if (value == 0u) {
5878 if (out != first) {
5879 __ mov(out, ShifterOperand(first));
5880 }
5881 return;
5882 }
5883 __ eor(out, first, ShifterOperand(value));
5884}
5885
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005886void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5887 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005888 Location first = locations->InAt(0);
5889 Location second = locations->InAt(1);
5890 Location out = locations->Out();
5891
5892 if (second.IsConstant()) {
5893 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5894 uint32_t value_low = Low32Bits(value);
5895 if (instruction->GetResultType() == Primitive::kPrimInt) {
5896 Register first_reg = first.AsRegister<Register>();
5897 Register out_reg = out.AsRegister<Register>();
5898 if (instruction->IsAnd()) {
5899 GenerateAndConst(out_reg, first_reg, value_low);
5900 } else if (instruction->IsOr()) {
5901 GenerateOrrConst(out_reg, first_reg, value_low);
5902 } else {
5903 DCHECK(instruction->IsXor());
5904 GenerateEorConst(out_reg, first_reg, value_low);
5905 }
5906 } else {
5907 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5908 uint32_t value_high = High32Bits(value);
5909 Register first_low = first.AsRegisterPairLow<Register>();
5910 Register first_high = first.AsRegisterPairHigh<Register>();
5911 Register out_low = out.AsRegisterPairLow<Register>();
5912 Register out_high = out.AsRegisterPairHigh<Register>();
5913 if (instruction->IsAnd()) {
5914 GenerateAndConst(out_low, first_low, value_low);
5915 GenerateAndConst(out_high, first_high, value_high);
5916 } else if (instruction->IsOr()) {
5917 GenerateOrrConst(out_low, first_low, value_low);
5918 GenerateOrrConst(out_high, first_high, value_high);
5919 } else {
5920 DCHECK(instruction->IsXor());
5921 GenerateEorConst(out_low, first_low, value_low);
5922 GenerateEorConst(out_high, first_high, value_high);
5923 }
5924 }
5925 return;
5926 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005927
5928 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005929 Register first_reg = first.AsRegister<Register>();
5930 ShifterOperand second_reg(second.AsRegister<Register>());
5931 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005932 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005933 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005934 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005935 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005936 } else {
5937 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005938 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005939 }
5940 } else {
5941 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005942 Register first_low = first.AsRegisterPairLow<Register>();
5943 Register first_high = first.AsRegisterPairHigh<Register>();
5944 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5945 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5946 Register out_low = out.AsRegisterPairLow<Register>();
5947 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005948 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005949 __ and_(out_low, first_low, second_low);
5950 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005951 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005952 __ orr(out_low, first_low, second_low);
5953 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005954 } else {
5955 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005956 __ eor(out_low, first_low, second_low);
5957 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005958 }
5959 }
5960}
5961
Roland Levillainc9285912015-12-18 10:38:42 +00005962void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5963 Location out,
5964 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005965 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005966 Register out_reg = out.AsRegister<Register>();
5967 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005968 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005969 if (kUseBakerReadBarrier) {
5970 // Load with fast path based Baker's read barrier.
5971 // /* HeapReference<Object> */ out = *(out + offset)
5972 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005973 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005974 } else {
5975 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005976 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00005977 // in the following move operation, as we will need it for the
5978 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005979 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00005980 // /* HeapReference<Object> */ out = *(out + offset)
5981 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005982 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00005983 }
5984 } else {
5985 // Plain load with no read barrier.
5986 // /* HeapReference<Object> */ out = *(out + offset)
5987 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5988 __ MaybeUnpoisonHeapReference(out_reg);
5989 }
5990}
5991
5992void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5993 Location out,
5994 Location obj,
5995 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005996 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005997 Register out_reg = out.AsRegister<Register>();
5998 Register obj_reg = obj.AsRegister<Register>();
5999 if (kEmitCompilerReadBarrier) {
6000 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006001 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006002 // Load with fast path based Baker's read barrier.
6003 // /* HeapReference<Object> */ out = *(obj + offset)
6004 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006005 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006006 } else {
6007 // Load with slow path based read barrier.
6008 // /* HeapReference<Object> */ out = *(obj + offset)
6009 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6010 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6011 }
6012 } else {
6013 // Plain load with no read barrier.
6014 // /* HeapReference<Object> */ out = *(obj + offset)
6015 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6016 __ MaybeUnpoisonHeapReference(out_reg);
6017 }
6018}
6019
6020void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6021 Location root,
6022 Register obj,
6023 uint32_t offset) {
6024 Register root_reg = root.AsRegister<Register>();
6025 if (kEmitCompilerReadBarrier) {
6026 if (kUseBakerReadBarrier) {
6027 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6028 // Baker's read barrier are used:
6029 //
6030 // root = obj.field;
6031 // if (Thread::Current()->GetIsGcMarking()) {
6032 // root = ReadBarrier::Mark(root)
6033 // }
6034
6035 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6036 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6037 static_assert(
6038 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6039 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6040 "have different sizes.");
6041 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6042 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6043 "have different sizes.");
6044
6045 // Slow path used to mark the GC root `root`.
6046 SlowPathCode* slow_path =
6047 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
6048 codegen_->AddSlowPath(slow_path);
6049
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006050 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006051 __ LoadFromOffset(
6052 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6053 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6054 __ Bind(slow_path->GetExitLabel());
6055 } else {
6056 // GC root loaded through a slow path for read barriers other
6057 // than Baker's.
6058 // /* GcRoot<mirror::Object>* */ root = obj + offset
6059 __ AddConstant(root_reg, obj, offset);
6060 // /* mirror::Object* */ root = root->Read()
6061 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6062 }
6063 } else {
6064 // Plain GC root load with no read barrier.
6065 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6066 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6067 // Note that GC roots are not affected by heap poisoning, thus we
6068 // do not have to unpoison `root_reg` here.
6069 }
6070}
6071
6072void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6073 Location ref,
6074 Register obj,
6075 uint32_t offset,
6076 Location temp,
6077 bool needs_null_check) {
6078 DCHECK(kEmitCompilerReadBarrier);
6079 DCHECK(kUseBakerReadBarrier);
6080
6081 // /* HeapReference<Object> */ ref = *(obj + offset)
6082 Location no_index = Location::NoLocation();
6083 GenerateReferenceLoadWithBakerReadBarrier(
6084 instruction, ref, obj, offset, no_index, temp, needs_null_check);
6085}
6086
6087void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6088 Location ref,
6089 Register obj,
6090 uint32_t data_offset,
6091 Location index,
6092 Location temp,
6093 bool needs_null_check) {
6094 DCHECK(kEmitCompilerReadBarrier);
6095 DCHECK(kUseBakerReadBarrier);
6096
6097 // /* HeapReference<Object> */ ref =
6098 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6099 GenerateReferenceLoadWithBakerReadBarrier(
6100 instruction, ref, obj, data_offset, index, temp, needs_null_check);
6101}
6102
6103void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6104 Location ref,
6105 Register obj,
6106 uint32_t offset,
6107 Location index,
6108 Location temp,
6109 bool needs_null_check) {
6110 DCHECK(kEmitCompilerReadBarrier);
6111 DCHECK(kUseBakerReadBarrier);
6112
6113 // In slow path based read barriers, the read barrier call is
6114 // inserted after the original load. However, in fast path based
6115 // Baker's read barriers, we need to perform the load of
6116 // mirror::Object::monitor_ *before* the original reference load.
6117 // This load-load ordering is required by the read barrier.
6118 // The fast path/slow path (for Baker's algorithm) should look like:
6119 //
6120 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6121 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6122 // HeapReference<Object> ref = *src; // Original reference load.
6123 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6124 // if (is_gray) {
6125 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6126 // }
6127 //
6128 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006129 // slightly more complex as it performs additional checks that we do
6130 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006131
6132 Register ref_reg = ref.AsRegister<Register>();
6133 Register temp_reg = temp.AsRegister<Register>();
6134 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6135
6136 // /* int32_t */ monitor = obj->monitor_
6137 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6138 if (needs_null_check) {
6139 MaybeRecordImplicitNullCheck(instruction);
6140 }
6141 // /* LockWord */ lock_word = LockWord(monitor)
6142 static_assert(sizeof(LockWord) == sizeof(int32_t),
6143 "art::LockWord and int32_t have different sizes.");
6144 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6145 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6146 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6147 static_assert(
6148 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6149 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6150
6151 // Introduce a dependency on the high bits of rb_state, which shall
6152 // be all zeroes, to prevent load-load reordering, and without using
6153 // a memory barrier (which would be more expensive).
6154 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6155 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6156 // obj is unchanged by this operation, but its value now depends on
6157 // IP, which depends on temp_reg.
6158 __ add(obj, obj, ShifterOperand(IP));
6159
6160 // The actual reference load.
6161 if (index.IsValid()) {
6162 static_assert(
6163 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6164 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6165 // /* HeapReference<Object> */ ref =
6166 // *(obj + offset + index * sizeof(HeapReference<Object>))
6167 if (index.IsConstant()) {
6168 size_t computed_offset =
6169 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6170 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6171 } else {
6172 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6173 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6174 }
6175 } else {
6176 // /* HeapReference<Object> */ ref = *(obj + offset)
6177 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6178 }
6179
6180 // Object* ref = ref_addr->AsMirrorPtr()
6181 __ MaybeUnpoisonHeapReference(ref_reg);
6182
6183 // Slow path used to mark the object `ref` when it is gray.
6184 SlowPathCode* slow_path =
6185 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6186 AddSlowPath(slow_path);
6187
6188 // if (rb_state == ReadBarrier::gray_ptr_)
6189 // ref = ReadBarrier::Mark(ref);
6190 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6191 __ b(slow_path->GetEntryLabel(), EQ);
6192 __ Bind(slow_path->GetExitLabel());
6193}
6194
6195void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6196 Location out,
6197 Location ref,
6198 Location obj,
6199 uint32_t offset,
6200 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006201 DCHECK(kEmitCompilerReadBarrier);
6202
Roland Levillainc9285912015-12-18 10:38:42 +00006203 // Insert a slow path based read barrier *after* the reference load.
6204 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006205 // If heap poisoning is enabled, the unpoisoning of the loaded
6206 // reference will be carried out by the runtime within the slow
6207 // path.
6208 //
6209 // Note that `ref` currently does not get unpoisoned (when heap
6210 // poisoning is enabled), which is alright as the `ref` argument is
6211 // not used by the artReadBarrierSlow entry point.
6212 //
6213 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6214 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6215 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6216 AddSlowPath(slow_path);
6217
Roland Levillain3b359c72015-11-17 19:35:12 +00006218 __ b(slow_path->GetEntryLabel());
6219 __ Bind(slow_path->GetExitLabel());
6220}
6221
Roland Levillainc9285912015-12-18 10:38:42 +00006222void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6223 Location out,
6224 Location ref,
6225 Location obj,
6226 uint32_t offset,
6227 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006228 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006229 // Baker's read barriers shall be handled by the fast path
6230 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6231 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006232 // If heap poisoning is enabled, unpoisoning will be taken care of
6233 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006234 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006235 } else if (kPoisonHeapReferences) {
6236 __ UnpoisonHeapReference(out.AsRegister<Register>());
6237 }
6238}
6239
Roland Levillainc9285912015-12-18 10:38:42 +00006240void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6241 Location out,
6242 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006243 DCHECK(kEmitCompilerReadBarrier);
6244
Roland Levillainc9285912015-12-18 10:38:42 +00006245 // Insert a slow path based read barrier *after* the GC root load.
6246 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006247 // Note that GC roots are not affected by heap poisoning, so we do
6248 // not need to do anything special for this here.
6249 SlowPathCode* slow_path =
6250 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6251 AddSlowPath(slow_path);
6252
Roland Levillain3b359c72015-11-17 19:35:12 +00006253 __ b(slow_path->GetEntryLabel());
6254 __ Bind(slow_path->GetExitLabel());
6255}
6256
Vladimir Markodc151b22015-10-15 18:02:30 +01006257HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6258 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6259 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006260 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6261 // We disable pc-relative load when there is an irreducible loop, as the optimization
6262 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006263 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6264 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006265 if (GetGraph()->HasIrreducibleLoops() &&
6266 (dispatch_info.method_load_kind ==
6267 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6268 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6269 }
6270
6271 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006272 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6273 if (&outer_dex_file != target_method.dex_file) {
6274 // Calls across dex files are more likely to exceed the available BL range,
6275 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6276 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6277 (desired_dispatch_info.method_load_kind ==
6278 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6279 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6280 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6281 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006282 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006283 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006284 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006285 0u
6286 };
6287 }
6288 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006289 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006290}
6291
Vladimir Markob4536b72015-11-24 13:45:23 +00006292Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6293 Register temp) {
6294 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6295 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6296 if (!invoke->GetLocations()->Intrinsified()) {
6297 return location.AsRegister<Register>();
6298 }
6299 // For intrinsics we allow any location, so it may be on the stack.
6300 if (!location.IsRegister()) {
6301 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6302 return temp;
6303 }
6304 // For register locations, check if the register was saved. If so, get it from the stack.
6305 // Note: There is a chance that the register was saved but not overwritten, so we could
6306 // save one load. However, since this is just an intrinsic slow path we prefer this
6307 // simple and more robust approach rather that trying to determine if that's the case.
6308 SlowPathCode* slow_path = GetCurrentSlowPath();
6309 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6310 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6311 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6312 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6313 return temp;
6314 }
6315 return location.AsRegister<Register>();
6316}
6317
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006318void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006319 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006320 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006321 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6322 // LR = code address from literal pool with link-time patch.
6323 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006324 break;
6325 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6326 // LR = invoke->GetDirectCodePtr();
6327 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006328 break;
6329 default:
6330 break;
6331 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006332
Vladimir Marko58155012015-08-19 12:49:41 +00006333 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6334 switch (invoke->GetMethodLoadKind()) {
6335 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6336 // temp = thread->string_init_entrypoint
6337 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6338 break;
6339 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006340 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006341 break;
6342 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6343 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6344 break;
6345 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6346 __ LoadLiteral(temp.AsRegister<Register>(),
6347 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6348 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006349 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6350 HArmDexCacheArraysBase* base =
6351 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6352 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6353 temp.AsRegister<Register>());
6354 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6355 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6356 break;
6357 }
Vladimir Marko58155012015-08-19 12:49:41 +00006358 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006359 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006360 Register method_reg;
6361 Register reg = temp.AsRegister<Register>();
6362 if (current_method.IsRegister()) {
6363 method_reg = current_method.AsRegister<Register>();
6364 } else {
6365 DCHECK(invoke->GetLocations()->Intrinsified());
6366 DCHECK(!current_method.IsValid());
6367 method_reg = reg;
6368 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6369 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006370 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6371 __ LoadFromOffset(kLoadWord,
6372 reg,
6373 method_reg,
6374 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006375 // temp = temp[index_in_cache];
6376 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6377 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006378 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6379 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006380 }
Vladimir Marko58155012015-08-19 12:49:41 +00006381 }
6382
6383 switch (invoke->GetCodePtrLocation()) {
6384 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6385 __ bl(GetFrameEntryLabel());
6386 break;
6387 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006388 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006389 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006390 // Arbitrarily branch to the BL itself, override at link time.
6391 __ bl(&relative_call_patches_.back().label);
6392 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006393 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6394 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6395 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006396 // LR()
6397 __ blx(LR);
6398 break;
6399 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6400 // LR = callee_method->entry_point_from_quick_compiled_code_
6401 __ LoadFromOffset(
6402 kLoadWord, LR, callee_method.AsRegister<Register>(),
6403 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6404 // LR()
6405 __ blx(LR);
6406 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006407 }
6408
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006409 DCHECK(!IsLeafMethod());
6410}
6411
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006412void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6413 Register temp = temp_location.AsRegister<Register>();
6414 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6415 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006416
6417 // Use the calling convention instead of the location of the receiver, as
6418 // intrinsics may have put the receiver in a different register. In the intrinsics
6419 // slow path, the arguments have been moved to the right place, so here we are
6420 // guaranteed that the receiver is the first register of the calling convention.
6421 InvokeDexCallingConvention calling_convention;
6422 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006423 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006424 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006425 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006426 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006427 // Instead of simply (possibly) unpoisoning `temp` here, we should
6428 // emit a read barrier for the previous class reference load.
6429 // However this is not required in practice, as this is an
6430 // intermediate/temporary reference and because the current
6431 // concurrent copying collector keeps the from-space memory
6432 // intact/accessible until the end of the marking phase (the
6433 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006434 __ MaybeUnpoisonHeapReference(temp);
6435 // temp = temp->GetMethodAt(method_offset);
6436 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6437 kArmWordSize).Int32Value();
6438 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6439 // LR = temp->GetEntryPoint();
6440 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6441 // LR();
6442 __ blx(LR);
6443}
6444
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006445CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6446 const DexFile& dex_file, uint32_t string_index) {
6447 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6448}
6449
6450CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6451 const DexFile& dex_file, uint32_t element_offset) {
6452 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6453}
6454
6455CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6456 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6457 patches->emplace_back(dex_file, offset_or_index);
6458 return &patches->back();
6459}
6460
6461Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6462 uint32_t string_index) {
6463 return boot_image_string_patches_.GetOrCreate(
6464 StringReference(&dex_file, string_index),
6465 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6466}
6467
6468Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6469 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6470 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6471 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6472}
6473
6474Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6475 return DeduplicateUint32Literal(address, &uint32_literals_);
6476}
6477
Vladimir Marko58155012015-08-19 12:49:41 +00006478void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6479 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006480 size_t size =
6481 method_patches_.size() +
6482 call_patches_.size() +
6483 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006484 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6485 boot_image_string_patches_.size() +
6486 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
6487 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006488 linker_patches->reserve(size);
6489 for (const auto& entry : method_patches_) {
6490 const MethodReference& target_method = entry.first;
6491 Literal* literal = entry.second;
6492 DCHECK(literal->GetLabel()->IsBound());
6493 uint32_t literal_offset = literal->GetLabel()->Position();
6494 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6495 target_method.dex_file,
6496 target_method.dex_method_index));
6497 }
6498 for (const auto& entry : call_patches_) {
6499 const MethodReference& target_method = entry.first;
6500 Literal* literal = entry.second;
6501 DCHECK(literal->GetLabel()->IsBound());
6502 uint32_t literal_offset = literal->GetLabel()->Position();
6503 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6504 target_method.dex_file,
6505 target_method.dex_method_index));
6506 }
6507 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6508 uint32_t literal_offset = info.label.Position();
6509 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6510 info.target_method.dex_file,
6511 info.target_method.dex_method_index));
6512 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006513 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6514 const DexFile& dex_file = info.target_dex_file;
6515 size_t base_element_offset = info.offset_or_index;
6516 DCHECK(info.add_pc_label.IsBound());
6517 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006518 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006519 DCHECK(info.movw_label.IsBound());
6520 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006521 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6522 &dex_file,
6523 add_pc_offset,
6524 base_element_offset));
6525 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006526 DCHECK(info.movt_label.IsBound());
6527 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006528 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6529 &dex_file,
6530 add_pc_offset,
6531 base_element_offset));
6532 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006533 for (const auto& entry : boot_image_string_patches_) {
6534 const StringReference& target_string = entry.first;
6535 Literal* literal = entry.second;
6536 DCHECK(literal->GetLabel()->IsBound());
6537 uint32_t literal_offset = literal->GetLabel()->Position();
6538 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6539 target_string.dex_file,
6540 target_string.string_index));
6541 }
6542 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6543 const DexFile& dex_file = info.target_dex_file;
6544 uint32_t string_index = info.offset_or_index;
6545 DCHECK(info.add_pc_label.IsBound());
6546 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6547 // Add MOVW patch.
6548 DCHECK(info.movw_label.IsBound());
6549 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6550 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6551 &dex_file,
6552 add_pc_offset,
6553 string_index));
6554 // Add MOVT patch.
6555 DCHECK(info.movt_label.IsBound());
6556 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6557 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6558 &dex_file,
6559 add_pc_offset,
6560 string_index));
6561 }
6562 for (const auto& entry : boot_image_address_patches_) {
6563 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
6564 Literal* literal = entry.second;
6565 DCHECK(literal->GetLabel()->IsBound());
6566 uint32_t literal_offset = literal->GetLabel()->Position();
6567 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
6568 }
6569}
6570
6571Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
6572 return map->GetOrCreate(
6573 value,
6574 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00006575}
6576
6577Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6578 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006579 return map->GetOrCreate(
6580 target_method,
6581 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00006582}
6583
6584Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6585 return DeduplicateMethodLiteral(target_method, &method_patches_);
6586}
6587
6588Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6589 return DeduplicateMethodLiteral(target_method, &call_patches_);
6590}
6591
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006592void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6593 LocationSummary* locations =
6594 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
6595 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
6596 Location::RequiresRegister());
6597 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
6598 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
6599 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6600}
6601
6602void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
6603 LocationSummary* locations = instr->GetLocations();
6604 Register res = locations->Out().AsRegister<Register>();
6605 Register accumulator =
6606 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
6607 Register mul_left =
6608 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
6609 Register mul_right =
6610 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
6611
6612 if (instr->GetOpKind() == HInstruction::kAdd) {
6613 __ mla(res, mul_left, mul_right, accumulator);
6614 } else {
6615 __ mls(res, mul_left, mul_right, accumulator);
6616 }
6617}
6618
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006619void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006620 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006621 LOG(FATAL) << "Unreachable";
6622}
6623
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006624void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006625 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006626 LOG(FATAL) << "Unreachable";
6627}
6628
Mark Mendellfe57faa2015-09-18 09:26:15 -04006629// Simple implementation of packed switch - generate cascaded compare/jumps.
6630void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6631 LocationSummary* locations =
6632 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6633 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006634 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006635 codegen_->GetAssembler()->IsThumb()) {
6636 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6637 if (switch_instr->GetStartValue() != 0) {
6638 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6639 }
6640 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006641}
6642
6643void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6644 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006645 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006646 LocationSummary* locations = switch_instr->GetLocations();
6647 Register value_reg = locations->InAt(0).AsRegister<Register>();
6648 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6649
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006650 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006651 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006652 Register temp_reg = IP;
6653 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6654 // the immediate, because IP is used as the destination register. For the other
6655 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6656 // and they can be encoded in the instruction without making use of IP register.
6657 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6658
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006659 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006660 // Jump to successors[0] if value == lower_bound.
6661 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6662 int32_t last_index = 0;
6663 for (; num_entries - last_index > 2; last_index += 2) {
6664 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6665 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6666 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6667 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6668 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6669 }
6670 if (num_entries - last_index == 2) {
6671 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006672 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006673 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006674 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006675
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006676 // And the default for any other value.
6677 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6678 __ b(codegen_->GetLabelOf(default_block));
6679 }
6680 } else {
6681 // Create a table lookup.
6682 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6683
6684 // Materialize a pointer to the switch table
6685 std::vector<Label*> labels(num_entries);
6686 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6687 for (uint32_t i = 0; i < num_entries; i++) {
6688 labels[i] = codegen_->GetLabelOf(successors[i]);
6689 }
6690 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6691
6692 // Remove the bias.
6693 Register key_reg;
6694 if (lower_bound != 0) {
6695 key_reg = locations->GetTemp(1).AsRegister<Register>();
6696 __ AddConstant(key_reg, value_reg, -lower_bound);
6697 } else {
6698 key_reg = value_reg;
6699 }
6700
6701 // Check whether the value is in the table, jump to default block if not.
6702 __ CmpConstant(key_reg, num_entries - 1);
6703 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6704
6705 // Load the displacement from the table.
6706 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6707
6708 // Dispatch is a direct add to the PC (for Thumb2).
6709 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006710 }
6711}
6712
Vladimir Markob4536b72015-11-24 13:45:23 +00006713void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6714 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6715 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00006716}
6717
6718void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6719 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006720 CodeGeneratorARM::PcRelativePatchInfo* labels =
6721 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00006722 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006723 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00006724 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006725 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00006726 __ BindTrackedLabel(&labels->add_pc_label);
6727 __ add(base_reg, base_reg, ShifterOperand(PC));
6728}
6729
Andreas Gampe85b62f22015-09-09 13:15:38 -07006730void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6731 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006732 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006733 return;
6734 }
6735
6736 DCHECK_NE(type, Primitive::kPrimVoid);
6737
6738 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6739 if (return_loc.Equals(trg)) {
6740 return;
6741 }
6742
6743 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6744 // with the last branch.
6745 if (type == Primitive::kPrimLong) {
6746 HParallelMove parallel_move(GetGraph()->GetArena());
6747 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6748 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6749 GetMoveResolver()->EmitNativeCode(&parallel_move);
6750 } else if (type == Primitive::kPrimDouble) {
6751 HParallelMove parallel_move(GetGraph()->GetArena());
6752 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6753 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6754 GetMoveResolver()->EmitNativeCode(&parallel_move);
6755 } else {
6756 // Let the parallel move resolver take care of all of this.
6757 HParallelMove parallel_move(GetGraph()->GetArena());
6758 parallel_move.AddMove(return_loc, trg, type, nullptr);
6759 GetMoveResolver()->EmitNativeCode(&parallel_move);
6760 }
6761}
6762
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006763void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
6764 LocationSummary* locations =
6765 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6766 locations->SetInAt(0, Location::RequiresRegister());
6767 locations->SetOut(Location::RequiresRegister());
6768}
6769
6770void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
6771 LocationSummary* locations = instruction->GetLocations();
6772 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006773 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006774 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6775 instruction->GetIndex(), kArmPointerSize).SizeValue();
6776 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03006777 __ LoadFromOffset(kLoadWord, locations->Out().AsRegister<Register>(),
6778 locations->InAt(0).AsRegister<Register>(),
6779 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
6780 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07006781 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006782 }
6783 __ LoadFromOffset(kLoadWord,
6784 locations->Out().AsRegister<Register>(),
6785 locations->InAt(0).AsRegister<Register>(),
6786 method_offset);
6787}
6788
Roland Levillain4d027112015-07-01 15:41:14 +01006789#undef __
6790#undef QUICK_ENTRY_POINT
6791
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006792} // namespace arm
6793} // namespace art