blob: dd538824bd2fa4985b6900596af5e8e582f9ad21 [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
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003515Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3516 Opcode opcode) {
3517 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3518 if (constant->IsConstant() &&
3519 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3520 return Location::ConstantLocation(constant->AsConstant());
3521 }
3522 return Location::RequiresRegister();
3523}
3524
3525bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3526 Opcode opcode) {
3527 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3528 if (Primitive::Is64BitType(input_cst->GetType())) {
3529 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3530 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3531 } else {
3532 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3533 }
3534}
3535
3536bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3537 ShifterOperand so;
3538 ArmAssembler* assembler = codegen_->GetAssembler();
3539 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3540 return true;
3541 }
3542 Opcode neg_opcode = kNoOperand;
3543 switch (opcode) {
3544 case AND:
3545 neg_opcode = BIC;
3546 break;
3547 case ORR:
3548 neg_opcode = ORN;
3549 break;
3550 default:
3551 return false;
3552 }
3553 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
3554}
3555
Calin Juravle52c48962014-12-16 17:02:57 +00003556void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3557 const FieldInfo& field_info) {
3558 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003559
Calin Juravle52c48962014-12-16 17:02:57 +00003560 LocationSummary* locations = instruction->GetLocations();
3561 Register base = locations->InAt(0).AsRegister<Register>();
3562 Location out = locations->Out();
3563 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003564 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003565 Primitive::Type field_type = field_info.GetFieldType();
3566 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3567
3568 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003569 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003570 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003571 break;
3572 }
3573
3574 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003575 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003576 break;
3577 }
3578
3579 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003580 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003581 break;
3582 }
3583
3584 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003585 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003586 break;
3587 }
3588
3589 case Primitive::kPrimInt:
3590 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003591 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003592 break;
3593 }
3594
3595 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003596 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003597 GenerateWideAtomicLoad(base, offset,
3598 out.AsRegisterPairLow<Register>(),
3599 out.AsRegisterPairHigh<Register>());
3600 } else {
3601 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3602 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003603 break;
3604 }
3605
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003606 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003607 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003608 break;
3609 }
3610
3611 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003612 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003613 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003614 Register lo = locations->GetTemp(0).AsRegister<Register>();
3615 Register hi = locations->GetTemp(1).AsRegister<Register>();
3616 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003617 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003618 __ vmovdrr(out_reg, lo, hi);
3619 } else {
3620 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003621 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003622 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003623 break;
3624 }
3625
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003626 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003627 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003628 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003629 }
Calin Juravle52c48962014-12-16 17:02:57 +00003630
Calin Juravle77520bc2015-01-12 18:45:46 +00003631 // Doubles are handled in the switch.
3632 if (field_type != Primitive::kPrimDouble) {
3633 codegen_->MaybeRecordImplicitNullCheck(instruction);
3634 }
3635
Calin Juravle52c48962014-12-16 17:02:57 +00003636 if (is_volatile) {
3637 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3638 }
Roland Levillain4d027112015-07-01 15:41:14 +01003639
3640 if (field_type == Primitive::kPrimNot) {
3641 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3642 }
Calin Juravle52c48962014-12-16 17:02:57 +00003643}
3644
3645void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3646 HandleFieldSet(instruction, instruction->GetFieldInfo());
3647}
3648
3649void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003650 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003651}
3652
3653void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3654 HandleFieldGet(instruction, instruction->GetFieldInfo());
3655}
3656
3657void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3658 HandleFieldGet(instruction, instruction->GetFieldInfo());
3659}
3660
3661void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3662 HandleFieldGet(instruction, instruction->GetFieldInfo());
3663}
3664
3665void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3666 HandleFieldGet(instruction, instruction->GetFieldInfo());
3667}
3668
3669void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3670 HandleFieldSet(instruction, instruction->GetFieldInfo());
3671}
3672
3673void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003674 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003675}
3676
Calin Juravlee460d1d2015-09-29 04:52:17 +01003677void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
3678 HUnresolvedInstanceFieldGet* instruction) {
3679 FieldAccessCallingConventionARM calling_convention;
3680 codegen_->CreateUnresolvedFieldLocationSummary(
3681 instruction, instruction->GetFieldType(), calling_convention);
3682}
3683
3684void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
3685 HUnresolvedInstanceFieldGet* instruction) {
3686 FieldAccessCallingConventionARM calling_convention;
3687 codegen_->GenerateUnresolvedFieldAccess(instruction,
3688 instruction->GetFieldType(),
3689 instruction->GetFieldIndex(),
3690 instruction->GetDexPc(),
3691 calling_convention);
3692}
3693
3694void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
3695 HUnresolvedInstanceFieldSet* instruction) {
3696 FieldAccessCallingConventionARM calling_convention;
3697 codegen_->CreateUnresolvedFieldLocationSummary(
3698 instruction, instruction->GetFieldType(), calling_convention);
3699}
3700
3701void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
3702 HUnresolvedInstanceFieldSet* instruction) {
3703 FieldAccessCallingConventionARM calling_convention;
3704 codegen_->GenerateUnresolvedFieldAccess(instruction,
3705 instruction->GetFieldType(),
3706 instruction->GetFieldIndex(),
3707 instruction->GetDexPc(),
3708 calling_convention);
3709}
3710
3711void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
3712 HUnresolvedStaticFieldGet* instruction) {
3713 FieldAccessCallingConventionARM calling_convention;
3714 codegen_->CreateUnresolvedFieldLocationSummary(
3715 instruction, instruction->GetFieldType(), calling_convention);
3716}
3717
3718void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
3719 HUnresolvedStaticFieldGet* instruction) {
3720 FieldAccessCallingConventionARM calling_convention;
3721 codegen_->GenerateUnresolvedFieldAccess(instruction,
3722 instruction->GetFieldType(),
3723 instruction->GetFieldIndex(),
3724 instruction->GetDexPc(),
3725 calling_convention);
3726}
3727
3728void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
3729 HUnresolvedStaticFieldSet* instruction) {
3730 FieldAccessCallingConventionARM calling_convention;
3731 codegen_->CreateUnresolvedFieldLocationSummary(
3732 instruction, instruction->GetFieldType(), calling_convention);
3733}
3734
3735void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
3736 HUnresolvedStaticFieldSet* instruction) {
3737 FieldAccessCallingConventionARM calling_convention;
3738 codegen_->GenerateUnresolvedFieldAccess(instruction,
3739 instruction->GetFieldType(),
3740 instruction->GetFieldIndex(),
3741 instruction->GetDexPc(),
3742 calling_convention);
3743}
3744
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003745void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003746 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3747 ? LocationSummary::kCallOnSlowPath
3748 : LocationSummary::kNoCall;
3749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003750 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003751 if (instruction->HasUses()) {
3752 locations->SetOut(Location::SameAsFirstInput());
3753 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003754}
3755
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003756void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003757 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3758 return;
3759 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003760 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003761
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003762 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3763 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3764}
3765
3766void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003767 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003768 codegen_->AddSlowPath(slow_path);
3769
3770 LocationSummary* locations = instruction->GetLocations();
3771 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003772
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003773 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003774}
3775
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003776void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003777 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003778 GenerateImplicitNullCheck(instruction);
3779 } else {
3780 GenerateExplicitNullCheck(instruction);
3781 }
3782}
3783
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003784void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003785 LocationSummary* locations =
3786 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003787 locations->SetInAt(0, Location::RequiresRegister());
3788 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003789 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3790 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3791 } else {
3792 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3793 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003794}
3795
3796void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3797 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003798 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003799 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003800 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003801
Roland Levillain4d027112015-07-01 15:41:14 +01003802 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003803 case Primitive::kPrimBoolean: {
3804 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003805 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003807 size_t offset =
3808 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003809 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3810 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3813 }
3814 break;
3815 }
3816
3817 case Primitive::kPrimByte: {
3818 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003819 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003820 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003821 size_t offset =
3822 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003823 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3824 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003825 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3827 }
3828 break;
3829 }
3830
3831 case Primitive::kPrimShort: {
3832 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003833 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003834 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003835 size_t offset =
3836 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003837 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3838 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003839 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003840 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3841 }
3842 break;
3843 }
3844
3845 case Primitive::kPrimChar: {
3846 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003847 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003848 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003849 size_t offset =
3850 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003851 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3852 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003853 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003854 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3855 }
3856 break;
3857 }
3858
3859 case Primitive::kPrimInt:
3860 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003861 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3862 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003866 size_t offset =
3867 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003868 __ LoadFromOffset(kLoadWord, out, obj, offset);
3869 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003870 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003871 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3872 }
3873 break;
3874 }
3875
3876 case Primitive::kPrimLong: {
3877 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003878 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003880 size_t offset =
3881 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003882 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003883 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003884 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003885 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003886 }
3887 break;
3888 }
3889
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003890 case Primitive::kPrimFloat: {
3891 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3892 Location out = locations->Out();
3893 DCHECK(out.IsFpuRegister());
3894 if (index.IsConstant()) {
3895 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3896 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3897 } else {
3898 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3899 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3900 }
3901 break;
3902 }
3903
3904 case Primitive::kPrimDouble: {
3905 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3906 Location out = locations->Out();
3907 DCHECK(out.IsFpuRegisterPair());
3908 if (index.IsConstant()) {
3909 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3910 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3911 } else {
3912 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3913 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3914 }
3915 break;
3916 }
3917
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003918 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003919 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003920 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003921 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003922 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003923
3924 if (type == Primitive::kPrimNot) {
3925 Register out = locations->Out().AsRegister<Register>();
3926 __ MaybeUnpoisonHeapReference(out);
3927 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003928}
3929
3930void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003931 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003932
3933 bool needs_write_barrier =
3934 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003935 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003936
Nicolas Geoffray39468442014-09-02 15:17:15 +01003937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003938 instruction,
3939 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
3940 locations->SetInAt(0, Location::RequiresRegister());
3941 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3942 if (Primitive::IsFloatingPointType(value_type)) {
3943 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003944 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003945 locations->SetInAt(2, Location::RequiresRegister());
3946 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003947
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003948 if (needs_write_barrier) {
3949 // Temporary registers for the write barrier.
3950 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
3951 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003952 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003953}
3954
3955void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3956 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003957 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003958 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003959 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003960 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003961 bool needs_write_barrier =
3962 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003963
3964 switch (value_type) {
3965 case Primitive::kPrimBoolean:
3966 case Primitive::kPrimByte: {
3967 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003968 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003969 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003970 size_t offset =
3971 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003972 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003974 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003975 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3976 }
3977 break;
3978 }
3979
3980 case Primitive::kPrimShort:
3981 case Primitive::kPrimChar: {
3982 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003983 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003984 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003985 size_t offset =
3986 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003987 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003988 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003989 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003990 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3991 }
3992 break;
3993 }
3994
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003995 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003996 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3997 Register value = locations->InAt(2).AsRegister<Register>();
3998 Register source = value;
3999
4000 if (instruction->InputAt(2)->IsNullConstant()) {
4001 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004002 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004003 size_t offset =
4004 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004005 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004006 } else {
4007 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004008 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004009 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004010 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004011 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004012 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004013
4014 DCHECK(needs_write_barrier);
4015 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4016 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4017 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4018 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4019 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4020 Label done;
4021 SlowPathCode* slow_path = nullptr;
4022
4023 if (may_need_runtime_call) {
4024 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4025 codegen_->AddSlowPath(slow_path);
4026 if (instruction->GetValueCanBeNull()) {
4027 Label non_zero;
4028 __ CompareAndBranchIfNonZero(value, &non_zero);
4029 if (index.IsConstant()) {
4030 size_t offset =
4031 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4032 __ StoreToOffset(kStoreWord, value, array, offset);
4033 } else {
4034 DCHECK(index.IsRegister()) << index;
4035 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4036 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4037 }
4038 codegen_->MaybeRecordImplicitNullCheck(instruction);
4039 __ b(&done);
4040 __ Bind(&non_zero);
4041 }
4042
4043 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4044 codegen_->MaybeRecordImplicitNullCheck(instruction);
4045 __ MaybeUnpoisonHeapReference(temp1);
4046 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4047 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4048 // No need to poison/unpoison, we're comparing two poisoined references.
4049 __ cmp(temp1, ShifterOperand(temp2));
4050 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4051 Label do_put;
4052 __ b(&do_put, EQ);
4053 __ MaybeUnpoisonHeapReference(temp1);
4054 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4055 // No need to poison/unpoison, we're comparing against null.
4056 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4057 __ Bind(&do_put);
4058 } else {
4059 __ b(slow_path->GetEntryLabel(), NE);
4060 }
4061 }
4062
4063 if (kPoisonHeapReferences) {
4064 // Note that in the case where `value` is a null reference,
4065 // we do not enter this block, as a null reference does not
4066 // need poisoning.
4067 DCHECK_EQ(value_type, Primitive::kPrimNot);
4068 __ Mov(temp1, value);
4069 __ PoisonHeapReference(temp1);
4070 source = temp1;
4071 }
4072
4073 if (index.IsConstant()) {
4074 size_t offset =
4075 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4076 __ StoreToOffset(kStoreWord, source, array, offset);
4077 } else {
4078 DCHECK(index.IsRegister()) << index;
4079 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4080 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4081 }
4082
4083 if (!may_need_runtime_call) {
4084 codegen_->MaybeRecordImplicitNullCheck(instruction);
4085 }
4086
4087 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4088
4089 if (done.IsLinked()) {
4090 __ Bind(&done);
4091 }
4092
4093 if (slow_path != nullptr) {
4094 __ Bind(slow_path->GetExitLabel());
4095 }
4096
4097 break;
4098 }
4099
4100 case Primitive::kPrimInt: {
4101 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4102 Register value = locations->InAt(2).AsRegister<Register>();
4103 if (index.IsConstant()) {
4104 size_t offset =
4105 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4106 __ StoreToOffset(kStoreWord, value, array, offset);
4107 } else {
4108 DCHECK(index.IsRegister()) << index;
4109 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4110 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4111 }
4112
4113 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004114 break;
4115 }
4116
4117 case Primitive::kPrimLong: {
4118 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004119 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004120 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004121 size_t offset =
4122 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004123 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004124 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004125 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004126 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004127 }
4128 break;
4129 }
4130
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004131 case Primitive::kPrimFloat: {
4132 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4133 Location value = locations->InAt(2);
4134 DCHECK(value.IsFpuRegister());
4135 if (index.IsConstant()) {
4136 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004137 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004138 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004139 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004140 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4141 }
4142 break;
4143 }
4144
4145 case Primitive::kPrimDouble: {
4146 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4147 Location value = locations->InAt(2);
4148 DCHECK(value.IsFpuRegisterPair());
4149 if (index.IsConstant()) {
4150 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004151 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004152 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004153 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004154 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4155 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004156
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004157 break;
4158 }
4159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004160 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004161 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004162 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004163 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004164
4165 // Ints and objects are handled in the switch.
4166 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4167 codegen_->MaybeRecordImplicitNullCheck(instruction);
4168 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004169}
4170
4171void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004172 LocationSummary* locations =
4173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004174 locations->SetInAt(0, Location::RequiresRegister());
4175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004176}
4177
4178void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4179 LocationSummary* locations = instruction->GetLocations();
4180 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004181 Register obj = locations->InAt(0).AsRegister<Register>();
4182 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004183 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004184 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004185}
4186
4187void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004188 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4189 ? LocationSummary::kCallOnSlowPath
4190 : LocationSummary::kNoCall;
4191 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004192 locations->SetInAt(0, Location::RequiresRegister());
4193 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004194 if (instruction->HasUses()) {
4195 locations->SetOut(Location::SameAsFirstInput());
4196 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004197}
4198
4199void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4200 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004201 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004202 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004203 codegen_->AddSlowPath(slow_path);
4204
Roland Levillain271ab9c2014-11-27 15:23:57 +00004205 Register index = locations->InAt(0).AsRegister<Register>();
4206 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004207
4208 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004209 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004210}
4211
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004212void CodeGeneratorARM::MarkGCCard(Register temp,
4213 Register card,
4214 Register object,
4215 Register value,
4216 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004217 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004218 if (can_be_null) {
4219 __ CompareAndBranchIfZero(value, &is_null);
4220 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004221 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4222 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4223 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004224 if (can_be_null) {
4225 __ Bind(&is_null);
4226 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004227}
4228
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004229void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4230 temp->SetLocations(nullptr);
4231}
4232
4233void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
4234 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004235 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236}
4237
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004238void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004239 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004240 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004241}
4242
4243void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004244 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4245}
4246
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004247void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4249}
4250
4251void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004252 HBasicBlock* block = instruction->GetBlock();
4253 if (block->GetLoopInformation() != nullptr) {
4254 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4255 // The back edge will generate the suspend check.
4256 return;
4257 }
4258 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4259 // The goto will generate the suspend check.
4260 return;
4261 }
4262 GenerateSuspendCheck(instruction, nullptr);
4263}
4264
4265void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4266 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004267 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004268 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4269 if (slow_path == nullptr) {
4270 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4271 instruction->SetSlowPath(slow_path);
4272 codegen_->AddSlowPath(slow_path);
4273 if (successor != nullptr) {
4274 DCHECK(successor->IsLoopHeader());
4275 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4276 }
4277 } else {
4278 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4279 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004280
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004281 __ LoadFromOffset(
4282 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004283 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004284 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004285 __ Bind(slow_path->GetReturnLabel());
4286 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004287 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004288 __ b(slow_path->GetEntryLabel());
4289 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004290}
4291
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004292ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4293 return codegen_->GetAssembler();
4294}
4295
4296void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004297 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004298 Location source = move->GetSource();
4299 Location destination = move->GetDestination();
4300
4301 if (source.IsRegister()) {
4302 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004303 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004304 } else {
4305 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004306 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004307 SP, destination.GetStackIndex());
4308 }
4309 } else if (source.IsStackSlot()) {
4310 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004311 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004312 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004313 } else if (destination.IsFpuRegister()) {
4314 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004315 } else {
4316 DCHECK(destination.IsStackSlot());
4317 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4318 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4319 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004320 } else if (source.IsFpuRegister()) {
4321 if (destination.IsFpuRegister()) {
4322 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004323 } else {
4324 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004325 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4326 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004327 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004328 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004329 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4330 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004331 } else if (destination.IsRegisterPair()) {
4332 DCHECK(ExpectedPairLayout(destination));
4333 __ LoadFromOffset(
4334 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4335 } else {
4336 DCHECK(destination.IsFpuRegisterPair()) << destination;
4337 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4338 SP,
4339 source.GetStackIndex());
4340 }
4341 } else if (source.IsRegisterPair()) {
4342 if (destination.IsRegisterPair()) {
4343 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4344 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4345 } else {
4346 DCHECK(destination.IsDoubleStackSlot()) << destination;
4347 DCHECK(ExpectedPairLayout(source));
4348 __ StoreToOffset(
4349 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4350 }
4351 } else if (source.IsFpuRegisterPair()) {
4352 if (destination.IsFpuRegisterPair()) {
4353 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4354 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4355 } else {
4356 DCHECK(destination.IsDoubleStackSlot()) << destination;
4357 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4358 SP,
4359 destination.GetStackIndex());
4360 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004361 } else {
4362 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004363 HConstant* constant = source.GetConstant();
4364 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4365 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004366 if (destination.IsRegister()) {
4367 __ LoadImmediate(destination.AsRegister<Register>(), value);
4368 } else {
4369 DCHECK(destination.IsStackSlot());
4370 __ LoadImmediate(IP, value);
4371 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4372 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004373 } else if (constant->IsLongConstant()) {
4374 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004375 if (destination.IsRegisterPair()) {
4376 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4377 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004378 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004379 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004380 __ LoadImmediate(IP, Low32Bits(value));
4381 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4382 __ LoadImmediate(IP, High32Bits(value));
4383 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4384 }
4385 } else if (constant->IsDoubleConstant()) {
4386 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004387 if (destination.IsFpuRegisterPair()) {
4388 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004389 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004390 DCHECK(destination.IsDoubleStackSlot()) << destination;
4391 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004392 __ LoadImmediate(IP, Low32Bits(int_value));
4393 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4394 __ LoadImmediate(IP, High32Bits(int_value));
4395 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4396 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004397 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004398 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004399 float value = constant->AsFloatConstant()->GetValue();
4400 if (destination.IsFpuRegister()) {
4401 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4402 } else {
4403 DCHECK(destination.IsStackSlot());
4404 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4405 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4406 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004407 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004408 }
4409}
4410
4411void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4412 __ Mov(IP, reg);
4413 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4414 __ StoreToOffset(kStoreWord, IP, SP, mem);
4415}
4416
4417void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4418 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4419 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4420 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4421 SP, mem1 + stack_offset);
4422 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4423 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4424 SP, mem2 + stack_offset);
4425 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4426}
4427
4428void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004429 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004430 Location source = move->GetSource();
4431 Location destination = move->GetDestination();
4432
4433 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004434 DCHECK_NE(source.AsRegister<Register>(), IP);
4435 DCHECK_NE(destination.AsRegister<Register>(), IP);
4436 __ Mov(IP, source.AsRegister<Register>());
4437 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4438 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004439 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004440 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004441 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004442 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004443 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4444 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004445 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004446 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004447 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004448 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004449 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004450 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004451 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004452 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004453 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4454 destination.AsRegisterPairHigh<Register>(),
4455 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004456 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004457 Register low_reg = source.IsRegisterPair()
4458 ? source.AsRegisterPairLow<Register>()
4459 : destination.AsRegisterPairLow<Register>();
4460 int mem = source.IsRegisterPair()
4461 ? destination.GetStackIndex()
4462 : source.GetStackIndex();
4463 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004464 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004465 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004466 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004467 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004468 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4469 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004470 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004471 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004472 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004473 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4474 DRegister reg = source.IsFpuRegisterPair()
4475 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4476 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4477 int mem = source.IsFpuRegisterPair()
4478 ? destination.GetStackIndex()
4479 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004480 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004481 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004482 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004483 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4484 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4485 : destination.AsFpuRegister<SRegister>();
4486 int mem = source.IsFpuRegister()
4487 ? destination.GetStackIndex()
4488 : source.GetStackIndex();
4489
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004490 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004491 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004492 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004493 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004494 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4495 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004496 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004497 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004498 }
4499}
4500
4501void ParallelMoveResolverARM::SpillScratch(int reg) {
4502 __ Push(static_cast<Register>(reg));
4503}
4504
4505void ParallelMoveResolverARM::RestoreScratch(int reg) {
4506 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004507}
4508
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004509void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004510 InvokeRuntimeCallingConvention calling_convention;
4511 CodeGenerator::CreateLoadClassLocationSummary(
4512 cls,
4513 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4514 Location::RegisterLocation(R0));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004515}
4516
4517void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004518 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004519 if (cls->NeedsAccessCheck()) {
4520 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4521 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4522 cls,
4523 cls->GetDexPc(),
4524 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004525 return;
4526 }
4527
4528 Register out = locations->Out().AsRegister<Register>();
4529 Register current_method = locations->InAt(0).AsRegister<Register>();
4530 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004531 DCHECK(!cls->CanCallRuntime());
4532 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004533 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004534 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004535 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004536 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004537 __ LoadFromOffset(kLoadWord,
4538 out,
4539 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004540 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004541 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004542 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004543
Andreas Gampe85b62f22015-09-09 13:15:38 -07004544 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004545 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4546 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004547 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004548 if (cls->MustGenerateClinitCheck()) {
4549 GenerateClassInitializationCheck(slow_path, out);
4550 } else {
4551 __ Bind(slow_path->GetExitLabel());
4552 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004553 }
4554}
4555
4556void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4557 LocationSummary* locations =
4558 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4559 locations->SetInAt(0, Location::RequiresRegister());
4560 if (check->HasUses()) {
4561 locations->SetOut(Location::SameAsFirstInput());
4562 }
4563}
4564
4565void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004566 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004567 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004568 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004569 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004570 GenerateClassInitializationCheck(slow_path,
4571 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004572}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004573
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004574void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004575 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004576 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4577 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4578 __ b(slow_path->GetEntryLabel(), LT);
4579 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4580 // properly. Therefore, we do a memory fence.
4581 __ dmb(ISH);
4582 __ Bind(slow_path->GetExitLabel());
4583}
4584
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004585void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4586 LocationSummary* locations =
4587 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004588 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004589 locations->SetOut(Location::RequiresRegister());
4590}
4591
4592void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004593 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004594 codegen_->AddSlowPath(slow_path);
4595
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004596 LocationSummary* locations = load->GetLocations();
4597 Register out = locations->Out().AsRegister<Register>();
4598 Register current_method = locations->InAt(0).AsRegister<Register>();
4599 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004600 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004601 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004602 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004603 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004604 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004605 __ Bind(slow_path->GetExitLabel());
4606}
4607
David Brazdilcb1c0552015-08-04 16:22:25 +01004608static int32_t GetExceptionTlsOffset() {
4609 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4610}
4611
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004612void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4613 LocationSummary* locations =
4614 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4615 locations->SetOut(Location::RequiresRegister());
4616}
4617
4618void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004619 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004620 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4621}
4622
4623void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4624 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4625}
4626
4627void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004628 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004629 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004630}
4631
4632void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4633 LocationSummary* locations =
4634 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4635 InvokeRuntimeCallingConvention calling_convention;
4636 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4637}
4638
4639void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4640 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004641 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004642}
4643
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004644void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004645 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4646 switch (instruction->GetTypeCheckKind()) {
4647 case TypeCheckKind::kExactCheck:
4648 case TypeCheckKind::kAbstractClassCheck:
4649 case TypeCheckKind::kClassHierarchyCheck:
4650 case TypeCheckKind::kArrayObjectCheck:
4651 call_kind = LocationSummary::kNoCall;
4652 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004653 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004654 case TypeCheckKind::kInterfaceCheck:
4655 call_kind = LocationSummary::kCall;
4656 break;
4657 case TypeCheckKind::kArrayCheck:
4658 call_kind = LocationSummary::kCallOnSlowPath;
4659 break;
4660 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004661 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004662 if (call_kind != LocationSummary::kCall) {
4663 locations->SetInAt(0, Location::RequiresRegister());
4664 locations->SetInAt(1, Location::RequiresRegister());
4665 // The out register is used as a temporary, so it overlaps with the inputs.
4666 // Note that TypeCheckSlowPathARM uses this register too.
4667 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4668 } else {
4669 InvokeRuntimeCallingConvention calling_convention;
4670 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4671 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4672 locations->SetOut(Location::RegisterLocation(R0));
4673 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004674}
4675
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004676void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004677 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004678 Register obj = locations->InAt(0).AsRegister<Register>();
4679 Register cls = locations->InAt(1).AsRegister<Register>();
4680 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004681 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004682 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4683 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4684 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004685 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004686 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004687
4688 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004689 // avoid null check if we know obj is not null.
4690 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004691 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004692 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004693
Calin Juravle98893e12015-10-02 21:05:03 +01004694 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004695 // This is safe, as the register is caller-save, and the object must be in another
4696 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004697 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4698 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004699 ? obj
4700 : out;
4701 __ LoadFromOffset(kLoadWord, target, obj, class_offset);
4702 __ MaybeUnpoisonHeapReference(target);
4703
4704 switch (instruction->GetTypeCheckKind()) {
4705 case TypeCheckKind::kExactCheck: {
4706 __ cmp(out, ShifterOperand(cls));
4707 // Classes must be equal for the instanceof to succeed.
4708 __ b(&zero, NE);
4709 __ LoadImmediate(out, 1);
4710 __ b(&done);
4711 break;
4712 }
4713 case TypeCheckKind::kAbstractClassCheck: {
4714 // If the class is abstract, we eagerly fetch the super class of the
4715 // object to avoid doing a comparison we know will fail.
4716 Label loop;
4717 __ Bind(&loop);
4718 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4719 __ MaybeUnpoisonHeapReference(out);
4720 // If `out` is null, we use it for the result, and jump to `done`.
4721 __ CompareAndBranchIfZero(out, &done);
4722 __ cmp(out, ShifterOperand(cls));
4723 __ b(&loop, NE);
4724 __ LoadImmediate(out, 1);
4725 if (zero.IsLinked()) {
4726 __ b(&done);
4727 }
4728 break;
4729 }
4730 case TypeCheckKind::kClassHierarchyCheck: {
4731 // Walk over the class hierarchy to find a match.
4732 Label loop, success;
4733 __ Bind(&loop);
4734 __ cmp(out, ShifterOperand(cls));
4735 __ b(&success, EQ);
4736 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4737 __ MaybeUnpoisonHeapReference(out);
4738 __ CompareAndBranchIfNonZero(out, &loop);
4739 // If `out` is null, we use it for the result, and jump to `done`.
4740 __ b(&done);
4741 __ Bind(&success);
4742 __ LoadImmediate(out, 1);
4743 if (zero.IsLinked()) {
4744 __ b(&done);
4745 }
4746 break;
4747 }
4748 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004749 // Do an exact check.
4750 Label exact_check;
4751 __ cmp(out, ShifterOperand(cls));
4752 __ b(&exact_check, EQ);
4753 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004754 __ LoadFromOffset(kLoadWord, out, out, component_offset);
4755 __ MaybeUnpoisonHeapReference(out);
4756 // If `out` is null, we use it for the result, and jump to `done`.
4757 __ CompareAndBranchIfZero(out, &done);
4758 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4759 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4760 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004761 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004762 __ LoadImmediate(out, 1);
4763 __ b(&done);
4764 break;
4765 }
4766 case TypeCheckKind::kArrayCheck: {
4767 __ cmp(out, ShifterOperand(cls));
4768 DCHECK(locations->OnlyCallsOnSlowPath());
4769 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4770 instruction, /* is_fatal */ false);
4771 codegen_->AddSlowPath(slow_path);
4772 __ b(slow_path->GetEntryLabel(), NE);
4773 __ LoadImmediate(out, 1);
4774 if (zero.IsLinked()) {
4775 __ b(&done);
4776 }
4777 break;
4778 }
Calin Juravle98893e12015-10-02 21:05:03 +01004779 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004780 case TypeCheckKind::kInterfaceCheck:
4781 default: {
4782 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4783 instruction,
4784 instruction->GetDexPc(),
4785 nullptr);
4786 if (zero.IsLinked()) {
4787 __ b(&done);
4788 }
4789 break;
4790 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004791 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004792
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004793 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004794 __ Bind(&zero);
4795 __ LoadImmediate(out, 0);
4796 }
4797
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004798 if (done.IsLinked()) {
4799 __ Bind(&done);
4800 }
4801
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004802 if (slow_path != nullptr) {
4803 __ Bind(slow_path->GetExitLabel());
4804 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004805}
4806
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004807void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004808 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4809 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4810
4811 switch (instruction->GetTypeCheckKind()) {
4812 case TypeCheckKind::kExactCheck:
4813 case TypeCheckKind::kAbstractClassCheck:
4814 case TypeCheckKind::kClassHierarchyCheck:
4815 case TypeCheckKind::kArrayObjectCheck:
4816 call_kind = throws_into_catch
4817 ? LocationSummary::kCallOnSlowPath
4818 : LocationSummary::kNoCall;
4819 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004820 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004821 case TypeCheckKind::kInterfaceCheck:
4822 call_kind = LocationSummary::kCall;
4823 break;
4824 case TypeCheckKind::kArrayCheck:
4825 call_kind = LocationSummary::kCallOnSlowPath;
4826 break;
4827 }
4828
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004829 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004830 instruction, call_kind);
4831 if (call_kind != LocationSummary::kCall) {
4832 locations->SetInAt(0, Location::RequiresRegister());
4833 locations->SetInAt(1, Location::RequiresRegister());
4834 // Note that TypeCheckSlowPathARM uses this register too.
4835 locations->AddTemp(Location::RequiresRegister());
4836 } else {
4837 InvokeRuntimeCallingConvention calling_convention;
4838 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4839 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4840 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004841}
4842
4843void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4844 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004845 Register obj = locations->InAt(0).AsRegister<Register>();
4846 Register cls = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004847 Register temp = locations->WillCall()
4848 ? Register(kNoRegister)
4849 : locations->GetTemp(0).AsRegister<Register>();
4850
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004851 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004852 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4853 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4854 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4855 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004856
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004857 if (!locations->WillCall()) {
4858 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4859 instruction, !locations->CanCall());
4860 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004861 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004862
4863 Label done;
4864 // Avoid null check if we know obj is not null.
4865 if (instruction->MustDoNullCheck()) {
4866 __ CompareAndBranchIfZero(obj, &done);
4867 }
4868
4869 if (locations->WillCall()) {
4870 __ LoadFromOffset(kLoadWord, obj, obj, class_offset);
4871 __ MaybeUnpoisonHeapReference(obj);
4872 } else {
4873 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4874 __ MaybeUnpoisonHeapReference(temp);
4875 }
4876
4877 switch (instruction->GetTypeCheckKind()) {
4878 case TypeCheckKind::kExactCheck:
4879 case TypeCheckKind::kArrayCheck: {
4880 __ cmp(temp, ShifterOperand(cls));
4881 // Jump to slow path for throwing the exception or doing a
4882 // more involved array check.
4883 __ b(slow_path->GetEntryLabel(), NE);
4884 break;
4885 }
4886 case TypeCheckKind::kAbstractClassCheck: {
4887 // If the class is abstract, we eagerly fetch the super class of the
4888 // object to avoid doing a comparison we know will fail.
4889 Label loop;
4890 __ Bind(&loop);
4891 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4892 __ MaybeUnpoisonHeapReference(temp);
4893 // Jump to the slow path to throw the exception.
4894 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4895 __ cmp(temp, ShifterOperand(cls));
4896 __ b(&loop, NE);
4897 break;
4898 }
4899 case TypeCheckKind::kClassHierarchyCheck: {
4900 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004901 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004902 __ Bind(&loop);
4903 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004904 __ b(&done, EQ);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004905 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4906 __ MaybeUnpoisonHeapReference(temp);
4907 __ CompareAndBranchIfNonZero(temp, &loop);
4908 // Jump to the slow path to throw the exception.
4909 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004910 break;
4911 }
4912 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004913 // Do an exact check.
4914 __ cmp(temp, ShifterOperand(cls));
4915 __ b(&done, EQ);
4916 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004917 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
4918 __ MaybeUnpoisonHeapReference(temp);
4919 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4920 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
4921 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4922 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
4923 break;
4924 }
Calin Juravle98893e12015-10-02 21:05:03 +01004925 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004926 case TypeCheckKind::kInterfaceCheck:
4927 default:
4928 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
4929 instruction,
4930 instruction->GetDexPc(),
4931 nullptr);
4932 break;
4933 }
4934 __ Bind(&done);
4935
4936 if (slow_path != nullptr) {
4937 __ Bind(slow_path->GetExitLabel());
4938 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004939}
4940
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004941void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4942 LocationSummary* locations =
4943 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4944 InvokeRuntimeCallingConvention calling_convention;
4945 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4946}
4947
4948void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4949 codegen_->InvokeRuntime(instruction->IsEnter()
4950 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4951 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004952 instruction->GetDexPc(),
4953 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004954}
4955
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004956void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
4957void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
4958void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004959
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004960void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004961 LocationSummary* locations =
4962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4963 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4964 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004965 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004966 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004967 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004969}
4970
4971void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4972 HandleBitwiseOperation(instruction);
4973}
4974
4975void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4976 HandleBitwiseOperation(instruction);
4977}
4978
4979void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4980 HandleBitwiseOperation(instruction);
4981}
4982
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004983void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
4984 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
4985 if (value == 0xffffffffu) {
4986 if (out != first) {
4987 __ mov(out, ShifterOperand(first));
4988 }
4989 return;
4990 }
4991 if (value == 0u) {
4992 __ mov(out, ShifterOperand(0));
4993 return;
4994 }
4995 ShifterOperand so;
4996 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
4997 __ and_(out, first, so);
4998 } else {
4999 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5000 __ bic(out, first, ShifterOperand(~value));
5001 }
5002}
5003
5004void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5005 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5006 if (value == 0u) {
5007 if (out != first) {
5008 __ mov(out, ShifterOperand(first));
5009 }
5010 return;
5011 }
5012 if (value == 0xffffffffu) {
5013 __ mvn(out, ShifterOperand(0));
5014 return;
5015 }
5016 ShifterOperand so;
5017 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5018 __ orr(out, first, so);
5019 } else {
5020 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5021 __ orn(out, first, ShifterOperand(~value));
5022 }
5023}
5024
5025void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5026 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5027 if (value == 0u) {
5028 if (out != first) {
5029 __ mov(out, ShifterOperand(first));
5030 }
5031 return;
5032 }
5033 __ eor(out, first, ShifterOperand(value));
5034}
5035
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005036void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5037 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005038 Location first = locations->InAt(0);
5039 Location second = locations->InAt(1);
5040 Location out = locations->Out();
5041
5042 if (second.IsConstant()) {
5043 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5044 uint32_t value_low = Low32Bits(value);
5045 if (instruction->GetResultType() == Primitive::kPrimInt) {
5046 Register first_reg = first.AsRegister<Register>();
5047 Register out_reg = out.AsRegister<Register>();
5048 if (instruction->IsAnd()) {
5049 GenerateAndConst(out_reg, first_reg, value_low);
5050 } else if (instruction->IsOr()) {
5051 GenerateOrrConst(out_reg, first_reg, value_low);
5052 } else {
5053 DCHECK(instruction->IsXor());
5054 GenerateEorConst(out_reg, first_reg, value_low);
5055 }
5056 } else {
5057 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5058 uint32_t value_high = High32Bits(value);
5059 Register first_low = first.AsRegisterPairLow<Register>();
5060 Register first_high = first.AsRegisterPairHigh<Register>();
5061 Register out_low = out.AsRegisterPairLow<Register>();
5062 Register out_high = out.AsRegisterPairHigh<Register>();
5063 if (instruction->IsAnd()) {
5064 GenerateAndConst(out_low, first_low, value_low);
5065 GenerateAndConst(out_high, first_high, value_high);
5066 } else if (instruction->IsOr()) {
5067 GenerateOrrConst(out_low, first_low, value_low);
5068 GenerateOrrConst(out_high, first_high, value_high);
5069 } else {
5070 DCHECK(instruction->IsXor());
5071 GenerateEorConst(out_low, first_low, value_low);
5072 GenerateEorConst(out_high, first_high, value_high);
5073 }
5074 }
5075 return;
5076 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005077
5078 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005079 Register first_reg = first.AsRegister<Register>();
5080 ShifterOperand second_reg(second.AsRegister<Register>());
5081 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005082 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005083 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005084 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005085 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005086 } else {
5087 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005088 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005089 }
5090 } else {
5091 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005092 Register first_low = first.AsRegisterPairLow<Register>();
5093 Register first_high = first.AsRegisterPairHigh<Register>();
5094 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5095 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5096 Register out_low = out.AsRegisterPairLow<Register>();
5097 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005098 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005099 __ and_(out_low, first_low, second_low);
5100 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005101 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005102 __ orr(out_low, first_low, second_low);
5103 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005104 } else {
5105 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005106 __ eor(out_low, first_low, second_low);
5107 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005108 }
5109 }
5110}
5111
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005112void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00005113 // For better instruction scheduling we load the direct code pointer before the method pointer.
5114 bool direct_code_loaded = false;
5115 switch (invoke->GetCodePtrLocation()) {
5116 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5117 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
5118 break;
5119 }
5120 // Calls across dex files are more likely to exceed the available BL range,
5121 // so use absolute patch by falling through to kDirectCodeFixup.
5122 FALLTHROUGH_INTENDED;
5123 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5124 // LR = code address from literal pool with link-time patch.
5125 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5126 direct_code_loaded = true;
5127 break;
5128 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5129 // LR = invoke->GetDirectCodePtr();
5130 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
5131 direct_code_loaded = true;
5132 break;
5133 default:
5134 break;
5135 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005136
Vladimir Marko58155012015-08-19 12:49:41 +00005137 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5138 switch (invoke->GetMethodLoadKind()) {
5139 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5140 // temp = thread->string_init_entrypoint
5141 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5142 break;
5143 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
5144 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5145 break;
5146 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5147 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5148 break;
5149 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5150 __ LoadLiteral(temp.AsRegister<Register>(),
5151 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5152 break;
5153 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5154 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5155 FALLTHROUGH_INTENDED;
5156 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5157 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5158 Register method_reg;
5159 Register reg = temp.AsRegister<Register>();
5160 if (current_method.IsRegister()) {
5161 method_reg = current_method.AsRegister<Register>();
5162 } else {
5163 DCHECK(invoke->GetLocations()->Intrinsified());
5164 DCHECK(!current_method.IsValid());
5165 method_reg = reg;
5166 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5167 }
5168 // temp = current_method->dex_cache_resolved_methods_;
5169 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01005170 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
5171 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005172 // temp = temp[index_in_cache]
5173 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5174 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5175 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005176 }
Vladimir Marko58155012015-08-19 12:49:41 +00005177 }
5178
5179 switch (invoke->GetCodePtrLocation()) {
5180 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5181 __ bl(GetFrameEntryLabel());
5182 break;
5183 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5184 if (!direct_code_loaded) {
5185 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
5186 __ Bind(&relative_call_patches_.back().label);
5187 Label label;
5188 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
5189 __ Bind(&label);
5190 break;
5191 }
5192 // If we loaded the direct code above, fall through.
5193 FALLTHROUGH_INTENDED;
5194 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5195 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5196 // LR prepared above for better instruction scheduling.
5197 DCHECK(direct_code_loaded);
5198 // LR()
5199 __ blx(LR);
5200 break;
5201 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5202 // LR = callee_method->entry_point_from_quick_compiled_code_
5203 __ LoadFromOffset(
5204 kLoadWord, LR, callee_method.AsRegister<Register>(),
5205 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5206 // LR()
5207 __ blx(LR);
5208 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005209 }
5210
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005211 DCHECK(!IsLeafMethod());
5212}
5213
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005214void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5215 Register temp = temp_location.AsRegister<Register>();
5216 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5217 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5218 LocationSummary* locations = invoke->GetLocations();
5219 Location receiver = locations->InAt(0);
5220 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5221 // temp = object->GetClass();
5222 DCHECK(receiver.IsRegister());
5223 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5224 MaybeRecordImplicitNullCheck(invoke);
5225 __ MaybeUnpoisonHeapReference(temp);
5226 // temp = temp->GetMethodAt(method_offset);
5227 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5228 kArmWordSize).Int32Value();
5229 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5230 // LR = temp->GetEntryPoint();
5231 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5232 // LR();
5233 __ blx(LR);
5234}
5235
Vladimir Marko58155012015-08-19 12:49:41 +00005236void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5237 DCHECK(linker_patches->empty());
5238 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5239 linker_patches->reserve(size);
5240 for (const auto& entry : method_patches_) {
5241 const MethodReference& target_method = entry.first;
5242 Literal* literal = entry.second;
5243 DCHECK(literal->GetLabel()->IsBound());
5244 uint32_t literal_offset = literal->GetLabel()->Position();
5245 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5246 target_method.dex_file,
5247 target_method.dex_method_index));
5248 }
5249 for (const auto& entry : call_patches_) {
5250 const MethodReference& target_method = entry.first;
5251 Literal* literal = entry.second;
5252 DCHECK(literal->GetLabel()->IsBound());
5253 uint32_t literal_offset = literal->GetLabel()->Position();
5254 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5255 target_method.dex_file,
5256 target_method.dex_method_index));
5257 }
5258 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5259 uint32_t literal_offset = info.label.Position();
5260 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5261 info.target_method.dex_file,
5262 info.target_method.dex_method_index));
5263 }
5264}
5265
5266Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5267 MethodToLiteralMap* map) {
5268 // Look up the literal for target_method.
5269 auto lb = map->lower_bound(target_method);
5270 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5271 return lb->second;
5272 }
5273 // We don't have a literal for this method yet, insert a new one.
5274 Literal* literal = __ NewLiteral<uint32_t>(0u);
5275 map->PutBefore(lb, target_method, literal);
5276 return literal;
5277}
5278
5279Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5280 return DeduplicateMethodLiteral(target_method, &method_patches_);
5281}
5282
5283Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5284 return DeduplicateMethodLiteral(target_method, &call_patches_);
5285}
5286
Calin Juravleb1498f62015-02-16 13:13:29 +00005287void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
5288 // Nothing to do, this should be removed during prepare for register allocator.
5289 UNUSED(instruction);
5290 LOG(FATAL) << "Unreachable";
5291}
5292
5293void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
5294 // Nothing to do, this should be removed during prepare for register allocator.
5295 UNUSED(instruction);
5296 LOG(FATAL) << "Unreachable";
5297}
5298
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005299void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5300 DCHECK(codegen_->IsBaseline());
5301 LocationSummary* locations =
5302 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5303 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5304}
5305
5306void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5307 DCHECK(codegen_->IsBaseline());
5308 // Will be generated at use site.
5309}
5310
Mark Mendellfe57faa2015-09-18 09:26:15 -04005311// Simple implementation of packed switch - generate cascaded compare/jumps.
5312void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5313 LocationSummary* locations =
5314 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5315 locations->SetInAt(0, Location::RequiresRegister());
5316}
5317
5318void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5319 int32_t lower_bound = switch_instr->GetStartValue();
5320 int32_t num_entries = switch_instr->GetNumEntries();
5321 LocationSummary* locations = switch_instr->GetLocations();
5322 Register value_reg = locations->InAt(0).AsRegister<Register>();
5323 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5324
5325 // Create a series of compare/jumps.
5326 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5327 for (int32_t i = 0; i < num_entries; i++) {
5328 GenerateCompareWithImmediate(value_reg, lower_bound + i);
Vladimir Markoec7802a2015-10-01 20:57:57 +01005329 __ b(codegen_->GetLabelOf(successors[i]), EQ);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005330 }
5331
5332 // And the default for any other value.
5333 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5334 __ b(codegen_->GetLabelOf(default_block));
5335 }
5336}
5337
Andreas Gampe85b62f22015-09-09 13:15:38 -07005338void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5339 if (!trg.IsValid()) {
5340 DCHECK(type == Primitive::kPrimVoid);
5341 return;
5342 }
5343
5344 DCHECK_NE(type, Primitive::kPrimVoid);
5345
5346 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5347 if (return_loc.Equals(trg)) {
5348 return;
5349 }
5350
5351 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5352 // with the last branch.
5353 if (type == Primitive::kPrimLong) {
5354 HParallelMove parallel_move(GetGraph()->GetArena());
5355 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
5356 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
5357 GetMoveResolver()->EmitNativeCode(&parallel_move);
5358 } else if (type == Primitive::kPrimDouble) {
5359 HParallelMove parallel_move(GetGraph()->GetArena());
5360 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
5361 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
5362 GetMoveResolver()->EmitNativeCode(&parallel_move);
5363 } else {
5364 // Let the parallel move resolver take care of all of this.
5365 HParallelMove parallel_move(GetGraph()->GetArena());
5366 parallel_move.AddMove(return_loc, trg, type, nullptr);
5367 GetMoveResolver()->EmitNativeCode(&parallel_move);
5368 }
5369}
5370
Roland Levillain4d027112015-07-01 15:41:14 +01005371#undef __
5372#undef QUICK_ENTRY_POINT
5373
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005374} // namespace arm
5375} // namespace art