blob: 35c2c43fca77cde9c3aed8cc3dbef7fc19b2b606 [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
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000050// We unconditionally allocate R5 to ensure we can do long operations
51// with baseline.
52static constexpr Register kCoreSavedRegisterForBaseline = R5;
53static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070054 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000055static constexpr SRegister kFpuCalleeSaves[] =
56 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000058// D31 cannot be split into two S registers, and the register allocator only works on
59// S registers. Therefore there is no need to block it.
60static constexpr DRegister DTMP = D31;
61
Vladimir Markof3e0ee22015-12-17 15:23:13 +000062static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070063
Roland Levillain62a46b22015-06-01 18:24:13 +010064#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010065#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
Andreas Gampe85b62f22015-09-09 13:15:38 -070067class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070
Alexandre Rames67555f72014-11-18 10:55:16 +000071 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010072 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000074 if (instruction_->CanThrowIntoCatchBlock()) {
75 // Live registers will be restored in the catch block if caught.
76 SaveLiveRegisters(codegen, instruction_->GetLocations());
77 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010078 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000079 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000080 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 }
82
Alexandre Rames8158f282015-08-07 10:26:17 +010083 bool IsFatal() const OVERRIDE { return true; }
84
Alexandre Rames9931f312015-06-19 14:47:01 +010085 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
86
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010088 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
90};
91
Andreas Gampe85b62f22015-09-09 13:15:38 -070092class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000093 public:
94 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
95
Alexandre Rames67555f72014-11-18 10:55:16 +000096 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000097 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
98 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000099 if (instruction_->CanThrowIntoCatchBlock()) {
100 // Live registers will be restored in the catch block if caught.
101 SaveLiveRegisters(codegen, instruction_->GetLocations());
102 }
Calin Juravled0d48522014-11-04 16:40:20 +0000103 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000104 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000105 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000106 }
107
Alexandre Rames8158f282015-08-07 10:26:17 +0100108 bool IsFatal() const OVERRIDE { return true; }
109
Alexandre Rames9931f312015-06-19 14:47:01 +0100110 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
111
Calin Juravled0d48522014-11-04 16:40:20 +0000112 private:
113 HDivZeroCheck* const instruction_;
114 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
115};
116
Andreas Gampe85b62f22015-09-09 13:15:38 -0700117class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000119 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100120 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121
Alexandre Rames67555f72014-11-18 10:55:16 +0000122 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100123 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000124 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000125 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100126 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000127 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000128 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000129 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100130 if (successor_ == nullptr) {
131 __ b(GetReturnLabel());
132 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100134 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135 }
136
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100137 Label* GetReturnLabel() {
138 DCHECK(successor_ == nullptr);
139 return &return_label_;
140 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100142 HBasicBlock* GetSuccessor() const {
143 return successor_;
144 }
145
Alexandre Rames9931f312015-06-19 14:47:01 +0100146 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
147
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 private:
149 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100150 // If not null, the block to branch to after the suspend check.
151 HBasicBlock* const successor_;
152
153 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154 Label return_label_;
155
156 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
157};
158
Andreas Gampe85b62f22015-09-09 13:15:38 -0700159class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100161 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
162 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163
Alexandre Rames67555f72014-11-18 10:55:16 +0000164 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100165 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100166 LocationSummary* locations = instruction_->GetLocations();
167
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100168 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000169 if (instruction_->CanThrowIntoCatchBlock()) {
170 // Live registers will be restored in the catch block if caught.
171 SaveLiveRegisters(codegen, instruction_->GetLocations());
172 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000173 // We're moving two locations to locations that could overlap, so we need a parallel
174 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100175 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000176 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100177 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000178 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100179 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100180 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
182 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100183 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000184 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000185 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100186 }
187
Alexandre Rames8158f282015-08-07 10:26:17 +0100188 bool IsFatal() const OVERRIDE { return true; }
189
Alexandre Rames9931f312015-06-19 14:47:01 +0100190 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
191
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100193 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194
195 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
196};
197
Andreas Gampe85b62f22015-09-09 13:15:38 -0700198class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100199 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000200 LoadClassSlowPathARM(HLoadClass* cls,
201 HInstruction* at,
202 uint32_t dex_pc,
203 bool do_clinit)
204 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
205 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
206 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207
Alexandre Rames67555f72014-11-18 10:55:16 +0000208 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209 LocationSummary* locations = at_->GetLocations();
210
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
212 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000213 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100215 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000216 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 int32_t entry_point_offset = do_clinit_
218 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
219 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000220 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000221 if (do_clinit_) {
222 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
223 } else {
224 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
225 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226
227 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000228 Location out = locations->Out();
229 if (out.IsValid()) {
230 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
232 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000233 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234 __ b(GetExitLabel());
235 }
236
Alexandre Rames9931f312015-06-19 14:47:01 +0100237 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
238
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100239 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 // The class this slow path will load.
241 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100242
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 // The instruction where this slow path is happening.
244 // (Might be the load class or an initialization check).
245 HInstruction* const at_;
246
247 // The dex PC of `at_`.
248 const uint32_t dex_pc_;
249
250 // Whether to initialize the class.
251 const bool do_clinit_;
252
253 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100254};
255
Andreas Gampe85b62f22015-09-09 13:15:38 -0700256class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000257 public:
258 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
259
Alexandre Rames67555f72014-11-18 10:55:16 +0000260 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000261 LocationSummary* locations = instruction_->GetLocations();
262 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
263
264 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000267
268 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800269 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000270 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000271 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000272 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000273 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
274
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000275 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000276 __ b(GetExitLabel());
277 }
278
Alexandre Rames9931f312015-06-19 14:47:01 +0100279 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
280
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000281 private:
282 HLoadString* const instruction_;
283
284 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
285};
286
Andreas Gampe85b62f22015-09-09 13:15:38 -0700287class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000289 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
290 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100294 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
295 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 DCHECK(instruction_->IsCheckCast()
297 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
299 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
300 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000301
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302 if (!is_fatal_) {
303 SaveLiveRegisters(codegen, locations);
304 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305
306 // We're moving two locations to locations that could overlap, so we need a parallel
307 // move resolver.
308 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100310 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000311 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100312 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100313 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100314 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
315 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100318 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
319 instruction_,
320 instruction_->GetDexPc(),
321 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000322 CheckEntrypointTypes<
323 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000324 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
325 } else {
326 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100327 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
328 instruction_,
329 instruction_->GetDexPc(),
330 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000331 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334 if (!is_fatal_) {
335 RestoreLiveRegisters(codegen, locations);
336 __ b(GetExitLabel());
337 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338 }
339
Alexandre Rames9931f312015-06-19 14:47:01 +0100340 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
341
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000342 bool IsFatal() const OVERRIDE { return is_fatal_; }
343
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000344 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347
348 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
349};
350
Andreas Gampe85b62f22015-09-09 13:15:38 -0700351class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700352 public:
353 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
354 : instruction_(instruction) {}
355
356 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
357 __ Bind(GetEntryLabel());
358 SaveLiveRegisters(codegen, instruction_->GetLocations());
359 DCHECK(instruction_->IsDeoptimize());
360 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
361 uint32_t dex_pc = deoptimize->GetDexPc();
362 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
363 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000364 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 }
366
Alexandre Rames9931f312015-06-19 14:47:01 +0100367 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
368
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 private:
370 HInstruction* const instruction_;
371 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
372};
373
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100374class ArraySetSlowPathARM : public SlowPathCode {
375 public:
376 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
377
378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
379 LocationSummary* locations = instruction_->GetLocations();
380 __ Bind(GetEntryLabel());
381 SaveLiveRegisters(codegen, locations);
382
383 InvokeRuntimeCallingConvention calling_convention;
384 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
385 parallel_move.AddMove(
386 locations->InAt(0),
387 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
388 Primitive::kPrimNot,
389 nullptr);
390 parallel_move.AddMove(
391 locations->InAt(1),
392 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
393 Primitive::kPrimInt,
394 nullptr);
395 parallel_move.AddMove(
396 locations->InAt(2),
397 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
398 Primitive::kPrimNot,
399 nullptr);
400 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
401
402 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
403 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
404 instruction_,
405 instruction_->GetDexPc(),
406 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000407 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100408 RestoreLiveRegisters(codegen, locations);
409 __ b(GetExitLabel());
410 }
411
412 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
413
414 private:
415 HInstruction* const instruction_;
416
417 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
418};
419
Roland Levillain3b359c72015-11-17 19:35:12 +0000420// Slow path generating a read barrier for a heap reference.
421class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
422 public:
423 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
424 Location out,
425 Location ref,
426 Location obj,
427 uint32_t offset,
428 Location index)
429 : instruction_(instruction),
430 out_(out),
431 ref_(ref),
432 obj_(obj),
433 offset_(offset),
434 index_(index) {
435 DCHECK(kEmitCompilerReadBarrier);
436 // If `obj` is equal to `out` or `ref`, it means the initial object
437 // has been overwritten by (or after) the heap object reference load
438 // to be instrumented, e.g.:
439 //
440 // __ LoadFromOffset(kLoadWord, out, out, offset);
441 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
442 //
443 // In that case, we have lost the information about the original
444 // object, and the emitted read barrier cannot work properly.
445 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
446 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
447 }
448
449 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
450 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
451 LocationSummary* locations = instruction_->GetLocations();
452 Register reg_out = out_.AsRegister<Register>();
453 DCHECK(locations->CanCall());
454 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
455 DCHECK(!instruction_->IsInvoke() ||
456 (instruction_->IsInvokeStaticOrDirect() &&
457 instruction_->GetLocations()->Intrinsified()));
458
459 __ Bind(GetEntryLabel());
460 SaveLiveRegisters(codegen, locations);
461
462 // We may have to change the index's value, but as `index_` is a
463 // constant member (like other "inputs" of this slow path),
464 // introduce a copy of it, `index`.
465 Location index = index_;
466 if (index_.IsValid()) {
467 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
468 if (instruction_->IsArrayGet()) {
469 // Compute the actual memory offset and store it in `index`.
470 Register index_reg = index_.AsRegister<Register>();
471 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
472 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
473 // We are about to change the value of `index_reg` (see the
474 // calls to art::arm::Thumb2Assembler::Lsl and
475 // art::arm::Thumb2Assembler::AddConstant below), but it has
476 // not been saved by the previous call to
477 // art::SlowPathCode::SaveLiveRegisters, as it is a
478 // callee-save register --
479 // art::SlowPathCode::SaveLiveRegisters does not consider
480 // callee-save registers, as it has been designed with the
481 // assumption that callee-save registers are supposed to be
482 // handled by the called function. So, as a callee-save
483 // register, `index_reg` _would_ eventually be saved onto
484 // the stack, but it would be too late: we would have
485 // changed its value earlier. Therefore, we manually save
486 // it here into another freely available register,
487 // `free_reg`, chosen of course among the caller-save
488 // registers (as a callee-save `free_reg` register would
489 // exhibit the same problem).
490 //
491 // Note we could have requested a temporary register from
492 // the register allocator instead; but we prefer not to, as
493 // this is a slow path, and we know we can find a
494 // caller-save register that is available.
495 Register free_reg = FindAvailableCallerSaveRegister(codegen);
496 __ Mov(free_reg, index_reg);
497 index_reg = free_reg;
498 index = Location::RegisterLocation(index_reg);
499 } else {
500 // The initial register stored in `index_` has already been
501 // saved in the call to art::SlowPathCode::SaveLiveRegisters
502 // (as it is not a callee-save register), so we can freely
503 // use it.
504 }
505 // Shifting the index value contained in `index_reg` by the scale
506 // factor (2) cannot overflow in practice, as the runtime is
507 // unable to allocate object arrays with a size larger than
508 // 2^26 - 1 (that is, 2^28 - 4 bytes).
509 __ Lsl(index_reg, index_reg, TIMES_4);
510 static_assert(
511 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
512 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
513 __ AddConstant(index_reg, index_reg, offset_);
514 } else {
515 DCHECK(instruction_->IsInvoke());
516 DCHECK(instruction_->GetLocations()->Intrinsified());
517 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
518 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
519 << instruction_->AsInvoke()->GetIntrinsic();
520 DCHECK_EQ(offset_, 0U);
521 DCHECK(index_.IsRegisterPair());
522 // UnsafeGet's offset location is a register pair, the low
523 // part contains the correct offset.
524 index = index_.ToLow();
525 }
526 }
527
528 // We're moving two or three locations to locations that could
529 // overlap, so we need a parallel move resolver.
530 InvokeRuntimeCallingConvention calling_convention;
531 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
532 parallel_move.AddMove(ref_,
533 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
534 Primitive::kPrimNot,
535 nullptr);
536 parallel_move.AddMove(obj_,
537 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
538 Primitive::kPrimNot,
539 nullptr);
540 if (index.IsValid()) {
541 parallel_move.AddMove(index,
542 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
543 Primitive::kPrimInt,
544 nullptr);
545 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
546 } else {
547 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
548 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
549 }
550 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
551 instruction_,
552 instruction_->GetDexPc(),
553 this);
554 CheckEntrypointTypes<
555 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
556 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
557
558 RestoreLiveRegisters(codegen, locations);
559 __ b(GetExitLabel());
560 }
561
562 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
563
564 private:
565 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
566 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
567 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
568 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
569 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
570 return static_cast<Register>(i);
571 }
572 }
573 // We shall never fail to find a free caller-save register, as
574 // there are more than two core caller-save registers on ARM
575 // (meaning it is possible to find one which is different from
576 // `ref` and `obj`).
577 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
578 LOG(FATAL) << "Could not find a free caller-save register";
579 UNREACHABLE();
580 }
581
582 HInstruction* const instruction_;
583 const Location out_;
584 const Location ref_;
585 const Location obj_;
586 const uint32_t offset_;
587 // An additional location containing an index to an array.
588 // Only used for HArrayGet and the UnsafeGetObject &
589 // UnsafeGetObjectVolatile intrinsics.
590 const Location index_;
591
592 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
593};
594
595// Slow path generating a read barrier for a GC root.
596class ReadBarrierForRootSlowPathARM : public SlowPathCode {
597 public:
598 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
599 : instruction_(instruction), out_(out), root_(root) {}
600
601 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
602 LocationSummary* locations = instruction_->GetLocations();
603 Register reg_out = out_.AsRegister<Register>();
604 DCHECK(locations->CanCall());
605 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
606 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
607
608 __ Bind(GetEntryLabel());
609 SaveLiveRegisters(codegen, locations);
610
611 InvokeRuntimeCallingConvention calling_convention;
612 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
613 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
614 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
615 instruction_,
616 instruction_->GetDexPc(),
617 this);
618 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
619 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
620
621 RestoreLiveRegisters(codegen, locations);
622 __ b(GetExitLabel());
623 }
624
625 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
626
627 private:
628 HInstruction* const instruction_;
629 const Location out_;
630 const Location root_;
631
632 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
633};
634
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000635#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100636#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700637
Aart Bike9f37602015-10-09 11:15:55 -0700638inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700639 switch (cond) {
640 case kCondEQ: return EQ;
641 case kCondNE: return NE;
642 case kCondLT: return LT;
643 case kCondLE: return LE;
644 case kCondGT: return GT;
645 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700646 case kCondB: return LO;
647 case kCondBE: return LS;
648 case kCondA: return HI;
649 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700650 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100651 LOG(FATAL) << "Unreachable";
652 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700653}
654
Aart Bike9f37602015-10-09 11:15:55 -0700655// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100656inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700657 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100658 case kCondEQ: return EQ;
659 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700660 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100661 case kCondLT: return LO;
662 case kCondLE: return LS;
663 case kCondGT: return HI;
664 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700665 // Unsigned remain unchanged.
666 case kCondB: return LO;
667 case kCondBE: return LS;
668 case kCondA: return HI;
669 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700670 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100671 LOG(FATAL) << "Unreachable";
672 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700673}
674
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100675void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100676 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100677}
678
679void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100680 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100681}
682
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100683size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
684 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
685 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100686}
687
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100688size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
689 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
690 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100691}
692
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000693size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
694 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
695 return kArmWordSize;
696}
697
698size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
699 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
700 return kArmWordSize;
701}
702
Calin Juravle34166012014-12-19 17:22:29 +0000703CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000704 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100705 const CompilerOptions& compiler_options,
706 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000707 : CodeGenerator(graph,
708 kNumberOfCoreRegisters,
709 kNumberOfSRegisters,
710 kNumberOfRegisterPairs,
711 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
712 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000713 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
714 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100715 compiler_options,
716 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100717 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100718 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100719 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100720 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000721 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000722 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100723 method_patches_(MethodReferenceComparator(),
724 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
725 call_patches_(MethodReferenceComparator(),
726 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000727 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
728 dex_cache_arrays_base_labels_(std::less<HArmDexCacheArraysBase*>(),
729 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700730 // Always save the LR register to mimic Quick.
731 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100732}
733
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000734void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
735 // Ensure that we fix up branches and literal loads and emit the literal pool.
736 __ FinalizeCode();
737
738 // Adjust native pc offsets in stack maps.
739 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
740 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
741 uint32_t new_position = __ GetAdjustedPosition(old_position);
742 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
743 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100744 // Adjust pc offsets for the disassembly information.
745 if (disasm_info_ != nullptr) {
746 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
747 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
748 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
749 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
750 it.second.start = __ GetAdjustedPosition(it.second.start);
751 it.second.end = __ GetAdjustedPosition(it.second.end);
752 }
753 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
754 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
755 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
756 }
757 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000758
759 CodeGenerator::Finalize(allocator);
760}
761
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100762Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100763 switch (type) {
764 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100765 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100766 ArmManagedRegister pair =
767 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100768 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
769 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
770
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100771 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
772 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100773 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100774 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100775 }
776
777 case Primitive::kPrimByte:
778 case Primitive::kPrimBoolean:
779 case Primitive::kPrimChar:
780 case Primitive::kPrimShort:
781 case Primitive::kPrimInt:
782 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100783 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100784 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100785 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
786 ArmManagedRegister current =
787 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
788 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100789 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100790 }
791 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100792 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100793 }
794
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000795 case Primitive::kPrimFloat: {
796 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100797 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100799
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000800 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000801 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
802 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000803 return Location::FpuRegisterPairLocation(reg, reg + 1);
804 }
805
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100806 case Primitive::kPrimVoid:
807 LOG(FATAL) << "Unreachable type " << type;
808 }
809
Roland Levillain3b359c72015-11-17 19:35:12 +0000810 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100811}
812
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000813void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100814 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100815 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100816
817 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100818 blocked_core_registers_[SP] = true;
819 blocked_core_registers_[LR] = true;
820 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100822 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100823 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100824
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100825 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100826 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100827
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000828 if (is_baseline) {
829 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
830 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
831 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000832
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000833 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100834 }
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000835
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100836 if (is_baseline || GetGraph()->IsDebuggable()) {
837 // Stubs do not save callee-save floating point registers. If the graph
838 // is debuggable, we need to deal with these registers differently. For
839 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000840 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
841 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
842 }
843 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100844
845 UpdateBlockedPairRegisters();
846}
847
848void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
849 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
850 ArmManagedRegister current =
851 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
852 if (blocked_core_registers_[current.AsRegisterPairLow()]
853 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
854 blocked_register_pairs_[i] = true;
855 }
856 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100857}
858
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100859InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
860 : HGraphVisitor(graph),
861 assembler_(codegen->GetAssembler()),
862 codegen_(codegen) {}
863
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000864void CodeGeneratorARM::ComputeSpillMask() {
865 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000866 // Save one extra register for baseline. Note that on thumb2, there is no easy
867 // instruction to restore just the PC, so this actually helps both baseline
868 // and non-baseline to save and restore at least two registers at entry and exit.
869 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000870 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
871 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
872 // We use vpush and vpop for saving and restoring floating point registers, which take
873 // a SRegister and the number of registers to save/restore after that SRegister. We
874 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
875 // but in the range.
876 if (fpu_spill_mask_ != 0) {
877 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
878 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
879 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
880 fpu_spill_mask_ |= (1 << i);
881 }
882 }
883}
884
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100885static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100886 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100887}
888
889static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100890 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100891}
892
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000893void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000894 bool skip_overflow_check =
895 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000896 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000897 __ Bind(&frame_entry_label_);
898
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000899 if (HasEmptyFrame()) {
900 return;
901 }
902
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100903 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000904 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
905 __ LoadFromOffset(kLoadWord, IP, IP, 0);
906 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100907 }
908
Andreas Gampe501fd632015-09-10 16:11:06 -0700909 __ PushList(core_spill_mask_);
910 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
911 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000912 if (fpu_spill_mask_ != 0) {
913 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
914 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100915 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100916 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000917 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100918 int adjust = GetFrameSize() - FrameEntrySpillSize();
919 __ AddConstant(SP, -adjust);
920 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100921 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000922}
923
924void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000925 if (HasEmptyFrame()) {
926 __ bx(LR);
927 return;
928 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100929 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930 int adjust = GetFrameSize() - FrameEntrySpillSize();
931 __ AddConstant(SP, adjust);
932 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000933 if (fpu_spill_mask_ != 0) {
934 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
935 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100936 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
937 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000938 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700939 // Pop LR into PC to return.
940 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
941 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
942 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100943 __ cfi().RestoreState();
944 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000945}
946
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100947void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700948 Label* label = GetLabelOf(block);
949 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000950}
951
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100952Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
953 switch (load->GetType()) {
954 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100955 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100956 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100957
958 case Primitive::kPrimInt:
959 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100960 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100962
963 case Primitive::kPrimBoolean:
964 case Primitive::kPrimByte:
965 case Primitive::kPrimChar:
966 case Primitive::kPrimShort:
967 case Primitive::kPrimVoid:
968 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700969 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100970 }
971
972 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700973 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100974}
975
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100976Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100977 switch (type) {
978 case Primitive::kPrimBoolean:
979 case Primitive::kPrimByte:
980 case Primitive::kPrimChar:
981 case Primitive::kPrimShort:
982 case Primitive::kPrimInt:
983 case Primitive::kPrimNot: {
984 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000985 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100986 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100987 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100988 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000989 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100990 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100991 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100992
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000993 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100994 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000995 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100996 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000997 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000999 if (calling_convention.GetRegisterAt(index) == R1) {
1000 // Skip R1, and use R2_R3 instead.
1001 gp_index_++;
1002 index++;
1003 }
1004 }
1005 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1006 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001007 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001008
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001009 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001010 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001011 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001012 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1013 }
1014 }
1015
1016 case Primitive::kPrimFloat: {
1017 uint32_t stack_index = stack_index_++;
1018 if (float_index_ % 2 == 0) {
1019 float_index_ = std::max(double_index_, float_index_);
1020 }
1021 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1022 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1023 } else {
1024 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1025 }
1026 }
1027
1028 case Primitive::kPrimDouble: {
1029 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1030 uint32_t stack_index = stack_index_;
1031 stack_index_ += 2;
1032 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1033 uint32_t index = double_index_;
1034 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001035 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001036 calling_convention.GetFpuRegisterAt(index),
1037 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001038 DCHECK(ExpectedPairLayout(result));
1039 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001040 } else {
1041 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001042 }
1043 }
1044
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001045 case Primitive::kPrimVoid:
1046 LOG(FATAL) << "Unexpected parameter type " << type;
1047 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001048 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001049 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001050}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001051
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001052Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001053 switch (type) {
1054 case Primitive::kPrimBoolean:
1055 case Primitive::kPrimByte:
1056 case Primitive::kPrimChar:
1057 case Primitive::kPrimShort:
1058 case Primitive::kPrimInt:
1059 case Primitive::kPrimNot: {
1060 return Location::RegisterLocation(R0);
1061 }
1062
1063 case Primitive::kPrimFloat: {
1064 return Location::FpuRegisterLocation(S0);
1065 }
1066
1067 case Primitive::kPrimLong: {
1068 return Location::RegisterPairLocation(R0, R1);
1069 }
1070
1071 case Primitive::kPrimDouble: {
1072 return Location::FpuRegisterPairLocation(S0, S1);
1073 }
1074
1075 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001076 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001077 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001078
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001079 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001080}
1081
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001082Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1083 return Location::RegisterLocation(kMethodRegisterArgument);
1084}
1085
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086void CodeGeneratorARM::Move32(Location destination, Location source) {
1087 if (source.Equals(destination)) {
1088 return;
1089 }
1090 if (destination.IsRegister()) {
1091 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001092 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001093 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001094 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001096 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001098 } else if (destination.IsFpuRegister()) {
1099 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001100 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001102 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001107 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001108 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001113 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001114 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1115 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001116 }
1117 }
1118}
1119
1120void CodeGeneratorARM::Move64(Location destination, Location source) {
1121 if (source.Equals(destination)) {
1122 return;
1123 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001124 if (destination.IsRegisterPair()) {
1125 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001126 EmitParallelMoves(
1127 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1128 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001129 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001130 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001131 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1132 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001134 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001135 } else if (source.IsFpuRegisterPair()) {
1136 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1137 destination.AsRegisterPairHigh<Register>(),
1138 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001139 } else {
1140 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001141 DCHECK(ExpectedPairLayout(destination));
1142 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1143 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001144 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001145 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001146 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001147 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1148 SP,
1149 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001150 } else if (source.IsRegisterPair()) {
1151 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1152 source.AsRegisterPairLow<Register>(),
1153 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001154 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001155 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001156 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 } else {
1158 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001159 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001160 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001161 if (source.AsRegisterPairLow<Register>() == R1) {
1162 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001163 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1164 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001166 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 SP, destination.GetStackIndex());
1168 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001169 } else if (source.IsFpuRegisterPair()) {
1170 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1171 SP,
1172 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001173 } else {
1174 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001175 EmitParallelMoves(
1176 Location::StackSlot(source.GetStackIndex()),
1177 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001178 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001179 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001180 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1181 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 }
1183 }
1184}
1185
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001186void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001187 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001188 if (instruction->IsCurrentMethod()) {
1189 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1190 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001191 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001192 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001193 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001194 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1195 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +00001196 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001197 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001198 } else {
1199 DCHECK(location.IsStackSlot());
1200 __ LoadImmediate(IP, value);
1201 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1202 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001203 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +00001204 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +00001205 int64_t value = const_to_move->AsLongConstant()->GetValue();
1206 if (location.IsRegisterPair()) {
1207 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
1208 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
1209 } else {
1210 DCHECK(location.IsDoubleStackSlot());
1211 __ LoadImmediate(IP, Low32Bits(value));
1212 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1213 __ LoadImmediate(IP, High32Bits(value));
1214 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
1215 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 }
Roland Levillain476df552014-10-09 17:51:36 +01001217 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1219 switch (instruction->GetType()) {
1220 case Primitive::kPrimBoolean:
1221 case Primitive::kPrimByte:
1222 case Primitive::kPrimChar:
1223 case Primitive::kPrimShort:
1224 case Primitive::kPrimInt:
1225 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001226 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001227 Move32(location, Location::StackSlot(stack_slot));
1228 break;
1229
1230 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 Move64(location, Location::DoubleStackSlot(stack_slot));
1233 break;
1234
1235 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001237 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001238 } else if (instruction->IsTemporary()) {
1239 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001240 if (temp_location.IsStackSlot()) {
1241 Move32(location, temp_location);
1242 } else {
1243 DCHECK(temp_location.IsDoubleStackSlot());
1244 Move64(location, temp_location);
1245 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001246 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001247 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 switch (instruction->GetType()) {
1249 case Primitive::kPrimBoolean:
1250 case Primitive::kPrimByte:
1251 case Primitive::kPrimChar:
1252 case Primitive::kPrimShort:
1253 case Primitive::kPrimNot:
1254 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001255 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001256 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 break;
1258
1259 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001260 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001261 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 break;
1263
1264 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001265 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001266 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001267 }
1268}
1269
Calin Juravle175dc732015-08-25 15:42:32 +01001270void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1271 DCHECK(location.IsRegister());
1272 __ LoadImmediate(location.AsRegister<Register>(), value);
1273}
1274
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1276 if (Primitive::Is64BitType(dst_type)) {
1277 Move64(dst, src);
1278 } else {
1279 Move32(dst, src);
1280 }
1281}
1282
1283void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1284 if (location.IsRegister()) {
1285 locations->AddTemp(location);
1286 } else if (location.IsRegisterPair()) {
1287 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1288 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1289 } else {
1290 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1291 }
1292}
1293
Calin Juravle175dc732015-08-25 15:42:32 +01001294void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1295 HInstruction* instruction,
1296 uint32_t dex_pc,
1297 SlowPathCode* slow_path) {
1298 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1299 instruction,
1300 dex_pc,
1301 slow_path);
1302}
1303
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001304void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1305 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001306 uint32_t dex_pc,
1307 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001308 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001309 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1310 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001311 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001312}
1313
David Brazdilfc6a86a2015-06-26 10:33:45 +00001314void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001315 DCHECK(!successor->IsExitBlock());
1316
1317 HBasicBlock* block = got->GetBlock();
1318 HInstruction* previous = got->GetPrevious();
1319
1320 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001321 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001322 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1323 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1324 return;
1325 }
1326
1327 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1328 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1329 }
1330 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001331 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001332 }
1333}
1334
David Brazdilfc6a86a2015-06-26 10:33:45 +00001335void LocationsBuilderARM::VisitGoto(HGoto* got) {
1336 got->SetLocations(nullptr);
1337}
1338
1339void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1340 HandleGoto(got, got->GetSuccessor());
1341}
1342
1343void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1344 try_boundary->SetLocations(nullptr);
1345}
1346
1347void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1348 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1349 if (!successor->IsExitBlock()) {
1350 HandleGoto(try_boundary, successor);
1351 }
1352}
1353
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001354void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001355 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001356}
1357
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001358void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001359}
1360
Roland Levillain4fa13f62015-07-06 18:11:54 +01001361void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1362 ShifterOperand operand;
1363 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1364 __ cmp(left, operand);
1365 } else {
1366 Register temp = IP;
1367 __ LoadImmediate(temp, right);
1368 __ cmp(left, ShifterOperand(temp));
1369 }
1370}
1371
1372void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1373 Label* true_label,
1374 Label* false_label) {
1375 __ vmstat(); // transfer FP status register to ARM APSR.
Aart Bike9f37602015-10-09 11:15:55 -07001376 // TODO: merge into a single branch (except "equal or unordered" and "not equal")
Roland Levillain4fa13f62015-07-06 18:11:54 +01001377 if (cond->IsFPConditionTrueIfNaN()) {
1378 __ b(true_label, VS); // VS for unordered.
1379 } else if (cond->IsFPConditionFalseIfNaN()) {
1380 __ b(false_label, VS); // VS for unordered.
1381 }
Aart Bike9f37602015-10-09 11:15:55 -07001382 __ b(true_label, ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001383}
1384
1385void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1386 Label* true_label,
1387 Label* false_label) {
1388 LocationSummary* locations = cond->GetLocations();
1389 Location left = locations->InAt(0);
1390 Location right = locations->InAt(1);
1391 IfCondition if_cond = cond->GetCondition();
1392
1393 Register left_high = left.AsRegisterPairHigh<Register>();
1394 Register left_low = left.AsRegisterPairLow<Register>();
1395 IfCondition true_high_cond = if_cond;
1396 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001397 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001398
1399 // Set the conditions for the test, remembering that == needs to be
1400 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001401 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 switch (if_cond) {
1403 case kCondEQ:
1404 case kCondNE:
1405 // Nothing to do.
1406 break;
1407 case kCondLT:
1408 false_high_cond = kCondGT;
1409 break;
1410 case kCondLE:
1411 true_high_cond = kCondLT;
1412 break;
1413 case kCondGT:
1414 false_high_cond = kCondLT;
1415 break;
1416 case kCondGE:
1417 true_high_cond = kCondGT;
1418 break;
Aart Bike9f37602015-10-09 11:15:55 -07001419 case kCondB:
1420 false_high_cond = kCondA;
1421 break;
1422 case kCondBE:
1423 true_high_cond = kCondB;
1424 break;
1425 case kCondA:
1426 false_high_cond = kCondB;
1427 break;
1428 case kCondAE:
1429 true_high_cond = kCondA;
1430 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001431 }
1432 if (right.IsConstant()) {
1433 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1434 int32_t val_low = Low32Bits(value);
1435 int32_t val_high = High32Bits(value);
1436
1437 GenerateCompareWithImmediate(left_high, val_high);
1438 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001439 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001440 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001441 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001442 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001443 __ b(true_label, ARMCondition(true_high_cond));
1444 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001445 }
1446 // Must be equal high, so compare the lows.
1447 GenerateCompareWithImmediate(left_low, val_low);
1448 } else {
1449 Register right_high = right.AsRegisterPairHigh<Register>();
1450 Register right_low = right.AsRegisterPairLow<Register>();
1451
1452 __ cmp(left_high, ShifterOperand(right_high));
1453 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001454 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001455 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001456 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001457 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001458 __ b(true_label, ARMCondition(true_high_cond));
1459 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001460 }
1461 // Must be equal high, so compare the lows.
1462 __ cmp(left_low, ShifterOperand(right_low));
1463 }
1464 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001465 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001466 __ b(true_label, final_condition);
1467}
1468
David Brazdil0debae72015-11-12 18:37:00 +00001469void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1470 Label* true_target_in,
1471 Label* false_target_in) {
1472 // Generated branching requires both targets to be explicit. If either of the
1473 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1474 Label fallthrough_target;
1475 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1476 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1477
Roland Levillain4fa13f62015-07-06 18:11:54 +01001478 LocationSummary* locations = condition->GetLocations();
1479 Location left = locations->InAt(0);
1480 Location right = locations->InAt(1);
1481
Roland Levillain4fa13f62015-07-06 18:11:54 +01001482 Primitive::Type type = condition->InputAt(0)->GetType();
1483 switch (type) {
1484 case Primitive::kPrimLong:
1485 GenerateLongComparesAndJumps(condition, true_target, false_target);
1486 break;
1487 case Primitive::kPrimFloat:
1488 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1489 GenerateFPJumps(condition, true_target, false_target);
1490 break;
1491 case Primitive::kPrimDouble:
1492 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1493 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1494 GenerateFPJumps(condition, true_target, false_target);
1495 break;
1496 default:
1497 LOG(FATAL) << "Unexpected compare type " << type;
1498 }
1499
David Brazdil0debae72015-11-12 18:37:00 +00001500 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001501 __ b(false_target);
1502 }
David Brazdil0debae72015-11-12 18:37:00 +00001503
1504 if (fallthrough_target.IsLinked()) {
1505 __ Bind(&fallthrough_target);
1506 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001507}
1508
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001509void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001510 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001511 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001512 Label* false_target) {
1513 HInstruction* cond = instruction->InputAt(condition_input_index);
1514
1515 if (true_target == nullptr && false_target == nullptr) {
1516 // Nothing to do. The code always falls through.
1517 return;
1518 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001519 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001520 if (cond->AsIntConstant()->IsOne()) {
1521 if (true_target != nullptr) {
1522 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001523 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001524 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001525 DCHECK(cond->AsIntConstant()->IsZero());
1526 if (false_target != nullptr) {
1527 __ b(false_target);
1528 }
1529 }
1530 return;
1531 }
1532
1533 // The following code generates these patterns:
1534 // (1) true_target == nullptr && false_target != nullptr
1535 // - opposite condition true => branch to false_target
1536 // (2) true_target != nullptr && false_target == nullptr
1537 // - condition true => branch to true_target
1538 // (3) true_target != nullptr && false_target != nullptr
1539 // - condition true => branch to true_target
1540 // - branch to false_target
1541 if (IsBooleanValueOrMaterializedCondition(cond)) {
1542 // Condition has been materialized, compare the output to 0.
1543 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1544 DCHECK(cond_val.IsRegister());
1545 if (true_target == nullptr) {
1546 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1547 } else {
1548 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001549 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001550 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001551 // Condition has not been materialized. Use its inputs as the comparison and
1552 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001553 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001554
1555 // If this is a long or FP comparison that has been folded into
1556 // the HCondition, generate the comparison directly.
1557 Primitive::Type type = condition->InputAt(0)->GetType();
1558 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1559 GenerateCompareTestAndBranch(condition, true_target, false_target);
1560 return;
1561 }
1562
1563 LocationSummary* locations = cond->GetLocations();
1564 DCHECK(locations->InAt(0).IsRegister());
1565 Register left = locations->InAt(0).AsRegister<Register>();
1566 Location right = locations->InAt(1);
1567 if (right.IsRegister()) {
1568 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001569 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001570 DCHECK(right.IsConstant());
1571 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1572 }
1573 if (true_target == nullptr) {
1574 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1575 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001576 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001577 }
Dave Allison20dfc792014-06-16 20:44:29 -07001578 }
David Brazdil0debae72015-11-12 18:37:00 +00001579
1580 // If neither branch falls through (case 3), the conditional branch to `true_target`
1581 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1582 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001583 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001584 }
1585}
1586
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001588 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1589 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001590 locations->SetInAt(0, Location::RequiresRegister());
1591 }
1592}
1593
1594void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001595 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1596 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1597 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1598 nullptr : codegen_->GetLabelOf(true_successor);
1599 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1600 nullptr : codegen_->GetLabelOf(false_successor);
1601 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001602}
1603
1604void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1605 LocationSummary* locations = new (GetGraph()->GetArena())
1606 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001607 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001608 locations->SetInAt(0, Location::RequiresRegister());
1609 }
1610}
1611
1612void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
David Brazdil0debae72015-11-12 18:37:00 +00001613 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DeoptimizationSlowPathARM(deoptimize);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001614 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001615 GenerateTestAndBranch(deoptimize,
1616 /* condition_input_index */ 0,
1617 slow_path->GetEntryLabel(),
1618 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001619}
Dave Allison20dfc792014-06-16 20:44:29 -07001620
David Srbecky0cf44932015-12-09 14:09:59 +00001621void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1622 new (GetGraph()->GetArena()) LocationSummary(info);
1623}
1624
1625void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1626 codegen_->RecordPcInfo(info, info->GetDexPc());
1627}
1628
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001629void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001630 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001631 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001632 // Handle the long/FP comparisons made in instruction simplification.
1633 switch (cond->InputAt(0)->GetType()) {
1634 case Primitive::kPrimLong:
1635 locations->SetInAt(0, Location::RequiresRegister());
1636 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1637 if (cond->NeedsMaterialization()) {
1638 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1639 }
1640 break;
1641
1642 case Primitive::kPrimFloat:
1643 case Primitive::kPrimDouble:
1644 locations->SetInAt(0, Location::RequiresFpuRegister());
1645 locations->SetInAt(1, Location::RequiresFpuRegister());
1646 if (cond->NeedsMaterialization()) {
1647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1648 }
1649 break;
1650
1651 default:
1652 locations->SetInAt(0, Location::RequiresRegister());
1653 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1654 if (cond->NeedsMaterialization()) {
1655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1656 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001657 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001658}
1659
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001660void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001661 if (!cond->NeedsMaterialization()) {
1662 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001663 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001664
1665 LocationSummary* locations = cond->GetLocations();
1666 Location left = locations->InAt(0);
1667 Location right = locations->InAt(1);
1668 Register out = locations->Out().AsRegister<Register>();
1669 Label true_label, false_label;
1670
1671 switch (cond->InputAt(0)->GetType()) {
1672 default: {
1673 // Integer case.
1674 if (right.IsRegister()) {
1675 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1676 } else {
1677 DCHECK(right.IsConstant());
1678 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1679 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1680 }
Aart Bike9f37602015-10-09 11:15:55 -07001681 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001682 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001683 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001684 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001685 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001686 return;
1687 }
1688 case Primitive::kPrimLong:
1689 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1690 break;
1691 case Primitive::kPrimFloat:
1692 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1693 GenerateFPJumps(cond, &true_label, &false_label);
1694 break;
1695 case Primitive::kPrimDouble:
1696 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1697 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1698 GenerateFPJumps(cond, &true_label, &false_label);
1699 break;
1700 }
1701
1702 // Convert the jumps into the result.
1703 Label done_label;
1704
1705 // False case: result = 0.
1706 __ Bind(&false_label);
1707 __ LoadImmediate(out, 0);
1708 __ b(&done_label);
1709
1710 // True case: result = 1.
1711 __ Bind(&true_label);
1712 __ LoadImmediate(out, 1);
1713 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001714}
1715
1716void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001717 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001718}
1719
1720void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001721 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001722}
1723
1724void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001725 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001726}
1727
1728void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001734}
1735
1736void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001738}
1739
1740void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001742}
1743
1744void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001746}
1747
1748void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001750}
1751
1752void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001754}
1755
1756void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001758}
1759
1760void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001762}
1763
Aart Bike9f37602015-10-09 11:15:55 -07001764void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001766}
1767
1768void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001770}
1771
1772void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001774}
1775
1776void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001778}
1779
1780void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001782}
1783
1784void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001786}
1787
1788void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001790}
1791
1792void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001794}
1795
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001797 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001798}
1799
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001800void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1801 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001802}
1803
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001805 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001806}
1807
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001808void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001809 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001810}
1811
1812void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 LocationSummary* locations =
1814 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001815 switch (store->InputAt(1)->GetType()) {
1816 case Primitive::kPrimBoolean:
1817 case Primitive::kPrimByte:
1818 case Primitive::kPrimChar:
1819 case Primitive::kPrimShort:
1820 case Primitive::kPrimInt:
1821 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001822 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001823 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1824 break;
1825
1826 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001827 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1829 break;
1830
1831 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001832 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834}
1835
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001836void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837}
1838
1839void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001840 LocationSummary* locations =
1841 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001842 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001843}
1844
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001845void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001846 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847}
1848
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001849void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1850 LocationSummary* locations =
1851 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1852 locations->SetOut(Location::ConstantLocation(constant));
1853}
1854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001855void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001856 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001857}
1858
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001860 LocationSummary* locations =
1861 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001862 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863}
1864
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001865void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001866 // Will be generated at use site.
1867}
1868
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001869void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1870 LocationSummary* locations =
1871 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1872 locations->SetOut(Location::ConstantLocation(constant));
1873}
1874
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001875void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001876 // Will be generated at use site.
1877}
1878
1879void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1880 LocationSummary* locations =
1881 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1882 locations->SetOut(Location::ConstantLocation(constant));
1883}
1884
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001885void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001886 // Will be generated at use site.
1887}
1888
Calin Juravle27df7582015-04-17 19:12:31 +01001889void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1890 memory_barrier->SetLocations(nullptr);
1891}
1892
1893void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1894 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1895}
1896
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001897void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001898 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001899}
1900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001901void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001902 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001903}
1904
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001905void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001906 LocationSummary* locations =
1907 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001908 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001909}
1910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001911void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001912 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001913}
1914
Calin Juravle175dc732015-08-25 15:42:32 +01001915void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1916 // The trampoline uses the same calling convention as dex calling conventions,
1917 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1918 // the method_idx.
1919 HandleInvoke(invoke);
1920}
1921
1922void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1923 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1924}
1925
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001926void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001927 // When we do not run baseline, explicit clinit checks triggered by static
1928 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1929 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001930
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001931 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001932 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001933 codegen_->GetInstructionSetFeatures());
1934 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001935 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1936 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1937 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001938 return;
1939 }
1940
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001941 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001942
1943 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1944 if (invoke->HasPcRelativeDexCache()) {
1945 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1946 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001947}
1948
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001949static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1950 if (invoke->GetLocations()->Intrinsified()) {
1951 IntrinsicCodeGeneratorARM intrinsic(codegen);
1952 intrinsic.Dispatch(invoke);
1953 return true;
1954 }
1955 return false;
1956}
1957
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001958void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001959 // When we do not run baseline, explicit clinit checks triggered by static
1960 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1961 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001962
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001963 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1964 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001965 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001966
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001967 LocationSummary* locations = invoke->GetLocations();
1968 codegen_->GenerateStaticOrDirectCall(
1969 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001970 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001971}
1972
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001973void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001974 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001975 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001976}
1977
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001978void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001979 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001980 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001981 codegen_->GetInstructionSetFeatures());
1982 if (intrinsic.TryDispatch(invoke)) {
1983 return;
1984 }
1985
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001986 HandleInvoke(invoke);
1987}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001988
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001989void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001990 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1991 return;
1992 }
1993
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001994 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001995 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001996 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001997}
1998
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001999void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2000 HandleInvoke(invoke);
2001 // Add the hidden argument.
2002 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2003}
2004
2005void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2006 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002007 LocationSummary* locations = invoke->GetLocations();
2008 Register temp = locations->GetTemp(0).AsRegister<Register>();
2009 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002010 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2011 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002012 Location receiver = locations->InAt(0);
2013 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2014
Roland Levillain3b359c72015-11-17 19:35:12 +00002015 // Set the hidden argument. This is safe to do this here, as R12
2016 // won't be modified thereafter, before the `blx` (call) instruction.
2017 DCHECK_EQ(R12, hidden_reg);
2018 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002020 if (receiver.IsStackSlot()) {
2021 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002022 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2024 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002025 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002027 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002028 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002029 // Instead of simply (possibly) unpoisoning `temp` here, we should
2030 // emit a read barrier for the previous class reference load.
2031 // However this is not required in practice, as this is an
2032 // intermediate/temporary reference and because the current
2033 // concurrent copying collector keeps the from-space memory
2034 // intact/accessible until the end of the marking phase (the
2035 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002036 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002037 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002038 uint32_t entry_point =
2039 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002040 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2041 // LR = temp->GetEntryPoint();
2042 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2043 // LR();
2044 __ blx(LR);
2045 DCHECK(!codegen_->IsLeafMethod());
2046 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2047}
2048
Roland Levillain88cb1752014-10-20 16:36:47 +01002049void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2050 LocationSummary* locations =
2051 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2052 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002053 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002054 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2056 break;
2057 }
2058 case Primitive::kPrimLong: {
2059 locations->SetInAt(0, Location::RequiresRegister());
2060 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002061 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002062 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002063
Roland Levillain88cb1752014-10-20 16:36:47 +01002064 case Primitive::kPrimFloat:
2065 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002066 locations->SetInAt(0, Location::RequiresFpuRegister());
2067 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002068 break;
2069
2070 default:
2071 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2072 }
2073}
2074
2075void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2076 LocationSummary* locations = neg->GetLocations();
2077 Location out = locations->Out();
2078 Location in = locations->InAt(0);
2079 switch (neg->GetResultType()) {
2080 case Primitive::kPrimInt:
2081 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002083 break;
2084
2085 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002086 DCHECK(in.IsRegisterPair());
2087 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2088 __ rsbs(out.AsRegisterPairLow<Register>(),
2089 in.AsRegisterPairLow<Register>(),
2090 ShifterOperand(0));
2091 // We cannot emit an RSC (Reverse Subtract with Carry)
2092 // instruction here, as it does not exist in the Thumb-2
2093 // instruction set. We use the following approach
2094 // using SBC and SUB instead.
2095 //
2096 // out.hi = -C
2097 __ sbc(out.AsRegisterPairHigh<Register>(),
2098 out.AsRegisterPairHigh<Register>(),
2099 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2100 // out.hi = out.hi - in.hi
2101 __ sub(out.AsRegisterPairHigh<Register>(),
2102 out.AsRegisterPairHigh<Register>(),
2103 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2104 break;
2105
Roland Levillain88cb1752014-10-20 16:36:47 +01002106 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002107 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002108 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002109 break;
2110
Roland Levillain88cb1752014-10-20 16:36:47 +01002111 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002112 DCHECK(in.IsFpuRegisterPair());
2113 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2114 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002115 break;
2116
2117 default:
2118 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2119 }
2120}
2121
Roland Levillaindff1f282014-11-05 14:15:05 +00002122void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002123 Primitive::Type result_type = conversion->GetResultType();
2124 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002125 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002126
Roland Levillain5b3ee562015-04-14 16:02:41 +01002127 // The float-to-long, double-to-long and long-to-float type conversions
2128 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002129 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002130 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2131 && result_type == Primitive::kPrimLong)
2132 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002133 ? LocationSummary::kCall
2134 : LocationSummary::kNoCall;
2135 LocationSummary* locations =
2136 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2137
David Brazdilb2bd1c52015-03-25 11:17:37 +00002138 // The Java language does not allow treating boolean as an integral type but
2139 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002140
Roland Levillaindff1f282014-11-05 14:15:05 +00002141 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002142 case Primitive::kPrimByte:
2143 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002144 case Primitive::kPrimBoolean:
2145 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002146 case Primitive::kPrimShort:
2147 case Primitive::kPrimInt:
2148 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002149 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002150 locations->SetInAt(0, Location::RequiresRegister());
2151 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2152 break;
2153
2154 default:
2155 LOG(FATAL) << "Unexpected type conversion from " << input_type
2156 << " to " << result_type;
2157 }
2158 break;
2159
Roland Levillain01a8d712014-11-14 16:27:39 +00002160 case Primitive::kPrimShort:
2161 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002162 case Primitive::kPrimBoolean:
2163 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002164 case Primitive::kPrimByte:
2165 case Primitive::kPrimInt:
2166 case Primitive::kPrimChar:
2167 // Processing a Dex `int-to-short' instruction.
2168 locations->SetInAt(0, Location::RequiresRegister());
2169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2170 break;
2171
2172 default:
2173 LOG(FATAL) << "Unexpected type conversion from " << input_type
2174 << " to " << result_type;
2175 }
2176 break;
2177
Roland Levillain946e1432014-11-11 17:35:19 +00002178 case Primitive::kPrimInt:
2179 switch (input_type) {
2180 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002181 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002182 locations->SetInAt(0, Location::Any());
2183 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2184 break;
2185
2186 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002187 // Processing a Dex `float-to-int' instruction.
2188 locations->SetInAt(0, Location::RequiresFpuRegister());
2189 locations->SetOut(Location::RequiresRegister());
2190 locations->AddTemp(Location::RequiresFpuRegister());
2191 break;
2192
Roland Levillain946e1432014-11-11 17:35:19 +00002193 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002194 // Processing a Dex `double-to-int' instruction.
2195 locations->SetInAt(0, Location::RequiresFpuRegister());
2196 locations->SetOut(Location::RequiresRegister());
2197 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 }
2204 break;
2205
Roland Levillaindff1f282014-11-05 14:15:05 +00002206 case Primitive::kPrimLong:
2207 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002208 case Primitive::kPrimBoolean:
2209 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002210 case Primitive::kPrimByte:
2211 case Primitive::kPrimShort:
2212 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002213 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002214 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002215 locations->SetInAt(0, Location::RequiresRegister());
2216 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2217 break;
2218
Roland Levillain624279f2014-12-04 11:54:28 +00002219 case Primitive::kPrimFloat: {
2220 // Processing a Dex `float-to-long' instruction.
2221 InvokeRuntimeCallingConvention calling_convention;
2222 locations->SetInAt(0, Location::FpuRegisterLocation(
2223 calling_convention.GetFpuRegisterAt(0)));
2224 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2225 break;
2226 }
2227
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002228 case Primitive::kPrimDouble: {
2229 // Processing a Dex `double-to-long' instruction.
2230 InvokeRuntimeCallingConvention calling_convention;
2231 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2232 calling_convention.GetFpuRegisterAt(0),
2233 calling_convention.GetFpuRegisterAt(1)));
2234 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002235 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002236 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002237
2238 default:
2239 LOG(FATAL) << "Unexpected type conversion from " << input_type
2240 << " to " << result_type;
2241 }
2242 break;
2243
Roland Levillain981e4542014-11-14 11:47:14 +00002244 case Primitive::kPrimChar:
2245 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002246 case Primitive::kPrimBoolean:
2247 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002248 case Primitive::kPrimByte:
2249 case Primitive::kPrimShort:
2250 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002251 // Processing a Dex `int-to-char' instruction.
2252 locations->SetInAt(0, Location::RequiresRegister());
2253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2254 break;
2255
2256 default:
2257 LOG(FATAL) << "Unexpected type conversion from " << input_type
2258 << " to " << result_type;
2259 }
2260 break;
2261
Roland Levillaindff1f282014-11-05 14:15:05 +00002262 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002263 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002264 case Primitive::kPrimBoolean:
2265 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002266 case Primitive::kPrimByte:
2267 case Primitive::kPrimShort:
2268 case Primitive::kPrimInt:
2269 case Primitive::kPrimChar:
2270 // Processing a Dex `int-to-float' instruction.
2271 locations->SetInAt(0, Location::RequiresRegister());
2272 locations->SetOut(Location::RequiresFpuRegister());
2273 break;
2274
Roland Levillain5b3ee562015-04-14 16:02:41 +01002275 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002276 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002277 InvokeRuntimeCallingConvention calling_convention;
2278 locations->SetInAt(0, Location::RegisterPairLocation(
2279 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2280 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002281 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002282 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002283
Roland Levillaincff13742014-11-17 14:32:17 +00002284 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002285 // Processing a Dex `double-to-float' instruction.
2286 locations->SetInAt(0, Location::RequiresFpuRegister());
2287 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002288 break;
2289
2290 default:
2291 LOG(FATAL) << "Unexpected type conversion from " << input_type
2292 << " to " << result_type;
2293 };
2294 break;
2295
Roland Levillaindff1f282014-11-05 14:15:05 +00002296 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002297 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002298 case Primitive::kPrimBoolean:
2299 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002300 case Primitive::kPrimByte:
2301 case Primitive::kPrimShort:
2302 case Primitive::kPrimInt:
2303 case Primitive::kPrimChar:
2304 // Processing a Dex `int-to-double' instruction.
2305 locations->SetInAt(0, Location::RequiresRegister());
2306 locations->SetOut(Location::RequiresFpuRegister());
2307 break;
2308
2309 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002310 // Processing a Dex `long-to-double' instruction.
2311 locations->SetInAt(0, Location::RequiresRegister());
2312 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002313 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002314 locations->AddTemp(Location::RequiresFpuRegister());
2315 break;
2316
Roland Levillaincff13742014-11-17 14:32:17 +00002317 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002318 // Processing a Dex `float-to-double' instruction.
2319 locations->SetInAt(0, Location::RequiresFpuRegister());
2320 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002321 break;
2322
2323 default:
2324 LOG(FATAL) << "Unexpected type conversion from " << input_type
2325 << " to " << result_type;
2326 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002327 break;
2328
2329 default:
2330 LOG(FATAL) << "Unexpected type conversion from " << input_type
2331 << " to " << result_type;
2332 }
2333}
2334
2335void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2336 LocationSummary* locations = conversion->GetLocations();
2337 Location out = locations->Out();
2338 Location in = locations->InAt(0);
2339 Primitive::Type result_type = conversion->GetResultType();
2340 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002341 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002342 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002343 case Primitive::kPrimByte:
2344 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002345 case Primitive::kPrimBoolean:
2346 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002347 case Primitive::kPrimShort:
2348 case Primitive::kPrimInt:
2349 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002350 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002351 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002352 break;
2353
2354 default:
2355 LOG(FATAL) << "Unexpected type conversion from " << input_type
2356 << " to " << result_type;
2357 }
2358 break;
2359
Roland Levillain01a8d712014-11-14 16:27:39 +00002360 case Primitive::kPrimShort:
2361 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimInt:
2366 case Primitive::kPrimChar:
2367 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002369 break;
2370
2371 default:
2372 LOG(FATAL) << "Unexpected type conversion from " << input_type
2373 << " to " << result_type;
2374 }
2375 break;
2376
Roland Levillain946e1432014-11-11 17:35:19 +00002377 case Primitive::kPrimInt:
2378 switch (input_type) {
2379 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002380 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002381 DCHECK(out.IsRegister());
2382 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002383 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002384 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002385 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002386 } else {
2387 DCHECK(in.IsConstant());
2388 DCHECK(in.GetConstant()->IsLongConstant());
2389 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002390 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002391 }
2392 break;
2393
Roland Levillain3f8f9362014-12-02 17:45:01 +00002394 case Primitive::kPrimFloat: {
2395 // Processing a Dex `float-to-int' instruction.
2396 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2397 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2398 __ vcvtis(temp, temp);
2399 __ vmovrs(out.AsRegister<Register>(), temp);
2400 break;
2401 }
2402
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002403 case Primitive::kPrimDouble: {
2404 // Processing a Dex `double-to-int' instruction.
2405 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2406 DRegister temp_d = FromLowSToD(temp_s);
2407 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2408 __ vcvtid(temp_s, temp_d);
2409 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002410 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002411 }
Roland Levillain946e1432014-11-11 17:35:19 +00002412
2413 default:
2414 LOG(FATAL) << "Unexpected type conversion from " << input_type
2415 << " to " << result_type;
2416 }
2417 break;
2418
Roland Levillaindff1f282014-11-05 14:15:05 +00002419 case Primitive::kPrimLong:
2420 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002421 case Primitive::kPrimBoolean:
2422 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002423 case Primitive::kPrimByte:
2424 case Primitive::kPrimShort:
2425 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002426 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002427 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002428 DCHECK(out.IsRegisterPair());
2429 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002430 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002431 // Sign extension.
2432 __ Asr(out.AsRegisterPairHigh<Register>(),
2433 out.AsRegisterPairLow<Register>(),
2434 31);
2435 break;
2436
2437 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002438 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002439 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2440 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002441 conversion->GetDexPc(),
2442 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002443 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002444 break;
2445
Roland Levillaindff1f282014-11-05 14:15:05 +00002446 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002447 // Processing a Dex `double-to-long' instruction.
2448 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2449 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002450 conversion->GetDexPc(),
2451 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002452 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002453 break;
2454
2455 default:
2456 LOG(FATAL) << "Unexpected type conversion from " << input_type
2457 << " to " << result_type;
2458 }
2459 break;
2460
Roland Levillain981e4542014-11-14 11:47:14 +00002461 case Primitive::kPrimChar:
2462 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002463 case Primitive::kPrimBoolean:
2464 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002465 case Primitive::kPrimByte:
2466 case Primitive::kPrimShort:
2467 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002468 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002469 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002470 break;
2471
2472 default:
2473 LOG(FATAL) << "Unexpected type conversion from " << input_type
2474 << " to " << result_type;
2475 }
2476 break;
2477
Roland Levillaindff1f282014-11-05 14:15:05 +00002478 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002479 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002480 case Primitive::kPrimBoolean:
2481 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002482 case Primitive::kPrimByte:
2483 case Primitive::kPrimShort:
2484 case Primitive::kPrimInt:
2485 case Primitive::kPrimChar: {
2486 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002487 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2488 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002489 break;
2490 }
2491
Roland Levillain5b3ee562015-04-14 16:02:41 +01002492 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002493 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002494 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2495 conversion,
2496 conversion->GetDexPc(),
2497 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002498 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002499 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002500
Roland Levillaincff13742014-11-17 14:32:17 +00002501 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002502 // Processing a Dex `double-to-float' instruction.
2503 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2504 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002505 break;
2506
2507 default:
2508 LOG(FATAL) << "Unexpected type conversion from " << input_type
2509 << " to " << result_type;
2510 };
2511 break;
2512
Roland Levillaindff1f282014-11-05 14:15:05 +00002513 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002514 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002515 case Primitive::kPrimBoolean:
2516 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002517 case Primitive::kPrimByte:
2518 case Primitive::kPrimShort:
2519 case Primitive::kPrimInt:
2520 case Primitive::kPrimChar: {
2521 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002522 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002523 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2524 out.AsFpuRegisterPairLow<SRegister>());
2525 break;
2526 }
2527
Roland Levillain647b9ed2014-11-27 12:06:00 +00002528 case Primitive::kPrimLong: {
2529 // Processing a Dex `long-to-double' instruction.
2530 Register low = in.AsRegisterPairLow<Register>();
2531 Register high = in.AsRegisterPairHigh<Register>();
2532 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2533 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002534 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002535 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002536 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2537 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002538
Roland Levillain682393c2015-04-14 15:57:52 +01002539 // temp_d = int-to-double(high)
2540 __ vmovsr(temp_s, high);
2541 __ vcvtdi(temp_d, temp_s);
2542 // constant_d = k2Pow32EncodingForDouble
2543 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2544 // out_d = unsigned-to-double(low)
2545 __ vmovsr(out_s, low);
2546 __ vcvtdu(out_d, out_s);
2547 // out_d += temp_d * constant_d
2548 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002549 break;
2550 }
2551
Roland Levillaincff13742014-11-17 14:32:17 +00002552 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002553 // Processing a Dex `float-to-double' instruction.
2554 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2555 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002556 break;
2557
2558 default:
2559 LOG(FATAL) << "Unexpected type conversion from " << input_type
2560 << " to " << result_type;
2561 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002562 break;
2563
2564 default:
2565 LOG(FATAL) << "Unexpected type conversion from " << input_type
2566 << " to " << result_type;
2567 }
2568}
2569
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002570void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002571 LocationSummary* locations =
2572 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002573 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002574 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002575 locations->SetInAt(0, Location::RequiresRegister());
2576 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2578 break;
2579 }
2580
2581 case Primitive::kPrimLong: {
2582 locations->SetInAt(0, Location::RequiresRegister());
2583 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002585 break;
2586 }
2587
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002588 case Primitive::kPrimFloat:
2589 case Primitive::kPrimDouble: {
2590 locations->SetInAt(0, Location::RequiresFpuRegister());
2591 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002592 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002593 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002594 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002595
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002596 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002597 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002598 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002599}
2600
2601void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2602 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002603 Location out = locations->Out();
2604 Location first = locations->InAt(0);
2605 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002606 switch (add->GetResultType()) {
2607 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002608 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002609 __ add(out.AsRegister<Register>(),
2610 first.AsRegister<Register>(),
2611 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002612 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002613 __ AddConstant(out.AsRegister<Register>(),
2614 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002615 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002616 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002617 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002618
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002619 case Primitive::kPrimLong: {
2620 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002621 __ adds(out.AsRegisterPairLow<Register>(),
2622 first.AsRegisterPairLow<Register>(),
2623 ShifterOperand(second.AsRegisterPairLow<Register>()));
2624 __ adc(out.AsRegisterPairHigh<Register>(),
2625 first.AsRegisterPairHigh<Register>(),
2626 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002627 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002628 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002629
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002630 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002631 __ vadds(out.AsFpuRegister<SRegister>(),
2632 first.AsFpuRegister<SRegister>(),
2633 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002634 break;
2635
2636 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002637 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2638 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2639 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002640 break;
2641
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002642 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002643 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002644 }
2645}
2646
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002647void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002648 LocationSummary* locations =
2649 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002650 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002651 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002652 locations->SetInAt(0, Location::RequiresRegister());
2653 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2655 break;
2656 }
2657
2658 case Primitive::kPrimLong: {
2659 locations->SetInAt(0, Location::RequiresRegister());
2660 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002662 break;
2663 }
Calin Juravle11351682014-10-23 15:38:15 +01002664 case Primitive::kPrimFloat:
2665 case Primitive::kPrimDouble: {
2666 locations->SetInAt(0, Location::RequiresFpuRegister());
2667 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002669 break;
Calin Juravle11351682014-10-23 15:38:15 +01002670 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002671 default:
Calin Juravle11351682014-10-23 15:38:15 +01002672 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002673 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002674}
2675
2676void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2677 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002678 Location out = locations->Out();
2679 Location first = locations->InAt(0);
2680 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002681 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002682 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002683 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002684 __ sub(out.AsRegister<Register>(),
2685 first.AsRegister<Register>(),
2686 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002687 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 __ AddConstant(out.AsRegister<Register>(),
2689 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002690 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002691 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002692 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002693 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002694
Calin Juravle11351682014-10-23 15:38:15 +01002695 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002696 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002697 __ subs(out.AsRegisterPairLow<Register>(),
2698 first.AsRegisterPairLow<Register>(),
2699 ShifterOperand(second.AsRegisterPairLow<Register>()));
2700 __ sbc(out.AsRegisterPairHigh<Register>(),
2701 first.AsRegisterPairHigh<Register>(),
2702 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002703 break;
Calin Juravle11351682014-10-23 15:38:15 +01002704 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002705
Calin Juravle11351682014-10-23 15:38:15 +01002706 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002707 __ vsubs(out.AsFpuRegister<SRegister>(),
2708 first.AsFpuRegister<SRegister>(),
2709 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002710 break;
Calin Juravle11351682014-10-23 15:38:15 +01002711 }
2712
2713 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002714 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2715 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2716 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002717 break;
2718 }
2719
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002720
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002721 default:
Calin Juravle11351682014-10-23 15:38:15 +01002722 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002723 }
2724}
2725
Calin Juravle34bacdf2014-10-07 20:23:36 +01002726void LocationsBuilderARM::VisitMul(HMul* mul) {
2727 LocationSummary* locations =
2728 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2729 switch (mul->GetResultType()) {
2730 case Primitive::kPrimInt:
2731 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002732 locations->SetInAt(0, Location::RequiresRegister());
2733 locations->SetInAt(1, Location::RequiresRegister());
2734 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002735 break;
2736 }
2737
Calin Juravleb5bfa962014-10-21 18:02:24 +01002738 case Primitive::kPrimFloat:
2739 case Primitive::kPrimDouble: {
2740 locations->SetInAt(0, Location::RequiresFpuRegister());
2741 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002742 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002743 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002744 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002745
2746 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002747 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002748 }
2749}
2750
2751void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2752 LocationSummary* locations = mul->GetLocations();
2753 Location out = locations->Out();
2754 Location first = locations->InAt(0);
2755 Location second = locations->InAt(1);
2756 switch (mul->GetResultType()) {
2757 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002758 __ mul(out.AsRegister<Register>(),
2759 first.AsRegister<Register>(),
2760 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002761 break;
2762 }
2763 case Primitive::kPrimLong: {
2764 Register out_hi = out.AsRegisterPairHigh<Register>();
2765 Register out_lo = out.AsRegisterPairLow<Register>();
2766 Register in1_hi = first.AsRegisterPairHigh<Register>();
2767 Register in1_lo = first.AsRegisterPairLow<Register>();
2768 Register in2_hi = second.AsRegisterPairHigh<Register>();
2769 Register in2_lo = second.AsRegisterPairLow<Register>();
2770
2771 // Extra checks to protect caused by the existence of R1_R2.
2772 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2773 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2774 DCHECK_NE(out_hi, in1_lo);
2775 DCHECK_NE(out_hi, in2_lo);
2776
2777 // input: in1 - 64 bits, in2 - 64 bits
2778 // output: out
2779 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2780 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2781 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2782
2783 // IP <- in1.lo * in2.hi
2784 __ mul(IP, in1_lo, in2_hi);
2785 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2786 __ mla(out_hi, in1_hi, in2_lo, IP);
2787 // out.lo <- (in1.lo * in2.lo)[31:0];
2788 __ umull(out_lo, IP, in1_lo, in2_lo);
2789 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2790 __ add(out_hi, out_hi, ShifterOperand(IP));
2791 break;
2792 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002793
2794 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002795 __ vmuls(out.AsFpuRegister<SRegister>(),
2796 first.AsFpuRegister<SRegister>(),
2797 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002798 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002799 }
2800
2801 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002802 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2803 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2804 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002805 break;
2806 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002807
2808 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002809 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002810 }
2811}
2812
Zheng Xuc6667102015-05-15 16:08:45 +08002813void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2814 DCHECK(instruction->IsDiv() || instruction->IsRem());
2815 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2816
2817 LocationSummary* locations = instruction->GetLocations();
2818 Location second = locations->InAt(1);
2819 DCHECK(second.IsConstant());
2820
2821 Register out = locations->Out().AsRegister<Register>();
2822 Register dividend = locations->InAt(0).AsRegister<Register>();
2823 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2824 DCHECK(imm == 1 || imm == -1);
2825
2826 if (instruction->IsRem()) {
2827 __ LoadImmediate(out, 0);
2828 } else {
2829 if (imm == 1) {
2830 __ Mov(out, dividend);
2831 } else {
2832 __ rsb(out, dividend, ShifterOperand(0));
2833 }
2834 }
2835}
2836
2837void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2838 DCHECK(instruction->IsDiv() || instruction->IsRem());
2839 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2840
2841 LocationSummary* locations = instruction->GetLocations();
2842 Location second = locations->InAt(1);
2843 DCHECK(second.IsConstant());
2844
2845 Register out = locations->Out().AsRegister<Register>();
2846 Register dividend = locations->InAt(0).AsRegister<Register>();
2847 Register temp = locations->GetTemp(0).AsRegister<Register>();
2848 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002849 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002850 int ctz_imm = CTZ(abs_imm);
2851
2852 if (ctz_imm == 1) {
2853 __ Lsr(temp, dividend, 32 - ctz_imm);
2854 } else {
2855 __ Asr(temp, dividend, 31);
2856 __ Lsr(temp, temp, 32 - ctz_imm);
2857 }
2858 __ add(out, temp, ShifterOperand(dividend));
2859
2860 if (instruction->IsDiv()) {
2861 __ Asr(out, out, ctz_imm);
2862 if (imm < 0) {
2863 __ rsb(out, out, ShifterOperand(0));
2864 }
2865 } else {
2866 __ ubfx(out, out, 0, ctz_imm);
2867 __ sub(out, out, ShifterOperand(temp));
2868 }
2869}
2870
2871void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2872 DCHECK(instruction->IsDiv() || instruction->IsRem());
2873 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2874
2875 LocationSummary* locations = instruction->GetLocations();
2876 Location second = locations->InAt(1);
2877 DCHECK(second.IsConstant());
2878
2879 Register out = locations->Out().AsRegister<Register>();
2880 Register dividend = locations->InAt(0).AsRegister<Register>();
2881 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2882 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2883 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2884
2885 int64_t magic;
2886 int shift;
2887 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2888
2889 __ LoadImmediate(temp1, magic);
2890 __ smull(temp2, temp1, dividend, temp1);
2891
2892 if (imm > 0 && magic < 0) {
2893 __ add(temp1, temp1, ShifterOperand(dividend));
2894 } else if (imm < 0 && magic > 0) {
2895 __ sub(temp1, temp1, ShifterOperand(dividend));
2896 }
2897
2898 if (shift != 0) {
2899 __ Asr(temp1, temp1, shift);
2900 }
2901
2902 if (instruction->IsDiv()) {
2903 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2904 } else {
2905 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2906 // TODO: Strength reduction for mls.
2907 __ LoadImmediate(temp2, imm);
2908 __ mls(out, temp1, temp2, dividend);
2909 }
2910}
2911
2912void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2913 DCHECK(instruction->IsDiv() || instruction->IsRem());
2914 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2915
2916 LocationSummary* locations = instruction->GetLocations();
2917 Location second = locations->InAt(1);
2918 DCHECK(second.IsConstant());
2919
2920 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2921 if (imm == 0) {
2922 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2923 } else if (imm == 1 || imm == -1) {
2924 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002925 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002926 DivRemByPowerOfTwo(instruction);
2927 } else {
2928 DCHECK(imm <= -2 || imm >= 2);
2929 GenerateDivRemWithAnyConstant(instruction);
2930 }
2931}
2932
Calin Juravle7c4954d2014-10-28 16:57:40 +00002933void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002934 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2935 if (div->GetResultType() == Primitive::kPrimLong) {
2936 // pLdiv runtime call.
2937 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002938 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2939 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002940 } else if (div->GetResultType() == Primitive::kPrimInt &&
2941 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2942 // pIdivmod runtime call.
2943 call_kind = LocationSummary::kCall;
2944 }
2945
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2947
Calin Juravle7c4954d2014-10-28 16:57:40 +00002948 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002949 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002950 if (div->InputAt(1)->IsConstant()) {
2951 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002952 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002953 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002954 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2955 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002956 // No temp register required.
2957 } else {
2958 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002959 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002960 locations->AddTemp(Location::RequiresRegister());
2961 }
2962 }
2963 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002964 locations->SetInAt(0, Location::RequiresRegister());
2965 locations->SetInAt(1, Location::RequiresRegister());
2966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2967 } else {
2968 InvokeRuntimeCallingConvention calling_convention;
2969 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2970 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2971 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2972 // we only need the former.
2973 locations->SetOut(Location::RegisterLocation(R0));
2974 }
Calin Juravled0d48522014-11-04 16:40:20 +00002975 break;
2976 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002977 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002978 InvokeRuntimeCallingConvention calling_convention;
2979 locations->SetInAt(0, Location::RegisterPairLocation(
2980 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2981 locations->SetInAt(1, Location::RegisterPairLocation(
2982 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002983 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002984 break;
2985 }
2986 case Primitive::kPrimFloat:
2987 case Primitive::kPrimDouble: {
2988 locations->SetInAt(0, Location::RequiresFpuRegister());
2989 locations->SetInAt(1, Location::RequiresFpuRegister());
2990 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2991 break;
2992 }
2993
2994 default:
2995 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2996 }
2997}
2998
2999void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3000 LocationSummary* locations = div->GetLocations();
3001 Location out = locations->Out();
3002 Location first = locations->InAt(0);
3003 Location second = locations->InAt(1);
3004
3005 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003006 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003007 if (second.IsConstant()) {
3008 GenerateDivRemConstantIntegral(div);
3009 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003010 __ sdiv(out.AsRegister<Register>(),
3011 first.AsRegister<Register>(),
3012 second.AsRegister<Register>());
3013 } else {
3014 InvokeRuntimeCallingConvention calling_convention;
3015 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3016 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3017 DCHECK_EQ(R0, out.AsRegister<Register>());
3018
3019 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003020 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003021 }
Calin Juravled0d48522014-11-04 16:40:20 +00003022 break;
3023 }
3024
Calin Juravle7c4954d2014-10-28 16:57:40 +00003025 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003026 InvokeRuntimeCallingConvention calling_convention;
3027 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3028 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3029 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3030 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3031 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003032 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003033
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003034 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003035 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003036 break;
3037 }
3038
3039 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003040 __ vdivs(out.AsFpuRegister<SRegister>(),
3041 first.AsFpuRegister<SRegister>(),
3042 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003043 break;
3044 }
3045
3046 case Primitive::kPrimDouble: {
3047 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3048 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3049 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3050 break;
3051 }
3052
3053 default:
3054 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3055 }
3056}
3057
Calin Juravlebacfec32014-11-14 15:54:36 +00003058void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003059 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003060
3061 // Most remainders are implemented in the runtime.
3062 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003063 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3064 // sdiv will be replaced by other instruction sequence.
3065 call_kind = LocationSummary::kNoCall;
3066 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3067 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003068 // Have hardware divide instruction for int, do it with three instructions.
3069 call_kind = LocationSummary::kNoCall;
3070 }
3071
Calin Juravlebacfec32014-11-14 15:54:36 +00003072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3073
Calin Juravled2ec87d2014-12-08 14:24:46 +00003074 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003075 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003076 if (rem->InputAt(1)->IsConstant()) {
3077 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003078 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003079 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003080 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3081 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003082 // No temp register required.
3083 } else {
3084 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003085 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003086 locations->AddTemp(Location::RequiresRegister());
3087 }
3088 }
3089 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003090 locations->SetInAt(0, Location::RequiresRegister());
3091 locations->SetInAt(1, Location::RequiresRegister());
3092 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3093 locations->AddTemp(Location::RequiresRegister());
3094 } else {
3095 InvokeRuntimeCallingConvention calling_convention;
3096 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3097 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3098 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3099 // we only need the latter.
3100 locations->SetOut(Location::RegisterLocation(R1));
3101 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003102 break;
3103 }
3104 case Primitive::kPrimLong: {
3105 InvokeRuntimeCallingConvention calling_convention;
3106 locations->SetInAt(0, Location::RegisterPairLocation(
3107 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3108 locations->SetInAt(1, Location::RegisterPairLocation(
3109 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3110 // The runtime helper puts the output in R2,R3.
3111 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3112 break;
3113 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003114 case Primitive::kPrimFloat: {
3115 InvokeRuntimeCallingConvention calling_convention;
3116 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3117 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3118 locations->SetOut(Location::FpuRegisterLocation(S0));
3119 break;
3120 }
3121
Calin Juravlebacfec32014-11-14 15:54:36 +00003122 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003123 InvokeRuntimeCallingConvention calling_convention;
3124 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3125 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3126 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3127 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3128 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003129 break;
3130 }
3131
3132 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003133 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003134 }
3135}
3136
3137void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3138 LocationSummary* locations = rem->GetLocations();
3139 Location out = locations->Out();
3140 Location first = locations->InAt(0);
3141 Location second = locations->InAt(1);
3142
Calin Juravled2ec87d2014-12-08 14:24:46 +00003143 Primitive::Type type = rem->GetResultType();
3144 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003145 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003146 if (second.IsConstant()) {
3147 GenerateDivRemConstantIntegral(rem);
3148 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003149 Register reg1 = first.AsRegister<Register>();
3150 Register reg2 = second.AsRegister<Register>();
3151 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003152
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003153 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003154 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003155 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003156 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003157 } else {
3158 InvokeRuntimeCallingConvention calling_convention;
3159 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3160 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3161 DCHECK_EQ(R1, out.AsRegister<Register>());
3162
3163 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003164 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003165 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003166 break;
3167 }
3168
3169 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003170 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003171 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003172 break;
3173 }
3174
Calin Juravled2ec87d2014-12-08 14:24:46 +00003175 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003176 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003177 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003178 break;
3179 }
3180
Calin Juravlebacfec32014-11-14 15:54:36 +00003181 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003182 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003183 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003184 break;
3185 }
3186
3187 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003188 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003189 }
3190}
3191
Calin Juravled0d48522014-11-04 16:40:20 +00003192void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003193 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3194 ? LocationSummary::kCallOnSlowPath
3195 : LocationSummary::kNoCall;
3196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003197 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003198 if (instruction->HasUses()) {
3199 locations->SetOut(Location::SameAsFirstInput());
3200 }
3201}
3202
3203void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003204 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003205 codegen_->AddSlowPath(slow_path);
3206
3207 LocationSummary* locations = instruction->GetLocations();
3208 Location value = locations->InAt(0);
3209
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003210 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003211 case Primitive::kPrimByte:
3212 case Primitive::kPrimChar:
3213 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003214 case Primitive::kPrimInt: {
3215 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003216 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003217 } else {
3218 DCHECK(value.IsConstant()) << value;
3219 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3220 __ b(slow_path->GetEntryLabel());
3221 }
3222 }
3223 break;
3224 }
3225 case Primitive::kPrimLong: {
3226 if (value.IsRegisterPair()) {
3227 __ orrs(IP,
3228 value.AsRegisterPairLow<Register>(),
3229 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3230 __ b(slow_path->GetEntryLabel(), EQ);
3231 } else {
3232 DCHECK(value.IsConstant()) << value;
3233 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3234 __ b(slow_path->GetEntryLabel());
3235 }
3236 }
3237 break;
3238 default:
3239 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3240 }
3241 }
Calin Juravled0d48522014-11-04 16:40:20 +00003242}
3243
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003244void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3245 Register in = locations->InAt(0).AsRegister<Register>();
3246 Location rhs = locations->InAt(1);
3247 Register out = locations->Out().AsRegister<Register>();
3248
3249 if (rhs.IsConstant()) {
3250 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3251 // so map all rotations to a +ve. equivalent in that range.
3252 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3253 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3254 if (rot) {
3255 // Rotate, mapping left rotations to right equivalents if necessary.
3256 // (e.g. left by 2 bits == right by 30.)
3257 __ Ror(out, in, rot);
3258 } else if (out != in) {
3259 __ Mov(out, in);
3260 }
3261 } else {
3262 __ Ror(out, in, rhs.AsRegister<Register>());
3263 }
3264}
3265
3266// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3267// rotates by swapping input regs (effectively rotating by the first 32-bits of
3268// a larger rotation) or flipping direction (thus treating larger right/left
3269// rotations as sub-word sized rotations in the other direction) as appropriate.
3270void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3271 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3272 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3273 Location rhs = locations->InAt(1);
3274 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3275 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3276
3277 if (rhs.IsConstant()) {
3278 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3279 // Map all rotations to +ve. equivalents on the interval [0,63].
3280 rot &= kMaxLongShiftValue;
3281 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3282 // logic below to a simple pair of binary orr.
3283 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3284 if (rot >= kArmBitsPerWord) {
3285 rot -= kArmBitsPerWord;
3286 std::swap(in_reg_hi, in_reg_lo);
3287 }
3288 // Rotate, or mov to out for zero or word size rotations.
3289 if (rot != 0u) {
3290 __ Lsr(out_reg_hi, in_reg_hi, rot);
3291 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3292 __ Lsr(out_reg_lo, in_reg_lo, rot);
3293 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3294 } else {
3295 __ Mov(out_reg_lo, in_reg_lo);
3296 __ Mov(out_reg_hi, in_reg_hi);
3297 }
3298 } else {
3299 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3300 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3301 Label end;
3302 Label shift_by_32_plus_shift_right;
3303
3304 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3305 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3306 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3307 __ b(&shift_by_32_plus_shift_right, CC);
3308
3309 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3310 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3311 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3312 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3313 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3314 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3315 __ Lsr(shift_left, in_reg_hi, shift_right);
3316 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3317 __ b(&end);
3318
3319 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3320 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3321 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3322 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3323 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3324 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3325 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3326 __ Lsl(shift_right, in_reg_hi, shift_left);
3327 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3328
3329 __ Bind(&end);
3330 }
3331}
3332void LocationsBuilderARM::HandleRotate(HRor* ror) {
3333 LocationSummary* locations =
3334 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3335 switch (ror->GetResultType()) {
3336 case Primitive::kPrimInt: {
3337 locations->SetInAt(0, Location::RequiresRegister());
3338 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3340 break;
3341 }
3342 case Primitive::kPrimLong: {
3343 locations->SetInAt(0, Location::RequiresRegister());
3344 if (ror->InputAt(1)->IsConstant()) {
3345 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3346 } else {
3347 locations->SetInAt(1, Location::RequiresRegister());
3348 locations->AddTemp(Location::RequiresRegister());
3349 locations->AddTemp(Location::RequiresRegister());
3350 }
3351 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3352 break;
3353 }
3354 default:
3355 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3356 }
3357}
3358
3359void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3360 LocationSummary* locations = ror->GetLocations();
3361 Primitive::Type type = ror->GetResultType();
3362 switch (type) {
3363 case Primitive::kPrimInt: {
3364 HandleIntegerRotate(locations);
3365 break;
3366 }
3367 case Primitive::kPrimLong: {
3368 HandleLongRotate(locations);
3369 break;
3370 }
3371 default:
3372 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003373 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003374 }
3375}
3376
3377void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003378 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003379}
3380
3381void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003382 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003383}
3384
Calin Juravle9aec02f2014-11-18 23:06:35 +00003385void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3386 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3387
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003388 LocationSummary* locations =
3389 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003390
3391 switch (op->GetResultType()) {
3392 case Primitive::kPrimInt: {
3393 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003394 if (op->InputAt(1)->IsConstant()) {
3395 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3396 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3397 } else {
3398 locations->SetInAt(1, Location::RequiresRegister());
3399 // Make the output overlap, as it will be used to hold the masked
3400 // second input.
3401 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3402 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003403 break;
3404 }
3405 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003406 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003407 if (op->InputAt(1)->IsConstant()) {
3408 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3409 // For simplicity, use kOutputOverlap even though we only require that low registers
3410 // don't clash with high registers which the register allocator currently guarantees.
3411 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3412 } else {
3413 locations->SetInAt(1, Location::RequiresRegister());
3414 locations->AddTemp(Location::RequiresRegister());
3415 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3416 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003417 break;
3418 }
3419 default:
3420 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3421 }
3422}
3423
3424void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3425 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3426
3427 LocationSummary* locations = op->GetLocations();
3428 Location out = locations->Out();
3429 Location first = locations->InAt(0);
3430 Location second = locations->InAt(1);
3431
3432 Primitive::Type type = op->GetResultType();
3433 switch (type) {
3434 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003435 Register out_reg = out.AsRegister<Register>();
3436 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003437 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003438 Register second_reg = second.AsRegister<Register>();
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003439 // Arm doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003440 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003441 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003442 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003443 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003444 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003445 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003446 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003447 }
3448 } else {
3449 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3450 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
3451 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
3452 __ Mov(out_reg, first_reg);
3453 } else if (op->IsShl()) {
3454 __ Lsl(out_reg, first_reg, shift_value);
3455 } else if (op->IsShr()) {
3456 __ Asr(out_reg, first_reg, shift_value);
3457 } else {
3458 __ Lsr(out_reg, first_reg, shift_value);
3459 }
3460 }
3461 break;
3462 }
3463 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003464 Register o_h = out.AsRegisterPairHigh<Register>();
3465 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003466
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003467 Register high = first.AsRegisterPairHigh<Register>();
3468 Register low = first.AsRegisterPairLow<Register>();
3469
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003470 if (second.IsRegister()) {
3471 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003472
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003473 Register second_reg = second.AsRegister<Register>();
3474
3475 if (op->IsShl()) {
3476 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3477 // Shift the high part
3478 __ Lsl(o_h, high, o_l);
3479 // Shift the low part and `or` what overflew on the high part
3480 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3481 __ Lsr(temp, low, temp);
3482 __ orr(o_h, o_h, ShifterOperand(temp));
3483 // If the shift is > 32 bits, override the high part
3484 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3485 __ it(PL);
3486 __ Lsl(o_h, low, temp, PL);
3487 // Shift the low part
3488 __ Lsl(o_l, low, o_l);
3489 } else if (op->IsShr()) {
3490 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3491 // Shift the low part
3492 __ Lsr(o_l, low, o_h);
3493 // Shift the high part and `or` what underflew on the low part
3494 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3495 __ Lsl(temp, high, temp);
3496 __ orr(o_l, o_l, ShifterOperand(temp));
3497 // If the shift is > 32 bits, override the low part
3498 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3499 __ it(PL);
3500 __ Asr(o_l, high, temp, PL);
3501 // Shift the high part
3502 __ Asr(o_h, high, o_h);
3503 } else {
3504 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3505 // same as Shr except we use `Lsr`s and not `Asr`s
3506 __ Lsr(o_l, low, o_h);
3507 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3508 __ Lsl(temp, high, temp);
3509 __ orr(o_l, o_l, ShifterOperand(temp));
3510 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3511 __ it(PL);
3512 __ Lsr(o_l, high, temp, PL);
3513 __ Lsr(o_h, high, o_h);
3514 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003515 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003516 // Register allocator doesn't create partial overlap.
3517 DCHECK_NE(o_l, high);
3518 DCHECK_NE(o_h, low);
3519 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3520 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3521 if (shift_value > 32) {
3522 if (op->IsShl()) {
3523 __ Lsl(o_h, low, shift_value - 32);
3524 __ LoadImmediate(o_l, 0);
3525 } else if (op->IsShr()) {
3526 __ Asr(o_l, high, shift_value - 32);
3527 __ Asr(o_h, high, 31);
3528 } else {
3529 __ Lsr(o_l, high, shift_value - 32);
3530 __ LoadImmediate(o_h, 0);
3531 }
3532 } else if (shift_value == 32) {
3533 if (op->IsShl()) {
3534 __ mov(o_h, ShifterOperand(low));
3535 __ LoadImmediate(o_l, 0);
3536 } else if (op->IsShr()) {
3537 __ mov(o_l, ShifterOperand(high));
3538 __ Asr(o_h, high, 31);
3539 } else {
3540 __ mov(o_l, ShifterOperand(high));
3541 __ LoadImmediate(o_h, 0);
3542 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003543 } else if (shift_value == 1) {
3544 if (op->IsShl()) {
3545 __ Lsls(o_l, low, 1);
3546 __ adc(o_h, high, ShifterOperand(high));
3547 } else if (op->IsShr()) {
3548 __ Asrs(o_h, high, 1);
3549 __ Rrx(o_l, low);
3550 } else {
3551 __ Lsrs(o_h, high, 1);
3552 __ Rrx(o_l, low);
3553 }
3554 } else {
3555 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003556 if (op->IsShl()) {
3557 __ Lsl(o_h, high, shift_value);
3558 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3559 __ Lsl(o_l, low, shift_value);
3560 } else if (op->IsShr()) {
3561 __ Lsr(o_l, low, shift_value);
3562 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3563 __ Asr(o_h, high, shift_value);
3564 } else {
3565 __ Lsr(o_l, low, shift_value);
3566 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3567 __ Lsr(o_h, high, shift_value);
3568 }
3569 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003570 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003571 break;
3572 }
3573 default:
3574 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003575 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003576 }
3577}
3578
3579void LocationsBuilderARM::VisitShl(HShl* shl) {
3580 HandleShift(shl);
3581}
3582
3583void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3584 HandleShift(shl);
3585}
3586
3587void LocationsBuilderARM::VisitShr(HShr* shr) {
3588 HandleShift(shr);
3589}
3590
3591void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3592 HandleShift(shr);
3593}
3594
3595void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3596 HandleShift(ushr);
3597}
3598
3599void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3600 HandleShift(ushr);
3601}
3602
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003603void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003604 LocationSummary* locations =
3605 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003606 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003607 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3608 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003609 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003610}
3611
3612void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003613 // Note: if heap poisoning is enabled, the entry point takes cares
3614 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003615 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003616 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003617 instruction->GetDexPc(),
3618 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003619 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003620}
3621
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003622void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3623 LocationSummary* locations =
3624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3625 InvokeRuntimeCallingConvention calling_convention;
3626 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003627 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003628 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003629 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003630}
3631
3632void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3633 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003634 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003635 // Note: if heap poisoning is enabled, the entry point takes cares
3636 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003637 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003638 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003639 instruction->GetDexPc(),
3640 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003641 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003642}
3643
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003644void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003645 LocationSummary* locations =
3646 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003647 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3648 if (location.IsStackSlot()) {
3649 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3650 } else if (location.IsDoubleStackSlot()) {
3651 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003652 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003653 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003654}
3655
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003656void InstructionCodeGeneratorARM::VisitParameterValue(
3657 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003658 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003659}
3660
3661void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3662 LocationSummary* locations =
3663 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3664 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3665}
3666
3667void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3668 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003669}
3670
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003671void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003672 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003673 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003674 locations->SetInAt(0, Location::RequiresRegister());
3675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003676}
3677
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003678void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3679 LocationSummary* locations = not_->GetLocations();
3680 Location out = locations->Out();
3681 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003682 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003683 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003685 break;
3686
3687 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003688 __ mvn(out.AsRegisterPairLow<Register>(),
3689 ShifterOperand(in.AsRegisterPairLow<Register>()));
3690 __ mvn(out.AsRegisterPairHigh<Register>(),
3691 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003692 break;
3693
3694 default:
3695 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3696 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003697}
3698
David Brazdil66d126e2015-04-03 16:02:44 +01003699void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3700 LocationSummary* locations =
3701 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3702 locations->SetInAt(0, Location::RequiresRegister());
3703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3704}
3705
3706void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003707 LocationSummary* locations = bool_not->GetLocations();
3708 Location out = locations->Out();
3709 Location in = locations->InAt(0);
3710 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3711}
3712
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003713void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003714 LocationSummary* locations =
3715 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003716 switch (compare->InputAt(0)->GetType()) {
3717 case Primitive::kPrimLong: {
3718 locations->SetInAt(0, Location::RequiresRegister());
3719 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003720 // Output overlaps because it is written before doing the low comparison.
3721 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003722 break;
3723 }
3724 case Primitive::kPrimFloat:
3725 case Primitive::kPrimDouble: {
3726 locations->SetInAt(0, Location::RequiresFpuRegister());
3727 locations->SetInAt(1, Location::RequiresFpuRegister());
3728 locations->SetOut(Location::RequiresRegister());
3729 break;
3730 }
3731 default:
3732 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3733 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003734}
3735
3736void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003737 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003739 Location left = locations->InAt(0);
3740 Location right = locations->InAt(1);
3741
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003742 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003743 Primitive::Type type = compare->InputAt(0)->GetType();
3744 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003745 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003746 __ cmp(left.AsRegisterPairHigh<Register>(),
3747 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003748 __ b(&less, LT);
3749 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003750 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003751 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003752 __ cmp(left.AsRegisterPairLow<Register>(),
3753 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003754 break;
3755 }
3756 case Primitive::kPrimFloat:
3757 case Primitive::kPrimDouble: {
3758 __ LoadImmediate(out, 0);
3759 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003760 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003761 } else {
3762 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3763 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3764 }
3765 __ vmstat(); // transfer FP status register to ARM APSR.
3766 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003767 break;
3768 }
3769 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003770 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003771 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003772 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003773 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003774
3775 __ Bind(&greater);
3776 __ LoadImmediate(out, 1);
3777 __ b(&done);
3778
3779 __ Bind(&less);
3780 __ LoadImmediate(out, -1);
3781
3782 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003783}
3784
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003785void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003786 LocationSummary* locations =
3787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003788 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3789 locations->SetInAt(i, Location::Any());
3790 }
3791 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003792}
3793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003794void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003795 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003796}
3797
Calin Juravle52c48962014-12-16 17:02:57 +00003798void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3799 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003800 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003801 switch (kind) {
3802 case MemBarrierKind::kAnyStore:
3803 case MemBarrierKind::kLoadAny:
3804 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003805 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003806 break;
3807 }
3808 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003809 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003810 break;
3811 }
3812 default:
3813 LOG(FATAL) << "Unexpected memory barrier " << kind;
3814 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003815 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003816}
3817
3818void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3819 uint32_t offset,
3820 Register out_lo,
3821 Register out_hi) {
3822 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003823 // Ensure `out_lo` is different from `addr`, so that loading
3824 // `offset` into `out_lo` does not clutter `addr`.
3825 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003826 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003827 __ add(IP, addr, ShifterOperand(out_lo));
3828 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003829 }
3830 __ ldrexd(out_lo, out_hi, addr);
3831}
3832
3833void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3834 uint32_t offset,
3835 Register value_lo,
3836 Register value_hi,
3837 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003838 Register temp2,
3839 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003840 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003841 if (offset != 0) {
3842 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003843 __ add(IP, addr, ShifterOperand(temp1));
3844 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003845 }
3846 __ Bind(&fail);
3847 // We need a load followed by store. (The address used in a STREX instruction must
3848 // be the same as the address in the most recently executed LDREX instruction.)
3849 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003850 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003851 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003852 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003853}
3854
3855void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3856 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3857
Nicolas Geoffray39468442014-09-02 15:17:15 +01003858 LocationSummary* locations =
3859 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003860 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003861
Calin Juravle52c48962014-12-16 17:02:57 +00003862 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003863 if (Primitive::IsFloatingPointType(field_type)) {
3864 locations->SetInAt(1, Location::RequiresFpuRegister());
3865 } else {
3866 locations->SetInAt(1, Location::RequiresRegister());
3867 }
3868
Calin Juravle52c48962014-12-16 17:02:57 +00003869 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003870 bool generate_volatile = field_info.IsVolatile()
3871 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003872 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003873 bool needs_write_barrier =
3874 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003875 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003876 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003877 if (needs_write_barrier) {
3878 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003879 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003880 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003881 // Arm encoding have some additional constraints for ldrexd/strexd:
3882 // - registers need to be consecutive
3883 // - the first register should be even but not R14.
3884 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3885 // enable Arm encoding.
3886 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3887
3888 locations->AddTemp(Location::RequiresRegister());
3889 locations->AddTemp(Location::RequiresRegister());
3890 if (field_type == Primitive::kPrimDouble) {
3891 // For doubles we need two more registers to copy the value.
3892 locations->AddTemp(Location::RegisterLocation(R2));
3893 locations->AddTemp(Location::RegisterLocation(R3));
3894 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003895 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003896}
3897
Calin Juravle52c48962014-12-16 17:02:57 +00003898void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003899 const FieldInfo& field_info,
3900 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003901 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003903 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003904 Register base = locations->InAt(0).AsRegister<Register>();
3905 Location value = locations->InAt(1);
3906
3907 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003908 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003909 Primitive::Type field_type = field_info.GetFieldType();
3910 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003911 bool needs_write_barrier =
3912 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003913
3914 if (is_volatile) {
3915 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3916 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003917
3918 switch (field_type) {
3919 case Primitive::kPrimBoolean:
3920 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003921 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003922 break;
3923 }
3924
3925 case Primitive::kPrimShort:
3926 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003927 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003928 break;
3929 }
3930
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003932 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003933 if (kPoisonHeapReferences && needs_write_barrier) {
3934 // Note that in the case where `value` is a null reference,
3935 // we do not enter this block, as a null reference does not
3936 // need poisoning.
3937 DCHECK_EQ(field_type, Primitive::kPrimNot);
3938 Register temp = locations->GetTemp(0).AsRegister<Register>();
3939 __ Mov(temp, value.AsRegister<Register>());
3940 __ PoisonHeapReference(temp);
3941 __ StoreToOffset(kStoreWord, temp, base, offset);
3942 } else {
3943 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3944 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003945 break;
3946 }
3947
3948 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003949 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003950 GenerateWideAtomicStore(base, offset,
3951 value.AsRegisterPairLow<Register>(),
3952 value.AsRegisterPairHigh<Register>(),
3953 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003954 locations->GetTemp(1).AsRegister<Register>(),
3955 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003956 } else {
3957 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003958 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003959 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003960 break;
3961 }
3962
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003963 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003964 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003965 break;
3966 }
3967
3968 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003969 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003970 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003971 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3972 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3973
3974 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3975
3976 GenerateWideAtomicStore(base, offset,
3977 value_reg_lo,
3978 value_reg_hi,
3979 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003980 locations->GetTemp(3).AsRegister<Register>(),
3981 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003982 } else {
3983 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003984 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003985 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003986 break;
3987 }
3988
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003989 case Primitive::kPrimVoid:
3990 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003991 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003992 }
Calin Juravle52c48962014-12-16 17:02:57 +00003993
Calin Juravle77520bc2015-01-12 18:45:46 +00003994 // Longs and doubles are handled in the switch.
3995 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3996 codegen_->MaybeRecordImplicitNullCheck(instruction);
3997 }
3998
3999 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4000 Register temp = locations->GetTemp(0).AsRegister<Register>();
4001 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004002 codegen_->MarkGCCard(
4003 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004004 }
4005
Calin Juravle52c48962014-12-16 17:02:57 +00004006 if (is_volatile) {
4007 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4008 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004009}
4010
Calin Juravle52c48962014-12-16 17:02:57 +00004011void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4012 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004013
4014 bool object_field_get_with_read_barrier =
4015 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004016 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004017 new (GetGraph()->GetArena()) LocationSummary(instruction,
4018 object_field_get_with_read_barrier ?
4019 LocationSummary::kCallOnSlowPath :
4020 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004021 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004022
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004023 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004024 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004025 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004026 // The output overlaps in case of volatile long: we don't want the
4027 // code generated by GenerateWideAtomicLoad to overwrite the
4028 // object's location. Likewise, in the case of an object field get
4029 // with read barriers enabled, we do not want the load to overwrite
4030 // the object's location, as we need it to emit the read barrier.
4031 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4032 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004033
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004034 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4035 locations->SetOut(Location::RequiresFpuRegister());
4036 } else {
4037 locations->SetOut(Location::RequiresRegister(),
4038 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4039 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004040 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00004041 // Arm encoding have some additional constraints for ldrexd/strexd:
4042 // - registers need to be consecutive
4043 // - the first register should be even but not R14.
4044 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
4045 // enable Arm encoding.
4046 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4047 locations->AddTemp(Location::RequiresRegister());
4048 locations->AddTemp(Location::RequiresRegister());
4049 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004050}
4051
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004052Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4053 Opcode opcode) {
4054 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4055 if (constant->IsConstant() &&
4056 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4057 return Location::ConstantLocation(constant->AsConstant());
4058 }
4059 return Location::RequiresRegister();
4060}
4061
4062bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4063 Opcode opcode) {
4064 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4065 if (Primitive::Is64BitType(input_cst->GetType())) {
4066 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4067 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4068 } else {
4069 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4070 }
4071}
4072
4073bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4074 ShifterOperand so;
4075 ArmAssembler* assembler = codegen_->GetAssembler();
4076 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4077 return true;
4078 }
4079 Opcode neg_opcode = kNoOperand;
4080 switch (opcode) {
4081 case AND:
4082 neg_opcode = BIC;
4083 break;
4084 case ORR:
4085 neg_opcode = ORN;
4086 break;
4087 default:
4088 return false;
4089 }
4090 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4091}
4092
Calin Juravle52c48962014-12-16 17:02:57 +00004093void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4094 const FieldInfo& field_info) {
4095 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004096
Calin Juravle52c48962014-12-16 17:02:57 +00004097 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004098 Location base_loc = locations->InAt(0);
4099 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004100 Location out = locations->Out();
4101 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004102 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004103 Primitive::Type field_type = field_info.GetFieldType();
4104 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4105
4106 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004108 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004109 break;
4110 }
4111
4112 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004113 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004114 break;
4115 }
4116
4117 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004118 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004119 break;
4120 }
4121
4122 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004123 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004124 break;
4125 }
4126
4127 case Primitive::kPrimInt:
4128 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00004129 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004130 break;
4131 }
4132
4133 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004134 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004135 GenerateWideAtomicLoad(base, offset,
4136 out.AsRegisterPairLow<Register>(),
4137 out.AsRegisterPairHigh<Register>());
4138 } else {
4139 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4140 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004141 break;
4142 }
4143
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004144 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004145 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004146 break;
4147 }
4148
4149 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004150 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004151 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004152 Register lo = locations->GetTemp(0).AsRegister<Register>();
4153 Register hi = locations->GetTemp(1).AsRegister<Register>();
4154 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004155 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004156 __ vmovdrr(out_reg, lo, hi);
4157 } else {
4158 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004159 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004160 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004161 break;
4162 }
4163
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004164 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004165 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004166 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004167 }
Calin Juravle52c48962014-12-16 17:02:57 +00004168
Calin Juravle77520bc2015-01-12 18:45:46 +00004169 // Doubles are handled in the switch.
4170 if (field_type != Primitive::kPrimDouble) {
4171 codegen_->MaybeRecordImplicitNullCheck(instruction);
4172 }
4173
Calin Juravle52c48962014-12-16 17:02:57 +00004174 if (is_volatile) {
4175 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4176 }
Roland Levillain4d027112015-07-01 15:41:14 +01004177
4178 if (field_type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004179 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004180 }
Calin Juravle52c48962014-12-16 17:02:57 +00004181}
4182
4183void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4184 HandleFieldSet(instruction, instruction->GetFieldInfo());
4185}
4186
4187void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004188 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004189}
4190
4191void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4192 HandleFieldGet(instruction, instruction->GetFieldInfo());
4193}
4194
4195void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4196 HandleFieldGet(instruction, instruction->GetFieldInfo());
4197}
4198
4199void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4200 HandleFieldGet(instruction, instruction->GetFieldInfo());
4201}
4202
4203void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4204 HandleFieldGet(instruction, instruction->GetFieldInfo());
4205}
4206
4207void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4208 HandleFieldSet(instruction, instruction->GetFieldInfo());
4209}
4210
4211void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004212 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004213}
4214
Calin Juravlee460d1d2015-09-29 04:52:17 +01004215void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4216 HUnresolvedInstanceFieldGet* instruction) {
4217 FieldAccessCallingConventionARM calling_convention;
4218 codegen_->CreateUnresolvedFieldLocationSummary(
4219 instruction, instruction->GetFieldType(), calling_convention);
4220}
4221
4222void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4223 HUnresolvedInstanceFieldGet* instruction) {
4224 FieldAccessCallingConventionARM calling_convention;
4225 codegen_->GenerateUnresolvedFieldAccess(instruction,
4226 instruction->GetFieldType(),
4227 instruction->GetFieldIndex(),
4228 instruction->GetDexPc(),
4229 calling_convention);
4230}
4231
4232void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4233 HUnresolvedInstanceFieldSet* instruction) {
4234 FieldAccessCallingConventionARM calling_convention;
4235 codegen_->CreateUnresolvedFieldLocationSummary(
4236 instruction, instruction->GetFieldType(), calling_convention);
4237}
4238
4239void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4240 HUnresolvedInstanceFieldSet* instruction) {
4241 FieldAccessCallingConventionARM calling_convention;
4242 codegen_->GenerateUnresolvedFieldAccess(instruction,
4243 instruction->GetFieldType(),
4244 instruction->GetFieldIndex(),
4245 instruction->GetDexPc(),
4246 calling_convention);
4247}
4248
4249void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4250 HUnresolvedStaticFieldGet* instruction) {
4251 FieldAccessCallingConventionARM calling_convention;
4252 codegen_->CreateUnresolvedFieldLocationSummary(
4253 instruction, instruction->GetFieldType(), calling_convention);
4254}
4255
4256void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4257 HUnresolvedStaticFieldGet* instruction) {
4258 FieldAccessCallingConventionARM calling_convention;
4259 codegen_->GenerateUnresolvedFieldAccess(instruction,
4260 instruction->GetFieldType(),
4261 instruction->GetFieldIndex(),
4262 instruction->GetDexPc(),
4263 calling_convention);
4264}
4265
4266void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4267 HUnresolvedStaticFieldSet* instruction) {
4268 FieldAccessCallingConventionARM calling_convention;
4269 codegen_->CreateUnresolvedFieldLocationSummary(
4270 instruction, instruction->GetFieldType(), calling_convention);
4271}
4272
4273void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4274 HUnresolvedStaticFieldSet* instruction) {
4275 FieldAccessCallingConventionARM calling_convention;
4276 codegen_->GenerateUnresolvedFieldAccess(instruction,
4277 instruction->GetFieldType(),
4278 instruction->GetFieldIndex(),
4279 instruction->GetDexPc(),
4280 calling_convention);
4281}
4282
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004284 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4285 ? LocationSummary::kCallOnSlowPath
4286 : LocationSummary::kNoCall;
4287 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004288 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004289 if (instruction->HasUses()) {
4290 locations->SetOut(Location::SameAsFirstInput());
4291 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004292}
4293
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004294void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004295 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4296 return;
4297 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004298 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004299
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004300 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4301 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4302}
4303
4304void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004305 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306 codegen_->AddSlowPath(slow_path);
4307
4308 LocationSummary* locations = instruction->GetLocations();
4309 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004311 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004312}
4313
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004314void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004315 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004316 GenerateImplicitNullCheck(instruction);
4317 } else {
4318 GenerateExplicitNullCheck(instruction);
4319 }
4320}
4321
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004322void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004323 bool object_array_get_with_read_barrier =
4324 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004325 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004326 new (GetGraph()->GetArena()) LocationSummary(instruction,
4327 object_array_get_with_read_barrier ?
4328 LocationSummary::kCallOnSlowPath :
4329 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004330 locations->SetInAt(0, Location::RequiresRegister());
4331 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004332 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4333 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4334 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004335 // The output overlaps in the case of an object array get with
4336 // read barriers enabled: we do not want the move to overwrite the
4337 // array's location, as we need it to emit the read barrier.
4338 locations->SetOut(
4339 Location::RequiresRegister(),
4340 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004341 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004342}
4343
4344void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4345 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004346 Location obj_loc = locations->InAt(0);
4347 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004348 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004349 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004350
Roland Levillain4d027112015-07-01 15:41:14 +01004351 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352 case Primitive::kPrimBoolean: {
4353 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004354 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004355 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004356 size_t offset =
4357 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4359 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004360 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4362 }
4363 break;
4364 }
4365
4366 case Primitive::kPrimByte: {
4367 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004369 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004370 size_t offset =
4371 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004372 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4373 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004374 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004375 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4376 }
4377 break;
4378 }
4379
4380 case Primitive::kPrimShort: {
4381 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004382 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004384 size_t offset =
4385 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004386 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4387 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004388 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004389 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4390 }
4391 break;
4392 }
4393
4394 case Primitive::kPrimChar: {
4395 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004396 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004397 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004398 size_t offset =
4399 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004400 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4401 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004402 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4404 }
4405 break;
4406 }
4407
4408 case Primitive::kPrimInt:
4409 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004410 static_assert(
4411 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4412 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004413 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004414 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004415 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004416 size_t offset =
4417 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004418 __ LoadFromOffset(kLoadWord, out, obj, offset);
4419 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004420 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004421 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4422 }
4423 break;
4424 }
4425
4426 case Primitive::kPrimLong: {
4427 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004428 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004429 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004430 size_t offset =
4431 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004432 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004433 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004434 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004435 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436 }
4437 break;
4438 }
4439
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004440 case Primitive::kPrimFloat: {
4441 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4442 Location out = locations->Out();
4443 DCHECK(out.IsFpuRegister());
4444 if (index.IsConstant()) {
4445 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4446 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
4447 } else {
4448 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4449 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
4450 }
4451 break;
4452 }
4453
4454 case Primitive::kPrimDouble: {
4455 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4456 Location out = locations->Out();
4457 DCHECK(out.IsFpuRegisterPair());
4458 if (index.IsConstant()) {
4459 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4460 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
4461 } else {
4462 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4463 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4464 }
4465 break;
4466 }
4467
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004468 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004469 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004470 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004471 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004472 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004473
4474 if (type == Primitive::kPrimNot) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004475 static_assert(
4476 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4477 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4478 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4479 Location out = locations->Out();
4480 if (index.IsConstant()) {
4481 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4482 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4483 } else {
4484 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4485 }
Roland Levillain4d027112015-07-01 15:41:14 +01004486 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487}
4488
4489void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004490 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004491
4492 bool needs_write_barrier =
4493 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004494 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4495 bool object_array_set_with_read_barrier =
4496 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004497
Nicolas Geoffray39468442014-09-02 15:17:15 +01004498 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004499 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004500 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4501 LocationSummary::kCallOnSlowPath :
4502 LocationSummary::kNoCall);
4503
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004504 locations->SetInAt(0, Location::RequiresRegister());
4505 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4506 if (Primitive::IsFloatingPointType(value_type)) {
4507 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004508 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004509 locations->SetInAt(2, Location::RequiresRegister());
4510 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004511 if (needs_write_barrier) {
4512 // Temporary registers for the write barrier.
4513 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004514 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004515 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004516}
4517
4518void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4519 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004520 Location array_loc = locations->InAt(0);
4521 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004522 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004523 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004524 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004525 bool needs_write_barrier =
4526 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004527
4528 switch (value_type) {
4529 case Primitive::kPrimBoolean:
4530 case Primitive::kPrimByte: {
4531 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004532 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004533 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004534 size_t offset =
4535 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004536 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004537 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004538 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004539 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4540 }
4541 break;
4542 }
4543
4544 case Primitive::kPrimShort:
4545 case Primitive::kPrimChar: {
4546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004547 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004548 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004549 size_t offset =
4550 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004551 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004553 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004554 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4555 }
4556 break;
4557 }
4558
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004559 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004561 Location value_loc = locations->InAt(2);
4562 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004563 Register source = value;
4564
4565 if (instruction->InputAt(2)->IsNullConstant()) {
4566 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004567 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004568 size_t offset =
4569 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004570 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004571 } else {
4572 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004573 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004574 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004575 }
Roland Levillain3b359c72015-11-17 19:35:12 +00004576 DCHECK(!needs_write_barrier);
4577 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004578 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004579 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004580
4581 DCHECK(needs_write_barrier);
4582 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4583 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4584 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4585 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4586 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4587 Label done;
4588 SlowPathCode* slow_path = nullptr;
4589
Roland Levillain3b359c72015-11-17 19:35:12 +00004590 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004591 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4592 codegen_->AddSlowPath(slow_path);
4593 if (instruction->GetValueCanBeNull()) {
4594 Label non_zero;
4595 __ CompareAndBranchIfNonZero(value, &non_zero);
4596 if (index.IsConstant()) {
4597 size_t offset =
4598 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4599 __ StoreToOffset(kStoreWord, value, array, offset);
4600 } else {
4601 DCHECK(index.IsRegister()) << index;
4602 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4603 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4604 }
4605 codegen_->MaybeRecordImplicitNullCheck(instruction);
4606 __ b(&done);
4607 __ Bind(&non_zero);
4608 }
4609
Roland Levillain3b359c72015-11-17 19:35:12 +00004610 if (kEmitCompilerReadBarrier) {
4611 // When read barriers are enabled, the type checking
4612 // instrumentation requires two read barriers:
4613 //
4614 // __ Mov(temp2, temp1);
4615 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4616 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4617 // codegen_->GenerateReadBarrier(
4618 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4619 //
4620 // // /* HeapReference<Class> */ temp2 = value->klass_
4621 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4622 // codegen_->GenerateReadBarrier(
4623 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4624 //
4625 // __ cmp(temp1, ShifterOperand(temp2));
4626 //
4627 // However, the second read barrier may trash `temp`, as it
4628 // is a temporary register, and as such would not be saved
4629 // along with live registers before calling the runtime (nor
4630 // restored afterwards). So in this case, we bail out and
4631 // delegate the work to the array set slow path.
4632 //
4633 // TODO: Extend the register allocator to support a new
4634 // "(locally) live temp" location so as to avoid always
4635 // going into the slow path when read barriers are enabled.
4636 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004637 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004638 // /* HeapReference<Class> */ temp1 = array->klass_
4639 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4640 codegen_->MaybeRecordImplicitNullCheck(instruction);
4641 __ MaybeUnpoisonHeapReference(temp1);
4642
4643 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4644 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4645 // /* HeapReference<Class> */ temp2 = value->klass_
4646 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4647 // If heap poisoning is enabled, no need to unpoison `temp1`
4648 // nor `temp2`, as we are comparing two poisoned references.
4649 __ cmp(temp1, ShifterOperand(temp2));
4650
4651 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4652 Label do_put;
4653 __ b(&do_put, EQ);
4654 // If heap poisoning is enabled, the `temp1` reference has
4655 // not been unpoisoned yet; unpoison it now.
4656 __ MaybeUnpoisonHeapReference(temp1);
4657
4658 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4659 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4660 // If heap poisoning is enabled, no need to unpoison
4661 // `temp1`, as we are comparing against null below.
4662 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4663 __ Bind(&do_put);
4664 } else {
4665 __ b(slow_path->GetEntryLabel(), NE);
4666 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004667 }
4668 }
4669
4670 if (kPoisonHeapReferences) {
4671 // Note that in the case where `value` is a null reference,
4672 // we do not enter this block, as a null reference does not
4673 // need poisoning.
4674 DCHECK_EQ(value_type, Primitive::kPrimNot);
4675 __ Mov(temp1, value);
4676 __ PoisonHeapReference(temp1);
4677 source = temp1;
4678 }
4679
4680 if (index.IsConstant()) {
4681 size_t offset =
4682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4683 __ StoreToOffset(kStoreWord, source, array, offset);
4684 } else {
4685 DCHECK(index.IsRegister()) << index;
4686 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4687 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4688 }
4689
Roland Levillain3b359c72015-11-17 19:35:12 +00004690 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004691 codegen_->MaybeRecordImplicitNullCheck(instruction);
4692 }
4693
4694 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4695
4696 if (done.IsLinked()) {
4697 __ Bind(&done);
4698 }
4699
4700 if (slow_path != nullptr) {
4701 __ Bind(slow_path->GetExitLabel());
4702 }
4703
4704 break;
4705 }
4706
4707 case Primitive::kPrimInt: {
4708 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4709 Register value = locations->InAt(2).AsRegister<Register>();
4710 if (index.IsConstant()) {
4711 size_t offset =
4712 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4713 __ StoreToOffset(kStoreWord, value, array, offset);
4714 } else {
4715 DCHECK(index.IsRegister()) << index;
4716 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4717 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4718 }
4719
4720 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004721 break;
4722 }
4723
4724 case Primitive::kPrimLong: {
4725 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004726 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004727 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004728 size_t offset =
4729 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004730 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004731 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004732 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004733 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004734 }
4735 break;
4736 }
4737
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004738 case Primitive::kPrimFloat: {
4739 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4740 Location value = locations->InAt(2);
4741 DCHECK(value.IsFpuRegister());
4742 if (index.IsConstant()) {
4743 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004744 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004745 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004746 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004747 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4748 }
4749 break;
4750 }
4751
4752 case Primitive::kPrimDouble: {
4753 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4754 Location value = locations->InAt(2);
4755 DCHECK(value.IsFpuRegisterPair());
4756 if (index.IsConstant()) {
4757 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004758 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004759 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004760 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004761 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4762 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004763
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004764 break;
4765 }
4766
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004767 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004768 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004769 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004770 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004771
4772 // Ints and objects are handled in the switch.
4773 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4774 codegen_->MaybeRecordImplicitNullCheck(instruction);
4775 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004776}
4777
4778void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004779 LocationSummary* locations =
4780 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004781 locations->SetInAt(0, Location::RequiresRegister());
4782 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783}
4784
4785void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4786 LocationSummary* locations = instruction->GetLocations();
4787 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004788 Register obj = locations->InAt(0).AsRegister<Register>();
4789 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004791 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004792}
4793
4794void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004795 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4796 ? LocationSummary::kCallOnSlowPath
4797 : LocationSummary::kNoCall;
4798 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004799 locations->SetInAt(0, Location::RequiresRegister());
4800 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004801 if (instruction->HasUses()) {
4802 locations->SetOut(Location::SameAsFirstInput());
4803 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004804}
4805
4806void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4807 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004808 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004809 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810 codegen_->AddSlowPath(slow_path);
4811
Roland Levillain271ab9c2014-11-27 15:23:57 +00004812 Register index = locations->InAt(0).AsRegister<Register>();
4813 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004814
4815 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004816 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817}
4818
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004819void CodeGeneratorARM::MarkGCCard(Register temp,
4820 Register card,
4821 Register object,
4822 Register value,
4823 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004824 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004825 if (can_be_null) {
4826 __ CompareAndBranchIfZero(value, &is_null);
4827 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004828 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4829 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4830 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004831 if (can_be_null) {
4832 __ Bind(&is_null);
4833 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004834}
4835
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004836void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4837 temp->SetLocations(nullptr);
4838}
4839
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004840void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004841 // Nothing to do, this is driven by the code generator.
4842}
4843
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004844void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004845 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004846}
4847
4848void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004849 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4850}
4851
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004852void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4853 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4854}
4855
4856void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004857 HBasicBlock* block = instruction->GetBlock();
4858 if (block->GetLoopInformation() != nullptr) {
4859 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4860 // The back edge will generate the suspend check.
4861 return;
4862 }
4863 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4864 // The goto will generate the suspend check.
4865 return;
4866 }
4867 GenerateSuspendCheck(instruction, nullptr);
4868}
4869
4870void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4871 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004872 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004873 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4874 if (slow_path == nullptr) {
4875 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4876 instruction->SetSlowPath(slow_path);
4877 codegen_->AddSlowPath(slow_path);
4878 if (successor != nullptr) {
4879 DCHECK(successor->IsLoopHeader());
4880 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4881 }
4882 } else {
4883 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4884 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004885
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004886 __ LoadFromOffset(
4887 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004888 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004889 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004890 __ Bind(slow_path->GetReturnLabel());
4891 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004892 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004893 __ b(slow_path->GetEntryLabel());
4894 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004895}
4896
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004897ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4898 return codegen_->GetAssembler();
4899}
4900
4901void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004902 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004903 Location source = move->GetSource();
4904 Location destination = move->GetDestination();
4905
4906 if (source.IsRegister()) {
4907 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004908 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004909 } else {
4910 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004911 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004912 SP, destination.GetStackIndex());
4913 }
4914 } else if (source.IsStackSlot()) {
4915 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004916 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004917 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004918 } else if (destination.IsFpuRegister()) {
4919 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004920 } else {
4921 DCHECK(destination.IsStackSlot());
4922 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4923 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4924 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004925 } else if (source.IsFpuRegister()) {
4926 if (destination.IsFpuRegister()) {
4927 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004928 } else {
4929 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004930 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4931 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004932 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004933 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004934 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4935 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004936 } else if (destination.IsRegisterPair()) {
4937 DCHECK(ExpectedPairLayout(destination));
4938 __ LoadFromOffset(
4939 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4940 } else {
4941 DCHECK(destination.IsFpuRegisterPair()) << destination;
4942 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4943 SP,
4944 source.GetStackIndex());
4945 }
4946 } else if (source.IsRegisterPair()) {
4947 if (destination.IsRegisterPair()) {
4948 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4949 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4950 } else {
4951 DCHECK(destination.IsDoubleStackSlot()) << destination;
4952 DCHECK(ExpectedPairLayout(source));
4953 __ StoreToOffset(
4954 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4955 }
4956 } else if (source.IsFpuRegisterPair()) {
4957 if (destination.IsFpuRegisterPair()) {
4958 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4959 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4960 } else {
4961 DCHECK(destination.IsDoubleStackSlot()) << destination;
4962 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4963 SP,
4964 destination.GetStackIndex());
4965 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004966 } else {
4967 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004968 HConstant* constant = source.GetConstant();
4969 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4970 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004971 if (destination.IsRegister()) {
4972 __ LoadImmediate(destination.AsRegister<Register>(), value);
4973 } else {
4974 DCHECK(destination.IsStackSlot());
4975 __ LoadImmediate(IP, value);
4976 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4977 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004978 } else if (constant->IsLongConstant()) {
4979 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004980 if (destination.IsRegisterPair()) {
4981 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4982 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004983 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004984 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004985 __ LoadImmediate(IP, Low32Bits(value));
4986 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4987 __ LoadImmediate(IP, High32Bits(value));
4988 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4989 }
4990 } else if (constant->IsDoubleConstant()) {
4991 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004992 if (destination.IsFpuRegisterPair()) {
4993 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004994 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004995 DCHECK(destination.IsDoubleStackSlot()) << destination;
4996 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004997 __ LoadImmediate(IP, Low32Bits(int_value));
4998 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4999 __ LoadImmediate(IP, High32Bits(int_value));
5000 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5001 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005002 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005003 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005004 float value = constant->AsFloatConstant()->GetValue();
5005 if (destination.IsFpuRegister()) {
5006 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5007 } else {
5008 DCHECK(destination.IsStackSlot());
5009 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5010 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5011 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005012 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005013 }
5014}
5015
5016void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5017 __ Mov(IP, reg);
5018 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5019 __ StoreToOffset(kStoreWord, IP, SP, mem);
5020}
5021
5022void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5023 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5024 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5025 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5026 SP, mem1 + stack_offset);
5027 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5028 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5029 SP, mem2 + stack_offset);
5030 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5031}
5032
5033void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005034 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005035 Location source = move->GetSource();
5036 Location destination = move->GetDestination();
5037
5038 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005039 DCHECK_NE(source.AsRegister<Register>(), IP);
5040 DCHECK_NE(destination.AsRegister<Register>(), IP);
5041 __ Mov(IP, source.AsRegister<Register>());
5042 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5043 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005044 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005045 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005046 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005047 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005048 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5049 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005050 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005051 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005052 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005053 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005054 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005055 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005056 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005057 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005058 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5059 destination.AsRegisterPairHigh<Register>(),
5060 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005061 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005062 Register low_reg = source.IsRegisterPair()
5063 ? source.AsRegisterPairLow<Register>()
5064 : destination.AsRegisterPairLow<Register>();
5065 int mem = source.IsRegisterPair()
5066 ? destination.GetStackIndex()
5067 : source.GetStackIndex();
5068 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005069 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005070 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005071 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005072 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005073 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5074 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005075 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005076 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005077 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005078 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5079 DRegister reg = source.IsFpuRegisterPair()
5080 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5081 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5082 int mem = source.IsFpuRegisterPair()
5083 ? destination.GetStackIndex()
5084 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005085 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005086 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005087 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005088 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5089 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5090 : destination.AsFpuRegister<SRegister>();
5091 int mem = source.IsFpuRegister()
5092 ? destination.GetStackIndex()
5093 : source.GetStackIndex();
5094
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005095 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005096 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005097 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005098 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005099 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5100 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005101 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005102 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005103 }
5104}
5105
5106void ParallelMoveResolverARM::SpillScratch(int reg) {
5107 __ Push(static_cast<Register>(reg));
5108}
5109
5110void ParallelMoveResolverARM::RestoreScratch(int reg) {
5111 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005112}
5113
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005114void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005115 InvokeRuntimeCallingConvention calling_convention;
5116 CodeGenerator::CreateLoadClassLocationSummary(
5117 cls,
5118 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005119 Location::RegisterLocation(R0),
5120 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005121}
5122
5123void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005124 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005125 if (cls->NeedsAccessCheck()) {
5126 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5127 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5128 cls,
5129 cls->GetDexPc(),
5130 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005131 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005132 return;
5133 }
5134
Roland Levillain3b359c72015-11-17 19:35:12 +00005135 Location out_loc = locations->Out();
5136 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005137 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005138
Calin Juravle580b6092015-10-06 17:35:58 +01005139 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005140 DCHECK(!cls->CanCallRuntime());
5141 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain3b359c72015-11-17 19:35:12 +00005142 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5143 if (kEmitCompilerReadBarrier) {
5144 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5145 __ AddConstant(out, current_method, declaring_class_offset);
5146 // /* mirror::Class* */ out = out->Read()
5147 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5148 } else {
5149 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5150 __ LoadFromOffset(kLoadWord, out, current_method, declaring_class_offset);
5151 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005152 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005153 // /* GcRoot<mirror::Class>[] */ out =
5154 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005155 __ LoadFromOffset(kLoadWord,
5156 out,
5157 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005158 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005159
5160 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5161 if (kEmitCompilerReadBarrier) {
5162 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5163 __ AddConstant(out, out, cache_offset);
5164 // /* mirror::Class* */ out = out->Read()
5165 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5166 } else {
5167 // /* GcRoot<mirror::Class> */ out = out[type_index]
5168 __ LoadFromOffset(kLoadWord, out, out, cache_offset);
5169 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005170
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005171 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5172 DCHECK(cls->CanCallRuntime());
5173 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5174 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5175 codegen_->AddSlowPath(slow_path);
5176 if (!cls->IsInDexCache()) {
5177 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5178 }
5179 if (cls->MustGenerateClinitCheck()) {
5180 GenerateClassInitializationCheck(slow_path, out);
5181 } else {
5182 __ Bind(slow_path->GetExitLabel());
5183 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005184 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005185 }
5186}
5187
5188void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5189 LocationSummary* locations =
5190 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5191 locations->SetInAt(0, Location::RequiresRegister());
5192 if (check->HasUses()) {
5193 locations->SetOut(Location::SameAsFirstInput());
5194 }
5195}
5196
5197void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005198 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005199 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005200 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005201 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005202 GenerateClassInitializationCheck(slow_path,
5203 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005204}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005205
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005206void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005207 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005208 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5209 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5210 __ b(slow_path->GetEntryLabel(), LT);
5211 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5212 // properly. Therefore, we do a memory fence.
5213 __ dmb(ISH);
5214 __ Bind(slow_path->GetExitLabel());
5215}
5216
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005217void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005218 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5219 ? LocationSummary::kCallOnSlowPath
5220 : LocationSummary::kNoCall;
5221 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005222 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005223 locations->SetOut(Location::RequiresRegister());
5224}
5225
5226void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005227 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005228 Location out_loc = locations->Out();
5229 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005230 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005231
5232 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5233 if (kEmitCompilerReadBarrier) {
5234 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5235 __ AddConstant(out, current_method, declaring_class_offset);
5236 // /* mirror::Class* */ out = out->Read()
5237 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5238 } else {
5239 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5240 __ LoadFromOffset(kLoadWord, out, current_method, declaring_class_offset);
5241 }
5242
5243 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005244 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005245
5246 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5247 if (kEmitCompilerReadBarrier) {
5248 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5249 __ AddConstant(out, out, cache_offset);
5250 // /* mirror::String* */ out = out->Read()
5251 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5252 } else {
5253 // /* GcRoot<mirror::String> */ out = out[string_index]
5254 __ LoadFromOffset(kLoadWord, out, out, cache_offset);
5255 }
5256
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005257 if (!load->IsInDexCache()) {
5258 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5259 codegen_->AddSlowPath(slow_path);
5260 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5261 __ Bind(slow_path->GetExitLabel());
5262 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005263}
5264
David Brazdilcb1c0552015-08-04 16:22:25 +01005265static int32_t GetExceptionTlsOffset() {
5266 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5267}
5268
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005269void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5270 LocationSummary* locations =
5271 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5272 locations->SetOut(Location::RequiresRegister());
5273}
5274
5275void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005276 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005277 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5278}
5279
5280void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5281 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5282}
5283
5284void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005285 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005286 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005287}
5288
5289void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5290 LocationSummary* locations =
5291 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5292 InvokeRuntimeCallingConvention calling_convention;
5293 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5294}
5295
5296void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5297 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005298 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005299 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005300}
5301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005302void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005303 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005304 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5305 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005306 case TypeCheckKind::kExactCheck:
5307 case TypeCheckKind::kAbstractClassCheck:
5308 case TypeCheckKind::kClassHierarchyCheck:
5309 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005310 call_kind =
5311 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005312 break;
5313 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005314 case TypeCheckKind::kUnresolvedCheck:
5315 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005316 call_kind = LocationSummary::kCallOnSlowPath;
5317 break;
5318 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005319
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005320 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005321 locations->SetInAt(0, Location::RequiresRegister());
5322 locations->SetInAt(1, Location::RequiresRegister());
5323 // The "out" register is used as a temporary, so it overlaps with the inputs.
5324 // Note that TypeCheckSlowPathARM uses this register too.
5325 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5326 // When read barriers are enabled, we need a temporary register for
5327 // some cases.
5328 if (kEmitCompilerReadBarrier &&
5329 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5330 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5331 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5332 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005333 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005334}
5335
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005336void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005337 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005338 Location obj_loc = locations->InAt(0);
5339 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005340 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005341 Location out_loc = locations->Out();
5342 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005343 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005344 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5345 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5346 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005347 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005348 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005349
5350 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005351 // avoid null check if we know obj is not null.
5352 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005353 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005354 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005355
Roland Levillain3b359c72015-11-17 19:35:12 +00005356 // /* HeapReference<Class> */ out = obj->klass_
5357 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
5358 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005359
5360 switch (instruction->GetTypeCheckKind()) {
5361 case TypeCheckKind::kExactCheck: {
5362 __ cmp(out, ShifterOperand(cls));
5363 // Classes must be equal for the instanceof to succeed.
5364 __ b(&zero, NE);
5365 __ LoadImmediate(out, 1);
5366 __ b(&done);
5367 break;
5368 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005370 case TypeCheckKind::kAbstractClassCheck: {
5371 // If the class is abstract, we eagerly fetch the super class of the
5372 // object to avoid doing a comparison we know will fail.
5373 Label loop;
5374 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005375 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5376 if (kEmitCompilerReadBarrier) {
5377 // Save the value of `out` into `temp` before overwriting it
5378 // in the following move operation, as we will need it for the
5379 // read barrier below.
5380 Register temp = temp_loc.AsRegister<Register>();
5381 __ Mov(temp, out);
5382 }
5383 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005384 __ LoadFromOffset(kLoadWord, out, out, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005385 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005386 // If `out` is null, we use it for the result, and jump to `done`.
5387 __ CompareAndBranchIfZero(out, &done);
5388 __ cmp(out, ShifterOperand(cls));
5389 __ b(&loop, NE);
5390 __ LoadImmediate(out, 1);
5391 if (zero.IsLinked()) {
5392 __ b(&done);
5393 }
5394 break;
5395 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005396
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005397 case TypeCheckKind::kClassHierarchyCheck: {
5398 // Walk over the class hierarchy to find a match.
5399 Label loop, success;
5400 __ Bind(&loop);
5401 __ cmp(out, ShifterOperand(cls));
5402 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005403 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5404 if (kEmitCompilerReadBarrier) {
5405 // Save the value of `out` into `temp` before overwriting it
5406 // in the following move operation, as we will need it for the
5407 // read barrier below.
5408 Register temp = temp_loc.AsRegister<Register>();
5409 __ Mov(temp, out);
5410 }
5411 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005412 __ LoadFromOffset(kLoadWord, out, out, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005413 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005414 __ CompareAndBranchIfNonZero(out, &loop);
5415 // If `out` is null, we use it for the result, and jump to `done`.
5416 __ b(&done);
5417 __ Bind(&success);
5418 __ LoadImmediate(out, 1);
5419 if (zero.IsLinked()) {
5420 __ b(&done);
5421 }
5422 break;
5423 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005424
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005425 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005426 // Do an exact check.
5427 Label exact_check;
5428 __ cmp(out, ShifterOperand(cls));
5429 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005430 // Otherwise, we need to check that the object's class is a non-primitive array.
5431 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5432 if (kEmitCompilerReadBarrier) {
5433 // Save the value of `out` into `temp` before overwriting it
5434 // in the following move operation, as we will need it for the
5435 // read barrier below.
5436 Register temp = temp_loc.AsRegister<Register>();
5437 __ Mov(temp, out);
5438 }
5439 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005440 __ LoadFromOffset(kLoadWord, out, out, component_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005441 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005442 // If `out` is null, we use it for the result, and jump to `done`.
5443 __ CompareAndBranchIfZero(out, &done);
5444 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5445 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5446 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005447 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005448 __ LoadImmediate(out, 1);
5449 __ b(&done);
5450 break;
5451 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005452
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005453 case TypeCheckKind::kArrayCheck: {
5454 __ cmp(out, ShifterOperand(cls));
5455 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005456 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5457 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005458 codegen_->AddSlowPath(slow_path);
5459 __ b(slow_path->GetEntryLabel(), NE);
5460 __ LoadImmediate(out, 1);
5461 if (zero.IsLinked()) {
5462 __ b(&done);
5463 }
5464 break;
5465 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005466
Calin Juravle98893e12015-10-02 21:05:03 +01005467 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005468 case TypeCheckKind::kInterfaceCheck: {
5469 // Note that we indeed only call on slow path, but we always go
5470 // into the slow path for the unresolved & interface check
5471 // cases.
5472 //
5473 // We cannot directly call the InstanceofNonTrivial runtime
5474 // entry point without resorting to a type checking slow path
5475 // here (i.e. by calling InvokeRuntime directly), as it would
5476 // require to assign fixed registers for the inputs of this
5477 // HInstanceOf instruction (following the runtime calling
5478 // convention), which might be cluttered by the potential first
5479 // read barrier emission at the beginning of this method.
5480 DCHECK(locations->OnlyCallsOnSlowPath());
5481 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5482 /* is_fatal */ false);
5483 codegen_->AddSlowPath(slow_path);
5484 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005485 if (zero.IsLinked()) {
5486 __ b(&done);
5487 }
5488 break;
5489 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005490 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005491
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005492 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005493 __ Bind(&zero);
5494 __ LoadImmediate(out, 0);
5495 }
5496
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005497 if (done.IsLinked()) {
5498 __ Bind(&done);
5499 }
5500
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005501 if (slow_path != nullptr) {
5502 __ Bind(slow_path->GetExitLabel());
5503 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005504}
5505
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005506void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005507 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5508 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5509
Roland Levillain3b359c72015-11-17 19:35:12 +00005510 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5511 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005512 case TypeCheckKind::kExactCheck:
5513 case TypeCheckKind::kAbstractClassCheck:
5514 case TypeCheckKind::kClassHierarchyCheck:
5515 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005516 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5517 LocationSummary::kCallOnSlowPath :
5518 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005519 break;
5520 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005521 case TypeCheckKind::kUnresolvedCheck:
5522 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005523 call_kind = LocationSummary::kCallOnSlowPath;
5524 break;
5525 }
5526
Roland Levillain3b359c72015-11-17 19:35:12 +00005527 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5528 locations->SetInAt(0, Location::RequiresRegister());
5529 locations->SetInAt(1, Location::RequiresRegister());
5530 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5531 locations->AddTemp(Location::RequiresRegister());
5532 // When read barriers are enabled, we need an additional temporary
5533 // register for some cases.
5534 if (kEmitCompilerReadBarrier &&
5535 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5536 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5537 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005538 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005539 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005540}
5541
5542void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
5543 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005544 Location obj_loc = locations->InAt(0);
5545 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005546 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005547 Location temp_loc = locations->GetTemp(0);
5548 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005549 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005550 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5551 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5552 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005553
Roland Levillain3b359c72015-11-17 19:35:12 +00005554 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5555 bool is_type_check_slow_path_fatal =
5556 (type_check_kind == TypeCheckKind::kExactCheck ||
5557 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5558 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5559 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5560 !instruction->CanThrowIntoCatchBlock();
5561 SlowPathCode* type_check_slow_path =
5562 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5563 is_type_check_slow_path_fatal);
5564 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005565
5566 Label done;
5567 // Avoid null check if we know obj is not null.
5568 if (instruction->MustDoNullCheck()) {
5569 __ CompareAndBranchIfZero(obj, &done);
5570 }
5571
Roland Levillain3b359c72015-11-17 19:35:12 +00005572 // /* HeapReference<Class> */ temp = obj->klass_
5573 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5574 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005575
Roland Levillain3b359c72015-11-17 19:35:12 +00005576 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005577 case TypeCheckKind::kExactCheck:
5578 case TypeCheckKind::kArrayCheck: {
5579 __ cmp(temp, ShifterOperand(cls));
5580 // Jump to slow path for throwing the exception or doing a
5581 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005582 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005583 break;
5584 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005585
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005586 case TypeCheckKind::kAbstractClassCheck: {
5587 // If the class is abstract, we eagerly fetch the super class of the
5588 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005589 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005590 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005591 Location temp2_loc =
5592 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5593 if (kEmitCompilerReadBarrier) {
5594 // Save the value of `temp` into `temp2` before overwriting it
5595 // in the following move operation, as we will need it for the
5596 // read barrier below.
5597 Register temp2 = temp2_loc.AsRegister<Register>();
5598 __ Mov(temp2, temp);
5599 }
5600 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005602 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5603
5604 // If the class reference currently in `temp` is not null, jump
5605 // to the `compare_classes` label to compare it with the checked
5606 // class.
5607 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5608 // Otherwise, jump to the slow path to throw the exception.
5609 //
5610 // But before, move back the object's class into `temp` before
5611 // going into the slow path, as it has been overwritten in the
5612 // meantime.
5613 // /* HeapReference<Class> */ temp = obj->klass_
5614 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5615 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5616 __ b(type_check_slow_path->GetEntryLabel());
5617
5618 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619 __ cmp(temp, ShifterOperand(cls));
5620 __ b(&loop, NE);
5621 break;
5622 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005623
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005624 case TypeCheckKind::kClassHierarchyCheck: {
5625 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005626 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005627 __ Bind(&loop);
5628 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005629 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005630
5631 Location temp2_loc =
5632 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5633 if (kEmitCompilerReadBarrier) {
5634 // Save the value of `temp` into `temp2` before overwriting it
5635 // in the following move operation, as we will need it for the
5636 // read barrier below.
5637 Register temp2 = temp2_loc.AsRegister<Register>();
5638 __ Mov(temp2, temp);
5639 }
5640 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005642 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5643
5644 // If the class reference currently in `temp` is not null, jump
5645 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005646 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005647 // Otherwise, jump to the slow path to throw the exception.
5648 //
5649 // But before, move back the object's class into `temp` before
5650 // going into the slow path, as it has been overwritten in the
5651 // meantime.
5652 // /* HeapReference<Class> */ temp = obj->klass_
5653 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5654 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5655 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005656 break;
5657 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005658
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005659 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005660 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005661 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005662 __ cmp(temp, ShifterOperand(cls));
5663 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005664
5665 // Otherwise, we need to check that the object's class is a non-primitive array.
5666 Location temp2_loc =
5667 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5668 if (kEmitCompilerReadBarrier) {
5669 // Save the value of `temp` into `temp2` before overwriting it
5670 // in the following move operation, as we will need it for the
5671 // read barrier below.
5672 Register temp2 = temp2_loc.AsRegister<Register>();
5673 __ Mov(temp2, temp);
5674 }
5675 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005677 codegen_->MaybeGenerateReadBarrier(
5678 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5679
5680 // If the component type is not null (i.e. the object is indeed
5681 // an array), jump to label `check_non_primitive_component_type`
5682 // to further check that this component type is not a primitive
5683 // type.
5684 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5685 // Otherwise, jump to the slow path to throw the exception.
5686 //
5687 // But before, move back the object's class into `temp` before
5688 // going into the slow path, as it has been overwritten in the
5689 // meantime.
5690 // /* HeapReference<Class> */ temp = obj->klass_
5691 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5692 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5693 __ b(type_check_slow_path->GetEntryLabel());
5694
5695 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005696 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005697 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5698 __ CompareAndBranchIfZero(temp, &done);
5699 // Same comment as above regarding `temp` and the slow path.
5700 // /* HeapReference<Class> */ temp = obj->klass_
5701 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
5702 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5703 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005704 break;
5705 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005706
Calin Juravle98893e12015-10-02 21:05:03 +01005707 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005708 case TypeCheckKind::kInterfaceCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005709 // We always go into the type check slow path for the unresolved &
5710 // interface check cases.
5711 //
5712 // We cannot directly call the CheckCast runtime entry point
5713 // without resorting to a type checking slow path here (i.e. by
5714 // calling InvokeRuntime directly), as it would require to
5715 // assign fixed registers for the inputs of this HInstanceOf
5716 // instruction (following the runtime calling convention), which
5717 // might be cluttered by the potential first read barrier
5718 // emission at the beginning of this method.
5719 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 break;
5721 }
5722 __ Bind(&done);
5723
Roland Levillain3b359c72015-11-17 19:35:12 +00005724 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005725}
5726
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005727void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5728 LocationSummary* locations =
5729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5730 InvokeRuntimeCallingConvention calling_convention;
5731 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5732}
5733
5734void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5735 codegen_->InvokeRuntime(instruction->IsEnter()
5736 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5737 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005738 instruction->GetDexPc(),
5739 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005740 if (instruction->IsEnter()) {
5741 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5742 } else {
5743 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5744 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005745}
5746
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005747void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5748void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5749void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005750
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005751void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005752 LocationSummary* locations =
5753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5754 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5755 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005756 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005757 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005758 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005759 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005760}
5761
5762void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5763 HandleBitwiseOperation(instruction);
5764}
5765
5766void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5767 HandleBitwiseOperation(instruction);
5768}
5769
5770void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5771 HandleBitwiseOperation(instruction);
5772}
5773
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005774void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5775 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5776 if (value == 0xffffffffu) {
5777 if (out != first) {
5778 __ mov(out, ShifterOperand(first));
5779 }
5780 return;
5781 }
5782 if (value == 0u) {
5783 __ mov(out, ShifterOperand(0));
5784 return;
5785 }
5786 ShifterOperand so;
5787 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5788 __ and_(out, first, so);
5789 } else {
5790 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5791 __ bic(out, first, ShifterOperand(~value));
5792 }
5793}
5794
5795void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5796 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5797 if (value == 0u) {
5798 if (out != first) {
5799 __ mov(out, ShifterOperand(first));
5800 }
5801 return;
5802 }
5803 if (value == 0xffffffffu) {
5804 __ mvn(out, ShifterOperand(0));
5805 return;
5806 }
5807 ShifterOperand so;
5808 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5809 __ orr(out, first, so);
5810 } else {
5811 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5812 __ orn(out, first, ShifterOperand(~value));
5813 }
5814}
5815
5816void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5817 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5818 if (value == 0u) {
5819 if (out != first) {
5820 __ mov(out, ShifterOperand(first));
5821 }
5822 return;
5823 }
5824 __ eor(out, first, ShifterOperand(value));
5825}
5826
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005827void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5828 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005829 Location first = locations->InAt(0);
5830 Location second = locations->InAt(1);
5831 Location out = locations->Out();
5832
5833 if (second.IsConstant()) {
5834 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5835 uint32_t value_low = Low32Bits(value);
5836 if (instruction->GetResultType() == Primitive::kPrimInt) {
5837 Register first_reg = first.AsRegister<Register>();
5838 Register out_reg = out.AsRegister<Register>();
5839 if (instruction->IsAnd()) {
5840 GenerateAndConst(out_reg, first_reg, value_low);
5841 } else if (instruction->IsOr()) {
5842 GenerateOrrConst(out_reg, first_reg, value_low);
5843 } else {
5844 DCHECK(instruction->IsXor());
5845 GenerateEorConst(out_reg, first_reg, value_low);
5846 }
5847 } else {
5848 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5849 uint32_t value_high = High32Bits(value);
5850 Register first_low = first.AsRegisterPairLow<Register>();
5851 Register first_high = first.AsRegisterPairHigh<Register>();
5852 Register out_low = out.AsRegisterPairLow<Register>();
5853 Register out_high = out.AsRegisterPairHigh<Register>();
5854 if (instruction->IsAnd()) {
5855 GenerateAndConst(out_low, first_low, value_low);
5856 GenerateAndConst(out_high, first_high, value_high);
5857 } else if (instruction->IsOr()) {
5858 GenerateOrrConst(out_low, first_low, value_low);
5859 GenerateOrrConst(out_high, first_high, value_high);
5860 } else {
5861 DCHECK(instruction->IsXor());
5862 GenerateEorConst(out_low, first_low, value_low);
5863 GenerateEorConst(out_high, first_high, value_high);
5864 }
5865 }
5866 return;
5867 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005868
5869 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005870 Register first_reg = first.AsRegister<Register>();
5871 ShifterOperand second_reg(second.AsRegister<Register>());
5872 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005873 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005874 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005875 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005876 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005877 } else {
5878 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005879 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005880 }
5881 } else {
5882 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005883 Register first_low = first.AsRegisterPairLow<Register>();
5884 Register first_high = first.AsRegisterPairHigh<Register>();
5885 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5886 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5887 Register out_low = out.AsRegisterPairLow<Register>();
5888 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005889 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005890 __ and_(out_low, first_low, second_low);
5891 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005892 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005893 __ orr(out_low, first_low, second_low);
5894 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005895 } else {
5896 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005897 __ eor(out_low, first_low, second_low);
5898 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005899 }
5900 }
5901}
5902
Roland Levillain3b359c72015-11-17 19:35:12 +00005903void CodeGeneratorARM::GenerateReadBarrier(HInstruction* instruction,
5904 Location out,
5905 Location ref,
5906 Location obj,
5907 uint32_t offset,
5908 Location index) {
5909 DCHECK(kEmitCompilerReadBarrier);
5910
5911 // If heap poisoning is enabled, the unpoisoning of the loaded
5912 // reference will be carried out by the runtime within the slow
5913 // path.
5914 //
5915 // Note that `ref` currently does not get unpoisoned (when heap
5916 // poisoning is enabled), which is alright as the `ref` argument is
5917 // not used by the artReadBarrierSlow entry point.
5918 //
5919 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5920 SlowPathCode* slow_path = new (GetGraph()->GetArena())
5921 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
5922 AddSlowPath(slow_path);
5923
5924 // TODO: When read barrier has a fast path, add it here.
5925 /* Currently the read barrier call is inserted after the original load.
5926 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
5927 * original load. This load-load ordering is required by the read barrier.
5928 * The fast path/slow path (for Baker's algorithm) should look like:
5929 *
5930 * bool isGray = obj.LockWord & kReadBarrierMask;
5931 * lfence; // load fence or artificial data dependence to prevent load-load reordering
5932 * ref = obj.field; // this is the original load
5933 * if (isGray) {
5934 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
5935 * }
5936 */
5937
5938 __ b(slow_path->GetEntryLabel());
5939 __ Bind(slow_path->GetExitLabel());
5940}
5941
5942void CodeGeneratorARM::MaybeGenerateReadBarrier(HInstruction* instruction,
5943 Location out,
5944 Location ref,
5945 Location obj,
5946 uint32_t offset,
5947 Location index) {
5948 if (kEmitCompilerReadBarrier) {
5949 // If heap poisoning is enabled, unpoisoning will be taken care of
5950 // by the runtime within the slow path.
5951 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
5952 } else if (kPoisonHeapReferences) {
5953 __ UnpoisonHeapReference(out.AsRegister<Register>());
5954 }
5955}
5956
5957void CodeGeneratorARM::GenerateReadBarrierForRoot(HInstruction* instruction,
5958 Location out,
5959 Location root) {
5960 DCHECK(kEmitCompilerReadBarrier);
5961
5962 // Note that GC roots are not affected by heap poisoning, so we do
5963 // not need to do anything special for this here.
5964 SlowPathCode* slow_path =
5965 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
5966 AddSlowPath(slow_path);
5967
5968 // TODO: Implement a fast path for ReadBarrierForRoot, performing
5969 // the following operation (for Baker's algorithm):
5970 //
5971 // if (thread.tls32_.is_gc_marking) {
5972 // root = Mark(root);
5973 // }
5974
5975 __ b(slow_path->GetEntryLabel());
5976 __ Bind(slow_path->GetExitLabel());
5977}
5978
Vladimir Markodc151b22015-10-15 18:02:30 +01005979HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
5980 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
5981 MethodReference target_method) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005982 if (desired_dispatch_info.code_ptr_location ==
5983 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
5984 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
5985 if (&outer_dex_file != target_method.dex_file) {
5986 // Calls across dex files are more likely to exceed the available BL range,
5987 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
5988 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
5989 (desired_dispatch_info.method_load_kind ==
5990 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
5991 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
5992 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5993 return HInvokeStaticOrDirect::DispatchInfo {
5994 desired_dispatch_info.method_load_kind,
5995 code_ptr_location,
5996 desired_dispatch_info.method_load_data,
5997 0u
5998 };
5999 }
6000 }
6001 return desired_dispatch_info;
6002}
6003
Vladimir Markob4536b72015-11-24 13:45:23 +00006004Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6005 Register temp) {
6006 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6007 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6008 if (!invoke->GetLocations()->Intrinsified()) {
6009 return location.AsRegister<Register>();
6010 }
6011 // For intrinsics we allow any location, so it may be on the stack.
6012 if (!location.IsRegister()) {
6013 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6014 return temp;
6015 }
6016 // For register locations, check if the register was saved. If so, get it from the stack.
6017 // Note: There is a chance that the register was saved but not overwritten, so we could
6018 // save one load. However, since this is just an intrinsic slow path we prefer this
6019 // simple and more robust approach rather that trying to determine if that's the case.
6020 SlowPathCode* slow_path = GetCurrentSlowPath();
6021 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6022 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6023 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6024 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6025 return temp;
6026 }
6027 return location.AsRegister<Register>();
6028}
6029
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006030void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006031 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006032 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006033 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6034 // LR = code address from literal pool with link-time patch.
6035 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006036 break;
6037 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6038 // LR = invoke->GetDirectCodePtr();
6039 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006040 break;
6041 default:
6042 break;
6043 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006044
Vladimir Marko58155012015-08-19 12:49:41 +00006045 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6046 switch (invoke->GetMethodLoadKind()) {
6047 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6048 // temp = thread->string_init_entrypoint
6049 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6050 break;
6051 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006052 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006053 break;
6054 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6055 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6056 break;
6057 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6058 __ LoadLiteral(temp.AsRegister<Register>(),
6059 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6060 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006061 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6062 HArmDexCacheArraysBase* base =
6063 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6064 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6065 temp.AsRegister<Register>());
6066 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6067 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6068 break;
6069 }
Vladimir Marko58155012015-08-19 12:49:41 +00006070 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006071 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006072 Register method_reg;
6073 Register reg = temp.AsRegister<Register>();
6074 if (current_method.IsRegister()) {
6075 method_reg = current_method.AsRegister<Register>();
6076 } else {
6077 DCHECK(invoke->GetLocations()->Intrinsified());
6078 DCHECK(!current_method.IsValid());
6079 method_reg = reg;
6080 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6081 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006082 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6083 __ LoadFromOffset(kLoadWord,
6084 reg,
6085 method_reg,
6086 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006087 // temp = temp[index_in_cache]
6088 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6089 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6090 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006091 }
Vladimir Marko58155012015-08-19 12:49:41 +00006092 }
6093
6094 switch (invoke->GetCodePtrLocation()) {
6095 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6096 __ bl(GetFrameEntryLabel());
6097 break;
6098 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006099 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006100 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006101 // Arbitrarily branch to the BL itself, override at link time.
6102 __ bl(&relative_call_patches_.back().label);
6103 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006104 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6105 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6106 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006107 // LR()
6108 __ blx(LR);
6109 break;
6110 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6111 // LR = callee_method->entry_point_from_quick_compiled_code_
6112 __ LoadFromOffset(
6113 kLoadWord, LR, callee_method.AsRegister<Register>(),
6114 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6115 // LR()
6116 __ blx(LR);
6117 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006118 }
6119
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006120 DCHECK(!IsLeafMethod());
6121}
6122
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006123void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6124 Register temp = temp_location.AsRegister<Register>();
6125 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6126 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006127
6128 // Use the calling convention instead of the location of the receiver, as
6129 // intrinsics may have put the receiver in a different register. In the intrinsics
6130 // slow path, the arguments have been moved to the right place, so here we are
6131 // guaranteed that the receiver is the first register of the calling convention.
6132 InvokeDexCallingConvention calling_convention;
6133 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006134 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006135 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006136 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006137 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006138 // Instead of simply (possibly) unpoisoning `temp` here, we should
6139 // emit a read barrier for the previous class reference load.
6140 // However this is not required in practice, as this is an
6141 // intermediate/temporary reference and because the current
6142 // concurrent copying collector keeps the from-space memory
6143 // intact/accessible until the end of the marking phase (the
6144 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006145 __ MaybeUnpoisonHeapReference(temp);
6146 // temp = temp->GetMethodAt(method_offset);
6147 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6148 kArmWordSize).Int32Value();
6149 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6150 // LR = temp->GetEntryPoint();
6151 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6152 // LR();
6153 __ blx(LR);
6154}
6155
Vladimir Marko58155012015-08-19 12:49:41 +00006156void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6157 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006158 size_t size =
6159 method_patches_.size() +
6160 call_patches_.size() +
6161 relative_call_patches_.size() +
6162 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006163 linker_patches->reserve(size);
6164 for (const auto& entry : method_patches_) {
6165 const MethodReference& target_method = entry.first;
6166 Literal* literal = entry.second;
6167 DCHECK(literal->GetLabel()->IsBound());
6168 uint32_t literal_offset = literal->GetLabel()->Position();
6169 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6170 target_method.dex_file,
6171 target_method.dex_method_index));
6172 }
6173 for (const auto& entry : call_patches_) {
6174 const MethodReference& target_method = entry.first;
6175 Literal* literal = entry.second;
6176 DCHECK(literal->GetLabel()->IsBound());
6177 uint32_t literal_offset = literal->GetLabel()->Position();
6178 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6179 target_method.dex_file,
6180 target_method.dex_method_index));
6181 }
6182 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6183 uint32_t literal_offset = info.label.Position();
6184 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6185 info.target_method.dex_file,
6186 info.target_method.dex_method_index));
6187 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006188 for (const auto& pair : dex_cache_arrays_base_labels_) {
6189 HArmDexCacheArraysBase* base = pair.first;
6190 const DexCacheArraysBaseLabels* labels = &pair.second;
6191 const DexFile& dex_file = base->GetDexFile();
6192 size_t base_element_offset = base->GetElementOffset();
6193 DCHECK(labels->add_pc_label.IsBound());
6194 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6195 // Add MOVW patch.
6196 DCHECK(labels->movw_label.IsBound());
6197 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6198 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6199 &dex_file,
6200 add_pc_offset,
6201 base_element_offset));
6202 // Add MOVT patch.
6203 DCHECK(labels->movt_label.IsBound());
6204 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6205 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6206 &dex_file,
6207 add_pc_offset,
6208 base_element_offset));
6209 }
Vladimir Marko58155012015-08-19 12:49:41 +00006210}
6211
6212Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6213 MethodToLiteralMap* map) {
6214 // Look up the literal for target_method.
6215 auto lb = map->lower_bound(target_method);
6216 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6217 return lb->second;
6218 }
6219 // We don't have a literal for this method yet, insert a new one.
6220 Literal* literal = __ NewLiteral<uint32_t>(0u);
6221 map->PutBefore(lb, target_method, literal);
6222 return literal;
6223}
6224
6225Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6226 return DeduplicateMethodLiteral(target_method, &method_patches_);
6227}
6228
6229Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6230 return DeduplicateMethodLiteral(target_method, &call_patches_);
6231}
6232
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006233void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006234 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006235 LOG(FATAL) << "Unreachable";
6236}
6237
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006238void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006239 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006240 LOG(FATAL) << "Unreachable";
6241}
6242
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006243void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
6244 DCHECK(codegen_->IsBaseline());
6245 LocationSummary* locations =
6246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6247 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6248}
6249
6250void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6251 DCHECK(codegen_->IsBaseline());
6252 // Will be generated at use site.
6253}
6254
Mark Mendellfe57faa2015-09-18 09:26:15 -04006255// Simple implementation of packed switch - generate cascaded compare/jumps.
6256void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6257 LocationSummary* locations =
6258 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6259 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006260 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006261 codegen_->GetAssembler()->IsThumb()) {
6262 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6263 if (switch_instr->GetStartValue() != 0) {
6264 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6265 }
6266 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006267}
6268
6269void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6270 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006271 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006272 LocationSummary* locations = switch_instr->GetLocations();
6273 Register value_reg = locations->InAt(0).AsRegister<Register>();
6274 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6275
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006276 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006277 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006278 Register temp_reg = IP;
6279 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6280 // the immediate, because IP is used as the destination register. For the other
6281 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6282 // and they can be encoded in the instruction without making use of IP register.
6283 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6284
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006285 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006286 // Jump to successors[0] if value == lower_bound.
6287 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6288 int32_t last_index = 0;
6289 for (; num_entries - last_index > 2; last_index += 2) {
6290 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6291 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6292 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6293 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6294 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6295 }
6296 if (num_entries - last_index == 2) {
6297 // The last missing case_value.
6298 GenerateCompareWithImmediate(temp_reg, 1);
6299 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006300 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006301
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006302 // And the default for any other value.
6303 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6304 __ b(codegen_->GetLabelOf(default_block));
6305 }
6306 } else {
6307 // Create a table lookup.
6308 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6309
6310 // Materialize a pointer to the switch table
6311 std::vector<Label*> labels(num_entries);
6312 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6313 for (uint32_t i = 0; i < num_entries; i++) {
6314 labels[i] = codegen_->GetLabelOf(successors[i]);
6315 }
6316 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6317
6318 // Remove the bias.
6319 Register key_reg;
6320 if (lower_bound != 0) {
6321 key_reg = locations->GetTemp(1).AsRegister<Register>();
6322 __ AddConstant(key_reg, value_reg, -lower_bound);
6323 } else {
6324 key_reg = value_reg;
6325 }
6326
6327 // Check whether the value is in the table, jump to default block if not.
6328 __ CmpConstant(key_reg, num_entries - 1);
6329 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6330
6331 // Load the displacement from the table.
6332 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6333
6334 // Dispatch is a direct add to the PC (for Thumb2).
6335 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006336 }
6337}
6338
Vladimir Markob4536b72015-11-24 13:45:23 +00006339void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6340 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6341 locations->SetOut(Location::RequiresRegister());
6342 codegen_->AddDexCacheArraysBase(base);
6343}
6344
6345void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6346 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6347 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6348 __ BindTrackedLabel(&labels->movw_label);
6349 __ movw(base_reg, 0u);
6350 __ BindTrackedLabel(&labels->movt_label);
6351 __ movt(base_reg, 0u);
6352 __ BindTrackedLabel(&labels->add_pc_label);
6353 __ add(base_reg, base_reg, ShifterOperand(PC));
6354}
6355
Andreas Gampe85b62f22015-09-09 13:15:38 -07006356void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6357 if (!trg.IsValid()) {
6358 DCHECK(type == Primitive::kPrimVoid);
6359 return;
6360 }
6361
6362 DCHECK_NE(type, Primitive::kPrimVoid);
6363
6364 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6365 if (return_loc.Equals(trg)) {
6366 return;
6367 }
6368
6369 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6370 // with the last branch.
6371 if (type == Primitive::kPrimLong) {
6372 HParallelMove parallel_move(GetGraph()->GetArena());
6373 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6374 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6375 GetMoveResolver()->EmitNativeCode(&parallel_move);
6376 } else if (type == Primitive::kPrimDouble) {
6377 HParallelMove parallel_move(GetGraph()->GetArena());
6378 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6379 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6380 GetMoveResolver()->EmitNativeCode(&parallel_move);
6381 } else {
6382 // Let the parallel move resolver take care of all of this.
6383 HParallelMove parallel_move(GetGraph()->GetArena());
6384 parallel_move.AddMove(return_loc, trg, type, nullptr);
6385 GetMoveResolver()->EmitNativeCode(&parallel_move);
6386 }
6387}
6388
Roland Levillain4d027112015-07-01 15:41:14 +01006389#undef __
6390#undef QUICK_ENTRY_POINT
6391
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006392} // namespace arm
6393} // namespace art