blob: b3e38f0946e8464bab360a69c2a54c4d22862123 [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 Brazdilb022fa12015-08-20 17:47:48 +010069 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 Brazdilb022fa12015-08-20 17:47:48 +010093 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 Brazdilb022fa12015-08-20 17:47:48 +0100161 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,
405 const CompilerOptions& compiler_options)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000406 : CodeGenerator(graph,
407 kNumberOfCoreRegisters,
408 kNumberOfSRegisters,
409 kNumberOfRegisterPairs,
410 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
411 arraysize(kCoreCalleeSaves)),
412 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
413 arraysize(kFpuCalleeSaves)),
414 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100415 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100416 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100417 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100418 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000419 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000420 isa_features_(isa_features),
421 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
422 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
423 relative_call_patches_(graph->GetArena()->Adapter()) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700424 // Always save the LR register to mimic Quick.
425 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100426}
427
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000428void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
429 // Ensure that we fix up branches and literal loads and emit the literal pool.
430 __ FinalizeCode();
431
432 // Adjust native pc offsets in stack maps.
433 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
434 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
435 uint32_t new_position = __ GetAdjustedPosition(old_position);
436 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
437 }
438 // Adjust native pc offsets of block labels.
439 for (size_t block_idx = 0u, end = block_order_->Size(); block_idx != end; ++block_idx) {
440 HBasicBlock* block = block_order_->Get(block_idx);
441 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
442 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
443 DCHECK_LT(static_cast<size_t>(block->GetBlockId()), block_labels_.Size());
444 Label* block_label = &block_labels_.GetRawStorage()[block->GetBlockId()];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000445 DCHECK_EQ(block_label->IsBound(), !block->IsSingleJump());
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000446 if (block_label->IsBound()) {
447 __ AdjustLabelPosition(block_label);
448 }
449 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100450 // Adjust pc offsets for the disassembly information.
451 if (disasm_info_ != nullptr) {
452 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
453 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
454 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
455 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
456 it.second.start = __ GetAdjustedPosition(it.second.start);
457 it.second.end = __ GetAdjustedPosition(it.second.end);
458 }
459 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
460 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
461 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
462 }
463 }
Vladimir Marko58155012015-08-19 12:49:41 +0000464 // Adjust pc offsets for relative call patches.
465 for (MethodPatchInfo<Label>& info : relative_call_patches_) {
466 __ AdjustLabelPosition(&info.label);
467 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000468
469 CodeGenerator::Finalize(allocator);
470}
471
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100472Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100473 switch (type) {
474 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100475 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100476 ArmManagedRegister pair =
477 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100478 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
479 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
480
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100481 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
482 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100483 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100484 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100485 }
486
487 case Primitive::kPrimByte:
488 case Primitive::kPrimBoolean:
489 case Primitive::kPrimChar:
490 case Primitive::kPrimShort:
491 case Primitive::kPrimInt:
492 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100493 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100494 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100495 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
496 ArmManagedRegister current =
497 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
498 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100499 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100500 }
501 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100502 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503 }
504
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000505 case Primitive::kPrimFloat: {
506 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100507 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100508 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100509
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000510 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000511 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
512 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000513 return Location::FpuRegisterPairLocation(reg, reg + 1);
514 }
515
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100516 case Primitive::kPrimVoid:
517 LOG(FATAL) << "Unreachable type " << type;
518 }
519
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100520 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100521}
522
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000523void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100524 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100525 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100526
527 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100528 blocked_core_registers_[SP] = true;
529 blocked_core_registers_[LR] = true;
530 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100531
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100532 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100533 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100534
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100535 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100536 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100537
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000538 if (is_baseline) {
539 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
540 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
541 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000542
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000543 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
544
545 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
546 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
547 }
548 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100549
550 UpdateBlockedPairRegisters();
551}
552
553void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
554 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
555 ArmManagedRegister current =
556 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
557 if (blocked_core_registers_[current.AsRegisterPairLow()]
558 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
559 blocked_register_pairs_[i] = true;
560 }
561 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100562}
563
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100564InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
565 : HGraphVisitor(graph),
566 assembler_(codegen->GetAssembler()),
567 codegen_(codegen) {}
568
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000569void CodeGeneratorARM::ComputeSpillMask() {
570 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000571 // Save one extra register for baseline. Note that on thumb2, there is no easy
572 // instruction to restore just the PC, so this actually helps both baseline
573 // and non-baseline to save and restore at least two registers at entry and exit.
574 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000575 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
576 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
577 // We use vpush and vpop for saving and restoring floating point registers, which take
578 // a SRegister and the number of registers to save/restore after that SRegister. We
579 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
580 // but in the range.
581 if (fpu_spill_mask_ != 0) {
582 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
583 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
584 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
585 fpu_spill_mask_ |= (1 << i);
586 }
587 }
588}
589
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100590static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100591 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100592}
593
594static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100595 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100596}
597
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000598void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000599 bool skip_overflow_check =
600 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000601 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000602 __ Bind(&frame_entry_label_);
603
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000604 if (HasEmptyFrame()) {
605 return;
606 }
607
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100608 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000609 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
610 __ LoadFromOffset(kLoadWord, IP, IP, 0);
611 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100612 }
613
Andreas Gampe501fd632015-09-10 16:11:06 -0700614 __ PushList(core_spill_mask_);
615 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
616 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000617 if (fpu_spill_mask_ != 0) {
618 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
619 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100620 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100621 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000622 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100623 int adjust = GetFrameSize() - FrameEntrySpillSize();
624 __ AddConstant(SP, -adjust);
625 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100626 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000627}
628
629void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000630 if (HasEmptyFrame()) {
631 __ bx(LR);
632 return;
633 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100634 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100635 int adjust = GetFrameSize() - FrameEntrySpillSize();
636 __ AddConstant(SP, adjust);
637 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000638 if (fpu_spill_mask_ != 0) {
639 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
640 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100641 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
642 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000643 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700644 // Pop LR into PC to return.
645 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
646 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
647 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100648 __ cfi().RestoreState();
649 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000650}
651
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100652void CodeGeneratorARM::Bind(HBasicBlock* block) {
653 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000654}
655
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100656Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
657 switch (load->GetType()) {
658 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100659 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100660 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100661
662 case Primitive::kPrimInt:
663 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100664 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100665 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100666
667 case Primitive::kPrimBoolean:
668 case Primitive::kPrimByte:
669 case Primitive::kPrimChar:
670 case Primitive::kPrimShort:
671 case Primitive::kPrimVoid:
672 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700673 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100674 }
675
676 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700677 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100678}
679
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100680Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100681 switch (type) {
682 case Primitive::kPrimBoolean:
683 case Primitive::kPrimByte:
684 case Primitive::kPrimChar:
685 case Primitive::kPrimShort:
686 case Primitive::kPrimInt:
687 case Primitive::kPrimNot: {
688 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000689 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100690 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100691 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100692 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000693 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100694 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100695 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100696
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000697 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100698 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000699 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100700 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000701 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100702 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000703 if (calling_convention.GetRegisterAt(index) == R1) {
704 // Skip R1, and use R2_R3 instead.
705 gp_index_++;
706 index++;
707 }
708 }
709 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
710 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000711 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000712 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000713 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100714 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000715 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
716 }
717 }
718
719 case Primitive::kPrimFloat: {
720 uint32_t stack_index = stack_index_++;
721 if (float_index_ % 2 == 0) {
722 float_index_ = std::max(double_index_, float_index_);
723 }
724 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
725 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
726 } else {
727 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
728 }
729 }
730
731 case Primitive::kPrimDouble: {
732 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
733 uint32_t stack_index = stack_index_;
734 stack_index_ += 2;
735 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
736 uint32_t index = double_index_;
737 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000738 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000739 calling_convention.GetFpuRegisterAt(index),
740 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000741 DCHECK(ExpectedPairLayout(result));
742 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000743 } else {
744 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100745 }
746 }
747
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100748 case Primitive::kPrimVoid:
749 LOG(FATAL) << "Unexpected parameter type " << type;
750 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100751 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100752 return Location();
753}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100754
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100755Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000756 switch (type) {
757 case Primitive::kPrimBoolean:
758 case Primitive::kPrimByte:
759 case Primitive::kPrimChar:
760 case Primitive::kPrimShort:
761 case Primitive::kPrimInt:
762 case Primitive::kPrimNot: {
763 return Location::RegisterLocation(R0);
764 }
765
766 case Primitive::kPrimFloat: {
767 return Location::FpuRegisterLocation(S0);
768 }
769
770 case Primitive::kPrimLong: {
771 return Location::RegisterPairLocation(R0, R1);
772 }
773
774 case Primitive::kPrimDouble: {
775 return Location::FpuRegisterPairLocation(S0, S1);
776 }
777
778 case Primitive::kPrimVoid:
779 return Location();
780 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100781
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000782 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000783}
784
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100785Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
786 return Location::RegisterLocation(kMethodRegisterArgument);
787}
788
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789void CodeGeneratorARM::Move32(Location destination, Location source) {
790 if (source.Equals(destination)) {
791 return;
792 }
793 if (destination.IsRegister()) {
794 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000795 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000799 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100800 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 } else if (destination.IsFpuRegister()) {
802 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000803 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100804 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000805 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100806 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000807 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100808 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100809 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000810 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000812 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100813 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000814 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100815 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000816 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100817 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
818 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100819 }
820 }
821}
822
823void CodeGeneratorARM::Move64(Location destination, Location source) {
824 if (source.Equals(destination)) {
825 return;
826 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100827 if (destination.IsRegisterPair()) {
828 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000829 EmitParallelMoves(
830 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
831 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100832 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000833 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100834 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
835 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100836 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000837 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100838 } else {
839 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000840 DCHECK(ExpectedPairLayout(destination));
841 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
842 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100843 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000844 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100845 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000846 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
847 SP,
848 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100849 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000850 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100851 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100852 } else {
853 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100854 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000855 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100856 if (source.AsRegisterPairLow<Register>() == R1) {
857 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100858 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
859 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100860 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100861 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100862 SP, destination.GetStackIndex());
863 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000864 } else if (source.IsFpuRegisterPair()) {
865 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
866 SP,
867 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100868 } else {
869 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000870 EmitParallelMoves(
871 Location::StackSlot(source.GetStackIndex()),
872 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100873 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000874 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100875 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
876 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100877 }
878 }
879}
880
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100881void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100882 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100883 if (instruction->IsCurrentMethod()) {
884 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
885 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100886 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100887 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000888 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000889 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
890 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000891 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000892 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000893 } else {
894 DCHECK(location.IsStackSlot());
895 __ LoadImmediate(IP, value);
896 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
897 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000898 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000899 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000900 int64_t value = const_to_move->AsLongConstant()->GetValue();
901 if (location.IsRegisterPair()) {
902 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
903 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
904 } else {
905 DCHECK(location.IsDoubleStackSlot());
906 __ LoadImmediate(IP, Low32Bits(value));
907 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
908 __ LoadImmediate(IP, High32Bits(value));
909 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
910 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100911 }
Roland Levillain476df552014-10-09 17:51:36 +0100912 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
914 switch (instruction->GetType()) {
915 case Primitive::kPrimBoolean:
916 case Primitive::kPrimByte:
917 case Primitive::kPrimChar:
918 case Primitive::kPrimShort:
919 case Primitive::kPrimInt:
920 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100922 Move32(location, Location::StackSlot(stack_slot));
923 break;
924
925 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100926 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100927 Move64(location, Location::DoubleStackSlot(stack_slot));
928 break;
929
930 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100931 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100932 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000933 } else if (instruction->IsTemporary()) {
934 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000935 if (temp_location.IsStackSlot()) {
936 Move32(location, temp_location);
937 } else {
938 DCHECK(temp_location.IsDoubleStackSlot());
939 Move64(location, temp_location);
940 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000941 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100942 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943 switch (instruction->GetType()) {
944 case Primitive::kPrimBoolean:
945 case Primitive::kPrimByte:
946 case Primitive::kPrimChar:
947 case Primitive::kPrimShort:
948 case Primitive::kPrimNot:
949 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100950 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100951 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100952 break;
953
954 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100955 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100956 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957 break;
958
959 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100960 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100961 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000962 }
963}
964
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100965void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
966 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000967 uint32_t dex_pc,
968 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100969 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100970 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
971 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000972 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100973}
974
David Brazdilfc6a86a2015-06-26 10:33:45 +0000975void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100976 DCHECK(!successor->IsExitBlock());
977
978 HBasicBlock* block = got->GetBlock();
979 HInstruction* previous = got->GetPrevious();
980
981 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000982 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100983 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
984 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
985 return;
986 }
987
988 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
989 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
990 }
991 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000992 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000993 }
994}
995
David Brazdilfc6a86a2015-06-26 10:33:45 +0000996void LocationsBuilderARM::VisitGoto(HGoto* got) {
997 got->SetLocations(nullptr);
998}
999
1000void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1001 HandleGoto(got, got->GetSuccessor());
1002}
1003
1004void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1005 try_boundary->SetLocations(nullptr);
1006}
1007
1008void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1009 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1010 if (!successor->IsExitBlock()) {
1011 HandleGoto(try_boundary, successor);
1012 }
1013}
1014
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001015void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001016 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001017}
1018
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001019void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001020 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001021}
1022
Roland Levillain4fa13f62015-07-06 18:11:54 +01001023void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1024 ShifterOperand operand;
1025 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1026 __ cmp(left, operand);
1027 } else {
1028 Register temp = IP;
1029 __ LoadImmediate(temp, right);
1030 __ cmp(left, ShifterOperand(temp));
1031 }
1032}
1033
1034void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1035 Label* true_label,
1036 Label* false_label) {
1037 __ vmstat(); // transfer FP status register to ARM APSR.
1038 if (cond->IsFPConditionTrueIfNaN()) {
1039 __ b(true_label, VS); // VS for unordered.
1040 } else if (cond->IsFPConditionFalseIfNaN()) {
1041 __ b(false_label, VS); // VS for unordered.
1042 }
1043 __ b(true_label, ARMSignedOrFPCondition(cond->GetCondition()));
1044}
1045
1046void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1047 Label* true_label,
1048 Label* false_label) {
1049 LocationSummary* locations = cond->GetLocations();
1050 Location left = locations->InAt(0);
1051 Location right = locations->InAt(1);
1052 IfCondition if_cond = cond->GetCondition();
1053
1054 Register left_high = left.AsRegisterPairHigh<Register>();
1055 Register left_low = left.AsRegisterPairLow<Register>();
1056 IfCondition true_high_cond = if_cond;
1057 IfCondition false_high_cond = cond->GetOppositeCondition();
1058 Condition final_condition = ARMUnsignedCondition(if_cond);
1059
1060 // Set the conditions for the test, remembering that == needs to be
1061 // decided using the low words.
1062 switch (if_cond) {
1063 case kCondEQ:
1064 case kCondNE:
1065 // Nothing to do.
1066 break;
1067 case kCondLT:
1068 false_high_cond = kCondGT;
1069 break;
1070 case kCondLE:
1071 true_high_cond = kCondLT;
1072 break;
1073 case kCondGT:
1074 false_high_cond = kCondLT;
1075 break;
1076 case kCondGE:
1077 true_high_cond = kCondGT;
1078 break;
1079 }
1080 if (right.IsConstant()) {
1081 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1082 int32_t val_low = Low32Bits(value);
1083 int32_t val_high = High32Bits(value);
1084
1085 GenerateCompareWithImmediate(left_high, val_high);
1086 if (if_cond == kCondNE) {
1087 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1088 } else if (if_cond == kCondEQ) {
1089 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1090 } else {
1091 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1092 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1093 }
1094 // Must be equal high, so compare the lows.
1095 GenerateCompareWithImmediate(left_low, val_low);
1096 } else {
1097 Register right_high = right.AsRegisterPairHigh<Register>();
1098 Register right_low = right.AsRegisterPairLow<Register>();
1099
1100 __ cmp(left_high, ShifterOperand(right_high));
1101 if (if_cond == kCondNE) {
1102 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1103 } else if (if_cond == kCondEQ) {
1104 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1105 } else {
1106 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1107 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1108 }
1109 // Must be equal high, so compare the lows.
1110 __ cmp(left_low, ShifterOperand(right_low));
1111 }
1112 // The last comparison might be unsigned.
1113 __ b(true_label, final_condition);
1114}
1115
1116void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1117 HCondition* condition,
1118 Label* true_target,
1119 Label* false_target,
1120 Label* always_true_target) {
1121 LocationSummary* locations = condition->GetLocations();
1122 Location left = locations->InAt(0);
1123 Location right = locations->InAt(1);
1124
1125 // We don't want true_target as a nullptr.
1126 if (true_target == nullptr) {
1127 true_target = always_true_target;
1128 }
1129 bool falls_through = (false_target == nullptr);
1130
1131 // FP compares don't like null false_targets.
1132 if (false_target == nullptr) {
1133 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1134 }
1135
1136 Primitive::Type type = condition->InputAt(0)->GetType();
1137 switch (type) {
1138 case Primitive::kPrimLong:
1139 GenerateLongComparesAndJumps(condition, true_target, false_target);
1140 break;
1141 case Primitive::kPrimFloat:
1142 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1143 GenerateFPJumps(condition, true_target, false_target);
1144 break;
1145 case Primitive::kPrimDouble:
1146 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1147 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1148 GenerateFPJumps(condition, true_target, false_target);
1149 break;
1150 default:
1151 LOG(FATAL) << "Unexpected compare type " << type;
1152 }
1153
1154 if (!falls_through) {
1155 __ b(false_target);
1156 }
1157}
1158
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001159void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1160 Label* true_target,
1161 Label* false_target,
1162 Label* always_true_target) {
1163 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001164 if (cond->IsIntConstant()) {
1165 // Constant condition, statically compared against 1.
1166 int32_t cond_value = cond->AsIntConstant()->GetValue();
1167 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001168 if (always_true_target != nullptr) {
1169 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001170 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001171 return;
1172 } else {
1173 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001174 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001175 } else {
1176 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1177 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001178 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001179 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1180 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001181 } else {
1182 // Condition has not been materialized, use its inputs as the
1183 // comparison and its condition as the branch condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001184 Primitive::Type type =
1185 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
1186 // Is this a long or FP comparison that has been folded into the HCondition?
1187 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1188 // Generate the comparison directly.
1189 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1190 true_target, false_target, always_true_target);
1191 return;
1192 }
1193
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001194 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001195 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001196 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001197 Location right = locations->InAt(1);
1198 if (right.IsRegister()) {
1199 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001200 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001201 DCHECK(right.IsConstant());
1202 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001203 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001204 __ b(true_target, ARMSignedOrFPCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001205 }
Dave Allison20dfc792014-06-16 20:44:29 -07001206 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001207 if (false_target != nullptr) {
1208 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001209 }
1210}
1211
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001212void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1213 LocationSummary* locations =
1214 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1215 HInstruction* cond = if_instr->InputAt(0);
1216 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1217 locations->SetInAt(0, Location::RequiresRegister());
1218 }
1219}
1220
1221void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1222 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1223 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1224 Label* always_true_target = true_target;
1225 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1226 if_instr->IfTrueSuccessor())) {
1227 always_true_target = nullptr;
1228 }
1229 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1230 if_instr->IfFalseSuccessor())) {
1231 false_target = nullptr;
1232 }
1233 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1234}
1235
1236void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1237 LocationSummary* locations = new (GetGraph()->GetArena())
1238 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1239 HInstruction* cond = deoptimize->InputAt(0);
1240 DCHECK(cond->IsCondition());
1241 if (cond->AsCondition()->NeedsMaterialization()) {
1242 locations->SetInAt(0, Location::RequiresRegister());
1243 }
1244}
1245
1246void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1247 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
1248 DeoptimizationSlowPathARM(deoptimize);
1249 codegen_->AddSlowPath(slow_path);
1250 Label* slow_path_entry = slow_path->GetEntryLabel();
1251 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1252}
Dave Allison20dfc792014-06-16 20:44:29 -07001253
Roland Levillain0d37cd02015-05-27 16:39:19 +01001254void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001255 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001256 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001257 // Handle the long/FP comparisons made in instruction simplification.
1258 switch (cond->InputAt(0)->GetType()) {
1259 case Primitive::kPrimLong:
1260 locations->SetInAt(0, Location::RequiresRegister());
1261 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1262 if (cond->NeedsMaterialization()) {
1263 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1264 }
1265 break;
1266
1267 case Primitive::kPrimFloat:
1268 case Primitive::kPrimDouble:
1269 locations->SetInAt(0, Location::RequiresFpuRegister());
1270 locations->SetInAt(1, Location::RequiresFpuRegister());
1271 if (cond->NeedsMaterialization()) {
1272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1273 }
1274 break;
1275
1276 default:
1277 locations->SetInAt(0, Location::RequiresRegister());
1278 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1279 if (cond->NeedsMaterialization()) {
1280 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1281 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001282 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001283}
1284
Roland Levillain0d37cd02015-05-27 16:39:19 +01001285void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001286 if (!cond->NeedsMaterialization()) {
1287 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001288 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001289
1290 LocationSummary* locations = cond->GetLocations();
1291 Location left = locations->InAt(0);
1292 Location right = locations->InAt(1);
1293 Register out = locations->Out().AsRegister<Register>();
1294 Label true_label, false_label;
1295
1296 switch (cond->InputAt(0)->GetType()) {
1297 default: {
1298 // Integer case.
1299 if (right.IsRegister()) {
1300 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1301 } else {
1302 DCHECK(right.IsConstant());
1303 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1304 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1305 }
1306 __ it(ARMSignedOrFPCondition(cond->GetCondition()), kItElse);
1307 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
1308 ARMSignedOrFPCondition(cond->GetCondition()));
1309 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
1310 ARMSignedOrFPCondition(cond->GetOppositeCondition()));
1311 return;
1312 }
1313 case Primitive::kPrimLong:
1314 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1315 break;
1316 case Primitive::kPrimFloat:
1317 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1318 GenerateFPJumps(cond, &true_label, &false_label);
1319 break;
1320 case Primitive::kPrimDouble:
1321 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1322 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1323 GenerateFPJumps(cond, &true_label, &false_label);
1324 break;
1325 }
1326
1327 // Convert the jumps into the result.
1328 Label done_label;
1329
1330 // False case: result = 0.
1331 __ Bind(&false_label);
1332 __ LoadImmediate(out, 0);
1333 __ b(&done_label);
1334
1335 // True case: result = 1.
1336 __ Bind(&true_label);
1337 __ LoadImmediate(out, 1);
1338 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001339}
1340
1341void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1342 VisitCondition(comp);
1343}
1344
1345void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1346 VisitCondition(comp);
1347}
1348
1349void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1350 VisitCondition(comp);
1351}
1352
1353void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1354 VisitCondition(comp);
1355}
1356
1357void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1358 VisitCondition(comp);
1359}
1360
1361void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1362 VisitCondition(comp);
1363}
1364
1365void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1366 VisitCondition(comp);
1367}
1368
1369void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1370 VisitCondition(comp);
1371}
1372
1373void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1374 VisitCondition(comp);
1375}
1376
1377void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1378 VisitCondition(comp);
1379}
1380
1381void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1382 VisitCondition(comp);
1383}
1384
1385void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1386 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001387}
1388
1389void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001390 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391}
1392
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001393void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1394 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001395}
1396
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001397void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001398 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001399}
1400
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001401void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001402 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001403 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001404}
1405
1406void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001407 LocationSummary* locations =
1408 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001409 switch (store->InputAt(1)->GetType()) {
1410 case Primitive::kPrimBoolean:
1411 case Primitive::kPrimByte:
1412 case Primitive::kPrimChar:
1413 case Primitive::kPrimShort:
1414 case Primitive::kPrimInt:
1415 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001416 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001417 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1418 break;
1419
1420 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001421 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001422 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1423 break;
1424
1425 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001426 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001427 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001428}
1429
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001430void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001431 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001432}
1433
1434void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001435 LocationSummary* locations =
1436 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001437 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001438}
1439
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001440void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001441 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001442 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001443}
1444
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001445void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1446 LocationSummary* locations =
1447 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1448 locations->SetOut(Location::ConstantLocation(constant));
1449}
1450
1451void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1452 // Will be generated at use site.
1453 UNUSED(constant);
1454}
1455
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001456void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001457 LocationSummary* locations =
1458 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001459 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001460}
1461
1462void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1463 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001464 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001465}
1466
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001467void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1468 LocationSummary* locations =
1469 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1470 locations->SetOut(Location::ConstantLocation(constant));
1471}
1472
1473void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1474 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001475 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001476}
1477
1478void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1479 LocationSummary* locations =
1480 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1481 locations->SetOut(Location::ConstantLocation(constant));
1482}
1483
1484void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1485 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001486 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001487}
1488
Calin Juravle27df7582015-04-17 19:12:31 +01001489void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1490 memory_barrier->SetLocations(nullptr);
1491}
1492
1493void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1494 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1495}
1496
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001497void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001498 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001499}
1500
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001501void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001502 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001503 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001504}
1505
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001506void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001507 LocationSummary* locations =
1508 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001509 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001510}
1511
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001512void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001513 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001514 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001515}
1516
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001517void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001518 // When we do not run baseline, explicit clinit checks triggered by static
1519 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1520 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001521
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001522 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1523 codegen_->GetInstructionSetFeatures());
1524 if (intrinsic.TryDispatch(invoke)) {
1525 return;
1526 }
1527
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001528 HandleInvoke(invoke);
1529}
1530
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001531static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1532 if (invoke->GetLocations()->Intrinsified()) {
1533 IntrinsicCodeGeneratorARM intrinsic(codegen);
1534 intrinsic.Dispatch(invoke);
1535 return true;
1536 }
1537 return false;
1538}
1539
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001540void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001541 // When we do not run baseline, explicit clinit checks triggered by static
1542 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1543 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001544
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001545 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1546 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001547 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001548
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001549 LocationSummary* locations = invoke->GetLocations();
1550 codegen_->GenerateStaticOrDirectCall(
1551 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001552 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001553}
1554
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001555void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001556 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001557 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001558}
1559
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001560void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001561 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1562 codegen_->GetInstructionSetFeatures());
1563 if (intrinsic.TryDispatch(invoke)) {
1564 return;
1565 }
1566
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001567 HandleInvoke(invoke);
1568}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001569
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001570void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001571 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1572 return;
1573 }
1574
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001575 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001576 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001577 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001578}
1579
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001580void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1581 HandleInvoke(invoke);
1582 // Add the hidden argument.
1583 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1584}
1585
1586void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1587 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001588 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001589 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1590 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001591 LocationSummary* locations = invoke->GetLocations();
1592 Location receiver = locations->InAt(0);
1593 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1594
1595 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001596 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1597 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001598
1599 // temp = object->GetClass();
1600 if (receiver.IsStackSlot()) {
1601 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1602 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1603 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001604 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001605 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001606 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001607 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001608 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001609 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001610 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001611 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1612 // LR = temp->GetEntryPoint();
1613 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1614 // LR();
1615 __ blx(LR);
1616 DCHECK(!codegen_->IsLeafMethod());
1617 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1618}
1619
Roland Levillain88cb1752014-10-20 16:36:47 +01001620void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1621 LocationSummary* locations =
1622 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1623 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001624 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001625 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1627 break;
1628 }
1629 case Primitive::kPrimLong: {
1630 locations->SetInAt(0, Location::RequiresRegister());
1631 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001632 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001633 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001634
Roland Levillain88cb1752014-10-20 16:36:47 +01001635 case Primitive::kPrimFloat:
1636 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001637 locations->SetInAt(0, Location::RequiresFpuRegister());
1638 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001639 break;
1640
1641 default:
1642 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1643 }
1644}
1645
1646void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1647 LocationSummary* locations = neg->GetLocations();
1648 Location out = locations->Out();
1649 Location in = locations->InAt(0);
1650 switch (neg->GetResultType()) {
1651 case Primitive::kPrimInt:
1652 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001653 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001654 break;
1655
1656 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001657 DCHECK(in.IsRegisterPair());
1658 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1659 __ rsbs(out.AsRegisterPairLow<Register>(),
1660 in.AsRegisterPairLow<Register>(),
1661 ShifterOperand(0));
1662 // We cannot emit an RSC (Reverse Subtract with Carry)
1663 // instruction here, as it does not exist in the Thumb-2
1664 // instruction set. We use the following approach
1665 // using SBC and SUB instead.
1666 //
1667 // out.hi = -C
1668 __ sbc(out.AsRegisterPairHigh<Register>(),
1669 out.AsRegisterPairHigh<Register>(),
1670 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1671 // out.hi = out.hi - in.hi
1672 __ sub(out.AsRegisterPairHigh<Register>(),
1673 out.AsRegisterPairHigh<Register>(),
1674 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1675 break;
1676
Roland Levillain88cb1752014-10-20 16:36:47 +01001677 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001678 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001680 break;
1681
Roland Levillain88cb1752014-10-20 16:36:47 +01001682 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001683 DCHECK(in.IsFpuRegisterPair());
1684 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1685 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001686 break;
1687
1688 default:
1689 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1690 }
1691}
1692
Roland Levillaindff1f282014-11-05 14:15:05 +00001693void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001694 Primitive::Type result_type = conversion->GetResultType();
1695 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001696 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001697
Roland Levillain5b3ee562015-04-14 16:02:41 +01001698 // The float-to-long, double-to-long and long-to-float type conversions
1699 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001700 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001701 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1702 && result_type == Primitive::kPrimLong)
1703 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001704 ? LocationSummary::kCall
1705 : LocationSummary::kNoCall;
1706 LocationSummary* locations =
1707 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1708
David Brazdilb2bd1c52015-03-25 11:17:37 +00001709 // The Java language does not allow treating boolean as an integral type but
1710 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001711
Roland Levillaindff1f282014-11-05 14:15:05 +00001712 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001713 case Primitive::kPrimByte:
1714 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001715 case Primitive::kPrimBoolean:
1716 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001717 case Primitive::kPrimShort:
1718 case Primitive::kPrimInt:
1719 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001720 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001721 locations->SetInAt(0, Location::RequiresRegister());
1722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1723 break;
1724
1725 default:
1726 LOG(FATAL) << "Unexpected type conversion from " << input_type
1727 << " to " << result_type;
1728 }
1729 break;
1730
Roland Levillain01a8d712014-11-14 16:27:39 +00001731 case Primitive::kPrimShort:
1732 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001733 case Primitive::kPrimBoolean:
1734 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001735 case Primitive::kPrimByte:
1736 case Primitive::kPrimInt:
1737 case Primitive::kPrimChar:
1738 // Processing a Dex `int-to-short' instruction.
1739 locations->SetInAt(0, Location::RequiresRegister());
1740 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1741 break;
1742
1743 default:
1744 LOG(FATAL) << "Unexpected type conversion from " << input_type
1745 << " to " << result_type;
1746 }
1747 break;
1748
Roland Levillain946e1432014-11-11 17:35:19 +00001749 case Primitive::kPrimInt:
1750 switch (input_type) {
1751 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001752 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001753 locations->SetInAt(0, Location::Any());
1754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1755 break;
1756
1757 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001758 // Processing a Dex `float-to-int' instruction.
1759 locations->SetInAt(0, Location::RequiresFpuRegister());
1760 locations->SetOut(Location::RequiresRegister());
1761 locations->AddTemp(Location::RequiresFpuRegister());
1762 break;
1763
Roland Levillain946e1432014-11-11 17:35:19 +00001764 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001765 // Processing a Dex `double-to-int' instruction.
1766 locations->SetInAt(0, Location::RequiresFpuRegister());
1767 locations->SetOut(Location::RequiresRegister());
1768 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected type conversion from " << input_type
1773 << " to " << result_type;
1774 }
1775 break;
1776
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 case Primitive::kPrimLong:
1778 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001779 case Primitive::kPrimBoolean:
1780 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001781 case Primitive::kPrimByte:
1782 case Primitive::kPrimShort:
1783 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001784 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001785 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001786 locations->SetInAt(0, Location::RequiresRegister());
1787 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1788 break;
1789
Roland Levillain624279f2014-12-04 11:54:28 +00001790 case Primitive::kPrimFloat: {
1791 // Processing a Dex `float-to-long' instruction.
1792 InvokeRuntimeCallingConvention calling_convention;
1793 locations->SetInAt(0, Location::FpuRegisterLocation(
1794 calling_convention.GetFpuRegisterAt(0)));
1795 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1796 break;
1797 }
1798
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001799 case Primitive::kPrimDouble: {
1800 // Processing a Dex `double-to-long' instruction.
1801 InvokeRuntimeCallingConvention calling_convention;
1802 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1803 calling_convention.GetFpuRegisterAt(0),
1804 calling_convention.GetFpuRegisterAt(1)));
1805 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001806 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001807 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001808
1809 default:
1810 LOG(FATAL) << "Unexpected type conversion from " << input_type
1811 << " to " << result_type;
1812 }
1813 break;
1814
Roland Levillain981e4542014-11-14 11:47:14 +00001815 case Primitive::kPrimChar:
1816 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001817 case Primitive::kPrimBoolean:
1818 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001819 case Primitive::kPrimByte:
1820 case Primitive::kPrimShort:
1821 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001822 // Processing a Dex `int-to-char' instruction.
1823 locations->SetInAt(0, Location::RequiresRegister());
1824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1825 break;
1826
1827 default:
1828 LOG(FATAL) << "Unexpected type conversion from " << input_type
1829 << " to " << result_type;
1830 }
1831 break;
1832
Roland Levillaindff1f282014-11-05 14:15:05 +00001833 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001834 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001835 case Primitive::kPrimBoolean:
1836 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001837 case Primitive::kPrimByte:
1838 case Primitive::kPrimShort:
1839 case Primitive::kPrimInt:
1840 case Primitive::kPrimChar:
1841 // Processing a Dex `int-to-float' instruction.
1842 locations->SetInAt(0, Location::RequiresRegister());
1843 locations->SetOut(Location::RequiresFpuRegister());
1844 break;
1845
Roland Levillain5b3ee562015-04-14 16:02:41 +01001846 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00001847 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001848 InvokeRuntimeCallingConvention calling_convention;
1849 locations->SetInAt(0, Location::RegisterPairLocation(
1850 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1851 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001852 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01001853 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001854
Roland Levillaincff13742014-11-17 14:32:17 +00001855 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001856 // Processing a Dex `double-to-float' instruction.
1857 locations->SetInAt(0, Location::RequiresFpuRegister());
1858 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001859 break;
1860
1861 default:
1862 LOG(FATAL) << "Unexpected type conversion from " << input_type
1863 << " to " << result_type;
1864 };
1865 break;
1866
Roland Levillaindff1f282014-11-05 14:15:05 +00001867 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001868 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001869 case Primitive::kPrimBoolean:
1870 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001871 case Primitive::kPrimByte:
1872 case Primitive::kPrimShort:
1873 case Primitive::kPrimInt:
1874 case Primitive::kPrimChar:
1875 // Processing a Dex `int-to-double' instruction.
1876 locations->SetInAt(0, Location::RequiresRegister());
1877 locations->SetOut(Location::RequiresFpuRegister());
1878 break;
1879
1880 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001881 // Processing a Dex `long-to-double' instruction.
1882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01001884 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001885 locations->AddTemp(Location::RequiresFpuRegister());
1886 break;
1887
Roland Levillaincff13742014-11-17 14:32:17 +00001888 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001889 // Processing a Dex `float-to-double' instruction.
1890 locations->SetInAt(0, Location::RequiresFpuRegister());
1891 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001892 break;
1893
1894 default:
1895 LOG(FATAL) << "Unexpected type conversion from " << input_type
1896 << " to " << result_type;
1897 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001898 break;
1899
1900 default:
1901 LOG(FATAL) << "Unexpected type conversion from " << input_type
1902 << " to " << result_type;
1903 }
1904}
1905
1906void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1907 LocationSummary* locations = conversion->GetLocations();
1908 Location out = locations->Out();
1909 Location in = locations->InAt(0);
1910 Primitive::Type result_type = conversion->GetResultType();
1911 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001912 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001913 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001914 case Primitive::kPrimByte:
1915 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001916 case Primitive::kPrimBoolean:
1917 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001918 case Primitive::kPrimShort:
1919 case Primitive::kPrimInt:
1920 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001921 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001923 break;
1924
1925 default:
1926 LOG(FATAL) << "Unexpected type conversion from " << input_type
1927 << " to " << result_type;
1928 }
1929 break;
1930
Roland Levillain01a8d712014-11-14 16:27:39 +00001931 case Primitive::kPrimShort:
1932 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001933 case Primitive::kPrimBoolean:
1934 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001935 case Primitive::kPrimByte:
1936 case Primitive::kPrimInt:
1937 case Primitive::kPrimChar:
1938 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001939 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001940 break;
1941
1942 default:
1943 LOG(FATAL) << "Unexpected type conversion from " << input_type
1944 << " to " << result_type;
1945 }
1946 break;
1947
Roland Levillain946e1432014-11-11 17:35:19 +00001948 case Primitive::kPrimInt:
1949 switch (input_type) {
1950 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001951 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001952 DCHECK(out.IsRegister());
1953 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001954 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001955 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001956 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001957 } else {
1958 DCHECK(in.IsConstant());
1959 DCHECK(in.GetConstant()->IsLongConstant());
1960 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001961 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001962 }
1963 break;
1964
Roland Levillain3f8f9362014-12-02 17:45:01 +00001965 case Primitive::kPrimFloat: {
1966 // Processing a Dex `float-to-int' instruction.
1967 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1968 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1969 __ vcvtis(temp, temp);
1970 __ vmovrs(out.AsRegister<Register>(), temp);
1971 break;
1972 }
1973
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001974 case Primitive::kPrimDouble: {
1975 // Processing a Dex `double-to-int' instruction.
1976 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1977 DRegister temp_d = FromLowSToD(temp_s);
1978 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1979 __ vcvtid(temp_s, temp_d);
1980 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001981 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001982 }
Roland Levillain946e1432014-11-11 17:35:19 +00001983
1984 default:
1985 LOG(FATAL) << "Unexpected type conversion from " << input_type
1986 << " to " << result_type;
1987 }
1988 break;
1989
Roland Levillaindff1f282014-11-05 14:15:05 +00001990 case Primitive::kPrimLong:
1991 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001992 case Primitive::kPrimBoolean:
1993 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001994 case Primitive::kPrimByte:
1995 case Primitive::kPrimShort:
1996 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001997 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001998 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001999 DCHECK(out.IsRegisterPair());
2000 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002001 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002002 // Sign extension.
2003 __ Asr(out.AsRegisterPairHigh<Register>(),
2004 out.AsRegisterPairLow<Register>(),
2005 31);
2006 break;
2007
2008 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002009 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002010 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2011 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002012 conversion->GetDexPc(),
2013 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002014 break;
2015
Roland Levillaindff1f282014-11-05 14:15:05 +00002016 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002017 // Processing a Dex `double-to-long' instruction.
2018 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2019 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002020 conversion->GetDexPc(),
2021 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002022 break;
2023
2024 default:
2025 LOG(FATAL) << "Unexpected type conversion from " << input_type
2026 << " to " << result_type;
2027 }
2028 break;
2029
Roland Levillain981e4542014-11-14 11:47:14 +00002030 case Primitive::kPrimChar:
2031 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002032 case Primitive::kPrimBoolean:
2033 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002034 case Primitive::kPrimByte:
2035 case Primitive::kPrimShort:
2036 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002037 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002038 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002039 break;
2040
2041 default:
2042 LOG(FATAL) << "Unexpected type conversion from " << input_type
2043 << " to " << result_type;
2044 }
2045 break;
2046
Roland Levillaindff1f282014-11-05 14:15:05 +00002047 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002048 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002049 case Primitive::kPrimBoolean:
2050 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002051 case Primitive::kPrimByte:
2052 case Primitive::kPrimShort:
2053 case Primitive::kPrimInt:
2054 case Primitive::kPrimChar: {
2055 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2057 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002058 break;
2059 }
2060
Roland Levillain5b3ee562015-04-14 16:02:41 +01002061 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002062 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002063 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2064 conversion,
2065 conversion->GetDexPc(),
2066 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002067 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002068
Roland Levillaincff13742014-11-17 14:32:17 +00002069 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002070 // Processing a Dex `double-to-float' instruction.
2071 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2072 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002073 break;
2074
2075 default:
2076 LOG(FATAL) << "Unexpected type conversion from " << input_type
2077 << " to " << result_type;
2078 };
2079 break;
2080
Roland Levillaindff1f282014-11-05 14:15:05 +00002081 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002082 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002083 case Primitive::kPrimBoolean:
2084 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002085 case Primitive::kPrimByte:
2086 case Primitive::kPrimShort:
2087 case Primitive::kPrimInt:
2088 case Primitive::kPrimChar: {
2089 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002090 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002091 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2092 out.AsFpuRegisterPairLow<SRegister>());
2093 break;
2094 }
2095
Roland Levillain647b9ed2014-11-27 12:06:00 +00002096 case Primitive::kPrimLong: {
2097 // Processing a Dex `long-to-double' instruction.
2098 Register low = in.AsRegisterPairLow<Register>();
2099 Register high = in.AsRegisterPairHigh<Register>();
2100 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2101 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002102 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002103 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002104 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2105 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002106
Roland Levillain682393c2015-04-14 15:57:52 +01002107 // temp_d = int-to-double(high)
2108 __ vmovsr(temp_s, high);
2109 __ vcvtdi(temp_d, temp_s);
2110 // constant_d = k2Pow32EncodingForDouble
2111 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2112 // out_d = unsigned-to-double(low)
2113 __ vmovsr(out_s, low);
2114 __ vcvtdu(out_d, out_s);
2115 // out_d += temp_d * constant_d
2116 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002117 break;
2118 }
2119
Roland Levillaincff13742014-11-17 14:32:17 +00002120 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002121 // Processing a Dex `float-to-double' instruction.
2122 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2123 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002124 break;
2125
2126 default:
2127 LOG(FATAL) << "Unexpected type conversion from " << input_type
2128 << " to " << result_type;
2129 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002130 break;
2131
2132 default:
2133 LOG(FATAL) << "Unexpected type conversion from " << input_type
2134 << " to " << result_type;
2135 }
2136}
2137
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002138void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002139 LocationSummary* locations =
2140 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002141 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002142 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002143 locations->SetInAt(0, Location::RequiresRegister());
2144 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002145 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2146 break;
2147 }
2148
2149 case Primitive::kPrimLong: {
2150 locations->SetInAt(0, Location::RequiresRegister());
2151 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002153 break;
2154 }
2155
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002156 case Primitive::kPrimFloat:
2157 case Primitive::kPrimDouble: {
2158 locations->SetInAt(0, Location::RequiresFpuRegister());
2159 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002160 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002161 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002162 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002163
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002164 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002165 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002166 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002167}
2168
2169void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2170 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002171 Location out = locations->Out();
2172 Location first = locations->InAt(0);
2173 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002174 switch (add->GetResultType()) {
2175 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002176 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002177 __ add(out.AsRegister<Register>(),
2178 first.AsRegister<Register>(),
2179 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002180 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002181 __ AddConstant(out.AsRegister<Register>(),
2182 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002183 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002184 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002185 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002186
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002187 case Primitive::kPrimLong: {
2188 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002189 __ adds(out.AsRegisterPairLow<Register>(),
2190 first.AsRegisterPairLow<Register>(),
2191 ShifterOperand(second.AsRegisterPairLow<Register>()));
2192 __ adc(out.AsRegisterPairHigh<Register>(),
2193 first.AsRegisterPairHigh<Register>(),
2194 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002195 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002196 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002197
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002198 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002199 __ vadds(out.AsFpuRegister<SRegister>(),
2200 first.AsFpuRegister<SRegister>(),
2201 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002202 break;
2203
2204 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002205 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2206 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2207 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002208 break;
2209
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002210 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002211 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002212 }
2213}
2214
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002215void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002216 LocationSummary* locations =
2217 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002218 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002219 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002220 locations->SetInAt(0, Location::RequiresRegister());
2221 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002222 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2223 break;
2224 }
2225
2226 case Primitive::kPrimLong: {
2227 locations->SetInAt(0, Location::RequiresRegister());
2228 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002230 break;
2231 }
Calin Juravle11351682014-10-23 15:38:15 +01002232 case Primitive::kPrimFloat:
2233 case Primitive::kPrimDouble: {
2234 locations->SetInAt(0, Location::RequiresFpuRegister());
2235 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002236 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002237 break;
Calin Juravle11351682014-10-23 15:38:15 +01002238 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002239 default:
Calin Juravle11351682014-10-23 15:38:15 +01002240 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002241 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002242}
2243
2244void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2245 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002246 Location out = locations->Out();
2247 Location first = locations->InAt(0);
2248 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002249 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002250 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002251 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002252 __ sub(out.AsRegister<Register>(),
2253 first.AsRegister<Register>(),
2254 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002255 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002256 __ AddConstant(out.AsRegister<Register>(),
2257 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002258 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002259 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002260 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002261 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002262
Calin Juravle11351682014-10-23 15:38:15 +01002263 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002264 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002265 __ subs(out.AsRegisterPairLow<Register>(),
2266 first.AsRegisterPairLow<Register>(),
2267 ShifterOperand(second.AsRegisterPairLow<Register>()));
2268 __ sbc(out.AsRegisterPairHigh<Register>(),
2269 first.AsRegisterPairHigh<Register>(),
2270 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002271 break;
Calin Juravle11351682014-10-23 15:38:15 +01002272 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002273
Calin Juravle11351682014-10-23 15:38:15 +01002274 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002275 __ vsubs(out.AsFpuRegister<SRegister>(),
2276 first.AsFpuRegister<SRegister>(),
2277 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002278 break;
Calin Juravle11351682014-10-23 15:38:15 +01002279 }
2280
2281 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002282 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2283 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2284 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002285 break;
2286 }
2287
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002289 default:
Calin Juravle11351682014-10-23 15:38:15 +01002290 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002291 }
2292}
2293
Calin Juravle34bacdf2014-10-07 20:23:36 +01002294void LocationsBuilderARM::VisitMul(HMul* mul) {
2295 LocationSummary* locations =
2296 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2297 switch (mul->GetResultType()) {
2298 case Primitive::kPrimInt:
2299 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002300 locations->SetInAt(0, Location::RequiresRegister());
2301 locations->SetInAt(1, Location::RequiresRegister());
2302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002303 break;
2304 }
2305
Calin Juravleb5bfa962014-10-21 18:02:24 +01002306 case Primitive::kPrimFloat:
2307 case Primitive::kPrimDouble: {
2308 locations->SetInAt(0, Location::RequiresFpuRegister());
2309 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002310 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002311 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002312 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002313
2314 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002315 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002316 }
2317}
2318
2319void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2320 LocationSummary* locations = mul->GetLocations();
2321 Location out = locations->Out();
2322 Location first = locations->InAt(0);
2323 Location second = locations->InAt(1);
2324 switch (mul->GetResultType()) {
2325 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002326 __ mul(out.AsRegister<Register>(),
2327 first.AsRegister<Register>(),
2328 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002329 break;
2330 }
2331 case Primitive::kPrimLong: {
2332 Register out_hi = out.AsRegisterPairHigh<Register>();
2333 Register out_lo = out.AsRegisterPairLow<Register>();
2334 Register in1_hi = first.AsRegisterPairHigh<Register>();
2335 Register in1_lo = first.AsRegisterPairLow<Register>();
2336 Register in2_hi = second.AsRegisterPairHigh<Register>();
2337 Register in2_lo = second.AsRegisterPairLow<Register>();
2338
2339 // Extra checks to protect caused by the existence of R1_R2.
2340 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2341 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2342 DCHECK_NE(out_hi, in1_lo);
2343 DCHECK_NE(out_hi, in2_lo);
2344
2345 // input: in1 - 64 bits, in2 - 64 bits
2346 // output: out
2347 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2348 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2349 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2350
2351 // IP <- in1.lo * in2.hi
2352 __ mul(IP, in1_lo, in2_hi);
2353 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2354 __ mla(out_hi, in1_hi, in2_lo, IP);
2355 // out.lo <- (in1.lo * in2.lo)[31:0];
2356 __ umull(out_lo, IP, in1_lo, in2_lo);
2357 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2358 __ add(out_hi, out_hi, ShifterOperand(IP));
2359 break;
2360 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002361
2362 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002363 __ vmuls(out.AsFpuRegister<SRegister>(),
2364 first.AsFpuRegister<SRegister>(),
2365 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002366 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002367 }
2368
2369 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002370 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2371 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2372 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002373 break;
2374 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002375
2376 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002377 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002378 }
2379}
2380
Zheng Xuc6667102015-05-15 16:08:45 +08002381void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2382 DCHECK(instruction->IsDiv() || instruction->IsRem());
2383 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2384
2385 LocationSummary* locations = instruction->GetLocations();
2386 Location second = locations->InAt(1);
2387 DCHECK(second.IsConstant());
2388
2389 Register out = locations->Out().AsRegister<Register>();
2390 Register dividend = locations->InAt(0).AsRegister<Register>();
2391 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2392 DCHECK(imm == 1 || imm == -1);
2393
2394 if (instruction->IsRem()) {
2395 __ LoadImmediate(out, 0);
2396 } else {
2397 if (imm == 1) {
2398 __ Mov(out, dividend);
2399 } else {
2400 __ rsb(out, dividend, ShifterOperand(0));
2401 }
2402 }
2403}
2404
2405void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2406 DCHECK(instruction->IsDiv() || instruction->IsRem());
2407 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2408
2409 LocationSummary* locations = instruction->GetLocations();
2410 Location second = locations->InAt(1);
2411 DCHECK(second.IsConstant());
2412
2413 Register out = locations->Out().AsRegister<Register>();
2414 Register dividend = locations->InAt(0).AsRegister<Register>();
2415 Register temp = locations->GetTemp(0).AsRegister<Register>();
2416 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002417 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002418 DCHECK(IsPowerOfTwo(abs_imm));
2419 int ctz_imm = CTZ(abs_imm);
2420
2421 if (ctz_imm == 1) {
2422 __ Lsr(temp, dividend, 32 - ctz_imm);
2423 } else {
2424 __ Asr(temp, dividend, 31);
2425 __ Lsr(temp, temp, 32 - ctz_imm);
2426 }
2427 __ add(out, temp, ShifterOperand(dividend));
2428
2429 if (instruction->IsDiv()) {
2430 __ Asr(out, out, ctz_imm);
2431 if (imm < 0) {
2432 __ rsb(out, out, ShifterOperand(0));
2433 }
2434 } else {
2435 __ ubfx(out, out, 0, ctz_imm);
2436 __ sub(out, out, ShifterOperand(temp));
2437 }
2438}
2439
2440void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2441 DCHECK(instruction->IsDiv() || instruction->IsRem());
2442 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2443
2444 LocationSummary* locations = instruction->GetLocations();
2445 Location second = locations->InAt(1);
2446 DCHECK(second.IsConstant());
2447
2448 Register out = locations->Out().AsRegister<Register>();
2449 Register dividend = locations->InAt(0).AsRegister<Register>();
2450 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2451 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2452 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2453
2454 int64_t magic;
2455 int shift;
2456 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2457
2458 __ LoadImmediate(temp1, magic);
2459 __ smull(temp2, temp1, dividend, temp1);
2460
2461 if (imm > 0 && magic < 0) {
2462 __ add(temp1, temp1, ShifterOperand(dividend));
2463 } else if (imm < 0 && magic > 0) {
2464 __ sub(temp1, temp1, ShifterOperand(dividend));
2465 }
2466
2467 if (shift != 0) {
2468 __ Asr(temp1, temp1, shift);
2469 }
2470
2471 if (instruction->IsDiv()) {
2472 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2473 } else {
2474 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2475 // TODO: Strength reduction for mls.
2476 __ LoadImmediate(temp2, imm);
2477 __ mls(out, temp1, temp2, dividend);
2478 }
2479}
2480
2481void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2482 DCHECK(instruction->IsDiv() || instruction->IsRem());
2483 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2484
2485 LocationSummary* locations = instruction->GetLocations();
2486 Location second = locations->InAt(1);
2487 DCHECK(second.IsConstant());
2488
2489 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2490 if (imm == 0) {
2491 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2492 } else if (imm == 1 || imm == -1) {
2493 DivRemOneOrMinusOne(instruction);
2494 } else if (IsPowerOfTwo(std::abs(imm))) {
2495 DivRemByPowerOfTwo(instruction);
2496 } else {
2497 DCHECK(imm <= -2 || imm >= 2);
2498 GenerateDivRemWithAnyConstant(instruction);
2499 }
2500}
2501
Calin Juravle7c4954d2014-10-28 16:57:40 +00002502void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002503 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2504 if (div->GetResultType() == Primitive::kPrimLong) {
2505 // pLdiv runtime call.
2506 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002507 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2508 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002509 } else if (div->GetResultType() == Primitive::kPrimInt &&
2510 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2511 // pIdivmod runtime call.
2512 call_kind = LocationSummary::kCall;
2513 }
2514
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2516
Calin Juravle7c4954d2014-10-28 16:57:40 +00002517 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002518 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002519 if (div->InputAt(1)->IsConstant()) {
2520 locations->SetInAt(0, Location::RequiresRegister());
2521 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2523 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2524 if (abs_imm <= 1) {
2525 // No temp register required.
2526 } else {
2527 locations->AddTemp(Location::RequiresRegister());
2528 if (!IsPowerOfTwo(abs_imm)) {
2529 locations->AddTemp(Location::RequiresRegister());
2530 }
2531 }
2532 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002533 locations->SetInAt(0, Location::RequiresRegister());
2534 locations->SetInAt(1, Location::RequiresRegister());
2535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2536 } else {
2537 InvokeRuntimeCallingConvention calling_convention;
2538 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2539 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2540 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2541 // we only need the former.
2542 locations->SetOut(Location::RegisterLocation(R0));
2543 }
Calin Juravled0d48522014-11-04 16:40:20 +00002544 break;
2545 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002546 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002547 InvokeRuntimeCallingConvention calling_convention;
2548 locations->SetInAt(0, Location::RegisterPairLocation(
2549 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2550 locations->SetInAt(1, Location::RegisterPairLocation(
2551 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002552 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002553 break;
2554 }
2555 case Primitive::kPrimFloat:
2556 case Primitive::kPrimDouble: {
2557 locations->SetInAt(0, Location::RequiresFpuRegister());
2558 locations->SetInAt(1, Location::RequiresFpuRegister());
2559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2560 break;
2561 }
2562
2563 default:
2564 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2565 }
2566}
2567
2568void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2569 LocationSummary* locations = div->GetLocations();
2570 Location out = locations->Out();
2571 Location first = locations->InAt(0);
2572 Location second = locations->InAt(1);
2573
2574 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002575 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002576 if (second.IsConstant()) {
2577 GenerateDivRemConstantIntegral(div);
2578 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002579 __ sdiv(out.AsRegister<Register>(),
2580 first.AsRegister<Register>(),
2581 second.AsRegister<Register>());
2582 } else {
2583 InvokeRuntimeCallingConvention calling_convention;
2584 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2585 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2586 DCHECK_EQ(R0, out.AsRegister<Register>());
2587
2588 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2589 }
Calin Juravled0d48522014-11-04 16:40:20 +00002590 break;
2591 }
2592
Calin Juravle7c4954d2014-10-28 16:57:40 +00002593 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002594 InvokeRuntimeCallingConvention calling_convention;
2595 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2596 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2597 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2598 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2599 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002600 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002601
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002602 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002603 break;
2604 }
2605
2606 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002607 __ vdivs(out.AsFpuRegister<SRegister>(),
2608 first.AsFpuRegister<SRegister>(),
2609 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002610 break;
2611 }
2612
2613 case Primitive::kPrimDouble: {
2614 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2615 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2616 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2617 break;
2618 }
2619
2620 default:
2621 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2622 }
2623}
2624
Calin Juravlebacfec32014-11-14 15:54:36 +00002625void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002626 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002627
2628 // Most remainders are implemented in the runtime.
2629 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002630 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2631 // sdiv will be replaced by other instruction sequence.
2632 call_kind = LocationSummary::kNoCall;
2633 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2634 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002635 // Have hardware divide instruction for int, do it with three instructions.
2636 call_kind = LocationSummary::kNoCall;
2637 }
2638
Calin Juravlebacfec32014-11-14 15:54:36 +00002639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2640
Calin Juravled2ec87d2014-12-08 14:24:46 +00002641 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002642 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002643 if (rem->InputAt(1)->IsConstant()) {
2644 locations->SetInAt(0, Location::RequiresRegister());
2645 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2647 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2648 if (abs_imm <= 1) {
2649 // No temp register required.
2650 } else {
2651 locations->AddTemp(Location::RequiresRegister());
2652 if (!IsPowerOfTwo(abs_imm)) {
2653 locations->AddTemp(Location::RequiresRegister());
2654 }
2655 }
2656 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002657 locations->SetInAt(0, Location::RequiresRegister());
2658 locations->SetInAt(1, Location::RequiresRegister());
2659 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2660 locations->AddTemp(Location::RequiresRegister());
2661 } else {
2662 InvokeRuntimeCallingConvention calling_convention;
2663 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2664 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2665 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2666 // we only need the latter.
2667 locations->SetOut(Location::RegisterLocation(R1));
2668 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002669 break;
2670 }
2671 case Primitive::kPrimLong: {
2672 InvokeRuntimeCallingConvention calling_convention;
2673 locations->SetInAt(0, Location::RegisterPairLocation(
2674 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2675 locations->SetInAt(1, Location::RegisterPairLocation(
2676 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2677 // The runtime helper puts the output in R2,R3.
2678 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2679 break;
2680 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002681 case Primitive::kPrimFloat: {
2682 InvokeRuntimeCallingConvention calling_convention;
2683 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2684 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2685 locations->SetOut(Location::FpuRegisterLocation(S0));
2686 break;
2687 }
2688
Calin Juravlebacfec32014-11-14 15:54:36 +00002689 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002690 InvokeRuntimeCallingConvention calling_convention;
2691 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2692 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2693 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2694 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2695 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002696 break;
2697 }
2698
2699 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002700 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002701 }
2702}
2703
2704void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2705 LocationSummary* locations = rem->GetLocations();
2706 Location out = locations->Out();
2707 Location first = locations->InAt(0);
2708 Location second = locations->InAt(1);
2709
Calin Juravled2ec87d2014-12-08 14:24:46 +00002710 Primitive::Type type = rem->GetResultType();
2711 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002712 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002713 if (second.IsConstant()) {
2714 GenerateDivRemConstantIntegral(rem);
2715 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002716 Register reg1 = first.AsRegister<Register>();
2717 Register reg2 = second.AsRegister<Register>();
2718 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002719
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002720 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002721 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002722 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002723 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002724 } else {
2725 InvokeRuntimeCallingConvention calling_convention;
2726 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2727 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2728 DCHECK_EQ(R1, out.AsRegister<Register>());
2729
2730 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2731 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002732 break;
2733 }
2734
2735 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002736 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002737 break;
2738 }
2739
Calin Juravled2ec87d2014-12-08 14:24:46 +00002740 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002741 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002742 break;
2743 }
2744
Calin Juravlebacfec32014-11-14 15:54:36 +00002745 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002746 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002747 break;
2748 }
2749
2750 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002751 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002752 }
2753}
2754
Calin Juravled0d48522014-11-04 16:40:20 +00002755void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdilb022fa12015-08-20 17:47:48 +01002756 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2757 ? LocationSummary::kCallOnSlowPath
2758 : LocationSummary::kNoCall;
2759 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002760 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002761 if (instruction->HasUses()) {
2762 locations->SetOut(Location::SameAsFirstInput());
2763 }
2764}
2765
2766void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2767 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2768 codegen_->AddSlowPath(slow_path);
2769
2770 LocationSummary* locations = instruction->GetLocations();
2771 Location value = locations->InAt(0);
2772
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002773 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002774 case Primitive::kPrimByte:
2775 case Primitive::kPrimChar:
2776 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002777 case Primitive::kPrimInt: {
2778 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002779 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002780 } else {
2781 DCHECK(value.IsConstant()) << value;
2782 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2783 __ b(slow_path->GetEntryLabel());
2784 }
2785 }
2786 break;
2787 }
2788 case Primitive::kPrimLong: {
2789 if (value.IsRegisterPair()) {
2790 __ orrs(IP,
2791 value.AsRegisterPairLow<Register>(),
2792 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2793 __ b(slow_path->GetEntryLabel(), EQ);
2794 } else {
2795 DCHECK(value.IsConstant()) << value;
2796 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2797 __ b(slow_path->GetEntryLabel());
2798 }
2799 }
2800 break;
2801 default:
2802 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2803 }
2804 }
Calin Juravled0d48522014-11-04 16:40:20 +00002805}
2806
Calin Juravle9aec02f2014-11-18 23:06:35 +00002807void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2808 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2809
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002810 LocationSummary* locations =
2811 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002812
2813 switch (op->GetResultType()) {
2814 case Primitive::kPrimInt: {
2815 locations->SetInAt(0, Location::RequiresRegister());
2816 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002817 // Make the output overlap, as it will be used to hold the masked
2818 // second input.
2819 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002820 break;
2821 }
2822 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002823 locations->SetInAt(0, Location::RequiresRegister());
2824 locations->SetInAt(1, Location::RequiresRegister());
2825 locations->AddTemp(Location::RequiresRegister());
2826 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002827 break;
2828 }
2829 default:
2830 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2831 }
2832}
2833
2834void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2835 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2836
2837 LocationSummary* locations = op->GetLocations();
2838 Location out = locations->Out();
2839 Location first = locations->InAt(0);
2840 Location second = locations->InAt(1);
2841
2842 Primitive::Type type = op->GetResultType();
2843 switch (type) {
2844 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002845 Register out_reg = out.AsRegister<Register>();
2846 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002847 // Arm doesn't mask the shift count so we need to do it ourselves.
2848 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002849 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002850 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002851 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002852 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002853 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002854 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002855 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002856 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002857 }
2858 } else {
2859 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2860 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2861 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2862 __ Mov(out_reg, first_reg);
2863 } else if (op->IsShl()) {
2864 __ Lsl(out_reg, first_reg, shift_value);
2865 } else if (op->IsShr()) {
2866 __ Asr(out_reg, first_reg, shift_value);
2867 } else {
2868 __ Lsr(out_reg, first_reg, shift_value);
2869 }
2870 }
2871 break;
2872 }
2873 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002874 Register o_h = out.AsRegisterPairHigh<Register>();
2875 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002876
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002877 Register temp = locations->GetTemp(0).AsRegister<Register>();
2878
2879 Register high = first.AsRegisterPairHigh<Register>();
2880 Register low = first.AsRegisterPairLow<Register>();
2881
2882 Register second_reg = second.AsRegister<Register>();
2883
Calin Juravle9aec02f2014-11-18 23:06:35 +00002884 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002885 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002886 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002887 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002888 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002889 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002890 __ Lsr(temp, low, temp);
2891 __ orr(o_h, o_h, ShifterOperand(temp));
2892 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002893 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002894 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002895 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002896 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002897 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002898 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002899 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002900 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002901 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002902 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002903 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002904 __ Lsl(temp, high, temp);
2905 __ orr(o_l, o_l, ShifterOperand(temp));
2906 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002907 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002908 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002909 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002910 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002911 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002912 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002913 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002914 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002915 __ Lsr(o_l, low, o_h);
2916 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002917 __ Lsl(temp, high, temp);
2918 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002919 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002920 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002921 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002922 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002923 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002924 break;
2925 }
2926 default:
2927 LOG(FATAL) << "Unexpected operation type " << type;
2928 }
2929}
2930
2931void LocationsBuilderARM::VisitShl(HShl* shl) {
2932 HandleShift(shl);
2933}
2934
2935void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2936 HandleShift(shl);
2937}
2938
2939void LocationsBuilderARM::VisitShr(HShr* shr) {
2940 HandleShift(shr);
2941}
2942
2943void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2944 HandleShift(shr);
2945}
2946
2947void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2948 HandleShift(ushr);
2949}
2950
2951void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2952 HandleShift(ushr);
2953}
2954
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002955void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002956 LocationSummary* locations =
2957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002958 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002959 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002960 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002961 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002962}
2963
2964void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2965 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002966 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002967 // Note: if heap poisoning is enabled, the entry point takes cares
2968 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002969 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2970 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002971 instruction->GetDexPc(),
2972 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002973}
2974
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002975void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2976 LocationSummary* locations =
2977 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2978 InvokeRuntimeCallingConvention calling_convention;
2979 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002980 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002981 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002982 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002983}
2984
2985void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2986 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002987 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002988 // Note: if heap poisoning is enabled, the entry point takes cares
2989 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002990 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2991 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002992 instruction->GetDexPc(),
2993 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002994}
2995
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002996void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002997 LocationSummary* locations =
2998 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002999 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3000 if (location.IsStackSlot()) {
3001 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3002 } else if (location.IsDoubleStackSlot()) {
3003 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003004 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003005 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003006}
3007
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003008void InstructionCodeGeneratorARM::VisitParameterValue(
3009 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003010 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003011}
3012
3013void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3014 LocationSummary* locations =
3015 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3016 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3017}
3018
3019void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3020 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003021}
3022
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003023void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003024 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003025 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003026 locations->SetInAt(0, Location::RequiresRegister());
3027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003028}
3029
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003030void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3031 LocationSummary* locations = not_->GetLocations();
3032 Location out = locations->Out();
3033 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003034 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003035 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003037 break;
3038
3039 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003040 __ mvn(out.AsRegisterPairLow<Register>(),
3041 ShifterOperand(in.AsRegisterPairLow<Register>()));
3042 __ mvn(out.AsRegisterPairHigh<Register>(),
3043 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003044 break;
3045
3046 default:
3047 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3048 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003049}
3050
David Brazdil66d126e2015-04-03 16:02:44 +01003051void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3052 LocationSummary* locations =
3053 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3054 locations->SetInAt(0, Location::RequiresRegister());
3055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3056}
3057
3058void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003059 LocationSummary* locations = bool_not->GetLocations();
3060 Location out = locations->Out();
3061 Location in = locations->InAt(0);
3062 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3063}
3064
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003065void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003066 LocationSummary* locations =
3067 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003068 switch (compare->InputAt(0)->GetType()) {
3069 case Primitive::kPrimLong: {
3070 locations->SetInAt(0, Location::RequiresRegister());
3071 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003072 // Output overlaps because it is written before doing the low comparison.
3073 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003074 break;
3075 }
3076 case Primitive::kPrimFloat:
3077 case Primitive::kPrimDouble: {
3078 locations->SetInAt(0, Location::RequiresFpuRegister());
3079 locations->SetInAt(1, Location::RequiresFpuRegister());
3080 locations->SetOut(Location::RequiresRegister());
3081 break;
3082 }
3083 default:
3084 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3085 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003086}
3087
3088void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003089 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003090 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003091 Location left = locations->InAt(0);
3092 Location right = locations->InAt(1);
3093
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003094 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003095 Primitive::Type type = compare->InputAt(0)->GetType();
3096 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003097 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003098 __ cmp(left.AsRegisterPairHigh<Register>(),
3099 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003100 __ b(&less, LT);
3101 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003102 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003103 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003104 __ cmp(left.AsRegisterPairLow<Register>(),
3105 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003106 break;
3107 }
3108 case Primitive::kPrimFloat:
3109 case Primitive::kPrimDouble: {
3110 __ LoadImmediate(out, 0);
3111 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003112 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003113 } else {
3114 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3115 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3116 }
3117 __ vmstat(); // transfer FP status register to ARM APSR.
3118 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003119 break;
3120 }
3121 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003122 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003123 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003124 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003125 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003126
3127 __ Bind(&greater);
3128 __ LoadImmediate(out, 1);
3129 __ b(&done);
3130
3131 __ Bind(&less);
3132 __ LoadImmediate(out, -1);
3133
3134 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003135}
3136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003137void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003138 LocationSummary* locations =
3139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003140 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3141 locations->SetInAt(i, Location::Any());
3142 }
3143 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003144}
3145
3146void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003147 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003148 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003149}
3150
Calin Juravle52c48962014-12-16 17:02:57 +00003151void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3152 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003153 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003154 switch (kind) {
3155 case MemBarrierKind::kAnyStore:
3156 case MemBarrierKind::kLoadAny:
3157 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003158 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003159 break;
3160 }
3161 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003162 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003163 break;
3164 }
3165 default:
3166 LOG(FATAL) << "Unexpected memory barrier " << kind;
3167 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003168 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003169}
3170
3171void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3172 uint32_t offset,
3173 Register out_lo,
3174 Register out_hi) {
3175 if (offset != 0) {
3176 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003177 __ add(IP, addr, ShifterOperand(out_lo));
3178 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003179 }
3180 __ ldrexd(out_lo, out_hi, addr);
3181}
3182
3183void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3184 uint32_t offset,
3185 Register value_lo,
3186 Register value_hi,
3187 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003188 Register temp2,
3189 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003190 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003191 if (offset != 0) {
3192 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003193 __ add(IP, addr, ShifterOperand(temp1));
3194 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003195 }
3196 __ Bind(&fail);
3197 // We need a load followed by store. (The address used in a STREX instruction must
3198 // be the same as the address in the most recently executed LDREX instruction.)
3199 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003200 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003201 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003202 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003203}
3204
3205void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3206 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3207
Nicolas Geoffray39468442014-09-02 15:17:15 +01003208 LocationSummary* locations =
3209 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003210 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003211
Calin Juravle52c48962014-12-16 17:02:57 +00003212 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003213 if (Primitive::IsFloatingPointType(field_type)) {
3214 locations->SetInAt(1, Location::RequiresFpuRegister());
3215 } else {
3216 locations->SetInAt(1, Location::RequiresRegister());
3217 }
3218
Calin Juravle52c48962014-12-16 17:02:57 +00003219 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003220 bool generate_volatile = field_info.IsVolatile()
3221 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003222 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003223 bool needs_write_barrier =
3224 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003225 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003226 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003227 if (needs_write_barrier) {
3228 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003229 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003230 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003231 // Arm encoding have some additional constraints for ldrexd/strexd:
3232 // - registers need to be consecutive
3233 // - the first register should be even but not R14.
3234 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3235 // enable Arm encoding.
3236 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3237
3238 locations->AddTemp(Location::RequiresRegister());
3239 locations->AddTemp(Location::RequiresRegister());
3240 if (field_type == Primitive::kPrimDouble) {
3241 // For doubles we need two more registers to copy the value.
3242 locations->AddTemp(Location::RegisterLocation(R2));
3243 locations->AddTemp(Location::RegisterLocation(R3));
3244 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003245 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003246}
3247
Calin Juravle52c48962014-12-16 17:02:57 +00003248void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003249 const FieldInfo& field_info,
3250 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003251 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3252
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003253 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003254 Register base = locations->InAt(0).AsRegister<Register>();
3255 Location value = locations->InAt(1);
3256
3257 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003258 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003259 Primitive::Type field_type = field_info.GetFieldType();
3260 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003261 bool needs_write_barrier =
3262 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003263
3264 if (is_volatile) {
3265 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3266 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003267
3268 switch (field_type) {
3269 case Primitive::kPrimBoolean:
3270 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003271 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003272 break;
3273 }
3274
3275 case Primitive::kPrimShort:
3276 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003277 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003278 break;
3279 }
3280
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003281 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003282 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003283 if (kPoisonHeapReferences && needs_write_barrier) {
3284 // Note that in the case where `value` is a null reference,
3285 // we do not enter this block, as a null reference does not
3286 // need poisoning.
3287 DCHECK_EQ(field_type, Primitive::kPrimNot);
3288 Register temp = locations->GetTemp(0).AsRegister<Register>();
3289 __ Mov(temp, value.AsRegister<Register>());
3290 __ PoisonHeapReference(temp);
3291 __ StoreToOffset(kStoreWord, temp, base, offset);
3292 } else {
3293 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3294 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003295 break;
3296 }
3297
3298 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003299 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003300 GenerateWideAtomicStore(base, offset,
3301 value.AsRegisterPairLow<Register>(),
3302 value.AsRegisterPairHigh<Register>(),
3303 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003304 locations->GetTemp(1).AsRegister<Register>(),
3305 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003306 } else {
3307 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003308 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003309 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003310 break;
3311 }
3312
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003313 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003314 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003315 break;
3316 }
3317
3318 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003319 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003320 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003321 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3322 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3323
3324 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3325
3326 GenerateWideAtomicStore(base, offset,
3327 value_reg_lo,
3328 value_reg_hi,
3329 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003330 locations->GetTemp(3).AsRegister<Register>(),
3331 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003332 } else {
3333 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003334 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003335 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003336 break;
3337 }
3338
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003339 case Primitive::kPrimVoid:
3340 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003341 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003342 }
Calin Juravle52c48962014-12-16 17:02:57 +00003343
Calin Juravle77520bc2015-01-12 18:45:46 +00003344 // Longs and doubles are handled in the switch.
3345 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3346 codegen_->MaybeRecordImplicitNullCheck(instruction);
3347 }
3348
3349 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3350 Register temp = locations->GetTemp(0).AsRegister<Register>();
3351 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003352 codegen_->MarkGCCard(
3353 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003354 }
3355
Calin Juravle52c48962014-12-16 17:02:57 +00003356 if (is_volatile) {
3357 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3358 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003359}
3360
Calin Juravle52c48962014-12-16 17:02:57 +00003361void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3362 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003363 LocationSummary* locations =
3364 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003365 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003366
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003367 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003368 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003369 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003370 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003371
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003372 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3373 locations->SetOut(Location::RequiresFpuRegister());
3374 } else {
3375 locations->SetOut(Location::RequiresRegister(),
3376 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3377 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003378 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003379 // Arm encoding have some additional constraints for ldrexd/strexd:
3380 // - registers need to be consecutive
3381 // - the first register should be even but not R14.
3382 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3383 // enable Arm encoding.
3384 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3385 locations->AddTemp(Location::RequiresRegister());
3386 locations->AddTemp(Location::RequiresRegister());
3387 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003388}
3389
Calin Juravle52c48962014-12-16 17:02:57 +00003390void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3391 const FieldInfo& field_info) {
3392 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003393
Calin Juravle52c48962014-12-16 17:02:57 +00003394 LocationSummary* locations = instruction->GetLocations();
3395 Register base = locations->InAt(0).AsRegister<Register>();
3396 Location out = locations->Out();
3397 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003398 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003399 Primitive::Type field_type = field_info.GetFieldType();
3400 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3401
3402 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003403 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003404 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003405 break;
3406 }
3407
3408 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003409 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003410 break;
3411 }
3412
3413 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003414 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003415 break;
3416 }
3417
3418 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003419 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420 break;
3421 }
3422
3423 case Primitive::kPrimInt:
3424 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003425 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003426 break;
3427 }
3428
3429 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003430 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003431 GenerateWideAtomicLoad(base, offset,
3432 out.AsRegisterPairLow<Register>(),
3433 out.AsRegisterPairHigh<Register>());
3434 } else {
3435 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3436 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003437 break;
3438 }
3439
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003440 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003441 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003442 break;
3443 }
3444
3445 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003446 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003447 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003448 Register lo = locations->GetTemp(0).AsRegister<Register>();
3449 Register hi = locations->GetTemp(1).AsRegister<Register>();
3450 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003451 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003452 __ vmovdrr(out_reg, lo, hi);
3453 } else {
3454 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003455 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003456 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003457 break;
3458 }
3459
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003460 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003461 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003462 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003463 }
Calin Juravle52c48962014-12-16 17:02:57 +00003464
Calin Juravle77520bc2015-01-12 18:45:46 +00003465 // Doubles are handled in the switch.
3466 if (field_type != Primitive::kPrimDouble) {
3467 codegen_->MaybeRecordImplicitNullCheck(instruction);
3468 }
3469
Calin Juravle52c48962014-12-16 17:02:57 +00003470 if (is_volatile) {
3471 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3472 }
Roland Levillain4d027112015-07-01 15:41:14 +01003473
3474 if (field_type == Primitive::kPrimNot) {
3475 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3476 }
Calin Juravle52c48962014-12-16 17:02:57 +00003477}
3478
3479void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3480 HandleFieldSet(instruction, instruction->GetFieldInfo());
3481}
3482
3483void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003484 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003485}
3486
3487void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3488 HandleFieldGet(instruction, instruction->GetFieldInfo());
3489}
3490
3491void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3492 HandleFieldGet(instruction, instruction->GetFieldInfo());
3493}
3494
3495void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3496 HandleFieldGet(instruction, instruction->GetFieldInfo());
3497}
3498
3499void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3500 HandleFieldGet(instruction, instruction->GetFieldInfo());
3501}
3502
3503void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3504 HandleFieldSet(instruction, instruction->GetFieldInfo());
3505}
3506
3507void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003508 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003509}
3510
3511void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdilb022fa12015-08-20 17:47:48 +01003512 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3513 ? LocationSummary::kCallOnSlowPath
3514 : LocationSummary::kNoCall;
3515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003516 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003517 if (instruction->HasUses()) {
3518 locations->SetOut(Location::SameAsFirstInput());
3519 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003520}
3521
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003522void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003523 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3524 return;
3525 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003526 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003527
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003528 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3529 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3530}
3531
3532void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003533 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003534 codegen_->AddSlowPath(slow_path);
3535
3536 LocationSummary* locations = instruction->GetLocations();
3537 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003538
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003539 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003540}
3541
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003542void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdilb022fa12015-08-20 17:47:48 +01003543 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003544 GenerateImplicitNullCheck(instruction);
3545 } else {
3546 GenerateExplicitNullCheck(instruction);
3547 }
3548}
3549
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003550void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003551 LocationSummary* locations =
3552 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003553 locations->SetInAt(0, Location::RequiresRegister());
3554 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003555 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3556 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3557 } else {
3558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3559 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003560}
3561
3562void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3563 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003564 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003565 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003566 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003567
Roland Levillain4d027112015-07-01 15:41:14 +01003568 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003569 case Primitive::kPrimBoolean: {
3570 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003571 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003572 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003573 size_t offset =
3574 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003575 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3576 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003577 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003578 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3579 }
3580 break;
3581 }
3582
3583 case Primitive::kPrimByte: {
3584 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003585 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003586 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003587 size_t offset =
3588 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003589 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3590 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003592 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3593 }
3594 break;
3595 }
3596
3597 case Primitive::kPrimShort: {
3598 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003599 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003600 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003601 size_t offset =
3602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003603 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3604 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003605 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3607 }
3608 break;
3609 }
3610
3611 case Primitive::kPrimChar: {
3612 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003613 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003614 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003615 size_t offset =
3616 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003617 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3618 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003620 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3621 }
3622 break;
3623 }
3624
3625 case Primitive::kPrimInt:
3626 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003627 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3628 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003629 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003630 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003631 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003632 size_t offset =
3633 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003634 __ LoadFromOffset(kLoadWord, out, obj, offset);
3635 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003636 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003637 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3638 }
3639 break;
3640 }
3641
3642 case Primitive::kPrimLong: {
3643 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003644 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003645 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003646 size_t offset =
3647 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003648 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003649 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003650 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003651 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003652 }
3653 break;
3654 }
3655
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003656 case Primitive::kPrimFloat: {
3657 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3658 Location out = locations->Out();
3659 DCHECK(out.IsFpuRegister());
3660 if (index.IsConstant()) {
3661 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3662 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3663 } else {
3664 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3665 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3666 }
3667 break;
3668 }
3669
3670 case Primitive::kPrimDouble: {
3671 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3672 Location out = locations->Out();
3673 DCHECK(out.IsFpuRegisterPair());
3674 if (index.IsConstant()) {
3675 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3676 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3677 } else {
3678 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3679 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3680 }
3681 break;
3682 }
3683
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003684 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003685 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003686 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003687 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003688 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003689
3690 if (type == Primitive::kPrimNot) {
3691 Register out = locations->Out().AsRegister<Register>();
3692 __ MaybeUnpoisonHeapReference(out);
3693 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003694}
3695
3696void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003697 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003698
3699 bool needs_write_barrier =
3700 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3701 bool needs_runtime_call = instruction->NeedsTypeCheck();
3702
Nicolas Geoffray39468442014-09-02 15:17:15 +01003703 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003704 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3705 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003706 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003707 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3708 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3709 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003710 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003711 locations->SetInAt(0, Location::RequiresRegister());
3712 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003713 if (Primitive::IsFloatingPointType(value_type)) {
3714 locations->SetInAt(2, Location::RequiresFpuRegister());
3715 } else {
3716 locations->SetInAt(2, Location::RequiresRegister());
3717 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003718
3719 if (needs_write_barrier) {
3720 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003721 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003722 locations->AddTemp(Location::RequiresRegister());
3723 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003724 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003725}
3726
3727void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3728 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003729 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003730 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003731 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003732 bool needs_runtime_call = locations->WillCall();
3733 bool needs_write_barrier =
3734 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003735
3736 switch (value_type) {
3737 case Primitive::kPrimBoolean:
3738 case Primitive::kPrimByte: {
3739 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003740 Register value = locations->InAt(2).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_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003744 __ StoreToOffset(kStoreByte, value, obj, offset);
3745 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003747 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3748 }
3749 break;
3750 }
3751
3752 case Primitive::kPrimShort:
3753 case Primitive::kPrimChar: {
3754 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003755 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003757 size_t offset =
3758 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003759 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3760 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003761 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3763 }
3764 break;
3765 }
3766
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003767 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003768 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003769 if (!needs_runtime_call) {
3770 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003771 Register value = locations->InAt(2).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01003772 Register source = value;
3773 if (kPoisonHeapReferences && needs_write_barrier) {
3774 // Note that in the case where `value` is a null reference,
3775 // we do not enter this block, as a null reference does not
3776 // need poisoning.
3777 DCHECK_EQ(value_type, Primitive::kPrimNot);
3778 Register temp = locations->GetTemp(0).AsRegister<Register>();
3779 __ Mov(temp, value);
3780 __ PoisonHeapReference(temp);
3781 source = temp;
3782 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003783 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003784 size_t offset =
3785 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain4d027112015-07-01 15:41:14 +01003786 __ StoreToOffset(kStoreWord, source, obj, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003787 } else {
3788 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003789 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01003790 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003791 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003792 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003793 if (needs_write_barrier) {
3794 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003795 Register temp = locations->GetTemp(0).AsRegister<Register>();
3796 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003797 codegen_->MarkGCCard(temp, card, obj, value, instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003798 }
3799 } else {
3800 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01003801 // Note: if heap poisoning is enabled, pAputObject takes cares
3802 // of poisoning the reference.
Roland Levillain199f3362014-11-27 17:15:16 +00003803 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3804 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003805 instruction->GetDexPc(),
3806 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003807 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003808 break;
3809 }
3810
3811 case Primitive::kPrimLong: {
3812 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003813 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003815 size_t offset =
3816 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003817 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003818 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003819 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003820 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003821 }
3822 break;
3823 }
3824
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003825 case Primitive::kPrimFloat: {
3826 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3827 Location value = locations->InAt(2);
3828 DCHECK(value.IsFpuRegister());
3829 if (index.IsConstant()) {
3830 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3831 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3832 } else {
3833 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3834 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3835 }
3836 break;
3837 }
3838
3839 case Primitive::kPrimDouble: {
3840 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3841 Location value = locations->InAt(2);
3842 DCHECK(value.IsFpuRegisterPair());
3843 if (index.IsConstant()) {
3844 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3845 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3846 } else {
3847 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3848 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3849 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003850
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003851 break;
3852 }
3853
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003854 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003855 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003856 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003857 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003858
3859 // Ints and objects are handled in the switch.
3860 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3861 codegen_->MaybeRecordImplicitNullCheck(instruction);
3862 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003863}
3864
3865void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003866 LocationSummary* locations =
3867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003868 locations->SetInAt(0, Location::RequiresRegister());
3869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003870}
3871
3872void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3873 LocationSummary* locations = instruction->GetLocations();
3874 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003875 Register obj = locations->InAt(0).AsRegister<Register>();
3876 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003877 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003878 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879}
3880
3881void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdilb022fa12015-08-20 17:47:48 +01003882 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3883 ? LocationSummary::kCallOnSlowPath
3884 : LocationSummary::kNoCall;
3885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003886 locations->SetInAt(0, Location::RequiresRegister());
3887 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003888 if (instruction->HasUses()) {
3889 locations->SetOut(Location::SameAsFirstInput());
3890 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003891}
3892
3893void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3894 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003895 SlowPathCodeARM* slow_path =
3896 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003897 codegen_->AddSlowPath(slow_path);
3898
Roland Levillain271ab9c2014-11-27 15:23:57 +00003899 Register index = locations->InAt(0).AsRegister<Register>();
3900 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003901
3902 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01003903 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003904}
3905
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003906void CodeGeneratorARM::MarkGCCard(Register temp,
3907 Register card,
3908 Register object,
3909 Register value,
3910 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003911 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003912 if (can_be_null) {
3913 __ CompareAndBranchIfZero(value, &is_null);
3914 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003915 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3916 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3917 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003918 if (can_be_null) {
3919 __ Bind(&is_null);
3920 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003921}
3922
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003923void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3924 temp->SetLocations(nullptr);
3925}
3926
3927void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3928 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003929 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003930}
3931
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003932void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003933 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003934 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003935}
3936
3937void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003938 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3939}
3940
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003941void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3942 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3943}
3944
3945void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003946 HBasicBlock* block = instruction->GetBlock();
3947 if (block->GetLoopInformation() != nullptr) {
3948 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3949 // The back edge will generate the suspend check.
3950 return;
3951 }
3952 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3953 // The goto will generate the suspend check.
3954 return;
3955 }
3956 GenerateSuspendCheck(instruction, nullptr);
3957}
3958
3959void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3960 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003961 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003962 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
3963 if (slow_path == nullptr) {
3964 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
3965 instruction->SetSlowPath(slow_path);
3966 codegen_->AddSlowPath(slow_path);
3967 if (successor != nullptr) {
3968 DCHECK(successor->IsLoopHeader());
3969 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
3970 }
3971 } else {
3972 DCHECK_EQ(slow_path->GetSuccessor(), successor);
3973 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003974
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003975 __ LoadFromOffset(
3976 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003977 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003978 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003979 __ Bind(slow_path->GetReturnLabel());
3980 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003981 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003982 __ b(slow_path->GetEntryLabel());
3983 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003984}
3985
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003986ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3987 return codegen_->GetAssembler();
3988}
3989
3990void ParallelMoveResolverARM::EmitMove(size_t index) {
3991 MoveOperands* move = moves_.Get(index);
3992 Location source = move->GetSource();
3993 Location destination = move->GetDestination();
3994
3995 if (source.IsRegister()) {
3996 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003997 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003998 } else {
3999 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004000 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004001 SP, destination.GetStackIndex());
4002 }
4003 } else if (source.IsStackSlot()) {
4004 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004005 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004006 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004007 } else if (destination.IsFpuRegister()) {
4008 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004009 } else {
4010 DCHECK(destination.IsStackSlot());
4011 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4012 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4013 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004014 } else if (source.IsFpuRegister()) {
4015 if (destination.IsFpuRegister()) {
4016 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004017 } else {
4018 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004019 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4020 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004021 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004022 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004023 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4024 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004025 } else if (destination.IsRegisterPair()) {
4026 DCHECK(ExpectedPairLayout(destination));
4027 __ LoadFromOffset(
4028 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4029 } else {
4030 DCHECK(destination.IsFpuRegisterPair()) << destination;
4031 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4032 SP,
4033 source.GetStackIndex());
4034 }
4035 } else if (source.IsRegisterPair()) {
4036 if (destination.IsRegisterPair()) {
4037 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4038 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4039 } else {
4040 DCHECK(destination.IsDoubleStackSlot()) << destination;
4041 DCHECK(ExpectedPairLayout(source));
4042 __ StoreToOffset(
4043 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4044 }
4045 } else if (source.IsFpuRegisterPair()) {
4046 if (destination.IsFpuRegisterPair()) {
4047 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4048 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4049 } else {
4050 DCHECK(destination.IsDoubleStackSlot()) << destination;
4051 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4052 SP,
4053 destination.GetStackIndex());
4054 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004055 } else {
4056 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004057 HConstant* constant = source.GetConstant();
4058 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4059 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004060 if (destination.IsRegister()) {
4061 __ LoadImmediate(destination.AsRegister<Register>(), value);
4062 } else {
4063 DCHECK(destination.IsStackSlot());
4064 __ LoadImmediate(IP, value);
4065 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4066 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004067 } else if (constant->IsLongConstant()) {
4068 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004069 if (destination.IsRegisterPair()) {
4070 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4071 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004072 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004073 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004074 __ LoadImmediate(IP, Low32Bits(value));
4075 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4076 __ LoadImmediate(IP, High32Bits(value));
4077 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4078 }
4079 } else if (constant->IsDoubleConstant()) {
4080 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004081 if (destination.IsFpuRegisterPair()) {
4082 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004083 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004084 DCHECK(destination.IsDoubleStackSlot()) << destination;
4085 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004086 __ LoadImmediate(IP, Low32Bits(int_value));
4087 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4088 __ LoadImmediate(IP, High32Bits(int_value));
4089 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4090 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004091 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004092 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004093 float value = constant->AsFloatConstant()->GetValue();
4094 if (destination.IsFpuRegister()) {
4095 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4096 } else {
4097 DCHECK(destination.IsStackSlot());
4098 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4099 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4100 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004101 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004102 }
4103}
4104
4105void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4106 __ Mov(IP, reg);
4107 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4108 __ StoreToOffset(kStoreWord, IP, SP, mem);
4109}
4110
4111void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4112 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4113 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4114 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4115 SP, mem1 + stack_offset);
4116 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4117 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4118 SP, mem2 + stack_offset);
4119 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4120}
4121
4122void ParallelMoveResolverARM::EmitSwap(size_t index) {
4123 MoveOperands* move = moves_.Get(index);
4124 Location source = move->GetSource();
4125 Location destination = move->GetDestination();
4126
4127 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 DCHECK_NE(source.AsRegister<Register>(), IP);
4129 DCHECK_NE(destination.AsRegister<Register>(), IP);
4130 __ Mov(IP, source.AsRegister<Register>());
4131 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4132 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004133 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004134 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004135 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004136 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004137 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4138 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004139 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004140 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004141 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004142 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004143 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004144 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004145 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004146 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004147 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4148 destination.AsRegisterPairHigh<Register>(),
4149 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004150 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004151 Register low_reg = source.IsRegisterPair()
4152 ? source.AsRegisterPairLow<Register>()
4153 : destination.AsRegisterPairLow<Register>();
4154 int mem = source.IsRegisterPair()
4155 ? destination.GetStackIndex()
4156 : source.GetStackIndex();
4157 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004158 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004159 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004160 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004161 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004162 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4163 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004164 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004165 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004166 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004167 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4168 DRegister reg = source.IsFpuRegisterPair()
4169 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4170 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4171 int mem = source.IsFpuRegisterPair()
4172 ? destination.GetStackIndex()
4173 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004174 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004175 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004176 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004177 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4178 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4179 : destination.AsFpuRegister<SRegister>();
4180 int mem = source.IsFpuRegister()
4181 ? destination.GetStackIndex()
4182 : source.GetStackIndex();
4183
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004184 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004185 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004186 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004187 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004188 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4189 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004190 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004191 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004192 }
4193}
4194
4195void ParallelMoveResolverARM::SpillScratch(int reg) {
4196 __ Push(static_cast<Register>(reg));
4197}
4198
4199void ParallelMoveResolverARM::RestoreScratch(int reg) {
4200 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004201}
4202
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004203void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004204 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4205 ? LocationSummary::kCallOnSlowPath
4206 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004207 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004208 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004209 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004210 locations->SetOut(Location::RequiresRegister());
4211}
4212
4213void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004214 LocationSummary* locations = cls->GetLocations();
4215 Register out = locations->Out().AsRegister<Register>();
4216 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004217 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004218 DCHECK(!cls->CanCallRuntime());
4219 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004220 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004221 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004222 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004223 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004224 __ LoadFromOffset(kLoadWord,
4225 out,
4226 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004227 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004228 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004229 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004230
4231 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
4232 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4233 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004234 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004235 if (cls->MustGenerateClinitCheck()) {
4236 GenerateClassInitializationCheck(slow_path, out);
4237 } else {
4238 __ Bind(slow_path->GetExitLabel());
4239 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004240 }
4241}
4242
4243void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4244 LocationSummary* locations =
4245 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4246 locations->SetInAt(0, Location::RequiresRegister());
4247 if (check->HasUses()) {
4248 locations->SetOut(Location::SameAsFirstInput());
4249 }
4250}
4251
4252void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004253 // We assume the class is not null.
4254 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
4255 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004256 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004257 GenerateClassInitializationCheck(slow_path,
4258 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004259}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004260
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004261void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
4262 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004263 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4264 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4265 __ b(slow_path->GetEntryLabel(), LT);
4266 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4267 // properly. Therefore, we do a memory fence.
4268 __ dmb(ISH);
4269 __ Bind(slow_path->GetExitLabel());
4270}
4271
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004272void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4273 LocationSummary* locations =
4274 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004275 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004276 locations->SetOut(Location::RequiresRegister());
4277}
4278
4279void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
4280 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
4281 codegen_->AddSlowPath(slow_path);
4282
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004283 LocationSummary* locations = load->GetLocations();
4284 Register out = locations->Out().AsRegister<Register>();
4285 Register current_method = locations->InAt(0).AsRegister<Register>();
4286 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004287 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004288 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004289 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004290 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004291 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004292 __ Bind(slow_path->GetExitLabel());
4293}
4294
David Brazdilcb1c0552015-08-04 16:22:25 +01004295static int32_t GetExceptionTlsOffset() {
4296 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4297}
4298
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004299void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4300 LocationSummary* locations =
4301 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4302 locations->SetOut(Location::RequiresRegister());
4303}
4304
4305void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004306 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004307 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4308}
4309
4310void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4311 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4312}
4313
4314void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004315 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004316 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004317}
4318
4319void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4320 LocationSummary* locations =
4321 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4322 InvokeRuntimeCallingConvention calling_convention;
4323 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4324}
4325
4326void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4327 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004328 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004329}
4330
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004331void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004332 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4333 ? LocationSummary::kNoCall
4334 : LocationSummary::kCallOnSlowPath;
4335 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4336 locations->SetInAt(0, Location::RequiresRegister());
4337 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004338 // The out register is used as a temporary, so it overlaps with the inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004339 // Note that TypeCheckSlowPathARM uses this register too.
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004340 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004341}
4342
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004343void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004344 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004345 Register obj = locations->InAt(0).AsRegister<Register>();
4346 Register cls = locations->InAt(1).AsRegister<Register>();
4347 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004348 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004349 Label done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004350 SlowPathCodeARM* slow_path = nullptr;
4351
4352 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004353 // avoid null check if we know obj is not null.
4354 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004355 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004356 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004357 // Compare the class of `obj` with `cls`.
4358 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004359 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004360 __ cmp(out, ShifterOperand(cls));
4361 if (instruction->IsClassFinal()) {
4362 // Classes must be equal for the instanceof to succeed.
4363 __ b(&zero, NE);
4364 __ LoadImmediate(out, 1);
4365 __ b(&done);
4366 } else {
4367 // If the classes are not equal, we go into a slow path.
4368 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004369 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004370 codegen_->AddSlowPath(slow_path);
4371 __ b(slow_path->GetEntryLabel(), NE);
4372 __ LoadImmediate(out, 1);
4373 __ b(&done);
4374 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004375
4376 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4377 __ Bind(&zero);
4378 __ LoadImmediate(out, 0);
4379 }
4380
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004381 if (slow_path != nullptr) {
4382 __ Bind(slow_path->GetExitLabel());
4383 }
4384 __ Bind(&done);
4385}
4386
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004387void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
4388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4389 instruction, LocationSummary::kCallOnSlowPath);
4390 locations->SetInAt(0, Location::RequiresRegister());
4391 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004392 // Note that TypeCheckSlowPathARM uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004393 locations->AddTemp(Location::RequiresRegister());
4394}
4395
4396void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4397 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004398 Register obj = locations->InAt(0).AsRegister<Register>();
4399 Register cls = locations->InAt(1).AsRegister<Register>();
4400 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004401 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4402
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004403 SlowPathCodeARM* slow_path =
4404 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004405 codegen_->AddSlowPath(slow_path);
4406
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004407 // avoid null check if we know obj is not null.
4408 if (instruction->MustDoNullCheck()) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004409 __ CompareAndBranchIfZero(obj, slow_path->GetExitLabel());
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004410 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004411 // Compare the class of `obj` with `cls`.
4412 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004413 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004414 __ cmp(temp, ShifterOperand(cls));
Roland Levillain4d027112015-07-01 15:41:14 +01004415 // The checkcast succeeds if the classes are equal (fast path).
4416 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004417 __ b(slow_path->GetEntryLabel(), NE);
4418 __ Bind(slow_path->GetExitLabel());
4419}
4420
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004421void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4422 LocationSummary* locations =
4423 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4424 InvokeRuntimeCallingConvention calling_convention;
4425 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4426}
4427
4428void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4429 codegen_->InvokeRuntime(instruction->IsEnter()
4430 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4431 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004432 instruction->GetDexPc(),
4433 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004434}
4435
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004436void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4437void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4438void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4439
4440void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4441 LocationSummary* locations =
4442 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4443 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4444 || instruction->GetResultType() == Primitive::kPrimLong);
4445 locations->SetInAt(0, Location::RequiresRegister());
4446 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004448}
4449
4450void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4451 HandleBitwiseOperation(instruction);
4452}
4453
4454void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4455 HandleBitwiseOperation(instruction);
4456}
4457
4458void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4459 HandleBitwiseOperation(instruction);
4460}
4461
4462void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4463 LocationSummary* locations = instruction->GetLocations();
4464
4465 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004466 Register first = locations->InAt(0).AsRegister<Register>();
4467 Register second = locations->InAt(1).AsRegister<Register>();
4468 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004469 if (instruction->IsAnd()) {
4470 __ and_(out, first, ShifterOperand(second));
4471 } else if (instruction->IsOr()) {
4472 __ orr(out, first, ShifterOperand(second));
4473 } else {
4474 DCHECK(instruction->IsXor());
4475 __ eor(out, first, ShifterOperand(second));
4476 }
4477 } else {
4478 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4479 Location first = locations->InAt(0);
4480 Location second = locations->InAt(1);
4481 Location out = locations->Out();
4482 if (instruction->IsAnd()) {
4483 __ and_(out.AsRegisterPairLow<Register>(),
4484 first.AsRegisterPairLow<Register>(),
4485 ShifterOperand(second.AsRegisterPairLow<Register>()));
4486 __ and_(out.AsRegisterPairHigh<Register>(),
4487 first.AsRegisterPairHigh<Register>(),
4488 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4489 } else if (instruction->IsOr()) {
4490 __ orr(out.AsRegisterPairLow<Register>(),
4491 first.AsRegisterPairLow<Register>(),
4492 ShifterOperand(second.AsRegisterPairLow<Register>()));
4493 __ orr(out.AsRegisterPairHigh<Register>(),
4494 first.AsRegisterPairHigh<Register>(),
4495 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4496 } else {
4497 DCHECK(instruction->IsXor());
4498 __ eor(out.AsRegisterPairLow<Register>(),
4499 first.AsRegisterPairLow<Register>(),
4500 ShifterOperand(second.AsRegisterPairLow<Register>()));
4501 __ eor(out.AsRegisterPairHigh<Register>(),
4502 first.AsRegisterPairHigh<Register>(),
4503 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4504 }
4505 }
4506}
4507
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004508void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004509 // For better instruction scheduling we load the direct code pointer before the method pointer.
4510 bool direct_code_loaded = false;
4511 switch (invoke->GetCodePtrLocation()) {
4512 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4513 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
4514 break;
4515 }
4516 // Calls across dex files are more likely to exceed the available BL range,
4517 // so use absolute patch by falling through to kDirectCodeFixup.
4518 FALLTHROUGH_INTENDED;
4519 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4520 // LR = code address from literal pool with link-time patch.
4521 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4522 direct_code_loaded = true;
4523 break;
4524 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4525 // LR = invoke->GetDirectCodePtr();
4526 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
4527 direct_code_loaded = true;
4528 break;
4529 default:
4530 break;
4531 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004532
Vladimir Marko58155012015-08-19 12:49:41 +00004533 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4534 switch (invoke->GetMethodLoadKind()) {
4535 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4536 // temp = thread->string_init_entrypoint
4537 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
4538 break;
4539 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
4540 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4541 break;
4542 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4543 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4544 break;
4545 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4546 __ LoadLiteral(temp.AsRegister<Register>(),
4547 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4548 break;
4549 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
4550 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
4551 FALLTHROUGH_INTENDED;
4552 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
4553 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4554 Register method_reg;
4555 Register reg = temp.AsRegister<Register>();
4556 if (current_method.IsRegister()) {
4557 method_reg = current_method.AsRegister<Register>();
4558 } else {
4559 DCHECK(invoke->GetLocations()->Intrinsified());
4560 DCHECK(!current_method.IsValid());
4561 method_reg = reg;
4562 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
4563 }
4564 // temp = current_method->dex_cache_resolved_methods_;
4565 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01004566 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
4567 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00004568 // temp = temp[index_in_cache]
4569 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4570 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
4571 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01004572 }
Vladimir Marko58155012015-08-19 12:49:41 +00004573 }
4574
4575 switch (invoke->GetCodePtrLocation()) {
4576 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4577 __ bl(GetFrameEntryLabel());
4578 break;
4579 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4580 if (!direct_code_loaded) {
4581 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4582 __ Bind(&relative_call_patches_.back().label);
4583 Label label;
4584 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
4585 __ Bind(&label);
4586 break;
4587 }
4588 // If we loaded the direct code above, fall through.
4589 FALLTHROUGH_INTENDED;
4590 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4591 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4592 // LR prepared above for better instruction scheduling.
4593 DCHECK(direct_code_loaded);
4594 // LR()
4595 __ blx(LR);
4596 break;
4597 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4598 // LR = callee_method->entry_point_from_quick_compiled_code_
4599 __ LoadFromOffset(
4600 kLoadWord, LR, callee_method.AsRegister<Register>(),
4601 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
4602 // LR()
4603 __ blx(LR);
4604 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004605 }
4606
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004607 DCHECK(!IsLeafMethod());
4608}
4609
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004610void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
4611 Register temp = temp_location.AsRegister<Register>();
4612 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4613 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
4614 LocationSummary* locations = invoke->GetLocations();
4615 Location receiver = locations->InAt(0);
4616 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4617 // temp = object->GetClass();
4618 DCHECK(receiver.IsRegister());
4619 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4620 MaybeRecordImplicitNullCheck(invoke);
4621 __ MaybeUnpoisonHeapReference(temp);
4622 // temp = temp->GetMethodAt(method_offset);
4623 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4624 kArmWordSize).Int32Value();
4625 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4626 // LR = temp->GetEntryPoint();
4627 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
4628 // LR();
4629 __ blx(LR);
4630}
4631
Vladimir Marko58155012015-08-19 12:49:41 +00004632void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4633 DCHECK(linker_patches->empty());
4634 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
4635 linker_patches->reserve(size);
4636 for (const auto& entry : method_patches_) {
4637 const MethodReference& target_method = entry.first;
4638 Literal* literal = entry.second;
4639 DCHECK(literal->GetLabel()->IsBound());
4640 uint32_t literal_offset = literal->GetLabel()->Position();
4641 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4642 target_method.dex_file,
4643 target_method.dex_method_index));
4644 }
4645 for (const auto& entry : call_patches_) {
4646 const MethodReference& target_method = entry.first;
4647 Literal* literal = entry.second;
4648 DCHECK(literal->GetLabel()->IsBound());
4649 uint32_t literal_offset = literal->GetLabel()->Position();
4650 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
4651 target_method.dex_file,
4652 target_method.dex_method_index));
4653 }
4654 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
4655 uint32_t literal_offset = info.label.Position();
4656 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4657 info.target_method.dex_file,
4658 info.target_method.dex_method_index));
4659 }
4660}
4661
4662Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
4663 MethodToLiteralMap* map) {
4664 // Look up the literal for target_method.
4665 auto lb = map->lower_bound(target_method);
4666 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
4667 return lb->second;
4668 }
4669 // We don't have a literal for this method yet, insert a new one.
4670 Literal* literal = __ NewLiteral<uint32_t>(0u);
4671 map->PutBefore(lb, target_method, literal);
4672 return literal;
4673}
4674
4675Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
4676 return DeduplicateMethodLiteral(target_method, &method_patches_);
4677}
4678
4679Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
4680 return DeduplicateMethodLiteral(target_method, &call_patches_);
4681}
4682
Calin Juravleb1498f62015-02-16 13:13:29 +00004683void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
4684 // Nothing to do, this should be removed during prepare for register allocator.
4685 UNUSED(instruction);
4686 LOG(FATAL) << "Unreachable";
4687}
4688
4689void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
4690 // Nothing to do, this should be removed during prepare for register allocator.
4691 UNUSED(instruction);
4692 LOG(FATAL) << "Unreachable";
4693}
4694
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004695void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
4696 DCHECK(codegen_->IsBaseline());
4697 LocationSummary* locations =
4698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4699 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4700}
4701
4702void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4703 DCHECK(codegen_->IsBaseline());
4704 // Will be generated at use site.
4705}
4706
Roland Levillain4d027112015-07-01 15:41:14 +01004707#undef __
4708#undef QUICK_ENTRY_POINT
4709
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004710} // namespace arm
4711} // namespace art