blob: 15fc446c077e86a9513d30df5ddc128e079f24c1 [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
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037namespace arm {
38
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000039static bool ExpectedPairLayout(Location location) {
40 // We expected this for both core and fpu register pairs.
41 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
42}
43
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000047// We unconditionally allocate R5 to ensure we can do long operations
48// with baseline.
49static constexpr Register kCoreSavedRegisterForBaseline = R5;
50static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070051 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000052static constexpr SRegister kFpuCalleeSaves[] =
53 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000055// D31 cannot be split into two S registers, and the register allocator only works on
56// S registers. Therefore there is no need to block it.
57static constexpr DRegister DTMP = D31;
58
Roland Levillain62a46b22015-06-01 18:24:13 +010059#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010060#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010064 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Alexandre Rames67555f72014-11-18 10:55:16 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010067 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000069 if (instruction_->CanThrowIntoCatchBlock()) {
70 // Live registers will be restored in the catch block if caught.
71 SaveLiveRegisters(codegen, instruction_->GetLocations());
72 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010073 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000074 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 }
76
Alexandre Rames8158f282015-08-07 10:26:17 +010077 bool IsFatal() const OVERRIDE { return true; }
78
Alexandre Rames9931f312015-06-19 14:47:01 +010079 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
80
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010082 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
84};
85
Calin Juravled0d48522014-11-04 16:40:20 +000086class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
87 public:
88 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
89
Alexandre Rames67555f72014-11-18 10:55:16 +000090 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000091 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
92 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000093 if (instruction_->CanThrowIntoCatchBlock()) {
94 // Live registers will be restored in the catch block if caught.
95 SaveLiveRegisters(codegen, instruction_->GetLocations());
96 }
Calin Juravled0d48522014-11-04 16:40:20 +000097 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000098 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Calin Juravled0d48522014-11-04 16:40:20 +000099 }
100
Alexandre Rames8158f282015-08-07 10:26:17 +0100101 bool IsFatal() const OVERRIDE { return true; }
102
Alexandre Rames9931f312015-06-19 14:47:01 +0100103 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
104
Calin Juravled0d48522014-11-04 16:40:20 +0000105 private:
106 HDivZeroCheck* const instruction_;
107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
108};
109
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100110class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000111 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000112 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100113 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000114
Alexandre Rames67555f72014-11-18 10:55:16 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100116 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000117 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000118 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100119 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000120 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000121 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100122 if (successor_ == nullptr) {
123 __ b(GetReturnLabel());
124 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100125 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100126 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000127 }
128
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 Label* GetReturnLabel() {
130 DCHECK(successor_ == nullptr);
131 return &return_label_;
132 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100134 HBasicBlock* GetSuccessor() const {
135 return successor_;
136 }
137
Alexandre Rames9931f312015-06-19 14:47:01 +0100138 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
139
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 private:
141 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 // If not null, the block to branch to after the suspend check.
143 HBasicBlock* const successor_;
144
145 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 Label return_label_;
147
148 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
149};
150
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100153 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
154 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155
Alexandre Rames67555f72014-11-18 10:55:16 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100157 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 LocationSummary* locations = instruction_->GetLocations();
159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000168 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100169 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100171 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100172 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100173 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
174 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100175 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000176 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 }
178
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 bool IsFatal() const OVERRIDE { return true; }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185
186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
187};
188
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000189class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000191 LoadClassSlowPathARM(HLoadClass* cls,
192 HInstruction* at,
193 uint32_t dex_pc,
194 bool do_clinit)
195 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
196 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
197 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198
Alexandre Rames67555f72014-11-18 10:55:16 +0000199 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000200 LocationSummary* locations = at_->GetLocations();
201
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100202 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
203 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000204 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100206 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 int32_t entry_point_offset = do_clinit_
209 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
210 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000214 Location out = locations->Out();
215 if (out.IsValid()) {
216 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
218 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000219 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220 __ b(GetExitLabel());
221 }
222
Alexandre Rames9931f312015-06-19 14:47:01 +0100223 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
224
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 // The class this slow path will load.
227 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 // The instruction where this slow path is happening.
230 // (Might be the load class or an initialization check).
231 HInstruction* const at_;
232
233 // The dex PC of `at_`.
234 const uint32_t dex_pc_;
235
236 // Whether to initialize the class.
237 const bool do_clinit_;
238
239 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240};
241
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000242class LoadStringSlowPathARM : public SlowPathCodeARM {
243 public:
244 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
245
Alexandre Rames67555f72014-11-18 10:55:16 +0000246 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 LocationSummary* locations = instruction_->GetLocations();
248 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
249
250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
251 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253
254 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000257 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
259
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000260 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000261 __ b(GetExitLabel());
262 }
263
Alexandre Rames9931f312015-06-19 14:47:01 +0100264 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
265
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000266 private:
267 HLoadString* const instruction_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
270};
271
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272class TypeCheckSlowPathARM : public SlowPathCodeARM {
273 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100274 explicit TypeCheckSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100278 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
279 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 DCHECK(instruction_->IsCheckCast()
281 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000282
283 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
284 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286
287 // We're moving two locations to locations that could overlap, so we need a parallel
288 // move resolver.
289 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000290 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100291 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000292 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100293 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100294 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100295 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
296 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000298 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100299 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
300 instruction_,
301 instruction_->GetDexPc(),
302 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000303 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
304 } else {
305 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
307 instruction_,
308 instruction_->GetDexPc(),
309 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313 __ b(GetExitLabel());
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
317
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
322};
323
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700324class DeoptimizationSlowPathARM : public SlowPathCodeARM {
325 public:
326 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
327 : instruction_(instruction) {}
328
329 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
330 __ Bind(GetEntryLabel());
331 SaveLiveRegisters(codegen, instruction_->GetLocations());
332 DCHECK(instruction_->IsDeoptimize());
333 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
334 uint32_t dex_pc = deoptimize->GetDexPc();
335 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
336 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
337 }
338
Alexandre Rames9931f312015-06-19 14:47:01 +0100339 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
340
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700341 private:
342 HInstruction* const instruction_;
343 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
344};
345
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000346#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100347#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700348
Roland Levillain4fa13f62015-07-06 18:11:54 +0100349inline Condition ARMSignedOrFPCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700350 switch (cond) {
351 case kCondEQ: return EQ;
352 case kCondNE: return NE;
353 case kCondLT: return LT;
354 case kCondLE: return LE;
355 case kCondGT: return GT;
356 case kCondGE: return GE;
Dave Allison20dfc792014-06-16 20:44:29 -0700357 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100358 LOG(FATAL) << "Unreachable";
359 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700360}
361
Roland Levillain4fa13f62015-07-06 18:11:54 +0100362inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700363 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100364 case kCondEQ: return EQ;
365 case kCondNE: return NE;
366 case kCondLT: return LO;
367 case kCondLE: return LS;
368 case kCondGT: return HI;
369 case kCondGE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700370 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100371 LOG(FATAL) << "Unreachable";
372 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700373}
374
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100375void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100376 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100377}
378
379void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100380 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100381}
382
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100383size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
384 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
385 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100386}
387
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100388size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
389 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
390 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100391}
392
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000393size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
394 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
395 return kArmWordSize;
396}
397
398size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
399 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
400 return kArmWordSize;
401}
402
Calin Juravle34166012014-12-19 17:22:29 +0000403CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000404 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100405 const CompilerOptions& compiler_options,
406 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000407 : CodeGenerator(graph,
408 kNumberOfCoreRegisters,
409 kNumberOfSRegisters,
410 kNumberOfRegisterPairs,
411 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
412 arraysize(kCoreCalleeSaves)),
413 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
414 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100415 compiler_options,
416 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100417 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100419 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100420 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000421 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000422 isa_features_(isa_features),
423 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
424 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
425 relative_call_patches_(graph->GetArena()->Adapter()) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700426 // Always save the LR register to mimic Quick.
427 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100428}
429
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000430void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
431 // Ensure that we fix up branches and literal loads and emit the literal pool.
432 __ FinalizeCode();
433
434 // Adjust native pc offsets in stack maps.
435 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
436 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
437 uint32_t new_position = __ GetAdjustedPosition(old_position);
438 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
439 }
440 // Adjust native pc offsets of block labels.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100441 for (HBasicBlock* block : *block_order_) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000442 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
443 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
444 DCHECK_LT(static_cast<size_t>(block->GetBlockId()), block_labels_.Size());
445 Label* block_label = &block_labels_.GetRawStorage()[block->GetBlockId()];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000446 DCHECK_EQ(block_label->IsBound(), !block->IsSingleJump());
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000447 if (block_label->IsBound()) {
448 __ AdjustLabelPosition(block_label);
449 }
450 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100451 // Adjust pc offsets for the disassembly information.
452 if (disasm_info_ != nullptr) {
453 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
454 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
455 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
456 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
457 it.second.start = __ GetAdjustedPosition(it.second.start);
458 it.second.end = __ GetAdjustedPosition(it.second.end);
459 }
460 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
461 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
462 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
463 }
464 }
Vladimir Marko58155012015-08-19 12:49:41 +0000465 // Adjust pc offsets for relative call patches.
466 for (MethodPatchInfo<Label>& info : relative_call_patches_) {
467 __ AdjustLabelPosition(&info.label);
468 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000469
470 CodeGenerator::Finalize(allocator);
471}
472
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100473Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474 switch (type) {
475 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100476 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100477 ArmManagedRegister pair =
478 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100479 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
480 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
481
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100482 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
483 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100484 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100485 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100486 }
487
488 case Primitive::kPrimByte:
489 case Primitive::kPrimBoolean:
490 case Primitive::kPrimChar:
491 case Primitive::kPrimShort:
492 case Primitive::kPrimInt:
493 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100494 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100495 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100496 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
497 ArmManagedRegister current =
498 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
499 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100500 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100501 }
502 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100503 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504 }
505
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000506 case Primitive::kPrimFloat: {
507 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100508 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100509 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100510
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000511 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000512 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
513 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000514 return Location::FpuRegisterPairLocation(reg, reg + 1);
515 }
516
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100517 case Primitive::kPrimVoid:
518 LOG(FATAL) << "Unreachable type " << type;
519 }
520
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100521 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100522}
523
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000524void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100525 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100526 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100527
528 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100529 blocked_core_registers_[SP] = true;
530 blocked_core_registers_[LR] = true;
531 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100532
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100533 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100534 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100536 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100537 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100538
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000539 if (is_baseline) {
540 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
541 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
542 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000543
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000544 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
545
546 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
547 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
548 }
549 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100550
551 UpdateBlockedPairRegisters();
552}
553
554void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
555 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
556 ArmManagedRegister current =
557 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
558 if (blocked_core_registers_[current.AsRegisterPairLow()]
559 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
560 blocked_register_pairs_[i] = true;
561 }
562 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100563}
564
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100565InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
566 : HGraphVisitor(graph),
567 assembler_(codegen->GetAssembler()),
568 codegen_(codegen) {}
569
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000570void CodeGeneratorARM::ComputeSpillMask() {
571 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000572 // Save one extra register for baseline. Note that on thumb2, there is no easy
573 // instruction to restore just the PC, so this actually helps both baseline
574 // and non-baseline to save and restore at least two registers at entry and exit.
575 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000576 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
577 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
578 // We use vpush and vpop for saving and restoring floating point registers, which take
579 // a SRegister and the number of registers to save/restore after that SRegister. We
580 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
581 // but in the range.
582 if (fpu_spill_mask_ != 0) {
583 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
584 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
585 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
586 fpu_spill_mask_ |= (1 << i);
587 }
588 }
589}
590
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100591static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100592 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100593}
594
595static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100596 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100597}
598
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000599void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000600 bool skip_overflow_check =
601 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000602 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000603 __ Bind(&frame_entry_label_);
604
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000605 if (HasEmptyFrame()) {
606 return;
607 }
608
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100609 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000610 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
611 __ LoadFromOffset(kLoadWord, IP, IP, 0);
612 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100613 }
614
Andreas Gampe501fd632015-09-10 16:11:06 -0700615 __ PushList(core_spill_mask_);
616 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
617 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000618 if (fpu_spill_mask_ != 0) {
619 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
620 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100621 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100622 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000623 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100624 int adjust = GetFrameSize() - FrameEntrySpillSize();
625 __ AddConstant(SP, -adjust);
626 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100627 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000628}
629
630void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000631 if (HasEmptyFrame()) {
632 __ bx(LR);
633 return;
634 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100635 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100636 int adjust = GetFrameSize() - FrameEntrySpillSize();
637 __ AddConstant(SP, adjust);
638 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000639 if (fpu_spill_mask_ != 0) {
640 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
641 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100642 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
643 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000644 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700645 // Pop LR into PC to return.
646 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
647 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
648 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100649 __ cfi().RestoreState();
650 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000651}
652
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100653void CodeGeneratorARM::Bind(HBasicBlock* block) {
654 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000655}
656
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100657Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
658 switch (load->GetType()) {
659 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100661 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100662
663 case Primitive::kPrimInt:
664 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100665 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100667
668 case Primitive::kPrimBoolean:
669 case Primitive::kPrimByte:
670 case Primitive::kPrimChar:
671 case Primitive::kPrimShort:
672 case Primitive::kPrimVoid:
673 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700674 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100675 }
676
677 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700678 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100679}
680
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100681Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100682 switch (type) {
683 case Primitive::kPrimBoolean:
684 case Primitive::kPrimByte:
685 case Primitive::kPrimChar:
686 case Primitive::kPrimShort:
687 case Primitive::kPrimInt:
688 case Primitive::kPrimNot: {
689 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000690 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100691 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100692 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100693 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000694 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100695 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100696 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100697
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000698 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100699 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000700 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100701 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000702 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100703 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000704 if (calling_convention.GetRegisterAt(index) == R1) {
705 // Skip R1, and use R2_R3 instead.
706 gp_index_++;
707 index++;
708 }
709 }
710 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
711 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000712 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100713
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000714 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000715 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100716 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000717 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
718 }
719 }
720
721 case Primitive::kPrimFloat: {
722 uint32_t stack_index = stack_index_++;
723 if (float_index_ % 2 == 0) {
724 float_index_ = std::max(double_index_, float_index_);
725 }
726 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
727 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
728 } else {
729 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
730 }
731 }
732
733 case Primitive::kPrimDouble: {
734 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
735 uint32_t stack_index = stack_index_;
736 stack_index_ += 2;
737 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
738 uint32_t index = double_index_;
739 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000740 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000741 calling_convention.GetFpuRegisterAt(index),
742 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000743 DCHECK(ExpectedPairLayout(result));
744 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000745 } else {
746 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100747 }
748 }
749
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100750 case Primitive::kPrimVoid:
751 LOG(FATAL) << "Unexpected parameter type " << type;
752 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100753 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100754 return Location();
755}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100756
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100757Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000758 switch (type) {
759 case Primitive::kPrimBoolean:
760 case Primitive::kPrimByte:
761 case Primitive::kPrimChar:
762 case Primitive::kPrimShort:
763 case Primitive::kPrimInt:
764 case Primitive::kPrimNot: {
765 return Location::RegisterLocation(R0);
766 }
767
768 case Primitive::kPrimFloat: {
769 return Location::FpuRegisterLocation(S0);
770 }
771
772 case Primitive::kPrimLong: {
773 return Location::RegisterPairLocation(R0, R1);
774 }
775
776 case Primitive::kPrimDouble: {
777 return Location::FpuRegisterPairLocation(S0, S1);
778 }
779
780 case Primitive::kPrimVoid:
781 return Location();
782 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100783
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000784 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000785}
786
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100787Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
788 return Location::RegisterLocation(kMethodRegisterArgument);
789}
790
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791void CodeGeneratorARM::Move32(Location destination, Location source) {
792 if (source.Equals(destination)) {
793 return;
794 }
795 if (destination.IsRegister()) {
796 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000799 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000801 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100802 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100803 } else if (destination.IsFpuRegister()) {
804 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000805 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100806 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000807 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100808 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000809 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100810 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000812 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100813 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000814 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000816 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100817 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100819 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
820 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100821 }
822 }
823}
824
825void CodeGeneratorARM::Move64(Location destination, Location source) {
826 if (source.Equals(destination)) {
827 return;
828 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100829 if (destination.IsRegisterPair()) {
830 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000831 EmitParallelMoves(
832 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
833 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100834 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000835 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100836 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
837 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100838 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000839 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100840 } else {
841 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000842 DCHECK(ExpectedPairLayout(destination));
843 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
844 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000846 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100847 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000848 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
849 SP,
850 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100851 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000852 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100854 } else {
855 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100856 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000857 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100858 if (source.AsRegisterPairLow<Register>() == R1) {
859 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100860 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
861 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100862 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100863 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864 SP, destination.GetStackIndex());
865 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000866 } else if (source.IsFpuRegisterPair()) {
867 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
868 SP,
869 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 } else {
871 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000872 EmitParallelMoves(
873 Location::StackSlot(source.GetStackIndex()),
874 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100875 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000876 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100877 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
878 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100879 }
880 }
881}
882
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100883void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100884 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100885 if (instruction->IsCurrentMethod()) {
886 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
887 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100888 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100889 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000890 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000891 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
892 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000893 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000894 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000895 } else {
896 DCHECK(location.IsStackSlot());
897 __ LoadImmediate(IP, value);
898 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
899 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000900 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000901 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000902 int64_t value = const_to_move->AsLongConstant()->GetValue();
903 if (location.IsRegisterPair()) {
904 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
905 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
906 } else {
907 DCHECK(location.IsDoubleStackSlot());
908 __ LoadImmediate(IP, Low32Bits(value));
909 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
910 __ LoadImmediate(IP, High32Bits(value));
911 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
912 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 }
Roland Levillain476df552014-10-09 17:51:36 +0100914 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100915 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
916 switch (instruction->GetType()) {
917 case Primitive::kPrimBoolean:
918 case Primitive::kPrimByte:
919 case Primitive::kPrimChar:
920 case Primitive::kPrimShort:
921 case Primitive::kPrimInt:
922 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100923 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100924 Move32(location, Location::StackSlot(stack_slot));
925 break;
926
927 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100928 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100929 Move64(location, Location::DoubleStackSlot(stack_slot));
930 break;
931
932 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100933 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100934 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000935 } else if (instruction->IsTemporary()) {
936 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000937 if (temp_location.IsStackSlot()) {
938 Move32(location, temp_location);
939 } else {
940 DCHECK(temp_location.IsDoubleStackSlot());
941 Move64(location, temp_location);
942 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000943 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100944 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100945 switch (instruction->GetType()) {
946 case Primitive::kPrimBoolean:
947 case Primitive::kPrimByte:
948 case Primitive::kPrimChar:
949 case Primitive::kPrimShort:
950 case Primitive::kPrimNot:
951 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100952 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100953 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100954 break;
955
956 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100957 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100958 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 break;
960
961 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100962 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100963 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000964 }
965}
966
Calin Juravle175dc732015-08-25 15:42:32 +0100967void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
968 DCHECK(location.IsRegister());
969 __ LoadImmediate(location.AsRegister<Register>(), value);
970}
971
Calin Juravle23a8e352015-09-08 19:56:31 +0100972void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
973 if (location.IsRegister()) {
974 locations->AddTemp(location);
975 } else if (location.IsRegisterPair()) {
976 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
977 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
978 } else {
979 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
980 }
981}
982
983void CodeGeneratorARM::MoveLocationToTemp(Location source,
984 const LocationSummary& locations,
985 int temp_index,
986 Primitive::Type type) {
987 if (!Primitive::IsFloatingPointType(type)) {
988 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
989 }
990
991 if (type == Primitive::kPrimFloat) {
992 DCHECK(source.IsFpuRegister()) << source;
993 __ vmovrs(locations.GetTemp(temp_index).AsRegister<Register>(),
994 source.AsFpuRegister<SRegister>());
995 } else {
996 DCHECK_EQ(type, Primitive::kPrimDouble) << type;
997 DCHECK(source.IsFpuRegisterPair()) << source;
998 __ vmovrrd(locations.GetTemp(temp_index).AsRegister<Register>(),
999 locations.GetTemp(temp_index + 1).AsRegister<Register>(),
1000 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
1001 }
1002}
1003
1004void CodeGeneratorARM::MoveTempToLocation(const LocationSummary& locations,
1005 int temp_index,
1006 Location destination,
1007 Primitive::Type type) {
1008 if (!Primitive::IsFloatingPointType(type)) {
1009 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
1010 }
1011
1012 if (type == Primitive::kPrimFloat) {
1013 DCHECK(destination.IsFpuRegister()) << destination;
1014 __ vmovsr(destination.AsFpuRegister<SRegister>(),
1015 locations.GetTemp(temp_index).AsRegister<Register>());
1016 } else {
1017 DCHECK(type == Primitive::kPrimDouble);
1018 DCHECK(destination.IsFpuRegisterPair()) << destination;
1019 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1020 locations.GetTemp(temp_index).AsRegister<Register>(),
1021 locations.GetTemp(temp_index + 1).AsRegister<Register>());
1022 }
1023}
1024
Calin Juravle175dc732015-08-25 15:42:32 +01001025void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1026 HInstruction* instruction,
1027 uint32_t dex_pc,
1028 SlowPathCode* slow_path) {
1029 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1030 instruction,
1031 dex_pc,
1032 slow_path);
1033}
1034
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001035void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1036 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001037 uint32_t dex_pc,
1038 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001039 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001040 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1041 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001042 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001043}
1044
David Brazdilfc6a86a2015-06-26 10:33:45 +00001045void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001046 DCHECK(!successor->IsExitBlock());
1047
1048 HBasicBlock* block = got->GetBlock();
1049 HInstruction* previous = got->GetPrevious();
1050
1051 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001052 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001053 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1054 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1055 return;
1056 }
1057
1058 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1059 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1060 }
1061 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001062 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001063 }
1064}
1065
David Brazdilfc6a86a2015-06-26 10:33:45 +00001066void LocationsBuilderARM::VisitGoto(HGoto* got) {
1067 got->SetLocations(nullptr);
1068}
1069
1070void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1071 HandleGoto(got, got->GetSuccessor());
1072}
1073
1074void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1075 try_boundary->SetLocations(nullptr);
1076}
1077
1078void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1079 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1080 if (!successor->IsExitBlock()) {
1081 HandleGoto(try_boundary, successor);
1082 }
1083}
1084
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001085void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001086 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001087}
1088
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001089void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001090 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001091}
1092
Roland Levillain4fa13f62015-07-06 18:11:54 +01001093void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1094 ShifterOperand operand;
1095 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1096 __ cmp(left, operand);
1097 } else {
1098 Register temp = IP;
1099 __ LoadImmediate(temp, right);
1100 __ cmp(left, ShifterOperand(temp));
1101 }
1102}
1103
1104void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1105 Label* true_label,
1106 Label* false_label) {
1107 __ vmstat(); // transfer FP status register to ARM APSR.
1108 if (cond->IsFPConditionTrueIfNaN()) {
1109 __ b(true_label, VS); // VS for unordered.
1110 } else if (cond->IsFPConditionFalseIfNaN()) {
1111 __ b(false_label, VS); // VS for unordered.
1112 }
1113 __ b(true_label, ARMSignedOrFPCondition(cond->GetCondition()));
1114}
1115
1116void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1117 Label* true_label,
1118 Label* false_label) {
1119 LocationSummary* locations = cond->GetLocations();
1120 Location left = locations->InAt(0);
1121 Location right = locations->InAt(1);
1122 IfCondition if_cond = cond->GetCondition();
1123
1124 Register left_high = left.AsRegisterPairHigh<Register>();
1125 Register left_low = left.AsRegisterPairLow<Register>();
1126 IfCondition true_high_cond = if_cond;
1127 IfCondition false_high_cond = cond->GetOppositeCondition();
1128 Condition final_condition = ARMUnsignedCondition(if_cond);
1129
1130 // Set the conditions for the test, remembering that == needs to be
1131 // decided using the low words.
1132 switch (if_cond) {
1133 case kCondEQ:
1134 case kCondNE:
1135 // Nothing to do.
1136 break;
1137 case kCondLT:
1138 false_high_cond = kCondGT;
1139 break;
1140 case kCondLE:
1141 true_high_cond = kCondLT;
1142 break;
1143 case kCondGT:
1144 false_high_cond = kCondLT;
1145 break;
1146 case kCondGE:
1147 true_high_cond = kCondGT;
1148 break;
1149 }
1150 if (right.IsConstant()) {
1151 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1152 int32_t val_low = Low32Bits(value);
1153 int32_t val_high = High32Bits(value);
1154
1155 GenerateCompareWithImmediate(left_high, val_high);
1156 if (if_cond == kCondNE) {
1157 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1158 } else if (if_cond == kCondEQ) {
1159 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1160 } else {
1161 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1162 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1163 }
1164 // Must be equal high, so compare the lows.
1165 GenerateCompareWithImmediate(left_low, val_low);
1166 } else {
1167 Register right_high = right.AsRegisterPairHigh<Register>();
1168 Register right_low = right.AsRegisterPairLow<Register>();
1169
1170 __ cmp(left_high, ShifterOperand(right_high));
1171 if (if_cond == kCondNE) {
1172 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1173 } else if (if_cond == kCondEQ) {
1174 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1175 } else {
1176 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1177 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1178 }
1179 // Must be equal high, so compare the lows.
1180 __ cmp(left_low, ShifterOperand(right_low));
1181 }
1182 // The last comparison might be unsigned.
1183 __ b(true_label, final_condition);
1184}
1185
1186void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1187 HCondition* condition,
1188 Label* true_target,
1189 Label* false_target,
1190 Label* always_true_target) {
1191 LocationSummary* locations = condition->GetLocations();
1192 Location left = locations->InAt(0);
1193 Location right = locations->InAt(1);
1194
1195 // We don't want true_target as a nullptr.
1196 if (true_target == nullptr) {
1197 true_target = always_true_target;
1198 }
1199 bool falls_through = (false_target == nullptr);
1200
1201 // FP compares don't like null false_targets.
1202 if (false_target == nullptr) {
1203 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1204 }
1205
1206 Primitive::Type type = condition->InputAt(0)->GetType();
1207 switch (type) {
1208 case Primitive::kPrimLong:
1209 GenerateLongComparesAndJumps(condition, true_target, false_target);
1210 break;
1211 case Primitive::kPrimFloat:
1212 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1213 GenerateFPJumps(condition, true_target, false_target);
1214 break;
1215 case Primitive::kPrimDouble:
1216 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1217 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1218 GenerateFPJumps(condition, true_target, false_target);
1219 break;
1220 default:
1221 LOG(FATAL) << "Unexpected compare type " << type;
1222 }
1223
1224 if (!falls_through) {
1225 __ b(false_target);
1226 }
1227}
1228
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001229void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1230 Label* true_target,
1231 Label* false_target,
1232 Label* always_true_target) {
1233 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001234 if (cond->IsIntConstant()) {
1235 // Constant condition, statically compared against 1.
1236 int32_t cond_value = cond->AsIntConstant()->GetValue();
1237 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001238 if (always_true_target != nullptr) {
1239 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001240 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001241 return;
1242 } else {
1243 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001244 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001245 } else {
1246 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1247 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001248 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001249 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1250 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001251 } else {
1252 // Condition has not been materialized, use its inputs as the
1253 // comparison and its condition as the branch condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001254 Primitive::Type type =
1255 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
1256 // Is this a long or FP comparison that has been folded into the HCondition?
1257 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1258 // Generate the comparison directly.
1259 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1260 true_target, false_target, always_true_target);
1261 return;
1262 }
1263
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001264 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001265 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001266 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001267 Location right = locations->InAt(1);
1268 if (right.IsRegister()) {
1269 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001270 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001271 DCHECK(right.IsConstant());
1272 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001273 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001274 __ b(true_target, ARMSignedOrFPCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001275 }
Dave Allison20dfc792014-06-16 20:44:29 -07001276 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001277 if (false_target != nullptr) {
1278 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001279 }
1280}
1281
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001282void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1283 LocationSummary* locations =
1284 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1285 HInstruction* cond = if_instr->InputAt(0);
1286 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1287 locations->SetInAt(0, Location::RequiresRegister());
1288 }
1289}
1290
1291void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1292 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1293 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1294 Label* always_true_target = true_target;
1295 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1296 if_instr->IfTrueSuccessor())) {
1297 always_true_target = nullptr;
1298 }
1299 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1300 if_instr->IfFalseSuccessor())) {
1301 false_target = nullptr;
1302 }
1303 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1304}
1305
1306void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1307 LocationSummary* locations = new (GetGraph()->GetArena())
1308 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1309 HInstruction* cond = deoptimize->InputAt(0);
1310 DCHECK(cond->IsCondition());
1311 if (cond->AsCondition()->NeedsMaterialization()) {
1312 locations->SetInAt(0, Location::RequiresRegister());
1313 }
1314}
1315
1316void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1317 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
1318 DeoptimizationSlowPathARM(deoptimize);
1319 codegen_->AddSlowPath(slow_path);
1320 Label* slow_path_entry = slow_path->GetEntryLabel();
1321 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1322}
Dave Allison20dfc792014-06-16 20:44:29 -07001323
Roland Levillain0d37cd02015-05-27 16:39:19 +01001324void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001325 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001326 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001327 // Handle the long/FP comparisons made in instruction simplification.
1328 switch (cond->InputAt(0)->GetType()) {
1329 case Primitive::kPrimLong:
1330 locations->SetInAt(0, Location::RequiresRegister());
1331 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1332 if (cond->NeedsMaterialization()) {
1333 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1334 }
1335 break;
1336
1337 case Primitive::kPrimFloat:
1338 case Primitive::kPrimDouble:
1339 locations->SetInAt(0, Location::RequiresFpuRegister());
1340 locations->SetInAt(1, Location::RequiresFpuRegister());
1341 if (cond->NeedsMaterialization()) {
1342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1343 }
1344 break;
1345
1346 default:
1347 locations->SetInAt(0, Location::RequiresRegister());
1348 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1349 if (cond->NeedsMaterialization()) {
1350 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1351 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001352 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001353}
1354
Roland Levillain0d37cd02015-05-27 16:39:19 +01001355void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001356 if (!cond->NeedsMaterialization()) {
1357 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001358 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001359
1360 LocationSummary* locations = cond->GetLocations();
1361 Location left = locations->InAt(0);
1362 Location right = locations->InAt(1);
1363 Register out = locations->Out().AsRegister<Register>();
1364 Label true_label, false_label;
1365
1366 switch (cond->InputAt(0)->GetType()) {
1367 default: {
1368 // Integer case.
1369 if (right.IsRegister()) {
1370 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1371 } else {
1372 DCHECK(right.IsConstant());
1373 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1374 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1375 }
1376 __ it(ARMSignedOrFPCondition(cond->GetCondition()), kItElse);
1377 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
1378 ARMSignedOrFPCondition(cond->GetCondition()));
1379 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
1380 ARMSignedOrFPCondition(cond->GetOppositeCondition()));
1381 return;
1382 }
1383 case Primitive::kPrimLong:
1384 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1385 break;
1386 case Primitive::kPrimFloat:
1387 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1388 GenerateFPJumps(cond, &true_label, &false_label);
1389 break;
1390 case Primitive::kPrimDouble:
1391 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1392 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1393 GenerateFPJumps(cond, &true_label, &false_label);
1394 break;
1395 }
1396
1397 // Convert the jumps into the result.
1398 Label done_label;
1399
1400 // False case: result = 0.
1401 __ Bind(&false_label);
1402 __ LoadImmediate(out, 0);
1403 __ b(&done_label);
1404
1405 // True case: result = 1.
1406 __ Bind(&true_label);
1407 __ LoadImmediate(out, 1);
1408 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001409}
1410
1411void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1412 VisitCondition(comp);
1413}
1414
1415void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1416 VisitCondition(comp);
1417}
1418
1419void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1420 VisitCondition(comp);
1421}
1422
1423void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1424 VisitCondition(comp);
1425}
1426
1427void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1428 VisitCondition(comp);
1429}
1430
1431void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1432 VisitCondition(comp);
1433}
1434
1435void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1436 VisitCondition(comp);
1437}
1438
1439void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1440 VisitCondition(comp);
1441}
1442
1443void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1444 VisitCondition(comp);
1445}
1446
1447void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1448 VisitCondition(comp);
1449}
1450
1451void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1452 VisitCondition(comp);
1453}
1454
1455void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1456 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001457}
1458
1459void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001460 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001461}
1462
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001463void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1464 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001465}
1466
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001467void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001468 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001469}
1470
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001471void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001472 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001473 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001474}
1475
1476void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001477 LocationSummary* locations =
1478 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001479 switch (store->InputAt(1)->GetType()) {
1480 case Primitive::kPrimBoolean:
1481 case Primitive::kPrimByte:
1482 case Primitive::kPrimChar:
1483 case Primitive::kPrimShort:
1484 case Primitive::kPrimInt:
1485 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001486 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1488 break;
1489
1490 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001491 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001492 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1493 break;
1494
1495 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001496 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001497 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001498}
1499
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001500void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001501 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001502}
1503
1504void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001505 LocationSummary* locations =
1506 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001507 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001508}
1509
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001510void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001511 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001512 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001513}
1514
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001515void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1516 LocationSummary* locations =
1517 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1518 locations->SetOut(Location::ConstantLocation(constant));
1519}
1520
1521void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1522 // Will be generated at use site.
1523 UNUSED(constant);
1524}
1525
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001527 LocationSummary* locations =
1528 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001529 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001530}
1531
1532void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1533 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001534 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001535}
1536
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001537void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1538 LocationSummary* locations =
1539 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1540 locations->SetOut(Location::ConstantLocation(constant));
1541}
1542
1543void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1544 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001545 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001546}
1547
1548void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1549 LocationSummary* locations =
1550 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1551 locations->SetOut(Location::ConstantLocation(constant));
1552}
1553
1554void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1555 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001556 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001557}
1558
Calin Juravle27df7582015-04-17 19:12:31 +01001559void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1560 memory_barrier->SetLocations(nullptr);
1561}
1562
1563void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1564 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1565}
1566
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001567void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001568 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001569}
1570
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001571void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001572 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001573 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001574}
1575
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001576void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001577 LocationSummary* locations =
1578 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001579 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001580}
1581
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001582void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001583 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001584 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001585}
1586
Calin Juravle175dc732015-08-25 15:42:32 +01001587void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1588 // The trampoline uses the same calling convention as dex calling conventions,
1589 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1590 // the method_idx.
1591 HandleInvoke(invoke);
1592}
1593
1594void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1595 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1596}
1597
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001598void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001599 // When we do not run baseline, explicit clinit checks triggered by static
1600 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1601 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001602
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001603 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1604 codegen_->GetInstructionSetFeatures());
1605 if (intrinsic.TryDispatch(invoke)) {
1606 return;
1607 }
1608
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001609 HandleInvoke(invoke);
1610}
1611
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001612static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1613 if (invoke->GetLocations()->Intrinsified()) {
1614 IntrinsicCodeGeneratorARM intrinsic(codegen);
1615 intrinsic.Dispatch(invoke);
1616 return true;
1617 }
1618 return false;
1619}
1620
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001621void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001622 // When we do not run baseline, explicit clinit checks triggered by static
1623 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1624 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001625
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001626 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1627 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001628 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001629
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001630 LocationSummary* locations = invoke->GetLocations();
1631 codegen_->GenerateStaticOrDirectCall(
1632 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001633 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001634}
1635
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001636void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001637 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001638 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001639}
1640
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001641void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001642 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1643 codegen_->GetInstructionSetFeatures());
1644 if (intrinsic.TryDispatch(invoke)) {
1645 return;
1646 }
1647
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001648 HandleInvoke(invoke);
1649}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001650
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001651void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001652 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1653 return;
1654 }
1655
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001656 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001657 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001658 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001659}
1660
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001661void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1662 HandleInvoke(invoke);
1663 // Add the hidden argument.
1664 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1665}
1666
1667void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1668 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001669 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001670 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1671 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001672 LocationSummary* locations = invoke->GetLocations();
1673 Location receiver = locations->InAt(0);
1674 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1675
1676 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001677 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1678 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001679
1680 // temp = object->GetClass();
1681 if (receiver.IsStackSlot()) {
1682 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1683 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1684 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001686 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001687 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001688 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001689 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001690 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001691 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001692 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1693 // LR = temp->GetEntryPoint();
1694 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1695 // LR();
1696 __ blx(LR);
1697 DCHECK(!codegen_->IsLeafMethod());
1698 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1699}
1700
Roland Levillain88cb1752014-10-20 16:36:47 +01001701void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1702 LocationSummary* locations =
1703 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1704 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001705 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001706 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1708 break;
1709 }
1710 case Primitive::kPrimLong: {
1711 locations->SetInAt(0, Location::RequiresRegister());
1712 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001713 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001714 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001715
Roland Levillain88cb1752014-10-20 16:36:47 +01001716 case Primitive::kPrimFloat:
1717 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001718 locations->SetInAt(0, Location::RequiresFpuRegister());
1719 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001720 break;
1721
1722 default:
1723 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1724 }
1725}
1726
1727void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1728 LocationSummary* locations = neg->GetLocations();
1729 Location out = locations->Out();
1730 Location in = locations->InAt(0);
1731 switch (neg->GetResultType()) {
1732 case Primitive::kPrimInt:
1733 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001734 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001735 break;
1736
1737 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001738 DCHECK(in.IsRegisterPair());
1739 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1740 __ rsbs(out.AsRegisterPairLow<Register>(),
1741 in.AsRegisterPairLow<Register>(),
1742 ShifterOperand(0));
1743 // We cannot emit an RSC (Reverse Subtract with Carry)
1744 // instruction here, as it does not exist in the Thumb-2
1745 // instruction set. We use the following approach
1746 // using SBC and SUB instead.
1747 //
1748 // out.hi = -C
1749 __ sbc(out.AsRegisterPairHigh<Register>(),
1750 out.AsRegisterPairHigh<Register>(),
1751 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1752 // out.hi = out.hi - in.hi
1753 __ sub(out.AsRegisterPairHigh<Register>(),
1754 out.AsRegisterPairHigh<Register>(),
1755 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1756 break;
1757
Roland Levillain88cb1752014-10-20 16:36:47 +01001758 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001759 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001760 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001761 break;
1762
Roland Levillain88cb1752014-10-20 16:36:47 +01001763 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001764 DCHECK(in.IsFpuRegisterPair());
1765 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1766 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001767 break;
1768
1769 default:
1770 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1771 }
1772}
1773
Roland Levillaindff1f282014-11-05 14:15:05 +00001774void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001775 Primitive::Type result_type = conversion->GetResultType();
1776 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001777 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001778
Roland Levillain5b3ee562015-04-14 16:02:41 +01001779 // The float-to-long, double-to-long and long-to-float type conversions
1780 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001781 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001782 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1783 && result_type == Primitive::kPrimLong)
1784 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001785 ? LocationSummary::kCall
1786 : LocationSummary::kNoCall;
1787 LocationSummary* locations =
1788 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1789
David Brazdilb2bd1c52015-03-25 11:17:37 +00001790 // The Java language does not allow treating boolean as an integral type but
1791 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001792
Roland Levillaindff1f282014-11-05 14:15:05 +00001793 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001794 case Primitive::kPrimByte:
1795 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001796 case Primitive::kPrimBoolean:
1797 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001798 case Primitive::kPrimShort:
1799 case Primitive::kPrimInt:
1800 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001801 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001802 locations->SetInAt(0, Location::RequiresRegister());
1803 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1804 break;
1805
1806 default:
1807 LOG(FATAL) << "Unexpected type conversion from " << input_type
1808 << " to " << result_type;
1809 }
1810 break;
1811
Roland Levillain01a8d712014-11-14 16:27:39 +00001812 case Primitive::kPrimShort:
1813 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001814 case Primitive::kPrimBoolean:
1815 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001816 case Primitive::kPrimByte:
1817 case Primitive::kPrimInt:
1818 case Primitive::kPrimChar:
1819 // Processing a Dex `int-to-short' instruction.
1820 locations->SetInAt(0, Location::RequiresRegister());
1821 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1822 break;
1823
1824 default:
1825 LOG(FATAL) << "Unexpected type conversion from " << input_type
1826 << " to " << result_type;
1827 }
1828 break;
1829
Roland Levillain946e1432014-11-11 17:35:19 +00001830 case Primitive::kPrimInt:
1831 switch (input_type) {
1832 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001833 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001834 locations->SetInAt(0, Location::Any());
1835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1836 break;
1837
1838 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001839 // Processing a Dex `float-to-int' instruction.
1840 locations->SetInAt(0, Location::RequiresFpuRegister());
1841 locations->SetOut(Location::RequiresRegister());
1842 locations->AddTemp(Location::RequiresFpuRegister());
1843 break;
1844
Roland Levillain946e1432014-11-11 17:35:19 +00001845 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001846 // Processing a Dex `double-to-int' instruction.
1847 locations->SetInAt(0, Location::RequiresFpuRegister());
1848 locations->SetOut(Location::RequiresRegister());
1849 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001850 break;
1851
1852 default:
1853 LOG(FATAL) << "Unexpected type conversion from " << input_type
1854 << " to " << result_type;
1855 }
1856 break;
1857
Roland Levillaindff1f282014-11-05 14:15:05 +00001858 case Primitive::kPrimLong:
1859 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001860 case Primitive::kPrimBoolean:
1861 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001862 case Primitive::kPrimByte:
1863 case Primitive::kPrimShort:
1864 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001865 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001866 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001867 locations->SetInAt(0, Location::RequiresRegister());
1868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1869 break;
1870
Roland Levillain624279f2014-12-04 11:54:28 +00001871 case Primitive::kPrimFloat: {
1872 // Processing a Dex `float-to-long' instruction.
1873 InvokeRuntimeCallingConvention calling_convention;
1874 locations->SetInAt(0, Location::FpuRegisterLocation(
1875 calling_convention.GetFpuRegisterAt(0)));
1876 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1877 break;
1878 }
1879
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001880 case Primitive::kPrimDouble: {
1881 // Processing a Dex `double-to-long' instruction.
1882 InvokeRuntimeCallingConvention calling_convention;
1883 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1884 calling_convention.GetFpuRegisterAt(0),
1885 calling_convention.GetFpuRegisterAt(1)));
1886 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001887 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001888 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001889
1890 default:
1891 LOG(FATAL) << "Unexpected type conversion from " << input_type
1892 << " to " << result_type;
1893 }
1894 break;
1895
Roland Levillain981e4542014-11-14 11:47:14 +00001896 case Primitive::kPrimChar:
1897 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001898 case Primitive::kPrimBoolean:
1899 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001900 case Primitive::kPrimByte:
1901 case Primitive::kPrimShort:
1902 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001903 // Processing a Dex `int-to-char' instruction.
1904 locations->SetInAt(0, Location::RequiresRegister());
1905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1906 break;
1907
1908 default:
1909 LOG(FATAL) << "Unexpected type conversion from " << input_type
1910 << " to " << result_type;
1911 }
1912 break;
1913
Roland Levillaindff1f282014-11-05 14:15:05 +00001914 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001915 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001916 case Primitive::kPrimBoolean:
1917 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001918 case Primitive::kPrimByte:
1919 case Primitive::kPrimShort:
1920 case Primitive::kPrimInt:
1921 case Primitive::kPrimChar:
1922 // Processing a Dex `int-to-float' instruction.
1923 locations->SetInAt(0, Location::RequiresRegister());
1924 locations->SetOut(Location::RequiresFpuRegister());
1925 break;
1926
Roland Levillain5b3ee562015-04-14 16:02:41 +01001927 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00001928 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001929 InvokeRuntimeCallingConvention calling_convention;
1930 locations->SetInAt(0, Location::RegisterPairLocation(
1931 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1932 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001933 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01001934 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001935
Roland Levillaincff13742014-11-17 14:32:17 +00001936 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001937 // Processing a Dex `double-to-float' instruction.
1938 locations->SetInAt(0, Location::RequiresFpuRegister());
1939 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001940 break;
1941
1942 default:
1943 LOG(FATAL) << "Unexpected type conversion from " << input_type
1944 << " to " << result_type;
1945 };
1946 break;
1947
Roland Levillaindff1f282014-11-05 14:15:05 +00001948 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001949 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001950 case Primitive::kPrimBoolean:
1951 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001952 case Primitive::kPrimByte:
1953 case Primitive::kPrimShort:
1954 case Primitive::kPrimInt:
1955 case Primitive::kPrimChar:
1956 // Processing a Dex `int-to-double' instruction.
1957 locations->SetInAt(0, Location::RequiresRegister());
1958 locations->SetOut(Location::RequiresFpuRegister());
1959 break;
1960
1961 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001962 // Processing a Dex `long-to-double' instruction.
1963 locations->SetInAt(0, Location::RequiresRegister());
1964 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01001965 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001966 locations->AddTemp(Location::RequiresFpuRegister());
1967 break;
1968
Roland Levillaincff13742014-11-17 14:32:17 +00001969 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001970 // Processing a Dex `float-to-double' instruction.
1971 locations->SetInAt(0, Location::RequiresFpuRegister());
1972 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001973 break;
1974
1975 default:
1976 LOG(FATAL) << "Unexpected type conversion from " << input_type
1977 << " to " << result_type;
1978 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001979 break;
1980
1981 default:
1982 LOG(FATAL) << "Unexpected type conversion from " << input_type
1983 << " to " << result_type;
1984 }
1985}
1986
1987void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1988 LocationSummary* locations = conversion->GetLocations();
1989 Location out = locations->Out();
1990 Location in = locations->InAt(0);
1991 Primitive::Type result_type = conversion->GetResultType();
1992 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001993 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001994 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001995 case Primitive::kPrimByte:
1996 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001997 case Primitive::kPrimBoolean:
1998 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001999 case Primitive::kPrimShort:
2000 case Primitive::kPrimInt:
2001 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002002 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002003 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002004 break;
2005
2006 default:
2007 LOG(FATAL) << "Unexpected type conversion from " << input_type
2008 << " to " << result_type;
2009 }
2010 break;
2011
Roland Levillain01a8d712014-11-14 16:27:39 +00002012 case Primitive::kPrimShort:
2013 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002014 case Primitive::kPrimBoolean:
2015 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002016 case Primitive::kPrimByte:
2017 case Primitive::kPrimInt:
2018 case Primitive::kPrimChar:
2019 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002020 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002021 break;
2022
2023 default:
2024 LOG(FATAL) << "Unexpected type conversion from " << input_type
2025 << " to " << result_type;
2026 }
2027 break;
2028
Roland Levillain946e1432014-11-11 17:35:19 +00002029 case Primitive::kPrimInt:
2030 switch (input_type) {
2031 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002032 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002033 DCHECK(out.IsRegister());
2034 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002035 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002036 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002037 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002038 } else {
2039 DCHECK(in.IsConstant());
2040 DCHECK(in.GetConstant()->IsLongConstant());
2041 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002042 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002043 }
2044 break;
2045
Roland Levillain3f8f9362014-12-02 17:45:01 +00002046 case Primitive::kPrimFloat: {
2047 // Processing a Dex `float-to-int' instruction.
2048 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2049 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2050 __ vcvtis(temp, temp);
2051 __ vmovrs(out.AsRegister<Register>(), temp);
2052 break;
2053 }
2054
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002055 case Primitive::kPrimDouble: {
2056 // Processing a Dex `double-to-int' instruction.
2057 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2058 DRegister temp_d = FromLowSToD(temp_s);
2059 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2060 __ vcvtid(temp_s, temp_d);
2061 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002062 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002063 }
Roland Levillain946e1432014-11-11 17:35:19 +00002064
2065 default:
2066 LOG(FATAL) << "Unexpected type conversion from " << input_type
2067 << " to " << result_type;
2068 }
2069 break;
2070
Roland Levillaindff1f282014-11-05 14:15:05 +00002071 case Primitive::kPrimLong:
2072 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002073 case Primitive::kPrimBoolean:
2074 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002075 case Primitive::kPrimByte:
2076 case Primitive::kPrimShort:
2077 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002078 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002079 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002080 DCHECK(out.IsRegisterPair());
2081 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002083 // Sign extension.
2084 __ Asr(out.AsRegisterPairHigh<Register>(),
2085 out.AsRegisterPairLow<Register>(),
2086 31);
2087 break;
2088
2089 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002090 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002091 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2092 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002093 conversion->GetDexPc(),
2094 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002095 break;
2096
Roland Levillaindff1f282014-11-05 14:15:05 +00002097 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002098 // Processing a Dex `double-to-long' instruction.
2099 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2100 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002101 conversion->GetDexPc(),
2102 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002103 break;
2104
2105 default:
2106 LOG(FATAL) << "Unexpected type conversion from " << input_type
2107 << " to " << result_type;
2108 }
2109 break;
2110
Roland Levillain981e4542014-11-14 11:47:14 +00002111 case Primitive::kPrimChar:
2112 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002113 case Primitive::kPrimBoolean:
2114 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002115 case Primitive::kPrimByte:
2116 case Primitive::kPrimShort:
2117 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002118 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002119 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002120 break;
2121
2122 default:
2123 LOG(FATAL) << "Unexpected type conversion from " << input_type
2124 << " to " << result_type;
2125 }
2126 break;
2127
Roland Levillaindff1f282014-11-05 14:15:05 +00002128 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002129 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002130 case Primitive::kPrimBoolean:
2131 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002132 case Primitive::kPrimByte:
2133 case Primitive::kPrimShort:
2134 case Primitive::kPrimInt:
2135 case Primitive::kPrimChar: {
2136 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2138 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002139 break;
2140 }
2141
Roland Levillain5b3ee562015-04-14 16:02:41 +01002142 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002143 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002144 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2145 conversion,
2146 conversion->GetDexPc(),
2147 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002148 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002149
Roland Levillaincff13742014-11-17 14:32:17 +00002150 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002151 // Processing a Dex `double-to-float' instruction.
2152 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2153 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002154 break;
2155
2156 default:
2157 LOG(FATAL) << "Unexpected type conversion from " << input_type
2158 << " to " << result_type;
2159 };
2160 break;
2161
Roland Levillaindff1f282014-11-05 14:15:05 +00002162 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002163 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002164 case Primitive::kPrimBoolean:
2165 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002166 case Primitive::kPrimByte:
2167 case Primitive::kPrimShort:
2168 case Primitive::kPrimInt:
2169 case Primitive::kPrimChar: {
2170 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002171 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002172 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2173 out.AsFpuRegisterPairLow<SRegister>());
2174 break;
2175 }
2176
Roland Levillain647b9ed2014-11-27 12:06:00 +00002177 case Primitive::kPrimLong: {
2178 // Processing a Dex `long-to-double' instruction.
2179 Register low = in.AsRegisterPairLow<Register>();
2180 Register high = in.AsRegisterPairHigh<Register>();
2181 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2182 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002183 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002184 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002185 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2186 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002187
Roland Levillain682393c2015-04-14 15:57:52 +01002188 // temp_d = int-to-double(high)
2189 __ vmovsr(temp_s, high);
2190 __ vcvtdi(temp_d, temp_s);
2191 // constant_d = k2Pow32EncodingForDouble
2192 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2193 // out_d = unsigned-to-double(low)
2194 __ vmovsr(out_s, low);
2195 __ vcvtdu(out_d, out_s);
2196 // out_d += temp_d * constant_d
2197 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002198 break;
2199 }
2200
Roland Levillaincff13742014-11-17 14:32:17 +00002201 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002202 // Processing a Dex `float-to-double' instruction.
2203 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2204 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002205 break;
2206
2207 default:
2208 LOG(FATAL) << "Unexpected type conversion from " << input_type
2209 << " to " << result_type;
2210 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002211 break;
2212
2213 default:
2214 LOG(FATAL) << "Unexpected type conversion from " << input_type
2215 << " to " << result_type;
2216 }
2217}
2218
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002219void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002220 LocationSummary* locations =
2221 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002222 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002223 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002224 locations->SetInAt(0, Location::RequiresRegister());
2225 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002226 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2227 break;
2228 }
2229
2230 case Primitive::kPrimLong: {
2231 locations->SetInAt(0, Location::RequiresRegister());
2232 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002234 break;
2235 }
2236
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002237 case Primitive::kPrimFloat:
2238 case Primitive::kPrimDouble: {
2239 locations->SetInAt(0, Location::RequiresFpuRegister());
2240 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002241 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002242 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002243 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002244
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002245 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002246 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002247 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002248}
2249
2250void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2251 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002252 Location out = locations->Out();
2253 Location first = locations->InAt(0);
2254 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002255 switch (add->GetResultType()) {
2256 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002257 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002258 __ add(out.AsRegister<Register>(),
2259 first.AsRegister<Register>(),
2260 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002261 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002262 __ AddConstant(out.AsRegister<Register>(),
2263 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002264 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002265 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002266 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002267
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002268 case Primitive::kPrimLong: {
2269 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002270 __ adds(out.AsRegisterPairLow<Register>(),
2271 first.AsRegisterPairLow<Register>(),
2272 ShifterOperand(second.AsRegisterPairLow<Register>()));
2273 __ adc(out.AsRegisterPairHigh<Register>(),
2274 first.AsRegisterPairHigh<Register>(),
2275 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002276 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002277 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002278
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002279 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002280 __ vadds(out.AsFpuRegister<SRegister>(),
2281 first.AsFpuRegister<SRegister>(),
2282 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002283 break;
2284
2285 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002286 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2287 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2288 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002289 break;
2290
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002291 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002292 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002293 }
2294}
2295
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002296void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002297 LocationSummary* locations =
2298 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002299 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002300 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2304 break;
2305 }
2306
2307 case Primitive::kPrimLong: {
2308 locations->SetInAt(0, Location::RequiresRegister());
2309 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002311 break;
2312 }
Calin Juravle11351682014-10-23 15:38:15 +01002313 case Primitive::kPrimFloat:
2314 case Primitive::kPrimDouble: {
2315 locations->SetInAt(0, Location::RequiresFpuRegister());
2316 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002317 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002318 break;
Calin Juravle11351682014-10-23 15:38:15 +01002319 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002320 default:
Calin Juravle11351682014-10-23 15:38:15 +01002321 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002322 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002323}
2324
2325void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2326 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002327 Location out = locations->Out();
2328 Location first = locations->InAt(0);
2329 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002330 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002331 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002332 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002333 __ sub(out.AsRegister<Register>(),
2334 first.AsRegister<Register>(),
2335 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002336 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ AddConstant(out.AsRegister<Register>(),
2338 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002339 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002340 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002341 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002342 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002343
Calin Juravle11351682014-10-23 15:38:15 +01002344 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002345 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002346 __ subs(out.AsRegisterPairLow<Register>(),
2347 first.AsRegisterPairLow<Register>(),
2348 ShifterOperand(second.AsRegisterPairLow<Register>()));
2349 __ sbc(out.AsRegisterPairHigh<Register>(),
2350 first.AsRegisterPairHigh<Register>(),
2351 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002352 break;
Calin Juravle11351682014-10-23 15:38:15 +01002353 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002354
Calin Juravle11351682014-10-23 15:38:15 +01002355 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002356 __ vsubs(out.AsFpuRegister<SRegister>(),
2357 first.AsFpuRegister<SRegister>(),
2358 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002359 break;
Calin Juravle11351682014-10-23 15:38:15 +01002360 }
2361
2362 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002363 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2364 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2365 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002366 break;
2367 }
2368
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002369
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002370 default:
Calin Juravle11351682014-10-23 15:38:15 +01002371 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002372 }
2373}
2374
Calin Juravle34bacdf2014-10-07 20:23:36 +01002375void LocationsBuilderARM::VisitMul(HMul* mul) {
2376 LocationSummary* locations =
2377 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2378 switch (mul->GetResultType()) {
2379 case Primitive::kPrimInt:
2380 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002381 locations->SetInAt(0, Location::RequiresRegister());
2382 locations->SetInAt(1, Location::RequiresRegister());
2383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002384 break;
2385 }
2386
Calin Juravleb5bfa962014-10-21 18:02:24 +01002387 case Primitive::kPrimFloat:
2388 case Primitive::kPrimDouble: {
2389 locations->SetInAt(0, Location::RequiresFpuRegister());
2390 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002391 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002392 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002393 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002394
2395 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002396 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002397 }
2398}
2399
2400void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2401 LocationSummary* locations = mul->GetLocations();
2402 Location out = locations->Out();
2403 Location first = locations->InAt(0);
2404 Location second = locations->InAt(1);
2405 switch (mul->GetResultType()) {
2406 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002407 __ mul(out.AsRegister<Register>(),
2408 first.AsRegister<Register>(),
2409 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002410 break;
2411 }
2412 case Primitive::kPrimLong: {
2413 Register out_hi = out.AsRegisterPairHigh<Register>();
2414 Register out_lo = out.AsRegisterPairLow<Register>();
2415 Register in1_hi = first.AsRegisterPairHigh<Register>();
2416 Register in1_lo = first.AsRegisterPairLow<Register>();
2417 Register in2_hi = second.AsRegisterPairHigh<Register>();
2418 Register in2_lo = second.AsRegisterPairLow<Register>();
2419
2420 // Extra checks to protect caused by the existence of R1_R2.
2421 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2422 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2423 DCHECK_NE(out_hi, in1_lo);
2424 DCHECK_NE(out_hi, in2_lo);
2425
2426 // input: in1 - 64 bits, in2 - 64 bits
2427 // output: out
2428 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2429 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2430 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2431
2432 // IP <- in1.lo * in2.hi
2433 __ mul(IP, in1_lo, in2_hi);
2434 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2435 __ mla(out_hi, in1_hi, in2_lo, IP);
2436 // out.lo <- (in1.lo * in2.lo)[31:0];
2437 __ umull(out_lo, IP, in1_lo, in2_lo);
2438 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2439 __ add(out_hi, out_hi, ShifterOperand(IP));
2440 break;
2441 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002442
2443 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002444 __ vmuls(out.AsFpuRegister<SRegister>(),
2445 first.AsFpuRegister<SRegister>(),
2446 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002447 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002448 }
2449
2450 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002451 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2452 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2453 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002454 break;
2455 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002456
2457 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002458 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002459 }
2460}
2461
Zheng Xuc6667102015-05-15 16:08:45 +08002462void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2463 DCHECK(instruction->IsDiv() || instruction->IsRem());
2464 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2465
2466 LocationSummary* locations = instruction->GetLocations();
2467 Location second = locations->InAt(1);
2468 DCHECK(second.IsConstant());
2469
2470 Register out = locations->Out().AsRegister<Register>();
2471 Register dividend = locations->InAt(0).AsRegister<Register>();
2472 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2473 DCHECK(imm == 1 || imm == -1);
2474
2475 if (instruction->IsRem()) {
2476 __ LoadImmediate(out, 0);
2477 } else {
2478 if (imm == 1) {
2479 __ Mov(out, dividend);
2480 } else {
2481 __ rsb(out, dividend, ShifterOperand(0));
2482 }
2483 }
2484}
2485
2486void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2487 DCHECK(instruction->IsDiv() || instruction->IsRem());
2488 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2489
2490 LocationSummary* locations = instruction->GetLocations();
2491 Location second = locations->InAt(1);
2492 DCHECK(second.IsConstant());
2493
2494 Register out = locations->Out().AsRegister<Register>();
2495 Register dividend = locations->InAt(0).AsRegister<Register>();
2496 Register temp = locations->GetTemp(0).AsRegister<Register>();
2497 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002498 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002499 DCHECK(IsPowerOfTwo(abs_imm));
2500 int ctz_imm = CTZ(abs_imm);
2501
2502 if (ctz_imm == 1) {
2503 __ Lsr(temp, dividend, 32 - ctz_imm);
2504 } else {
2505 __ Asr(temp, dividend, 31);
2506 __ Lsr(temp, temp, 32 - ctz_imm);
2507 }
2508 __ add(out, temp, ShifterOperand(dividend));
2509
2510 if (instruction->IsDiv()) {
2511 __ Asr(out, out, ctz_imm);
2512 if (imm < 0) {
2513 __ rsb(out, out, ShifterOperand(0));
2514 }
2515 } else {
2516 __ ubfx(out, out, 0, ctz_imm);
2517 __ sub(out, out, ShifterOperand(temp));
2518 }
2519}
2520
2521void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2522 DCHECK(instruction->IsDiv() || instruction->IsRem());
2523 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2524
2525 LocationSummary* locations = instruction->GetLocations();
2526 Location second = locations->InAt(1);
2527 DCHECK(second.IsConstant());
2528
2529 Register out = locations->Out().AsRegister<Register>();
2530 Register dividend = locations->InAt(0).AsRegister<Register>();
2531 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2532 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2533 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2534
2535 int64_t magic;
2536 int shift;
2537 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2538
2539 __ LoadImmediate(temp1, magic);
2540 __ smull(temp2, temp1, dividend, temp1);
2541
2542 if (imm > 0 && magic < 0) {
2543 __ add(temp1, temp1, ShifterOperand(dividend));
2544 } else if (imm < 0 && magic > 0) {
2545 __ sub(temp1, temp1, ShifterOperand(dividend));
2546 }
2547
2548 if (shift != 0) {
2549 __ Asr(temp1, temp1, shift);
2550 }
2551
2552 if (instruction->IsDiv()) {
2553 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2554 } else {
2555 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2556 // TODO: Strength reduction for mls.
2557 __ LoadImmediate(temp2, imm);
2558 __ mls(out, temp1, temp2, dividend);
2559 }
2560}
2561
2562void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2563 DCHECK(instruction->IsDiv() || instruction->IsRem());
2564 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2565
2566 LocationSummary* locations = instruction->GetLocations();
2567 Location second = locations->InAt(1);
2568 DCHECK(second.IsConstant());
2569
2570 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2571 if (imm == 0) {
2572 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2573 } else if (imm == 1 || imm == -1) {
2574 DivRemOneOrMinusOne(instruction);
2575 } else if (IsPowerOfTwo(std::abs(imm))) {
2576 DivRemByPowerOfTwo(instruction);
2577 } else {
2578 DCHECK(imm <= -2 || imm >= 2);
2579 GenerateDivRemWithAnyConstant(instruction);
2580 }
2581}
2582
Calin Juravle7c4954d2014-10-28 16:57:40 +00002583void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002584 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2585 if (div->GetResultType() == Primitive::kPrimLong) {
2586 // pLdiv runtime call.
2587 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002588 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2589 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002590 } else if (div->GetResultType() == Primitive::kPrimInt &&
2591 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2592 // pIdivmod runtime call.
2593 call_kind = LocationSummary::kCall;
2594 }
2595
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2597
Calin Juravle7c4954d2014-10-28 16:57:40 +00002598 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002599 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002600 if (div->InputAt(1)->IsConstant()) {
2601 locations->SetInAt(0, Location::RequiresRegister());
2602 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2603 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2604 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2605 if (abs_imm <= 1) {
2606 // No temp register required.
2607 } else {
2608 locations->AddTemp(Location::RequiresRegister());
2609 if (!IsPowerOfTwo(abs_imm)) {
2610 locations->AddTemp(Location::RequiresRegister());
2611 }
2612 }
2613 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002614 locations->SetInAt(0, Location::RequiresRegister());
2615 locations->SetInAt(1, Location::RequiresRegister());
2616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2617 } else {
2618 InvokeRuntimeCallingConvention calling_convention;
2619 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2620 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2621 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2622 // we only need the former.
2623 locations->SetOut(Location::RegisterLocation(R0));
2624 }
Calin Juravled0d48522014-11-04 16:40:20 +00002625 break;
2626 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002627 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002628 InvokeRuntimeCallingConvention calling_convention;
2629 locations->SetInAt(0, Location::RegisterPairLocation(
2630 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2631 locations->SetInAt(1, Location::RegisterPairLocation(
2632 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002633 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002634 break;
2635 }
2636 case Primitive::kPrimFloat:
2637 case Primitive::kPrimDouble: {
2638 locations->SetInAt(0, Location::RequiresFpuRegister());
2639 locations->SetInAt(1, Location::RequiresFpuRegister());
2640 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2641 break;
2642 }
2643
2644 default:
2645 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2646 }
2647}
2648
2649void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2650 LocationSummary* locations = div->GetLocations();
2651 Location out = locations->Out();
2652 Location first = locations->InAt(0);
2653 Location second = locations->InAt(1);
2654
2655 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002656 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002657 if (second.IsConstant()) {
2658 GenerateDivRemConstantIntegral(div);
2659 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002660 __ sdiv(out.AsRegister<Register>(),
2661 first.AsRegister<Register>(),
2662 second.AsRegister<Register>());
2663 } else {
2664 InvokeRuntimeCallingConvention calling_convention;
2665 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2666 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2667 DCHECK_EQ(R0, out.AsRegister<Register>());
2668
2669 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2670 }
Calin Juravled0d48522014-11-04 16:40:20 +00002671 break;
2672 }
2673
Calin Juravle7c4954d2014-10-28 16:57:40 +00002674 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002675 InvokeRuntimeCallingConvention calling_convention;
2676 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2677 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2678 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2679 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2680 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002681 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002682
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002683 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002684 break;
2685 }
2686
2687 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002688 __ vdivs(out.AsFpuRegister<SRegister>(),
2689 first.AsFpuRegister<SRegister>(),
2690 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002691 break;
2692 }
2693
2694 case Primitive::kPrimDouble: {
2695 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2696 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2697 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2698 break;
2699 }
2700
2701 default:
2702 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2703 }
2704}
2705
Calin Juravlebacfec32014-11-14 15:54:36 +00002706void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002707 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002708
2709 // Most remainders are implemented in the runtime.
2710 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002711 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2712 // sdiv will be replaced by other instruction sequence.
2713 call_kind = LocationSummary::kNoCall;
2714 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2715 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002716 // Have hardware divide instruction for int, do it with three instructions.
2717 call_kind = LocationSummary::kNoCall;
2718 }
2719
Calin Juravlebacfec32014-11-14 15:54:36 +00002720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2721
Calin Juravled2ec87d2014-12-08 14:24:46 +00002722 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002723 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002724 if (rem->InputAt(1)->IsConstant()) {
2725 locations->SetInAt(0, Location::RequiresRegister());
2726 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2728 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2729 if (abs_imm <= 1) {
2730 // No temp register required.
2731 } else {
2732 locations->AddTemp(Location::RequiresRegister());
2733 if (!IsPowerOfTwo(abs_imm)) {
2734 locations->AddTemp(Location::RequiresRegister());
2735 }
2736 }
2737 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002738 locations->SetInAt(0, Location::RequiresRegister());
2739 locations->SetInAt(1, Location::RequiresRegister());
2740 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2741 locations->AddTemp(Location::RequiresRegister());
2742 } else {
2743 InvokeRuntimeCallingConvention calling_convention;
2744 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2745 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2746 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2747 // we only need the latter.
2748 locations->SetOut(Location::RegisterLocation(R1));
2749 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002750 break;
2751 }
2752 case Primitive::kPrimLong: {
2753 InvokeRuntimeCallingConvention calling_convention;
2754 locations->SetInAt(0, Location::RegisterPairLocation(
2755 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2756 locations->SetInAt(1, Location::RegisterPairLocation(
2757 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2758 // The runtime helper puts the output in R2,R3.
2759 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2760 break;
2761 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002762 case Primitive::kPrimFloat: {
2763 InvokeRuntimeCallingConvention calling_convention;
2764 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2765 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2766 locations->SetOut(Location::FpuRegisterLocation(S0));
2767 break;
2768 }
2769
Calin Juravlebacfec32014-11-14 15:54:36 +00002770 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002771 InvokeRuntimeCallingConvention calling_convention;
2772 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2773 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2774 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2775 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2776 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002777 break;
2778 }
2779
2780 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002781 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002782 }
2783}
2784
2785void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2786 LocationSummary* locations = rem->GetLocations();
2787 Location out = locations->Out();
2788 Location first = locations->InAt(0);
2789 Location second = locations->InAt(1);
2790
Calin Juravled2ec87d2014-12-08 14:24:46 +00002791 Primitive::Type type = rem->GetResultType();
2792 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002793 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002794 if (second.IsConstant()) {
2795 GenerateDivRemConstantIntegral(rem);
2796 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002797 Register reg1 = first.AsRegister<Register>();
2798 Register reg2 = second.AsRegister<Register>();
2799 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002800
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002801 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002802 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002803 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002804 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002805 } else {
2806 InvokeRuntimeCallingConvention calling_convention;
2807 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2808 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2809 DCHECK_EQ(R1, out.AsRegister<Register>());
2810
2811 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2812 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002813 break;
2814 }
2815
2816 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002817 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002818 break;
2819 }
2820
Calin Juravled2ec87d2014-12-08 14:24:46 +00002821 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002822 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002823 break;
2824 }
2825
Calin Juravlebacfec32014-11-14 15:54:36 +00002826 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002827 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002828 break;
2829 }
2830
2831 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002832 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002833 }
2834}
2835
Calin Juravled0d48522014-11-04 16:40:20 +00002836void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002837 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2838 ? LocationSummary::kCallOnSlowPath
2839 : LocationSummary::kNoCall;
2840 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002841 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002842 if (instruction->HasUses()) {
2843 locations->SetOut(Location::SameAsFirstInput());
2844 }
2845}
2846
2847void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2848 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2849 codegen_->AddSlowPath(slow_path);
2850
2851 LocationSummary* locations = instruction->GetLocations();
2852 Location value = locations->InAt(0);
2853
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002854 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002855 case Primitive::kPrimByte:
2856 case Primitive::kPrimChar:
2857 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002858 case Primitive::kPrimInt: {
2859 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002860 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002861 } else {
2862 DCHECK(value.IsConstant()) << value;
2863 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2864 __ b(slow_path->GetEntryLabel());
2865 }
2866 }
2867 break;
2868 }
2869 case Primitive::kPrimLong: {
2870 if (value.IsRegisterPair()) {
2871 __ orrs(IP,
2872 value.AsRegisterPairLow<Register>(),
2873 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2874 __ b(slow_path->GetEntryLabel(), EQ);
2875 } else {
2876 DCHECK(value.IsConstant()) << value;
2877 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2878 __ b(slow_path->GetEntryLabel());
2879 }
2880 }
2881 break;
2882 default:
2883 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2884 }
2885 }
Calin Juravled0d48522014-11-04 16:40:20 +00002886}
2887
Calin Juravle9aec02f2014-11-18 23:06:35 +00002888void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2889 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2890
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002891 LocationSummary* locations =
2892 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002893
2894 switch (op->GetResultType()) {
2895 case Primitive::kPrimInt: {
2896 locations->SetInAt(0, Location::RequiresRegister());
2897 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002898 // Make the output overlap, as it will be used to hold the masked
2899 // second input.
2900 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002901 break;
2902 }
2903 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002904 locations->SetInAt(0, Location::RequiresRegister());
2905 locations->SetInAt(1, Location::RequiresRegister());
2906 locations->AddTemp(Location::RequiresRegister());
2907 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002908 break;
2909 }
2910 default:
2911 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2912 }
2913}
2914
2915void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2916 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2917
2918 LocationSummary* locations = op->GetLocations();
2919 Location out = locations->Out();
2920 Location first = locations->InAt(0);
2921 Location second = locations->InAt(1);
2922
2923 Primitive::Type type = op->GetResultType();
2924 switch (type) {
2925 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002926 Register out_reg = out.AsRegister<Register>();
2927 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002928 // Arm doesn't mask the shift count so we need to do it ourselves.
2929 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002930 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002931 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002932 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002933 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002934 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002935 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002936 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002937 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002938 }
2939 } else {
2940 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2941 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2942 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2943 __ Mov(out_reg, first_reg);
2944 } else if (op->IsShl()) {
2945 __ Lsl(out_reg, first_reg, shift_value);
2946 } else if (op->IsShr()) {
2947 __ Asr(out_reg, first_reg, shift_value);
2948 } else {
2949 __ Lsr(out_reg, first_reg, shift_value);
2950 }
2951 }
2952 break;
2953 }
2954 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002955 Register o_h = out.AsRegisterPairHigh<Register>();
2956 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002957
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002958 Register temp = locations->GetTemp(0).AsRegister<Register>();
2959
2960 Register high = first.AsRegisterPairHigh<Register>();
2961 Register low = first.AsRegisterPairLow<Register>();
2962
2963 Register second_reg = second.AsRegister<Register>();
2964
Calin Juravle9aec02f2014-11-18 23:06:35 +00002965 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002966 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002967 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002968 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002969 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002970 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002971 __ Lsr(temp, low, temp);
2972 __ orr(o_h, o_h, ShifterOperand(temp));
2973 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002974 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002975 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002976 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002977 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002978 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002979 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002980 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002981 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002982 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002983 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002984 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002985 __ Lsl(temp, high, temp);
2986 __ orr(o_l, o_l, ShifterOperand(temp));
2987 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002988 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002989 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002990 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002991 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002992 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002993 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002994 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002995 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002996 __ Lsr(o_l, low, o_h);
2997 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002998 __ Lsl(temp, high, temp);
2999 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003000 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003001 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003002 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003003 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003004 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003005 break;
3006 }
3007 default:
3008 LOG(FATAL) << "Unexpected operation type " << type;
3009 }
3010}
3011
3012void LocationsBuilderARM::VisitShl(HShl* shl) {
3013 HandleShift(shl);
3014}
3015
3016void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3017 HandleShift(shl);
3018}
3019
3020void LocationsBuilderARM::VisitShr(HShr* shr) {
3021 HandleShift(shr);
3022}
3023
3024void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3025 HandleShift(shr);
3026}
3027
3028void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3029 HandleShift(ushr);
3030}
3031
3032void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3033 HandleShift(ushr);
3034}
3035
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003036void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003037 LocationSummary* locations =
3038 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003039 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003040 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003041 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003042 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003043}
3044
3045void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3046 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003047 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003048 // Note: if heap poisoning is enabled, the entry point takes cares
3049 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003050 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003051 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003052 instruction->GetDexPc(),
3053 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003054}
3055
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003056void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3057 LocationSummary* locations =
3058 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3059 InvokeRuntimeCallingConvention calling_convention;
3060 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003061 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003062 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003063 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003064}
3065
3066void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3067 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003068 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003069 // Note: if heap poisoning is enabled, the entry point takes cares
3070 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003071 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003072 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003073 instruction->GetDexPc(),
3074 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003075}
3076
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003077void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003078 LocationSummary* locations =
3079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003080 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3081 if (location.IsStackSlot()) {
3082 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3083 } else if (location.IsDoubleStackSlot()) {
3084 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003085 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003086 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003087}
3088
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003089void InstructionCodeGeneratorARM::VisitParameterValue(
3090 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003091 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003092}
3093
3094void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3097 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3098}
3099
3100void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3101 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003102}
3103
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003104void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003105 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003106 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003107 locations->SetInAt(0, Location::RequiresRegister());
3108 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003109}
3110
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003111void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3112 LocationSummary* locations = not_->GetLocations();
3113 Location out = locations->Out();
3114 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003115 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003116 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003117 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003118 break;
3119
3120 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003121 __ mvn(out.AsRegisterPairLow<Register>(),
3122 ShifterOperand(in.AsRegisterPairLow<Register>()));
3123 __ mvn(out.AsRegisterPairHigh<Register>(),
3124 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003125 break;
3126
3127 default:
3128 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3129 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003130}
3131
David Brazdil66d126e2015-04-03 16:02:44 +01003132void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3133 LocationSummary* locations =
3134 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3135 locations->SetInAt(0, Location::RequiresRegister());
3136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3137}
3138
3139void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003140 LocationSummary* locations = bool_not->GetLocations();
3141 Location out = locations->Out();
3142 Location in = locations->InAt(0);
3143 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3144}
3145
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003146void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003147 LocationSummary* locations =
3148 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003149 switch (compare->InputAt(0)->GetType()) {
3150 case Primitive::kPrimLong: {
3151 locations->SetInAt(0, Location::RequiresRegister());
3152 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003153 // Output overlaps because it is written before doing the low comparison.
3154 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003155 break;
3156 }
3157 case Primitive::kPrimFloat:
3158 case Primitive::kPrimDouble: {
3159 locations->SetInAt(0, Location::RequiresFpuRegister());
3160 locations->SetInAt(1, Location::RequiresFpuRegister());
3161 locations->SetOut(Location::RequiresRegister());
3162 break;
3163 }
3164 default:
3165 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3166 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003167}
3168
3169void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003170 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003171 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003172 Location left = locations->InAt(0);
3173 Location right = locations->InAt(1);
3174
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003175 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003176 Primitive::Type type = compare->InputAt(0)->GetType();
3177 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003178 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003179 __ cmp(left.AsRegisterPairHigh<Register>(),
3180 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003181 __ b(&less, LT);
3182 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003183 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003184 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003185 __ cmp(left.AsRegisterPairLow<Register>(),
3186 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003187 break;
3188 }
3189 case Primitive::kPrimFloat:
3190 case Primitive::kPrimDouble: {
3191 __ LoadImmediate(out, 0);
3192 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003194 } else {
3195 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3196 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3197 }
3198 __ vmstat(); // transfer FP status register to ARM APSR.
3199 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003200 break;
3201 }
3202 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003203 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003204 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003205 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003206 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003207
3208 __ Bind(&greater);
3209 __ LoadImmediate(out, 1);
3210 __ b(&done);
3211
3212 __ Bind(&less);
3213 __ LoadImmediate(out, -1);
3214
3215 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003216}
3217
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003218void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003219 LocationSummary* locations =
3220 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003221 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3222 locations->SetInAt(i, Location::Any());
3223 }
3224 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003225}
3226
3227void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003228 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003229 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003230}
3231
Calin Juravle52c48962014-12-16 17:02:57 +00003232void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3233 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003234 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003235 switch (kind) {
3236 case MemBarrierKind::kAnyStore:
3237 case MemBarrierKind::kLoadAny:
3238 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003239 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003240 break;
3241 }
3242 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003243 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003244 break;
3245 }
3246 default:
3247 LOG(FATAL) << "Unexpected memory barrier " << kind;
3248 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003249 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003250}
3251
3252void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3253 uint32_t offset,
3254 Register out_lo,
3255 Register out_hi) {
3256 if (offset != 0) {
3257 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003258 __ add(IP, addr, ShifterOperand(out_lo));
3259 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003260 }
3261 __ ldrexd(out_lo, out_hi, addr);
3262}
3263
3264void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3265 uint32_t offset,
3266 Register value_lo,
3267 Register value_hi,
3268 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003269 Register temp2,
3270 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003271 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003272 if (offset != 0) {
3273 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003274 __ add(IP, addr, ShifterOperand(temp1));
3275 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003276 }
3277 __ Bind(&fail);
3278 // We need a load followed by store. (The address used in a STREX instruction must
3279 // be the same as the address in the most recently executed LDREX instruction.)
3280 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003281 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003282 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003283 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003284}
3285
3286void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3287 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3288
Nicolas Geoffray39468442014-09-02 15:17:15 +01003289 LocationSummary* locations =
3290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003291 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003292
Calin Juravle52c48962014-12-16 17:02:57 +00003293 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003294 if (Primitive::IsFloatingPointType(field_type)) {
3295 locations->SetInAt(1, Location::RequiresFpuRegister());
3296 } else {
3297 locations->SetInAt(1, Location::RequiresRegister());
3298 }
3299
Calin Juravle52c48962014-12-16 17:02:57 +00003300 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003301 bool generate_volatile = field_info.IsVolatile()
3302 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003303 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003304 bool needs_write_barrier =
3305 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003306 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003307 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003308 if (needs_write_barrier) {
3309 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003310 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003311 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003312 // Arm encoding have some additional constraints for ldrexd/strexd:
3313 // - registers need to be consecutive
3314 // - the first register should be even but not R14.
3315 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3316 // enable Arm encoding.
3317 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3318
3319 locations->AddTemp(Location::RequiresRegister());
3320 locations->AddTemp(Location::RequiresRegister());
3321 if (field_type == Primitive::kPrimDouble) {
3322 // For doubles we need two more registers to copy the value.
3323 locations->AddTemp(Location::RegisterLocation(R2));
3324 locations->AddTemp(Location::RegisterLocation(R3));
3325 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003326 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003327}
3328
Calin Juravle52c48962014-12-16 17:02:57 +00003329void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003330 const FieldInfo& field_info,
3331 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003332 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3333
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003334 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003335 Register base = locations->InAt(0).AsRegister<Register>();
3336 Location value = locations->InAt(1);
3337
3338 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003339 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003340 Primitive::Type field_type = field_info.GetFieldType();
3341 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003342 bool needs_write_barrier =
3343 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003344
3345 if (is_volatile) {
3346 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3347 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003348
3349 switch (field_type) {
3350 case Primitive::kPrimBoolean:
3351 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003352 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003353 break;
3354 }
3355
3356 case Primitive::kPrimShort:
3357 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003358 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003359 break;
3360 }
3361
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003362 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003363 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003364 if (kPoisonHeapReferences && needs_write_barrier) {
3365 // Note that in the case where `value` is a null reference,
3366 // we do not enter this block, as a null reference does not
3367 // need poisoning.
3368 DCHECK_EQ(field_type, Primitive::kPrimNot);
3369 Register temp = locations->GetTemp(0).AsRegister<Register>();
3370 __ Mov(temp, value.AsRegister<Register>());
3371 __ PoisonHeapReference(temp);
3372 __ StoreToOffset(kStoreWord, temp, base, offset);
3373 } else {
3374 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3375 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003376 break;
3377 }
3378
3379 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003380 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003381 GenerateWideAtomicStore(base, offset,
3382 value.AsRegisterPairLow<Register>(),
3383 value.AsRegisterPairHigh<Register>(),
3384 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003385 locations->GetTemp(1).AsRegister<Register>(),
3386 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003387 } else {
3388 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003389 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003390 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003391 break;
3392 }
3393
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003394 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003395 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003396 break;
3397 }
3398
3399 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003400 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003401 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003402 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3403 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3404
3405 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3406
3407 GenerateWideAtomicStore(base, offset,
3408 value_reg_lo,
3409 value_reg_hi,
3410 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003411 locations->GetTemp(3).AsRegister<Register>(),
3412 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003413 } else {
3414 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003415 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003416 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003417 break;
3418 }
3419
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420 case Primitive::kPrimVoid:
3421 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003422 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003423 }
Calin Juravle52c48962014-12-16 17:02:57 +00003424
Calin Juravle77520bc2015-01-12 18:45:46 +00003425 // Longs and doubles are handled in the switch.
3426 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3427 codegen_->MaybeRecordImplicitNullCheck(instruction);
3428 }
3429
3430 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3431 Register temp = locations->GetTemp(0).AsRegister<Register>();
3432 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003433 codegen_->MarkGCCard(
3434 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003435 }
3436
Calin Juravle52c48962014-12-16 17:02:57 +00003437 if (is_volatile) {
3438 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3439 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003440}
3441
Calin Juravle52c48962014-12-16 17:02:57 +00003442void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3443 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003444 LocationSummary* locations =
3445 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003446 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003447
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003448 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003449 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003450 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003451 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003452
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003453 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3454 locations->SetOut(Location::RequiresFpuRegister());
3455 } else {
3456 locations->SetOut(Location::RequiresRegister(),
3457 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3458 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003459 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003460 // Arm encoding have some additional constraints for ldrexd/strexd:
3461 // - registers need to be consecutive
3462 // - the first register should be even but not R14.
3463 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3464 // enable Arm encoding.
3465 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3466 locations->AddTemp(Location::RequiresRegister());
3467 locations->AddTemp(Location::RequiresRegister());
3468 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003469}
3470
Calin Juravle52c48962014-12-16 17:02:57 +00003471void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3472 const FieldInfo& field_info) {
3473 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003474
Calin Juravle52c48962014-12-16 17:02:57 +00003475 LocationSummary* locations = instruction->GetLocations();
3476 Register base = locations->InAt(0).AsRegister<Register>();
3477 Location out = locations->Out();
3478 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003479 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003480 Primitive::Type field_type = field_info.GetFieldType();
3481 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3482
3483 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003484 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003485 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003486 break;
3487 }
3488
3489 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003490 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003491 break;
3492 }
3493
3494 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003495 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003496 break;
3497 }
3498
3499 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003500 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003501 break;
3502 }
3503
3504 case Primitive::kPrimInt:
3505 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003506 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003507 break;
3508 }
3509
3510 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003511 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003512 GenerateWideAtomicLoad(base, offset,
3513 out.AsRegisterPairLow<Register>(),
3514 out.AsRegisterPairHigh<Register>());
3515 } else {
3516 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3517 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003518 break;
3519 }
3520
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003521 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003522 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003523 break;
3524 }
3525
3526 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003527 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003528 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003529 Register lo = locations->GetTemp(0).AsRegister<Register>();
3530 Register hi = locations->GetTemp(1).AsRegister<Register>();
3531 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003532 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003533 __ vmovdrr(out_reg, lo, hi);
3534 } else {
3535 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003536 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003537 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003538 break;
3539 }
3540
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003541 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003542 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003543 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544 }
Calin Juravle52c48962014-12-16 17:02:57 +00003545
Calin Juravle77520bc2015-01-12 18:45:46 +00003546 // Doubles are handled in the switch.
3547 if (field_type != Primitive::kPrimDouble) {
3548 codegen_->MaybeRecordImplicitNullCheck(instruction);
3549 }
3550
Calin Juravle52c48962014-12-16 17:02:57 +00003551 if (is_volatile) {
3552 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3553 }
Roland Levillain4d027112015-07-01 15:41:14 +01003554
3555 if (field_type == Primitive::kPrimNot) {
3556 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3557 }
Calin Juravle52c48962014-12-16 17:02:57 +00003558}
3559
3560void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3561 HandleFieldSet(instruction, instruction->GetFieldInfo());
3562}
3563
3564void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003565 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003566}
3567
3568void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3569 HandleFieldGet(instruction, instruction->GetFieldInfo());
3570}
3571
3572void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3573 HandleFieldGet(instruction, instruction->GetFieldInfo());
3574}
3575
3576void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3577 HandleFieldGet(instruction, instruction->GetFieldInfo());
3578}
3579
3580void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3581 HandleFieldGet(instruction, instruction->GetFieldInfo());
3582}
3583
3584void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3585 HandleFieldSet(instruction, instruction->GetFieldInfo());
3586}
3587
3588void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003589 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003590}
3591
Calin Juravle23a8e352015-09-08 19:56:31 +01003592void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
3593 HUnresolvedInstanceFieldGet* instruction) {
3594 FieldAccessCallingConvetionARM calling_convention;
3595 codegen_->CreateUnresolvedFieldLocationSummary(
3596 instruction, instruction->GetFieldType(), calling_convention);
3597}
3598
3599void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
3600 HUnresolvedInstanceFieldGet* instruction) {
3601 codegen_->GenerateUnresolvedFieldAccess(instruction,
3602 instruction->GetFieldType(),
3603 instruction->GetFieldIndex(),
3604 instruction->GetDexPc());
3605}
3606
3607void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
3608 HUnresolvedInstanceFieldSet* instruction) {
3609 FieldAccessCallingConvetionARM calling_convention;
3610 codegen_->CreateUnresolvedFieldLocationSummary(
3611 instruction, instruction->GetFieldType(), calling_convention);
3612}
3613
3614void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
3615 HUnresolvedInstanceFieldSet* instruction) {
3616 codegen_->GenerateUnresolvedFieldAccess(instruction,
3617 instruction->GetFieldType(),
3618 instruction->GetFieldIndex(),
3619 instruction->GetDexPc());
3620}
3621
3622void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
3623 HUnresolvedStaticFieldGet* instruction) {
3624 FieldAccessCallingConvetionARM calling_convention;
3625 codegen_->CreateUnresolvedFieldLocationSummary(
3626 instruction, instruction->GetFieldType(), calling_convention);
3627}
3628
3629void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
3630 HUnresolvedStaticFieldGet* instruction) {
3631 codegen_->GenerateUnresolvedFieldAccess(instruction,
3632 instruction->GetFieldType(),
3633 instruction->GetFieldIndex(),
3634 instruction->GetDexPc());
3635}
3636
3637void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
3638 HUnresolvedStaticFieldSet* instruction) {
3639 FieldAccessCallingConvetionARM calling_convention;
3640 codegen_->CreateUnresolvedFieldLocationSummary(
3641 instruction, instruction->GetFieldType(), calling_convention);
3642}
3643
3644void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
3645 HUnresolvedStaticFieldSet* instruction) {
3646 codegen_->GenerateUnresolvedFieldAccess(instruction,
3647 instruction->GetFieldType(),
3648 instruction->GetFieldIndex(),
3649 instruction->GetDexPc());
3650}
3651
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003652void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003653 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3654 ? LocationSummary::kCallOnSlowPath
3655 : LocationSummary::kNoCall;
3656 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003657 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003658 if (instruction->HasUses()) {
3659 locations->SetOut(Location::SameAsFirstInput());
3660 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003661}
3662
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003663void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003664 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3665 return;
3666 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003667 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003668
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003669 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3670 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3671}
3672
3673void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003674 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003675 codegen_->AddSlowPath(slow_path);
3676
3677 LocationSummary* locations = instruction->GetLocations();
3678 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003679
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003680 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003681}
3682
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003683void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003684 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003685 GenerateImplicitNullCheck(instruction);
3686 } else {
3687 GenerateExplicitNullCheck(instruction);
3688 }
3689}
3690
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003691void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003692 LocationSummary* locations =
3693 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003694 locations->SetInAt(0, Location::RequiresRegister());
3695 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003696 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3697 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3698 } else {
3699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3700 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003701}
3702
3703void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3704 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003705 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003706 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003707 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003708
Roland Levillain4d027112015-07-01 15:41:14 +01003709 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003710 case Primitive::kPrimBoolean: {
3711 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003712 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003713 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003714 size_t offset =
3715 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003716 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3717 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003718 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003719 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3720 }
3721 break;
3722 }
3723
3724 case Primitive::kPrimByte: {
3725 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003727 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003728 size_t offset =
3729 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003730 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3731 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003732 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003733 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3734 }
3735 break;
3736 }
3737
3738 case Primitive::kPrimShort: {
3739 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003740 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003741 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003742 size_t offset =
3743 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3745 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003747 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3748 }
3749 break;
3750 }
3751
3752 case Primitive::kPrimChar: {
3753 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003754 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003755 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003756 size_t offset =
3757 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003758 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3759 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003760 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003761 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3762 }
3763 break;
3764 }
3765
3766 case Primitive::kPrimInt:
3767 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003768 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3769 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003771 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003772 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003773 size_t offset =
3774 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003775 __ LoadFromOffset(kLoadWord, out, obj, offset);
3776 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003777 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3779 }
3780 break;
3781 }
3782
3783 case Primitive::kPrimLong: {
3784 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003785 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003787 size_t offset =
3788 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003789 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003790 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003791 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003792 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003793 }
3794 break;
3795 }
3796
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003797 case Primitive::kPrimFloat: {
3798 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3799 Location out = locations->Out();
3800 DCHECK(out.IsFpuRegister());
3801 if (index.IsConstant()) {
3802 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3803 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3804 } else {
3805 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3806 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3807 }
3808 break;
3809 }
3810
3811 case Primitive::kPrimDouble: {
3812 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3813 Location out = locations->Out();
3814 DCHECK(out.IsFpuRegisterPair());
3815 if (index.IsConstant()) {
3816 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3817 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3818 } else {
3819 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3820 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3821 }
3822 break;
3823 }
3824
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003825 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003826 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003827 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003828 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003829 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003830
3831 if (type == Primitive::kPrimNot) {
3832 Register out = locations->Out().AsRegister<Register>();
3833 __ MaybeUnpoisonHeapReference(out);
3834 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003835}
3836
3837void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003838 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003839
3840 bool needs_write_barrier =
3841 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3842 bool needs_runtime_call = instruction->NeedsTypeCheck();
3843
Nicolas Geoffray39468442014-09-02 15:17:15 +01003844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003845 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3846 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003847 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003848 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3849 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3850 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003851 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003852 locations->SetInAt(0, Location::RequiresRegister());
3853 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003854 if (Primitive::IsFloatingPointType(value_type)) {
3855 locations->SetInAt(2, Location::RequiresFpuRegister());
3856 } else {
3857 locations->SetInAt(2, Location::RequiresRegister());
3858 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003859
3860 if (needs_write_barrier) {
3861 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003862 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003863 locations->AddTemp(Location::RequiresRegister());
3864 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003866}
3867
3868void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3869 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003870 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003871 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003872 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003873 bool needs_runtime_call = locations->WillCall();
3874 bool needs_write_barrier =
3875 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003876
3877 switch (value_type) {
3878 case Primitive::kPrimBoolean:
3879 case Primitive::kPrimByte: {
3880 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003882 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003883 size_t offset =
3884 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003885 __ StoreToOffset(kStoreByte, value, obj, offset);
3886 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003887 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003888 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3889 }
3890 break;
3891 }
3892
3893 case Primitive::kPrimShort:
3894 case Primitive::kPrimChar: {
3895 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003896 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003897 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003898 size_t offset =
3899 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003900 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3901 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003902 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003903 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3904 }
3905 break;
3906 }
3907
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003908 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003909 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003910 if (!needs_runtime_call) {
3911 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003912 Register value = locations->InAt(2).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01003913 Register source = value;
3914 if (kPoisonHeapReferences && needs_write_barrier) {
3915 // Note that in the case where `value` is a null reference,
3916 // we do not enter this block, as a null reference does not
3917 // need poisoning.
3918 DCHECK_EQ(value_type, Primitive::kPrimNot);
3919 Register temp = locations->GetTemp(0).AsRegister<Register>();
3920 __ Mov(temp, value);
3921 __ PoisonHeapReference(temp);
3922 source = temp;
3923 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003924 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003925 size_t offset =
3926 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain4d027112015-07-01 15:41:14 +01003927 __ StoreToOffset(kStoreWord, source, obj, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003928 } else {
3929 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01003931 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003932 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003933 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003934 if (needs_write_barrier) {
3935 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003936 Register temp = locations->GetTemp(0).AsRegister<Register>();
3937 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003938 codegen_->MarkGCCard(temp, card, obj, value, instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003939 }
3940 } else {
3941 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01003942 // Note: if heap poisoning is enabled, pAputObject takes cares
3943 // of poisoning the reference.
Roland Levillain199f3362014-11-27 17:15:16 +00003944 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3945 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003946 instruction->GetDexPc(),
3947 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003948 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949 break;
3950 }
3951
3952 case Primitive::kPrimLong: {
3953 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003954 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003955 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003956 size_t offset =
3957 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003958 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003959 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003960 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003961 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003962 }
3963 break;
3964 }
3965
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003966 case Primitive::kPrimFloat: {
3967 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3968 Location value = locations->InAt(2);
3969 DCHECK(value.IsFpuRegister());
3970 if (index.IsConstant()) {
3971 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3972 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3973 } else {
3974 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3975 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3976 }
3977 break;
3978 }
3979
3980 case Primitive::kPrimDouble: {
3981 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3982 Location value = locations->InAt(2);
3983 DCHECK(value.IsFpuRegisterPair());
3984 if (index.IsConstant()) {
3985 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3986 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3987 } else {
3988 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3989 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3990 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003991
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003992 break;
3993 }
3994
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003995 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003996 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003997 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003998 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003999
4000 // Ints and objects are handled in the switch.
4001 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4002 codegen_->MaybeRecordImplicitNullCheck(instruction);
4003 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004004}
4005
4006void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004007 LocationSummary* locations =
4008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004009 locations->SetInAt(0, Location::RequiresRegister());
4010 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004011}
4012
4013void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4014 LocationSummary* locations = instruction->GetLocations();
4015 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004016 Register obj = locations->InAt(0).AsRegister<Register>();
4017 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004019 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004020}
4021
4022void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004023 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4024 ? LocationSummary::kCallOnSlowPath
4025 : LocationSummary::kNoCall;
4026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004027 locations->SetInAt(0, Location::RequiresRegister());
4028 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004029 if (instruction->HasUses()) {
4030 locations->SetOut(Location::SameAsFirstInput());
4031 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004032}
4033
4034void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4035 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004036 SlowPathCodeARM* slow_path =
4037 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004038 codegen_->AddSlowPath(slow_path);
4039
Roland Levillain271ab9c2014-11-27 15:23:57 +00004040 Register index = locations->InAt(0).AsRegister<Register>();
4041 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004042
4043 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004044 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004045}
4046
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004047void CodeGeneratorARM::MarkGCCard(Register temp,
4048 Register card,
4049 Register object,
4050 Register value,
4051 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004052 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004053 if (can_be_null) {
4054 __ CompareAndBranchIfZero(value, &is_null);
4055 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004056 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4057 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4058 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004059 if (can_be_null) {
4060 __ Bind(&is_null);
4061 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004062}
4063
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004064void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4065 temp->SetLocations(nullptr);
4066}
4067
4068void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
4069 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004070 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004071}
4072
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004073void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004074 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004075 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004076}
4077
4078void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004079 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4080}
4081
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004082void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4084}
4085
4086void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004087 HBasicBlock* block = instruction->GetBlock();
4088 if (block->GetLoopInformation() != nullptr) {
4089 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4090 // The back edge will generate the suspend check.
4091 return;
4092 }
4093 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4094 // The goto will generate the suspend check.
4095 return;
4096 }
4097 GenerateSuspendCheck(instruction, nullptr);
4098}
4099
4100void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4101 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004102 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004103 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4104 if (slow_path == nullptr) {
4105 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4106 instruction->SetSlowPath(slow_path);
4107 codegen_->AddSlowPath(slow_path);
4108 if (successor != nullptr) {
4109 DCHECK(successor->IsLoopHeader());
4110 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4111 }
4112 } else {
4113 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4114 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004115
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004116 __ LoadFromOffset(
4117 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004118 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004119 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004120 __ Bind(slow_path->GetReturnLabel());
4121 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004122 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004123 __ b(slow_path->GetEntryLabel());
4124 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004125}
4126
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004127ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4128 return codegen_->GetAssembler();
4129}
4130
4131void ParallelMoveResolverARM::EmitMove(size_t index) {
4132 MoveOperands* move = moves_.Get(index);
4133 Location source = move->GetSource();
4134 Location destination = move->GetDestination();
4135
4136 if (source.IsRegister()) {
4137 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004138 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004139 } else {
4140 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004141 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004142 SP, destination.GetStackIndex());
4143 }
4144 } else if (source.IsStackSlot()) {
4145 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004146 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004147 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004148 } else if (destination.IsFpuRegister()) {
4149 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004150 } else {
4151 DCHECK(destination.IsStackSlot());
4152 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4153 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4154 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004155 } else if (source.IsFpuRegister()) {
4156 if (destination.IsFpuRegister()) {
4157 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004158 } else {
4159 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004160 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4161 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004162 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004163 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004164 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4165 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004166 } else if (destination.IsRegisterPair()) {
4167 DCHECK(ExpectedPairLayout(destination));
4168 __ LoadFromOffset(
4169 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4170 } else {
4171 DCHECK(destination.IsFpuRegisterPair()) << destination;
4172 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4173 SP,
4174 source.GetStackIndex());
4175 }
4176 } else if (source.IsRegisterPair()) {
4177 if (destination.IsRegisterPair()) {
4178 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4179 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4180 } else {
4181 DCHECK(destination.IsDoubleStackSlot()) << destination;
4182 DCHECK(ExpectedPairLayout(source));
4183 __ StoreToOffset(
4184 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4185 }
4186 } else if (source.IsFpuRegisterPair()) {
4187 if (destination.IsFpuRegisterPair()) {
4188 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4189 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4190 } else {
4191 DCHECK(destination.IsDoubleStackSlot()) << destination;
4192 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4193 SP,
4194 destination.GetStackIndex());
4195 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004196 } else {
4197 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004198 HConstant* constant = source.GetConstant();
4199 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4200 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004201 if (destination.IsRegister()) {
4202 __ LoadImmediate(destination.AsRegister<Register>(), value);
4203 } else {
4204 DCHECK(destination.IsStackSlot());
4205 __ LoadImmediate(IP, value);
4206 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4207 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004208 } else if (constant->IsLongConstant()) {
4209 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004210 if (destination.IsRegisterPair()) {
4211 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4212 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004213 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004214 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004215 __ LoadImmediate(IP, Low32Bits(value));
4216 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4217 __ LoadImmediate(IP, High32Bits(value));
4218 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4219 }
4220 } else if (constant->IsDoubleConstant()) {
4221 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004222 if (destination.IsFpuRegisterPair()) {
4223 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004224 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004225 DCHECK(destination.IsDoubleStackSlot()) << destination;
4226 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004227 __ LoadImmediate(IP, Low32Bits(int_value));
4228 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4229 __ LoadImmediate(IP, High32Bits(int_value));
4230 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4231 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004232 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004233 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004234 float value = constant->AsFloatConstant()->GetValue();
4235 if (destination.IsFpuRegister()) {
4236 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4237 } else {
4238 DCHECK(destination.IsStackSlot());
4239 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4240 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4241 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004242 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004243 }
4244}
4245
4246void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4247 __ Mov(IP, reg);
4248 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4249 __ StoreToOffset(kStoreWord, IP, SP, mem);
4250}
4251
4252void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4253 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4254 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4255 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4256 SP, mem1 + stack_offset);
4257 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4258 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4259 SP, mem2 + stack_offset);
4260 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4261}
4262
4263void ParallelMoveResolverARM::EmitSwap(size_t index) {
4264 MoveOperands* move = moves_.Get(index);
4265 Location source = move->GetSource();
4266 Location destination = move->GetDestination();
4267
4268 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004269 DCHECK_NE(source.AsRegister<Register>(), IP);
4270 DCHECK_NE(destination.AsRegister<Register>(), IP);
4271 __ Mov(IP, source.AsRegister<Register>());
4272 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4273 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004274 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004275 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004276 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004277 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004278 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4279 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004280 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004281 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004282 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004283 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004284 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004285 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004286 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004287 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004288 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4289 destination.AsRegisterPairHigh<Register>(),
4290 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004291 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004292 Register low_reg = source.IsRegisterPair()
4293 ? source.AsRegisterPairLow<Register>()
4294 : destination.AsRegisterPairLow<Register>();
4295 int mem = source.IsRegisterPair()
4296 ? destination.GetStackIndex()
4297 : source.GetStackIndex();
4298 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004299 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004300 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004301 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004302 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004303 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4304 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004305 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004306 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004307 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004308 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4309 DRegister reg = source.IsFpuRegisterPair()
4310 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4311 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4312 int mem = source.IsFpuRegisterPair()
4313 ? destination.GetStackIndex()
4314 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004315 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004316 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004317 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004318 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4319 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4320 : destination.AsFpuRegister<SRegister>();
4321 int mem = source.IsFpuRegister()
4322 ? destination.GetStackIndex()
4323 : source.GetStackIndex();
4324
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004325 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004326 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004327 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004328 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004329 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4330 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004331 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004332 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004333 }
4334}
4335
4336void ParallelMoveResolverARM::SpillScratch(int reg) {
4337 __ Push(static_cast<Register>(reg));
4338}
4339
4340void ParallelMoveResolverARM::RestoreScratch(int reg) {
4341 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004342}
4343
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004344void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004345 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4346 ? LocationSummary::kCallOnSlowPath
4347 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004348 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004349 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004350 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004351 locations->SetOut(Location::RequiresRegister());
4352}
4353
4354void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004355 LocationSummary* locations = cls->GetLocations();
4356 Register out = locations->Out().AsRegister<Register>();
4357 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004358 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004359 DCHECK(!cls->CanCallRuntime());
4360 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004361 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004362 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004363 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004364 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004365 __ LoadFromOffset(kLoadWord,
4366 out,
4367 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004368 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004369 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004370 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004371
4372 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
4373 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4374 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004375 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004376 if (cls->MustGenerateClinitCheck()) {
4377 GenerateClassInitializationCheck(slow_path, out);
4378 } else {
4379 __ Bind(slow_path->GetExitLabel());
4380 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004381 }
4382}
4383
4384void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4385 LocationSummary* locations =
4386 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4387 locations->SetInAt(0, Location::RequiresRegister());
4388 if (check->HasUses()) {
4389 locations->SetOut(Location::SameAsFirstInput());
4390 }
4391}
4392
4393void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004394 // We assume the class is not null.
4395 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
4396 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004397 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004398 GenerateClassInitializationCheck(slow_path,
4399 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004400}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004401
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004402void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
4403 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004404 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4405 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4406 __ b(slow_path->GetEntryLabel(), LT);
4407 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4408 // properly. Therefore, we do a memory fence.
4409 __ dmb(ISH);
4410 __ Bind(slow_path->GetExitLabel());
4411}
4412
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004413void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4414 LocationSummary* locations =
4415 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004416 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004417 locations->SetOut(Location::RequiresRegister());
4418}
4419
4420void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
4421 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
4422 codegen_->AddSlowPath(slow_path);
4423
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004424 LocationSummary* locations = load->GetLocations();
4425 Register out = locations->Out().AsRegister<Register>();
4426 Register current_method = locations->InAt(0).AsRegister<Register>();
4427 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004428 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004429 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004430 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004431 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004432 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004433 __ Bind(slow_path->GetExitLabel());
4434}
4435
David Brazdilcb1c0552015-08-04 16:22:25 +01004436static int32_t GetExceptionTlsOffset() {
4437 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4438}
4439
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004440void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4441 LocationSummary* locations =
4442 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4443 locations->SetOut(Location::RequiresRegister());
4444}
4445
4446void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004448 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4449}
4450
4451void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4452 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4453}
4454
4455void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004456 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004457 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004458}
4459
4460void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4461 LocationSummary* locations =
4462 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4463 InvokeRuntimeCallingConvention calling_convention;
4464 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4465}
4466
4467void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4468 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004469 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004470}
4471
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004472void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004473 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4474 ? LocationSummary::kNoCall
4475 : LocationSummary::kCallOnSlowPath;
4476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4477 locations->SetInAt(0, Location::RequiresRegister());
4478 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004479 // The out register is used as a temporary, so it overlaps with the inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004480 // Note that TypeCheckSlowPathARM uses this register too.
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004481 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004482}
4483
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004484void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004485 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004486 Register obj = locations->InAt(0).AsRegister<Register>();
4487 Register cls = locations->InAt(1).AsRegister<Register>();
4488 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004489 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004490 Label done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004491 SlowPathCodeARM* slow_path = nullptr;
4492
4493 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004494 // avoid null check if we know obj is not null.
4495 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004496 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004497 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004498 // Compare the class of `obj` with `cls`.
4499 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004500 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004501 __ cmp(out, ShifterOperand(cls));
4502 if (instruction->IsClassFinal()) {
4503 // Classes must be equal for the instanceof to succeed.
4504 __ b(&zero, NE);
4505 __ LoadImmediate(out, 1);
4506 __ b(&done);
4507 } else {
4508 // If the classes are not equal, we go into a slow path.
4509 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004510 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004511 codegen_->AddSlowPath(slow_path);
4512 __ b(slow_path->GetEntryLabel(), NE);
4513 __ LoadImmediate(out, 1);
4514 __ b(&done);
4515 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004516
4517 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4518 __ Bind(&zero);
4519 __ LoadImmediate(out, 0);
4520 }
4521
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004522 if (slow_path != nullptr) {
4523 __ Bind(slow_path->GetExitLabel());
4524 }
4525 __ Bind(&done);
4526}
4527
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004528void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
4529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4530 instruction, LocationSummary::kCallOnSlowPath);
4531 locations->SetInAt(0, Location::RequiresRegister());
4532 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004533 // Note that TypeCheckSlowPathARM uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004534 locations->AddTemp(Location::RequiresRegister());
4535}
4536
4537void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4538 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004539 Register obj = locations->InAt(0).AsRegister<Register>();
4540 Register cls = locations->InAt(1).AsRegister<Register>();
4541 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004542 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4543
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004544 SlowPathCodeARM* slow_path =
4545 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004546 codegen_->AddSlowPath(slow_path);
4547
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004548 // avoid null check if we know obj is not null.
4549 if (instruction->MustDoNullCheck()) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004550 __ CompareAndBranchIfZero(obj, slow_path->GetExitLabel());
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004551 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004552 // Compare the class of `obj` with `cls`.
4553 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004554 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004555 __ cmp(temp, ShifterOperand(cls));
Roland Levillain4d027112015-07-01 15:41:14 +01004556 // The checkcast succeeds if the classes are equal (fast path).
4557 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004558 __ b(slow_path->GetEntryLabel(), NE);
4559 __ Bind(slow_path->GetExitLabel());
4560}
4561
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004562void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4563 LocationSummary* locations =
4564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4565 InvokeRuntimeCallingConvention calling_convention;
4566 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4567}
4568
4569void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4570 codegen_->InvokeRuntime(instruction->IsEnter()
4571 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4572 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004573 instruction->GetDexPc(),
4574 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004575}
4576
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004577void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4578void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4579void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4580
4581void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4582 LocationSummary* locations =
4583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4584 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4585 || instruction->GetResultType() == Primitive::kPrimLong);
4586 locations->SetInAt(0, Location::RequiresRegister());
4587 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004589}
4590
4591void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4592 HandleBitwiseOperation(instruction);
4593}
4594
4595void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4596 HandleBitwiseOperation(instruction);
4597}
4598
4599void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4600 HandleBitwiseOperation(instruction);
4601}
4602
4603void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4604 LocationSummary* locations = instruction->GetLocations();
4605
4606 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004607 Register first = locations->InAt(0).AsRegister<Register>();
4608 Register second = locations->InAt(1).AsRegister<Register>();
4609 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004610 if (instruction->IsAnd()) {
4611 __ and_(out, first, ShifterOperand(second));
4612 } else if (instruction->IsOr()) {
4613 __ orr(out, first, ShifterOperand(second));
4614 } else {
4615 DCHECK(instruction->IsXor());
4616 __ eor(out, first, ShifterOperand(second));
4617 }
4618 } else {
4619 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4620 Location first = locations->InAt(0);
4621 Location second = locations->InAt(1);
4622 Location out = locations->Out();
4623 if (instruction->IsAnd()) {
4624 __ and_(out.AsRegisterPairLow<Register>(),
4625 first.AsRegisterPairLow<Register>(),
4626 ShifterOperand(second.AsRegisterPairLow<Register>()));
4627 __ and_(out.AsRegisterPairHigh<Register>(),
4628 first.AsRegisterPairHigh<Register>(),
4629 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4630 } else if (instruction->IsOr()) {
4631 __ orr(out.AsRegisterPairLow<Register>(),
4632 first.AsRegisterPairLow<Register>(),
4633 ShifterOperand(second.AsRegisterPairLow<Register>()));
4634 __ orr(out.AsRegisterPairHigh<Register>(),
4635 first.AsRegisterPairHigh<Register>(),
4636 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4637 } else {
4638 DCHECK(instruction->IsXor());
4639 __ eor(out.AsRegisterPairLow<Register>(),
4640 first.AsRegisterPairLow<Register>(),
4641 ShifterOperand(second.AsRegisterPairLow<Register>()));
4642 __ eor(out.AsRegisterPairHigh<Register>(),
4643 first.AsRegisterPairHigh<Register>(),
4644 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4645 }
4646 }
4647}
4648
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004649void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004650 // For better instruction scheduling we load the direct code pointer before the method pointer.
4651 bool direct_code_loaded = false;
4652 switch (invoke->GetCodePtrLocation()) {
4653 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4654 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
4655 break;
4656 }
4657 // Calls across dex files are more likely to exceed the available BL range,
4658 // so use absolute patch by falling through to kDirectCodeFixup.
4659 FALLTHROUGH_INTENDED;
4660 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4661 // LR = code address from literal pool with link-time patch.
4662 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4663 direct_code_loaded = true;
4664 break;
4665 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4666 // LR = invoke->GetDirectCodePtr();
4667 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
4668 direct_code_loaded = true;
4669 break;
4670 default:
4671 break;
4672 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004673
Vladimir Marko58155012015-08-19 12:49:41 +00004674 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4675 switch (invoke->GetMethodLoadKind()) {
4676 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4677 // temp = thread->string_init_entrypoint
4678 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
4679 break;
4680 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
4681 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4682 break;
4683 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4684 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4685 break;
4686 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4687 __ LoadLiteral(temp.AsRegister<Register>(),
4688 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4689 break;
4690 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
4691 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
4692 FALLTHROUGH_INTENDED;
4693 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
4694 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4695 Register method_reg;
4696 Register reg = temp.AsRegister<Register>();
4697 if (current_method.IsRegister()) {
4698 method_reg = current_method.AsRegister<Register>();
4699 } else {
4700 DCHECK(invoke->GetLocations()->Intrinsified());
4701 DCHECK(!current_method.IsValid());
4702 method_reg = reg;
4703 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
4704 }
4705 // temp = current_method->dex_cache_resolved_methods_;
4706 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01004707 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
4708 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00004709 // temp = temp[index_in_cache]
4710 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4711 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
4712 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01004713 }
Vladimir Marko58155012015-08-19 12:49:41 +00004714 }
4715
4716 switch (invoke->GetCodePtrLocation()) {
4717 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4718 __ bl(GetFrameEntryLabel());
4719 break;
4720 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4721 if (!direct_code_loaded) {
4722 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4723 __ Bind(&relative_call_patches_.back().label);
4724 Label label;
4725 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
4726 __ Bind(&label);
4727 break;
4728 }
4729 // If we loaded the direct code above, fall through.
4730 FALLTHROUGH_INTENDED;
4731 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4732 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4733 // LR prepared above for better instruction scheduling.
4734 DCHECK(direct_code_loaded);
4735 // LR()
4736 __ blx(LR);
4737 break;
4738 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4739 // LR = callee_method->entry_point_from_quick_compiled_code_
4740 __ LoadFromOffset(
4741 kLoadWord, LR, callee_method.AsRegister<Register>(),
4742 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
4743 // LR()
4744 __ blx(LR);
4745 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004746 }
4747
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004748 DCHECK(!IsLeafMethod());
4749}
4750
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004751void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
4752 Register temp = temp_location.AsRegister<Register>();
4753 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4754 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
4755 LocationSummary* locations = invoke->GetLocations();
4756 Location receiver = locations->InAt(0);
4757 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4758 // temp = object->GetClass();
4759 DCHECK(receiver.IsRegister());
4760 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4761 MaybeRecordImplicitNullCheck(invoke);
4762 __ MaybeUnpoisonHeapReference(temp);
4763 // temp = temp->GetMethodAt(method_offset);
4764 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4765 kArmWordSize).Int32Value();
4766 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4767 // LR = temp->GetEntryPoint();
4768 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
4769 // LR();
4770 __ blx(LR);
4771}
4772
Vladimir Marko58155012015-08-19 12:49:41 +00004773void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4774 DCHECK(linker_patches->empty());
4775 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
4776 linker_patches->reserve(size);
4777 for (const auto& entry : method_patches_) {
4778 const MethodReference& target_method = entry.first;
4779 Literal* literal = entry.second;
4780 DCHECK(literal->GetLabel()->IsBound());
4781 uint32_t literal_offset = literal->GetLabel()->Position();
4782 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4783 target_method.dex_file,
4784 target_method.dex_method_index));
4785 }
4786 for (const auto& entry : call_patches_) {
4787 const MethodReference& target_method = entry.first;
4788 Literal* literal = entry.second;
4789 DCHECK(literal->GetLabel()->IsBound());
4790 uint32_t literal_offset = literal->GetLabel()->Position();
4791 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
4792 target_method.dex_file,
4793 target_method.dex_method_index));
4794 }
4795 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
4796 uint32_t literal_offset = info.label.Position();
4797 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4798 info.target_method.dex_file,
4799 info.target_method.dex_method_index));
4800 }
4801}
4802
4803Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
4804 MethodToLiteralMap* map) {
4805 // Look up the literal for target_method.
4806 auto lb = map->lower_bound(target_method);
4807 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
4808 return lb->second;
4809 }
4810 // We don't have a literal for this method yet, insert a new one.
4811 Literal* literal = __ NewLiteral<uint32_t>(0u);
4812 map->PutBefore(lb, target_method, literal);
4813 return literal;
4814}
4815
4816Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
4817 return DeduplicateMethodLiteral(target_method, &method_patches_);
4818}
4819
4820Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
4821 return DeduplicateMethodLiteral(target_method, &call_patches_);
4822}
4823
Calin Juravleb1498f62015-02-16 13:13:29 +00004824void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
4825 // Nothing to do, this should be removed during prepare for register allocator.
4826 UNUSED(instruction);
4827 LOG(FATAL) << "Unreachable";
4828}
4829
4830void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
4831 // Nothing to do, this should be removed during prepare for register allocator.
4832 UNUSED(instruction);
4833 LOG(FATAL) << "Unreachable";
4834}
4835
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004836void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
4837 DCHECK(codegen_->IsBaseline());
4838 LocationSummary* locations =
4839 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4840 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4841}
4842
4843void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4844 DCHECK(codegen_->IsBaseline());
4845 // Will be generated at use site.
4846}
4847
Roland Levillain4d027112015-07-01 15:41:14 +01004848#undef __
4849#undef QUICK_ENTRY_POINT
4850
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004851} // namespace arm
4852} // namespace art