blob: c3d63b9952987501d6bd73391e83f72990efc15c [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037namespace arm {
38
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000039static bool ExpectedPairLayout(Location location) {
40 // We expected this for both core and fpu register pairs.
41 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
42}
43
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000047// We unconditionally allocate R5 to ensure we can do long operations
48// with baseline.
49static constexpr Register kCoreSavedRegisterForBaseline = R5;
50static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070051 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000052static constexpr SRegister kFpuCalleeSaves[] =
53 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000055// D31 cannot be split into two S registers, and the register allocator only works on
56// S registers. Therefore there is no need to block it.
57static constexpr DRegister DTMP = D31;
58
Roland Levillain62a46b22015-06-01 18:24:13 +010059#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010060#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010064 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Alexandre Rames67555f72014-11-18 10:55:16 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010067 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000069 if (instruction_->CanThrowIntoCatchBlock()) {
70 // Live registers will be restored in the catch block if caught.
71 SaveLiveRegisters(codegen, instruction_->GetLocations());
72 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010073 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000074 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 }
76
Alexandre Rames8158f282015-08-07 10:26:17 +010077 bool IsFatal() const OVERRIDE { return true; }
78
Alexandre Rames9931f312015-06-19 14:47:01 +010079 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
80
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010082 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
84};
85
Calin Juravled0d48522014-11-04 16:40:20 +000086class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
87 public:
88 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
89
Alexandre Rames67555f72014-11-18 10:55:16 +000090 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000091 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
92 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000093 if (instruction_->CanThrowIntoCatchBlock()) {
94 // Live registers will be restored in the catch block if caught.
95 SaveLiveRegisters(codegen, instruction_->GetLocations());
96 }
Calin Juravled0d48522014-11-04 16:40:20 +000097 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000098 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Calin Juravled0d48522014-11-04 16:40:20 +000099 }
100
Alexandre Rames8158f282015-08-07 10:26:17 +0100101 bool IsFatal() const OVERRIDE { return true; }
102
Alexandre Rames9931f312015-06-19 14:47:01 +0100103 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
104
Calin Juravled0d48522014-11-04 16:40:20 +0000105 private:
106 HDivZeroCheck* const instruction_;
107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
108};
109
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100110class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000111 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000112 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100113 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000114
Alexandre Rames67555f72014-11-18 10:55:16 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100116 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000117 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000118 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100119 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000120 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000121 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100122 if (successor_ == nullptr) {
123 __ b(GetReturnLabel());
124 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100125 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100126 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000127 }
128
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 Label* GetReturnLabel() {
130 DCHECK(successor_ == nullptr);
131 return &return_label_;
132 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100134 HBasicBlock* GetSuccessor() const {
135 return successor_;
136 }
137
Alexandre Rames9931f312015-06-19 14:47:01 +0100138 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
139
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 private:
141 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 // If not null, the block to branch to after the suspend check.
143 HBasicBlock* const successor_;
144
145 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 Label return_label_;
147
148 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
149};
150
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100153 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
154 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155
Alexandre Rames67555f72014-11-18 10:55:16 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100157 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 LocationSummary* locations = instruction_->GetLocations();
159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000168 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100169 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100171 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100172 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100173 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
174 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100175 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000176 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 }
178
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 bool IsFatal() const OVERRIDE { return true; }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185
186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
187};
188
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000189class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000191 LoadClassSlowPathARM(HLoadClass* cls,
192 HInstruction* at,
193 uint32_t dex_pc,
194 bool do_clinit)
195 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
196 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
197 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198
Alexandre Rames67555f72014-11-18 10:55:16 +0000199 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000200 LocationSummary* locations = at_->GetLocations();
201
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100202 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
203 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000204 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100206 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 int32_t entry_point_offset = do_clinit_
209 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
210 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000214 Location out = locations->Out();
215 if (out.IsValid()) {
216 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
218 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000219 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220 __ b(GetExitLabel());
221 }
222
Alexandre Rames9931f312015-06-19 14:47:01 +0100223 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
224
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 // The class this slow path will load.
227 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 // The instruction where this slow path is happening.
230 // (Might be the load class or an initialization check).
231 HInstruction* const at_;
232
233 // The dex PC of `at_`.
234 const uint32_t dex_pc_;
235
236 // Whether to initialize the class.
237 const bool do_clinit_;
238
239 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240};
241
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000242class LoadStringSlowPathARM : public SlowPathCodeARM {
243 public:
244 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
245
Alexandre Rames67555f72014-11-18 10:55:16 +0000246 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 LocationSummary* locations = instruction_->GetLocations();
248 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
249
250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
251 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253
254 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000257 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
259
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000260 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000261 __ b(GetExitLabel());
262 }
263
Alexandre Rames9931f312015-06-19 14:47:01 +0100264 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
265
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000266 private:
267 HLoadString* const instruction_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
270};
271
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272class TypeCheckSlowPathARM : public SlowPathCodeARM {
273 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100274 explicit TypeCheckSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100278 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
279 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 DCHECK(instruction_->IsCheckCast()
281 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000282
283 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
284 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286
287 // We're moving two locations to locations that could overlap, so we need a parallel
288 // move resolver.
289 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000290 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100291 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000292 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100293 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100294 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100295 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
296 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000298 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100299 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
300 instruction_,
301 instruction_->GetDexPc(),
302 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000303 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
304 } else {
305 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
307 instruction_,
308 instruction_->GetDexPc(),
309 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313 __ b(GetExitLabel());
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
317
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
321 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
322};
323
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700324class DeoptimizationSlowPathARM : public SlowPathCodeARM {
325 public:
326 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
327 : instruction_(instruction) {}
328
329 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
330 __ Bind(GetEntryLabel());
331 SaveLiveRegisters(codegen, instruction_->GetLocations());
332 DCHECK(instruction_->IsDeoptimize());
333 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
334 uint32_t dex_pc = deoptimize->GetDexPc();
335 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
336 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
337 }
338
Alexandre Rames9931f312015-06-19 14:47:01 +0100339 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
340
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700341 private:
342 HInstruction* const instruction_;
343 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
344};
345
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000346#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100347#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700348
Roland Levillain4fa13f62015-07-06 18:11:54 +0100349inline Condition ARMSignedOrFPCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700350 switch (cond) {
351 case kCondEQ: return EQ;
352 case kCondNE: return NE;
353 case kCondLT: return LT;
354 case kCondLE: return LE;
355 case kCondGT: return GT;
356 case kCondGE: return GE;
Dave Allison20dfc792014-06-16 20:44:29 -0700357 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100358 LOG(FATAL) << "Unreachable";
359 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700360}
361
Roland Levillain4fa13f62015-07-06 18:11:54 +0100362inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700363 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100364 case kCondEQ: return EQ;
365 case kCondNE: return NE;
366 case kCondLT: return LO;
367 case kCondLE: return LS;
368 case kCondGT: return HI;
369 case kCondGE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700370 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100371 LOG(FATAL) << "Unreachable";
372 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700373}
374
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100375void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100376 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100377}
378
379void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100380 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100381}
382
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100383size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
384 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
385 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100386}
387
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100388size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
389 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
390 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100391}
392
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000393size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
394 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
395 return kArmWordSize;
396}
397
398size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
399 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
400 return kArmWordSize;
401}
402
Calin Juravle34166012014-12-19 17:22:29 +0000403CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000404 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100405 const CompilerOptions& compiler_options,
406 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000407 : CodeGenerator(graph,
408 kNumberOfCoreRegisters,
409 kNumberOfSRegisters,
410 kNumberOfRegisterPairs,
411 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
412 arraysize(kCoreCalleeSaves)),
413 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
414 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100415 compiler_options,
416 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100417 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100419 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100420 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000421 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000422 isa_features_(isa_features),
423 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
424 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
425 relative_call_patches_(graph->GetArena()->Adapter()) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700426 // Always save the LR register to mimic Quick.
427 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100428}
429
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000430void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
431 // Ensure that we fix up branches and literal loads and emit the literal pool.
432 __ FinalizeCode();
433
434 // Adjust native pc offsets in stack maps.
435 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
436 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
437 uint32_t new_position = __ GetAdjustedPosition(old_position);
438 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
439 }
440 // Adjust native pc offsets of block labels.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100441 for (HBasicBlock* block : *block_order_) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000442 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
443 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
444 DCHECK_LT(static_cast<size_t>(block->GetBlockId()), block_labels_.Size());
445 Label* block_label = &block_labels_.GetRawStorage()[block->GetBlockId()];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000446 DCHECK_EQ(block_label->IsBound(), !block->IsSingleJump());
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000447 if (block_label->IsBound()) {
448 __ AdjustLabelPosition(block_label);
449 }
450 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100451 // Adjust pc offsets for the disassembly information.
452 if (disasm_info_ != nullptr) {
453 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
454 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
455 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
456 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
457 it.second.start = __ GetAdjustedPosition(it.second.start);
458 it.second.end = __ GetAdjustedPosition(it.second.end);
459 }
460 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
461 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
462 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
463 }
464 }
Vladimir Marko58155012015-08-19 12:49:41 +0000465 // Adjust pc offsets for relative call patches.
466 for (MethodPatchInfo<Label>& info : relative_call_patches_) {
467 __ AdjustLabelPosition(&info.label);
468 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000469
470 CodeGenerator::Finalize(allocator);
471}
472
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100473Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474 switch (type) {
475 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100476 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100477 ArmManagedRegister pair =
478 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100479 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
480 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
481
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100482 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
483 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100484 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100485 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100486 }
487
488 case Primitive::kPrimByte:
489 case Primitive::kPrimBoolean:
490 case Primitive::kPrimChar:
491 case Primitive::kPrimShort:
492 case Primitive::kPrimInt:
493 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100494 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100495 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100496 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
497 ArmManagedRegister current =
498 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
499 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100500 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100501 }
502 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100503 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504 }
505
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000506 case Primitive::kPrimFloat: {
507 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100508 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100509 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100510
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000511 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000512 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
513 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000514 return Location::FpuRegisterPairLocation(reg, reg + 1);
515 }
516
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100517 case Primitive::kPrimVoid:
518 LOG(FATAL) << "Unreachable type " << type;
519 }
520
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100521 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100522}
523
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000524void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100525 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100526 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100527
528 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100529 blocked_core_registers_[SP] = true;
530 blocked_core_registers_[LR] = true;
531 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100532
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100533 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100534 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100536 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100537 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100538
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000539 if (is_baseline) {
540 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
541 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
542 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000543
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000544 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
545
546 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
547 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
548 }
549 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100550
551 UpdateBlockedPairRegisters();
552}
553
554void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
555 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
556 ArmManagedRegister current =
557 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
558 if (blocked_core_registers_[current.AsRegisterPairLow()]
559 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
560 blocked_register_pairs_[i] = true;
561 }
562 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100563}
564
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100565InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
566 : HGraphVisitor(graph),
567 assembler_(codegen->GetAssembler()),
568 codegen_(codegen) {}
569
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000570void CodeGeneratorARM::ComputeSpillMask() {
571 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000572 // Save one extra register for baseline. Note that on thumb2, there is no easy
573 // instruction to restore just the PC, so this actually helps both baseline
574 // and non-baseline to save and restore at least two registers at entry and exit.
575 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000576 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
577 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
578 // We use vpush and vpop for saving and restoring floating point registers, which take
579 // a SRegister and the number of registers to save/restore after that SRegister. We
580 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
581 // but in the range.
582 if (fpu_spill_mask_ != 0) {
583 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
584 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
585 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
586 fpu_spill_mask_ |= (1 << i);
587 }
588 }
589}
590
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100591static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100592 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100593}
594
595static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100596 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100597}
598
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000599void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000600 bool skip_overflow_check =
601 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000602 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000603 __ Bind(&frame_entry_label_);
604
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000605 if (HasEmptyFrame()) {
606 return;
607 }
608
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100609 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000610 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
611 __ LoadFromOffset(kLoadWord, IP, IP, 0);
612 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100613 }
614
Andreas Gampe501fd632015-09-10 16:11:06 -0700615 __ PushList(core_spill_mask_);
616 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
617 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000618 if (fpu_spill_mask_ != 0) {
619 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
620 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100621 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100622 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000623 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100624 int adjust = GetFrameSize() - FrameEntrySpillSize();
625 __ AddConstant(SP, -adjust);
626 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100627 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000628}
629
630void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000631 if (HasEmptyFrame()) {
632 __ bx(LR);
633 return;
634 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100635 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100636 int adjust = GetFrameSize() - FrameEntrySpillSize();
637 __ AddConstant(SP, adjust);
638 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000639 if (fpu_spill_mask_ != 0) {
640 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
641 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100642 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
643 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000644 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700645 // Pop LR into PC to return.
646 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
647 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
648 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100649 __ cfi().RestoreState();
650 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000651}
652
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100653void CodeGeneratorARM::Bind(HBasicBlock* block) {
654 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000655}
656
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100657Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
658 switch (load->GetType()) {
659 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100661 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100662
663 case Primitive::kPrimInt:
664 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100665 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100667
668 case Primitive::kPrimBoolean:
669 case Primitive::kPrimByte:
670 case Primitive::kPrimChar:
671 case Primitive::kPrimShort:
672 case Primitive::kPrimVoid:
673 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700674 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100675 }
676
677 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700678 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100679}
680
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100681Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100682 switch (type) {
683 case Primitive::kPrimBoolean:
684 case Primitive::kPrimByte:
685 case Primitive::kPrimChar:
686 case Primitive::kPrimShort:
687 case Primitive::kPrimInt:
688 case Primitive::kPrimNot: {
689 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000690 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100691 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100692 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100693 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000694 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100695 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100696 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100697
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000698 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100699 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000700 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100701 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000702 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100703 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000704 if (calling_convention.GetRegisterAt(index) == R1) {
705 // Skip R1, and use R2_R3 instead.
706 gp_index_++;
707 index++;
708 }
709 }
710 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
711 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000712 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000713 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000714 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100715 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000716 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
717 }
718 }
719
720 case Primitive::kPrimFloat: {
721 uint32_t stack_index = stack_index_++;
722 if (float_index_ % 2 == 0) {
723 float_index_ = std::max(double_index_, float_index_);
724 }
725 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
726 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
727 } else {
728 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
729 }
730 }
731
732 case Primitive::kPrimDouble: {
733 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
734 uint32_t stack_index = stack_index_;
735 stack_index_ += 2;
736 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
737 uint32_t index = double_index_;
738 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000739 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000740 calling_convention.GetFpuRegisterAt(index),
741 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000742 DCHECK(ExpectedPairLayout(result));
743 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000744 } else {
745 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100746 }
747 }
748
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100749 case Primitive::kPrimVoid:
750 LOG(FATAL) << "Unexpected parameter type " << type;
751 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100752 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100753 return Location();
754}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100755
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100756Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000757 switch (type) {
758 case Primitive::kPrimBoolean:
759 case Primitive::kPrimByte:
760 case Primitive::kPrimChar:
761 case Primitive::kPrimShort:
762 case Primitive::kPrimInt:
763 case Primitive::kPrimNot: {
764 return Location::RegisterLocation(R0);
765 }
766
767 case Primitive::kPrimFloat: {
768 return Location::FpuRegisterLocation(S0);
769 }
770
771 case Primitive::kPrimLong: {
772 return Location::RegisterPairLocation(R0, R1);
773 }
774
775 case Primitive::kPrimDouble: {
776 return Location::FpuRegisterPairLocation(S0, S1);
777 }
778
779 case Primitive::kPrimVoid:
780 return Location();
781 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100782
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000783 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000784}
785
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100786Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
787 return Location::RegisterLocation(kMethodRegisterArgument);
788}
789
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100790void CodeGeneratorARM::Move32(Location destination, Location source) {
791 if (source.Equals(destination)) {
792 return;
793 }
794 if (destination.IsRegister()) {
795 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000796 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100797 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000798 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100799 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000800 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100801 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100802 } else if (destination.IsFpuRegister()) {
803 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000804 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100805 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000806 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100807 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000808 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100810 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000811 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100812 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000813 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100814 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000815 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100816 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000817 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
819 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100820 }
821 }
822}
823
824void CodeGeneratorARM::Move64(Location destination, Location source) {
825 if (source.Equals(destination)) {
826 return;
827 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100828 if (destination.IsRegisterPair()) {
829 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000830 EmitParallelMoves(
831 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
832 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100833 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000834 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100835 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
836 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100837 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000838 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100839 } else {
840 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000841 DCHECK(ExpectedPairLayout(destination));
842 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
843 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000845 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100846 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000847 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
848 SP,
849 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100850 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000851 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100852 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100853 } else {
854 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100855 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000856 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100857 if (source.AsRegisterPairLow<Register>() == R1) {
858 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100859 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
860 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100861 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100862 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863 SP, destination.GetStackIndex());
864 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000865 } else if (source.IsFpuRegisterPair()) {
866 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
867 SP,
868 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100869 } else {
870 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000871 EmitParallelMoves(
872 Location::StackSlot(source.GetStackIndex()),
873 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100874 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000875 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100876 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
877 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100878 }
879 }
880}
881
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100882void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100883 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100884 if (instruction->IsCurrentMethod()) {
885 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
886 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100887 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100888 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000889 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000890 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
891 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000892 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000893 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000894 } else {
895 DCHECK(location.IsStackSlot());
896 __ LoadImmediate(IP, value);
897 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
898 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000899 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000900 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000901 int64_t value = const_to_move->AsLongConstant()->GetValue();
902 if (location.IsRegisterPair()) {
903 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
904 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
905 } else {
906 DCHECK(location.IsDoubleStackSlot());
907 __ LoadImmediate(IP, Low32Bits(value));
908 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
909 __ LoadImmediate(IP, High32Bits(value));
910 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
911 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100912 }
Roland Levillain476df552014-10-09 17:51:36 +0100913 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
915 switch (instruction->GetType()) {
916 case Primitive::kPrimBoolean:
917 case Primitive::kPrimByte:
918 case Primitive::kPrimChar:
919 case Primitive::kPrimShort:
920 case Primitive::kPrimInt:
921 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100922 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100923 Move32(location, Location::StackSlot(stack_slot));
924 break;
925
926 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100927 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100928 Move64(location, Location::DoubleStackSlot(stack_slot));
929 break;
930
931 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100932 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100933 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000934 } else if (instruction->IsTemporary()) {
935 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000936 if (temp_location.IsStackSlot()) {
937 Move32(location, temp_location);
938 } else {
939 DCHECK(temp_location.IsDoubleStackSlot());
940 Move64(location, temp_location);
941 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000942 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100943 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100944 switch (instruction->GetType()) {
945 case Primitive::kPrimBoolean:
946 case Primitive::kPrimByte:
947 case Primitive::kPrimChar:
948 case Primitive::kPrimShort:
949 case Primitive::kPrimNot:
950 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100951 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100952 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100953 break;
954
955 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100956 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100957 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100958 break;
959
960 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100962 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000963 }
964}
965
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100966void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
967 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000968 uint32_t dex_pc,
969 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100970 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100971 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
972 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000973 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100974}
975
David Brazdilfc6a86a2015-06-26 10:33:45 +0000976void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100977 DCHECK(!successor->IsExitBlock());
978
979 HBasicBlock* block = got->GetBlock();
980 HInstruction* previous = got->GetPrevious();
981
982 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000983 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100984 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
985 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
986 return;
987 }
988
989 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
990 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
991 }
992 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000993 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000994 }
995}
996
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997void LocationsBuilderARM::VisitGoto(HGoto* got) {
998 got->SetLocations(nullptr);
999}
1000
1001void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1002 HandleGoto(got, got->GetSuccessor());
1003}
1004
1005void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1006 try_boundary->SetLocations(nullptr);
1007}
1008
1009void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1010 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1011 if (!successor->IsExitBlock()) {
1012 HandleGoto(try_boundary, successor);
1013 }
1014}
1015
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001016void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001017 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001018}
1019
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001020void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001021 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001022}
1023
Roland Levillain4fa13f62015-07-06 18:11:54 +01001024void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1025 ShifterOperand operand;
1026 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1027 __ cmp(left, operand);
1028 } else {
1029 Register temp = IP;
1030 __ LoadImmediate(temp, right);
1031 __ cmp(left, ShifterOperand(temp));
1032 }
1033}
1034
1035void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1036 Label* true_label,
1037 Label* false_label) {
1038 __ vmstat(); // transfer FP status register to ARM APSR.
1039 if (cond->IsFPConditionTrueIfNaN()) {
1040 __ b(true_label, VS); // VS for unordered.
1041 } else if (cond->IsFPConditionFalseIfNaN()) {
1042 __ b(false_label, VS); // VS for unordered.
1043 }
1044 __ b(true_label, ARMSignedOrFPCondition(cond->GetCondition()));
1045}
1046
1047void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1048 Label* true_label,
1049 Label* false_label) {
1050 LocationSummary* locations = cond->GetLocations();
1051 Location left = locations->InAt(0);
1052 Location right = locations->InAt(1);
1053 IfCondition if_cond = cond->GetCondition();
1054
1055 Register left_high = left.AsRegisterPairHigh<Register>();
1056 Register left_low = left.AsRegisterPairLow<Register>();
1057 IfCondition true_high_cond = if_cond;
1058 IfCondition false_high_cond = cond->GetOppositeCondition();
1059 Condition final_condition = ARMUnsignedCondition(if_cond);
1060
1061 // Set the conditions for the test, remembering that == needs to be
1062 // decided using the low words.
1063 switch (if_cond) {
1064 case kCondEQ:
1065 case kCondNE:
1066 // Nothing to do.
1067 break;
1068 case kCondLT:
1069 false_high_cond = kCondGT;
1070 break;
1071 case kCondLE:
1072 true_high_cond = kCondLT;
1073 break;
1074 case kCondGT:
1075 false_high_cond = kCondLT;
1076 break;
1077 case kCondGE:
1078 true_high_cond = kCondGT;
1079 break;
1080 }
1081 if (right.IsConstant()) {
1082 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1083 int32_t val_low = Low32Bits(value);
1084 int32_t val_high = High32Bits(value);
1085
1086 GenerateCompareWithImmediate(left_high, val_high);
1087 if (if_cond == kCondNE) {
1088 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1089 } else if (if_cond == kCondEQ) {
1090 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1091 } else {
1092 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1093 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1094 }
1095 // Must be equal high, so compare the lows.
1096 GenerateCompareWithImmediate(left_low, val_low);
1097 } else {
1098 Register right_high = right.AsRegisterPairHigh<Register>();
1099 Register right_low = right.AsRegisterPairLow<Register>();
1100
1101 __ cmp(left_high, ShifterOperand(right_high));
1102 if (if_cond == kCondNE) {
1103 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1104 } else if (if_cond == kCondEQ) {
1105 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1106 } else {
1107 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1108 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1109 }
1110 // Must be equal high, so compare the lows.
1111 __ cmp(left_low, ShifterOperand(right_low));
1112 }
1113 // The last comparison might be unsigned.
1114 __ b(true_label, final_condition);
1115}
1116
1117void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1118 HCondition* condition,
1119 Label* true_target,
1120 Label* false_target,
1121 Label* always_true_target) {
1122 LocationSummary* locations = condition->GetLocations();
1123 Location left = locations->InAt(0);
1124 Location right = locations->InAt(1);
1125
1126 // We don't want true_target as a nullptr.
1127 if (true_target == nullptr) {
1128 true_target = always_true_target;
1129 }
1130 bool falls_through = (false_target == nullptr);
1131
1132 // FP compares don't like null false_targets.
1133 if (false_target == nullptr) {
1134 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1135 }
1136
1137 Primitive::Type type = condition->InputAt(0)->GetType();
1138 switch (type) {
1139 case Primitive::kPrimLong:
1140 GenerateLongComparesAndJumps(condition, true_target, false_target);
1141 break;
1142 case Primitive::kPrimFloat:
1143 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1144 GenerateFPJumps(condition, true_target, false_target);
1145 break;
1146 case Primitive::kPrimDouble:
1147 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1148 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1149 GenerateFPJumps(condition, true_target, false_target);
1150 break;
1151 default:
1152 LOG(FATAL) << "Unexpected compare type " << type;
1153 }
1154
1155 if (!falls_through) {
1156 __ b(false_target);
1157 }
1158}
1159
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001160void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1161 Label* true_target,
1162 Label* false_target,
1163 Label* always_true_target) {
1164 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001165 if (cond->IsIntConstant()) {
1166 // Constant condition, statically compared against 1.
1167 int32_t cond_value = cond->AsIntConstant()->GetValue();
1168 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001169 if (always_true_target != nullptr) {
1170 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001171 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001172 return;
1173 } else {
1174 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001175 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001176 } else {
1177 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1178 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001179 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001180 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1181 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001182 } else {
1183 // Condition has not been materialized, use its inputs as the
1184 // comparison and its condition as the branch condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001185 Primitive::Type type =
1186 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
1187 // Is this a long or FP comparison that has been folded into the HCondition?
1188 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1189 // Generate the comparison directly.
1190 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1191 true_target, false_target, always_true_target);
1192 return;
1193 }
1194
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001195 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001196 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001197 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001198 Location right = locations->InAt(1);
1199 if (right.IsRegister()) {
1200 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001201 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001202 DCHECK(right.IsConstant());
1203 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001204 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001205 __ b(true_target, ARMSignedOrFPCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001206 }
Dave Allison20dfc792014-06-16 20:44:29 -07001207 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001208 if (false_target != nullptr) {
1209 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001210 }
1211}
1212
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001213void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1214 LocationSummary* locations =
1215 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1216 HInstruction* cond = if_instr->InputAt(0);
1217 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1218 locations->SetInAt(0, Location::RequiresRegister());
1219 }
1220}
1221
1222void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1223 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1224 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1225 Label* always_true_target = true_target;
1226 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1227 if_instr->IfTrueSuccessor())) {
1228 always_true_target = nullptr;
1229 }
1230 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1231 if_instr->IfFalseSuccessor())) {
1232 false_target = nullptr;
1233 }
1234 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1235}
1236
1237void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1238 LocationSummary* locations = new (GetGraph()->GetArena())
1239 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1240 HInstruction* cond = deoptimize->InputAt(0);
1241 DCHECK(cond->IsCondition());
1242 if (cond->AsCondition()->NeedsMaterialization()) {
1243 locations->SetInAt(0, Location::RequiresRegister());
1244 }
1245}
1246
1247void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1248 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
1249 DeoptimizationSlowPathARM(deoptimize);
1250 codegen_->AddSlowPath(slow_path);
1251 Label* slow_path_entry = slow_path->GetEntryLabel();
1252 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1253}
Dave Allison20dfc792014-06-16 20:44:29 -07001254
Roland Levillain0d37cd02015-05-27 16:39:19 +01001255void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001256 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001257 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001258 // Handle the long/FP comparisons made in instruction simplification.
1259 switch (cond->InputAt(0)->GetType()) {
1260 case Primitive::kPrimLong:
1261 locations->SetInAt(0, Location::RequiresRegister());
1262 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1263 if (cond->NeedsMaterialization()) {
1264 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1265 }
1266 break;
1267
1268 case Primitive::kPrimFloat:
1269 case Primitive::kPrimDouble:
1270 locations->SetInAt(0, Location::RequiresFpuRegister());
1271 locations->SetInAt(1, Location::RequiresFpuRegister());
1272 if (cond->NeedsMaterialization()) {
1273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1274 }
1275 break;
1276
1277 default:
1278 locations->SetInAt(0, Location::RequiresRegister());
1279 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1280 if (cond->NeedsMaterialization()) {
1281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1282 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001283 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001284}
1285
Roland Levillain0d37cd02015-05-27 16:39:19 +01001286void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001287 if (!cond->NeedsMaterialization()) {
1288 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001289 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001290
1291 LocationSummary* locations = cond->GetLocations();
1292 Location left = locations->InAt(0);
1293 Location right = locations->InAt(1);
1294 Register out = locations->Out().AsRegister<Register>();
1295 Label true_label, false_label;
1296
1297 switch (cond->InputAt(0)->GetType()) {
1298 default: {
1299 // Integer case.
1300 if (right.IsRegister()) {
1301 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1302 } else {
1303 DCHECK(right.IsConstant());
1304 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1305 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1306 }
1307 __ it(ARMSignedOrFPCondition(cond->GetCondition()), kItElse);
1308 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
1309 ARMSignedOrFPCondition(cond->GetCondition()));
1310 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
1311 ARMSignedOrFPCondition(cond->GetOppositeCondition()));
1312 return;
1313 }
1314 case Primitive::kPrimLong:
1315 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1316 break;
1317 case Primitive::kPrimFloat:
1318 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1319 GenerateFPJumps(cond, &true_label, &false_label);
1320 break;
1321 case Primitive::kPrimDouble:
1322 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1323 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1324 GenerateFPJumps(cond, &true_label, &false_label);
1325 break;
1326 }
1327
1328 // Convert the jumps into the result.
1329 Label done_label;
1330
1331 // False case: result = 0.
1332 __ Bind(&false_label);
1333 __ LoadImmediate(out, 0);
1334 __ b(&done_label);
1335
1336 // True case: result = 1.
1337 __ Bind(&true_label);
1338 __ LoadImmediate(out, 1);
1339 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001340}
1341
1342void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1343 VisitCondition(comp);
1344}
1345
1346void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1347 VisitCondition(comp);
1348}
1349
1350void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1351 VisitCondition(comp);
1352}
1353
1354void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1355 VisitCondition(comp);
1356}
1357
1358void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1359 VisitCondition(comp);
1360}
1361
1362void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1363 VisitCondition(comp);
1364}
1365
1366void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1367 VisitCondition(comp);
1368}
1369
1370void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1371 VisitCondition(comp);
1372}
1373
1374void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1375 VisitCondition(comp);
1376}
1377
1378void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1379 VisitCondition(comp);
1380}
1381
1382void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1383 VisitCondition(comp);
1384}
1385
1386void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1387 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001388}
1389
1390void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001391 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392}
1393
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001394void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1395 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396}
1397
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001398void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001399 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001400}
1401
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001402void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001403 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001404 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001405}
1406
1407void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001408 LocationSummary* locations =
1409 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001410 switch (store->InputAt(1)->GetType()) {
1411 case Primitive::kPrimBoolean:
1412 case Primitive::kPrimByte:
1413 case Primitive::kPrimChar:
1414 case Primitive::kPrimShort:
1415 case Primitive::kPrimInt:
1416 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001417 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001418 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1419 break;
1420
1421 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001422 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001423 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1424 break;
1425
1426 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001427 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001428 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001429}
1430
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001431void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001432 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001433}
1434
1435void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001436 LocationSummary* locations =
1437 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001438 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001439}
1440
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001441void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001442 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001443 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001444}
1445
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001446void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1447 LocationSummary* locations =
1448 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1449 locations->SetOut(Location::ConstantLocation(constant));
1450}
1451
1452void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1453 // Will be generated at use site.
1454 UNUSED(constant);
1455}
1456
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001457void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001458 LocationSummary* locations =
1459 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001460 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001461}
1462
1463void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1464 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001465 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001466}
1467
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001468void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1469 LocationSummary* locations =
1470 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1471 locations->SetOut(Location::ConstantLocation(constant));
1472}
1473
1474void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1475 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001476 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001477}
1478
1479void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1480 LocationSummary* locations =
1481 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1482 locations->SetOut(Location::ConstantLocation(constant));
1483}
1484
1485void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1486 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001487 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001488}
1489
Calin Juravle27df7582015-04-17 19:12:31 +01001490void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1491 memory_barrier->SetLocations(nullptr);
1492}
1493
1494void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1495 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1496}
1497
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001498void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001499 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500}
1501
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001502void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001503 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001504 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001505}
1506
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001507void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001508 LocationSummary* locations =
1509 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001510 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001511}
1512
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001513void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001514 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001515 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001516}
1517
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001518void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001519 // When we do not run baseline, explicit clinit checks triggered by static
1520 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1521 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001522
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001523 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1524 codegen_->GetInstructionSetFeatures());
1525 if (intrinsic.TryDispatch(invoke)) {
1526 return;
1527 }
1528
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001529 HandleInvoke(invoke);
1530}
1531
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001532static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1533 if (invoke->GetLocations()->Intrinsified()) {
1534 IntrinsicCodeGeneratorARM intrinsic(codegen);
1535 intrinsic.Dispatch(invoke);
1536 return true;
1537 }
1538 return false;
1539}
1540
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001541void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001542 // When we do not run baseline, explicit clinit checks triggered by static
1543 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1544 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001545
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001546 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1547 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001548 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001549
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001550 LocationSummary* locations = invoke->GetLocations();
1551 codegen_->GenerateStaticOrDirectCall(
1552 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001553 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001554}
1555
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001556void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001557 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001558 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001559}
1560
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001561void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001562 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1563 codegen_->GetInstructionSetFeatures());
1564 if (intrinsic.TryDispatch(invoke)) {
1565 return;
1566 }
1567
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001568 HandleInvoke(invoke);
1569}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001570
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001571void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001572 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1573 return;
1574 }
1575
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001576 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001577 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001578 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001579}
1580
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001581void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1582 HandleInvoke(invoke);
1583 // Add the hidden argument.
1584 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1585}
1586
1587void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1588 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001589 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001590 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1591 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001592 LocationSummary* locations = invoke->GetLocations();
1593 Location receiver = locations->InAt(0);
1594 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1595
1596 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001597 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1598 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001599
1600 // temp = object->GetClass();
1601 if (receiver.IsStackSlot()) {
1602 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1603 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1604 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001605 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001606 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001607 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001608 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001609 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001610 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001611 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001612 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1613 // LR = temp->GetEntryPoint();
1614 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1615 // LR();
1616 __ blx(LR);
1617 DCHECK(!codegen_->IsLeafMethod());
1618 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1619}
1620
Roland Levillain88cb1752014-10-20 16:36:47 +01001621void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1622 LocationSummary* locations =
1623 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1624 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001625 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001626 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1628 break;
1629 }
1630 case Primitive::kPrimLong: {
1631 locations->SetInAt(0, Location::RequiresRegister());
1632 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001633 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001634 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001635
Roland Levillain88cb1752014-10-20 16:36:47 +01001636 case Primitive::kPrimFloat:
1637 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001638 locations->SetInAt(0, Location::RequiresFpuRegister());
1639 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001640 break;
1641
1642 default:
1643 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1644 }
1645}
1646
1647void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1648 LocationSummary* locations = neg->GetLocations();
1649 Location out = locations->Out();
1650 Location in = locations->InAt(0);
1651 switch (neg->GetResultType()) {
1652 case Primitive::kPrimInt:
1653 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001654 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001655 break;
1656
1657 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001658 DCHECK(in.IsRegisterPair());
1659 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1660 __ rsbs(out.AsRegisterPairLow<Register>(),
1661 in.AsRegisterPairLow<Register>(),
1662 ShifterOperand(0));
1663 // We cannot emit an RSC (Reverse Subtract with Carry)
1664 // instruction here, as it does not exist in the Thumb-2
1665 // instruction set. We use the following approach
1666 // using SBC and SUB instead.
1667 //
1668 // out.hi = -C
1669 __ sbc(out.AsRegisterPairHigh<Register>(),
1670 out.AsRegisterPairHigh<Register>(),
1671 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1672 // out.hi = out.hi - in.hi
1673 __ sub(out.AsRegisterPairHigh<Register>(),
1674 out.AsRegisterPairHigh<Register>(),
1675 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1676 break;
1677
Roland Levillain88cb1752014-10-20 16:36:47 +01001678 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001679 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001681 break;
1682
Roland Levillain88cb1752014-10-20 16:36:47 +01001683 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001684 DCHECK(in.IsFpuRegisterPair());
1685 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1686 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001687 break;
1688
1689 default:
1690 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1691 }
1692}
1693
Roland Levillaindff1f282014-11-05 14:15:05 +00001694void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001695 Primitive::Type result_type = conversion->GetResultType();
1696 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001697 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001698
Roland Levillain5b3ee562015-04-14 16:02:41 +01001699 // The float-to-long, double-to-long and long-to-float type conversions
1700 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001701 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001702 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1703 && result_type == Primitive::kPrimLong)
1704 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001705 ? LocationSummary::kCall
1706 : LocationSummary::kNoCall;
1707 LocationSummary* locations =
1708 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1709
David Brazdilb2bd1c52015-03-25 11:17:37 +00001710 // The Java language does not allow treating boolean as an integral type but
1711 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001712
Roland Levillaindff1f282014-11-05 14:15:05 +00001713 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001714 case Primitive::kPrimByte:
1715 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001716 case Primitive::kPrimBoolean:
1717 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001718 case Primitive::kPrimShort:
1719 case Primitive::kPrimInt:
1720 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001721 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001722 locations->SetInAt(0, Location::RequiresRegister());
1723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1724 break;
1725
1726 default:
1727 LOG(FATAL) << "Unexpected type conversion from " << input_type
1728 << " to " << result_type;
1729 }
1730 break;
1731
Roland Levillain01a8d712014-11-14 16:27:39 +00001732 case Primitive::kPrimShort:
1733 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001734 case Primitive::kPrimBoolean:
1735 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001736 case Primitive::kPrimByte:
1737 case Primitive::kPrimInt:
1738 case Primitive::kPrimChar:
1739 // Processing a Dex `int-to-short' instruction.
1740 locations->SetInAt(0, Location::RequiresRegister());
1741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1742 break;
1743
1744 default:
1745 LOG(FATAL) << "Unexpected type conversion from " << input_type
1746 << " to " << result_type;
1747 }
1748 break;
1749
Roland Levillain946e1432014-11-11 17:35:19 +00001750 case Primitive::kPrimInt:
1751 switch (input_type) {
1752 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001753 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001754 locations->SetInAt(0, Location::Any());
1755 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1756 break;
1757
1758 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001759 // Processing a Dex `float-to-int' instruction.
1760 locations->SetInAt(0, Location::RequiresFpuRegister());
1761 locations->SetOut(Location::RequiresRegister());
1762 locations->AddTemp(Location::RequiresFpuRegister());
1763 break;
1764
Roland Levillain946e1432014-11-11 17:35:19 +00001765 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001766 // Processing a Dex `double-to-int' instruction.
1767 locations->SetInAt(0, Location::RequiresFpuRegister());
1768 locations->SetOut(Location::RequiresRegister());
1769 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001770 break;
1771
1772 default:
1773 LOG(FATAL) << "Unexpected type conversion from " << input_type
1774 << " to " << result_type;
1775 }
1776 break;
1777
Roland Levillaindff1f282014-11-05 14:15:05 +00001778 case Primitive::kPrimLong:
1779 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001780 case Primitive::kPrimBoolean:
1781 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001782 case Primitive::kPrimByte:
1783 case Primitive::kPrimShort:
1784 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001785 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001786 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001787 locations->SetInAt(0, Location::RequiresRegister());
1788 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1789 break;
1790
Roland Levillain624279f2014-12-04 11:54:28 +00001791 case Primitive::kPrimFloat: {
1792 // Processing a Dex `float-to-long' instruction.
1793 InvokeRuntimeCallingConvention calling_convention;
1794 locations->SetInAt(0, Location::FpuRegisterLocation(
1795 calling_convention.GetFpuRegisterAt(0)));
1796 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1797 break;
1798 }
1799
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001800 case Primitive::kPrimDouble: {
1801 // Processing a Dex `double-to-long' instruction.
1802 InvokeRuntimeCallingConvention calling_convention;
1803 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1804 calling_convention.GetFpuRegisterAt(0),
1805 calling_convention.GetFpuRegisterAt(1)));
1806 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001807 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001808 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001809
1810 default:
1811 LOG(FATAL) << "Unexpected type conversion from " << input_type
1812 << " to " << result_type;
1813 }
1814 break;
1815
Roland Levillain981e4542014-11-14 11:47:14 +00001816 case Primitive::kPrimChar:
1817 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001818 case Primitive::kPrimBoolean:
1819 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001820 case Primitive::kPrimByte:
1821 case Primitive::kPrimShort:
1822 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001823 // Processing a Dex `int-to-char' instruction.
1824 locations->SetInAt(0, Location::RequiresRegister());
1825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1826 break;
1827
1828 default:
1829 LOG(FATAL) << "Unexpected type conversion from " << input_type
1830 << " to " << result_type;
1831 }
1832 break;
1833
Roland Levillaindff1f282014-11-05 14:15:05 +00001834 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001835 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001836 case Primitive::kPrimBoolean:
1837 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001838 case Primitive::kPrimByte:
1839 case Primitive::kPrimShort:
1840 case Primitive::kPrimInt:
1841 case Primitive::kPrimChar:
1842 // Processing a Dex `int-to-float' instruction.
1843 locations->SetInAt(0, Location::RequiresRegister());
1844 locations->SetOut(Location::RequiresFpuRegister());
1845 break;
1846
Roland Levillain5b3ee562015-04-14 16:02:41 +01001847 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00001848 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001849 InvokeRuntimeCallingConvention calling_convention;
1850 locations->SetInAt(0, Location::RegisterPairLocation(
1851 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1852 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001853 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01001854 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001855
Roland Levillaincff13742014-11-17 14:32:17 +00001856 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001857 // Processing a Dex `double-to-float' instruction.
1858 locations->SetInAt(0, Location::RequiresFpuRegister());
1859 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001860 break;
1861
1862 default:
1863 LOG(FATAL) << "Unexpected type conversion from " << input_type
1864 << " to " << result_type;
1865 };
1866 break;
1867
Roland Levillaindff1f282014-11-05 14:15:05 +00001868 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001869 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001870 case Primitive::kPrimBoolean:
1871 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001872 case Primitive::kPrimByte:
1873 case Primitive::kPrimShort:
1874 case Primitive::kPrimInt:
1875 case Primitive::kPrimChar:
1876 // Processing a Dex `int-to-double' instruction.
1877 locations->SetInAt(0, Location::RequiresRegister());
1878 locations->SetOut(Location::RequiresFpuRegister());
1879 break;
1880
1881 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001882 // Processing a Dex `long-to-double' instruction.
1883 locations->SetInAt(0, Location::RequiresRegister());
1884 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01001885 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001886 locations->AddTemp(Location::RequiresFpuRegister());
1887 break;
1888
Roland Levillaincff13742014-11-17 14:32:17 +00001889 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001890 // Processing a Dex `float-to-double' instruction.
1891 locations->SetInAt(0, Location::RequiresFpuRegister());
1892 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001893 break;
1894
1895 default:
1896 LOG(FATAL) << "Unexpected type conversion from " << input_type
1897 << " to " << result_type;
1898 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001899 break;
1900
1901 default:
1902 LOG(FATAL) << "Unexpected type conversion from " << input_type
1903 << " to " << result_type;
1904 }
1905}
1906
1907void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1908 LocationSummary* locations = conversion->GetLocations();
1909 Location out = locations->Out();
1910 Location in = locations->InAt(0);
1911 Primitive::Type result_type = conversion->GetResultType();
1912 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001913 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001914 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001915 case Primitive::kPrimByte:
1916 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001917 case Primitive::kPrimBoolean:
1918 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001919 case Primitive::kPrimShort:
1920 case Primitive::kPrimInt:
1921 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001922 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001923 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001924 break;
1925
1926 default:
1927 LOG(FATAL) << "Unexpected type conversion from " << input_type
1928 << " to " << result_type;
1929 }
1930 break;
1931
Roland Levillain01a8d712014-11-14 16:27:39 +00001932 case Primitive::kPrimShort:
1933 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001934 case Primitive::kPrimBoolean:
1935 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001936 case Primitive::kPrimByte:
1937 case Primitive::kPrimInt:
1938 case Primitive::kPrimChar:
1939 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001940 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001941 break;
1942
1943 default:
1944 LOG(FATAL) << "Unexpected type conversion from " << input_type
1945 << " to " << result_type;
1946 }
1947 break;
1948
Roland Levillain946e1432014-11-11 17:35:19 +00001949 case Primitive::kPrimInt:
1950 switch (input_type) {
1951 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001952 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001953 DCHECK(out.IsRegister());
1954 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001955 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001956 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001957 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00001958 } else {
1959 DCHECK(in.IsConstant());
1960 DCHECK(in.GetConstant()->IsLongConstant());
1961 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001962 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00001963 }
1964 break;
1965
Roland Levillain3f8f9362014-12-02 17:45:01 +00001966 case Primitive::kPrimFloat: {
1967 // Processing a Dex `float-to-int' instruction.
1968 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1969 __ vmovs(temp, in.AsFpuRegister<SRegister>());
1970 __ vcvtis(temp, temp);
1971 __ vmovrs(out.AsRegister<Register>(), temp);
1972 break;
1973 }
1974
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001975 case Primitive::kPrimDouble: {
1976 // Processing a Dex `double-to-int' instruction.
1977 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
1978 DRegister temp_d = FromLowSToD(temp_s);
1979 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
1980 __ vcvtid(temp_s, temp_d);
1981 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00001982 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001983 }
Roland Levillain946e1432014-11-11 17:35:19 +00001984
1985 default:
1986 LOG(FATAL) << "Unexpected type conversion from " << input_type
1987 << " to " << result_type;
1988 }
1989 break;
1990
Roland Levillaindff1f282014-11-05 14:15:05 +00001991 case Primitive::kPrimLong:
1992 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001993 case Primitive::kPrimBoolean:
1994 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001995 case Primitive::kPrimByte:
1996 case Primitive::kPrimShort:
1997 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001998 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001999 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002000 DCHECK(out.IsRegisterPair());
2001 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002002 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002003 // Sign extension.
2004 __ Asr(out.AsRegisterPairHigh<Register>(),
2005 out.AsRegisterPairLow<Register>(),
2006 31);
2007 break;
2008
2009 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002010 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002011 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2012 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002013 conversion->GetDexPc(),
2014 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002015 break;
2016
Roland Levillaindff1f282014-11-05 14:15:05 +00002017 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002018 // Processing a Dex `double-to-long' instruction.
2019 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2020 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002021 conversion->GetDexPc(),
2022 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002023 break;
2024
2025 default:
2026 LOG(FATAL) << "Unexpected type conversion from " << input_type
2027 << " to " << result_type;
2028 }
2029 break;
2030
Roland Levillain981e4542014-11-14 11:47:14 +00002031 case Primitive::kPrimChar:
2032 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002033 case Primitive::kPrimBoolean:
2034 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002035 case Primitive::kPrimByte:
2036 case Primitive::kPrimShort:
2037 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002038 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002039 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002040 break;
2041
2042 default:
2043 LOG(FATAL) << "Unexpected type conversion from " << input_type
2044 << " to " << result_type;
2045 }
2046 break;
2047
Roland Levillaindff1f282014-11-05 14:15:05 +00002048 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002049 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002050 case Primitive::kPrimBoolean:
2051 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002052 case Primitive::kPrimByte:
2053 case Primitive::kPrimShort:
2054 case Primitive::kPrimInt:
2055 case Primitive::kPrimChar: {
2056 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002057 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2058 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002059 break;
2060 }
2061
Roland Levillain5b3ee562015-04-14 16:02:41 +01002062 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002063 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002064 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2065 conversion,
2066 conversion->GetDexPc(),
2067 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002068 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002069
Roland Levillaincff13742014-11-17 14:32:17 +00002070 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002071 // Processing a Dex `double-to-float' instruction.
2072 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2073 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002074 break;
2075
2076 default:
2077 LOG(FATAL) << "Unexpected type conversion from " << input_type
2078 << " to " << result_type;
2079 };
2080 break;
2081
Roland Levillaindff1f282014-11-05 14:15:05 +00002082 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002083 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002084 case Primitive::kPrimBoolean:
2085 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002086 case Primitive::kPrimByte:
2087 case Primitive::kPrimShort:
2088 case Primitive::kPrimInt:
2089 case Primitive::kPrimChar: {
2090 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002091 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002092 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2093 out.AsFpuRegisterPairLow<SRegister>());
2094 break;
2095 }
2096
Roland Levillain647b9ed2014-11-27 12:06:00 +00002097 case Primitive::kPrimLong: {
2098 // Processing a Dex `long-to-double' instruction.
2099 Register low = in.AsRegisterPairLow<Register>();
2100 Register high = in.AsRegisterPairHigh<Register>();
2101 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2102 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002103 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002104 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002105 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2106 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002107
Roland Levillain682393c2015-04-14 15:57:52 +01002108 // temp_d = int-to-double(high)
2109 __ vmovsr(temp_s, high);
2110 __ vcvtdi(temp_d, temp_s);
2111 // constant_d = k2Pow32EncodingForDouble
2112 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2113 // out_d = unsigned-to-double(low)
2114 __ vmovsr(out_s, low);
2115 __ vcvtdu(out_d, out_s);
2116 // out_d += temp_d * constant_d
2117 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002118 break;
2119 }
2120
Roland Levillaincff13742014-11-17 14:32:17 +00002121 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002122 // Processing a Dex `float-to-double' instruction.
2123 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2124 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002125 break;
2126
2127 default:
2128 LOG(FATAL) << "Unexpected type conversion from " << input_type
2129 << " to " << result_type;
2130 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002131 break;
2132
2133 default:
2134 LOG(FATAL) << "Unexpected type conversion from " << input_type
2135 << " to " << result_type;
2136 }
2137}
2138
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002139void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002140 LocationSummary* locations =
2141 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002142 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002143 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002144 locations->SetInAt(0, Location::RequiresRegister());
2145 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002146 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2147 break;
2148 }
2149
2150 case Primitive::kPrimLong: {
2151 locations->SetInAt(0, Location::RequiresRegister());
2152 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002153 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002154 break;
2155 }
2156
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002157 case Primitive::kPrimFloat:
2158 case Primitive::kPrimDouble: {
2159 locations->SetInAt(0, Location::RequiresFpuRegister());
2160 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002161 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002162 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002163 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002164
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002165 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002166 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002167 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002168}
2169
2170void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2171 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002172 Location out = locations->Out();
2173 Location first = locations->InAt(0);
2174 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002175 switch (add->GetResultType()) {
2176 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002177 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002178 __ add(out.AsRegister<Register>(),
2179 first.AsRegister<Register>(),
2180 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002181 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002182 __ AddConstant(out.AsRegister<Register>(),
2183 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002184 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002185 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002186 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002187
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002188 case Primitive::kPrimLong: {
2189 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002190 __ adds(out.AsRegisterPairLow<Register>(),
2191 first.AsRegisterPairLow<Register>(),
2192 ShifterOperand(second.AsRegisterPairLow<Register>()));
2193 __ adc(out.AsRegisterPairHigh<Register>(),
2194 first.AsRegisterPairHigh<Register>(),
2195 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002196 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002197 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002198
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002199 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002200 __ vadds(out.AsFpuRegister<SRegister>(),
2201 first.AsFpuRegister<SRegister>(),
2202 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002203 break;
2204
2205 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002206 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2207 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2208 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002209 break;
2210
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002211 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002212 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002213 }
2214}
2215
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002216void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002217 LocationSummary* locations =
2218 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002219 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002220 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002221 locations->SetInAt(0, Location::RequiresRegister());
2222 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002223 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2224 break;
2225 }
2226
2227 case Primitive::kPrimLong: {
2228 locations->SetInAt(0, Location::RequiresRegister());
2229 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002231 break;
2232 }
Calin Juravle11351682014-10-23 15:38:15 +01002233 case Primitive::kPrimFloat:
2234 case Primitive::kPrimDouble: {
2235 locations->SetInAt(0, Location::RequiresFpuRegister());
2236 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002237 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002238 break;
Calin Juravle11351682014-10-23 15:38:15 +01002239 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002240 default:
Calin Juravle11351682014-10-23 15:38:15 +01002241 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002242 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002243}
2244
2245void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2246 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002247 Location out = locations->Out();
2248 Location first = locations->InAt(0);
2249 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002250 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002251 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002252 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002253 __ sub(out.AsRegister<Register>(),
2254 first.AsRegister<Register>(),
2255 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002256 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002257 __ AddConstant(out.AsRegister<Register>(),
2258 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002259 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002260 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002261 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002262 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002263
Calin Juravle11351682014-10-23 15:38:15 +01002264 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002265 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002266 __ subs(out.AsRegisterPairLow<Register>(),
2267 first.AsRegisterPairLow<Register>(),
2268 ShifterOperand(second.AsRegisterPairLow<Register>()));
2269 __ sbc(out.AsRegisterPairHigh<Register>(),
2270 first.AsRegisterPairHigh<Register>(),
2271 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002272 break;
Calin Juravle11351682014-10-23 15:38:15 +01002273 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002274
Calin Juravle11351682014-10-23 15:38:15 +01002275 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002276 __ vsubs(out.AsFpuRegister<SRegister>(),
2277 first.AsFpuRegister<SRegister>(),
2278 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002279 break;
Calin Juravle11351682014-10-23 15:38:15 +01002280 }
2281
2282 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002283 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2284 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2285 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002286 break;
2287 }
2288
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002289
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002290 default:
Calin Juravle11351682014-10-23 15:38:15 +01002291 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002292 }
2293}
2294
Calin Juravle34bacdf2014-10-07 20:23:36 +01002295void LocationsBuilderARM::VisitMul(HMul* mul) {
2296 LocationSummary* locations =
2297 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2298 switch (mul->GetResultType()) {
2299 case Primitive::kPrimInt:
2300 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetInAt(1, Location::RequiresRegister());
2303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002304 break;
2305 }
2306
Calin Juravleb5bfa962014-10-21 18:02:24 +01002307 case Primitive::kPrimFloat:
2308 case Primitive::kPrimDouble: {
2309 locations->SetInAt(0, Location::RequiresFpuRegister());
2310 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002311 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002312 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002313 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002314
2315 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002316 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002317 }
2318}
2319
2320void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2321 LocationSummary* locations = mul->GetLocations();
2322 Location out = locations->Out();
2323 Location first = locations->InAt(0);
2324 Location second = locations->InAt(1);
2325 switch (mul->GetResultType()) {
2326 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002327 __ mul(out.AsRegister<Register>(),
2328 first.AsRegister<Register>(),
2329 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002330 break;
2331 }
2332 case Primitive::kPrimLong: {
2333 Register out_hi = out.AsRegisterPairHigh<Register>();
2334 Register out_lo = out.AsRegisterPairLow<Register>();
2335 Register in1_hi = first.AsRegisterPairHigh<Register>();
2336 Register in1_lo = first.AsRegisterPairLow<Register>();
2337 Register in2_hi = second.AsRegisterPairHigh<Register>();
2338 Register in2_lo = second.AsRegisterPairLow<Register>();
2339
2340 // Extra checks to protect caused by the existence of R1_R2.
2341 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2342 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2343 DCHECK_NE(out_hi, in1_lo);
2344 DCHECK_NE(out_hi, in2_lo);
2345
2346 // input: in1 - 64 bits, in2 - 64 bits
2347 // output: out
2348 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2349 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2350 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2351
2352 // IP <- in1.lo * in2.hi
2353 __ mul(IP, in1_lo, in2_hi);
2354 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2355 __ mla(out_hi, in1_hi, in2_lo, IP);
2356 // out.lo <- (in1.lo * in2.lo)[31:0];
2357 __ umull(out_lo, IP, in1_lo, in2_lo);
2358 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2359 __ add(out_hi, out_hi, ShifterOperand(IP));
2360 break;
2361 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002362
2363 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002364 __ vmuls(out.AsFpuRegister<SRegister>(),
2365 first.AsFpuRegister<SRegister>(),
2366 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002367 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002368 }
2369
2370 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002371 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2372 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2373 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002374 break;
2375 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002376
2377 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002378 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002379 }
2380}
2381
Zheng Xuc6667102015-05-15 16:08:45 +08002382void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2383 DCHECK(instruction->IsDiv() || instruction->IsRem());
2384 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2385
2386 LocationSummary* locations = instruction->GetLocations();
2387 Location second = locations->InAt(1);
2388 DCHECK(second.IsConstant());
2389
2390 Register out = locations->Out().AsRegister<Register>();
2391 Register dividend = locations->InAt(0).AsRegister<Register>();
2392 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2393 DCHECK(imm == 1 || imm == -1);
2394
2395 if (instruction->IsRem()) {
2396 __ LoadImmediate(out, 0);
2397 } else {
2398 if (imm == 1) {
2399 __ Mov(out, dividend);
2400 } else {
2401 __ rsb(out, dividend, ShifterOperand(0));
2402 }
2403 }
2404}
2405
2406void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2407 DCHECK(instruction->IsDiv() || instruction->IsRem());
2408 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2409
2410 LocationSummary* locations = instruction->GetLocations();
2411 Location second = locations->InAt(1);
2412 DCHECK(second.IsConstant());
2413
2414 Register out = locations->Out().AsRegister<Register>();
2415 Register dividend = locations->InAt(0).AsRegister<Register>();
2416 Register temp = locations->GetTemp(0).AsRegister<Register>();
2417 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002418 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002419 DCHECK(IsPowerOfTwo(abs_imm));
2420 int ctz_imm = CTZ(abs_imm);
2421
2422 if (ctz_imm == 1) {
2423 __ Lsr(temp, dividend, 32 - ctz_imm);
2424 } else {
2425 __ Asr(temp, dividend, 31);
2426 __ Lsr(temp, temp, 32 - ctz_imm);
2427 }
2428 __ add(out, temp, ShifterOperand(dividend));
2429
2430 if (instruction->IsDiv()) {
2431 __ Asr(out, out, ctz_imm);
2432 if (imm < 0) {
2433 __ rsb(out, out, ShifterOperand(0));
2434 }
2435 } else {
2436 __ ubfx(out, out, 0, ctz_imm);
2437 __ sub(out, out, ShifterOperand(temp));
2438 }
2439}
2440
2441void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2442 DCHECK(instruction->IsDiv() || instruction->IsRem());
2443 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2444
2445 LocationSummary* locations = instruction->GetLocations();
2446 Location second = locations->InAt(1);
2447 DCHECK(second.IsConstant());
2448
2449 Register out = locations->Out().AsRegister<Register>();
2450 Register dividend = locations->InAt(0).AsRegister<Register>();
2451 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2452 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2453 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2454
2455 int64_t magic;
2456 int shift;
2457 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2458
2459 __ LoadImmediate(temp1, magic);
2460 __ smull(temp2, temp1, dividend, temp1);
2461
2462 if (imm > 0 && magic < 0) {
2463 __ add(temp1, temp1, ShifterOperand(dividend));
2464 } else if (imm < 0 && magic > 0) {
2465 __ sub(temp1, temp1, ShifterOperand(dividend));
2466 }
2467
2468 if (shift != 0) {
2469 __ Asr(temp1, temp1, shift);
2470 }
2471
2472 if (instruction->IsDiv()) {
2473 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2474 } else {
2475 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2476 // TODO: Strength reduction for mls.
2477 __ LoadImmediate(temp2, imm);
2478 __ mls(out, temp1, temp2, dividend);
2479 }
2480}
2481
2482void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2483 DCHECK(instruction->IsDiv() || instruction->IsRem());
2484 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2485
2486 LocationSummary* locations = instruction->GetLocations();
2487 Location second = locations->InAt(1);
2488 DCHECK(second.IsConstant());
2489
2490 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2491 if (imm == 0) {
2492 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2493 } else if (imm == 1 || imm == -1) {
2494 DivRemOneOrMinusOne(instruction);
2495 } else if (IsPowerOfTwo(std::abs(imm))) {
2496 DivRemByPowerOfTwo(instruction);
2497 } else {
2498 DCHECK(imm <= -2 || imm >= 2);
2499 GenerateDivRemWithAnyConstant(instruction);
2500 }
2501}
2502
Calin Juravle7c4954d2014-10-28 16:57:40 +00002503void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002504 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2505 if (div->GetResultType() == Primitive::kPrimLong) {
2506 // pLdiv runtime call.
2507 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002508 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2509 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002510 } else if (div->GetResultType() == Primitive::kPrimInt &&
2511 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2512 // pIdivmod runtime call.
2513 call_kind = LocationSummary::kCall;
2514 }
2515
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002516 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2517
Calin Juravle7c4954d2014-10-28 16:57:40 +00002518 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002519 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002520 if (div->InputAt(1)->IsConstant()) {
2521 locations->SetInAt(0, Location::RequiresRegister());
2522 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2524 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2525 if (abs_imm <= 1) {
2526 // No temp register required.
2527 } else {
2528 locations->AddTemp(Location::RequiresRegister());
2529 if (!IsPowerOfTwo(abs_imm)) {
2530 locations->AddTemp(Location::RequiresRegister());
2531 }
2532 }
2533 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002534 locations->SetInAt(0, Location::RequiresRegister());
2535 locations->SetInAt(1, Location::RequiresRegister());
2536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2537 } else {
2538 InvokeRuntimeCallingConvention calling_convention;
2539 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2540 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2541 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2542 // we only need the former.
2543 locations->SetOut(Location::RegisterLocation(R0));
2544 }
Calin Juravled0d48522014-11-04 16:40:20 +00002545 break;
2546 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002547 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002548 InvokeRuntimeCallingConvention calling_convention;
2549 locations->SetInAt(0, Location::RegisterPairLocation(
2550 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2551 locations->SetInAt(1, Location::RegisterPairLocation(
2552 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002553 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002554 break;
2555 }
2556 case Primitive::kPrimFloat:
2557 case Primitive::kPrimDouble: {
2558 locations->SetInAt(0, Location::RequiresFpuRegister());
2559 locations->SetInAt(1, Location::RequiresFpuRegister());
2560 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2561 break;
2562 }
2563
2564 default:
2565 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2566 }
2567}
2568
2569void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2570 LocationSummary* locations = div->GetLocations();
2571 Location out = locations->Out();
2572 Location first = locations->InAt(0);
2573 Location second = locations->InAt(1);
2574
2575 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002576 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002577 if (second.IsConstant()) {
2578 GenerateDivRemConstantIntegral(div);
2579 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002580 __ sdiv(out.AsRegister<Register>(),
2581 first.AsRegister<Register>(),
2582 second.AsRegister<Register>());
2583 } else {
2584 InvokeRuntimeCallingConvention calling_convention;
2585 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2586 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2587 DCHECK_EQ(R0, out.AsRegister<Register>());
2588
2589 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2590 }
Calin Juravled0d48522014-11-04 16:40:20 +00002591 break;
2592 }
2593
Calin Juravle7c4954d2014-10-28 16:57:40 +00002594 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002595 InvokeRuntimeCallingConvention calling_convention;
2596 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2597 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2598 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2599 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2600 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002601 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002602
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002603 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002604 break;
2605 }
2606
2607 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002608 __ vdivs(out.AsFpuRegister<SRegister>(),
2609 first.AsFpuRegister<SRegister>(),
2610 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002611 break;
2612 }
2613
2614 case Primitive::kPrimDouble: {
2615 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2616 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2617 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2618 break;
2619 }
2620
2621 default:
2622 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2623 }
2624}
2625
Calin Juravlebacfec32014-11-14 15:54:36 +00002626void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002627 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002628
2629 // Most remainders are implemented in the runtime.
2630 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002631 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2632 // sdiv will be replaced by other instruction sequence.
2633 call_kind = LocationSummary::kNoCall;
2634 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2635 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002636 // Have hardware divide instruction for int, do it with three instructions.
2637 call_kind = LocationSummary::kNoCall;
2638 }
2639
Calin Juravlebacfec32014-11-14 15:54:36 +00002640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2641
Calin Juravled2ec87d2014-12-08 14:24:46 +00002642 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002643 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002644 if (rem->InputAt(1)->IsConstant()) {
2645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2648 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2649 if (abs_imm <= 1) {
2650 // No temp register required.
2651 } else {
2652 locations->AddTemp(Location::RequiresRegister());
2653 if (!IsPowerOfTwo(abs_imm)) {
2654 locations->AddTemp(Location::RequiresRegister());
2655 }
2656 }
2657 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002658 locations->SetInAt(0, Location::RequiresRegister());
2659 locations->SetInAt(1, Location::RequiresRegister());
2660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2661 locations->AddTemp(Location::RequiresRegister());
2662 } else {
2663 InvokeRuntimeCallingConvention calling_convention;
2664 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2665 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2666 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2667 // we only need the latter.
2668 locations->SetOut(Location::RegisterLocation(R1));
2669 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002670 break;
2671 }
2672 case Primitive::kPrimLong: {
2673 InvokeRuntimeCallingConvention calling_convention;
2674 locations->SetInAt(0, Location::RegisterPairLocation(
2675 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2676 locations->SetInAt(1, Location::RegisterPairLocation(
2677 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2678 // The runtime helper puts the output in R2,R3.
2679 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2680 break;
2681 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002682 case Primitive::kPrimFloat: {
2683 InvokeRuntimeCallingConvention calling_convention;
2684 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2685 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2686 locations->SetOut(Location::FpuRegisterLocation(S0));
2687 break;
2688 }
2689
Calin Juravlebacfec32014-11-14 15:54:36 +00002690 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002691 InvokeRuntimeCallingConvention calling_convention;
2692 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2693 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2694 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2695 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2696 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002697 break;
2698 }
2699
2700 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002701 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002702 }
2703}
2704
2705void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2706 LocationSummary* locations = rem->GetLocations();
2707 Location out = locations->Out();
2708 Location first = locations->InAt(0);
2709 Location second = locations->InAt(1);
2710
Calin Juravled2ec87d2014-12-08 14:24:46 +00002711 Primitive::Type type = rem->GetResultType();
2712 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002713 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002714 if (second.IsConstant()) {
2715 GenerateDivRemConstantIntegral(rem);
2716 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002717 Register reg1 = first.AsRegister<Register>();
2718 Register reg2 = second.AsRegister<Register>();
2719 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002720
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002721 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002722 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002723 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002724 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002725 } else {
2726 InvokeRuntimeCallingConvention calling_convention;
2727 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2728 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2729 DCHECK_EQ(R1, out.AsRegister<Register>());
2730
2731 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2732 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002733 break;
2734 }
2735
2736 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002737 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002738 break;
2739 }
2740
Calin Juravled2ec87d2014-12-08 14:24:46 +00002741 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002742 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002743 break;
2744 }
2745
Calin Juravlebacfec32014-11-14 15:54:36 +00002746 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002747 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002748 break;
2749 }
2750
2751 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002752 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002753 }
2754}
2755
Calin Juravled0d48522014-11-04 16:40:20 +00002756void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002757 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2758 ? LocationSummary::kCallOnSlowPath
2759 : LocationSummary::kNoCall;
2760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002761 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002762 if (instruction->HasUses()) {
2763 locations->SetOut(Location::SameAsFirstInput());
2764 }
2765}
2766
2767void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2768 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
2769 codegen_->AddSlowPath(slow_path);
2770
2771 LocationSummary* locations = instruction->GetLocations();
2772 Location value = locations->InAt(0);
2773
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002774 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002775 case Primitive::kPrimByte:
2776 case Primitive::kPrimChar:
2777 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002778 case Primitive::kPrimInt: {
2779 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002780 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002781 } else {
2782 DCHECK(value.IsConstant()) << value;
2783 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2784 __ b(slow_path->GetEntryLabel());
2785 }
2786 }
2787 break;
2788 }
2789 case Primitive::kPrimLong: {
2790 if (value.IsRegisterPair()) {
2791 __ orrs(IP,
2792 value.AsRegisterPairLow<Register>(),
2793 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2794 __ b(slow_path->GetEntryLabel(), EQ);
2795 } else {
2796 DCHECK(value.IsConstant()) << value;
2797 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2798 __ b(slow_path->GetEntryLabel());
2799 }
2800 }
2801 break;
2802 default:
2803 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2804 }
2805 }
Calin Juravled0d48522014-11-04 16:40:20 +00002806}
2807
Calin Juravle9aec02f2014-11-18 23:06:35 +00002808void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2809 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2810
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002811 LocationSummary* locations =
2812 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002813
2814 switch (op->GetResultType()) {
2815 case Primitive::kPrimInt: {
2816 locations->SetInAt(0, Location::RequiresRegister());
2817 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002818 // Make the output overlap, as it will be used to hold the masked
2819 // second input.
2820 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002821 break;
2822 }
2823 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002824 locations->SetInAt(0, Location::RequiresRegister());
2825 locations->SetInAt(1, Location::RequiresRegister());
2826 locations->AddTemp(Location::RequiresRegister());
2827 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002828 break;
2829 }
2830 default:
2831 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2832 }
2833}
2834
2835void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2836 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2837
2838 LocationSummary* locations = op->GetLocations();
2839 Location out = locations->Out();
2840 Location first = locations->InAt(0);
2841 Location second = locations->InAt(1);
2842
2843 Primitive::Type type = op->GetResultType();
2844 switch (type) {
2845 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002846 Register out_reg = out.AsRegister<Register>();
2847 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002848 // Arm doesn't mask the shift count so we need to do it ourselves.
2849 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002850 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002851 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002852 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002853 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002854 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002855 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002856 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002857 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002858 }
2859 } else {
2860 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2861 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2862 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2863 __ Mov(out_reg, first_reg);
2864 } else if (op->IsShl()) {
2865 __ Lsl(out_reg, first_reg, shift_value);
2866 } else if (op->IsShr()) {
2867 __ Asr(out_reg, first_reg, shift_value);
2868 } else {
2869 __ Lsr(out_reg, first_reg, shift_value);
2870 }
2871 }
2872 break;
2873 }
2874 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002875 Register o_h = out.AsRegisterPairHigh<Register>();
2876 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002877
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002878 Register temp = locations->GetTemp(0).AsRegister<Register>();
2879
2880 Register high = first.AsRegisterPairHigh<Register>();
2881 Register low = first.AsRegisterPairLow<Register>();
2882
2883 Register second_reg = second.AsRegister<Register>();
2884
Calin Juravle9aec02f2014-11-18 23:06:35 +00002885 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002886 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002887 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002888 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002889 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002890 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002891 __ Lsr(temp, low, temp);
2892 __ orr(o_h, o_h, ShifterOperand(temp));
2893 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002894 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002895 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002896 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002897 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002898 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002899 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002900 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002901 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002902 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002903 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002904 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002905 __ Lsl(temp, high, temp);
2906 __ orr(o_l, o_l, ShifterOperand(temp));
2907 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002908 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002909 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002910 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002911 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002912 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002913 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002914 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002915 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002916 __ Lsr(o_l, low, o_h);
2917 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002918 __ Lsl(temp, high, temp);
2919 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002920 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002921 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002922 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002923 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002924 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002925 break;
2926 }
2927 default:
2928 LOG(FATAL) << "Unexpected operation type " << type;
2929 }
2930}
2931
2932void LocationsBuilderARM::VisitShl(HShl* shl) {
2933 HandleShift(shl);
2934}
2935
2936void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2937 HandleShift(shl);
2938}
2939
2940void LocationsBuilderARM::VisitShr(HShr* shr) {
2941 HandleShift(shr);
2942}
2943
2944void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2945 HandleShift(shr);
2946}
2947
2948void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2949 HandleShift(ushr);
2950}
2951
2952void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2953 HandleShift(ushr);
2954}
2955
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002956void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002957 LocationSummary* locations =
2958 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002959 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002960 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002961 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002962 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002963}
2964
2965void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
2966 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002967 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002968 // Note: if heap poisoning is enabled, the entry point takes cares
2969 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002970 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2971 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002972 instruction->GetDexPc(),
2973 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002974}
2975
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002976void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
2977 LocationSummary* locations =
2978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2979 InvokeRuntimeCallingConvention calling_convention;
2980 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002981 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002982 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01002983 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002984}
2985
2986void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
2987 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002988 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01002989 // Note: if heap poisoning is enabled, the entry point takes cares
2990 // of poisoning the reference.
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002991 codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
2992 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002993 instruction->GetDexPc(),
2994 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002995}
2996
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002997void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002998 LocationSummary* locations =
2999 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003000 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3001 if (location.IsStackSlot()) {
3002 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3003 } else if (location.IsDoubleStackSlot()) {
3004 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003005 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003006 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003007}
3008
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003009void InstructionCodeGeneratorARM::VisitParameterValue(
3010 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003011 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003012}
3013
3014void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3015 LocationSummary* locations =
3016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3017 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3018}
3019
3020void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3021 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003022}
3023
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003024void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003025 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003026 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003027 locations->SetInAt(0, Location::RequiresRegister());
3028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003029}
3030
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003031void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3032 LocationSummary* locations = not_->GetLocations();
3033 Location out = locations->Out();
3034 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003035 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003036 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003038 break;
3039
3040 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003041 __ mvn(out.AsRegisterPairLow<Register>(),
3042 ShifterOperand(in.AsRegisterPairLow<Register>()));
3043 __ mvn(out.AsRegisterPairHigh<Register>(),
3044 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003045 break;
3046
3047 default:
3048 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3049 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003050}
3051
David Brazdil66d126e2015-04-03 16:02:44 +01003052void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3053 LocationSummary* locations =
3054 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3055 locations->SetInAt(0, Location::RequiresRegister());
3056 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3057}
3058
3059void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003060 LocationSummary* locations = bool_not->GetLocations();
3061 Location out = locations->Out();
3062 Location in = locations->InAt(0);
3063 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3064}
3065
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003066void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003067 LocationSummary* locations =
3068 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003069 switch (compare->InputAt(0)->GetType()) {
3070 case Primitive::kPrimLong: {
3071 locations->SetInAt(0, Location::RequiresRegister());
3072 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003073 // Output overlaps because it is written before doing the low comparison.
3074 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003075 break;
3076 }
3077 case Primitive::kPrimFloat:
3078 case Primitive::kPrimDouble: {
3079 locations->SetInAt(0, Location::RequiresFpuRegister());
3080 locations->SetInAt(1, Location::RequiresFpuRegister());
3081 locations->SetOut(Location::RequiresRegister());
3082 break;
3083 }
3084 default:
3085 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3086 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003087}
3088
3089void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003090 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003091 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003092 Location left = locations->InAt(0);
3093 Location right = locations->InAt(1);
3094
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003095 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003096 Primitive::Type type = compare->InputAt(0)->GetType();
3097 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003098 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003099 __ cmp(left.AsRegisterPairHigh<Register>(),
3100 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003101 __ b(&less, LT);
3102 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003103 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003104 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003105 __ cmp(left.AsRegisterPairLow<Register>(),
3106 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003107 break;
3108 }
3109 case Primitive::kPrimFloat:
3110 case Primitive::kPrimDouble: {
3111 __ LoadImmediate(out, 0);
3112 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003113 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003114 } else {
3115 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3116 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3117 }
3118 __ vmstat(); // transfer FP status register to ARM APSR.
3119 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003120 break;
3121 }
3122 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003123 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003124 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003125 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003126 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003127
3128 __ Bind(&greater);
3129 __ LoadImmediate(out, 1);
3130 __ b(&done);
3131
3132 __ Bind(&less);
3133 __ LoadImmediate(out, -1);
3134
3135 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003136}
3137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003138void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003139 LocationSummary* locations =
3140 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003141 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3142 locations->SetInAt(i, Location::Any());
3143 }
3144 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003145}
3146
3147void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003148 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003149 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003150}
3151
Calin Juravle52c48962014-12-16 17:02:57 +00003152void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3153 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003154 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003155 switch (kind) {
3156 case MemBarrierKind::kAnyStore:
3157 case MemBarrierKind::kLoadAny:
3158 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003159 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003160 break;
3161 }
3162 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003163 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003164 break;
3165 }
3166 default:
3167 LOG(FATAL) << "Unexpected memory barrier " << kind;
3168 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003169 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003170}
3171
3172void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3173 uint32_t offset,
3174 Register out_lo,
3175 Register out_hi) {
3176 if (offset != 0) {
3177 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003178 __ add(IP, addr, ShifterOperand(out_lo));
3179 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003180 }
3181 __ ldrexd(out_lo, out_hi, addr);
3182}
3183
3184void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3185 uint32_t offset,
3186 Register value_lo,
3187 Register value_hi,
3188 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003189 Register temp2,
3190 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003191 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003192 if (offset != 0) {
3193 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003194 __ add(IP, addr, ShifterOperand(temp1));
3195 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003196 }
3197 __ Bind(&fail);
3198 // We need a load followed by store. (The address used in a STREX instruction must
3199 // be the same as the address in the most recently executed LDREX instruction.)
3200 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003201 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003202 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003203 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003204}
3205
3206void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3207 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3208
Nicolas Geoffray39468442014-09-02 15:17:15 +01003209 LocationSummary* locations =
3210 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003211 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003212
Calin Juravle52c48962014-12-16 17:02:57 +00003213 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003214 if (Primitive::IsFloatingPointType(field_type)) {
3215 locations->SetInAt(1, Location::RequiresFpuRegister());
3216 } else {
3217 locations->SetInAt(1, Location::RequiresRegister());
3218 }
3219
Calin Juravle52c48962014-12-16 17:02:57 +00003220 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003221 bool generate_volatile = field_info.IsVolatile()
3222 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003223 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003224 bool needs_write_barrier =
3225 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003226 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003227 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003228 if (needs_write_barrier) {
3229 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003230 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003231 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003232 // Arm encoding have some additional constraints for ldrexd/strexd:
3233 // - registers need to be consecutive
3234 // - the first register should be even but not R14.
3235 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3236 // enable Arm encoding.
3237 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3238
3239 locations->AddTemp(Location::RequiresRegister());
3240 locations->AddTemp(Location::RequiresRegister());
3241 if (field_type == Primitive::kPrimDouble) {
3242 // For doubles we need two more registers to copy the value.
3243 locations->AddTemp(Location::RegisterLocation(R2));
3244 locations->AddTemp(Location::RegisterLocation(R3));
3245 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003246 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003247}
3248
Calin Juravle52c48962014-12-16 17:02:57 +00003249void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003250 const FieldInfo& field_info,
3251 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003252 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3253
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003254 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003255 Register base = locations->InAt(0).AsRegister<Register>();
3256 Location value = locations->InAt(1);
3257
3258 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003259 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003260 Primitive::Type field_type = field_info.GetFieldType();
3261 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003262 bool needs_write_barrier =
3263 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003264
3265 if (is_volatile) {
3266 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3267 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003268
3269 switch (field_type) {
3270 case Primitive::kPrimBoolean:
3271 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003272 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003273 break;
3274 }
3275
3276 case Primitive::kPrimShort:
3277 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003278 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003279 break;
3280 }
3281
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003282 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003283 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003284 if (kPoisonHeapReferences && needs_write_barrier) {
3285 // Note that in the case where `value` is a null reference,
3286 // we do not enter this block, as a null reference does not
3287 // need poisoning.
3288 DCHECK_EQ(field_type, Primitive::kPrimNot);
3289 Register temp = locations->GetTemp(0).AsRegister<Register>();
3290 __ Mov(temp, value.AsRegister<Register>());
3291 __ PoisonHeapReference(temp);
3292 __ StoreToOffset(kStoreWord, temp, base, offset);
3293 } else {
3294 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3295 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003296 break;
3297 }
3298
3299 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003300 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003301 GenerateWideAtomicStore(base, offset,
3302 value.AsRegisterPairLow<Register>(),
3303 value.AsRegisterPairHigh<Register>(),
3304 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003305 locations->GetTemp(1).AsRegister<Register>(),
3306 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003307 } else {
3308 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003309 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003310 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003311 break;
3312 }
3313
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003314 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003315 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003316 break;
3317 }
3318
3319 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003320 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003321 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003322 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3323 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3324
3325 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3326
3327 GenerateWideAtomicStore(base, offset,
3328 value_reg_lo,
3329 value_reg_hi,
3330 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003331 locations->GetTemp(3).AsRegister<Register>(),
3332 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003333 } else {
3334 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003335 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003336 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003337 break;
3338 }
3339
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003340 case Primitive::kPrimVoid:
3341 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003342 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003343 }
Calin Juravle52c48962014-12-16 17:02:57 +00003344
Calin Juravle77520bc2015-01-12 18:45:46 +00003345 // Longs and doubles are handled in the switch.
3346 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3347 codegen_->MaybeRecordImplicitNullCheck(instruction);
3348 }
3349
3350 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3351 Register temp = locations->GetTemp(0).AsRegister<Register>();
3352 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003353 codegen_->MarkGCCard(
3354 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003355 }
3356
Calin Juravle52c48962014-12-16 17:02:57 +00003357 if (is_volatile) {
3358 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3359 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003360}
3361
Calin Juravle52c48962014-12-16 17:02:57 +00003362void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3363 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003364 LocationSummary* locations =
3365 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003366 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003367
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003368 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003369 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003370 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003371 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003372
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003373 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3374 locations->SetOut(Location::RequiresFpuRegister());
3375 } else {
3376 locations->SetOut(Location::RequiresRegister(),
3377 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3378 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003379 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003380 // Arm encoding have some additional constraints for ldrexd/strexd:
3381 // - registers need to be consecutive
3382 // - the first register should be even but not R14.
3383 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3384 // enable Arm encoding.
3385 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3386 locations->AddTemp(Location::RequiresRegister());
3387 locations->AddTemp(Location::RequiresRegister());
3388 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003389}
3390
Calin Juravle52c48962014-12-16 17:02:57 +00003391void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3392 const FieldInfo& field_info) {
3393 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003394
Calin Juravle52c48962014-12-16 17:02:57 +00003395 LocationSummary* locations = instruction->GetLocations();
3396 Register base = locations->InAt(0).AsRegister<Register>();
3397 Location out = locations->Out();
3398 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003399 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003400 Primitive::Type field_type = field_info.GetFieldType();
3401 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3402
3403 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003404 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003405 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003406 break;
3407 }
3408
3409 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003410 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003411 break;
3412 }
3413
3414 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003415 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003416 break;
3417 }
3418
3419 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003420 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003421 break;
3422 }
3423
3424 case Primitive::kPrimInt:
3425 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003426 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003427 break;
3428 }
3429
3430 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003431 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003432 GenerateWideAtomicLoad(base, offset,
3433 out.AsRegisterPairLow<Register>(),
3434 out.AsRegisterPairHigh<Register>());
3435 } else {
3436 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3437 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003438 break;
3439 }
3440
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003441 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003442 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003443 break;
3444 }
3445
3446 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003447 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003448 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003449 Register lo = locations->GetTemp(0).AsRegister<Register>();
3450 Register hi = locations->GetTemp(1).AsRegister<Register>();
3451 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003452 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003453 __ vmovdrr(out_reg, lo, hi);
3454 } else {
3455 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003456 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003457 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003458 break;
3459 }
3460
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003461 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003462 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003463 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003464 }
Calin Juravle52c48962014-12-16 17:02:57 +00003465
Calin Juravle77520bc2015-01-12 18:45:46 +00003466 // Doubles are handled in the switch.
3467 if (field_type != Primitive::kPrimDouble) {
3468 codegen_->MaybeRecordImplicitNullCheck(instruction);
3469 }
3470
Calin Juravle52c48962014-12-16 17:02:57 +00003471 if (is_volatile) {
3472 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3473 }
Roland Levillain4d027112015-07-01 15:41:14 +01003474
3475 if (field_type == Primitive::kPrimNot) {
3476 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3477 }
Calin Juravle52c48962014-12-16 17:02:57 +00003478}
3479
3480void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3481 HandleFieldSet(instruction, instruction->GetFieldInfo());
3482}
3483
3484void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003485 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003486}
3487
3488void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3489 HandleFieldGet(instruction, instruction->GetFieldInfo());
3490}
3491
3492void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3493 HandleFieldGet(instruction, instruction->GetFieldInfo());
3494}
3495
3496void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3497 HandleFieldGet(instruction, instruction->GetFieldInfo());
3498}
3499
3500void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3501 HandleFieldGet(instruction, instruction->GetFieldInfo());
3502}
3503
3504void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3505 HandleFieldSet(instruction, instruction->GetFieldInfo());
3506}
3507
3508void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003509 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003510}
3511
3512void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003513 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3514 ? LocationSummary::kCallOnSlowPath
3515 : LocationSummary::kNoCall;
3516 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003517 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003518 if (instruction->HasUses()) {
3519 locations->SetOut(Location::SameAsFirstInput());
3520 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003521}
3522
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003523void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003524 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3525 return;
3526 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003527 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003528
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003529 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3530 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3531}
3532
3533void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003534 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003535 codegen_->AddSlowPath(slow_path);
3536
3537 LocationSummary* locations = instruction->GetLocations();
3538 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003539
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003540 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003541}
3542
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003543void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003544 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003545 GenerateImplicitNullCheck(instruction);
3546 } else {
3547 GenerateExplicitNullCheck(instruction);
3548 }
3549}
3550
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003551void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003552 LocationSummary* locations =
3553 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003554 locations->SetInAt(0, Location::RequiresRegister());
3555 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003556 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3558 } else {
3559 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3560 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003561}
3562
3563void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3564 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003565 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003566 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003567 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003568
Roland Levillain4d027112015-07-01 15:41:14 +01003569 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003570 case Primitive::kPrimBoolean: {
3571 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003573 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003574 size_t offset =
3575 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003576 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3577 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003579 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3580 }
3581 break;
3582 }
3583
3584 case Primitive::kPrimByte: {
3585 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003586 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003587 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003588 size_t offset =
3589 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003590 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3591 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003592 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003593 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3594 }
3595 break;
3596 }
3597
3598 case Primitive::kPrimShort: {
3599 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003600 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003601 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003602 size_t offset =
3603 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003604 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3605 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003606 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003607 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3608 }
3609 break;
3610 }
3611
3612 case Primitive::kPrimChar: {
3613 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003615 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003616 size_t offset =
3617 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003618 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3619 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3622 }
3623 break;
3624 }
3625
3626 case Primitive::kPrimInt:
3627 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003628 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3629 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003630 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003631 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003632 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003633 size_t offset =
3634 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 __ LoadFromOffset(kLoadWord, out, obj, offset);
3636 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003638 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3639 }
3640 break;
3641 }
3642
3643 case Primitive::kPrimLong: {
3644 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003645 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003646 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003647 size_t offset =
3648 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003649 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003650 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003651 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003652 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003653 }
3654 break;
3655 }
3656
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003657 case Primitive::kPrimFloat: {
3658 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3659 Location out = locations->Out();
3660 DCHECK(out.IsFpuRegister());
3661 if (index.IsConstant()) {
3662 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3663 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3664 } else {
3665 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3666 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3667 }
3668 break;
3669 }
3670
3671 case Primitive::kPrimDouble: {
3672 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3673 Location out = locations->Out();
3674 DCHECK(out.IsFpuRegisterPair());
3675 if (index.IsConstant()) {
3676 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3677 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3678 } else {
3679 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3680 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3681 }
3682 break;
3683 }
3684
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003685 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003686 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003687 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003688 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003689 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003690
3691 if (type == Primitive::kPrimNot) {
3692 Register out = locations->Out().AsRegister<Register>();
3693 __ MaybeUnpoisonHeapReference(out);
3694 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003695}
3696
3697void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003698 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003699
3700 bool needs_write_barrier =
3701 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3702 bool needs_runtime_call = instruction->NeedsTypeCheck();
3703
Nicolas Geoffray39468442014-09-02 15:17:15 +01003704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003705 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3706 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003707 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003708 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3709 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3710 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003711 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003712 locations->SetInAt(0, Location::RequiresRegister());
3713 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003714 if (Primitive::IsFloatingPointType(value_type)) {
3715 locations->SetInAt(2, Location::RequiresFpuRegister());
3716 } else {
3717 locations->SetInAt(2, Location::RequiresRegister());
3718 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003719
3720 if (needs_write_barrier) {
3721 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003722 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003723 locations->AddTemp(Location::RequiresRegister());
3724 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003725 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003726}
3727
3728void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3729 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003730 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003731 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003732 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003733 bool needs_runtime_call = locations->WillCall();
3734 bool needs_write_barrier =
3735 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003736
3737 switch (value_type) {
3738 case Primitive::kPrimBoolean:
3739 case Primitive::kPrimByte: {
3740 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003741 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003742 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003743 size_t offset =
3744 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003745 __ StoreToOffset(kStoreByte, value, obj, offset);
3746 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003747 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003748 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3749 }
3750 break;
3751 }
3752
3753 case Primitive::kPrimShort:
3754 case Primitive::kPrimChar: {
3755 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003757 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003758 size_t offset =
3759 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003760 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3761 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003762 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003763 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3764 }
3765 break;
3766 }
3767
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003768 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003769 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003770 if (!needs_runtime_call) {
3771 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 Register value = locations->InAt(2).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01003773 Register source = value;
3774 if (kPoisonHeapReferences && needs_write_barrier) {
3775 // Note that in the case where `value` is a null reference,
3776 // we do not enter this block, as a null reference does not
3777 // need poisoning.
3778 DCHECK_EQ(value_type, Primitive::kPrimNot);
3779 Register temp = locations->GetTemp(0).AsRegister<Register>();
3780 __ Mov(temp, value);
3781 __ PoisonHeapReference(temp);
3782 source = temp;
3783 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003784 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003785 size_t offset =
3786 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain4d027112015-07-01 15:41:14 +01003787 __ StoreToOffset(kStoreWord, source, obj, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003788 } else {
3789 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003790 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01003791 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003792 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003793 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003794 if (needs_write_barrier) {
3795 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003796 Register temp = locations->GetTemp(0).AsRegister<Register>();
3797 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003798 codegen_->MarkGCCard(temp, card, obj, value, instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003799 }
3800 } else {
3801 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01003802 // Note: if heap poisoning is enabled, pAputObject takes cares
3803 // of poisoning the reference.
Roland Levillain199f3362014-11-27 17:15:16 +00003804 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3805 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003806 instruction->GetDexPc(),
3807 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003808 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003809 break;
3810 }
3811
3812 case Primitive::kPrimLong: {
3813 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003814 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003815 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003816 size_t offset =
3817 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003818 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003819 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003820 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003821 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822 }
3823 break;
3824 }
3825
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003826 case Primitive::kPrimFloat: {
3827 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3828 Location value = locations->InAt(2);
3829 DCHECK(value.IsFpuRegister());
3830 if (index.IsConstant()) {
3831 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3832 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3833 } else {
3834 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3835 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3836 }
3837 break;
3838 }
3839
3840 case Primitive::kPrimDouble: {
3841 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3842 Location value = locations->InAt(2);
3843 DCHECK(value.IsFpuRegisterPair());
3844 if (index.IsConstant()) {
3845 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3846 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3847 } else {
3848 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3849 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3850 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003851
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003852 break;
3853 }
3854
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003855 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003856 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003857 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003858 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003859
3860 // Ints and objects are handled in the switch.
3861 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3862 codegen_->MaybeRecordImplicitNullCheck(instruction);
3863 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003864}
3865
3866void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003867 LocationSummary* locations =
3868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003869 locations->SetInAt(0, Location::RequiresRegister());
3870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003871}
3872
3873void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3874 LocationSummary* locations = instruction->GetLocations();
3875 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003876 Register obj = locations->InAt(0).AsRegister<Register>();
3877 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003878 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003879 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003880}
3881
3882void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003883 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3884 ? LocationSummary::kCallOnSlowPath
3885 : LocationSummary::kNoCall;
3886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003887 locations->SetInAt(0, Location::RequiresRegister());
3888 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003889 if (instruction->HasUses()) {
3890 locations->SetOut(Location::SameAsFirstInput());
3891 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003892}
3893
3894void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3895 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003896 SlowPathCodeARM* slow_path =
3897 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003898 codegen_->AddSlowPath(slow_path);
3899
Roland Levillain271ab9c2014-11-27 15:23:57 +00003900 Register index = locations->InAt(0).AsRegister<Register>();
3901 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003902
3903 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01003904 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003905}
3906
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003907void CodeGeneratorARM::MarkGCCard(Register temp,
3908 Register card,
3909 Register object,
3910 Register value,
3911 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003912 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003913 if (can_be_null) {
3914 __ CompareAndBranchIfZero(value, &is_null);
3915 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003916 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3917 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3918 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003919 if (can_be_null) {
3920 __ Bind(&is_null);
3921 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922}
3923
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003924void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3925 temp->SetLocations(nullptr);
3926}
3927
3928void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3929 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003930 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003931}
3932
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003933void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003934 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003935 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003936}
3937
3938void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003939 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3940}
3941
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003942void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3943 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3944}
3945
3946void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003947 HBasicBlock* block = instruction->GetBlock();
3948 if (block->GetLoopInformation() != nullptr) {
3949 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3950 // The back edge will generate the suspend check.
3951 return;
3952 }
3953 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3954 // The goto will generate the suspend check.
3955 return;
3956 }
3957 GenerateSuspendCheck(instruction, nullptr);
3958}
3959
3960void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
3961 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003962 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003963 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
3964 if (slow_path == nullptr) {
3965 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
3966 instruction->SetSlowPath(slow_path);
3967 codegen_->AddSlowPath(slow_path);
3968 if (successor != nullptr) {
3969 DCHECK(successor->IsLoopHeader());
3970 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
3971 }
3972 } else {
3973 DCHECK_EQ(slow_path->GetSuccessor(), successor);
3974 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003975
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00003976 __ LoadFromOffset(
3977 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003978 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003979 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003980 __ Bind(slow_path->GetReturnLabel());
3981 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003982 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003983 __ b(slow_path->GetEntryLabel());
3984 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003985}
3986
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003987ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
3988 return codegen_->GetAssembler();
3989}
3990
3991void ParallelMoveResolverARM::EmitMove(size_t index) {
3992 MoveOperands* move = moves_.Get(index);
3993 Location source = move->GetSource();
3994 Location destination = move->GetDestination();
3995
3996 if (source.IsRegister()) {
3997 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003998 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003999 } else {
4000 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004002 SP, destination.GetStackIndex());
4003 }
4004 } else if (source.IsStackSlot()) {
4005 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004006 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004007 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004008 } else if (destination.IsFpuRegister()) {
4009 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004010 } else {
4011 DCHECK(destination.IsStackSlot());
4012 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4013 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4014 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004015 } else if (source.IsFpuRegister()) {
4016 if (destination.IsFpuRegister()) {
4017 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004018 } else {
4019 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004020 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4021 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004022 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004023 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004024 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4025 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004026 } else if (destination.IsRegisterPair()) {
4027 DCHECK(ExpectedPairLayout(destination));
4028 __ LoadFromOffset(
4029 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4030 } else {
4031 DCHECK(destination.IsFpuRegisterPair()) << destination;
4032 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4033 SP,
4034 source.GetStackIndex());
4035 }
4036 } else if (source.IsRegisterPair()) {
4037 if (destination.IsRegisterPair()) {
4038 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4039 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4040 } else {
4041 DCHECK(destination.IsDoubleStackSlot()) << destination;
4042 DCHECK(ExpectedPairLayout(source));
4043 __ StoreToOffset(
4044 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4045 }
4046 } else if (source.IsFpuRegisterPair()) {
4047 if (destination.IsFpuRegisterPair()) {
4048 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4049 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4050 } else {
4051 DCHECK(destination.IsDoubleStackSlot()) << destination;
4052 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4053 SP,
4054 destination.GetStackIndex());
4055 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004056 } else {
4057 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004058 HConstant* constant = source.GetConstant();
4059 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4060 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004061 if (destination.IsRegister()) {
4062 __ LoadImmediate(destination.AsRegister<Register>(), value);
4063 } else {
4064 DCHECK(destination.IsStackSlot());
4065 __ LoadImmediate(IP, value);
4066 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4067 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004068 } else if (constant->IsLongConstant()) {
4069 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004070 if (destination.IsRegisterPair()) {
4071 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4072 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004073 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004074 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004075 __ LoadImmediate(IP, Low32Bits(value));
4076 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4077 __ LoadImmediate(IP, High32Bits(value));
4078 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4079 }
4080 } else if (constant->IsDoubleConstant()) {
4081 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004082 if (destination.IsFpuRegisterPair()) {
4083 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004084 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004085 DCHECK(destination.IsDoubleStackSlot()) << destination;
4086 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004087 __ LoadImmediate(IP, Low32Bits(int_value));
4088 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4089 __ LoadImmediate(IP, High32Bits(int_value));
4090 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4091 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004092 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004093 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004094 float value = constant->AsFloatConstant()->GetValue();
4095 if (destination.IsFpuRegister()) {
4096 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4097 } else {
4098 DCHECK(destination.IsStackSlot());
4099 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4100 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4101 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004102 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004103 }
4104}
4105
4106void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4107 __ Mov(IP, reg);
4108 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4109 __ StoreToOffset(kStoreWord, IP, SP, mem);
4110}
4111
4112void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4113 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4114 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4115 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4116 SP, mem1 + stack_offset);
4117 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4118 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4119 SP, mem2 + stack_offset);
4120 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4121}
4122
4123void ParallelMoveResolverARM::EmitSwap(size_t index) {
4124 MoveOperands* move = moves_.Get(index);
4125 Location source = move->GetSource();
4126 Location destination = move->GetDestination();
4127
4128 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004129 DCHECK_NE(source.AsRegister<Register>(), IP);
4130 DCHECK_NE(destination.AsRegister<Register>(), IP);
4131 __ Mov(IP, source.AsRegister<Register>());
4132 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4133 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004134 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004135 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004136 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004137 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004138 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4139 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004140 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004141 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004142 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004143 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004144 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004145 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004146 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004147 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004148 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4149 destination.AsRegisterPairHigh<Register>(),
4150 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004151 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004152 Register low_reg = source.IsRegisterPair()
4153 ? source.AsRegisterPairLow<Register>()
4154 : destination.AsRegisterPairLow<Register>();
4155 int mem = source.IsRegisterPair()
4156 ? destination.GetStackIndex()
4157 : source.GetStackIndex();
4158 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004159 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004160 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004161 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004162 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004163 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4164 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004165 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004166 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004167 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004168 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4169 DRegister reg = source.IsFpuRegisterPair()
4170 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4171 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4172 int mem = source.IsFpuRegisterPair()
4173 ? destination.GetStackIndex()
4174 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004175 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004176 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004177 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004178 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4179 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4180 : destination.AsFpuRegister<SRegister>();
4181 int mem = source.IsFpuRegister()
4182 ? destination.GetStackIndex()
4183 : source.GetStackIndex();
4184
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004185 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004186 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004187 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004188 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004189 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4190 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004191 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004192 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004193 }
4194}
4195
4196void ParallelMoveResolverARM::SpillScratch(int reg) {
4197 __ Push(static_cast<Register>(reg));
4198}
4199
4200void ParallelMoveResolverARM::RestoreScratch(int reg) {
4201 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004202}
4203
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004204void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004205 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4206 ? LocationSummary::kCallOnSlowPath
4207 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004208 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004209 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004210 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004211 locations->SetOut(Location::RequiresRegister());
4212}
4213
4214void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004215 LocationSummary* locations = cls->GetLocations();
4216 Register out = locations->Out().AsRegister<Register>();
4217 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004218 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004219 DCHECK(!cls->CanCallRuntime());
4220 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004221 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004222 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004223 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004224 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004225 __ LoadFromOffset(kLoadWord,
4226 out,
4227 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004228 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004229 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004230 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004231
4232 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
4233 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4234 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004235 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004236 if (cls->MustGenerateClinitCheck()) {
4237 GenerateClassInitializationCheck(slow_path, out);
4238 } else {
4239 __ Bind(slow_path->GetExitLabel());
4240 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004241 }
4242}
4243
4244void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4245 LocationSummary* locations =
4246 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4247 locations->SetInAt(0, Location::RequiresRegister());
4248 if (check->HasUses()) {
4249 locations->SetOut(Location::SameAsFirstInput());
4250 }
4251}
4252
4253void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004254 // We assume the class is not null.
4255 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
4256 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004257 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004258 GenerateClassInitializationCheck(slow_path,
4259 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004260}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004261
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004262void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
4263 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004264 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4265 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4266 __ b(slow_path->GetEntryLabel(), LT);
4267 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4268 // properly. Therefore, we do a memory fence.
4269 __ dmb(ISH);
4270 __ Bind(slow_path->GetExitLabel());
4271}
4272
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004273void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4274 LocationSummary* locations =
4275 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004276 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004277 locations->SetOut(Location::RequiresRegister());
4278}
4279
4280void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
4281 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
4282 codegen_->AddSlowPath(slow_path);
4283
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004284 LocationSummary* locations = load->GetLocations();
4285 Register out = locations->Out().AsRegister<Register>();
4286 Register current_method = locations->InAt(0).AsRegister<Register>();
4287 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004288 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004289 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004290 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004291 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004292 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004293 __ Bind(slow_path->GetExitLabel());
4294}
4295
David Brazdilcb1c0552015-08-04 16:22:25 +01004296static int32_t GetExceptionTlsOffset() {
4297 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4298}
4299
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004300void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4301 LocationSummary* locations =
4302 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4303 locations->SetOut(Location::RequiresRegister());
4304}
4305
4306void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004307 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004308 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4309}
4310
4311void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4312 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4313}
4314
4315void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004316 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004317 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004318}
4319
4320void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4321 LocationSummary* locations =
4322 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4323 InvokeRuntimeCallingConvention calling_convention;
4324 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4325}
4326
4327void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4328 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004329 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004330}
4331
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004332void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004333 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
4334 ? LocationSummary::kNoCall
4335 : LocationSummary::kCallOnSlowPath;
4336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4337 locations->SetInAt(0, Location::RequiresRegister());
4338 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004339 // The out register is used as a temporary, so it overlaps with the inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004340 // Note that TypeCheckSlowPathARM uses this register too.
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004341 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004342}
4343
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004344void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004345 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004346 Register obj = locations->InAt(0).AsRegister<Register>();
4347 Register cls = locations->InAt(1).AsRegister<Register>();
4348 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004349 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004350 Label done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004351 SlowPathCodeARM* slow_path = nullptr;
4352
4353 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004354 // avoid null check if we know obj is not null.
4355 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004356 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004357 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004358 // Compare the class of `obj` with `cls`.
4359 __ LoadFromOffset(kLoadWord, out, obj, class_offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004360 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004361 __ cmp(out, ShifterOperand(cls));
4362 if (instruction->IsClassFinal()) {
4363 // Classes must be equal for the instanceof to succeed.
4364 __ b(&zero, NE);
4365 __ LoadImmediate(out, 1);
4366 __ b(&done);
4367 } else {
4368 // If the classes are not equal, we go into a slow path.
4369 DCHECK(locations->OnlyCallsOnSlowPath());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004370 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004371 codegen_->AddSlowPath(slow_path);
4372 __ b(slow_path->GetEntryLabel(), NE);
4373 __ LoadImmediate(out, 1);
4374 __ b(&done);
4375 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004376
4377 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
4378 __ Bind(&zero);
4379 __ LoadImmediate(out, 0);
4380 }
4381
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004382 if (slow_path != nullptr) {
4383 __ Bind(slow_path->GetExitLabel());
4384 }
4385 __ Bind(&done);
4386}
4387
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004388void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
4389 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4390 instruction, LocationSummary::kCallOnSlowPath);
4391 locations->SetInAt(0, Location::RequiresRegister());
4392 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004393 // Note that TypeCheckSlowPathARM uses this register too.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004394 locations->AddTemp(Location::RequiresRegister());
4395}
4396
4397void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4398 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004399 Register obj = locations->InAt(0).AsRegister<Register>();
4400 Register cls = locations->InAt(1).AsRegister<Register>();
4401 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004402 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4403
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004404 SlowPathCodeARM* slow_path =
4405 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004406 codegen_->AddSlowPath(slow_path);
4407
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004408 // avoid null check if we know obj is not null.
4409 if (instruction->MustDoNullCheck()) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004410 __ CompareAndBranchIfZero(obj, slow_path->GetExitLabel());
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004411 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004412 // Compare the class of `obj` with `cls`.
4413 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004414 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004415 __ cmp(temp, ShifterOperand(cls));
Roland Levillain4d027112015-07-01 15:41:14 +01004416 // The checkcast succeeds if the classes are equal (fast path).
4417 // Otherwise, we need to go into the slow path to check the types.
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004418 __ b(slow_path->GetEntryLabel(), NE);
4419 __ Bind(slow_path->GetExitLabel());
4420}
4421
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004422void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4423 LocationSummary* locations =
4424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4425 InvokeRuntimeCallingConvention calling_convention;
4426 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4427}
4428
4429void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4430 codegen_->InvokeRuntime(instruction->IsEnter()
4431 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4432 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004433 instruction->GetDexPc(),
4434 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004435}
4436
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004437void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4438void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4439void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4440
4441void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4442 LocationSummary* locations =
4443 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4444 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4445 || instruction->GetResultType() == Primitive::kPrimLong);
4446 locations->SetInAt(0, Location::RequiresRegister());
4447 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004449}
4450
4451void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4452 HandleBitwiseOperation(instruction);
4453}
4454
4455void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4456 HandleBitwiseOperation(instruction);
4457}
4458
4459void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4460 HandleBitwiseOperation(instruction);
4461}
4462
4463void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4464 LocationSummary* locations = instruction->GetLocations();
4465
4466 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004467 Register first = locations->InAt(0).AsRegister<Register>();
4468 Register second = locations->InAt(1).AsRegister<Register>();
4469 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004470 if (instruction->IsAnd()) {
4471 __ and_(out, first, ShifterOperand(second));
4472 } else if (instruction->IsOr()) {
4473 __ orr(out, first, ShifterOperand(second));
4474 } else {
4475 DCHECK(instruction->IsXor());
4476 __ eor(out, first, ShifterOperand(second));
4477 }
4478 } else {
4479 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4480 Location first = locations->InAt(0);
4481 Location second = locations->InAt(1);
4482 Location out = locations->Out();
4483 if (instruction->IsAnd()) {
4484 __ and_(out.AsRegisterPairLow<Register>(),
4485 first.AsRegisterPairLow<Register>(),
4486 ShifterOperand(second.AsRegisterPairLow<Register>()));
4487 __ and_(out.AsRegisterPairHigh<Register>(),
4488 first.AsRegisterPairHigh<Register>(),
4489 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4490 } else if (instruction->IsOr()) {
4491 __ orr(out.AsRegisterPairLow<Register>(),
4492 first.AsRegisterPairLow<Register>(),
4493 ShifterOperand(second.AsRegisterPairLow<Register>()));
4494 __ orr(out.AsRegisterPairHigh<Register>(),
4495 first.AsRegisterPairHigh<Register>(),
4496 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4497 } else {
4498 DCHECK(instruction->IsXor());
4499 __ eor(out.AsRegisterPairLow<Register>(),
4500 first.AsRegisterPairLow<Register>(),
4501 ShifterOperand(second.AsRegisterPairLow<Register>()));
4502 __ eor(out.AsRegisterPairHigh<Register>(),
4503 first.AsRegisterPairHigh<Register>(),
4504 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4505 }
4506 }
4507}
4508
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004509void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004510 // For better instruction scheduling we load the direct code pointer before the method pointer.
4511 bool direct_code_loaded = false;
4512 switch (invoke->GetCodePtrLocation()) {
4513 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4514 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
4515 break;
4516 }
4517 // Calls across dex files are more likely to exceed the available BL range,
4518 // so use absolute patch by falling through to kDirectCodeFixup.
4519 FALLTHROUGH_INTENDED;
4520 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4521 // LR = code address from literal pool with link-time patch.
4522 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4523 direct_code_loaded = true;
4524 break;
4525 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4526 // LR = invoke->GetDirectCodePtr();
4527 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
4528 direct_code_loaded = true;
4529 break;
4530 default:
4531 break;
4532 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004533
Vladimir Marko58155012015-08-19 12:49:41 +00004534 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4535 switch (invoke->GetMethodLoadKind()) {
4536 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4537 // temp = thread->string_init_entrypoint
4538 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
4539 break;
4540 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
4541 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4542 break;
4543 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4544 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4545 break;
4546 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4547 __ LoadLiteral(temp.AsRegister<Register>(),
4548 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4549 break;
4550 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
4551 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
4552 FALLTHROUGH_INTENDED;
4553 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
4554 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4555 Register method_reg;
4556 Register reg = temp.AsRegister<Register>();
4557 if (current_method.IsRegister()) {
4558 method_reg = current_method.AsRegister<Register>();
4559 } else {
4560 DCHECK(invoke->GetLocations()->Intrinsified());
4561 DCHECK(!current_method.IsValid());
4562 method_reg = reg;
4563 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
4564 }
4565 // temp = current_method->dex_cache_resolved_methods_;
4566 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01004567 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
4568 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00004569 // temp = temp[index_in_cache]
4570 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4571 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
4572 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01004573 }
Vladimir Marko58155012015-08-19 12:49:41 +00004574 }
4575
4576 switch (invoke->GetCodePtrLocation()) {
4577 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4578 __ bl(GetFrameEntryLabel());
4579 break;
4580 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4581 if (!direct_code_loaded) {
4582 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4583 __ Bind(&relative_call_patches_.back().label);
4584 Label label;
4585 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
4586 __ Bind(&label);
4587 break;
4588 }
4589 // If we loaded the direct code above, fall through.
4590 FALLTHROUGH_INTENDED;
4591 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4592 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4593 // LR prepared above for better instruction scheduling.
4594 DCHECK(direct_code_loaded);
4595 // LR()
4596 __ blx(LR);
4597 break;
4598 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4599 // LR = callee_method->entry_point_from_quick_compiled_code_
4600 __ LoadFromOffset(
4601 kLoadWord, LR, callee_method.AsRegister<Register>(),
4602 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
4603 // LR()
4604 __ blx(LR);
4605 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004606 }
4607
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004608 DCHECK(!IsLeafMethod());
4609}
4610
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004611void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
4612 Register temp = temp_location.AsRegister<Register>();
4613 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4614 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
4615 LocationSummary* locations = invoke->GetLocations();
4616 Location receiver = locations->InAt(0);
4617 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4618 // temp = object->GetClass();
4619 DCHECK(receiver.IsRegister());
4620 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4621 MaybeRecordImplicitNullCheck(invoke);
4622 __ MaybeUnpoisonHeapReference(temp);
4623 // temp = temp->GetMethodAt(method_offset);
4624 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4625 kArmWordSize).Int32Value();
4626 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4627 // LR = temp->GetEntryPoint();
4628 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
4629 // LR();
4630 __ blx(LR);
4631}
4632
Vladimir Marko58155012015-08-19 12:49:41 +00004633void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4634 DCHECK(linker_patches->empty());
4635 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
4636 linker_patches->reserve(size);
4637 for (const auto& entry : method_patches_) {
4638 const MethodReference& target_method = entry.first;
4639 Literal* literal = entry.second;
4640 DCHECK(literal->GetLabel()->IsBound());
4641 uint32_t literal_offset = literal->GetLabel()->Position();
4642 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4643 target_method.dex_file,
4644 target_method.dex_method_index));
4645 }
4646 for (const auto& entry : call_patches_) {
4647 const MethodReference& target_method = entry.first;
4648 Literal* literal = entry.second;
4649 DCHECK(literal->GetLabel()->IsBound());
4650 uint32_t literal_offset = literal->GetLabel()->Position();
4651 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
4652 target_method.dex_file,
4653 target_method.dex_method_index));
4654 }
4655 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
4656 uint32_t literal_offset = info.label.Position();
4657 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4658 info.target_method.dex_file,
4659 info.target_method.dex_method_index));
4660 }
4661}
4662
4663Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
4664 MethodToLiteralMap* map) {
4665 // Look up the literal for target_method.
4666 auto lb = map->lower_bound(target_method);
4667 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
4668 return lb->second;
4669 }
4670 // We don't have a literal for this method yet, insert a new one.
4671 Literal* literal = __ NewLiteral<uint32_t>(0u);
4672 map->PutBefore(lb, target_method, literal);
4673 return literal;
4674}
4675
4676Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
4677 return DeduplicateMethodLiteral(target_method, &method_patches_);
4678}
4679
4680Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
4681 return DeduplicateMethodLiteral(target_method, &call_patches_);
4682}
4683
Calin Juravleb1498f62015-02-16 13:13:29 +00004684void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
4685 // Nothing to do, this should be removed during prepare for register allocator.
4686 UNUSED(instruction);
4687 LOG(FATAL) << "Unreachable";
4688}
4689
4690void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
4691 // Nothing to do, this should be removed during prepare for register allocator.
4692 UNUSED(instruction);
4693 LOG(FATAL) << "Unreachable";
4694}
4695
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004696void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
4697 DCHECK(codegen_->IsBaseline());
4698 LocationSummary* locations =
4699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4700 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4701}
4702
4703void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4704 DCHECK(codegen_->IsBaseline());
4705 // Will be generated at use site.
4706}
4707
Roland Levillain4d027112015-07-01 15:41:14 +01004708#undef __
4709#undef QUICK_ENTRY_POINT
4710
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004711} // namespace arm
4712} // namespace art