blob: d172fba88836d8d1274cb07f499d9cced456420a [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 Geoffraye0395dd2015-09-25 11:04:45 +0100364class ArraySetSlowPathARM : public SlowPathCode {
365 public:
366 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
367
368 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
369 LocationSummary* locations = instruction_->GetLocations();
370 __ Bind(GetEntryLabel());
371 SaveLiveRegisters(codegen, locations);
372
373 InvokeRuntimeCallingConvention calling_convention;
374 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
375 parallel_move.AddMove(
376 locations->InAt(0),
377 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
378 Primitive::kPrimNot,
379 nullptr);
380 parallel_move.AddMove(
381 locations->InAt(1),
382 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
383 Primitive::kPrimInt,
384 nullptr);
385 parallel_move.AddMove(
386 locations->InAt(2),
387 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
388 Primitive::kPrimNot,
389 nullptr);
390 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
391
392 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
393 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
394 instruction_,
395 instruction_->GetDexPc(),
396 this);
397 RestoreLiveRegisters(codegen, locations);
398 __ b(GetExitLabel());
399 }
400
401 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
402
403 private:
404 HInstruction* const instruction_;
405
406 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
407};
408
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000409#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100410#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700411
Roland Levillain4fa13f62015-07-06 18:11:54 +0100412inline Condition ARMSignedOrFPCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700413 switch (cond) {
414 case kCondEQ: return EQ;
415 case kCondNE: return NE;
416 case kCondLT: return LT;
417 case kCondLE: return LE;
418 case kCondGT: return GT;
419 case kCondGE: return GE;
Dave Allison20dfc792014-06-16 20:44:29 -0700420 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100421 LOG(FATAL) << "Unreachable";
422 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700423}
424
Roland Levillain4fa13f62015-07-06 18:11:54 +0100425inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700426 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100427 case kCondEQ: return EQ;
428 case kCondNE: return NE;
429 case kCondLT: return LO;
430 case kCondLE: return LS;
431 case kCondGT: return HI;
432 case kCondGE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700433 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100434 LOG(FATAL) << "Unreachable";
435 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700436}
437
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100438void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100439 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100440}
441
442void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100443 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100444}
445
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100446size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
447 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
448 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100449}
450
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100451size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
452 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
453 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100454}
455
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000456size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
457 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
458 return kArmWordSize;
459}
460
461size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
462 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
463 return kArmWordSize;
464}
465
Calin Juravle34166012014-12-19 17:22:29 +0000466CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000467 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100468 const CompilerOptions& compiler_options,
469 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000470 : CodeGenerator(graph,
471 kNumberOfCoreRegisters,
472 kNumberOfSRegisters,
473 kNumberOfRegisterPairs,
474 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
475 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000476 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
477 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100478 compiler_options,
479 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100480 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100481 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100482 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100483 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000484 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000485 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100486 method_patches_(MethodReferenceComparator(),
487 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
488 call_patches_(MethodReferenceComparator(),
489 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
490 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700491 // Always save the LR register to mimic Quick.
492 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100493}
494
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000495void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
496 // Ensure that we fix up branches and literal loads and emit the literal pool.
497 __ FinalizeCode();
498
499 // Adjust native pc offsets in stack maps.
500 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
501 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
502 uint32_t new_position = __ GetAdjustedPosition(old_position);
503 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
504 }
505 // Adjust native pc offsets of block labels.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100506 for (HBasicBlock* block : *block_order_) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000507 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
508 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
Vladimir Marko225b6462015-09-28 12:17:40 +0100509 DCHECK_LT(block->GetBlockId(), GetGraph()->GetBlocks().size());
510 Label* block_label = &block_labels_[block->GetBlockId()];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000511 DCHECK_EQ(block_label->IsBound(), !block->IsSingleJump());
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000512 if (block_label->IsBound()) {
513 __ AdjustLabelPosition(block_label);
514 }
515 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100516 // Adjust pc offsets for the disassembly information.
517 if (disasm_info_ != nullptr) {
518 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
519 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
520 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
521 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
522 it.second.start = __ GetAdjustedPosition(it.second.start);
523 it.second.end = __ GetAdjustedPosition(it.second.end);
524 }
525 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
526 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
527 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
528 }
529 }
Vladimir Marko58155012015-08-19 12:49:41 +0000530 // Adjust pc offsets for relative call patches.
531 for (MethodPatchInfo<Label>& info : relative_call_patches_) {
532 __ AdjustLabelPosition(&info.label);
533 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000534
535 CodeGenerator::Finalize(allocator);
536}
537
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100538Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100539 switch (type) {
540 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100541 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100542 ArmManagedRegister pair =
543 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100544 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
545 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
546
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100547 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
548 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100549 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100550 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100551 }
552
553 case Primitive::kPrimByte:
554 case Primitive::kPrimBoolean:
555 case Primitive::kPrimChar:
556 case Primitive::kPrimShort:
557 case Primitive::kPrimInt:
558 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100559 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100560 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100561 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
562 ArmManagedRegister current =
563 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
564 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100565 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100566 }
567 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100568 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100569 }
570
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000571 case Primitive::kPrimFloat: {
572 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100573 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100574 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100575
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000576 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000577 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
578 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000579 return Location::FpuRegisterPairLocation(reg, reg + 1);
580 }
581
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100582 case Primitive::kPrimVoid:
583 LOG(FATAL) << "Unreachable type " << type;
584 }
585
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100586 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100587}
588
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000589void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100590 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100591 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100592
593 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100594 blocked_core_registers_[SP] = true;
595 blocked_core_registers_[LR] = true;
596 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100597
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100599 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100600
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100601 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100602 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100603
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000604 if (is_baseline) {
605 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
606 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
607 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000608
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000609 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100610 }
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000611
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100612 if (is_baseline || GetGraph()->IsDebuggable()) {
613 // Stubs do not save callee-save floating point registers. If the graph
614 // is debuggable, we need to deal with these registers differently. For
615 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000616 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
617 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
618 }
619 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100620
621 UpdateBlockedPairRegisters();
622}
623
624void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
625 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
626 ArmManagedRegister current =
627 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
628 if (blocked_core_registers_[current.AsRegisterPairLow()]
629 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
630 blocked_register_pairs_[i] = true;
631 }
632 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100633}
634
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100635InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
636 : HGraphVisitor(graph),
637 assembler_(codegen->GetAssembler()),
638 codegen_(codegen) {}
639
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000640void CodeGeneratorARM::ComputeSpillMask() {
641 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000642 // Save one extra register for baseline. Note that on thumb2, there is no easy
643 // instruction to restore just the PC, so this actually helps both baseline
644 // and non-baseline to save and restore at least two registers at entry and exit.
645 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000646 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
647 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
648 // We use vpush and vpop for saving and restoring floating point registers, which take
649 // a SRegister and the number of registers to save/restore after that SRegister. We
650 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
651 // but in the range.
652 if (fpu_spill_mask_ != 0) {
653 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
654 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
655 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
656 fpu_spill_mask_ |= (1 << i);
657 }
658 }
659}
660
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100661static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100662 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100663}
664
665static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100666 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100667}
668
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000669void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000670 bool skip_overflow_check =
671 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000672 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000673 __ Bind(&frame_entry_label_);
674
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000675 if (HasEmptyFrame()) {
676 return;
677 }
678
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100679 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000680 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
681 __ LoadFromOffset(kLoadWord, IP, IP, 0);
682 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100683 }
684
Andreas Gampe501fd632015-09-10 16:11:06 -0700685 __ PushList(core_spill_mask_);
686 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
687 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000688 if (fpu_spill_mask_ != 0) {
689 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
690 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100691 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100692 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000693 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100694 int adjust = GetFrameSize() - FrameEntrySpillSize();
695 __ AddConstant(SP, -adjust);
696 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100697 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000698}
699
700void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000701 if (HasEmptyFrame()) {
702 __ bx(LR);
703 return;
704 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100705 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100706 int adjust = GetFrameSize() - FrameEntrySpillSize();
707 __ AddConstant(SP, adjust);
708 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000709 if (fpu_spill_mask_ != 0) {
710 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
711 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100712 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
713 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000714 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700715 // Pop LR into PC to return.
716 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
717 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
718 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100719 __ cfi().RestoreState();
720 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000721}
722
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100723void CodeGeneratorARM::Bind(HBasicBlock* block) {
724 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000725}
726
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100727Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
728 switch (load->GetType()) {
729 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100731 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100732
733 case Primitive::kPrimInt:
734 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100735 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100736 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100737
738 case Primitive::kPrimBoolean:
739 case Primitive::kPrimByte:
740 case Primitive::kPrimChar:
741 case Primitive::kPrimShort:
742 case Primitive::kPrimVoid:
743 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700744 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100745 }
746
747 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700748 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100749}
750
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100751Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100752 switch (type) {
753 case Primitive::kPrimBoolean:
754 case Primitive::kPrimByte:
755 case Primitive::kPrimChar:
756 case Primitive::kPrimShort:
757 case Primitive::kPrimInt:
758 case Primitive::kPrimNot: {
759 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000760 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100761 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100762 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100763 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000764 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100765 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100766 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100767
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000768 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100769 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000770 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100771 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000772 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100773 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000774 if (calling_convention.GetRegisterAt(index) == R1) {
775 // Skip R1, and use R2_R3 instead.
776 gp_index_++;
777 index++;
778 }
779 }
780 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
781 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000782 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100783
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000784 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000785 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100786 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000787 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
788 }
789 }
790
791 case Primitive::kPrimFloat: {
792 uint32_t stack_index = stack_index_++;
793 if (float_index_ % 2 == 0) {
794 float_index_ = std::max(double_index_, float_index_);
795 }
796 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
797 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
798 } else {
799 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
800 }
801 }
802
803 case Primitive::kPrimDouble: {
804 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
805 uint32_t stack_index = stack_index_;
806 stack_index_ += 2;
807 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
808 uint32_t index = double_index_;
809 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000810 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000811 calling_convention.GetFpuRegisterAt(index),
812 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000813 DCHECK(ExpectedPairLayout(result));
814 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000815 } else {
816 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100817 }
818 }
819
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100820 case Primitive::kPrimVoid:
821 LOG(FATAL) << "Unexpected parameter type " << type;
822 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100823 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100824 return Location();
825}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100826
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100827Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000828 switch (type) {
829 case Primitive::kPrimBoolean:
830 case Primitive::kPrimByte:
831 case Primitive::kPrimChar:
832 case Primitive::kPrimShort:
833 case Primitive::kPrimInt:
834 case Primitive::kPrimNot: {
835 return Location::RegisterLocation(R0);
836 }
837
838 case Primitive::kPrimFloat: {
839 return Location::FpuRegisterLocation(S0);
840 }
841
842 case Primitive::kPrimLong: {
843 return Location::RegisterPairLocation(R0, R1);
844 }
845
846 case Primitive::kPrimDouble: {
847 return Location::FpuRegisterPairLocation(S0, S1);
848 }
849
850 case Primitive::kPrimVoid:
851 return Location();
852 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100853
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000854 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000855}
856
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100857Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
858 return Location::RegisterLocation(kMethodRegisterArgument);
859}
860
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100861void CodeGeneratorARM::Move32(Location destination, Location source) {
862 if (source.Equals(destination)) {
863 return;
864 }
865 if (destination.IsRegister()) {
866 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000867 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100868 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000869 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000871 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100873 } else if (destination.IsFpuRegister()) {
874 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000875 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100876 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000877 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100878 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000879 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100880 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100881 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000882 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000884 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100885 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000886 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100887 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000888 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100889 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
890 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100891 }
892 }
893}
894
895void CodeGeneratorARM::Move64(Location destination, Location source) {
896 if (source.Equals(destination)) {
897 return;
898 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100899 if (destination.IsRegisterPair()) {
900 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000901 EmitParallelMoves(
902 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
903 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100904 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000905 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100906 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
907 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100908 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000909 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100910 } else if (source.IsFpuRegisterPair()) {
911 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
912 destination.AsRegisterPairHigh<Register>(),
913 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914 } else {
915 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000916 DCHECK(ExpectedPairLayout(destination));
917 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
918 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100919 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000920 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000922 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
923 SP,
924 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100925 } else if (source.IsRegisterPair()) {
926 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
927 source.AsRegisterPairLow<Register>(),
928 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100929 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000930 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100931 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100932 } else {
933 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100934 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000935 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100936 if (source.AsRegisterPairLow<Register>() == R1) {
937 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100938 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
939 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100940 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100941 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100942 SP, destination.GetStackIndex());
943 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000944 } else if (source.IsFpuRegisterPair()) {
945 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
946 SP,
947 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100948 } else {
949 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000950 EmitParallelMoves(
951 Location::StackSlot(source.GetStackIndex()),
952 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100953 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000954 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100955 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
956 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957 }
958 }
959}
960
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100961void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100962 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100963 if (instruction->IsCurrentMethod()) {
964 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
965 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100966 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100967 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000968 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000969 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
970 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000971 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000972 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000973 } else {
974 DCHECK(location.IsStackSlot());
975 __ LoadImmediate(IP, value);
976 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
977 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000978 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000979 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000980 int64_t value = const_to_move->AsLongConstant()->GetValue();
981 if (location.IsRegisterPair()) {
982 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
983 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
984 } else {
985 DCHECK(location.IsDoubleStackSlot());
986 __ LoadImmediate(IP, Low32Bits(value));
987 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
988 __ LoadImmediate(IP, High32Bits(value));
989 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
990 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100991 }
Roland Levillain476df552014-10-09 17:51:36 +0100992 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100993 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
994 switch (instruction->GetType()) {
995 case Primitive::kPrimBoolean:
996 case Primitive::kPrimByte:
997 case Primitive::kPrimChar:
998 case Primitive::kPrimShort:
999 case Primitive::kPrimInt:
1000 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 Move32(location, Location::StackSlot(stack_slot));
1003 break;
1004
1005 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001007 Move64(location, Location::DoubleStackSlot(stack_slot));
1008 break;
1009
1010 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001011 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001013 } else if (instruction->IsTemporary()) {
1014 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001015 if (temp_location.IsStackSlot()) {
1016 Move32(location, temp_location);
1017 } else {
1018 DCHECK(temp_location.IsDoubleStackSlot());
1019 Move64(location, temp_location);
1020 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001021 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001022 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023 switch (instruction->GetType()) {
1024 case Primitive::kPrimBoolean:
1025 case Primitive::kPrimByte:
1026 case Primitive::kPrimChar:
1027 case Primitive::kPrimShort:
1028 case Primitive::kPrimNot:
1029 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001031 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 break;
1033
1034 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001035 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001036 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 break;
1038
1039 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001040 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001041 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001042 }
1043}
1044
Calin Juravle175dc732015-08-25 15:42:32 +01001045void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1046 DCHECK(location.IsRegister());
1047 __ LoadImmediate(location.AsRegister<Register>(), value);
1048}
1049
Calin Juravlee460d1d2015-09-29 04:52:17 +01001050void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1051 if (Primitive::Is64BitType(dst_type)) {
1052 Move64(dst, src);
1053 } else {
1054 Move32(dst, src);
1055 }
1056}
1057
1058void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1059 if (location.IsRegister()) {
1060 locations->AddTemp(location);
1061 } else if (location.IsRegisterPair()) {
1062 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1063 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1064 } else {
1065 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1066 }
1067}
1068
Calin Juravle175dc732015-08-25 15:42:32 +01001069void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1070 HInstruction* instruction,
1071 uint32_t dex_pc,
1072 SlowPathCode* slow_path) {
1073 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1074 instruction,
1075 dex_pc,
1076 slow_path);
1077}
1078
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001079void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1080 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001081 uint32_t dex_pc,
1082 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001083 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001084 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1085 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001086 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001087}
1088
David Brazdilfc6a86a2015-06-26 10:33:45 +00001089void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001090 DCHECK(!successor->IsExitBlock());
1091
1092 HBasicBlock* block = got->GetBlock();
1093 HInstruction* previous = got->GetPrevious();
1094
1095 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001096 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001097 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1098 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1099 return;
1100 }
1101
1102 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1103 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1104 }
1105 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001106 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001107 }
1108}
1109
David Brazdilfc6a86a2015-06-26 10:33:45 +00001110void LocationsBuilderARM::VisitGoto(HGoto* got) {
1111 got->SetLocations(nullptr);
1112}
1113
1114void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1115 HandleGoto(got, got->GetSuccessor());
1116}
1117
1118void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1119 try_boundary->SetLocations(nullptr);
1120}
1121
1122void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1123 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1124 if (!successor->IsExitBlock()) {
1125 HandleGoto(try_boundary, successor);
1126 }
1127}
1128
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001129void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001130 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001131}
1132
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001134 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001135}
1136
Roland Levillain4fa13f62015-07-06 18:11:54 +01001137void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1138 ShifterOperand operand;
1139 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1140 __ cmp(left, operand);
1141 } else {
1142 Register temp = IP;
1143 __ LoadImmediate(temp, right);
1144 __ cmp(left, ShifterOperand(temp));
1145 }
1146}
1147
1148void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1149 Label* true_label,
1150 Label* false_label) {
1151 __ vmstat(); // transfer FP status register to ARM APSR.
1152 if (cond->IsFPConditionTrueIfNaN()) {
1153 __ b(true_label, VS); // VS for unordered.
1154 } else if (cond->IsFPConditionFalseIfNaN()) {
1155 __ b(false_label, VS); // VS for unordered.
1156 }
1157 __ b(true_label, ARMSignedOrFPCondition(cond->GetCondition()));
1158}
1159
1160void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1161 Label* true_label,
1162 Label* false_label) {
1163 LocationSummary* locations = cond->GetLocations();
1164 Location left = locations->InAt(0);
1165 Location right = locations->InAt(1);
1166 IfCondition if_cond = cond->GetCondition();
1167
1168 Register left_high = left.AsRegisterPairHigh<Register>();
1169 Register left_low = left.AsRegisterPairLow<Register>();
1170 IfCondition true_high_cond = if_cond;
1171 IfCondition false_high_cond = cond->GetOppositeCondition();
1172 Condition final_condition = ARMUnsignedCondition(if_cond);
1173
1174 // Set the conditions for the test, remembering that == needs to be
1175 // decided using the low words.
1176 switch (if_cond) {
1177 case kCondEQ:
1178 case kCondNE:
1179 // Nothing to do.
1180 break;
1181 case kCondLT:
1182 false_high_cond = kCondGT;
1183 break;
1184 case kCondLE:
1185 true_high_cond = kCondLT;
1186 break;
1187 case kCondGT:
1188 false_high_cond = kCondLT;
1189 break;
1190 case kCondGE:
1191 true_high_cond = kCondGT;
1192 break;
1193 }
1194 if (right.IsConstant()) {
1195 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1196 int32_t val_low = Low32Bits(value);
1197 int32_t val_high = High32Bits(value);
1198
1199 GenerateCompareWithImmediate(left_high, val_high);
1200 if (if_cond == kCondNE) {
1201 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1202 } else if (if_cond == kCondEQ) {
1203 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1204 } else {
1205 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1206 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1207 }
1208 // Must be equal high, so compare the lows.
1209 GenerateCompareWithImmediate(left_low, val_low);
1210 } else {
1211 Register right_high = right.AsRegisterPairHigh<Register>();
1212 Register right_low = right.AsRegisterPairLow<Register>();
1213
1214 __ cmp(left_high, ShifterOperand(right_high));
1215 if (if_cond == kCondNE) {
1216 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1217 } else if (if_cond == kCondEQ) {
1218 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1219 } else {
1220 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1221 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1222 }
1223 // Must be equal high, so compare the lows.
1224 __ cmp(left_low, ShifterOperand(right_low));
1225 }
1226 // The last comparison might be unsigned.
1227 __ b(true_label, final_condition);
1228}
1229
1230void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1231 HCondition* condition,
1232 Label* true_target,
1233 Label* false_target,
1234 Label* always_true_target) {
1235 LocationSummary* locations = condition->GetLocations();
1236 Location left = locations->InAt(0);
1237 Location right = locations->InAt(1);
1238
1239 // We don't want true_target as a nullptr.
1240 if (true_target == nullptr) {
1241 true_target = always_true_target;
1242 }
1243 bool falls_through = (false_target == nullptr);
1244
1245 // FP compares don't like null false_targets.
1246 if (false_target == nullptr) {
1247 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1248 }
1249
1250 Primitive::Type type = condition->InputAt(0)->GetType();
1251 switch (type) {
1252 case Primitive::kPrimLong:
1253 GenerateLongComparesAndJumps(condition, true_target, false_target);
1254 break;
1255 case Primitive::kPrimFloat:
1256 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1257 GenerateFPJumps(condition, true_target, false_target);
1258 break;
1259 case Primitive::kPrimDouble:
1260 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1261 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1262 GenerateFPJumps(condition, true_target, false_target);
1263 break;
1264 default:
1265 LOG(FATAL) << "Unexpected compare type " << type;
1266 }
1267
1268 if (!falls_through) {
1269 __ b(false_target);
1270 }
1271}
1272
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001273void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1274 Label* true_target,
1275 Label* false_target,
1276 Label* always_true_target) {
1277 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001278 if (cond->IsIntConstant()) {
1279 // Constant condition, statically compared against 1.
1280 int32_t cond_value = cond->AsIntConstant()->GetValue();
1281 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001282 if (always_true_target != nullptr) {
1283 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001284 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001285 return;
1286 } else {
1287 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001288 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001289 } else {
1290 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1291 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001292 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001293 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1294 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001295 } else {
1296 // Condition has not been materialized, use its inputs as the
1297 // comparison and its condition as the branch condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001298 Primitive::Type type =
1299 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
1300 // Is this a long or FP comparison that has been folded into the HCondition?
1301 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1302 // Generate the comparison directly.
1303 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1304 true_target, false_target, always_true_target);
1305 return;
1306 }
1307
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001308 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001309 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001310 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001311 Location right = locations->InAt(1);
1312 if (right.IsRegister()) {
1313 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001314 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001315 DCHECK(right.IsConstant());
1316 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001317 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001318 __ b(true_target, ARMSignedOrFPCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001319 }
Dave Allison20dfc792014-06-16 20:44:29 -07001320 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001321 if (false_target != nullptr) {
1322 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001323 }
1324}
1325
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001326void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1327 LocationSummary* locations =
1328 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1329 HInstruction* cond = if_instr->InputAt(0);
1330 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1331 locations->SetInAt(0, Location::RequiresRegister());
1332 }
1333}
1334
1335void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1336 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1337 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1338 Label* always_true_target = true_target;
1339 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1340 if_instr->IfTrueSuccessor())) {
1341 always_true_target = nullptr;
1342 }
1343 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1344 if_instr->IfFalseSuccessor())) {
1345 false_target = nullptr;
1346 }
1347 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1348}
1349
1350void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1351 LocationSummary* locations = new (GetGraph()->GetArena())
1352 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1353 HInstruction* cond = deoptimize->InputAt(0);
1354 DCHECK(cond->IsCondition());
1355 if (cond->AsCondition()->NeedsMaterialization()) {
1356 locations->SetInAt(0, Location::RequiresRegister());
1357 }
1358}
1359
1360void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001361 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001362 DeoptimizationSlowPathARM(deoptimize);
1363 codegen_->AddSlowPath(slow_path);
1364 Label* slow_path_entry = slow_path->GetEntryLabel();
1365 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1366}
Dave Allison20dfc792014-06-16 20:44:29 -07001367
Roland Levillain0d37cd02015-05-27 16:39:19 +01001368void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001369 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001370 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001371 // Handle the long/FP comparisons made in instruction simplification.
1372 switch (cond->InputAt(0)->GetType()) {
1373 case Primitive::kPrimLong:
1374 locations->SetInAt(0, Location::RequiresRegister());
1375 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1376 if (cond->NeedsMaterialization()) {
1377 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1378 }
1379 break;
1380
1381 case Primitive::kPrimFloat:
1382 case Primitive::kPrimDouble:
1383 locations->SetInAt(0, Location::RequiresFpuRegister());
1384 locations->SetInAt(1, Location::RequiresFpuRegister());
1385 if (cond->NeedsMaterialization()) {
1386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1387 }
1388 break;
1389
1390 default:
1391 locations->SetInAt(0, Location::RequiresRegister());
1392 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1393 if (cond->NeedsMaterialization()) {
1394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1395 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001396 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001397}
1398
Roland Levillain0d37cd02015-05-27 16:39:19 +01001399void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001400 if (!cond->NeedsMaterialization()) {
1401 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001402 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001403
1404 LocationSummary* locations = cond->GetLocations();
1405 Location left = locations->InAt(0);
1406 Location right = locations->InAt(1);
1407 Register out = locations->Out().AsRegister<Register>();
1408 Label true_label, false_label;
1409
1410 switch (cond->InputAt(0)->GetType()) {
1411 default: {
1412 // Integer case.
1413 if (right.IsRegister()) {
1414 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1415 } else {
1416 DCHECK(right.IsConstant());
1417 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1418 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1419 }
1420 __ it(ARMSignedOrFPCondition(cond->GetCondition()), kItElse);
1421 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
1422 ARMSignedOrFPCondition(cond->GetCondition()));
1423 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
1424 ARMSignedOrFPCondition(cond->GetOppositeCondition()));
1425 return;
1426 }
1427 case Primitive::kPrimLong:
1428 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1429 break;
1430 case Primitive::kPrimFloat:
1431 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1432 GenerateFPJumps(cond, &true_label, &false_label);
1433 break;
1434 case Primitive::kPrimDouble:
1435 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1436 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1437 GenerateFPJumps(cond, &true_label, &false_label);
1438 break;
1439 }
1440
1441 // Convert the jumps into the result.
1442 Label done_label;
1443
1444 // False case: result = 0.
1445 __ Bind(&false_label);
1446 __ LoadImmediate(out, 0);
1447 __ b(&done_label);
1448
1449 // True case: result = 1.
1450 __ Bind(&true_label);
1451 __ LoadImmediate(out, 1);
1452 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001453}
1454
1455void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1456 VisitCondition(comp);
1457}
1458
1459void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1460 VisitCondition(comp);
1461}
1462
1463void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1464 VisitCondition(comp);
1465}
1466
1467void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1468 VisitCondition(comp);
1469}
1470
1471void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1472 VisitCondition(comp);
1473}
1474
1475void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1476 VisitCondition(comp);
1477}
1478
1479void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1480 VisitCondition(comp);
1481}
1482
1483void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1484 VisitCondition(comp);
1485}
1486
1487void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1488 VisitCondition(comp);
1489}
1490
1491void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1492 VisitCondition(comp);
1493}
1494
1495void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1496 VisitCondition(comp);
1497}
1498
1499void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1500 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001501}
1502
1503void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001504 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001505}
1506
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001507void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1508 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001509}
1510
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001511void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001512 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001513}
1514
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001515void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001516 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001517 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001518}
1519
1520void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001521 LocationSummary* locations =
1522 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001523 switch (store->InputAt(1)->GetType()) {
1524 case Primitive::kPrimBoolean:
1525 case Primitive::kPrimByte:
1526 case Primitive::kPrimChar:
1527 case Primitive::kPrimShort:
1528 case Primitive::kPrimInt:
1529 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001530 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001531 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1532 break;
1533
1534 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001535 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001536 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1537 break;
1538
1539 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001540 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001541 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001542}
1543
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001544void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001545 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001546}
1547
1548void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001549 LocationSummary* locations =
1550 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001551 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001552}
1553
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001554void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001555 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001556 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001557}
1558
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001559void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1560 LocationSummary* locations =
1561 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1562 locations->SetOut(Location::ConstantLocation(constant));
1563}
1564
1565void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1566 // Will be generated at use site.
1567 UNUSED(constant);
1568}
1569
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001570void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001571 LocationSummary* locations =
1572 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001573 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001574}
1575
1576void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1577 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001578 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001579}
1580
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001581void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1582 LocationSummary* locations =
1583 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1584 locations->SetOut(Location::ConstantLocation(constant));
1585}
1586
1587void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1588 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001589 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001590}
1591
1592void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1593 LocationSummary* locations =
1594 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1595 locations->SetOut(Location::ConstantLocation(constant));
1596}
1597
1598void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1599 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001600 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001601}
1602
Calin Juravle27df7582015-04-17 19:12:31 +01001603void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1604 memory_barrier->SetLocations(nullptr);
1605}
1606
1607void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1608 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1609}
1610
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001611void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001612 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001613}
1614
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001615void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001616 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001617 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001618}
1619
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001620void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001621 LocationSummary* locations =
1622 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001623 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001624}
1625
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001626void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001627 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001628 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001629}
1630
Calin Juravle175dc732015-08-25 15:42:32 +01001631void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1632 // The trampoline uses the same calling convention as dex calling conventions,
1633 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1634 // the method_idx.
1635 HandleInvoke(invoke);
1636}
1637
1638void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1639 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1640}
1641
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001642void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001643 // When we do not run baseline, explicit clinit checks triggered by static
1644 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1645 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001646
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001647 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1648 codegen_->GetInstructionSetFeatures());
1649 if (intrinsic.TryDispatch(invoke)) {
1650 return;
1651 }
1652
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001653 HandleInvoke(invoke);
1654}
1655
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001656static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1657 if (invoke->GetLocations()->Intrinsified()) {
1658 IntrinsicCodeGeneratorARM intrinsic(codegen);
1659 intrinsic.Dispatch(invoke);
1660 return true;
1661 }
1662 return false;
1663}
1664
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001665void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001666 // When we do not run baseline, explicit clinit checks triggered by static
1667 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1668 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001669
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001670 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1671 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001672 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001673
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001674 LocationSummary* locations = invoke->GetLocations();
1675 codegen_->GenerateStaticOrDirectCall(
1676 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001677 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001678}
1679
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001680void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001681 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001682 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001683}
1684
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001685void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001686 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1687 codegen_->GetInstructionSetFeatures());
1688 if (intrinsic.TryDispatch(invoke)) {
1689 return;
1690 }
1691
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001692 HandleInvoke(invoke);
1693}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001694
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001695void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001696 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1697 return;
1698 }
1699
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001700 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001701 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001702 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001703}
1704
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001705void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1706 HandleInvoke(invoke);
1707 // Add the hidden argument.
1708 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1709}
1710
1711void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1712 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001713 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001714 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1715 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001716 LocationSummary* locations = invoke->GetLocations();
1717 Location receiver = locations->InAt(0);
1718 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1719
1720 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001721 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1722 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001723
1724 // temp = object->GetClass();
1725 if (receiver.IsStackSlot()) {
1726 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1727 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1728 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001729 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001730 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001731 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001732 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001733 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001734 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001735 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001736 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1737 // LR = temp->GetEntryPoint();
1738 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1739 // LR();
1740 __ blx(LR);
1741 DCHECK(!codegen_->IsLeafMethod());
1742 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1743}
1744
Roland Levillain88cb1752014-10-20 16:36:47 +01001745void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1746 LocationSummary* locations =
1747 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1748 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001749 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001750 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1752 break;
1753 }
1754 case Primitive::kPrimLong: {
1755 locations->SetInAt(0, Location::RequiresRegister());
1756 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001757 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001758 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001759
Roland Levillain88cb1752014-10-20 16:36:47 +01001760 case Primitive::kPrimFloat:
1761 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001762 locations->SetInAt(0, Location::RequiresFpuRegister());
1763 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001764 break;
1765
1766 default:
1767 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1768 }
1769}
1770
1771void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1772 LocationSummary* locations = neg->GetLocations();
1773 Location out = locations->Out();
1774 Location in = locations->InAt(0);
1775 switch (neg->GetResultType()) {
1776 case Primitive::kPrimInt:
1777 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001778 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001779 break;
1780
1781 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001782 DCHECK(in.IsRegisterPair());
1783 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1784 __ rsbs(out.AsRegisterPairLow<Register>(),
1785 in.AsRegisterPairLow<Register>(),
1786 ShifterOperand(0));
1787 // We cannot emit an RSC (Reverse Subtract with Carry)
1788 // instruction here, as it does not exist in the Thumb-2
1789 // instruction set. We use the following approach
1790 // using SBC and SUB instead.
1791 //
1792 // out.hi = -C
1793 __ sbc(out.AsRegisterPairHigh<Register>(),
1794 out.AsRegisterPairHigh<Register>(),
1795 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1796 // out.hi = out.hi - in.hi
1797 __ sub(out.AsRegisterPairHigh<Register>(),
1798 out.AsRegisterPairHigh<Register>(),
1799 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1800 break;
1801
Roland Levillain88cb1752014-10-20 16:36:47 +01001802 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001803 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001804 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001805 break;
1806
Roland Levillain88cb1752014-10-20 16:36:47 +01001807 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001808 DCHECK(in.IsFpuRegisterPair());
1809 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1810 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001811 break;
1812
1813 default:
1814 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1815 }
1816}
1817
Roland Levillaindff1f282014-11-05 14:15:05 +00001818void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001819 Primitive::Type result_type = conversion->GetResultType();
1820 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001821 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001822
Roland Levillain5b3ee562015-04-14 16:02:41 +01001823 // The float-to-long, double-to-long and long-to-float type conversions
1824 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001825 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001826 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1827 && result_type == Primitive::kPrimLong)
1828 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001829 ? LocationSummary::kCall
1830 : LocationSummary::kNoCall;
1831 LocationSummary* locations =
1832 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1833
David Brazdilb2bd1c52015-03-25 11:17:37 +00001834 // The Java language does not allow treating boolean as an integral type but
1835 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001836
Roland Levillaindff1f282014-11-05 14:15:05 +00001837 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001838 case Primitive::kPrimByte:
1839 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001840 case Primitive::kPrimBoolean:
1841 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001842 case Primitive::kPrimShort:
1843 case Primitive::kPrimInt:
1844 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001845 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001846 locations->SetInAt(0, Location::RequiresRegister());
1847 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1848 break;
1849
1850 default:
1851 LOG(FATAL) << "Unexpected type conversion from " << input_type
1852 << " to " << result_type;
1853 }
1854 break;
1855
Roland Levillain01a8d712014-11-14 16:27:39 +00001856 case Primitive::kPrimShort:
1857 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001858 case Primitive::kPrimBoolean:
1859 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001860 case Primitive::kPrimByte:
1861 case Primitive::kPrimInt:
1862 case Primitive::kPrimChar:
1863 // Processing a Dex `int-to-short' instruction.
1864 locations->SetInAt(0, Location::RequiresRegister());
1865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1866 break;
1867
1868 default:
1869 LOG(FATAL) << "Unexpected type conversion from " << input_type
1870 << " to " << result_type;
1871 }
1872 break;
1873
Roland Levillain946e1432014-11-11 17:35:19 +00001874 case Primitive::kPrimInt:
1875 switch (input_type) {
1876 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001877 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001878 locations->SetInAt(0, Location::Any());
1879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1880 break;
1881
1882 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001883 // Processing a Dex `float-to-int' instruction.
1884 locations->SetInAt(0, Location::RequiresFpuRegister());
1885 locations->SetOut(Location::RequiresRegister());
1886 locations->AddTemp(Location::RequiresFpuRegister());
1887 break;
1888
Roland Levillain946e1432014-11-11 17:35:19 +00001889 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001890 // Processing a Dex `double-to-int' instruction.
1891 locations->SetInAt(0, Location::RequiresFpuRegister());
1892 locations->SetOut(Location::RequiresRegister());
1893 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001894 break;
1895
1896 default:
1897 LOG(FATAL) << "Unexpected type conversion from " << input_type
1898 << " to " << result_type;
1899 }
1900 break;
1901
Roland Levillaindff1f282014-11-05 14:15:05 +00001902 case Primitive::kPrimLong:
1903 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001904 case Primitive::kPrimBoolean:
1905 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001906 case Primitive::kPrimByte:
1907 case Primitive::kPrimShort:
1908 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001909 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001910 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001911 locations->SetInAt(0, Location::RequiresRegister());
1912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1913 break;
1914
Roland Levillain624279f2014-12-04 11:54:28 +00001915 case Primitive::kPrimFloat: {
1916 // Processing a Dex `float-to-long' instruction.
1917 InvokeRuntimeCallingConvention calling_convention;
1918 locations->SetInAt(0, Location::FpuRegisterLocation(
1919 calling_convention.GetFpuRegisterAt(0)));
1920 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1921 break;
1922 }
1923
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001924 case Primitive::kPrimDouble: {
1925 // Processing a Dex `double-to-long' instruction.
1926 InvokeRuntimeCallingConvention calling_convention;
1927 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1928 calling_convention.GetFpuRegisterAt(0),
1929 calling_convention.GetFpuRegisterAt(1)));
1930 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001931 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001932 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001933
1934 default:
1935 LOG(FATAL) << "Unexpected type conversion from " << input_type
1936 << " to " << result_type;
1937 }
1938 break;
1939
Roland Levillain981e4542014-11-14 11:47:14 +00001940 case Primitive::kPrimChar:
1941 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001942 case Primitive::kPrimBoolean:
1943 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001944 case Primitive::kPrimByte:
1945 case Primitive::kPrimShort:
1946 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001947 // Processing a Dex `int-to-char' instruction.
1948 locations->SetInAt(0, Location::RequiresRegister());
1949 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1950 break;
1951
1952 default:
1953 LOG(FATAL) << "Unexpected type conversion from " << input_type
1954 << " to " << result_type;
1955 }
1956 break;
1957
Roland Levillaindff1f282014-11-05 14:15:05 +00001958 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001959 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001960 case Primitive::kPrimBoolean:
1961 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001962 case Primitive::kPrimByte:
1963 case Primitive::kPrimShort:
1964 case Primitive::kPrimInt:
1965 case Primitive::kPrimChar:
1966 // Processing a Dex `int-to-float' instruction.
1967 locations->SetInAt(0, Location::RequiresRegister());
1968 locations->SetOut(Location::RequiresFpuRegister());
1969 break;
1970
Roland Levillain5b3ee562015-04-14 16:02:41 +01001971 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00001972 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001973 InvokeRuntimeCallingConvention calling_convention;
1974 locations->SetInAt(0, Location::RegisterPairLocation(
1975 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1976 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001977 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01001978 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001979
Roland Levillaincff13742014-11-17 14:32:17 +00001980 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001981 // Processing a Dex `double-to-float' instruction.
1982 locations->SetInAt(0, Location::RequiresFpuRegister());
1983 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001984 break;
1985
1986 default:
1987 LOG(FATAL) << "Unexpected type conversion from " << input_type
1988 << " to " << result_type;
1989 };
1990 break;
1991
Roland Levillaindff1f282014-11-05 14:15:05 +00001992 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001993 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001994 case Primitive::kPrimBoolean:
1995 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001996 case Primitive::kPrimByte:
1997 case Primitive::kPrimShort:
1998 case Primitive::kPrimInt:
1999 case Primitive::kPrimChar:
2000 // Processing a Dex `int-to-double' instruction.
2001 locations->SetInAt(0, Location::RequiresRegister());
2002 locations->SetOut(Location::RequiresFpuRegister());
2003 break;
2004
2005 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002006 // Processing a Dex `long-to-double' instruction.
2007 locations->SetInAt(0, Location::RequiresRegister());
2008 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002009 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002010 locations->AddTemp(Location::RequiresFpuRegister());
2011 break;
2012
Roland Levillaincff13742014-11-17 14:32:17 +00002013 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002014 // Processing a Dex `float-to-double' instruction.
2015 locations->SetInAt(0, Location::RequiresFpuRegister());
2016 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002017 break;
2018
2019 default:
2020 LOG(FATAL) << "Unexpected type conversion from " << input_type
2021 << " to " << result_type;
2022 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002023 break;
2024
2025 default:
2026 LOG(FATAL) << "Unexpected type conversion from " << input_type
2027 << " to " << result_type;
2028 }
2029}
2030
2031void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2032 LocationSummary* locations = conversion->GetLocations();
2033 Location out = locations->Out();
2034 Location in = locations->InAt(0);
2035 Primitive::Type result_type = conversion->GetResultType();
2036 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002037 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002038 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002039 case Primitive::kPrimByte:
2040 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002041 case Primitive::kPrimBoolean:
2042 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002043 case Primitive::kPrimShort:
2044 case Primitive::kPrimInt:
2045 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002046 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002047 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002048 break;
2049
2050 default:
2051 LOG(FATAL) << "Unexpected type conversion from " << input_type
2052 << " to " << result_type;
2053 }
2054 break;
2055
Roland Levillain01a8d712014-11-14 16:27:39 +00002056 case Primitive::kPrimShort:
2057 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002058 case Primitive::kPrimBoolean:
2059 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002060 case Primitive::kPrimByte:
2061 case Primitive::kPrimInt:
2062 case Primitive::kPrimChar:
2063 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002064 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002065 break;
2066
2067 default:
2068 LOG(FATAL) << "Unexpected type conversion from " << input_type
2069 << " to " << result_type;
2070 }
2071 break;
2072
Roland Levillain946e1432014-11-11 17:35:19 +00002073 case Primitive::kPrimInt:
2074 switch (input_type) {
2075 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002076 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002077 DCHECK(out.IsRegister());
2078 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002079 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002080 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002082 } else {
2083 DCHECK(in.IsConstant());
2084 DCHECK(in.GetConstant()->IsLongConstant());
2085 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002086 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002087 }
2088 break;
2089
Roland Levillain3f8f9362014-12-02 17:45:01 +00002090 case Primitive::kPrimFloat: {
2091 // Processing a Dex `float-to-int' instruction.
2092 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2093 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2094 __ vcvtis(temp, temp);
2095 __ vmovrs(out.AsRegister<Register>(), temp);
2096 break;
2097 }
2098
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002099 case Primitive::kPrimDouble: {
2100 // Processing a Dex `double-to-int' instruction.
2101 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2102 DRegister temp_d = FromLowSToD(temp_s);
2103 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2104 __ vcvtid(temp_s, temp_d);
2105 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002106 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002107 }
Roland Levillain946e1432014-11-11 17:35:19 +00002108
2109 default:
2110 LOG(FATAL) << "Unexpected type conversion from " << input_type
2111 << " to " << result_type;
2112 }
2113 break;
2114
Roland Levillaindff1f282014-11-05 14:15:05 +00002115 case Primitive::kPrimLong:
2116 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002117 case Primitive::kPrimBoolean:
2118 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002119 case Primitive::kPrimByte:
2120 case Primitive::kPrimShort:
2121 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002122 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002123 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002124 DCHECK(out.IsRegisterPair());
2125 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002126 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002127 // Sign extension.
2128 __ Asr(out.AsRegisterPairHigh<Register>(),
2129 out.AsRegisterPairLow<Register>(),
2130 31);
2131 break;
2132
2133 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002134 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002135 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2136 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002137 conversion->GetDexPc(),
2138 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002139 break;
2140
Roland Levillaindff1f282014-11-05 14:15:05 +00002141 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002142 // Processing a Dex `double-to-long' instruction.
2143 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2144 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002145 conversion->GetDexPc(),
2146 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002147 break;
2148
2149 default:
2150 LOG(FATAL) << "Unexpected type conversion from " << input_type
2151 << " to " << result_type;
2152 }
2153 break;
2154
Roland Levillain981e4542014-11-14 11:47:14 +00002155 case Primitive::kPrimChar:
2156 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002157 case Primitive::kPrimBoolean:
2158 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002159 case Primitive::kPrimByte:
2160 case Primitive::kPrimShort:
2161 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002162 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002164 break;
2165
2166 default:
2167 LOG(FATAL) << "Unexpected type conversion from " << input_type
2168 << " to " << result_type;
2169 }
2170 break;
2171
Roland Levillaindff1f282014-11-05 14:15:05 +00002172 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002173 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002174 case Primitive::kPrimBoolean:
2175 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002176 case Primitive::kPrimByte:
2177 case Primitive::kPrimShort:
2178 case Primitive::kPrimInt:
2179 case Primitive::kPrimChar: {
2180 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002181 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2182 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002183 break;
2184 }
2185
Roland Levillain5b3ee562015-04-14 16:02:41 +01002186 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002187 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002188 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2189 conversion,
2190 conversion->GetDexPc(),
2191 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002192 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002193
Roland Levillaincff13742014-11-17 14:32:17 +00002194 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002195 // Processing a Dex `double-to-float' instruction.
2196 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2197 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 };
2204 break;
2205
Roland Levillaindff1f282014-11-05 14:15:05 +00002206 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002207 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002208 case Primitive::kPrimBoolean:
2209 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002210 case Primitive::kPrimByte:
2211 case Primitive::kPrimShort:
2212 case Primitive::kPrimInt:
2213 case Primitive::kPrimChar: {
2214 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002215 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002216 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2217 out.AsFpuRegisterPairLow<SRegister>());
2218 break;
2219 }
2220
Roland Levillain647b9ed2014-11-27 12:06:00 +00002221 case Primitive::kPrimLong: {
2222 // Processing a Dex `long-to-double' instruction.
2223 Register low = in.AsRegisterPairLow<Register>();
2224 Register high = in.AsRegisterPairHigh<Register>();
2225 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2226 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002227 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002228 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002229 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2230 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002231
Roland Levillain682393c2015-04-14 15:57:52 +01002232 // temp_d = int-to-double(high)
2233 __ vmovsr(temp_s, high);
2234 __ vcvtdi(temp_d, temp_s);
2235 // constant_d = k2Pow32EncodingForDouble
2236 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2237 // out_d = unsigned-to-double(low)
2238 __ vmovsr(out_s, low);
2239 __ vcvtdu(out_d, out_s);
2240 // out_d += temp_d * constant_d
2241 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002242 break;
2243 }
2244
Roland Levillaincff13742014-11-17 14:32:17 +00002245 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002246 // Processing a Dex `float-to-double' instruction.
2247 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2248 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002249 break;
2250
2251 default:
2252 LOG(FATAL) << "Unexpected type conversion from " << input_type
2253 << " to " << result_type;
2254 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002255 break;
2256
2257 default:
2258 LOG(FATAL) << "Unexpected type conversion from " << input_type
2259 << " to " << result_type;
2260 }
2261}
2262
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002263void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002264 LocationSummary* locations =
2265 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002266 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002267 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002268 locations->SetInAt(0, Location::RequiresRegister());
2269 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2271 break;
2272 }
2273
2274 case Primitive::kPrimLong: {
2275 locations->SetInAt(0, Location::RequiresRegister());
2276 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002278 break;
2279 }
2280
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002281 case Primitive::kPrimFloat:
2282 case Primitive::kPrimDouble: {
2283 locations->SetInAt(0, Location::RequiresFpuRegister());
2284 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002285 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002286 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002287 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002288
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002289 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002290 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002291 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002292}
2293
2294void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2295 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002296 Location out = locations->Out();
2297 Location first = locations->InAt(0);
2298 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002299 switch (add->GetResultType()) {
2300 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002301 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002302 __ add(out.AsRegister<Register>(),
2303 first.AsRegister<Register>(),
2304 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002305 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 __ AddConstant(out.AsRegister<Register>(),
2307 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002308 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002309 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002310 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002311
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002312 case Primitive::kPrimLong: {
2313 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002314 __ adds(out.AsRegisterPairLow<Register>(),
2315 first.AsRegisterPairLow<Register>(),
2316 ShifterOperand(second.AsRegisterPairLow<Register>()));
2317 __ adc(out.AsRegisterPairHigh<Register>(),
2318 first.AsRegisterPairHigh<Register>(),
2319 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002320 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002321 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002322
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002323 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002324 __ vadds(out.AsFpuRegister<SRegister>(),
2325 first.AsFpuRegister<SRegister>(),
2326 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002327 break;
2328
2329 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002330 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2331 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2332 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002333 break;
2334
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002335 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002336 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002337 }
2338}
2339
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002340void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002341 LocationSummary* locations =
2342 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002343 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002344 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002345 locations->SetInAt(0, Location::RequiresRegister());
2346 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2348 break;
2349 }
2350
2351 case Primitive::kPrimLong: {
2352 locations->SetInAt(0, Location::RequiresRegister());
2353 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002355 break;
2356 }
Calin Juravle11351682014-10-23 15:38:15 +01002357 case Primitive::kPrimFloat:
2358 case Primitive::kPrimDouble: {
2359 locations->SetInAt(0, Location::RequiresFpuRegister());
2360 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002361 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002362 break;
Calin Juravle11351682014-10-23 15:38:15 +01002363 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002364 default:
Calin Juravle11351682014-10-23 15:38:15 +01002365 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002366 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002367}
2368
2369void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2370 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002371 Location out = locations->Out();
2372 Location first = locations->InAt(0);
2373 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002374 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002375 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002376 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002377 __ sub(out.AsRegister<Register>(),
2378 first.AsRegister<Register>(),
2379 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002380 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002381 __ AddConstant(out.AsRegister<Register>(),
2382 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002383 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002384 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002385 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002386 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002387
Calin Juravle11351682014-10-23 15:38:15 +01002388 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002389 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002390 __ subs(out.AsRegisterPairLow<Register>(),
2391 first.AsRegisterPairLow<Register>(),
2392 ShifterOperand(second.AsRegisterPairLow<Register>()));
2393 __ sbc(out.AsRegisterPairHigh<Register>(),
2394 first.AsRegisterPairHigh<Register>(),
2395 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002396 break;
Calin Juravle11351682014-10-23 15:38:15 +01002397 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002398
Calin Juravle11351682014-10-23 15:38:15 +01002399 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002400 __ vsubs(out.AsFpuRegister<SRegister>(),
2401 first.AsFpuRegister<SRegister>(),
2402 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002403 break;
Calin Juravle11351682014-10-23 15:38:15 +01002404 }
2405
2406 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002407 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2408 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2409 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002410 break;
2411 }
2412
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002413
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002414 default:
Calin Juravle11351682014-10-23 15:38:15 +01002415 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002416 }
2417}
2418
Calin Juravle34bacdf2014-10-07 20:23:36 +01002419void LocationsBuilderARM::VisitMul(HMul* mul) {
2420 LocationSummary* locations =
2421 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2422 switch (mul->GetResultType()) {
2423 case Primitive::kPrimInt:
2424 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002425 locations->SetInAt(0, Location::RequiresRegister());
2426 locations->SetInAt(1, Location::RequiresRegister());
2427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002428 break;
2429 }
2430
Calin Juravleb5bfa962014-10-21 18:02:24 +01002431 case Primitive::kPrimFloat:
2432 case Primitive::kPrimDouble: {
2433 locations->SetInAt(0, Location::RequiresFpuRegister());
2434 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002435 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002436 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002437 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002438
2439 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002440 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002441 }
2442}
2443
2444void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2445 LocationSummary* locations = mul->GetLocations();
2446 Location out = locations->Out();
2447 Location first = locations->InAt(0);
2448 Location second = locations->InAt(1);
2449 switch (mul->GetResultType()) {
2450 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002451 __ mul(out.AsRegister<Register>(),
2452 first.AsRegister<Register>(),
2453 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002454 break;
2455 }
2456 case Primitive::kPrimLong: {
2457 Register out_hi = out.AsRegisterPairHigh<Register>();
2458 Register out_lo = out.AsRegisterPairLow<Register>();
2459 Register in1_hi = first.AsRegisterPairHigh<Register>();
2460 Register in1_lo = first.AsRegisterPairLow<Register>();
2461 Register in2_hi = second.AsRegisterPairHigh<Register>();
2462 Register in2_lo = second.AsRegisterPairLow<Register>();
2463
2464 // Extra checks to protect caused by the existence of R1_R2.
2465 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2466 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2467 DCHECK_NE(out_hi, in1_lo);
2468 DCHECK_NE(out_hi, in2_lo);
2469
2470 // input: in1 - 64 bits, in2 - 64 bits
2471 // output: out
2472 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2473 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2474 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2475
2476 // IP <- in1.lo * in2.hi
2477 __ mul(IP, in1_lo, in2_hi);
2478 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2479 __ mla(out_hi, in1_hi, in2_lo, IP);
2480 // out.lo <- (in1.lo * in2.lo)[31:0];
2481 __ umull(out_lo, IP, in1_lo, in2_lo);
2482 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2483 __ add(out_hi, out_hi, ShifterOperand(IP));
2484 break;
2485 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002486
2487 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002488 __ vmuls(out.AsFpuRegister<SRegister>(),
2489 first.AsFpuRegister<SRegister>(),
2490 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002491 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002492 }
2493
2494 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002495 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2496 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2497 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002498 break;
2499 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002500
2501 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002502 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002503 }
2504}
2505
Zheng Xuc6667102015-05-15 16:08:45 +08002506void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2507 DCHECK(instruction->IsDiv() || instruction->IsRem());
2508 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2509
2510 LocationSummary* locations = instruction->GetLocations();
2511 Location second = locations->InAt(1);
2512 DCHECK(second.IsConstant());
2513
2514 Register out = locations->Out().AsRegister<Register>();
2515 Register dividend = locations->InAt(0).AsRegister<Register>();
2516 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2517 DCHECK(imm == 1 || imm == -1);
2518
2519 if (instruction->IsRem()) {
2520 __ LoadImmediate(out, 0);
2521 } else {
2522 if (imm == 1) {
2523 __ Mov(out, dividend);
2524 } else {
2525 __ rsb(out, dividend, ShifterOperand(0));
2526 }
2527 }
2528}
2529
2530void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2531 DCHECK(instruction->IsDiv() || instruction->IsRem());
2532 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2533
2534 LocationSummary* locations = instruction->GetLocations();
2535 Location second = locations->InAt(1);
2536 DCHECK(second.IsConstant());
2537
2538 Register out = locations->Out().AsRegister<Register>();
2539 Register dividend = locations->InAt(0).AsRegister<Register>();
2540 Register temp = locations->GetTemp(0).AsRegister<Register>();
2541 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002542 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002543 DCHECK(IsPowerOfTwo(abs_imm));
2544 int ctz_imm = CTZ(abs_imm);
2545
2546 if (ctz_imm == 1) {
2547 __ Lsr(temp, dividend, 32 - ctz_imm);
2548 } else {
2549 __ Asr(temp, dividend, 31);
2550 __ Lsr(temp, temp, 32 - ctz_imm);
2551 }
2552 __ add(out, temp, ShifterOperand(dividend));
2553
2554 if (instruction->IsDiv()) {
2555 __ Asr(out, out, ctz_imm);
2556 if (imm < 0) {
2557 __ rsb(out, out, ShifterOperand(0));
2558 }
2559 } else {
2560 __ ubfx(out, out, 0, ctz_imm);
2561 __ sub(out, out, ShifterOperand(temp));
2562 }
2563}
2564
2565void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2566 DCHECK(instruction->IsDiv() || instruction->IsRem());
2567 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2568
2569 LocationSummary* locations = instruction->GetLocations();
2570 Location second = locations->InAt(1);
2571 DCHECK(second.IsConstant());
2572
2573 Register out = locations->Out().AsRegister<Register>();
2574 Register dividend = locations->InAt(0).AsRegister<Register>();
2575 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2576 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2577 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2578
2579 int64_t magic;
2580 int shift;
2581 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2582
2583 __ LoadImmediate(temp1, magic);
2584 __ smull(temp2, temp1, dividend, temp1);
2585
2586 if (imm > 0 && magic < 0) {
2587 __ add(temp1, temp1, ShifterOperand(dividend));
2588 } else if (imm < 0 && magic > 0) {
2589 __ sub(temp1, temp1, ShifterOperand(dividend));
2590 }
2591
2592 if (shift != 0) {
2593 __ Asr(temp1, temp1, shift);
2594 }
2595
2596 if (instruction->IsDiv()) {
2597 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2598 } else {
2599 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2600 // TODO: Strength reduction for mls.
2601 __ LoadImmediate(temp2, imm);
2602 __ mls(out, temp1, temp2, dividend);
2603 }
2604}
2605
2606void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2607 DCHECK(instruction->IsDiv() || instruction->IsRem());
2608 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2609
2610 LocationSummary* locations = instruction->GetLocations();
2611 Location second = locations->InAt(1);
2612 DCHECK(second.IsConstant());
2613
2614 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2615 if (imm == 0) {
2616 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2617 } else if (imm == 1 || imm == -1) {
2618 DivRemOneOrMinusOne(instruction);
2619 } else if (IsPowerOfTwo(std::abs(imm))) {
2620 DivRemByPowerOfTwo(instruction);
2621 } else {
2622 DCHECK(imm <= -2 || imm >= 2);
2623 GenerateDivRemWithAnyConstant(instruction);
2624 }
2625}
2626
Calin Juravle7c4954d2014-10-28 16:57:40 +00002627void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002628 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2629 if (div->GetResultType() == Primitive::kPrimLong) {
2630 // pLdiv runtime call.
2631 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002632 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2633 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002634 } else if (div->GetResultType() == Primitive::kPrimInt &&
2635 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2636 // pIdivmod runtime call.
2637 call_kind = LocationSummary::kCall;
2638 }
2639
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2641
Calin Juravle7c4954d2014-10-28 16:57:40 +00002642 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002643 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002644 if (div->InputAt(1)->IsConstant()) {
2645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2648 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2649 if (abs_imm <= 1) {
2650 // No temp register required.
2651 } else {
2652 locations->AddTemp(Location::RequiresRegister());
2653 if (!IsPowerOfTwo(abs_imm)) {
2654 locations->AddTemp(Location::RequiresRegister());
2655 }
2656 }
2657 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002658 locations->SetInAt(0, Location::RequiresRegister());
2659 locations->SetInAt(1, Location::RequiresRegister());
2660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2661 } else {
2662 InvokeRuntimeCallingConvention calling_convention;
2663 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2664 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2665 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2666 // we only need the former.
2667 locations->SetOut(Location::RegisterLocation(R0));
2668 }
Calin Juravled0d48522014-11-04 16:40:20 +00002669 break;
2670 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002671 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002672 InvokeRuntimeCallingConvention calling_convention;
2673 locations->SetInAt(0, Location::RegisterPairLocation(
2674 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2675 locations->SetInAt(1, Location::RegisterPairLocation(
2676 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002677 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002678 break;
2679 }
2680 case Primitive::kPrimFloat:
2681 case Primitive::kPrimDouble: {
2682 locations->SetInAt(0, Location::RequiresFpuRegister());
2683 locations->SetInAt(1, Location::RequiresFpuRegister());
2684 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2685 break;
2686 }
2687
2688 default:
2689 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2690 }
2691}
2692
2693void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2694 LocationSummary* locations = div->GetLocations();
2695 Location out = locations->Out();
2696 Location first = locations->InAt(0);
2697 Location second = locations->InAt(1);
2698
2699 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002700 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002701 if (second.IsConstant()) {
2702 GenerateDivRemConstantIntegral(div);
2703 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002704 __ sdiv(out.AsRegister<Register>(),
2705 first.AsRegister<Register>(),
2706 second.AsRegister<Register>());
2707 } else {
2708 InvokeRuntimeCallingConvention calling_convention;
2709 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2710 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2711 DCHECK_EQ(R0, out.AsRegister<Register>());
2712
2713 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2714 }
Calin Juravled0d48522014-11-04 16:40:20 +00002715 break;
2716 }
2717
Calin Juravle7c4954d2014-10-28 16:57:40 +00002718 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002719 InvokeRuntimeCallingConvention calling_convention;
2720 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2721 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2722 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2723 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2724 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002725 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002726
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002727 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002728 break;
2729 }
2730
2731 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002732 __ vdivs(out.AsFpuRegister<SRegister>(),
2733 first.AsFpuRegister<SRegister>(),
2734 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002735 break;
2736 }
2737
2738 case Primitive::kPrimDouble: {
2739 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2740 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2741 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2742 break;
2743 }
2744
2745 default:
2746 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2747 }
2748}
2749
Calin Juravlebacfec32014-11-14 15:54:36 +00002750void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002751 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002752
2753 // Most remainders are implemented in the runtime.
2754 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002755 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2756 // sdiv will be replaced by other instruction sequence.
2757 call_kind = LocationSummary::kNoCall;
2758 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2759 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002760 // Have hardware divide instruction for int, do it with three instructions.
2761 call_kind = LocationSummary::kNoCall;
2762 }
2763
Calin Juravlebacfec32014-11-14 15:54:36 +00002764 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2765
Calin Juravled2ec87d2014-12-08 14:24:46 +00002766 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002767 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002768 if (rem->InputAt(1)->IsConstant()) {
2769 locations->SetInAt(0, Location::RequiresRegister());
2770 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2772 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2773 if (abs_imm <= 1) {
2774 // No temp register required.
2775 } else {
2776 locations->AddTemp(Location::RequiresRegister());
2777 if (!IsPowerOfTwo(abs_imm)) {
2778 locations->AddTemp(Location::RequiresRegister());
2779 }
2780 }
2781 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002782 locations->SetInAt(0, Location::RequiresRegister());
2783 locations->SetInAt(1, Location::RequiresRegister());
2784 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2785 locations->AddTemp(Location::RequiresRegister());
2786 } else {
2787 InvokeRuntimeCallingConvention calling_convention;
2788 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2789 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2790 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2791 // we only need the latter.
2792 locations->SetOut(Location::RegisterLocation(R1));
2793 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002794 break;
2795 }
2796 case Primitive::kPrimLong: {
2797 InvokeRuntimeCallingConvention calling_convention;
2798 locations->SetInAt(0, Location::RegisterPairLocation(
2799 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2800 locations->SetInAt(1, Location::RegisterPairLocation(
2801 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2802 // The runtime helper puts the output in R2,R3.
2803 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2804 break;
2805 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002806 case Primitive::kPrimFloat: {
2807 InvokeRuntimeCallingConvention calling_convention;
2808 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2809 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2810 locations->SetOut(Location::FpuRegisterLocation(S0));
2811 break;
2812 }
2813
Calin Juravlebacfec32014-11-14 15:54:36 +00002814 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002815 InvokeRuntimeCallingConvention calling_convention;
2816 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2817 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2818 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2819 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2820 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002821 break;
2822 }
2823
2824 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002825 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002826 }
2827}
2828
2829void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2830 LocationSummary* locations = rem->GetLocations();
2831 Location out = locations->Out();
2832 Location first = locations->InAt(0);
2833 Location second = locations->InAt(1);
2834
Calin Juravled2ec87d2014-12-08 14:24:46 +00002835 Primitive::Type type = rem->GetResultType();
2836 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002837 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002838 if (second.IsConstant()) {
2839 GenerateDivRemConstantIntegral(rem);
2840 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002841 Register reg1 = first.AsRegister<Register>();
2842 Register reg2 = second.AsRegister<Register>();
2843 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002844
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002845 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002846 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002847 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002848 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002849 } else {
2850 InvokeRuntimeCallingConvention calling_convention;
2851 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2852 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2853 DCHECK_EQ(R1, out.AsRegister<Register>());
2854
2855 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2856 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002857 break;
2858 }
2859
2860 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002861 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002862 break;
2863 }
2864
Calin Juravled2ec87d2014-12-08 14:24:46 +00002865 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002866 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002867 break;
2868 }
2869
Calin Juravlebacfec32014-11-14 15:54:36 +00002870 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002871 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002872 break;
2873 }
2874
2875 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002876 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002877 }
2878}
2879
Calin Juravled0d48522014-11-04 16:40:20 +00002880void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002881 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2882 ? LocationSummary::kCallOnSlowPath
2883 : LocationSummary::kNoCall;
2884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002885 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002886 if (instruction->HasUses()) {
2887 locations->SetOut(Location::SameAsFirstInput());
2888 }
2889}
2890
2891void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002892 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00002893 codegen_->AddSlowPath(slow_path);
2894
2895 LocationSummary* locations = instruction->GetLocations();
2896 Location value = locations->InAt(0);
2897
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002898 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002899 case Primitive::kPrimByte:
2900 case Primitive::kPrimChar:
2901 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002902 case Primitive::kPrimInt: {
2903 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002904 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002905 } else {
2906 DCHECK(value.IsConstant()) << value;
2907 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2908 __ b(slow_path->GetEntryLabel());
2909 }
2910 }
2911 break;
2912 }
2913 case Primitive::kPrimLong: {
2914 if (value.IsRegisterPair()) {
2915 __ orrs(IP,
2916 value.AsRegisterPairLow<Register>(),
2917 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2918 __ b(slow_path->GetEntryLabel(), EQ);
2919 } else {
2920 DCHECK(value.IsConstant()) << value;
2921 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2922 __ b(slow_path->GetEntryLabel());
2923 }
2924 }
2925 break;
2926 default:
2927 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2928 }
2929 }
Calin Juravled0d48522014-11-04 16:40:20 +00002930}
2931
Calin Juravle9aec02f2014-11-18 23:06:35 +00002932void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2933 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2934
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002935 LocationSummary* locations =
2936 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002937
2938 switch (op->GetResultType()) {
2939 case Primitive::kPrimInt: {
2940 locations->SetInAt(0, Location::RequiresRegister());
2941 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002942 // Make the output overlap, as it will be used to hold the masked
2943 // second input.
2944 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002945 break;
2946 }
2947 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002948 locations->SetInAt(0, Location::RequiresRegister());
2949 locations->SetInAt(1, Location::RequiresRegister());
2950 locations->AddTemp(Location::RequiresRegister());
2951 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002952 break;
2953 }
2954 default:
2955 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2956 }
2957}
2958
2959void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2960 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2961
2962 LocationSummary* locations = op->GetLocations();
2963 Location out = locations->Out();
2964 Location first = locations->InAt(0);
2965 Location second = locations->InAt(1);
2966
2967 Primitive::Type type = op->GetResultType();
2968 switch (type) {
2969 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 Register out_reg = out.AsRegister<Register>();
2971 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002972 // Arm doesn't mask the shift count so we need to do it ourselves.
2973 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002975 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002976 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002977 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002978 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002979 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002980 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002981 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002982 }
2983 } else {
2984 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2985 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2986 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2987 __ Mov(out_reg, first_reg);
2988 } else if (op->IsShl()) {
2989 __ Lsl(out_reg, first_reg, shift_value);
2990 } else if (op->IsShr()) {
2991 __ Asr(out_reg, first_reg, shift_value);
2992 } else {
2993 __ Lsr(out_reg, first_reg, shift_value);
2994 }
2995 }
2996 break;
2997 }
2998 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002999 Register o_h = out.AsRegisterPairHigh<Register>();
3000 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003001
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003002 Register temp = locations->GetTemp(0).AsRegister<Register>();
3003
3004 Register high = first.AsRegisterPairHigh<Register>();
3005 Register low = first.AsRegisterPairLow<Register>();
3006
3007 Register second_reg = second.AsRegister<Register>();
3008
Calin Juravle9aec02f2014-11-18 23:06:35 +00003009 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003010 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003011 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003012 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003013 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003014 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003015 __ Lsr(temp, low, temp);
3016 __ orr(o_h, o_h, ShifterOperand(temp));
3017 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003018 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003019 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003020 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003021 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003022 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003023 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003024 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003025 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003026 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003027 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003028 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003029 __ Lsl(temp, high, temp);
3030 __ orr(o_l, o_l, ShifterOperand(temp));
3031 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003032 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003033 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003034 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003035 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003036 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003037 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003038 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003039 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003040 __ Lsr(o_l, low, o_h);
3041 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003042 __ Lsl(temp, high, temp);
3043 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003044 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003045 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003046 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003047 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003048 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003049 break;
3050 }
3051 default:
3052 LOG(FATAL) << "Unexpected operation type " << type;
3053 }
3054}
3055
3056void LocationsBuilderARM::VisitShl(HShl* shl) {
3057 HandleShift(shl);
3058}
3059
3060void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3061 HandleShift(shl);
3062}
3063
3064void LocationsBuilderARM::VisitShr(HShr* shr) {
3065 HandleShift(shr);
3066}
3067
3068void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3069 HandleShift(shr);
3070}
3071
3072void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3073 HandleShift(ushr);
3074}
3075
3076void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3077 HandleShift(ushr);
3078}
3079
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003080void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003081 LocationSummary* locations =
3082 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003083 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003084 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003085 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003086 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003087}
3088
3089void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3090 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003091 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003092 // Note: if heap poisoning is enabled, the entry point takes cares
3093 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003094 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003095 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003096 instruction->GetDexPc(),
3097 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003098}
3099
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003100void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3101 LocationSummary* locations =
3102 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3103 InvokeRuntimeCallingConvention calling_convention;
3104 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003105 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003106 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003107 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003108}
3109
3110void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3111 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003112 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003113 // Note: if heap poisoning is enabled, the entry point takes cares
3114 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003115 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003116 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003117 instruction->GetDexPc(),
3118 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003119}
3120
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003121void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003122 LocationSummary* locations =
3123 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003124 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3125 if (location.IsStackSlot()) {
3126 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3127 } else if (location.IsDoubleStackSlot()) {
3128 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003129 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003130 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003131}
3132
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003133void InstructionCodeGeneratorARM::VisitParameterValue(
3134 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003135 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003136}
3137
3138void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3139 LocationSummary* locations =
3140 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3141 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3142}
3143
3144void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3145 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003146}
3147
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003148void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003149 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003150 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003151 locations->SetInAt(0, Location::RequiresRegister());
3152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003153}
3154
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003155void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3156 LocationSummary* locations = not_->GetLocations();
3157 Location out = locations->Out();
3158 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003159 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003160 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003161 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003162 break;
3163
3164 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003165 __ mvn(out.AsRegisterPairLow<Register>(),
3166 ShifterOperand(in.AsRegisterPairLow<Register>()));
3167 __ mvn(out.AsRegisterPairHigh<Register>(),
3168 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003169 break;
3170
3171 default:
3172 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3173 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003174}
3175
David Brazdil66d126e2015-04-03 16:02:44 +01003176void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3177 LocationSummary* locations =
3178 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3179 locations->SetInAt(0, Location::RequiresRegister());
3180 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3181}
3182
3183void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003184 LocationSummary* locations = bool_not->GetLocations();
3185 Location out = locations->Out();
3186 Location in = locations->InAt(0);
3187 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3188}
3189
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003190void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003191 LocationSummary* locations =
3192 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003193 switch (compare->InputAt(0)->GetType()) {
3194 case Primitive::kPrimLong: {
3195 locations->SetInAt(0, Location::RequiresRegister());
3196 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003197 // Output overlaps because it is written before doing the low comparison.
3198 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003199 break;
3200 }
3201 case Primitive::kPrimFloat:
3202 case Primitive::kPrimDouble: {
3203 locations->SetInAt(0, Location::RequiresFpuRegister());
3204 locations->SetInAt(1, Location::RequiresFpuRegister());
3205 locations->SetOut(Location::RequiresRegister());
3206 break;
3207 }
3208 default:
3209 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3210 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003211}
3212
3213void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003214 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003215 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003216 Location left = locations->InAt(0);
3217 Location right = locations->InAt(1);
3218
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003219 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003220 Primitive::Type type = compare->InputAt(0)->GetType();
3221 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003222 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003223 __ cmp(left.AsRegisterPairHigh<Register>(),
3224 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003225 __ b(&less, LT);
3226 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003227 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003228 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003229 __ cmp(left.AsRegisterPairLow<Register>(),
3230 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003231 break;
3232 }
3233 case Primitive::kPrimFloat:
3234 case Primitive::kPrimDouble: {
3235 __ LoadImmediate(out, 0);
3236 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003237 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003238 } else {
3239 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3240 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3241 }
3242 __ vmstat(); // transfer FP status register to ARM APSR.
3243 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003244 break;
3245 }
3246 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003247 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003248 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003249 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003250 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003251
3252 __ Bind(&greater);
3253 __ LoadImmediate(out, 1);
3254 __ b(&done);
3255
3256 __ Bind(&less);
3257 __ LoadImmediate(out, -1);
3258
3259 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003260}
3261
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003262void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003263 LocationSummary* locations =
3264 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003265 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3266 locations->SetInAt(i, Location::Any());
3267 }
3268 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003269}
3270
3271void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003272 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003273 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003274}
3275
Calin Juravle52c48962014-12-16 17:02:57 +00003276void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3277 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003278 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003279 switch (kind) {
3280 case MemBarrierKind::kAnyStore:
3281 case MemBarrierKind::kLoadAny:
3282 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003283 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003284 break;
3285 }
3286 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003287 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003288 break;
3289 }
3290 default:
3291 LOG(FATAL) << "Unexpected memory barrier " << kind;
3292 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003293 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003294}
3295
3296void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3297 uint32_t offset,
3298 Register out_lo,
3299 Register out_hi) {
3300 if (offset != 0) {
3301 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003302 __ add(IP, addr, ShifterOperand(out_lo));
3303 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003304 }
3305 __ ldrexd(out_lo, out_hi, addr);
3306}
3307
3308void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3309 uint32_t offset,
3310 Register value_lo,
3311 Register value_hi,
3312 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003313 Register temp2,
3314 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003315 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003316 if (offset != 0) {
3317 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003318 __ add(IP, addr, ShifterOperand(temp1));
3319 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003320 }
3321 __ Bind(&fail);
3322 // We need a load followed by store. (The address used in a STREX instruction must
3323 // be the same as the address in the most recently executed LDREX instruction.)
3324 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003326 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003327 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003328}
3329
3330void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3331 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3332
Nicolas Geoffray39468442014-09-02 15:17:15 +01003333 LocationSummary* locations =
3334 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003335 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003336
Calin Juravle52c48962014-12-16 17:02:57 +00003337 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003338 if (Primitive::IsFloatingPointType(field_type)) {
3339 locations->SetInAt(1, Location::RequiresFpuRegister());
3340 } else {
3341 locations->SetInAt(1, Location::RequiresRegister());
3342 }
3343
Calin Juravle52c48962014-12-16 17:02:57 +00003344 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003345 bool generate_volatile = field_info.IsVolatile()
3346 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003347 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003348 bool needs_write_barrier =
3349 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003350 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003351 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003352 if (needs_write_barrier) {
3353 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003354 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003355 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003356 // Arm encoding have some additional constraints for ldrexd/strexd:
3357 // - registers need to be consecutive
3358 // - the first register should be even but not R14.
3359 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3360 // enable Arm encoding.
3361 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3362
3363 locations->AddTemp(Location::RequiresRegister());
3364 locations->AddTemp(Location::RequiresRegister());
3365 if (field_type == Primitive::kPrimDouble) {
3366 // For doubles we need two more registers to copy the value.
3367 locations->AddTemp(Location::RegisterLocation(R2));
3368 locations->AddTemp(Location::RegisterLocation(R3));
3369 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003370 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003371}
3372
Calin Juravle52c48962014-12-16 17:02:57 +00003373void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003374 const FieldInfo& field_info,
3375 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003376 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3377
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003378 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003379 Register base = locations->InAt(0).AsRegister<Register>();
3380 Location value = locations->InAt(1);
3381
3382 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003383 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003384 Primitive::Type field_type = field_info.GetFieldType();
3385 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003386 bool needs_write_barrier =
3387 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003388
3389 if (is_volatile) {
3390 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3391 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003392
3393 switch (field_type) {
3394 case Primitive::kPrimBoolean:
3395 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003396 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003397 break;
3398 }
3399
3400 case Primitive::kPrimShort:
3401 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003402 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003403 break;
3404 }
3405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003406 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003407 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003408 if (kPoisonHeapReferences && needs_write_barrier) {
3409 // Note that in the case where `value` is a null reference,
3410 // we do not enter this block, as a null reference does not
3411 // need poisoning.
3412 DCHECK_EQ(field_type, Primitive::kPrimNot);
3413 Register temp = locations->GetTemp(0).AsRegister<Register>();
3414 __ Mov(temp, value.AsRegister<Register>());
3415 __ PoisonHeapReference(temp);
3416 __ StoreToOffset(kStoreWord, temp, base, offset);
3417 } else {
3418 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3419 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003420 break;
3421 }
3422
3423 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003424 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003425 GenerateWideAtomicStore(base, offset,
3426 value.AsRegisterPairLow<Register>(),
3427 value.AsRegisterPairHigh<Register>(),
3428 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003429 locations->GetTemp(1).AsRegister<Register>(),
3430 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003431 } else {
3432 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003433 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003434 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003435 break;
3436 }
3437
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003438 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003439 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003440 break;
3441 }
3442
3443 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003444 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003445 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003446 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3447 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3448
3449 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3450
3451 GenerateWideAtomicStore(base, offset,
3452 value_reg_lo,
3453 value_reg_hi,
3454 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003455 locations->GetTemp(3).AsRegister<Register>(),
3456 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003457 } else {
3458 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003459 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003460 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003461 break;
3462 }
3463
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003464 case Primitive::kPrimVoid:
3465 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003466 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003467 }
Calin Juravle52c48962014-12-16 17:02:57 +00003468
Calin Juravle77520bc2015-01-12 18:45:46 +00003469 // Longs and doubles are handled in the switch.
3470 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3471 codegen_->MaybeRecordImplicitNullCheck(instruction);
3472 }
3473
3474 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3475 Register temp = locations->GetTemp(0).AsRegister<Register>();
3476 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003477 codegen_->MarkGCCard(
3478 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003479 }
3480
Calin Juravle52c48962014-12-16 17:02:57 +00003481 if (is_volatile) {
3482 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3483 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003484}
3485
Calin Juravle52c48962014-12-16 17:02:57 +00003486void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3487 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003488 LocationSummary* locations =
3489 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003490 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003491
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003492 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003493 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003494 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003495 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003496
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003497 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3498 locations->SetOut(Location::RequiresFpuRegister());
3499 } else {
3500 locations->SetOut(Location::RequiresRegister(),
3501 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3502 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003503 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003504 // Arm encoding have some additional constraints for ldrexd/strexd:
3505 // - registers need to be consecutive
3506 // - the first register should be even but not R14.
3507 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3508 // enable Arm encoding.
3509 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3510 locations->AddTemp(Location::RequiresRegister());
3511 locations->AddTemp(Location::RequiresRegister());
3512 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003513}
3514
Calin Juravle52c48962014-12-16 17:02:57 +00003515void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3516 const FieldInfo& field_info) {
3517 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003518
Calin Juravle52c48962014-12-16 17:02:57 +00003519 LocationSummary* locations = instruction->GetLocations();
3520 Register base = locations->InAt(0).AsRegister<Register>();
3521 Location out = locations->Out();
3522 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003523 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003524 Primitive::Type field_type = field_info.GetFieldType();
3525 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3526
3527 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003528 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003529 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003530 break;
3531 }
3532
3533 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003534 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003535 break;
3536 }
3537
3538 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003539 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003540 break;
3541 }
3542
3543 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003544 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003545 break;
3546 }
3547
3548 case Primitive::kPrimInt:
3549 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003550 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003551 break;
3552 }
3553
3554 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003555 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003556 GenerateWideAtomicLoad(base, offset,
3557 out.AsRegisterPairLow<Register>(),
3558 out.AsRegisterPairHigh<Register>());
3559 } else {
3560 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3561 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003562 break;
3563 }
3564
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003565 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003566 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003567 break;
3568 }
3569
3570 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003571 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003572 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003573 Register lo = locations->GetTemp(0).AsRegister<Register>();
3574 Register hi = locations->GetTemp(1).AsRegister<Register>();
3575 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003576 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003577 __ vmovdrr(out_reg, lo, hi);
3578 } else {
3579 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003580 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003581 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003582 break;
3583 }
3584
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003585 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003586 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003587 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003588 }
Calin Juravle52c48962014-12-16 17:02:57 +00003589
Calin Juravle77520bc2015-01-12 18:45:46 +00003590 // Doubles are handled in the switch.
3591 if (field_type != Primitive::kPrimDouble) {
3592 codegen_->MaybeRecordImplicitNullCheck(instruction);
3593 }
3594
Calin Juravle52c48962014-12-16 17:02:57 +00003595 if (is_volatile) {
3596 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3597 }
Roland Levillain4d027112015-07-01 15:41:14 +01003598
3599 if (field_type == Primitive::kPrimNot) {
3600 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3601 }
Calin Juravle52c48962014-12-16 17:02:57 +00003602}
3603
3604void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3605 HandleFieldSet(instruction, instruction->GetFieldInfo());
3606}
3607
3608void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003609 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003610}
3611
3612void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3613 HandleFieldGet(instruction, instruction->GetFieldInfo());
3614}
3615
3616void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3617 HandleFieldGet(instruction, instruction->GetFieldInfo());
3618}
3619
3620void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3621 HandleFieldGet(instruction, instruction->GetFieldInfo());
3622}
3623
3624void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3625 HandleFieldGet(instruction, instruction->GetFieldInfo());
3626}
3627
3628void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3629 HandleFieldSet(instruction, instruction->GetFieldInfo());
3630}
3631
3632void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003633 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003634}
3635
Calin Juravlee460d1d2015-09-29 04:52:17 +01003636void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
3637 HUnresolvedInstanceFieldGet* instruction) {
3638 FieldAccessCallingConventionARM calling_convention;
3639 codegen_->CreateUnresolvedFieldLocationSummary(
3640 instruction, instruction->GetFieldType(), calling_convention);
3641}
3642
3643void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
3644 HUnresolvedInstanceFieldGet* instruction) {
3645 FieldAccessCallingConventionARM calling_convention;
3646 codegen_->GenerateUnresolvedFieldAccess(instruction,
3647 instruction->GetFieldType(),
3648 instruction->GetFieldIndex(),
3649 instruction->GetDexPc(),
3650 calling_convention);
3651}
3652
3653void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
3654 HUnresolvedInstanceFieldSet* instruction) {
3655 FieldAccessCallingConventionARM calling_convention;
3656 codegen_->CreateUnresolvedFieldLocationSummary(
3657 instruction, instruction->GetFieldType(), calling_convention);
3658}
3659
3660void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
3661 HUnresolvedInstanceFieldSet* instruction) {
3662 FieldAccessCallingConventionARM calling_convention;
3663 codegen_->GenerateUnresolvedFieldAccess(instruction,
3664 instruction->GetFieldType(),
3665 instruction->GetFieldIndex(),
3666 instruction->GetDexPc(),
3667 calling_convention);
3668}
3669
3670void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
3671 HUnresolvedStaticFieldGet* instruction) {
3672 FieldAccessCallingConventionARM calling_convention;
3673 codegen_->CreateUnresolvedFieldLocationSummary(
3674 instruction, instruction->GetFieldType(), calling_convention);
3675}
3676
3677void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
3678 HUnresolvedStaticFieldGet* instruction) {
3679 FieldAccessCallingConventionARM calling_convention;
3680 codegen_->GenerateUnresolvedFieldAccess(instruction,
3681 instruction->GetFieldType(),
3682 instruction->GetFieldIndex(),
3683 instruction->GetDexPc(),
3684 calling_convention);
3685}
3686
3687void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
3688 HUnresolvedStaticFieldSet* instruction) {
3689 FieldAccessCallingConventionARM calling_convention;
3690 codegen_->CreateUnresolvedFieldLocationSummary(
3691 instruction, instruction->GetFieldType(), calling_convention);
3692}
3693
3694void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
3695 HUnresolvedStaticFieldSet* instruction) {
3696 FieldAccessCallingConventionARM calling_convention;
3697 codegen_->GenerateUnresolvedFieldAccess(instruction,
3698 instruction->GetFieldType(),
3699 instruction->GetFieldIndex(),
3700 instruction->GetDexPc(),
3701 calling_convention);
3702}
3703
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003704void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003705 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3706 ? LocationSummary::kCallOnSlowPath
3707 : LocationSummary::kNoCall;
3708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003709 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003710 if (instruction->HasUses()) {
3711 locations->SetOut(Location::SameAsFirstInput());
3712 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003713}
3714
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003715void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003716 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3717 return;
3718 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003719 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003720
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003721 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3722 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3723}
3724
3725void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003726 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003727 codegen_->AddSlowPath(slow_path);
3728
3729 LocationSummary* locations = instruction->GetLocations();
3730 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003731
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003732 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003733}
3734
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003735void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003736 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003737 GenerateImplicitNullCheck(instruction);
3738 } else {
3739 GenerateExplicitNullCheck(instruction);
3740 }
3741}
3742
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003743void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003744 LocationSummary* locations =
3745 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003746 locations->SetInAt(0, Location::RequiresRegister());
3747 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003748 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3749 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3750 } else {
3751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3752 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003753}
3754
3755void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3756 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003757 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003758 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003759 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003760
Roland Levillain4d027112015-07-01 15:41:14 +01003761 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003762 case Primitive::kPrimBoolean: {
3763 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003764 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003765 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003766 size_t offset =
3767 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003768 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3769 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003770 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003771 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3772 }
3773 break;
3774 }
3775
3776 case Primitive::kPrimByte: {
3777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003778 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003779 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003780 size_t offset =
3781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003782 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3783 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003784 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003785 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3786 }
3787 break;
3788 }
3789
3790 case Primitive::kPrimShort: {
3791 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003792 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003793 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003794 size_t offset =
3795 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3797 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003799 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3800 }
3801 break;
3802 }
3803
3804 case Primitive::kPrimChar: {
3805 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003806 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003807 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003808 size_t offset =
3809 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003810 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3811 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003812 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003813 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3814 }
3815 break;
3816 }
3817
3818 case Primitive::kPrimInt:
3819 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003820 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3821 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003823 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003824 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003825 size_t offset =
3826 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003827 __ LoadFromOffset(kLoadWord, out, obj, offset);
3828 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003829 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003830 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3831 }
3832 break;
3833 }
3834
3835 case Primitive::kPrimLong: {
3836 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003837 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003838 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003839 size_t offset =
3840 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003841 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003843 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003844 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003845 }
3846 break;
3847 }
3848
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003849 case Primitive::kPrimFloat: {
3850 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3851 Location out = locations->Out();
3852 DCHECK(out.IsFpuRegister());
3853 if (index.IsConstant()) {
3854 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3855 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3856 } else {
3857 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3858 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3859 }
3860 break;
3861 }
3862
3863 case Primitive::kPrimDouble: {
3864 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3865 Location out = locations->Out();
3866 DCHECK(out.IsFpuRegisterPair());
3867 if (index.IsConstant()) {
3868 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3869 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3870 } else {
3871 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3872 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3873 }
3874 break;
3875 }
3876
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003877 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003878 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003879 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003880 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003881 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003882
3883 if (type == Primitive::kPrimNot) {
3884 Register out = locations->Out().AsRegister<Register>();
3885 __ MaybeUnpoisonHeapReference(out);
3886 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003887}
3888
3889void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003890 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003891
3892 bool needs_write_barrier =
3893 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003894 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003895
Nicolas Geoffray39468442014-09-02 15:17:15 +01003896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003897 instruction,
3898 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
3899 locations->SetInAt(0, Location::RequiresRegister());
3900 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3901 if (Primitive::IsFloatingPointType(value_type)) {
3902 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003903 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003904 locations->SetInAt(2, Location::RequiresRegister());
3905 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003906
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003907 if (needs_write_barrier) {
3908 // Temporary registers for the write barrier.
3909 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
3910 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003912}
3913
3914void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3915 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003916 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003917 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003918 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003919 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003920 bool needs_write_barrier =
3921 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922
3923 switch (value_type) {
3924 case Primitive::kPrimBoolean:
3925 case Primitive::kPrimByte: {
3926 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003927 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003928 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003929 size_t offset =
3930 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003931 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003932 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003933 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003934 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3935 }
3936 break;
3937 }
3938
3939 case Primitive::kPrimShort:
3940 case Primitive::kPrimChar: {
3941 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003942 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003943 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003944 size_t offset =
3945 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003946 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003947 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003948 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003949 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3950 }
3951 break;
3952 }
3953
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003954 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003955 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3956 Register value = locations->InAt(2).AsRegister<Register>();
3957 Register source = value;
3958
3959 if (instruction->InputAt(2)->IsNullConstant()) {
3960 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003961 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003962 size_t offset =
3963 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003964 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003965 } else {
3966 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003967 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01003968 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003969 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003970 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003971 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003972
3973 DCHECK(needs_write_barrier);
3974 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3975 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3976 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3977 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3978 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3979 Label done;
3980 SlowPathCode* slow_path = nullptr;
3981
3982 if (may_need_runtime_call) {
3983 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
3984 codegen_->AddSlowPath(slow_path);
3985 if (instruction->GetValueCanBeNull()) {
3986 Label non_zero;
3987 __ CompareAndBranchIfNonZero(value, &non_zero);
3988 if (index.IsConstant()) {
3989 size_t offset =
3990 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3991 __ StoreToOffset(kStoreWord, value, array, offset);
3992 } else {
3993 DCHECK(index.IsRegister()) << index;
3994 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3995 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3996 }
3997 codegen_->MaybeRecordImplicitNullCheck(instruction);
3998 __ b(&done);
3999 __ Bind(&non_zero);
4000 }
4001
4002 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4003 codegen_->MaybeRecordImplicitNullCheck(instruction);
4004 __ MaybeUnpoisonHeapReference(temp1);
4005 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4006 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4007 // No need to poison/unpoison, we're comparing two poisoined references.
4008 __ cmp(temp1, ShifterOperand(temp2));
4009 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4010 Label do_put;
4011 __ b(&do_put, EQ);
4012 __ MaybeUnpoisonHeapReference(temp1);
4013 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4014 // No need to poison/unpoison, we're comparing against null.
4015 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4016 __ Bind(&do_put);
4017 } else {
4018 __ b(slow_path->GetEntryLabel(), NE);
4019 }
4020 }
4021
4022 if (kPoisonHeapReferences) {
4023 // Note that in the case where `value` is a null reference,
4024 // we do not enter this block, as a null reference does not
4025 // need poisoning.
4026 DCHECK_EQ(value_type, Primitive::kPrimNot);
4027 __ Mov(temp1, value);
4028 __ PoisonHeapReference(temp1);
4029 source = temp1;
4030 }
4031
4032 if (index.IsConstant()) {
4033 size_t offset =
4034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4035 __ StoreToOffset(kStoreWord, source, array, offset);
4036 } else {
4037 DCHECK(index.IsRegister()) << index;
4038 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4039 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4040 }
4041
4042 if (!may_need_runtime_call) {
4043 codegen_->MaybeRecordImplicitNullCheck(instruction);
4044 }
4045
4046 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4047
4048 if (done.IsLinked()) {
4049 __ Bind(&done);
4050 }
4051
4052 if (slow_path != nullptr) {
4053 __ Bind(slow_path->GetExitLabel());
4054 }
4055
4056 break;
4057 }
4058
4059 case Primitive::kPrimInt: {
4060 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4061 Register value = locations->InAt(2).AsRegister<Register>();
4062 if (index.IsConstant()) {
4063 size_t offset =
4064 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4065 __ StoreToOffset(kStoreWord, value, array, offset);
4066 } else {
4067 DCHECK(index.IsRegister()) << index;
4068 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4069 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4070 }
4071
4072 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004073 break;
4074 }
4075
4076 case Primitive::kPrimLong: {
4077 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004078 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004079 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004080 size_t offset =
4081 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004082 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004083 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004084 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004085 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004086 }
4087 break;
4088 }
4089
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004090 case Primitive::kPrimFloat: {
4091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4092 Location value = locations->InAt(2);
4093 DCHECK(value.IsFpuRegister());
4094 if (index.IsConstant()) {
4095 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004096 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004097 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004098 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004099 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4100 }
4101 break;
4102 }
4103
4104 case Primitive::kPrimDouble: {
4105 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4106 Location value = locations->InAt(2);
4107 DCHECK(value.IsFpuRegisterPair());
4108 if (index.IsConstant()) {
4109 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004110 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004111 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004112 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004113 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4114 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004115
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004116 break;
4117 }
4118
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004119 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004120 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004121 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004123
4124 // Ints and objects are handled in the switch.
4125 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4126 codegen_->MaybeRecordImplicitNullCheck(instruction);
4127 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004128}
4129
4130void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004131 LocationSummary* locations =
4132 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004133 locations->SetInAt(0, Location::RequiresRegister());
4134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004135}
4136
4137void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4138 LocationSummary* locations = instruction->GetLocations();
4139 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004140 Register obj = locations->InAt(0).AsRegister<Register>();
4141 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004142 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004143 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004144}
4145
4146void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004147 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4148 ? LocationSummary::kCallOnSlowPath
4149 : LocationSummary::kNoCall;
4150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004151 locations->SetInAt(0, Location::RequiresRegister());
4152 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004153 if (instruction->HasUses()) {
4154 locations->SetOut(Location::SameAsFirstInput());
4155 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004156}
4157
4158void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4159 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004160 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004161 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004162 codegen_->AddSlowPath(slow_path);
4163
Roland Levillain271ab9c2014-11-27 15:23:57 +00004164 Register index = locations->InAt(0).AsRegister<Register>();
4165 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004166
4167 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004168 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004169}
4170
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004171void CodeGeneratorARM::MarkGCCard(Register temp,
4172 Register card,
4173 Register object,
4174 Register value,
4175 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004176 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004177 if (can_be_null) {
4178 __ CompareAndBranchIfZero(value, &is_null);
4179 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4181 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4182 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004183 if (can_be_null) {
4184 __ Bind(&is_null);
4185 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004186}
4187
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004188void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4189 temp->SetLocations(nullptr);
4190}
4191
4192void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
4193 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004194 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004195}
4196
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004197void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004198 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004199 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004200}
4201
4202void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004203 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4204}
4205
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004206void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4207 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4208}
4209
4210void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004211 HBasicBlock* block = instruction->GetBlock();
4212 if (block->GetLoopInformation() != nullptr) {
4213 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4214 // The back edge will generate the suspend check.
4215 return;
4216 }
4217 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4218 // The goto will generate the suspend check.
4219 return;
4220 }
4221 GenerateSuspendCheck(instruction, nullptr);
4222}
4223
4224void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4225 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004226 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004227 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4228 if (slow_path == nullptr) {
4229 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4230 instruction->SetSlowPath(slow_path);
4231 codegen_->AddSlowPath(slow_path);
4232 if (successor != nullptr) {
4233 DCHECK(successor->IsLoopHeader());
4234 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4235 }
4236 } else {
4237 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4238 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004239
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004240 __ LoadFromOffset(
4241 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004242 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004243 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004244 __ Bind(slow_path->GetReturnLabel());
4245 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004246 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004247 __ b(slow_path->GetEntryLabel());
4248 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004249}
4250
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004251ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4252 return codegen_->GetAssembler();
4253}
4254
4255void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004256 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004257 Location source = move->GetSource();
4258 Location destination = move->GetDestination();
4259
4260 if (source.IsRegister()) {
4261 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004262 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004263 } else {
4264 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004265 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004266 SP, destination.GetStackIndex());
4267 }
4268 } else if (source.IsStackSlot()) {
4269 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004270 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004271 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004272 } else if (destination.IsFpuRegister()) {
4273 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004274 } else {
4275 DCHECK(destination.IsStackSlot());
4276 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4277 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4278 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004279 } else if (source.IsFpuRegister()) {
4280 if (destination.IsFpuRegister()) {
4281 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004282 } else {
4283 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004284 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4285 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004286 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004287 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004288 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4289 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004290 } else if (destination.IsRegisterPair()) {
4291 DCHECK(ExpectedPairLayout(destination));
4292 __ LoadFromOffset(
4293 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4294 } else {
4295 DCHECK(destination.IsFpuRegisterPair()) << destination;
4296 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4297 SP,
4298 source.GetStackIndex());
4299 }
4300 } else if (source.IsRegisterPair()) {
4301 if (destination.IsRegisterPair()) {
4302 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4303 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4304 } else {
4305 DCHECK(destination.IsDoubleStackSlot()) << destination;
4306 DCHECK(ExpectedPairLayout(source));
4307 __ StoreToOffset(
4308 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4309 }
4310 } else if (source.IsFpuRegisterPair()) {
4311 if (destination.IsFpuRegisterPair()) {
4312 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4313 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4314 } else {
4315 DCHECK(destination.IsDoubleStackSlot()) << destination;
4316 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4317 SP,
4318 destination.GetStackIndex());
4319 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004320 } else {
4321 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004322 HConstant* constant = source.GetConstant();
4323 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4324 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004325 if (destination.IsRegister()) {
4326 __ LoadImmediate(destination.AsRegister<Register>(), value);
4327 } else {
4328 DCHECK(destination.IsStackSlot());
4329 __ LoadImmediate(IP, value);
4330 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4331 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004332 } else if (constant->IsLongConstant()) {
4333 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004334 if (destination.IsRegisterPair()) {
4335 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4336 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004337 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004338 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004339 __ LoadImmediate(IP, Low32Bits(value));
4340 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4341 __ LoadImmediate(IP, High32Bits(value));
4342 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4343 }
4344 } else if (constant->IsDoubleConstant()) {
4345 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004346 if (destination.IsFpuRegisterPair()) {
4347 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004348 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004349 DCHECK(destination.IsDoubleStackSlot()) << destination;
4350 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004351 __ LoadImmediate(IP, Low32Bits(int_value));
4352 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4353 __ LoadImmediate(IP, High32Bits(int_value));
4354 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4355 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004356 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004357 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004358 float value = constant->AsFloatConstant()->GetValue();
4359 if (destination.IsFpuRegister()) {
4360 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4361 } else {
4362 DCHECK(destination.IsStackSlot());
4363 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4364 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4365 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004366 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004367 }
4368}
4369
4370void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4371 __ Mov(IP, reg);
4372 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4373 __ StoreToOffset(kStoreWord, IP, SP, mem);
4374}
4375
4376void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4377 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4378 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4379 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4380 SP, mem1 + stack_offset);
4381 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4382 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4383 SP, mem2 + stack_offset);
4384 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4385}
4386
4387void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004388 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004389 Location source = move->GetSource();
4390 Location destination = move->GetDestination();
4391
4392 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004393 DCHECK_NE(source.AsRegister<Register>(), IP);
4394 DCHECK_NE(destination.AsRegister<Register>(), IP);
4395 __ Mov(IP, source.AsRegister<Register>());
4396 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4397 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004398 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004399 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004400 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004401 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004402 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4403 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004404 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004405 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004406 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004407 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004408 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004409 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004410 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004411 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004412 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4413 destination.AsRegisterPairHigh<Register>(),
4414 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004415 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004416 Register low_reg = source.IsRegisterPair()
4417 ? source.AsRegisterPairLow<Register>()
4418 : destination.AsRegisterPairLow<Register>();
4419 int mem = source.IsRegisterPair()
4420 ? destination.GetStackIndex()
4421 : source.GetStackIndex();
4422 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004423 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004424 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004425 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004426 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004427 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4428 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004429 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004430 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004431 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004432 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4433 DRegister reg = source.IsFpuRegisterPair()
4434 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4435 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4436 int mem = source.IsFpuRegisterPair()
4437 ? destination.GetStackIndex()
4438 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004439 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004440 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004441 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004442 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4443 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4444 : destination.AsFpuRegister<SRegister>();
4445 int mem = source.IsFpuRegister()
4446 ? destination.GetStackIndex()
4447 : source.GetStackIndex();
4448
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004449 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004450 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004451 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004452 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004453 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4454 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004455 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004456 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004457 }
4458}
4459
4460void ParallelMoveResolverARM::SpillScratch(int reg) {
4461 __ Push(static_cast<Register>(reg));
4462}
4463
4464void ParallelMoveResolverARM::RestoreScratch(int reg) {
4465 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004466}
4467
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004468void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004469 InvokeRuntimeCallingConvention calling_convention;
4470 CodeGenerator::CreateLoadClassLocationSummary(
4471 cls,
4472 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4473 Location::RegisterLocation(R0));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004474}
4475
4476void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004477 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004478 if (cls->NeedsAccessCheck()) {
4479 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4480 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4481 cls,
4482 cls->GetDexPc(),
4483 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004484 return;
4485 }
4486
4487 Register out = locations->Out().AsRegister<Register>();
4488 Register current_method = locations->InAt(0).AsRegister<Register>();
4489 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004490 DCHECK(!cls->CanCallRuntime());
4491 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004492 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004493 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004494 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004495 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004496 __ LoadFromOffset(kLoadWord,
4497 out,
4498 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004499 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004500 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004501 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004502
Andreas Gampe85b62f22015-09-09 13:15:38 -07004503 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004504 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4505 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004506 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004507 if (cls->MustGenerateClinitCheck()) {
4508 GenerateClassInitializationCheck(slow_path, out);
4509 } else {
4510 __ Bind(slow_path->GetExitLabel());
4511 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004512 }
4513}
4514
4515void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4516 LocationSummary* locations =
4517 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4518 locations->SetInAt(0, Location::RequiresRegister());
4519 if (check->HasUses()) {
4520 locations->SetOut(Location::SameAsFirstInput());
4521 }
4522}
4523
4524void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004525 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004526 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004527 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004528 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004529 GenerateClassInitializationCheck(slow_path,
4530 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004531}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004532
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004533void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004534 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004535 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4536 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4537 __ b(slow_path->GetEntryLabel(), LT);
4538 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4539 // properly. Therefore, we do a memory fence.
4540 __ dmb(ISH);
4541 __ Bind(slow_path->GetExitLabel());
4542}
4543
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004544void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4545 LocationSummary* locations =
4546 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004547 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004548 locations->SetOut(Location::RequiresRegister());
4549}
4550
4551void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004552 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004553 codegen_->AddSlowPath(slow_path);
4554
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004555 LocationSummary* locations = load->GetLocations();
4556 Register out = locations->Out().AsRegister<Register>();
4557 Register current_method = locations->InAt(0).AsRegister<Register>();
4558 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004559 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004560 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004561 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004562 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004563 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004564 __ Bind(slow_path->GetExitLabel());
4565}
4566
David Brazdilcb1c0552015-08-04 16:22:25 +01004567static int32_t GetExceptionTlsOffset() {
4568 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4569}
4570
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004571void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4572 LocationSummary* locations =
4573 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4574 locations->SetOut(Location::RequiresRegister());
4575}
4576
4577void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004578 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004579 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4580}
4581
4582void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4583 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4584}
4585
4586void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004587 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004588 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004589}
4590
4591void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4592 LocationSummary* locations =
4593 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4594 InvokeRuntimeCallingConvention calling_convention;
4595 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4596}
4597
4598void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4599 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004600 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004601}
4602
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004603void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004604 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4605 switch (instruction->GetTypeCheckKind()) {
4606 case TypeCheckKind::kExactCheck:
4607 case TypeCheckKind::kAbstractClassCheck:
4608 case TypeCheckKind::kClassHierarchyCheck:
4609 case TypeCheckKind::kArrayObjectCheck:
4610 call_kind = LocationSummary::kNoCall;
4611 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004612 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004613 case TypeCheckKind::kInterfaceCheck:
4614 call_kind = LocationSummary::kCall;
4615 break;
4616 case TypeCheckKind::kArrayCheck:
4617 call_kind = LocationSummary::kCallOnSlowPath;
4618 break;
4619 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004621 if (call_kind != LocationSummary::kCall) {
4622 locations->SetInAt(0, Location::RequiresRegister());
4623 locations->SetInAt(1, Location::RequiresRegister());
4624 // The out register is used as a temporary, so it overlaps with the inputs.
4625 // Note that TypeCheckSlowPathARM uses this register too.
4626 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4627 } else {
4628 InvokeRuntimeCallingConvention calling_convention;
4629 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4630 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4631 locations->SetOut(Location::RegisterLocation(R0));
4632 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004633}
4634
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004635void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004636 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004637 Register obj = locations->InAt(0).AsRegister<Register>();
4638 Register cls = locations->InAt(1).AsRegister<Register>();
4639 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004640 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004641 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4642 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4643 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004644 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004645 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004646
4647 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004648 // avoid null check if we know obj is not null.
4649 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004650 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004651 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004652
Calin Juravle98893e12015-10-02 21:05:03 +01004653 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004654 // This is safe, as the register is caller-save, and the object must be in another
4655 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004656 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4657 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004658 ? obj
4659 : out;
4660 __ LoadFromOffset(kLoadWord, target, obj, class_offset);
4661 __ MaybeUnpoisonHeapReference(target);
4662
4663 switch (instruction->GetTypeCheckKind()) {
4664 case TypeCheckKind::kExactCheck: {
4665 __ cmp(out, ShifterOperand(cls));
4666 // Classes must be equal for the instanceof to succeed.
4667 __ b(&zero, NE);
4668 __ LoadImmediate(out, 1);
4669 __ b(&done);
4670 break;
4671 }
4672 case TypeCheckKind::kAbstractClassCheck: {
4673 // If the class is abstract, we eagerly fetch the super class of the
4674 // object to avoid doing a comparison we know will fail.
4675 Label loop;
4676 __ Bind(&loop);
4677 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4678 __ MaybeUnpoisonHeapReference(out);
4679 // If `out` is null, we use it for the result, and jump to `done`.
4680 __ CompareAndBranchIfZero(out, &done);
4681 __ cmp(out, ShifterOperand(cls));
4682 __ b(&loop, NE);
4683 __ LoadImmediate(out, 1);
4684 if (zero.IsLinked()) {
4685 __ b(&done);
4686 }
4687 break;
4688 }
4689 case TypeCheckKind::kClassHierarchyCheck: {
4690 // Walk over the class hierarchy to find a match.
4691 Label loop, success;
4692 __ Bind(&loop);
4693 __ cmp(out, ShifterOperand(cls));
4694 __ b(&success, EQ);
4695 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4696 __ MaybeUnpoisonHeapReference(out);
4697 __ CompareAndBranchIfNonZero(out, &loop);
4698 // If `out` is null, we use it for the result, and jump to `done`.
4699 __ b(&done);
4700 __ Bind(&success);
4701 __ LoadImmediate(out, 1);
4702 if (zero.IsLinked()) {
4703 __ b(&done);
4704 }
4705 break;
4706 }
4707 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004708 // Do an exact check.
4709 Label exact_check;
4710 __ cmp(out, ShifterOperand(cls));
4711 __ b(&exact_check, EQ);
4712 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004713 __ LoadFromOffset(kLoadWord, out, out, component_offset);
4714 __ MaybeUnpoisonHeapReference(out);
4715 // If `out` is null, we use it for the result, and jump to `done`.
4716 __ CompareAndBranchIfZero(out, &done);
4717 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4718 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4719 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004720 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004721 __ LoadImmediate(out, 1);
4722 __ b(&done);
4723 break;
4724 }
4725 case TypeCheckKind::kArrayCheck: {
4726 __ cmp(out, ShifterOperand(cls));
4727 DCHECK(locations->OnlyCallsOnSlowPath());
4728 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4729 instruction, /* is_fatal */ false);
4730 codegen_->AddSlowPath(slow_path);
4731 __ b(slow_path->GetEntryLabel(), NE);
4732 __ LoadImmediate(out, 1);
4733 if (zero.IsLinked()) {
4734 __ b(&done);
4735 }
4736 break;
4737 }
Calin Juravle98893e12015-10-02 21:05:03 +01004738 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004739 case TypeCheckKind::kInterfaceCheck:
4740 default: {
4741 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4742 instruction,
4743 instruction->GetDexPc(),
4744 nullptr);
4745 if (zero.IsLinked()) {
4746 __ b(&done);
4747 }
4748 break;
4749 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004750 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004751
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004752 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004753 __ Bind(&zero);
4754 __ LoadImmediate(out, 0);
4755 }
4756
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004757 if (done.IsLinked()) {
4758 __ Bind(&done);
4759 }
4760
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004761 if (slow_path != nullptr) {
4762 __ Bind(slow_path->GetExitLabel());
4763 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004764}
4765
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004766void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004767 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4768 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4769
4770 switch (instruction->GetTypeCheckKind()) {
4771 case TypeCheckKind::kExactCheck:
4772 case TypeCheckKind::kAbstractClassCheck:
4773 case TypeCheckKind::kClassHierarchyCheck:
4774 case TypeCheckKind::kArrayObjectCheck:
4775 call_kind = throws_into_catch
4776 ? LocationSummary::kCallOnSlowPath
4777 : LocationSummary::kNoCall;
4778 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004779 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004780 case TypeCheckKind::kInterfaceCheck:
4781 call_kind = LocationSummary::kCall;
4782 break;
4783 case TypeCheckKind::kArrayCheck:
4784 call_kind = LocationSummary::kCallOnSlowPath;
4785 break;
4786 }
4787
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004788 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004789 instruction, call_kind);
4790 if (call_kind != LocationSummary::kCall) {
4791 locations->SetInAt(0, Location::RequiresRegister());
4792 locations->SetInAt(1, Location::RequiresRegister());
4793 // Note that TypeCheckSlowPathARM uses this register too.
4794 locations->AddTemp(Location::RequiresRegister());
4795 } else {
4796 InvokeRuntimeCallingConvention calling_convention;
4797 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4798 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4799 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004800}
4801
4802void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4803 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004804 Register obj = locations->InAt(0).AsRegister<Register>();
4805 Register cls = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004806 Register temp = locations->WillCall()
4807 ? Register(kNoRegister)
4808 : locations->GetTemp(0).AsRegister<Register>();
4809
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004810 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004811 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4812 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4813 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4814 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004815
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004816 if (!locations->WillCall()) {
4817 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4818 instruction, !locations->CanCall());
4819 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004820 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004821
4822 Label done;
4823 // Avoid null check if we know obj is not null.
4824 if (instruction->MustDoNullCheck()) {
4825 __ CompareAndBranchIfZero(obj, &done);
4826 }
4827
4828 if (locations->WillCall()) {
4829 __ LoadFromOffset(kLoadWord, obj, obj, class_offset);
4830 __ MaybeUnpoisonHeapReference(obj);
4831 } else {
4832 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4833 __ MaybeUnpoisonHeapReference(temp);
4834 }
4835
4836 switch (instruction->GetTypeCheckKind()) {
4837 case TypeCheckKind::kExactCheck:
4838 case TypeCheckKind::kArrayCheck: {
4839 __ cmp(temp, ShifterOperand(cls));
4840 // Jump to slow path for throwing the exception or doing a
4841 // more involved array check.
4842 __ b(slow_path->GetEntryLabel(), NE);
4843 break;
4844 }
4845 case TypeCheckKind::kAbstractClassCheck: {
4846 // If the class is abstract, we eagerly fetch the super class of the
4847 // object to avoid doing a comparison we know will fail.
4848 Label loop;
4849 __ Bind(&loop);
4850 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4851 __ MaybeUnpoisonHeapReference(temp);
4852 // Jump to the slow path to throw the exception.
4853 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4854 __ cmp(temp, ShifterOperand(cls));
4855 __ b(&loop, NE);
4856 break;
4857 }
4858 case TypeCheckKind::kClassHierarchyCheck: {
4859 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004860 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004861 __ Bind(&loop);
4862 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004863 __ b(&done, EQ);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004864 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4865 __ MaybeUnpoisonHeapReference(temp);
4866 __ CompareAndBranchIfNonZero(temp, &loop);
4867 // Jump to the slow path to throw the exception.
4868 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004869 break;
4870 }
4871 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004872 // Do an exact check.
4873 __ cmp(temp, ShifterOperand(cls));
4874 __ b(&done, EQ);
4875 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004876 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
4877 __ MaybeUnpoisonHeapReference(temp);
4878 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4879 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
4880 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4881 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
4882 break;
4883 }
Calin Juravle98893e12015-10-02 21:05:03 +01004884 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004885 case TypeCheckKind::kInterfaceCheck:
4886 default:
4887 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
4888 instruction,
4889 instruction->GetDexPc(),
4890 nullptr);
4891 break;
4892 }
4893 __ Bind(&done);
4894
4895 if (slow_path != nullptr) {
4896 __ Bind(slow_path->GetExitLabel());
4897 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004898}
4899
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004900void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4901 LocationSummary* locations =
4902 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4903 InvokeRuntimeCallingConvention calling_convention;
4904 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4905}
4906
4907void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4908 codegen_->InvokeRuntime(instruction->IsEnter()
4909 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4910 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004911 instruction->GetDexPc(),
4912 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004913}
4914
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004915void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4916void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4917void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4918
4919void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4920 LocationSummary* locations =
4921 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4922 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4923 || instruction->GetResultType() == Primitive::kPrimLong);
4924 locations->SetInAt(0, Location::RequiresRegister());
4925 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004927}
4928
4929void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4930 HandleBitwiseOperation(instruction);
4931}
4932
4933void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4934 HandleBitwiseOperation(instruction);
4935}
4936
4937void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4938 HandleBitwiseOperation(instruction);
4939}
4940
4941void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4942 LocationSummary* locations = instruction->GetLocations();
4943
4944 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004945 Register first = locations->InAt(0).AsRegister<Register>();
4946 Register second = locations->InAt(1).AsRegister<Register>();
4947 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004948 if (instruction->IsAnd()) {
4949 __ and_(out, first, ShifterOperand(second));
4950 } else if (instruction->IsOr()) {
4951 __ orr(out, first, ShifterOperand(second));
4952 } else {
4953 DCHECK(instruction->IsXor());
4954 __ eor(out, first, ShifterOperand(second));
4955 }
4956 } else {
4957 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4958 Location first = locations->InAt(0);
4959 Location second = locations->InAt(1);
4960 Location out = locations->Out();
4961 if (instruction->IsAnd()) {
4962 __ and_(out.AsRegisterPairLow<Register>(),
4963 first.AsRegisterPairLow<Register>(),
4964 ShifterOperand(second.AsRegisterPairLow<Register>()));
4965 __ and_(out.AsRegisterPairHigh<Register>(),
4966 first.AsRegisterPairHigh<Register>(),
4967 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4968 } else if (instruction->IsOr()) {
4969 __ orr(out.AsRegisterPairLow<Register>(),
4970 first.AsRegisterPairLow<Register>(),
4971 ShifterOperand(second.AsRegisterPairLow<Register>()));
4972 __ orr(out.AsRegisterPairHigh<Register>(),
4973 first.AsRegisterPairHigh<Register>(),
4974 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4975 } else {
4976 DCHECK(instruction->IsXor());
4977 __ eor(out.AsRegisterPairLow<Register>(),
4978 first.AsRegisterPairLow<Register>(),
4979 ShifterOperand(second.AsRegisterPairLow<Register>()));
4980 __ eor(out.AsRegisterPairHigh<Register>(),
4981 first.AsRegisterPairHigh<Register>(),
4982 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4983 }
4984 }
4985}
4986
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004987void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004988 // For better instruction scheduling we load the direct code pointer before the method pointer.
4989 bool direct_code_loaded = false;
4990 switch (invoke->GetCodePtrLocation()) {
4991 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4992 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
4993 break;
4994 }
4995 // Calls across dex files are more likely to exceed the available BL range,
4996 // so use absolute patch by falling through to kDirectCodeFixup.
4997 FALLTHROUGH_INTENDED;
4998 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4999 // LR = code address from literal pool with link-time patch.
5000 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5001 direct_code_loaded = true;
5002 break;
5003 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5004 // LR = invoke->GetDirectCodePtr();
5005 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
5006 direct_code_loaded = true;
5007 break;
5008 default:
5009 break;
5010 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005011
Vladimir Marko58155012015-08-19 12:49:41 +00005012 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5013 switch (invoke->GetMethodLoadKind()) {
5014 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5015 // temp = thread->string_init_entrypoint
5016 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5017 break;
5018 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
5019 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5020 break;
5021 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5022 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5023 break;
5024 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5025 __ LoadLiteral(temp.AsRegister<Register>(),
5026 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5027 break;
5028 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5029 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5030 FALLTHROUGH_INTENDED;
5031 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5032 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5033 Register method_reg;
5034 Register reg = temp.AsRegister<Register>();
5035 if (current_method.IsRegister()) {
5036 method_reg = current_method.AsRegister<Register>();
5037 } else {
5038 DCHECK(invoke->GetLocations()->Intrinsified());
5039 DCHECK(!current_method.IsValid());
5040 method_reg = reg;
5041 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5042 }
5043 // temp = current_method->dex_cache_resolved_methods_;
5044 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01005045 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
5046 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005047 // temp = temp[index_in_cache]
5048 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5049 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5050 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005051 }
Vladimir Marko58155012015-08-19 12:49:41 +00005052 }
5053
5054 switch (invoke->GetCodePtrLocation()) {
5055 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5056 __ bl(GetFrameEntryLabel());
5057 break;
5058 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5059 if (!direct_code_loaded) {
5060 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
5061 __ Bind(&relative_call_patches_.back().label);
5062 Label label;
5063 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
5064 __ Bind(&label);
5065 break;
5066 }
5067 // If we loaded the direct code above, fall through.
5068 FALLTHROUGH_INTENDED;
5069 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5070 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5071 // LR prepared above for better instruction scheduling.
5072 DCHECK(direct_code_loaded);
5073 // LR()
5074 __ blx(LR);
5075 break;
5076 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5077 // LR = callee_method->entry_point_from_quick_compiled_code_
5078 __ LoadFromOffset(
5079 kLoadWord, LR, callee_method.AsRegister<Register>(),
5080 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5081 // LR()
5082 __ blx(LR);
5083 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005084 }
5085
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005086 DCHECK(!IsLeafMethod());
5087}
5088
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005089void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5090 Register temp = temp_location.AsRegister<Register>();
5091 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5092 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5093 LocationSummary* locations = invoke->GetLocations();
5094 Location receiver = locations->InAt(0);
5095 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5096 // temp = object->GetClass();
5097 DCHECK(receiver.IsRegister());
5098 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5099 MaybeRecordImplicitNullCheck(invoke);
5100 __ MaybeUnpoisonHeapReference(temp);
5101 // temp = temp->GetMethodAt(method_offset);
5102 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5103 kArmWordSize).Int32Value();
5104 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5105 // LR = temp->GetEntryPoint();
5106 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5107 // LR();
5108 __ blx(LR);
5109}
5110
Vladimir Marko58155012015-08-19 12:49:41 +00005111void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5112 DCHECK(linker_patches->empty());
5113 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5114 linker_patches->reserve(size);
5115 for (const auto& entry : method_patches_) {
5116 const MethodReference& target_method = entry.first;
5117 Literal* literal = entry.second;
5118 DCHECK(literal->GetLabel()->IsBound());
5119 uint32_t literal_offset = literal->GetLabel()->Position();
5120 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5121 target_method.dex_file,
5122 target_method.dex_method_index));
5123 }
5124 for (const auto& entry : call_patches_) {
5125 const MethodReference& target_method = entry.first;
5126 Literal* literal = entry.second;
5127 DCHECK(literal->GetLabel()->IsBound());
5128 uint32_t literal_offset = literal->GetLabel()->Position();
5129 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5130 target_method.dex_file,
5131 target_method.dex_method_index));
5132 }
5133 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5134 uint32_t literal_offset = info.label.Position();
5135 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5136 info.target_method.dex_file,
5137 info.target_method.dex_method_index));
5138 }
5139}
5140
5141Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5142 MethodToLiteralMap* map) {
5143 // Look up the literal for target_method.
5144 auto lb = map->lower_bound(target_method);
5145 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5146 return lb->second;
5147 }
5148 // We don't have a literal for this method yet, insert a new one.
5149 Literal* literal = __ NewLiteral<uint32_t>(0u);
5150 map->PutBefore(lb, target_method, literal);
5151 return literal;
5152}
5153
5154Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5155 return DeduplicateMethodLiteral(target_method, &method_patches_);
5156}
5157
5158Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5159 return DeduplicateMethodLiteral(target_method, &call_patches_);
5160}
5161
Calin Juravleb1498f62015-02-16 13:13:29 +00005162void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
5163 // Nothing to do, this should be removed during prepare for register allocator.
5164 UNUSED(instruction);
5165 LOG(FATAL) << "Unreachable";
5166}
5167
5168void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
5169 // Nothing to do, this should be removed during prepare for register allocator.
5170 UNUSED(instruction);
5171 LOG(FATAL) << "Unreachable";
5172}
5173
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005174void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5175 DCHECK(codegen_->IsBaseline());
5176 LocationSummary* locations =
5177 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5178 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5179}
5180
5181void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5182 DCHECK(codegen_->IsBaseline());
5183 // Will be generated at use site.
5184}
5185
Mark Mendellfe57faa2015-09-18 09:26:15 -04005186// Simple implementation of packed switch - generate cascaded compare/jumps.
5187void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5188 LocationSummary* locations =
5189 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5190 locations->SetInAt(0, Location::RequiresRegister());
5191}
5192
5193void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5194 int32_t lower_bound = switch_instr->GetStartValue();
5195 int32_t num_entries = switch_instr->GetNumEntries();
5196 LocationSummary* locations = switch_instr->GetLocations();
5197 Register value_reg = locations->InAt(0).AsRegister<Register>();
5198 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5199
5200 // Create a series of compare/jumps.
5201 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5202 for (int32_t i = 0; i < num_entries; i++) {
5203 GenerateCompareWithImmediate(value_reg, lower_bound + i);
Vladimir Markoec7802a2015-10-01 20:57:57 +01005204 __ b(codegen_->GetLabelOf(successors[i]), EQ);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005205 }
5206
5207 // And the default for any other value.
5208 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5209 __ b(codegen_->GetLabelOf(default_block));
5210 }
5211}
5212
Andreas Gampe85b62f22015-09-09 13:15:38 -07005213void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5214 if (!trg.IsValid()) {
5215 DCHECK(type == Primitive::kPrimVoid);
5216 return;
5217 }
5218
5219 DCHECK_NE(type, Primitive::kPrimVoid);
5220
5221 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5222 if (return_loc.Equals(trg)) {
5223 return;
5224 }
5225
5226 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5227 // with the last branch.
5228 if (type == Primitive::kPrimLong) {
5229 HParallelMove parallel_move(GetGraph()->GetArena());
5230 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
5231 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
5232 GetMoveResolver()->EmitNativeCode(&parallel_move);
5233 } else if (type == Primitive::kPrimDouble) {
5234 HParallelMove parallel_move(GetGraph()->GetArena());
5235 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
5236 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
5237 GetMoveResolver()->EmitNativeCode(&parallel_move);
5238 } else {
5239 // Let the parallel move resolver take care of all of this.
5240 HParallelMove parallel_move(GetGraph()->GetArena());
5241 parallel_move.AddMove(return_loc, trg, type, nullptr);
5242 GetMoveResolver()->EmitNativeCode(&parallel_move);
5243 }
5244}
5245
Roland Levillain4d027112015-07-01 15:41:14 +01005246#undef __
5247#undef QUICK_ENTRY_POINT
5248
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005249} // namespace arm
5250} // namespace art