blob: d431acfb536de1d64b3aa8c8770112ea12ce7f8c [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
Andreas Gampe85b62f22015-09-09 13:15:38 -070062class NullCheckSlowPathARM : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -070086class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000087 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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110class SuspendCheckSlowPathARM : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700151class BoundsCheckSlowPathARM : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700189class LoadClassSlowPathARM : public SlowPathCode {
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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700242class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000243 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
Andreas Gampe85b62f22015-09-09 13:15:38 -0700272class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000274 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
275 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100279 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
280 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 DCHECK(instruction_->IsCheckCast()
282 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
285 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000286
287 if (instruction_->IsCheckCast()) {
288 // The codegen for the instruction overwrites `temp`, so put it back in place.
289 Register obj = locations->InAt(0).AsRegister<Register>();
290 Register temp = locations->GetTemp(0).AsRegister<Register>();
291 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
292 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
293 __ MaybeUnpoisonHeapReference(temp);
294 }
295
296 if (!is_fatal_) {
297 SaveLiveRegisters(codegen, locations);
298 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 // We're moving two locations to locations that could overlap, so we need a parallel
301 // move resolver.
302 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000303 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100304 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
309 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100312 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
313 instruction_,
314 instruction_->GetDexPc(),
315 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
317 } else {
318 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100319 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
320 instruction_,
321 instruction_->GetDexPc(),
322 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000325 if (!is_fatal_) {
326 RestoreLiveRegisters(codegen, locations);
327 __ b(GetExitLabel());
328 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 }
330
Alexandre Rames9931f312015-06-19 14:47:01 +0100331 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000333 bool IsFatal() const OVERRIDE { return is_fatal_; }
334
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338
339 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
340};
341
Andreas Gampe85b62f22015-09-09 13:15:38 -0700342class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700343 public:
344 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
345 : instruction_(instruction) {}
346
347 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, instruction_->GetLocations());
350 DCHECK(instruction_->IsDeoptimize());
351 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
352 uint32_t dex_pc = deoptimize->GetDexPc();
353 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
354 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
355 }
356
Alexandre Rames9931f312015-06-19 14:47:01 +0100357 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
358
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700359 private:
360 HInstruction* const instruction_;
361 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
362};
363
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000364#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100365#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700366
Roland Levillain4fa13f62015-07-06 18:11:54 +0100367inline Condition ARMSignedOrFPCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700368 switch (cond) {
369 case kCondEQ: return EQ;
370 case kCondNE: return NE;
371 case kCondLT: return LT;
372 case kCondLE: return LE;
373 case kCondGT: return GT;
374 case kCondGE: return GE;
Dave Allison20dfc792014-06-16 20:44:29 -0700375 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100376 LOG(FATAL) << "Unreachable";
377 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700378}
379
Roland Levillain4fa13f62015-07-06 18:11:54 +0100380inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700381 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100382 case kCondEQ: return EQ;
383 case kCondNE: return NE;
384 case kCondLT: return LO;
385 case kCondLE: return LS;
386 case kCondGT: return HI;
387 case kCondGE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700388 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100389 LOG(FATAL) << "Unreachable";
390 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700391}
392
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100393void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100394 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100395}
396
397void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100398 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100399}
400
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100401size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
402 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
403 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100404}
405
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100406size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
407 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
408 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100409}
410
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000411size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
412 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
413 return kArmWordSize;
414}
415
416size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
417 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
418 return kArmWordSize;
419}
420
Calin Juravle34166012014-12-19 17:22:29 +0000421CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000422 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100423 const CompilerOptions& compiler_options,
424 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000425 : CodeGenerator(graph,
426 kNumberOfCoreRegisters,
427 kNumberOfSRegisters,
428 kNumberOfRegisterPairs,
429 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
430 arraysize(kCoreCalleeSaves)),
431 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
432 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100433 compiler_options,
434 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100435 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100437 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100438 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000439 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000440 isa_features_(isa_features),
441 method_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
442 call_patches_(MethodReferenceComparator(), graph->GetArena()->Adapter()),
443 relative_call_patches_(graph->GetArena()->Adapter()) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700444 // Always save the LR register to mimic Quick.
445 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100446}
447
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000448void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
449 // Ensure that we fix up branches and literal loads and emit the literal pool.
450 __ FinalizeCode();
451
452 // Adjust native pc offsets in stack maps.
453 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
454 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
455 uint32_t new_position = __ GetAdjustedPosition(old_position);
456 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
457 }
458 // Adjust native pc offsets of block labels.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100459 for (HBasicBlock* block : *block_order_) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000460 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
461 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
462 DCHECK_LT(static_cast<size_t>(block->GetBlockId()), block_labels_.Size());
463 Label* block_label = &block_labels_.GetRawStorage()[block->GetBlockId()];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000464 DCHECK_EQ(block_label->IsBound(), !block->IsSingleJump());
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000465 if (block_label->IsBound()) {
466 __ AdjustLabelPosition(block_label);
467 }
468 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100469 // Adjust pc offsets for the disassembly information.
470 if (disasm_info_ != nullptr) {
471 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
472 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
473 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
474 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
475 it.second.start = __ GetAdjustedPosition(it.second.start);
476 it.second.end = __ GetAdjustedPosition(it.second.end);
477 }
478 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
479 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
480 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
481 }
482 }
Vladimir Marko58155012015-08-19 12:49:41 +0000483 // Adjust pc offsets for relative call patches.
484 for (MethodPatchInfo<Label>& info : relative_call_patches_) {
485 __ AdjustLabelPosition(&info.label);
486 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000487
488 CodeGenerator::Finalize(allocator);
489}
490
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100491Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100492 switch (type) {
493 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100494 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100495 ArmManagedRegister pair =
496 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100497 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
498 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
499
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100500 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
501 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100502 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100503 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504 }
505
506 case Primitive::kPrimByte:
507 case Primitive::kPrimBoolean:
508 case Primitive::kPrimChar:
509 case Primitive::kPrimShort:
510 case Primitive::kPrimInt:
511 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100512 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100513 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100514 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
515 ArmManagedRegister current =
516 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
517 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100518 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100519 }
520 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100521 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100522 }
523
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000524 case Primitive::kPrimFloat: {
525 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100526 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100527 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100528
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000529 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000530 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
531 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000532 return Location::FpuRegisterPairLocation(reg, reg + 1);
533 }
534
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100535 case Primitive::kPrimVoid:
536 LOG(FATAL) << "Unreachable type " << type;
537 }
538
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100539 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540}
541
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000542void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100543 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100544 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100545
546 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100547 blocked_core_registers_[SP] = true;
548 blocked_core_registers_[LR] = true;
549 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100550
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100551 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100552 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100554 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100555 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100556
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000557 if (is_baseline) {
558 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
559 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
560 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000561
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000562 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
563
564 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
565 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
566 }
567 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100568
569 UpdateBlockedPairRegisters();
570}
571
572void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
573 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
574 ArmManagedRegister current =
575 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
576 if (blocked_core_registers_[current.AsRegisterPairLow()]
577 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
578 blocked_register_pairs_[i] = true;
579 }
580 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100581}
582
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100583InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
584 : HGraphVisitor(graph),
585 assembler_(codegen->GetAssembler()),
586 codegen_(codegen) {}
587
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000588void CodeGeneratorARM::ComputeSpillMask() {
589 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000590 // Save one extra register for baseline. Note that on thumb2, there is no easy
591 // instruction to restore just the PC, so this actually helps both baseline
592 // and non-baseline to save and restore at least two registers at entry and exit.
593 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000594 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
595 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
596 // We use vpush and vpop for saving and restoring floating point registers, which take
597 // a SRegister and the number of registers to save/restore after that SRegister. We
598 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
599 // but in the range.
600 if (fpu_spill_mask_ != 0) {
601 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
602 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
603 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
604 fpu_spill_mask_ |= (1 << i);
605 }
606 }
607}
608
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100609static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100610 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100611}
612
613static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100614 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100615}
616
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000617void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000618 bool skip_overflow_check =
619 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000620 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000621 __ Bind(&frame_entry_label_);
622
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000623 if (HasEmptyFrame()) {
624 return;
625 }
626
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100627 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000628 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
629 __ LoadFromOffset(kLoadWord, IP, IP, 0);
630 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100631 }
632
Andreas Gampe501fd632015-09-10 16:11:06 -0700633 __ PushList(core_spill_mask_);
634 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
635 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000636 if (fpu_spill_mask_ != 0) {
637 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
638 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100639 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100640 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000641 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100642 int adjust = GetFrameSize() - FrameEntrySpillSize();
643 __ AddConstant(SP, -adjust);
644 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100645 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000646}
647
648void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000649 if (HasEmptyFrame()) {
650 __ bx(LR);
651 return;
652 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100653 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100654 int adjust = GetFrameSize() - FrameEntrySpillSize();
655 __ AddConstant(SP, adjust);
656 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000657 if (fpu_spill_mask_ != 0) {
658 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
659 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100660 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
661 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000662 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700663 // Pop LR into PC to return.
664 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
665 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
666 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100667 __ cfi().RestoreState();
668 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000669}
670
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100671void CodeGeneratorARM::Bind(HBasicBlock* block) {
672 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000673}
674
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100675Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
676 switch (load->GetType()) {
677 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100679 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100680
681 case Primitive::kPrimInt:
682 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100683 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100684 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100685
686 case Primitive::kPrimBoolean:
687 case Primitive::kPrimByte:
688 case Primitive::kPrimChar:
689 case Primitive::kPrimShort:
690 case Primitive::kPrimVoid:
691 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700692 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100693 }
694
695 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700696 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100697}
698
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100699Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100700 switch (type) {
701 case Primitive::kPrimBoolean:
702 case Primitive::kPrimByte:
703 case Primitive::kPrimChar:
704 case Primitive::kPrimShort:
705 case Primitive::kPrimInt:
706 case Primitive::kPrimNot: {
707 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000708 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100709 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100710 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100711 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000712 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100713 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100714 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100715
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000716 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100717 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000718 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100719 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000720 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100721 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000722 if (calling_convention.GetRegisterAt(index) == R1) {
723 // Skip R1, and use R2_R3 instead.
724 gp_index_++;
725 index++;
726 }
727 }
728 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
729 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000730 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100731
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000732 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000733 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100734 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000735 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
736 }
737 }
738
739 case Primitive::kPrimFloat: {
740 uint32_t stack_index = stack_index_++;
741 if (float_index_ % 2 == 0) {
742 float_index_ = std::max(double_index_, float_index_);
743 }
744 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
745 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
746 } else {
747 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
748 }
749 }
750
751 case Primitive::kPrimDouble: {
752 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
753 uint32_t stack_index = stack_index_;
754 stack_index_ += 2;
755 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
756 uint32_t index = double_index_;
757 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000758 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000759 calling_convention.GetFpuRegisterAt(index),
760 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000761 DCHECK(ExpectedPairLayout(result));
762 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000763 } else {
764 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100765 }
766 }
767
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100768 case Primitive::kPrimVoid:
769 LOG(FATAL) << "Unexpected parameter type " << type;
770 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100771 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100772 return Location();
773}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100774
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100775Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000776 switch (type) {
777 case Primitive::kPrimBoolean:
778 case Primitive::kPrimByte:
779 case Primitive::kPrimChar:
780 case Primitive::kPrimShort:
781 case Primitive::kPrimInt:
782 case Primitive::kPrimNot: {
783 return Location::RegisterLocation(R0);
784 }
785
786 case Primitive::kPrimFloat: {
787 return Location::FpuRegisterLocation(S0);
788 }
789
790 case Primitive::kPrimLong: {
791 return Location::RegisterPairLocation(R0, R1);
792 }
793
794 case Primitive::kPrimDouble: {
795 return Location::FpuRegisterPairLocation(S0, S1);
796 }
797
798 case Primitive::kPrimVoid:
799 return Location();
800 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100801
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000802 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000803}
804
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100805Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
806 return Location::RegisterLocation(kMethodRegisterArgument);
807}
808
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100809void CodeGeneratorARM::Move32(Location destination, Location source) {
810 if (source.Equals(destination)) {
811 return;
812 }
813 if (destination.IsRegister()) {
814 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000815 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100816 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000817 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100818 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000819 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100820 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100821 } else if (destination.IsFpuRegister()) {
822 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000823 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100824 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000825 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100826 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000827 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100828 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100829 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000830 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100831 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000832 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100833 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000834 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100835 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000836 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100837 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
838 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100839 }
840 }
841}
842
843void CodeGeneratorARM::Move64(Location destination, Location source) {
844 if (source.Equals(destination)) {
845 return;
846 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100847 if (destination.IsRegisterPair()) {
848 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000849 EmitParallelMoves(
850 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
851 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100852 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000853 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100854 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
855 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100856 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000857 UNIMPLEMENTED(FATAL);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100858 } else {
859 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000860 DCHECK(ExpectedPairLayout(destination));
861 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
862 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100863 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000864 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100865 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000866 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
867 SP,
868 source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100869 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000870 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100871 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872 } else {
873 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100874 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000875 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100876 if (source.AsRegisterPairLow<Register>() == R1) {
877 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100878 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
879 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100881 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 SP, destination.GetStackIndex());
883 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000884 } else if (source.IsFpuRegisterPair()) {
885 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
886 SP,
887 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 } else {
889 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000890 EmitParallelMoves(
891 Location::StackSlot(source.GetStackIndex()),
892 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100893 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000894 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100895 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
896 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897 }
898 }
899}
900
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100901void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100902 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100903 if (instruction->IsCurrentMethod()) {
904 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
905 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100906 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100907 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000908 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000909 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
910 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000911 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000912 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000913 } else {
914 DCHECK(location.IsStackSlot());
915 __ LoadImmediate(IP, value);
916 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
917 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000918 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000919 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000920 int64_t value = const_to_move->AsLongConstant()->GetValue();
921 if (location.IsRegisterPair()) {
922 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
923 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
924 } else {
925 DCHECK(location.IsDoubleStackSlot());
926 __ LoadImmediate(IP, Low32Bits(value));
927 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
928 __ LoadImmediate(IP, High32Bits(value));
929 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
930 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100931 }
Roland Levillain476df552014-10-09 17:51:36 +0100932 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100933 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
934 switch (instruction->GetType()) {
935 case Primitive::kPrimBoolean:
936 case Primitive::kPrimByte:
937 case Primitive::kPrimChar:
938 case Primitive::kPrimShort:
939 case Primitive::kPrimInt:
940 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100941 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100942 Move32(location, Location::StackSlot(stack_slot));
943 break;
944
945 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100946 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100947 Move64(location, Location::DoubleStackSlot(stack_slot));
948 break;
949
950 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100951 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100952 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000953 } else if (instruction->IsTemporary()) {
954 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000955 if (temp_location.IsStackSlot()) {
956 Move32(location, temp_location);
957 } else {
958 DCHECK(temp_location.IsDoubleStackSlot());
959 Move64(location, temp_location);
960 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000961 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100962 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100963 switch (instruction->GetType()) {
964 case Primitive::kPrimBoolean:
965 case Primitive::kPrimByte:
966 case Primitive::kPrimChar:
967 case Primitive::kPrimShort:
968 case Primitive::kPrimNot:
969 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100970 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100971 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972 break;
973
974 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100975 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100976 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100977 break;
978
979 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100980 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100981 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000982 }
983}
984
Calin Juravle175dc732015-08-25 15:42:32 +0100985void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
986 DCHECK(location.IsRegister());
987 __ LoadImmediate(location.AsRegister<Register>(), value);
988}
989
990void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
991 HInstruction* instruction,
992 uint32_t dex_pc,
993 SlowPathCode* slow_path) {
994 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
995 instruction,
996 dex_pc,
997 slow_path);
998}
999
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001000void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1001 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001002 uint32_t dex_pc,
1003 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001004 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001005 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1006 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001007 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001008}
1009
David Brazdilfc6a86a2015-06-26 10:33:45 +00001010void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001011 DCHECK(!successor->IsExitBlock());
1012
1013 HBasicBlock* block = got->GetBlock();
1014 HInstruction* previous = got->GetPrevious();
1015
1016 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001017 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001018 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1019 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1020 return;
1021 }
1022
1023 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1024 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1025 }
1026 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001027 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001028 }
1029}
1030
David Brazdilfc6a86a2015-06-26 10:33:45 +00001031void LocationsBuilderARM::VisitGoto(HGoto* got) {
1032 got->SetLocations(nullptr);
1033}
1034
1035void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1036 HandleGoto(got, got->GetSuccessor());
1037}
1038
1039void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1040 try_boundary->SetLocations(nullptr);
1041}
1042
1043void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1044 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1045 if (!successor->IsExitBlock()) {
1046 HandleGoto(try_boundary, successor);
1047 }
1048}
1049
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001050void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001051 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001052}
1053
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001054void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001055 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001056}
1057
Roland Levillain4fa13f62015-07-06 18:11:54 +01001058void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1059 ShifterOperand operand;
1060 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1061 __ cmp(left, operand);
1062 } else {
1063 Register temp = IP;
1064 __ LoadImmediate(temp, right);
1065 __ cmp(left, ShifterOperand(temp));
1066 }
1067}
1068
1069void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1070 Label* true_label,
1071 Label* false_label) {
1072 __ vmstat(); // transfer FP status register to ARM APSR.
1073 if (cond->IsFPConditionTrueIfNaN()) {
1074 __ b(true_label, VS); // VS for unordered.
1075 } else if (cond->IsFPConditionFalseIfNaN()) {
1076 __ b(false_label, VS); // VS for unordered.
1077 }
1078 __ b(true_label, ARMSignedOrFPCondition(cond->GetCondition()));
1079}
1080
1081void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1082 Label* true_label,
1083 Label* false_label) {
1084 LocationSummary* locations = cond->GetLocations();
1085 Location left = locations->InAt(0);
1086 Location right = locations->InAt(1);
1087 IfCondition if_cond = cond->GetCondition();
1088
1089 Register left_high = left.AsRegisterPairHigh<Register>();
1090 Register left_low = left.AsRegisterPairLow<Register>();
1091 IfCondition true_high_cond = if_cond;
1092 IfCondition false_high_cond = cond->GetOppositeCondition();
1093 Condition final_condition = ARMUnsignedCondition(if_cond);
1094
1095 // Set the conditions for the test, remembering that == needs to be
1096 // decided using the low words.
1097 switch (if_cond) {
1098 case kCondEQ:
1099 case kCondNE:
1100 // Nothing to do.
1101 break;
1102 case kCondLT:
1103 false_high_cond = kCondGT;
1104 break;
1105 case kCondLE:
1106 true_high_cond = kCondLT;
1107 break;
1108 case kCondGT:
1109 false_high_cond = kCondLT;
1110 break;
1111 case kCondGE:
1112 true_high_cond = kCondGT;
1113 break;
1114 }
1115 if (right.IsConstant()) {
1116 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1117 int32_t val_low = Low32Bits(value);
1118 int32_t val_high = High32Bits(value);
1119
1120 GenerateCompareWithImmediate(left_high, val_high);
1121 if (if_cond == kCondNE) {
1122 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1123 } else if (if_cond == kCondEQ) {
1124 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1125 } else {
1126 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1127 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1128 }
1129 // Must be equal high, so compare the lows.
1130 GenerateCompareWithImmediate(left_low, val_low);
1131 } else {
1132 Register right_high = right.AsRegisterPairHigh<Register>();
1133 Register right_low = right.AsRegisterPairLow<Register>();
1134
1135 __ cmp(left_high, ShifterOperand(right_high));
1136 if (if_cond == kCondNE) {
1137 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1138 } else if (if_cond == kCondEQ) {
1139 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1140 } else {
1141 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1142 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1143 }
1144 // Must be equal high, so compare the lows.
1145 __ cmp(left_low, ShifterOperand(right_low));
1146 }
1147 // The last comparison might be unsigned.
1148 __ b(true_label, final_condition);
1149}
1150
1151void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1152 HCondition* condition,
1153 Label* true_target,
1154 Label* false_target,
1155 Label* always_true_target) {
1156 LocationSummary* locations = condition->GetLocations();
1157 Location left = locations->InAt(0);
1158 Location right = locations->InAt(1);
1159
1160 // We don't want true_target as a nullptr.
1161 if (true_target == nullptr) {
1162 true_target = always_true_target;
1163 }
1164 bool falls_through = (false_target == nullptr);
1165
1166 // FP compares don't like null false_targets.
1167 if (false_target == nullptr) {
1168 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1169 }
1170
1171 Primitive::Type type = condition->InputAt(0)->GetType();
1172 switch (type) {
1173 case Primitive::kPrimLong:
1174 GenerateLongComparesAndJumps(condition, true_target, false_target);
1175 break;
1176 case Primitive::kPrimFloat:
1177 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1178 GenerateFPJumps(condition, true_target, false_target);
1179 break;
1180 case Primitive::kPrimDouble:
1181 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1182 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1183 GenerateFPJumps(condition, true_target, false_target);
1184 break;
1185 default:
1186 LOG(FATAL) << "Unexpected compare type " << type;
1187 }
1188
1189 if (!falls_through) {
1190 __ b(false_target);
1191 }
1192}
1193
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001194void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1195 Label* true_target,
1196 Label* false_target,
1197 Label* always_true_target) {
1198 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001199 if (cond->IsIntConstant()) {
1200 // Constant condition, statically compared against 1.
1201 int32_t cond_value = cond->AsIntConstant()->GetValue();
1202 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001203 if (always_true_target != nullptr) {
1204 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001205 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001206 return;
1207 } else {
1208 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001209 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001210 } else {
1211 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1212 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001213 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001214 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1215 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001216 } else {
1217 // Condition has not been materialized, use its inputs as the
1218 // comparison and its condition as the branch condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001219 Primitive::Type type =
1220 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
1221 // Is this a long or FP comparison that has been folded into the HCondition?
1222 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1223 // Generate the comparison directly.
1224 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1225 true_target, false_target, always_true_target);
1226 return;
1227 }
1228
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001229 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001230 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001231 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001232 Location right = locations->InAt(1);
1233 if (right.IsRegister()) {
1234 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001235 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001236 DCHECK(right.IsConstant());
1237 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001238 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001239 __ b(true_target, ARMSignedOrFPCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001240 }
Dave Allison20dfc792014-06-16 20:44:29 -07001241 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001242 if (false_target != nullptr) {
1243 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001244 }
1245}
1246
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001247void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1248 LocationSummary* locations =
1249 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1250 HInstruction* cond = if_instr->InputAt(0);
1251 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1252 locations->SetInAt(0, Location::RequiresRegister());
1253 }
1254}
1255
1256void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1257 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1258 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1259 Label* always_true_target = true_target;
1260 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1261 if_instr->IfTrueSuccessor())) {
1262 always_true_target = nullptr;
1263 }
1264 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1265 if_instr->IfFalseSuccessor())) {
1266 false_target = nullptr;
1267 }
1268 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1269}
1270
1271void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1272 LocationSummary* locations = new (GetGraph()->GetArena())
1273 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1274 HInstruction* cond = deoptimize->InputAt(0);
1275 DCHECK(cond->IsCondition());
1276 if (cond->AsCondition()->NeedsMaterialization()) {
1277 locations->SetInAt(0, Location::RequiresRegister());
1278 }
1279}
1280
1281void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001282 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001283 DeoptimizationSlowPathARM(deoptimize);
1284 codegen_->AddSlowPath(slow_path);
1285 Label* slow_path_entry = slow_path->GetEntryLabel();
1286 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1287}
Dave Allison20dfc792014-06-16 20:44:29 -07001288
Roland Levillain0d37cd02015-05-27 16:39:19 +01001289void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001290 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001291 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001292 // Handle the long/FP comparisons made in instruction simplification.
1293 switch (cond->InputAt(0)->GetType()) {
1294 case Primitive::kPrimLong:
1295 locations->SetInAt(0, Location::RequiresRegister());
1296 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1297 if (cond->NeedsMaterialization()) {
1298 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1299 }
1300 break;
1301
1302 case Primitive::kPrimFloat:
1303 case Primitive::kPrimDouble:
1304 locations->SetInAt(0, Location::RequiresFpuRegister());
1305 locations->SetInAt(1, Location::RequiresFpuRegister());
1306 if (cond->NeedsMaterialization()) {
1307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1308 }
1309 break;
1310
1311 default:
1312 locations->SetInAt(0, Location::RequiresRegister());
1313 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1314 if (cond->NeedsMaterialization()) {
1315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1316 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001317 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001318}
1319
Roland Levillain0d37cd02015-05-27 16:39:19 +01001320void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001321 if (!cond->NeedsMaterialization()) {
1322 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001323 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001324
1325 LocationSummary* locations = cond->GetLocations();
1326 Location left = locations->InAt(0);
1327 Location right = locations->InAt(1);
1328 Register out = locations->Out().AsRegister<Register>();
1329 Label true_label, false_label;
1330
1331 switch (cond->InputAt(0)->GetType()) {
1332 default: {
1333 // Integer case.
1334 if (right.IsRegister()) {
1335 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1336 } else {
1337 DCHECK(right.IsConstant());
1338 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1339 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1340 }
1341 __ it(ARMSignedOrFPCondition(cond->GetCondition()), kItElse);
1342 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
1343 ARMSignedOrFPCondition(cond->GetCondition()));
1344 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
1345 ARMSignedOrFPCondition(cond->GetOppositeCondition()));
1346 return;
1347 }
1348 case Primitive::kPrimLong:
1349 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1350 break;
1351 case Primitive::kPrimFloat:
1352 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1353 GenerateFPJumps(cond, &true_label, &false_label);
1354 break;
1355 case Primitive::kPrimDouble:
1356 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1357 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1358 GenerateFPJumps(cond, &true_label, &false_label);
1359 break;
1360 }
1361
1362 // Convert the jumps into the result.
1363 Label done_label;
1364
1365 // False case: result = 0.
1366 __ Bind(&false_label);
1367 __ LoadImmediate(out, 0);
1368 __ b(&done_label);
1369
1370 // True case: result = 1.
1371 __ Bind(&true_label);
1372 __ LoadImmediate(out, 1);
1373 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001374}
1375
1376void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1377 VisitCondition(comp);
1378}
1379
1380void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1381 VisitCondition(comp);
1382}
1383
1384void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1385 VisitCondition(comp);
1386}
1387
1388void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1389 VisitCondition(comp);
1390}
1391
1392void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1393 VisitCondition(comp);
1394}
1395
1396void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1397 VisitCondition(comp);
1398}
1399
1400void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1401 VisitCondition(comp);
1402}
1403
1404void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1405 VisitCondition(comp);
1406}
1407
1408void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1409 VisitCondition(comp);
1410}
1411
1412void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1413 VisitCondition(comp);
1414}
1415
1416void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1417 VisitCondition(comp);
1418}
1419
1420void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1421 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001422}
1423
1424void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001425 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001426}
1427
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001428void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1429 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001430}
1431
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001432void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001433 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001434}
1435
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001436void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001437 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001438 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001439}
1440
1441void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001442 LocationSummary* locations =
1443 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001444 switch (store->InputAt(1)->GetType()) {
1445 case Primitive::kPrimBoolean:
1446 case Primitive::kPrimByte:
1447 case Primitive::kPrimChar:
1448 case Primitive::kPrimShort:
1449 case Primitive::kPrimInt:
1450 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001451 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001452 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1453 break;
1454
1455 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001456 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001457 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1458 break;
1459
1460 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001461 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001462 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001463}
1464
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001465void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001466 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001467}
1468
1469void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001470 LocationSummary* locations =
1471 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001472 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001473}
1474
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001475void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001476 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001477 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001478}
1479
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001480void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1481 LocationSummary* locations =
1482 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1483 locations->SetOut(Location::ConstantLocation(constant));
1484}
1485
1486void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1487 // Will be generated at use site.
1488 UNUSED(constant);
1489}
1490
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001491void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001492 LocationSummary* locations =
1493 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001494 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001495}
1496
1497void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1498 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001499 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500}
1501
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001502void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1503 LocationSummary* locations =
1504 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1505 locations->SetOut(Location::ConstantLocation(constant));
1506}
1507
1508void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1509 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001510 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001511}
1512
1513void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1514 LocationSummary* locations =
1515 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1516 locations->SetOut(Location::ConstantLocation(constant));
1517}
1518
1519void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1520 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001521 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001522}
1523
Calin Juravle27df7582015-04-17 19:12:31 +01001524void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1525 memory_barrier->SetLocations(nullptr);
1526}
1527
1528void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1529 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1530}
1531
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001532void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001533 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001534}
1535
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001536void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001537 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001538 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001539}
1540
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001541void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001542 LocationSummary* locations =
1543 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001544 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001545}
1546
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001547void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001548 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001549 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001550}
1551
Calin Juravle175dc732015-08-25 15:42:32 +01001552void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1553 // The trampoline uses the same calling convention as dex calling conventions,
1554 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1555 // the method_idx.
1556 HandleInvoke(invoke);
1557}
1558
1559void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1560 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1561}
1562
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001563void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001564 // When we do not run baseline, explicit clinit checks triggered by static
1565 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1566 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001567
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001568 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1569 codegen_->GetInstructionSetFeatures());
1570 if (intrinsic.TryDispatch(invoke)) {
1571 return;
1572 }
1573
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001574 HandleInvoke(invoke);
1575}
1576
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001577static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1578 if (invoke->GetLocations()->Intrinsified()) {
1579 IntrinsicCodeGeneratorARM intrinsic(codegen);
1580 intrinsic.Dispatch(invoke);
1581 return true;
1582 }
1583 return false;
1584}
1585
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001586void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001587 // When we do not run baseline, explicit clinit checks triggered by static
1588 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1589 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001590
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001591 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1592 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001593 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001594
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001595 LocationSummary* locations = invoke->GetLocations();
1596 codegen_->GenerateStaticOrDirectCall(
1597 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001598 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001599}
1600
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001601void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001602 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001603 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001604}
1605
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001606void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001607 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1608 codegen_->GetInstructionSetFeatures());
1609 if (intrinsic.TryDispatch(invoke)) {
1610 return;
1611 }
1612
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001613 HandleInvoke(invoke);
1614}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001615
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001616void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001617 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1618 return;
1619 }
1620
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001621 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001622 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001623 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001624}
1625
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001626void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1627 HandleInvoke(invoke);
1628 // Add the hidden argument.
1629 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1630}
1631
1632void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1633 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001634 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001635 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1636 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001637 LocationSummary* locations = invoke->GetLocations();
1638 Location receiver = locations->InAt(0);
1639 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1640
1641 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001642 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1643 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001644
1645 // temp = object->GetClass();
1646 if (receiver.IsStackSlot()) {
1647 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1648 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1649 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001650 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001651 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001652 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001653 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001654 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001655 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001656 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001657 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1658 // LR = temp->GetEntryPoint();
1659 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1660 // LR();
1661 __ blx(LR);
1662 DCHECK(!codegen_->IsLeafMethod());
1663 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1664}
1665
Roland Levillain88cb1752014-10-20 16:36:47 +01001666void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1667 LocationSummary* locations =
1668 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1669 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001670 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001671 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1673 break;
1674 }
1675 case Primitive::kPrimLong: {
1676 locations->SetInAt(0, Location::RequiresRegister());
1677 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001678 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001679 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001680
Roland Levillain88cb1752014-10-20 16:36:47 +01001681 case Primitive::kPrimFloat:
1682 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001683 locations->SetInAt(0, Location::RequiresFpuRegister());
1684 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001685 break;
1686
1687 default:
1688 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1689 }
1690}
1691
1692void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1693 LocationSummary* locations = neg->GetLocations();
1694 Location out = locations->Out();
1695 Location in = locations->InAt(0);
1696 switch (neg->GetResultType()) {
1697 case Primitive::kPrimInt:
1698 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001699 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001700 break;
1701
1702 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001703 DCHECK(in.IsRegisterPair());
1704 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1705 __ rsbs(out.AsRegisterPairLow<Register>(),
1706 in.AsRegisterPairLow<Register>(),
1707 ShifterOperand(0));
1708 // We cannot emit an RSC (Reverse Subtract with Carry)
1709 // instruction here, as it does not exist in the Thumb-2
1710 // instruction set. We use the following approach
1711 // using SBC and SUB instead.
1712 //
1713 // out.hi = -C
1714 __ sbc(out.AsRegisterPairHigh<Register>(),
1715 out.AsRegisterPairHigh<Register>(),
1716 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1717 // out.hi = out.hi - in.hi
1718 __ sub(out.AsRegisterPairHigh<Register>(),
1719 out.AsRegisterPairHigh<Register>(),
1720 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1721 break;
1722
Roland Levillain88cb1752014-10-20 16:36:47 +01001723 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001724 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001725 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001726 break;
1727
Roland Levillain88cb1752014-10-20 16:36:47 +01001728 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001729 DCHECK(in.IsFpuRegisterPair());
1730 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1731 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001732 break;
1733
1734 default:
1735 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1736 }
1737}
1738
Roland Levillaindff1f282014-11-05 14:15:05 +00001739void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001740 Primitive::Type result_type = conversion->GetResultType();
1741 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001742 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001743
Roland Levillain5b3ee562015-04-14 16:02:41 +01001744 // The float-to-long, double-to-long and long-to-float type conversions
1745 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001746 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001747 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1748 && result_type == Primitive::kPrimLong)
1749 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001750 ? LocationSummary::kCall
1751 : LocationSummary::kNoCall;
1752 LocationSummary* locations =
1753 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1754
David Brazdilb2bd1c52015-03-25 11:17:37 +00001755 // The Java language does not allow treating boolean as an integral type but
1756 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001757
Roland Levillaindff1f282014-11-05 14:15:05 +00001758 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001759 case Primitive::kPrimByte:
1760 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001761 case Primitive::kPrimBoolean:
1762 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001763 case Primitive::kPrimShort:
1764 case Primitive::kPrimInt:
1765 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001766 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001767 locations->SetInAt(0, Location::RequiresRegister());
1768 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected type conversion from " << input_type
1773 << " to " << result_type;
1774 }
1775 break;
1776
Roland Levillain01a8d712014-11-14 16:27:39 +00001777 case Primitive::kPrimShort:
1778 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001779 case Primitive::kPrimBoolean:
1780 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001781 case Primitive::kPrimByte:
1782 case Primitive::kPrimInt:
1783 case Primitive::kPrimChar:
1784 // Processing a Dex `int-to-short' instruction.
1785 locations->SetInAt(0, Location::RequiresRegister());
1786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1787 break;
1788
1789 default:
1790 LOG(FATAL) << "Unexpected type conversion from " << input_type
1791 << " to " << result_type;
1792 }
1793 break;
1794
Roland Levillain946e1432014-11-11 17:35:19 +00001795 case Primitive::kPrimInt:
1796 switch (input_type) {
1797 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001798 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001799 locations->SetInAt(0, Location::Any());
1800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1801 break;
1802
1803 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001804 // Processing a Dex `float-to-int' instruction.
1805 locations->SetInAt(0, Location::RequiresFpuRegister());
1806 locations->SetOut(Location::RequiresRegister());
1807 locations->AddTemp(Location::RequiresFpuRegister());
1808 break;
1809
Roland Levillain946e1432014-11-11 17:35:19 +00001810 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001811 // Processing a Dex `double-to-int' instruction.
1812 locations->SetInAt(0, Location::RequiresFpuRegister());
1813 locations->SetOut(Location::RequiresRegister());
1814 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001815 break;
1816
1817 default:
1818 LOG(FATAL) << "Unexpected type conversion from " << input_type
1819 << " to " << result_type;
1820 }
1821 break;
1822
Roland Levillaindff1f282014-11-05 14:15:05 +00001823 case Primitive::kPrimLong:
1824 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001825 case Primitive::kPrimBoolean:
1826 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001827 case Primitive::kPrimByte:
1828 case Primitive::kPrimShort:
1829 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001830 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001831 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1834 break;
1835
Roland Levillain624279f2014-12-04 11:54:28 +00001836 case Primitive::kPrimFloat: {
1837 // Processing a Dex `float-to-long' instruction.
1838 InvokeRuntimeCallingConvention calling_convention;
1839 locations->SetInAt(0, Location::FpuRegisterLocation(
1840 calling_convention.GetFpuRegisterAt(0)));
1841 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1842 break;
1843 }
1844
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001845 case Primitive::kPrimDouble: {
1846 // Processing a Dex `double-to-long' instruction.
1847 InvokeRuntimeCallingConvention calling_convention;
1848 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1849 calling_convention.GetFpuRegisterAt(0),
1850 calling_convention.GetFpuRegisterAt(1)));
1851 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001852 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001853 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001854
1855 default:
1856 LOG(FATAL) << "Unexpected type conversion from " << input_type
1857 << " to " << result_type;
1858 }
1859 break;
1860
Roland Levillain981e4542014-11-14 11:47:14 +00001861 case Primitive::kPrimChar:
1862 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001863 case Primitive::kPrimBoolean:
1864 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001865 case Primitive::kPrimByte:
1866 case Primitive::kPrimShort:
1867 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001868 // Processing a Dex `int-to-char' instruction.
1869 locations->SetInAt(0, Location::RequiresRegister());
1870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1871 break;
1872
1873 default:
1874 LOG(FATAL) << "Unexpected type conversion from " << input_type
1875 << " to " << result_type;
1876 }
1877 break;
1878
Roland Levillaindff1f282014-11-05 14:15:05 +00001879 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001880 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001881 case Primitive::kPrimBoolean:
1882 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001883 case Primitive::kPrimByte:
1884 case Primitive::kPrimShort:
1885 case Primitive::kPrimInt:
1886 case Primitive::kPrimChar:
1887 // Processing a Dex `int-to-float' instruction.
1888 locations->SetInAt(0, Location::RequiresRegister());
1889 locations->SetOut(Location::RequiresFpuRegister());
1890 break;
1891
Roland Levillain5b3ee562015-04-14 16:02:41 +01001892 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00001893 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001894 InvokeRuntimeCallingConvention calling_convention;
1895 locations->SetInAt(0, Location::RegisterPairLocation(
1896 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1897 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001898 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01001899 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001900
Roland Levillaincff13742014-11-17 14:32:17 +00001901 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001902 // Processing a Dex `double-to-float' instruction.
1903 locations->SetInAt(0, Location::RequiresFpuRegister());
1904 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001905 break;
1906
1907 default:
1908 LOG(FATAL) << "Unexpected type conversion from " << input_type
1909 << " to " << result_type;
1910 };
1911 break;
1912
Roland Levillaindff1f282014-11-05 14:15:05 +00001913 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001914 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001915 case Primitive::kPrimBoolean:
1916 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001917 case Primitive::kPrimByte:
1918 case Primitive::kPrimShort:
1919 case Primitive::kPrimInt:
1920 case Primitive::kPrimChar:
1921 // Processing a Dex `int-to-double' instruction.
1922 locations->SetInAt(0, Location::RequiresRegister());
1923 locations->SetOut(Location::RequiresFpuRegister());
1924 break;
1925
1926 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001927 // Processing a Dex `long-to-double' instruction.
1928 locations->SetInAt(0, Location::RequiresRegister());
1929 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01001930 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001931 locations->AddTemp(Location::RequiresFpuRegister());
1932 break;
1933
Roland Levillaincff13742014-11-17 14:32:17 +00001934 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001935 // Processing a Dex `float-to-double' instruction.
1936 locations->SetInAt(0, Location::RequiresFpuRegister());
1937 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001938 break;
1939
1940 default:
1941 LOG(FATAL) << "Unexpected type conversion from " << input_type
1942 << " to " << result_type;
1943 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001944 break;
1945
1946 default:
1947 LOG(FATAL) << "Unexpected type conversion from " << input_type
1948 << " to " << result_type;
1949 }
1950}
1951
1952void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
1953 LocationSummary* locations = conversion->GetLocations();
1954 Location out = locations->Out();
1955 Location in = locations->InAt(0);
1956 Primitive::Type result_type = conversion->GetResultType();
1957 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001958 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001959 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001960 case Primitive::kPrimByte:
1961 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001962 case Primitive::kPrimBoolean:
1963 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001964 case Primitive::kPrimShort:
1965 case Primitive::kPrimInt:
1966 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001967 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001968 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001969 break;
1970
1971 default:
1972 LOG(FATAL) << "Unexpected type conversion from " << input_type
1973 << " to " << result_type;
1974 }
1975 break;
1976
Roland Levillain01a8d712014-11-14 16:27:39 +00001977 case Primitive::kPrimShort:
1978 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001979 case Primitive::kPrimBoolean:
1980 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001981 case Primitive::kPrimByte:
1982 case Primitive::kPrimInt:
1983 case Primitive::kPrimChar:
1984 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001985 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00001986 break;
1987
1988 default:
1989 LOG(FATAL) << "Unexpected type conversion from " << input_type
1990 << " to " << result_type;
1991 }
1992 break;
1993
Roland Levillain946e1432014-11-11 17:35:19 +00001994 case Primitive::kPrimInt:
1995 switch (input_type) {
1996 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001997 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001998 DCHECK(out.IsRegister());
1999 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002000 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002001 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002002 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002003 } else {
2004 DCHECK(in.IsConstant());
2005 DCHECK(in.GetConstant()->IsLongConstant());
2006 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002007 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002008 }
2009 break;
2010
Roland Levillain3f8f9362014-12-02 17:45:01 +00002011 case Primitive::kPrimFloat: {
2012 // Processing a Dex `float-to-int' instruction.
2013 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2014 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2015 __ vcvtis(temp, temp);
2016 __ vmovrs(out.AsRegister<Register>(), temp);
2017 break;
2018 }
2019
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002020 case Primitive::kPrimDouble: {
2021 // Processing a Dex `double-to-int' instruction.
2022 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2023 DRegister temp_d = FromLowSToD(temp_s);
2024 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2025 __ vcvtid(temp_s, temp_d);
2026 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002027 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002028 }
Roland Levillain946e1432014-11-11 17:35:19 +00002029
2030 default:
2031 LOG(FATAL) << "Unexpected type conversion from " << input_type
2032 << " to " << result_type;
2033 }
2034 break;
2035
Roland Levillaindff1f282014-11-05 14:15:05 +00002036 case Primitive::kPrimLong:
2037 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002038 case Primitive::kPrimBoolean:
2039 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002040 case Primitive::kPrimByte:
2041 case Primitive::kPrimShort:
2042 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002043 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002044 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002045 DCHECK(out.IsRegisterPair());
2046 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002047 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002048 // Sign extension.
2049 __ Asr(out.AsRegisterPairHigh<Register>(),
2050 out.AsRegisterPairLow<Register>(),
2051 31);
2052 break;
2053
2054 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002055 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002056 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2057 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002058 conversion->GetDexPc(),
2059 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002060 break;
2061
Roland Levillaindff1f282014-11-05 14:15:05 +00002062 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002063 // Processing a Dex `double-to-long' instruction.
2064 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2065 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002066 conversion->GetDexPc(),
2067 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002068 break;
2069
2070 default:
2071 LOG(FATAL) << "Unexpected type conversion from " << input_type
2072 << " to " << result_type;
2073 }
2074 break;
2075
Roland Levillain981e4542014-11-14 11:47:14 +00002076 case Primitive::kPrimChar:
2077 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002078 case Primitive::kPrimBoolean:
2079 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002080 case Primitive::kPrimByte:
2081 case Primitive::kPrimShort:
2082 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002083 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002084 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002085 break;
2086
2087 default:
2088 LOG(FATAL) << "Unexpected type conversion from " << input_type
2089 << " to " << result_type;
2090 }
2091 break;
2092
Roland Levillaindff1f282014-11-05 14:15:05 +00002093 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002094 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002095 case Primitive::kPrimBoolean:
2096 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002097 case Primitive::kPrimByte:
2098 case Primitive::kPrimShort:
2099 case Primitive::kPrimInt:
2100 case Primitive::kPrimChar: {
2101 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002102 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2103 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002104 break;
2105 }
2106
Roland Levillain5b3ee562015-04-14 16:02:41 +01002107 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002108 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002109 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2110 conversion,
2111 conversion->GetDexPc(),
2112 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002113 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002114
Roland Levillaincff13742014-11-17 14:32:17 +00002115 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002116 // Processing a Dex `double-to-float' instruction.
2117 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2118 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002119 break;
2120
2121 default:
2122 LOG(FATAL) << "Unexpected type conversion from " << input_type
2123 << " to " << result_type;
2124 };
2125 break;
2126
Roland Levillaindff1f282014-11-05 14:15:05 +00002127 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002128 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002129 case Primitive::kPrimBoolean:
2130 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002131 case Primitive::kPrimByte:
2132 case Primitive::kPrimShort:
2133 case Primitive::kPrimInt:
2134 case Primitive::kPrimChar: {
2135 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002137 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2138 out.AsFpuRegisterPairLow<SRegister>());
2139 break;
2140 }
2141
Roland Levillain647b9ed2014-11-27 12:06:00 +00002142 case Primitive::kPrimLong: {
2143 // Processing a Dex `long-to-double' instruction.
2144 Register low = in.AsRegisterPairLow<Register>();
2145 Register high = in.AsRegisterPairHigh<Register>();
2146 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2147 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002148 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002149 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002150 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2151 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002152
Roland Levillain682393c2015-04-14 15:57:52 +01002153 // temp_d = int-to-double(high)
2154 __ vmovsr(temp_s, high);
2155 __ vcvtdi(temp_d, temp_s);
2156 // constant_d = k2Pow32EncodingForDouble
2157 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2158 // out_d = unsigned-to-double(low)
2159 __ vmovsr(out_s, low);
2160 __ vcvtdu(out_d, out_s);
2161 // out_d += temp_d * constant_d
2162 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002163 break;
2164 }
2165
Roland Levillaincff13742014-11-17 14:32:17 +00002166 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002167 // Processing a Dex `float-to-double' instruction.
2168 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2169 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002170 break;
2171
2172 default:
2173 LOG(FATAL) << "Unexpected type conversion from " << input_type
2174 << " to " << result_type;
2175 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002176 break;
2177
2178 default:
2179 LOG(FATAL) << "Unexpected type conversion from " << input_type
2180 << " to " << result_type;
2181 }
2182}
2183
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002184void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002187 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002188 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002189 locations->SetInAt(0, Location::RequiresRegister());
2190 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2192 break;
2193 }
2194
2195 case Primitive::kPrimLong: {
2196 locations->SetInAt(0, Location::RequiresRegister());
2197 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002199 break;
2200 }
2201
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002202 case Primitive::kPrimFloat:
2203 case Primitive::kPrimDouble: {
2204 locations->SetInAt(0, Location::RequiresFpuRegister());
2205 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002206 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002207 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002208 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002209
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002210 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002211 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002212 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002213}
2214
2215void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2216 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002217 Location out = locations->Out();
2218 Location first = locations->InAt(0);
2219 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002220 switch (add->GetResultType()) {
2221 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002222 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002223 __ add(out.AsRegister<Register>(),
2224 first.AsRegister<Register>(),
2225 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002226 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002227 __ AddConstant(out.AsRegister<Register>(),
2228 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002229 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002230 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002231 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002232
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002233 case Primitive::kPrimLong: {
2234 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002235 __ adds(out.AsRegisterPairLow<Register>(),
2236 first.AsRegisterPairLow<Register>(),
2237 ShifterOperand(second.AsRegisterPairLow<Register>()));
2238 __ adc(out.AsRegisterPairHigh<Register>(),
2239 first.AsRegisterPairHigh<Register>(),
2240 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002241 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002242 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002243
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002244 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002245 __ vadds(out.AsFpuRegister<SRegister>(),
2246 first.AsFpuRegister<SRegister>(),
2247 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002248 break;
2249
2250 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002251 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2252 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2253 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002254 break;
2255
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002256 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002257 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002258 }
2259}
2260
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002261void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002262 LocationSummary* locations =
2263 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002264 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002265 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002266 locations->SetInAt(0, Location::RequiresRegister());
2267 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002268 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2269 break;
2270 }
2271
2272 case Primitive::kPrimLong: {
2273 locations->SetInAt(0, Location::RequiresRegister());
2274 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002276 break;
2277 }
Calin Juravle11351682014-10-23 15:38:15 +01002278 case Primitive::kPrimFloat:
2279 case Primitive::kPrimDouble: {
2280 locations->SetInAt(0, Location::RequiresFpuRegister());
2281 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002282 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002283 break;
Calin Juravle11351682014-10-23 15:38:15 +01002284 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002285 default:
Calin Juravle11351682014-10-23 15:38:15 +01002286 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002287 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002288}
2289
2290void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2291 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002292 Location out = locations->Out();
2293 Location first = locations->InAt(0);
2294 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002295 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002296 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002297 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002298 __ sub(out.AsRegister<Register>(),
2299 first.AsRegister<Register>(),
2300 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002301 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ AddConstant(out.AsRegister<Register>(),
2303 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002304 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002305 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002306 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002307 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002308
Calin Juravle11351682014-10-23 15:38:15 +01002309 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002310 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002311 __ subs(out.AsRegisterPairLow<Register>(),
2312 first.AsRegisterPairLow<Register>(),
2313 ShifterOperand(second.AsRegisterPairLow<Register>()));
2314 __ sbc(out.AsRegisterPairHigh<Register>(),
2315 first.AsRegisterPairHigh<Register>(),
2316 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002317 break;
Calin Juravle11351682014-10-23 15:38:15 +01002318 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002319
Calin Juravle11351682014-10-23 15:38:15 +01002320 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002321 __ vsubs(out.AsFpuRegister<SRegister>(),
2322 first.AsFpuRegister<SRegister>(),
2323 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324 break;
Calin Juravle11351682014-10-23 15:38:15 +01002325 }
2326
2327 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002328 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2329 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2330 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002331 break;
2332 }
2333
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002334
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002335 default:
Calin Juravle11351682014-10-23 15:38:15 +01002336 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002337 }
2338}
2339
Calin Juravle34bacdf2014-10-07 20:23:36 +01002340void LocationsBuilderARM::VisitMul(HMul* mul) {
2341 LocationSummary* locations =
2342 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2343 switch (mul->GetResultType()) {
2344 case Primitive::kPrimInt:
2345 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002346 locations->SetInAt(0, Location::RequiresRegister());
2347 locations->SetInAt(1, Location::RequiresRegister());
2348 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002349 break;
2350 }
2351
Calin Juravleb5bfa962014-10-21 18:02:24 +01002352 case Primitive::kPrimFloat:
2353 case Primitive::kPrimDouble: {
2354 locations->SetInAt(0, Location::RequiresFpuRegister());
2355 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002356 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002357 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002358 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002359
2360 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002361 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002362 }
2363}
2364
2365void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2366 LocationSummary* locations = mul->GetLocations();
2367 Location out = locations->Out();
2368 Location first = locations->InAt(0);
2369 Location second = locations->InAt(1);
2370 switch (mul->GetResultType()) {
2371 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002372 __ mul(out.AsRegister<Register>(),
2373 first.AsRegister<Register>(),
2374 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002375 break;
2376 }
2377 case Primitive::kPrimLong: {
2378 Register out_hi = out.AsRegisterPairHigh<Register>();
2379 Register out_lo = out.AsRegisterPairLow<Register>();
2380 Register in1_hi = first.AsRegisterPairHigh<Register>();
2381 Register in1_lo = first.AsRegisterPairLow<Register>();
2382 Register in2_hi = second.AsRegisterPairHigh<Register>();
2383 Register in2_lo = second.AsRegisterPairLow<Register>();
2384
2385 // Extra checks to protect caused by the existence of R1_R2.
2386 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2387 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2388 DCHECK_NE(out_hi, in1_lo);
2389 DCHECK_NE(out_hi, in2_lo);
2390
2391 // input: in1 - 64 bits, in2 - 64 bits
2392 // output: out
2393 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2394 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2395 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2396
2397 // IP <- in1.lo * in2.hi
2398 __ mul(IP, in1_lo, in2_hi);
2399 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2400 __ mla(out_hi, in1_hi, in2_lo, IP);
2401 // out.lo <- (in1.lo * in2.lo)[31:0];
2402 __ umull(out_lo, IP, in1_lo, in2_lo);
2403 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2404 __ add(out_hi, out_hi, ShifterOperand(IP));
2405 break;
2406 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002407
2408 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002409 __ vmuls(out.AsFpuRegister<SRegister>(),
2410 first.AsFpuRegister<SRegister>(),
2411 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002412 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002413 }
2414
2415 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002416 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2417 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2418 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002419 break;
2420 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002421
2422 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002423 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002424 }
2425}
2426
Zheng Xuc6667102015-05-15 16:08:45 +08002427void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2428 DCHECK(instruction->IsDiv() || instruction->IsRem());
2429 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2430
2431 LocationSummary* locations = instruction->GetLocations();
2432 Location second = locations->InAt(1);
2433 DCHECK(second.IsConstant());
2434
2435 Register out = locations->Out().AsRegister<Register>();
2436 Register dividend = locations->InAt(0).AsRegister<Register>();
2437 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2438 DCHECK(imm == 1 || imm == -1);
2439
2440 if (instruction->IsRem()) {
2441 __ LoadImmediate(out, 0);
2442 } else {
2443 if (imm == 1) {
2444 __ Mov(out, dividend);
2445 } else {
2446 __ rsb(out, dividend, ShifterOperand(0));
2447 }
2448 }
2449}
2450
2451void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2452 DCHECK(instruction->IsDiv() || instruction->IsRem());
2453 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2454
2455 LocationSummary* locations = instruction->GetLocations();
2456 Location second = locations->InAt(1);
2457 DCHECK(second.IsConstant());
2458
2459 Register out = locations->Out().AsRegister<Register>();
2460 Register dividend = locations->InAt(0).AsRegister<Register>();
2461 Register temp = locations->GetTemp(0).AsRegister<Register>();
2462 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002463 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002464 DCHECK(IsPowerOfTwo(abs_imm));
2465 int ctz_imm = CTZ(abs_imm);
2466
2467 if (ctz_imm == 1) {
2468 __ Lsr(temp, dividend, 32 - ctz_imm);
2469 } else {
2470 __ Asr(temp, dividend, 31);
2471 __ Lsr(temp, temp, 32 - ctz_imm);
2472 }
2473 __ add(out, temp, ShifterOperand(dividend));
2474
2475 if (instruction->IsDiv()) {
2476 __ Asr(out, out, ctz_imm);
2477 if (imm < 0) {
2478 __ rsb(out, out, ShifterOperand(0));
2479 }
2480 } else {
2481 __ ubfx(out, out, 0, ctz_imm);
2482 __ sub(out, out, ShifterOperand(temp));
2483 }
2484}
2485
2486void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2487 DCHECK(instruction->IsDiv() || instruction->IsRem());
2488 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2489
2490 LocationSummary* locations = instruction->GetLocations();
2491 Location second = locations->InAt(1);
2492 DCHECK(second.IsConstant());
2493
2494 Register out = locations->Out().AsRegister<Register>();
2495 Register dividend = locations->InAt(0).AsRegister<Register>();
2496 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2497 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2498 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2499
2500 int64_t magic;
2501 int shift;
2502 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2503
2504 __ LoadImmediate(temp1, magic);
2505 __ smull(temp2, temp1, dividend, temp1);
2506
2507 if (imm > 0 && magic < 0) {
2508 __ add(temp1, temp1, ShifterOperand(dividend));
2509 } else if (imm < 0 && magic > 0) {
2510 __ sub(temp1, temp1, ShifterOperand(dividend));
2511 }
2512
2513 if (shift != 0) {
2514 __ Asr(temp1, temp1, shift);
2515 }
2516
2517 if (instruction->IsDiv()) {
2518 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2519 } else {
2520 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2521 // TODO: Strength reduction for mls.
2522 __ LoadImmediate(temp2, imm);
2523 __ mls(out, temp1, temp2, dividend);
2524 }
2525}
2526
2527void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2528 DCHECK(instruction->IsDiv() || instruction->IsRem());
2529 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2530
2531 LocationSummary* locations = instruction->GetLocations();
2532 Location second = locations->InAt(1);
2533 DCHECK(second.IsConstant());
2534
2535 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2536 if (imm == 0) {
2537 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2538 } else if (imm == 1 || imm == -1) {
2539 DivRemOneOrMinusOne(instruction);
2540 } else if (IsPowerOfTwo(std::abs(imm))) {
2541 DivRemByPowerOfTwo(instruction);
2542 } else {
2543 DCHECK(imm <= -2 || imm >= 2);
2544 GenerateDivRemWithAnyConstant(instruction);
2545 }
2546}
2547
Calin Juravle7c4954d2014-10-28 16:57:40 +00002548void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002549 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2550 if (div->GetResultType() == Primitive::kPrimLong) {
2551 // pLdiv runtime call.
2552 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002553 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2554 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002555 } else if (div->GetResultType() == Primitive::kPrimInt &&
2556 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2557 // pIdivmod runtime call.
2558 call_kind = LocationSummary::kCall;
2559 }
2560
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2562
Calin Juravle7c4954d2014-10-28 16:57:40 +00002563 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002564 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002565 if (div->InputAt(1)->IsConstant()) {
2566 locations->SetInAt(0, Location::RequiresRegister());
2567 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2568 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2569 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2570 if (abs_imm <= 1) {
2571 // No temp register required.
2572 } else {
2573 locations->AddTemp(Location::RequiresRegister());
2574 if (!IsPowerOfTwo(abs_imm)) {
2575 locations->AddTemp(Location::RequiresRegister());
2576 }
2577 }
2578 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002579 locations->SetInAt(0, Location::RequiresRegister());
2580 locations->SetInAt(1, Location::RequiresRegister());
2581 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2582 } else {
2583 InvokeRuntimeCallingConvention calling_convention;
2584 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2585 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2586 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2587 // we only need the former.
2588 locations->SetOut(Location::RegisterLocation(R0));
2589 }
Calin Juravled0d48522014-11-04 16:40:20 +00002590 break;
2591 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002592 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002593 InvokeRuntimeCallingConvention calling_convention;
2594 locations->SetInAt(0, Location::RegisterPairLocation(
2595 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2596 locations->SetInAt(1, Location::RegisterPairLocation(
2597 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002598 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002599 break;
2600 }
2601 case Primitive::kPrimFloat:
2602 case Primitive::kPrimDouble: {
2603 locations->SetInAt(0, Location::RequiresFpuRegister());
2604 locations->SetInAt(1, Location::RequiresFpuRegister());
2605 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2606 break;
2607 }
2608
2609 default:
2610 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2611 }
2612}
2613
2614void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2615 LocationSummary* locations = div->GetLocations();
2616 Location out = locations->Out();
2617 Location first = locations->InAt(0);
2618 Location second = locations->InAt(1);
2619
2620 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002621 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002622 if (second.IsConstant()) {
2623 GenerateDivRemConstantIntegral(div);
2624 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002625 __ sdiv(out.AsRegister<Register>(),
2626 first.AsRegister<Register>(),
2627 second.AsRegister<Register>());
2628 } else {
2629 InvokeRuntimeCallingConvention calling_convention;
2630 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2631 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2632 DCHECK_EQ(R0, out.AsRegister<Register>());
2633
2634 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2635 }
Calin Juravled0d48522014-11-04 16:40:20 +00002636 break;
2637 }
2638
Calin Juravle7c4954d2014-10-28 16:57:40 +00002639 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002640 InvokeRuntimeCallingConvention calling_convention;
2641 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2642 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2643 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2644 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2645 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002646 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002647
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002648 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002649 break;
2650 }
2651
2652 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002653 __ vdivs(out.AsFpuRegister<SRegister>(),
2654 first.AsFpuRegister<SRegister>(),
2655 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002656 break;
2657 }
2658
2659 case Primitive::kPrimDouble: {
2660 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2661 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2662 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2663 break;
2664 }
2665
2666 default:
2667 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2668 }
2669}
2670
Calin Juravlebacfec32014-11-14 15:54:36 +00002671void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002672 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002673
2674 // Most remainders are implemented in the runtime.
2675 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002676 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2677 // sdiv will be replaced by other instruction sequence.
2678 call_kind = LocationSummary::kNoCall;
2679 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2680 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002681 // Have hardware divide instruction for int, do it with three instructions.
2682 call_kind = LocationSummary::kNoCall;
2683 }
2684
Calin Juravlebacfec32014-11-14 15:54:36 +00002685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2686
Calin Juravled2ec87d2014-12-08 14:24:46 +00002687 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002688 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002689 if (rem->InputAt(1)->IsConstant()) {
2690 locations->SetInAt(0, Location::RequiresRegister());
2691 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2692 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2693 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2694 if (abs_imm <= 1) {
2695 // No temp register required.
2696 } else {
2697 locations->AddTemp(Location::RequiresRegister());
2698 if (!IsPowerOfTwo(abs_imm)) {
2699 locations->AddTemp(Location::RequiresRegister());
2700 }
2701 }
2702 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002703 locations->SetInAt(0, Location::RequiresRegister());
2704 locations->SetInAt(1, Location::RequiresRegister());
2705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2706 locations->AddTemp(Location::RequiresRegister());
2707 } else {
2708 InvokeRuntimeCallingConvention calling_convention;
2709 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2710 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2711 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2712 // we only need the latter.
2713 locations->SetOut(Location::RegisterLocation(R1));
2714 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002715 break;
2716 }
2717 case Primitive::kPrimLong: {
2718 InvokeRuntimeCallingConvention calling_convention;
2719 locations->SetInAt(0, Location::RegisterPairLocation(
2720 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2721 locations->SetInAt(1, Location::RegisterPairLocation(
2722 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2723 // The runtime helper puts the output in R2,R3.
2724 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2725 break;
2726 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002727 case Primitive::kPrimFloat: {
2728 InvokeRuntimeCallingConvention calling_convention;
2729 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2730 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2731 locations->SetOut(Location::FpuRegisterLocation(S0));
2732 break;
2733 }
2734
Calin Juravlebacfec32014-11-14 15:54:36 +00002735 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002736 InvokeRuntimeCallingConvention calling_convention;
2737 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2738 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2739 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2740 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2741 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002742 break;
2743 }
2744
2745 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002746 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002747 }
2748}
2749
2750void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2751 LocationSummary* locations = rem->GetLocations();
2752 Location out = locations->Out();
2753 Location first = locations->InAt(0);
2754 Location second = locations->InAt(1);
2755
Calin Juravled2ec87d2014-12-08 14:24:46 +00002756 Primitive::Type type = rem->GetResultType();
2757 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002758 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002759 if (second.IsConstant()) {
2760 GenerateDivRemConstantIntegral(rem);
2761 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002762 Register reg1 = first.AsRegister<Register>();
2763 Register reg2 = second.AsRegister<Register>();
2764 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002765
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002766 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002767 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002768 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002769 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002770 } else {
2771 InvokeRuntimeCallingConvention calling_convention;
2772 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2773 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2774 DCHECK_EQ(R1, out.AsRegister<Register>());
2775
2776 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2777 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002778 break;
2779 }
2780
2781 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002782 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002783 break;
2784 }
2785
Calin Juravled2ec87d2014-12-08 14:24:46 +00002786 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002787 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002788 break;
2789 }
2790
Calin Juravlebacfec32014-11-14 15:54:36 +00002791 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002792 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002793 break;
2794 }
2795
2796 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002797 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002798 }
2799}
2800
Calin Juravled0d48522014-11-04 16:40:20 +00002801void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002802 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2803 ? LocationSummary::kCallOnSlowPath
2804 : LocationSummary::kNoCall;
2805 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002806 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002807 if (instruction->HasUses()) {
2808 locations->SetOut(Location::SameAsFirstInput());
2809 }
2810}
2811
2812void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002813 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00002814 codegen_->AddSlowPath(slow_path);
2815
2816 LocationSummary* locations = instruction->GetLocations();
2817 Location value = locations->InAt(0);
2818
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002819 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002820 case Primitive::kPrimByte:
2821 case Primitive::kPrimChar:
2822 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002823 case Primitive::kPrimInt: {
2824 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002825 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002826 } else {
2827 DCHECK(value.IsConstant()) << value;
2828 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2829 __ b(slow_path->GetEntryLabel());
2830 }
2831 }
2832 break;
2833 }
2834 case Primitive::kPrimLong: {
2835 if (value.IsRegisterPair()) {
2836 __ orrs(IP,
2837 value.AsRegisterPairLow<Register>(),
2838 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2839 __ b(slow_path->GetEntryLabel(), EQ);
2840 } else {
2841 DCHECK(value.IsConstant()) << value;
2842 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2843 __ b(slow_path->GetEntryLabel());
2844 }
2845 }
2846 break;
2847 default:
2848 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2849 }
2850 }
Calin Juravled0d48522014-11-04 16:40:20 +00002851}
2852
Calin Juravle9aec02f2014-11-18 23:06:35 +00002853void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2854 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2855
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002856 LocationSummary* locations =
2857 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002858
2859 switch (op->GetResultType()) {
2860 case Primitive::kPrimInt: {
2861 locations->SetInAt(0, Location::RequiresRegister());
2862 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002863 // Make the output overlap, as it will be used to hold the masked
2864 // second input.
2865 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002866 break;
2867 }
2868 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002869 locations->SetInAt(0, Location::RequiresRegister());
2870 locations->SetInAt(1, Location::RequiresRegister());
2871 locations->AddTemp(Location::RequiresRegister());
2872 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002873 break;
2874 }
2875 default:
2876 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2877 }
2878}
2879
2880void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2881 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2882
2883 LocationSummary* locations = op->GetLocations();
2884 Location out = locations->Out();
2885 Location first = locations->InAt(0);
2886 Location second = locations->InAt(1);
2887
2888 Primitive::Type type = op->GetResultType();
2889 switch (type) {
2890 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002891 Register out_reg = out.AsRegister<Register>();
2892 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002893 // Arm doesn't mask the shift count so we need to do it ourselves.
2894 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002895 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002896 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002897 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002898 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002899 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002900 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002901 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002902 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002903 }
2904 } else {
2905 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2906 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2907 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2908 __ Mov(out_reg, first_reg);
2909 } else if (op->IsShl()) {
2910 __ Lsl(out_reg, first_reg, shift_value);
2911 } else if (op->IsShr()) {
2912 __ Asr(out_reg, first_reg, shift_value);
2913 } else {
2914 __ Lsr(out_reg, first_reg, shift_value);
2915 }
2916 }
2917 break;
2918 }
2919 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002920 Register o_h = out.AsRegisterPairHigh<Register>();
2921 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002922
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002923 Register temp = locations->GetTemp(0).AsRegister<Register>();
2924
2925 Register high = first.AsRegisterPairHigh<Register>();
2926 Register low = first.AsRegisterPairLow<Register>();
2927
2928 Register second_reg = second.AsRegister<Register>();
2929
Calin Juravle9aec02f2014-11-18 23:06:35 +00002930 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002931 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002932 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002933 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002934 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002935 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002936 __ Lsr(temp, low, temp);
2937 __ orr(o_h, o_h, ShifterOperand(temp));
2938 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002939 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002940 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002941 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002942 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002943 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002944 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002945 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002946 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002947 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002948 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002949 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002950 __ Lsl(temp, high, temp);
2951 __ orr(o_l, o_l, ShifterOperand(temp));
2952 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002953 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002954 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002955 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002956 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002957 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002958 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002959 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002960 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002961 __ Lsr(o_l, low, o_h);
2962 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002963 __ Lsl(temp, high, temp);
2964 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002965 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002966 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002967 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002968 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002969 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00002970 break;
2971 }
2972 default:
2973 LOG(FATAL) << "Unexpected operation type " << type;
2974 }
2975}
2976
2977void LocationsBuilderARM::VisitShl(HShl* shl) {
2978 HandleShift(shl);
2979}
2980
2981void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
2982 HandleShift(shl);
2983}
2984
2985void LocationsBuilderARM::VisitShr(HShr* shr) {
2986 HandleShift(shr);
2987}
2988
2989void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
2990 HandleShift(shr);
2991}
2992
2993void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
2994 HandleShift(ushr);
2995}
2996
2997void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
2998 HandleShift(ushr);
2999}
3000
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003001void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003002 LocationSummary* locations =
3003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003004 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003005 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003006 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003007 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003008}
3009
3010void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3011 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003012 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003013 // Note: if heap poisoning is enabled, the entry point takes cares
3014 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003015 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003016 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003017 instruction->GetDexPc(),
3018 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003019}
3020
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003021void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3022 LocationSummary* locations =
3023 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3024 InvokeRuntimeCallingConvention calling_convention;
3025 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003026 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003027 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003028 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003029}
3030
3031void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3032 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003033 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003034 // Note: if heap poisoning is enabled, the entry point takes cares
3035 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003036 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003037 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003038 instruction->GetDexPc(),
3039 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003040}
3041
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003042void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003043 LocationSummary* locations =
3044 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003045 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3046 if (location.IsStackSlot()) {
3047 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3048 } else if (location.IsDoubleStackSlot()) {
3049 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003050 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003051 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003052}
3053
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003054void InstructionCodeGeneratorARM::VisitParameterValue(
3055 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003056 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003057}
3058
3059void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3060 LocationSummary* locations =
3061 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3062 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3063}
3064
3065void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3066 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003067}
3068
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003069void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003070 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003071 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003072 locations->SetInAt(0, Location::RequiresRegister());
3073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003074}
3075
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003076void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3077 LocationSummary* locations = not_->GetLocations();
3078 Location out = locations->Out();
3079 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003080 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003081 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003083 break;
3084
3085 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003086 __ mvn(out.AsRegisterPairLow<Register>(),
3087 ShifterOperand(in.AsRegisterPairLow<Register>()));
3088 __ mvn(out.AsRegisterPairHigh<Register>(),
3089 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003090 break;
3091
3092 default:
3093 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3094 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003095}
3096
David Brazdil66d126e2015-04-03 16:02:44 +01003097void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3098 LocationSummary* locations =
3099 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3100 locations->SetInAt(0, Location::RequiresRegister());
3101 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3102}
3103
3104void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003105 LocationSummary* locations = bool_not->GetLocations();
3106 Location out = locations->Out();
3107 Location in = locations->InAt(0);
3108 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3109}
3110
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003111void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003112 LocationSummary* locations =
3113 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003114 switch (compare->InputAt(0)->GetType()) {
3115 case Primitive::kPrimLong: {
3116 locations->SetInAt(0, Location::RequiresRegister());
3117 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003118 // Output overlaps because it is written before doing the low comparison.
3119 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003120 break;
3121 }
3122 case Primitive::kPrimFloat:
3123 case Primitive::kPrimDouble: {
3124 locations->SetInAt(0, Location::RequiresFpuRegister());
3125 locations->SetInAt(1, Location::RequiresFpuRegister());
3126 locations->SetOut(Location::RequiresRegister());
3127 break;
3128 }
3129 default:
3130 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3131 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003132}
3133
3134void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003135 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003136 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003137 Location left = locations->InAt(0);
3138 Location right = locations->InAt(1);
3139
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003140 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003141 Primitive::Type type = compare->InputAt(0)->GetType();
3142 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003143 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003144 __ cmp(left.AsRegisterPairHigh<Register>(),
3145 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003146 __ b(&less, LT);
3147 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003148 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003149 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003150 __ cmp(left.AsRegisterPairLow<Register>(),
3151 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003152 break;
3153 }
3154 case Primitive::kPrimFloat:
3155 case Primitive::kPrimDouble: {
3156 __ LoadImmediate(out, 0);
3157 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003158 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003159 } else {
3160 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3161 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3162 }
3163 __ vmstat(); // transfer FP status register to ARM APSR.
3164 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003165 break;
3166 }
3167 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003168 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003169 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003170 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003171 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003172
3173 __ Bind(&greater);
3174 __ LoadImmediate(out, 1);
3175 __ b(&done);
3176
3177 __ Bind(&less);
3178 __ LoadImmediate(out, -1);
3179
3180 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003181}
3182
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003183void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003184 LocationSummary* locations =
3185 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003186 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3187 locations->SetInAt(i, Location::Any());
3188 }
3189 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003190}
3191
3192void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003193 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003194 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003195}
3196
Calin Juravle52c48962014-12-16 17:02:57 +00003197void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3198 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003199 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003200 switch (kind) {
3201 case MemBarrierKind::kAnyStore:
3202 case MemBarrierKind::kLoadAny:
3203 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003204 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003205 break;
3206 }
3207 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003208 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003209 break;
3210 }
3211 default:
3212 LOG(FATAL) << "Unexpected memory barrier " << kind;
3213 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003214 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003215}
3216
3217void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3218 uint32_t offset,
3219 Register out_lo,
3220 Register out_hi) {
3221 if (offset != 0) {
3222 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003223 __ add(IP, addr, ShifterOperand(out_lo));
3224 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003225 }
3226 __ ldrexd(out_lo, out_hi, addr);
3227}
3228
3229void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3230 uint32_t offset,
3231 Register value_lo,
3232 Register value_hi,
3233 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003234 Register temp2,
3235 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003236 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003237 if (offset != 0) {
3238 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003239 __ add(IP, addr, ShifterOperand(temp1));
3240 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003241 }
3242 __ Bind(&fail);
3243 // We need a load followed by store. (The address used in a STREX instruction must
3244 // be the same as the address in the most recently executed LDREX instruction.)
3245 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003246 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003247 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003248 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003249}
3250
3251void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3252 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3253
Nicolas Geoffray39468442014-09-02 15:17:15 +01003254 LocationSummary* locations =
3255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003256 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003257
Calin Juravle52c48962014-12-16 17:02:57 +00003258 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003259 if (Primitive::IsFloatingPointType(field_type)) {
3260 locations->SetInAt(1, Location::RequiresFpuRegister());
3261 } else {
3262 locations->SetInAt(1, Location::RequiresRegister());
3263 }
3264
Calin Juravle52c48962014-12-16 17:02:57 +00003265 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003266 bool generate_volatile = field_info.IsVolatile()
3267 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003268 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003269 bool needs_write_barrier =
3270 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003271 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003272 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003273 if (needs_write_barrier) {
3274 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003275 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003276 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003277 // Arm encoding have some additional constraints for ldrexd/strexd:
3278 // - registers need to be consecutive
3279 // - the first register should be even but not R14.
3280 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3281 // enable Arm encoding.
3282 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3283
3284 locations->AddTemp(Location::RequiresRegister());
3285 locations->AddTemp(Location::RequiresRegister());
3286 if (field_type == Primitive::kPrimDouble) {
3287 // For doubles we need two more registers to copy the value.
3288 locations->AddTemp(Location::RegisterLocation(R2));
3289 locations->AddTemp(Location::RegisterLocation(R3));
3290 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003291 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003292}
3293
Calin Juravle52c48962014-12-16 17:02:57 +00003294void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003295 const FieldInfo& field_info,
3296 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003297 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3298
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003299 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003300 Register base = locations->InAt(0).AsRegister<Register>();
3301 Location value = locations->InAt(1);
3302
3303 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003304 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003305 Primitive::Type field_type = field_info.GetFieldType();
3306 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003307 bool needs_write_barrier =
3308 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003309
3310 if (is_volatile) {
3311 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3312 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003313
3314 switch (field_type) {
3315 case Primitive::kPrimBoolean:
3316 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003317 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003318 break;
3319 }
3320
3321 case Primitive::kPrimShort:
3322 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003323 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003324 break;
3325 }
3326
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003327 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003328 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003329 if (kPoisonHeapReferences && needs_write_barrier) {
3330 // Note that in the case where `value` is a null reference,
3331 // we do not enter this block, as a null reference does not
3332 // need poisoning.
3333 DCHECK_EQ(field_type, Primitive::kPrimNot);
3334 Register temp = locations->GetTemp(0).AsRegister<Register>();
3335 __ Mov(temp, value.AsRegister<Register>());
3336 __ PoisonHeapReference(temp);
3337 __ StoreToOffset(kStoreWord, temp, base, offset);
3338 } else {
3339 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3340 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003341 break;
3342 }
3343
3344 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003345 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003346 GenerateWideAtomicStore(base, offset,
3347 value.AsRegisterPairLow<Register>(),
3348 value.AsRegisterPairHigh<Register>(),
3349 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003350 locations->GetTemp(1).AsRegister<Register>(),
3351 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003352 } else {
3353 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003354 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003355 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003356 break;
3357 }
3358
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003359 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003360 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003361 break;
3362 }
3363
3364 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003365 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003366 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003367 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3368 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3369
3370 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3371
3372 GenerateWideAtomicStore(base, offset,
3373 value_reg_lo,
3374 value_reg_hi,
3375 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003376 locations->GetTemp(3).AsRegister<Register>(),
3377 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003378 } else {
3379 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003380 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003381 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003382 break;
3383 }
3384
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003385 case Primitive::kPrimVoid:
3386 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003387 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003388 }
Calin Juravle52c48962014-12-16 17:02:57 +00003389
Calin Juravle77520bc2015-01-12 18:45:46 +00003390 // Longs and doubles are handled in the switch.
3391 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3392 codegen_->MaybeRecordImplicitNullCheck(instruction);
3393 }
3394
3395 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3396 Register temp = locations->GetTemp(0).AsRegister<Register>();
3397 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003398 codegen_->MarkGCCard(
3399 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003400 }
3401
Calin Juravle52c48962014-12-16 17:02:57 +00003402 if (is_volatile) {
3403 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3404 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003405}
3406
Calin Juravle52c48962014-12-16 17:02:57 +00003407void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3408 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003409 LocationSummary* locations =
3410 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003411 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003412
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003413 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003414 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003415 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003416 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003417
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003418 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3419 locations->SetOut(Location::RequiresFpuRegister());
3420 } else {
3421 locations->SetOut(Location::RequiresRegister(),
3422 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3423 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003424 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003425 // Arm encoding have some additional constraints for ldrexd/strexd:
3426 // - registers need to be consecutive
3427 // - the first register should be even but not R14.
3428 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3429 // enable Arm encoding.
3430 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3431 locations->AddTemp(Location::RequiresRegister());
3432 locations->AddTemp(Location::RequiresRegister());
3433 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003434}
3435
Calin Juravle52c48962014-12-16 17:02:57 +00003436void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3437 const FieldInfo& field_info) {
3438 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003439
Calin Juravle52c48962014-12-16 17:02:57 +00003440 LocationSummary* locations = instruction->GetLocations();
3441 Register base = locations->InAt(0).AsRegister<Register>();
3442 Location out = locations->Out();
3443 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003444 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003445 Primitive::Type field_type = field_info.GetFieldType();
3446 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3447
3448 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003449 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003450 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003451 break;
3452 }
3453
3454 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003455 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003456 break;
3457 }
3458
3459 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003460 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003461 break;
3462 }
3463
3464 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003465 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003466 break;
3467 }
3468
3469 case Primitive::kPrimInt:
3470 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003471 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003472 break;
3473 }
3474
3475 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003476 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003477 GenerateWideAtomicLoad(base, offset,
3478 out.AsRegisterPairLow<Register>(),
3479 out.AsRegisterPairHigh<Register>());
3480 } else {
3481 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3482 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003483 break;
3484 }
3485
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003486 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003487 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003488 break;
3489 }
3490
3491 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003492 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003493 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003494 Register lo = locations->GetTemp(0).AsRegister<Register>();
3495 Register hi = locations->GetTemp(1).AsRegister<Register>();
3496 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003497 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003498 __ vmovdrr(out_reg, lo, hi);
3499 } else {
3500 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003501 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003502 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003503 break;
3504 }
3505
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003506 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003507 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003508 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003509 }
Calin Juravle52c48962014-12-16 17:02:57 +00003510
Calin Juravle77520bc2015-01-12 18:45:46 +00003511 // Doubles are handled in the switch.
3512 if (field_type != Primitive::kPrimDouble) {
3513 codegen_->MaybeRecordImplicitNullCheck(instruction);
3514 }
3515
Calin Juravle52c48962014-12-16 17:02:57 +00003516 if (is_volatile) {
3517 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3518 }
Roland Levillain4d027112015-07-01 15:41:14 +01003519
3520 if (field_type == Primitive::kPrimNot) {
3521 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3522 }
Calin Juravle52c48962014-12-16 17:02:57 +00003523}
3524
3525void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3526 HandleFieldSet(instruction, instruction->GetFieldInfo());
3527}
3528
3529void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003530 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003531}
3532
3533void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3534 HandleFieldGet(instruction, instruction->GetFieldInfo());
3535}
3536
3537void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3538 HandleFieldGet(instruction, instruction->GetFieldInfo());
3539}
3540
3541void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3542 HandleFieldGet(instruction, instruction->GetFieldInfo());
3543}
3544
3545void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3546 HandleFieldGet(instruction, instruction->GetFieldInfo());
3547}
3548
3549void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3550 HandleFieldSet(instruction, instruction->GetFieldInfo());
3551}
3552
3553void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003554 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003555}
3556
3557void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003558 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3559 ? LocationSummary::kCallOnSlowPath
3560 : LocationSummary::kNoCall;
3561 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003562 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003563 if (instruction->HasUses()) {
3564 locations->SetOut(Location::SameAsFirstInput());
3565 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003566}
3567
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003568void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003569 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3570 return;
3571 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003572 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003573
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003574 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3575 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3576}
3577
3578void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003579 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003580 codegen_->AddSlowPath(slow_path);
3581
3582 LocationSummary* locations = instruction->GetLocations();
3583 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003584
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003585 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003586}
3587
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003588void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003589 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003590 GenerateImplicitNullCheck(instruction);
3591 } else {
3592 GenerateExplicitNullCheck(instruction);
3593 }
3594}
3595
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003596void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003599 locations->SetInAt(0, Location::RequiresRegister());
3600 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003601 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3602 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3603 } else {
3604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3605 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003606}
3607
3608void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3609 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003610 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003611 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003612 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003613
Roland Levillain4d027112015-07-01 15:41:14 +01003614 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003615 case Primitive::kPrimBoolean: {
3616 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003617 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003618 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003619 size_t offset =
3620 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003621 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3622 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003623 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003624 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3625 }
3626 break;
3627 }
3628
3629 case Primitive::kPrimByte: {
3630 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_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_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003635 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3636 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003638 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3639 }
3640 break;
3641 }
3642
3643 case Primitive::kPrimShort: {
3644 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 Register out = locations->Out().AsRegister<Register>();
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_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003649 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3650 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003651 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003652 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3653 }
3654 break;
3655 }
3656
3657 case Primitive::kPrimChar: {
3658 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003659 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003660 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003661 size_t offset =
3662 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003663 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3664 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003666 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3667 }
3668 break;
3669 }
3670
3671 case Primitive::kPrimInt:
3672 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003673 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3674 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003675 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003676 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003677 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003678 size_t offset =
3679 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003680 __ LoadFromOffset(kLoadWord, out, obj, offset);
3681 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003682 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003683 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3684 }
3685 break;
3686 }
3687
3688 case Primitive::kPrimLong: {
3689 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003690 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003691 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003692 size_t offset =
3693 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003694 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003695 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003696 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003697 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003698 }
3699 break;
3700 }
3701
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003702 case Primitive::kPrimFloat: {
3703 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3704 Location out = locations->Out();
3705 DCHECK(out.IsFpuRegister());
3706 if (index.IsConstant()) {
3707 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3708 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3709 } else {
3710 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3711 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3712 }
3713 break;
3714 }
3715
3716 case Primitive::kPrimDouble: {
3717 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3718 Location out = locations->Out();
3719 DCHECK(out.IsFpuRegisterPair());
3720 if (index.IsConstant()) {
3721 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3722 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3723 } else {
3724 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3725 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3726 }
3727 break;
3728 }
3729
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003730 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003731 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003732 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003733 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003734 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003735
3736 if (type == Primitive::kPrimNot) {
3737 Register out = locations->Out().AsRegister<Register>();
3738 __ MaybeUnpoisonHeapReference(out);
3739 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003740}
3741
3742void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003743 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003744
3745 bool needs_write_barrier =
3746 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3747 bool needs_runtime_call = instruction->NeedsTypeCheck();
3748
Nicolas Geoffray39468442014-09-02 15:17:15 +01003749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003750 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
3751 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003752 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003753 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3754 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3755 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003756 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003757 locations->SetInAt(0, Location::RequiresRegister());
3758 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003759 if (Primitive::IsFloatingPointType(value_type)) {
3760 locations->SetInAt(2, Location::RequiresFpuRegister());
3761 } else {
3762 locations->SetInAt(2, Location::RequiresRegister());
3763 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003764
3765 if (needs_write_barrier) {
3766 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01003767 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003768 locations->AddTemp(Location::RequiresRegister());
3769 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771}
3772
3773void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3774 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003776 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003777 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003778 bool needs_runtime_call = locations->WillCall();
3779 bool needs_write_barrier =
3780 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003781
3782 switch (value_type) {
3783 case Primitive::kPrimBoolean:
3784 case Primitive::kPrimByte: {
3785 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003786 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003787 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003788 size_t offset =
3789 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003790 __ StoreToOffset(kStoreByte, value, obj, offset);
3791 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003792 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003793 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3794 }
3795 break;
3796 }
3797
3798 case Primitive::kPrimShort:
3799 case Primitive::kPrimChar: {
3800 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003801 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003802 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003803 size_t offset =
3804 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003805 __ StoreToOffset(kStoreHalfword, value, obj, offset);
3806 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003807 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003808 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3809 }
3810 break;
3811 }
3812
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003813 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003815 if (!needs_runtime_call) {
3816 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003817 Register value = locations->InAt(2).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01003818 Register source = value;
3819 if (kPoisonHeapReferences && needs_write_barrier) {
3820 // Note that in the case where `value` is a null reference,
3821 // we do not enter this block, as a null reference does not
3822 // need poisoning.
3823 DCHECK_EQ(value_type, Primitive::kPrimNot);
3824 Register temp = locations->GetTemp(0).AsRegister<Register>();
3825 __ Mov(temp, value);
3826 __ PoisonHeapReference(temp);
3827 source = temp;
3828 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003829 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003830 size_t offset =
3831 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain4d027112015-07-01 15:41:14 +01003832 __ StoreToOffset(kStoreWord, source, obj, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003833 } else {
3834 DCHECK(index.IsRegister()) << index;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01003836 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003837 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003838 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003839 if (needs_write_barrier) {
3840 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003841 Register temp = locations->GetTemp(0).AsRegister<Register>();
3842 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003843 codegen_->MarkGCCard(temp, card, obj, value, instruction->GetValueCanBeNull());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003844 }
3845 } else {
3846 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain4d027112015-07-01 15:41:14 +01003847 // Note: if heap poisoning is enabled, pAputObject takes cares
3848 // of poisoning the reference.
Roland Levillain199f3362014-11-27 17:15:16 +00003849 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
3850 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003851 instruction->GetDexPc(),
3852 nullptr);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003853 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003854 break;
3855 }
3856
3857 case Primitive::kPrimLong: {
3858 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003859 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003860 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003861 size_t offset =
3862 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003863 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003864 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003865 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003866 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003867 }
3868 break;
3869 }
3870
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003871 case Primitive::kPrimFloat: {
3872 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3873 Location value = locations->InAt(2);
3874 DCHECK(value.IsFpuRegister());
3875 if (index.IsConstant()) {
3876 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3877 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), obj, offset);
3878 } else {
3879 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3880 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
3881 }
3882 break;
3883 }
3884
3885 case Primitive::kPrimDouble: {
3886 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3887 Location value = locations->InAt(2);
3888 DCHECK(value.IsFpuRegisterPair());
3889 if (index.IsConstant()) {
3890 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3891 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3892 } else {
3893 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3894 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3895 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003896
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003897 break;
3898 }
3899
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003900 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003901 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003902 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003903 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003904
3905 // Ints and objects are handled in the switch.
3906 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
3907 codegen_->MaybeRecordImplicitNullCheck(instruction);
3908 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003909}
3910
3911void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003912 LocationSummary* locations =
3913 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003914 locations->SetInAt(0, Location::RequiresRegister());
3915 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003916}
3917
3918void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
3919 LocationSummary* locations = instruction->GetLocations();
3920 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003921 Register obj = locations->InAt(0).AsRegister<Register>();
3922 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003923 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003924 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003925}
3926
3927void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003928 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3929 ? LocationSummary::kCallOnSlowPath
3930 : LocationSummary::kNoCall;
3931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003932 locations->SetInAt(0, Location::RequiresRegister());
3933 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003934 if (instruction->HasUses()) {
3935 locations->SetOut(Location::SameAsFirstInput());
3936 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003937}
3938
3939void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
3940 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07003941 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003942 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003943 codegen_->AddSlowPath(slow_path);
3944
Roland Levillain271ab9c2014-11-27 15:23:57 +00003945 Register index = locations->InAt(0).AsRegister<Register>();
3946 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003947
3948 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01003949 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003950}
3951
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003952void CodeGeneratorARM::MarkGCCard(Register temp,
3953 Register card,
3954 Register object,
3955 Register value,
3956 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003957 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003958 if (can_be_null) {
3959 __ CompareAndBranchIfZero(value, &is_null);
3960 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003961 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
3962 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
3963 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003964 if (can_be_null) {
3965 __ Bind(&is_null);
3966 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003967}
3968
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003969void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
3970 temp->SetLocations(nullptr);
3971}
3972
3973void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
3974 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003975 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003976}
3977
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003978void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003979 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003980 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003981}
3982
3983void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003984 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3985}
3986
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003987void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
3988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3989}
3990
3991void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003992 HBasicBlock* block = instruction->GetBlock();
3993 if (block->GetLoopInformation() != nullptr) {
3994 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3995 // The back edge will generate the suspend check.
3996 return;
3997 }
3998 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3999 // The goto will generate the suspend check.
4000 return;
4001 }
4002 GenerateSuspendCheck(instruction, nullptr);
4003}
4004
4005void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4006 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004007 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004008 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4009 if (slow_path == nullptr) {
4010 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4011 instruction->SetSlowPath(slow_path);
4012 codegen_->AddSlowPath(slow_path);
4013 if (successor != nullptr) {
4014 DCHECK(successor->IsLoopHeader());
4015 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4016 }
4017 } else {
4018 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4019 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004020
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004021 __ LoadFromOffset(
4022 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004023 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004024 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004025 __ Bind(slow_path->GetReturnLabel());
4026 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004027 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004028 __ b(slow_path->GetEntryLabel());
4029 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004030}
4031
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004032ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4033 return codegen_->GetAssembler();
4034}
4035
4036void ParallelMoveResolverARM::EmitMove(size_t index) {
4037 MoveOperands* move = moves_.Get(index);
4038 Location source = move->GetSource();
4039 Location destination = move->GetDestination();
4040
4041 if (source.IsRegister()) {
4042 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004044 } else {
4045 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004046 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004047 SP, destination.GetStackIndex());
4048 }
4049 } else if (source.IsStackSlot()) {
4050 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004051 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004052 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004053 } else if (destination.IsFpuRegister()) {
4054 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004055 } else {
4056 DCHECK(destination.IsStackSlot());
4057 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4058 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4059 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004060 } else if (source.IsFpuRegister()) {
4061 if (destination.IsFpuRegister()) {
4062 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004063 } else {
4064 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004065 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4066 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004067 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004068 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004069 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4070 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004071 } else if (destination.IsRegisterPair()) {
4072 DCHECK(ExpectedPairLayout(destination));
4073 __ LoadFromOffset(
4074 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4075 } else {
4076 DCHECK(destination.IsFpuRegisterPair()) << destination;
4077 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4078 SP,
4079 source.GetStackIndex());
4080 }
4081 } else if (source.IsRegisterPair()) {
4082 if (destination.IsRegisterPair()) {
4083 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4084 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4085 } else {
4086 DCHECK(destination.IsDoubleStackSlot()) << destination;
4087 DCHECK(ExpectedPairLayout(source));
4088 __ StoreToOffset(
4089 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4090 }
4091 } else if (source.IsFpuRegisterPair()) {
4092 if (destination.IsFpuRegisterPair()) {
4093 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4094 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4095 } else {
4096 DCHECK(destination.IsDoubleStackSlot()) << destination;
4097 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4098 SP,
4099 destination.GetStackIndex());
4100 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004101 } else {
4102 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004103 HConstant* constant = source.GetConstant();
4104 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4105 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004106 if (destination.IsRegister()) {
4107 __ LoadImmediate(destination.AsRegister<Register>(), value);
4108 } else {
4109 DCHECK(destination.IsStackSlot());
4110 __ LoadImmediate(IP, value);
4111 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4112 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004113 } else if (constant->IsLongConstant()) {
4114 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004115 if (destination.IsRegisterPair()) {
4116 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4117 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004118 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004119 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004120 __ LoadImmediate(IP, Low32Bits(value));
4121 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4122 __ LoadImmediate(IP, High32Bits(value));
4123 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4124 }
4125 } else if (constant->IsDoubleConstant()) {
4126 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004127 if (destination.IsFpuRegisterPair()) {
4128 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004129 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004130 DCHECK(destination.IsDoubleStackSlot()) << destination;
4131 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004132 __ LoadImmediate(IP, Low32Bits(int_value));
4133 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4134 __ LoadImmediate(IP, High32Bits(int_value));
4135 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4136 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004137 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004138 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004139 float value = constant->AsFloatConstant()->GetValue();
4140 if (destination.IsFpuRegister()) {
4141 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4142 } else {
4143 DCHECK(destination.IsStackSlot());
4144 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4145 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4146 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004147 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004148 }
4149}
4150
4151void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4152 __ Mov(IP, reg);
4153 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4154 __ StoreToOffset(kStoreWord, IP, SP, mem);
4155}
4156
4157void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4158 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4159 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4160 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4161 SP, mem1 + stack_offset);
4162 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4163 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4164 SP, mem2 + stack_offset);
4165 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4166}
4167
4168void ParallelMoveResolverARM::EmitSwap(size_t index) {
4169 MoveOperands* move = moves_.Get(index);
4170 Location source = move->GetSource();
4171 Location destination = move->GetDestination();
4172
4173 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004174 DCHECK_NE(source.AsRegister<Register>(), IP);
4175 DCHECK_NE(destination.AsRegister<Register>(), IP);
4176 __ Mov(IP, source.AsRegister<Register>());
4177 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4178 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004179 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004180 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004181 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004182 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004183 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4184 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004185 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004186 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004187 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004188 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004189 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004190 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004191 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004192 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004193 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4194 destination.AsRegisterPairHigh<Register>(),
4195 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004196 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004197 Register low_reg = source.IsRegisterPair()
4198 ? source.AsRegisterPairLow<Register>()
4199 : destination.AsRegisterPairLow<Register>();
4200 int mem = source.IsRegisterPair()
4201 ? destination.GetStackIndex()
4202 : source.GetStackIndex();
4203 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004204 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004205 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004206 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004207 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004208 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4209 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004210 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004211 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004212 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004213 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4214 DRegister reg = source.IsFpuRegisterPair()
4215 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4216 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4217 int mem = source.IsFpuRegisterPair()
4218 ? destination.GetStackIndex()
4219 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004220 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004221 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004222 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004223 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4224 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4225 : destination.AsFpuRegister<SRegister>();
4226 int mem = source.IsFpuRegister()
4227 ? destination.GetStackIndex()
4228 : source.GetStackIndex();
4229
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004230 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004231 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004232 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004233 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004234 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4235 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004236 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004237 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004238 }
4239}
4240
4241void ParallelMoveResolverARM::SpillScratch(int reg) {
4242 __ Push(static_cast<Register>(reg));
4243}
4244
4245void ParallelMoveResolverARM::RestoreScratch(int reg) {
4246 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004247}
4248
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004249void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004250 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4251 ? LocationSummary::kCallOnSlowPath
4252 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004253 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004254 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004255 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004256 locations->SetOut(Location::RequiresRegister());
4257}
4258
4259void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004260 LocationSummary* locations = cls->GetLocations();
4261 Register out = locations->Out().AsRegister<Register>();
4262 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004263 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004264 DCHECK(!cls->CanCallRuntime());
4265 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004266 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004267 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004268 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004269 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004270 __ LoadFromOffset(kLoadWord,
4271 out,
4272 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004273 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004274 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004275 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004276
Andreas Gampe85b62f22015-09-09 13:15:38 -07004277 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004278 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4279 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004280 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004281 if (cls->MustGenerateClinitCheck()) {
4282 GenerateClassInitializationCheck(slow_path, out);
4283 } else {
4284 __ Bind(slow_path->GetExitLabel());
4285 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004286 }
4287}
4288
4289void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4290 LocationSummary* locations =
4291 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4292 locations->SetInAt(0, Location::RequiresRegister());
4293 if (check->HasUses()) {
4294 locations->SetOut(Location::SameAsFirstInput());
4295 }
4296}
4297
4298void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004299 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004300 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004301 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004302 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004303 GenerateClassInitializationCheck(slow_path,
4304 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004305}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004306
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004307void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004308 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004309 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4310 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4311 __ b(slow_path->GetEntryLabel(), LT);
4312 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4313 // properly. Therefore, we do a memory fence.
4314 __ dmb(ISH);
4315 __ Bind(slow_path->GetExitLabel());
4316}
4317
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004318void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4319 LocationSummary* locations =
4320 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004321 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004322 locations->SetOut(Location::RequiresRegister());
4323}
4324
4325void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004326 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004327 codegen_->AddSlowPath(slow_path);
4328
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004329 LocationSummary* locations = load->GetLocations();
4330 Register out = locations->Out().AsRegister<Register>();
4331 Register current_method = locations->InAt(0).AsRegister<Register>();
4332 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004333 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004334 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004335 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004336 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004337 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004338 __ Bind(slow_path->GetExitLabel());
4339}
4340
David Brazdilcb1c0552015-08-04 16:22:25 +01004341static int32_t GetExceptionTlsOffset() {
4342 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4343}
4344
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004345void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4346 LocationSummary* locations =
4347 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4348 locations->SetOut(Location::RequiresRegister());
4349}
4350
4351void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004352 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004353 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4354}
4355
4356void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4357 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4358}
4359
4360void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004361 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004362 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004363}
4364
4365void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4366 LocationSummary* locations =
4367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4368 InvokeRuntimeCallingConvention calling_convention;
4369 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4370}
4371
4372void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4373 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004374 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004375}
4376
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004377void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004378 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4379 switch (instruction->GetTypeCheckKind()) {
4380 case TypeCheckKind::kExactCheck:
4381 case TypeCheckKind::kAbstractClassCheck:
4382 case TypeCheckKind::kClassHierarchyCheck:
4383 case TypeCheckKind::kArrayObjectCheck:
4384 call_kind = LocationSummary::kNoCall;
4385 break;
4386 case TypeCheckKind::kInterfaceCheck:
4387 call_kind = LocationSummary::kCall;
4388 break;
4389 case TypeCheckKind::kArrayCheck:
4390 call_kind = LocationSummary::kCallOnSlowPath;
4391 break;
4392 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004394 if (call_kind != LocationSummary::kCall) {
4395 locations->SetInAt(0, Location::RequiresRegister());
4396 locations->SetInAt(1, Location::RequiresRegister());
4397 // The out register is used as a temporary, so it overlaps with the inputs.
4398 // Note that TypeCheckSlowPathARM uses this register too.
4399 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4400 } else {
4401 InvokeRuntimeCallingConvention calling_convention;
4402 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4403 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4404 locations->SetOut(Location::RegisterLocation(R0));
4405 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004406}
4407
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004408void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004409 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004410 Register obj = locations->InAt(0).AsRegister<Register>();
4411 Register cls = locations->InAt(1).AsRegister<Register>();
4412 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004413 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004414 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4415 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4416 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004417 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004418 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004419
4420 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004421 // avoid null check if we know obj is not null.
4422 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004423 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004424 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004425
4426 // In case of an interface check, we put the object class into the object register.
4427 // This is safe, as the register is caller-save, and the object must be in another
4428 // register if it survives the runtime call.
4429 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
4430 ? obj
4431 : out;
4432 __ LoadFromOffset(kLoadWord, target, obj, class_offset);
4433 __ MaybeUnpoisonHeapReference(target);
4434
4435 switch (instruction->GetTypeCheckKind()) {
4436 case TypeCheckKind::kExactCheck: {
4437 __ cmp(out, ShifterOperand(cls));
4438 // Classes must be equal for the instanceof to succeed.
4439 __ b(&zero, NE);
4440 __ LoadImmediate(out, 1);
4441 __ b(&done);
4442 break;
4443 }
4444 case TypeCheckKind::kAbstractClassCheck: {
4445 // If the class is abstract, we eagerly fetch the super class of the
4446 // object to avoid doing a comparison we know will fail.
4447 Label loop;
4448 __ Bind(&loop);
4449 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4450 __ MaybeUnpoisonHeapReference(out);
4451 // If `out` is null, we use it for the result, and jump to `done`.
4452 __ CompareAndBranchIfZero(out, &done);
4453 __ cmp(out, ShifterOperand(cls));
4454 __ b(&loop, NE);
4455 __ LoadImmediate(out, 1);
4456 if (zero.IsLinked()) {
4457 __ b(&done);
4458 }
4459 break;
4460 }
4461 case TypeCheckKind::kClassHierarchyCheck: {
4462 // Walk over the class hierarchy to find a match.
4463 Label loop, success;
4464 __ Bind(&loop);
4465 __ cmp(out, ShifterOperand(cls));
4466 __ b(&success, EQ);
4467 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4468 __ MaybeUnpoisonHeapReference(out);
4469 __ CompareAndBranchIfNonZero(out, &loop);
4470 // If `out` is null, we use it for the result, and jump to `done`.
4471 __ b(&done);
4472 __ Bind(&success);
4473 __ LoadImmediate(out, 1);
4474 if (zero.IsLinked()) {
4475 __ b(&done);
4476 }
4477 break;
4478 }
4479 case TypeCheckKind::kArrayObjectCheck: {
4480 // Just need to check that the object's class is a non primitive array.
4481 __ LoadFromOffset(kLoadWord, out, out, component_offset);
4482 __ MaybeUnpoisonHeapReference(out);
4483 // If `out` is null, we use it for the result, and jump to `done`.
4484 __ CompareAndBranchIfZero(out, &done);
4485 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4486 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4487 __ CompareAndBranchIfNonZero(out, &zero);
4488 __ LoadImmediate(out, 1);
4489 __ b(&done);
4490 break;
4491 }
4492 case TypeCheckKind::kArrayCheck: {
4493 __ cmp(out, ShifterOperand(cls));
4494 DCHECK(locations->OnlyCallsOnSlowPath());
4495 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4496 instruction, /* is_fatal */ false);
4497 codegen_->AddSlowPath(slow_path);
4498 __ b(slow_path->GetEntryLabel(), NE);
4499 __ LoadImmediate(out, 1);
4500 if (zero.IsLinked()) {
4501 __ b(&done);
4502 }
4503 break;
4504 }
4505
4506 case TypeCheckKind::kInterfaceCheck:
4507 default: {
4508 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4509 instruction,
4510 instruction->GetDexPc(),
4511 nullptr);
4512 if (zero.IsLinked()) {
4513 __ b(&done);
4514 }
4515 break;
4516 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004517 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004518
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004519 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004520 __ Bind(&zero);
4521 __ LoadImmediate(out, 0);
4522 }
4523
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004524 if (done.IsLinked()) {
4525 __ Bind(&done);
4526 }
4527
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004528 if (slow_path != nullptr) {
4529 __ Bind(slow_path->GetExitLabel());
4530 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004531}
4532
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004533void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004534 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4535 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4536
4537 switch (instruction->GetTypeCheckKind()) {
4538 case TypeCheckKind::kExactCheck:
4539 case TypeCheckKind::kAbstractClassCheck:
4540 case TypeCheckKind::kClassHierarchyCheck:
4541 case TypeCheckKind::kArrayObjectCheck:
4542 call_kind = throws_into_catch
4543 ? LocationSummary::kCallOnSlowPath
4544 : LocationSummary::kNoCall;
4545 break;
4546 case TypeCheckKind::kInterfaceCheck:
4547 call_kind = LocationSummary::kCall;
4548 break;
4549 case TypeCheckKind::kArrayCheck:
4550 call_kind = LocationSummary::kCallOnSlowPath;
4551 break;
4552 }
4553
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004555 instruction, call_kind);
4556 if (call_kind != LocationSummary::kCall) {
4557 locations->SetInAt(0, Location::RequiresRegister());
4558 locations->SetInAt(1, Location::RequiresRegister());
4559 // Note that TypeCheckSlowPathARM uses this register too.
4560 locations->AddTemp(Location::RequiresRegister());
4561 } else {
4562 InvokeRuntimeCallingConvention calling_convention;
4563 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4564 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4565 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004566}
4567
4568void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4569 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004570 Register obj = locations->InAt(0).AsRegister<Register>();
4571 Register cls = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004572 Register temp = locations->WillCall()
4573 ? Register(kNoRegister)
4574 : locations->GetTemp(0).AsRegister<Register>();
4575
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004576 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004577 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4578 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4579 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4580 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004581
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004582 if (!locations->WillCall()) {
4583 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4584 instruction, !locations->CanCall());
4585 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004586 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004587
4588 Label done;
4589 // Avoid null check if we know obj is not null.
4590 if (instruction->MustDoNullCheck()) {
4591 __ CompareAndBranchIfZero(obj, &done);
4592 }
4593
4594 if (locations->WillCall()) {
4595 __ LoadFromOffset(kLoadWord, obj, obj, class_offset);
4596 __ MaybeUnpoisonHeapReference(obj);
4597 } else {
4598 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4599 __ MaybeUnpoisonHeapReference(temp);
4600 }
4601
4602 switch (instruction->GetTypeCheckKind()) {
4603 case TypeCheckKind::kExactCheck:
4604 case TypeCheckKind::kArrayCheck: {
4605 __ cmp(temp, ShifterOperand(cls));
4606 // Jump to slow path for throwing the exception or doing a
4607 // more involved array check.
4608 __ b(slow_path->GetEntryLabel(), NE);
4609 break;
4610 }
4611 case TypeCheckKind::kAbstractClassCheck: {
4612 // If the class is abstract, we eagerly fetch the super class of the
4613 // object to avoid doing a comparison we know will fail.
4614 Label loop;
4615 __ Bind(&loop);
4616 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4617 __ MaybeUnpoisonHeapReference(temp);
4618 // Jump to the slow path to throw the exception.
4619 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4620 __ cmp(temp, ShifterOperand(cls));
4621 __ b(&loop, NE);
4622 break;
4623 }
4624 case TypeCheckKind::kClassHierarchyCheck: {
4625 // Walk over the class hierarchy to find a match.
4626 Label loop, success;
4627 __ Bind(&loop);
4628 __ cmp(temp, ShifterOperand(cls));
4629 __ b(&success, EQ);
4630 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4631 __ MaybeUnpoisonHeapReference(temp);
4632 __ CompareAndBranchIfNonZero(temp, &loop);
4633 // Jump to the slow path to throw the exception.
4634 __ b(slow_path->GetEntryLabel());
4635 __ Bind(&success);
4636 break;
4637 }
4638 case TypeCheckKind::kArrayObjectCheck: {
4639 // Just need to check that the object's class is a non primitive array.
4640 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
4641 __ MaybeUnpoisonHeapReference(temp);
4642 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4643 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
4644 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4645 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
4646 break;
4647 }
4648 case TypeCheckKind::kInterfaceCheck:
4649 default:
4650 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
4651 instruction,
4652 instruction->GetDexPc(),
4653 nullptr);
4654 break;
4655 }
4656 __ Bind(&done);
4657
4658 if (slow_path != nullptr) {
4659 __ Bind(slow_path->GetExitLabel());
4660 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004661}
4662
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004663void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4664 LocationSummary* locations =
4665 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4666 InvokeRuntimeCallingConvention calling_convention;
4667 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4668}
4669
4670void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4671 codegen_->InvokeRuntime(instruction->IsEnter()
4672 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4673 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004674 instruction->GetDexPc(),
4675 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004676}
4677
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004678void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4679void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4680void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4681
4682void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4683 LocationSummary* locations =
4684 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4685 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4686 || instruction->GetResultType() == Primitive::kPrimLong);
4687 locations->SetInAt(0, Location::RequiresRegister());
4688 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004689 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004690}
4691
4692void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4693 HandleBitwiseOperation(instruction);
4694}
4695
4696void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4697 HandleBitwiseOperation(instruction);
4698}
4699
4700void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4701 HandleBitwiseOperation(instruction);
4702}
4703
4704void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4705 LocationSummary* locations = instruction->GetLocations();
4706
4707 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004708 Register first = locations->InAt(0).AsRegister<Register>();
4709 Register second = locations->InAt(1).AsRegister<Register>();
4710 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004711 if (instruction->IsAnd()) {
4712 __ and_(out, first, ShifterOperand(second));
4713 } else if (instruction->IsOr()) {
4714 __ orr(out, first, ShifterOperand(second));
4715 } else {
4716 DCHECK(instruction->IsXor());
4717 __ eor(out, first, ShifterOperand(second));
4718 }
4719 } else {
4720 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4721 Location first = locations->InAt(0);
4722 Location second = locations->InAt(1);
4723 Location out = locations->Out();
4724 if (instruction->IsAnd()) {
4725 __ and_(out.AsRegisterPairLow<Register>(),
4726 first.AsRegisterPairLow<Register>(),
4727 ShifterOperand(second.AsRegisterPairLow<Register>()));
4728 __ and_(out.AsRegisterPairHigh<Register>(),
4729 first.AsRegisterPairHigh<Register>(),
4730 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4731 } else if (instruction->IsOr()) {
4732 __ orr(out.AsRegisterPairLow<Register>(),
4733 first.AsRegisterPairLow<Register>(),
4734 ShifterOperand(second.AsRegisterPairLow<Register>()));
4735 __ orr(out.AsRegisterPairHigh<Register>(),
4736 first.AsRegisterPairHigh<Register>(),
4737 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4738 } else {
4739 DCHECK(instruction->IsXor());
4740 __ eor(out.AsRegisterPairLow<Register>(),
4741 first.AsRegisterPairLow<Register>(),
4742 ShifterOperand(second.AsRegisterPairLow<Register>()));
4743 __ eor(out.AsRegisterPairHigh<Register>(),
4744 first.AsRegisterPairHigh<Register>(),
4745 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4746 }
4747 }
4748}
4749
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004750void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004751 // For better instruction scheduling we load the direct code pointer before the method pointer.
4752 bool direct_code_loaded = false;
4753 switch (invoke->GetCodePtrLocation()) {
4754 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4755 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
4756 break;
4757 }
4758 // Calls across dex files are more likely to exceed the available BL range,
4759 // so use absolute patch by falling through to kDirectCodeFixup.
4760 FALLTHROUGH_INTENDED;
4761 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4762 // LR = code address from literal pool with link-time patch.
4763 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4764 direct_code_loaded = true;
4765 break;
4766 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4767 // LR = invoke->GetDirectCodePtr();
4768 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
4769 direct_code_loaded = true;
4770 break;
4771 default:
4772 break;
4773 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004774
Vladimir Marko58155012015-08-19 12:49:41 +00004775 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4776 switch (invoke->GetMethodLoadKind()) {
4777 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4778 // temp = thread->string_init_entrypoint
4779 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
4780 break;
4781 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
4782 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4783 break;
4784 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4785 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4786 break;
4787 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4788 __ LoadLiteral(temp.AsRegister<Register>(),
4789 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4790 break;
4791 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
4792 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
4793 FALLTHROUGH_INTENDED;
4794 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
4795 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4796 Register method_reg;
4797 Register reg = temp.AsRegister<Register>();
4798 if (current_method.IsRegister()) {
4799 method_reg = current_method.AsRegister<Register>();
4800 } else {
4801 DCHECK(invoke->GetLocations()->Intrinsified());
4802 DCHECK(!current_method.IsValid());
4803 method_reg = reg;
4804 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
4805 }
4806 // temp = current_method->dex_cache_resolved_methods_;
4807 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01004808 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
4809 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00004810 // temp = temp[index_in_cache]
4811 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4812 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
4813 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01004814 }
Vladimir Marko58155012015-08-19 12:49:41 +00004815 }
4816
4817 switch (invoke->GetCodePtrLocation()) {
4818 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4819 __ bl(GetFrameEntryLabel());
4820 break;
4821 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4822 if (!direct_code_loaded) {
4823 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4824 __ Bind(&relative_call_patches_.back().label);
4825 Label label;
4826 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
4827 __ Bind(&label);
4828 break;
4829 }
4830 // If we loaded the direct code above, fall through.
4831 FALLTHROUGH_INTENDED;
4832 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4833 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4834 // LR prepared above for better instruction scheduling.
4835 DCHECK(direct_code_loaded);
4836 // LR()
4837 __ blx(LR);
4838 break;
4839 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4840 // LR = callee_method->entry_point_from_quick_compiled_code_
4841 __ LoadFromOffset(
4842 kLoadWord, LR, callee_method.AsRegister<Register>(),
4843 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
4844 // LR()
4845 __ blx(LR);
4846 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004847 }
4848
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08004849 DCHECK(!IsLeafMethod());
4850}
4851
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004852void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
4853 Register temp = temp_location.AsRegister<Register>();
4854 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4855 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
4856 LocationSummary* locations = invoke->GetLocations();
4857 Location receiver = locations->InAt(0);
4858 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4859 // temp = object->GetClass();
4860 DCHECK(receiver.IsRegister());
4861 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4862 MaybeRecordImplicitNullCheck(invoke);
4863 __ MaybeUnpoisonHeapReference(temp);
4864 // temp = temp->GetMethodAt(method_offset);
4865 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4866 kArmWordSize).Int32Value();
4867 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4868 // LR = temp->GetEntryPoint();
4869 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
4870 // LR();
4871 __ blx(LR);
4872}
4873
Vladimir Marko58155012015-08-19 12:49:41 +00004874void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4875 DCHECK(linker_patches->empty());
4876 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
4877 linker_patches->reserve(size);
4878 for (const auto& entry : method_patches_) {
4879 const MethodReference& target_method = entry.first;
4880 Literal* literal = entry.second;
4881 DCHECK(literal->GetLabel()->IsBound());
4882 uint32_t literal_offset = literal->GetLabel()->Position();
4883 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4884 target_method.dex_file,
4885 target_method.dex_method_index));
4886 }
4887 for (const auto& entry : call_patches_) {
4888 const MethodReference& target_method = entry.first;
4889 Literal* literal = entry.second;
4890 DCHECK(literal->GetLabel()->IsBound());
4891 uint32_t literal_offset = literal->GetLabel()->Position();
4892 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
4893 target_method.dex_file,
4894 target_method.dex_method_index));
4895 }
4896 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
4897 uint32_t literal_offset = info.label.Position();
4898 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4899 info.target_method.dex_file,
4900 info.target_method.dex_method_index));
4901 }
4902}
4903
4904Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
4905 MethodToLiteralMap* map) {
4906 // Look up the literal for target_method.
4907 auto lb = map->lower_bound(target_method);
4908 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
4909 return lb->second;
4910 }
4911 // We don't have a literal for this method yet, insert a new one.
4912 Literal* literal = __ NewLiteral<uint32_t>(0u);
4913 map->PutBefore(lb, target_method, literal);
4914 return literal;
4915}
4916
4917Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
4918 return DeduplicateMethodLiteral(target_method, &method_patches_);
4919}
4920
4921Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
4922 return DeduplicateMethodLiteral(target_method, &call_patches_);
4923}
4924
Calin Juravleb1498f62015-02-16 13:13:29 +00004925void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
4926 // Nothing to do, this should be removed during prepare for register allocator.
4927 UNUSED(instruction);
4928 LOG(FATAL) << "Unreachable";
4929}
4930
4931void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
4932 // Nothing to do, this should be removed during prepare for register allocator.
4933 UNUSED(instruction);
4934 LOG(FATAL) << "Unreachable";
4935}
4936
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004937void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
4938 DCHECK(codegen_->IsBaseline());
4939 LocationSummary* locations =
4940 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4941 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4942}
4943
4944void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4945 DCHECK(codegen_->IsBaseline());
4946 // Will be generated at use site.
4947}
4948
Andreas Gampe85b62f22015-09-09 13:15:38 -07004949void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
4950 if (!trg.IsValid()) {
4951 DCHECK(type == Primitive::kPrimVoid);
4952 return;
4953 }
4954
4955 DCHECK_NE(type, Primitive::kPrimVoid);
4956
4957 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
4958 if (return_loc.Equals(trg)) {
4959 return;
4960 }
4961
4962 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
4963 // with the last branch.
4964 if (type == Primitive::kPrimLong) {
4965 HParallelMove parallel_move(GetGraph()->GetArena());
4966 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
4967 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
4968 GetMoveResolver()->EmitNativeCode(&parallel_move);
4969 } else if (type == Primitive::kPrimDouble) {
4970 HParallelMove parallel_move(GetGraph()->GetArena());
4971 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
4972 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
4973 GetMoveResolver()->EmitNativeCode(&parallel_move);
4974 } else {
4975 // Let the parallel move resolver take care of all of this.
4976 HParallelMove parallel_move(GetGraph()->GetArena());
4977 parallel_move.AddMove(return_loc, trg, type, nullptr);
4978 GetMoveResolver()->EmitNativeCode(&parallel_move);
4979 }
4980}
4981
Roland Levillain4d027112015-07-01 15:41:14 +01004982#undef __
4983#undef QUICK_ENTRY_POINT
4984
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004985} // namespace arm
4986} // namespace art