blob: 8c1820b6cdbb90347207fff62e793c465f38a73f [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(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001648 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001649 codegen_->GetInstructionSetFeatures());
1650 if (intrinsic.TryDispatch(invoke)) {
1651 return;
1652 }
1653
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001654 HandleInvoke(invoke);
1655}
1656
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001657static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1658 if (invoke->GetLocations()->Intrinsified()) {
1659 IntrinsicCodeGeneratorARM intrinsic(codegen);
1660 intrinsic.Dispatch(invoke);
1661 return true;
1662 }
1663 return false;
1664}
1665
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001666void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001667 // When we do not run baseline, explicit clinit checks triggered by static
1668 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1669 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001670
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001671 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1672 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001673 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001674
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001675 LocationSummary* locations = invoke->GetLocations();
1676 codegen_->GenerateStaticOrDirectCall(
1677 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001678 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001679}
1680
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001681void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001682 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001683 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001684}
1685
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001686void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001687 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001688 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001689 codegen_->GetInstructionSetFeatures());
1690 if (intrinsic.TryDispatch(invoke)) {
1691 return;
1692 }
1693
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001694 HandleInvoke(invoke);
1695}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001696
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001697void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001698 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1699 return;
1700 }
1701
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001702 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001703 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001704 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001705}
1706
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001707void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1708 HandleInvoke(invoke);
1709 // Add the hidden argument.
1710 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1711}
1712
1713void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1714 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001715 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001716 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1717 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001718 LocationSummary* locations = invoke->GetLocations();
1719 Location receiver = locations->InAt(0);
1720 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1721
1722 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001723 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1724 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001725
1726 // temp = object->GetClass();
1727 if (receiver.IsStackSlot()) {
1728 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1729 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1730 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001731 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001732 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001733 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001734 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001735 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001736 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001737 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001738 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1739 // LR = temp->GetEntryPoint();
1740 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1741 // LR();
1742 __ blx(LR);
1743 DCHECK(!codegen_->IsLeafMethod());
1744 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1745}
1746
Roland Levillain88cb1752014-10-20 16:36:47 +01001747void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1748 LocationSummary* locations =
1749 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1750 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001751 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001752 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1754 break;
1755 }
1756 case Primitive::kPrimLong: {
1757 locations->SetInAt(0, Location::RequiresRegister());
1758 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001759 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001760 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001761
Roland Levillain88cb1752014-10-20 16:36:47 +01001762 case Primitive::kPrimFloat:
1763 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001764 locations->SetInAt(0, Location::RequiresFpuRegister());
1765 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001766 break;
1767
1768 default:
1769 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1770 }
1771}
1772
1773void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1774 LocationSummary* locations = neg->GetLocations();
1775 Location out = locations->Out();
1776 Location in = locations->InAt(0);
1777 switch (neg->GetResultType()) {
1778 case Primitive::kPrimInt:
1779 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001780 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001781 break;
1782
1783 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001784 DCHECK(in.IsRegisterPair());
1785 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1786 __ rsbs(out.AsRegisterPairLow<Register>(),
1787 in.AsRegisterPairLow<Register>(),
1788 ShifterOperand(0));
1789 // We cannot emit an RSC (Reverse Subtract with Carry)
1790 // instruction here, as it does not exist in the Thumb-2
1791 // instruction set. We use the following approach
1792 // using SBC and SUB instead.
1793 //
1794 // out.hi = -C
1795 __ sbc(out.AsRegisterPairHigh<Register>(),
1796 out.AsRegisterPairHigh<Register>(),
1797 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1798 // out.hi = out.hi - in.hi
1799 __ sub(out.AsRegisterPairHigh<Register>(),
1800 out.AsRegisterPairHigh<Register>(),
1801 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1802 break;
1803
Roland Levillain88cb1752014-10-20 16:36:47 +01001804 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001805 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001806 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001807 break;
1808
Roland Levillain88cb1752014-10-20 16:36:47 +01001809 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001810 DCHECK(in.IsFpuRegisterPair());
1811 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1812 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001813 break;
1814
1815 default:
1816 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1817 }
1818}
1819
Roland Levillaindff1f282014-11-05 14:15:05 +00001820void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001821 Primitive::Type result_type = conversion->GetResultType();
1822 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001823 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001824
Roland Levillain5b3ee562015-04-14 16:02:41 +01001825 // The float-to-long, double-to-long and long-to-float type conversions
1826 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001827 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001828 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1829 && result_type == Primitive::kPrimLong)
1830 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001831 ? LocationSummary::kCall
1832 : LocationSummary::kNoCall;
1833 LocationSummary* locations =
1834 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1835
David Brazdilb2bd1c52015-03-25 11:17:37 +00001836 // The Java language does not allow treating boolean as an integral type but
1837 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001838
Roland Levillaindff1f282014-11-05 14:15:05 +00001839 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001840 case Primitive::kPrimByte:
1841 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001842 case Primitive::kPrimBoolean:
1843 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001844 case Primitive::kPrimShort:
1845 case Primitive::kPrimInt:
1846 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001847 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001848 locations->SetInAt(0, Location::RequiresRegister());
1849 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1850 break;
1851
1852 default:
1853 LOG(FATAL) << "Unexpected type conversion from " << input_type
1854 << " to " << result_type;
1855 }
1856 break;
1857
Roland Levillain01a8d712014-11-14 16:27:39 +00001858 case Primitive::kPrimShort:
1859 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001860 case Primitive::kPrimBoolean:
1861 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001862 case Primitive::kPrimByte:
1863 case Primitive::kPrimInt:
1864 case Primitive::kPrimChar:
1865 // Processing a Dex `int-to-short' instruction.
1866 locations->SetInAt(0, Location::RequiresRegister());
1867 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1868 break;
1869
1870 default:
1871 LOG(FATAL) << "Unexpected type conversion from " << input_type
1872 << " to " << result_type;
1873 }
1874 break;
1875
Roland Levillain946e1432014-11-11 17:35:19 +00001876 case Primitive::kPrimInt:
1877 switch (input_type) {
1878 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001879 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001880 locations->SetInAt(0, Location::Any());
1881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1882 break;
1883
1884 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001885 // Processing a Dex `float-to-int' instruction.
1886 locations->SetInAt(0, Location::RequiresFpuRegister());
1887 locations->SetOut(Location::RequiresRegister());
1888 locations->AddTemp(Location::RequiresFpuRegister());
1889 break;
1890
Roland Levillain946e1432014-11-11 17:35:19 +00001891 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001892 // Processing a Dex `double-to-int' instruction.
1893 locations->SetInAt(0, Location::RequiresFpuRegister());
1894 locations->SetOut(Location::RequiresRegister());
1895 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001896 break;
1897
1898 default:
1899 LOG(FATAL) << "Unexpected type conversion from " << input_type
1900 << " to " << result_type;
1901 }
1902 break;
1903
Roland Levillaindff1f282014-11-05 14:15:05 +00001904 case Primitive::kPrimLong:
1905 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001906 case Primitive::kPrimBoolean:
1907 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001908 case Primitive::kPrimByte:
1909 case Primitive::kPrimShort:
1910 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001911 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001912 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001913 locations->SetInAt(0, Location::RequiresRegister());
1914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1915 break;
1916
Roland Levillain624279f2014-12-04 11:54:28 +00001917 case Primitive::kPrimFloat: {
1918 // Processing a Dex `float-to-long' instruction.
1919 InvokeRuntimeCallingConvention calling_convention;
1920 locations->SetInAt(0, Location::FpuRegisterLocation(
1921 calling_convention.GetFpuRegisterAt(0)));
1922 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1923 break;
1924 }
1925
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001926 case Primitive::kPrimDouble: {
1927 // Processing a Dex `double-to-long' instruction.
1928 InvokeRuntimeCallingConvention calling_convention;
1929 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1930 calling_convention.GetFpuRegisterAt(0),
1931 calling_convention.GetFpuRegisterAt(1)));
1932 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001933 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001934 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001935
1936 default:
1937 LOG(FATAL) << "Unexpected type conversion from " << input_type
1938 << " to " << result_type;
1939 }
1940 break;
1941
Roland Levillain981e4542014-11-14 11:47:14 +00001942 case Primitive::kPrimChar:
1943 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001944 case Primitive::kPrimBoolean:
1945 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001946 case Primitive::kPrimByte:
1947 case Primitive::kPrimShort:
1948 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001949 // Processing a Dex `int-to-char' instruction.
1950 locations->SetInAt(0, Location::RequiresRegister());
1951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1952 break;
1953
1954 default:
1955 LOG(FATAL) << "Unexpected type conversion from " << input_type
1956 << " to " << result_type;
1957 }
1958 break;
1959
Roland Levillaindff1f282014-11-05 14:15:05 +00001960 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001961 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001962 case Primitive::kPrimBoolean:
1963 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001964 case Primitive::kPrimByte:
1965 case Primitive::kPrimShort:
1966 case Primitive::kPrimInt:
1967 case Primitive::kPrimChar:
1968 // Processing a Dex `int-to-float' instruction.
1969 locations->SetInAt(0, Location::RequiresRegister());
1970 locations->SetOut(Location::RequiresFpuRegister());
1971 break;
1972
Roland Levillain5b3ee562015-04-14 16:02:41 +01001973 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00001974 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001975 InvokeRuntimeCallingConvention calling_convention;
1976 locations->SetInAt(0, Location::RegisterPairLocation(
1977 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1978 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001979 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01001980 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001981
Roland Levillaincff13742014-11-17 14:32:17 +00001982 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001983 // Processing a Dex `double-to-float' instruction.
1984 locations->SetInAt(0, Location::RequiresFpuRegister());
1985 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001986 break;
1987
1988 default:
1989 LOG(FATAL) << "Unexpected type conversion from " << input_type
1990 << " to " << result_type;
1991 };
1992 break;
1993
Roland Levillaindff1f282014-11-05 14:15:05 +00001994 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001995 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001996 case Primitive::kPrimBoolean:
1997 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001998 case Primitive::kPrimByte:
1999 case Primitive::kPrimShort:
2000 case Primitive::kPrimInt:
2001 case Primitive::kPrimChar:
2002 // Processing a Dex `int-to-double' instruction.
2003 locations->SetInAt(0, Location::RequiresRegister());
2004 locations->SetOut(Location::RequiresFpuRegister());
2005 break;
2006
2007 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002008 // Processing a Dex `long-to-double' instruction.
2009 locations->SetInAt(0, Location::RequiresRegister());
2010 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002011 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002012 locations->AddTemp(Location::RequiresFpuRegister());
2013 break;
2014
Roland Levillaincff13742014-11-17 14:32:17 +00002015 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002016 // Processing a Dex `float-to-double' instruction.
2017 locations->SetInAt(0, Location::RequiresFpuRegister());
2018 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002019 break;
2020
2021 default:
2022 LOG(FATAL) << "Unexpected type conversion from " << input_type
2023 << " to " << result_type;
2024 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002025 break;
2026
2027 default:
2028 LOG(FATAL) << "Unexpected type conversion from " << input_type
2029 << " to " << result_type;
2030 }
2031}
2032
2033void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2034 LocationSummary* locations = conversion->GetLocations();
2035 Location out = locations->Out();
2036 Location in = locations->InAt(0);
2037 Primitive::Type result_type = conversion->GetResultType();
2038 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002039 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002040 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002041 case Primitive::kPrimByte:
2042 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002043 case Primitive::kPrimBoolean:
2044 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002045 case Primitive::kPrimShort:
2046 case Primitive::kPrimInt:
2047 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002048 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002049 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002050 break;
2051
2052 default:
2053 LOG(FATAL) << "Unexpected type conversion from " << input_type
2054 << " to " << result_type;
2055 }
2056 break;
2057
Roland Levillain01a8d712014-11-14 16:27:39 +00002058 case Primitive::kPrimShort:
2059 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002060 case Primitive::kPrimBoolean:
2061 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002062 case Primitive::kPrimByte:
2063 case Primitive::kPrimInt:
2064 case Primitive::kPrimChar:
2065 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002066 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002067 break;
2068
2069 default:
2070 LOG(FATAL) << "Unexpected type conversion from " << input_type
2071 << " to " << result_type;
2072 }
2073 break;
2074
Roland Levillain946e1432014-11-11 17:35:19 +00002075 case Primitive::kPrimInt:
2076 switch (input_type) {
2077 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002078 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002079 DCHECK(out.IsRegister());
2080 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002082 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002083 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002084 } else {
2085 DCHECK(in.IsConstant());
2086 DCHECK(in.GetConstant()->IsLongConstant());
2087 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002089 }
2090 break;
2091
Roland Levillain3f8f9362014-12-02 17:45:01 +00002092 case Primitive::kPrimFloat: {
2093 // Processing a Dex `float-to-int' instruction.
2094 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2095 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2096 __ vcvtis(temp, temp);
2097 __ vmovrs(out.AsRegister<Register>(), temp);
2098 break;
2099 }
2100
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002101 case Primitive::kPrimDouble: {
2102 // Processing a Dex `double-to-int' instruction.
2103 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2104 DRegister temp_d = FromLowSToD(temp_s);
2105 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2106 __ vcvtid(temp_s, temp_d);
2107 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002108 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002109 }
Roland Levillain946e1432014-11-11 17:35:19 +00002110
2111 default:
2112 LOG(FATAL) << "Unexpected type conversion from " << input_type
2113 << " to " << result_type;
2114 }
2115 break;
2116
Roland Levillaindff1f282014-11-05 14:15:05 +00002117 case Primitive::kPrimLong:
2118 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002119 case Primitive::kPrimBoolean:
2120 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002121 case Primitive::kPrimByte:
2122 case Primitive::kPrimShort:
2123 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002124 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002125 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002126 DCHECK(out.IsRegisterPair());
2127 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002128 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002129 // Sign extension.
2130 __ Asr(out.AsRegisterPairHigh<Register>(),
2131 out.AsRegisterPairLow<Register>(),
2132 31);
2133 break;
2134
2135 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002136 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002137 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2138 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002139 conversion->GetDexPc(),
2140 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002141 break;
2142
Roland Levillaindff1f282014-11-05 14:15:05 +00002143 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002144 // Processing a Dex `double-to-long' instruction.
2145 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2146 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002147 conversion->GetDexPc(),
2148 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002149 break;
2150
2151 default:
2152 LOG(FATAL) << "Unexpected type conversion from " << input_type
2153 << " to " << result_type;
2154 }
2155 break;
2156
Roland Levillain981e4542014-11-14 11:47:14 +00002157 case Primitive::kPrimChar:
2158 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002159 case Primitive::kPrimBoolean:
2160 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002161 case Primitive::kPrimByte:
2162 case Primitive::kPrimShort:
2163 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002164 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002165 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002166 break;
2167
2168 default:
2169 LOG(FATAL) << "Unexpected type conversion from " << input_type
2170 << " to " << result_type;
2171 }
2172 break;
2173
Roland Levillaindff1f282014-11-05 14:15:05 +00002174 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002175 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002176 case Primitive::kPrimBoolean:
2177 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002178 case Primitive::kPrimByte:
2179 case Primitive::kPrimShort:
2180 case Primitive::kPrimInt:
2181 case Primitive::kPrimChar: {
2182 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002183 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2184 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002185 break;
2186 }
2187
Roland Levillain5b3ee562015-04-14 16:02:41 +01002188 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002189 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002190 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2191 conversion,
2192 conversion->GetDexPc(),
2193 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002194 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002195
Roland Levillaincff13742014-11-17 14:32:17 +00002196 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002197 // Processing a Dex `double-to-float' instruction.
2198 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2199 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002200 break;
2201
2202 default:
2203 LOG(FATAL) << "Unexpected type conversion from " << input_type
2204 << " to " << result_type;
2205 };
2206 break;
2207
Roland Levillaindff1f282014-11-05 14:15:05 +00002208 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002209 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002210 case Primitive::kPrimBoolean:
2211 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002212 case Primitive::kPrimByte:
2213 case Primitive::kPrimShort:
2214 case Primitive::kPrimInt:
2215 case Primitive::kPrimChar: {
2216 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002217 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002218 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2219 out.AsFpuRegisterPairLow<SRegister>());
2220 break;
2221 }
2222
Roland Levillain647b9ed2014-11-27 12:06:00 +00002223 case Primitive::kPrimLong: {
2224 // Processing a Dex `long-to-double' instruction.
2225 Register low = in.AsRegisterPairLow<Register>();
2226 Register high = in.AsRegisterPairHigh<Register>();
2227 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2228 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002229 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002230 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002231 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2232 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002233
Roland Levillain682393c2015-04-14 15:57:52 +01002234 // temp_d = int-to-double(high)
2235 __ vmovsr(temp_s, high);
2236 __ vcvtdi(temp_d, temp_s);
2237 // constant_d = k2Pow32EncodingForDouble
2238 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2239 // out_d = unsigned-to-double(low)
2240 __ vmovsr(out_s, low);
2241 __ vcvtdu(out_d, out_s);
2242 // out_d += temp_d * constant_d
2243 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002244 break;
2245 }
2246
Roland Levillaincff13742014-11-17 14:32:17 +00002247 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002248 // Processing a Dex `float-to-double' instruction.
2249 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2250 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002251 break;
2252
2253 default:
2254 LOG(FATAL) << "Unexpected type conversion from " << input_type
2255 << " to " << result_type;
2256 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002257 break;
2258
2259 default:
2260 LOG(FATAL) << "Unexpected type conversion from " << input_type
2261 << " to " << result_type;
2262 }
2263}
2264
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002265void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002266 LocationSummary* locations =
2267 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002268 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002269 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002270 locations->SetInAt(0, Location::RequiresRegister());
2271 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2273 break;
2274 }
2275
2276 case Primitive::kPrimLong: {
2277 locations->SetInAt(0, Location::RequiresRegister());
2278 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002279 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002280 break;
2281 }
2282
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002283 case Primitive::kPrimFloat:
2284 case Primitive::kPrimDouble: {
2285 locations->SetInAt(0, Location::RequiresFpuRegister());
2286 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002287 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002288 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002289 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002290
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002291 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002292 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002293 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002294}
2295
2296void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2297 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002298 Location out = locations->Out();
2299 Location first = locations->InAt(0);
2300 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002301 switch (add->GetResultType()) {
2302 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002303 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002304 __ add(out.AsRegister<Register>(),
2305 first.AsRegister<Register>(),
2306 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002307 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ AddConstant(out.AsRegister<Register>(),
2309 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002310 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002311 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002312 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002313
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002314 case Primitive::kPrimLong: {
2315 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002316 __ adds(out.AsRegisterPairLow<Register>(),
2317 first.AsRegisterPairLow<Register>(),
2318 ShifterOperand(second.AsRegisterPairLow<Register>()));
2319 __ adc(out.AsRegisterPairHigh<Register>(),
2320 first.AsRegisterPairHigh<Register>(),
2321 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002322 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002323 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002325 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002326 __ vadds(out.AsFpuRegister<SRegister>(),
2327 first.AsFpuRegister<SRegister>(),
2328 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002329 break;
2330
2331 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002332 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2333 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2334 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002335 break;
2336
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002337 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002338 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002339 }
2340}
2341
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002342void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002343 LocationSummary* locations =
2344 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002345 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002346 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002347 locations->SetInAt(0, Location::RequiresRegister());
2348 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2350 break;
2351 }
2352
2353 case Primitive::kPrimLong: {
2354 locations->SetInAt(0, Location::RequiresRegister());
2355 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002357 break;
2358 }
Calin Juravle11351682014-10-23 15:38:15 +01002359 case Primitive::kPrimFloat:
2360 case Primitive::kPrimDouble: {
2361 locations->SetInAt(0, Location::RequiresFpuRegister());
2362 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002363 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002364 break;
Calin Juravle11351682014-10-23 15:38:15 +01002365 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002366 default:
Calin Juravle11351682014-10-23 15:38:15 +01002367 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002368 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002369}
2370
2371void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2372 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002373 Location out = locations->Out();
2374 Location first = locations->InAt(0);
2375 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002376 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002377 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002378 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002379 __ sub(out.AsRegister<Register>(),
2380 first.AsRegister<Register>(),
2381 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002382 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002383 __ AddConstant(out.AsRegister<Register>(),
2384 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002385 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002386 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002387 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002388 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002389
Calin Juravle11351682014-10-23 15:38:15 +01002390 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002391 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002392 __ subs(out.AsRegisterPairLow<Register>(),
2393 first.AsRegisterPairLow<Register>(),
2394 ShifterOperand(second.AsRegisterPairLow<Register>()));
2395 __ sbc(out.AsRegisterPairHigh<Register>(),
2396 first.AsRegisterPairHigh<Register>(),
2397 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002398 break;
Calin Juravle11351682014-10-23 15:38:15 +01002399 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002400
Calin Juravle11351682014-10-23 15:38:15 +01002401 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002402 __ vsubs(out.AsFpuRegister<SRegister>(),
2403 first.AsFpuRegister<SRegister>(),
2404 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002405 break;
Calin Juravle11351682014-10-23 15:38:15 +01002406 }
2407
2408 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002409 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2410 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2411 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002412 break;
2413 }
2414
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002416 default:
Calin Juravle11351682014-10-23 15:38:15 +01002417 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002418 }
2419}
2420
Calin Juravle34bacdf2014-10-07 20:23:36 +01002421void LocationsBuilderARM::VisitMul(HMul* mul) {
2422 LocationSummary* locations =
2423 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2424 switch (mul->GetResultType()) {
2425 case Primitive::kPrimInt:
2426 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002427 locations->SetInAt(0, Location::RequiresRegister());
2428 locations->SetInAt(1, Location::RequiresRegister());
2429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002430 break;
2431 }
2432
Calin Juravleb5bfa962014-10-21 18:02:24 +01002433 case Primitive::kPrimFloat:
2434 case Primitive::kPrimDouble: {
2435 locations->SetInAt(0, Location::RequiresFpuRegister());
2436 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002437 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002438 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002439 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002440
2441 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002442 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002443 }
2444}
2445
2446void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2447 LocationSummary* locations = mul->GetLocations();
2448 Location out = locations->Out();
2449 Location first = locations->InAt(0);
2450 Location second = locations->InAt(1);
2451 switch (mul->GetResultType()) {
2452 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002453 __ mul(out.AsRegister<Register>(),
2454 first.AsRegister<Register>(),
2455 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002456 break;
2457 }
2458 case Primitive::kPrimLong: {
2459 Register out_hi = out.AsRegisterPairHigh<Register>();
2460 Register out_lo = out.AsRegisterPairLow<Register>();
2461 Register in1_hi = first.AsRegisterPairHigh<Register>();
2462 Register in1_lo = first.AsRegisterPairLow<Register>();
2463 Register in2_hi = second.AsRegisterPairHigh<Register>();
2464 Register in2_lo = second.AsRegisterPairLow<Register>();
2465
2466 // Extra checks to protect caused by the existence of R1_R2.
2467 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2468 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2469 DCHECK_NE(out_hi, in1_lo);
2470 DCHECK_NE(out_hi, in2_lo);
2471
2472 // input: in1 - 64 bits, in2 - 64 bits
2473 // output: out
2474 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2475 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2476 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2477
2478 // IP <- in1.lo * in2.hi
2479 __ mul(IP, in1_lo, in2_hi);
2480 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2481 __ mla(out_hi, in1_hi, in2_lo, IP);
2482 // out.lo <- (in1.lo * in2.lo)[31:0];
2483 __ umull(out_lo, IP, in1_lo, in2_lo);
2484 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2485 __ add(out_hi, out_hi, ShifterOperand(IP));
2486 break;
2487 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002488
2489 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002490 __ vmuls(out.AsFpuRegister<SRegister>(),
2491 first.AsFpuRegister<SRegister>(),
2492 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002493 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002494 }
2495
2496 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002497 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2498 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2499 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002500 break;
2501 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002502
2503 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002504 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002505 }
2506}
2507
Zheng Xuc6667102015-05-15 16:08:45 +08002508void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2509 DCHECK(instruction->IsDiv() || instruction->IsRem());
2510 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2511
2512 LocationSummary* locations = instruction->GetLocations();
2513 Location second = locations->InAt(1);
2514 DCHECK(second.IsConstant());
2515
2516 Register out = locations->Out().AsRegister<Register>();
2517 Register dividend = locations->InAt(0).AsRegister<Register>();
2518 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2519 DCHECK(imm == 1 || imm == -1);
2520
2521 if (instruction->IsRem()) {
2522 __ LoadImmediate(out, 0);
2523 } else {
2524 if (imm == 1) {
2525 __ Mov(out, dividend);
2526 } else {
2527 __ rsb(out, dividend, ShifterOperand(0));
2528 }
2529 }
2530}
2531
2532void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2533 DCHECK(instruction->IsDiv() || instruction->IsRem());
2534 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2535
2536 LocationSummary* locations = instruction->GetLocations();
2537 Location second = locations->InAt(1);
2538 DCHECK(second.IsConstant());
2539
2540 Register out = locations->Out().AsRegister<Register>();
2541 Register dividend = locations->InAt(0).AsRegister<Register>();
2542 Register temp = locations->GetTemp(0).AsRegister<Register>();
2543 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002544 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002545 DCHECK(IsPowerOfTwo(abs_imm));
2546 int ctz_imm = CTZ(abs_imm);
2547
2548 if (ctz_imm == 1) {
2549 __ Lsr(temp, dividend, 32 - ctz_imm);
2550 } else {
2551 __ Asr(temp, dividend, 31);
2552 __ Lsr(temp, temp, 32 - ctz_imm);
2553 }
2554 __ add(out, temp, ShifterOperand(dividend));
2555
2556 if (instruction->IsDiv()) {
2557 __ Asr(out, out, ctz_imm);
2558 if (imm < 0) {
2559 __ rsb(out, out, ShifterOperand(0));
2560 }
2561 } else {
2562 __ ubfx(out, out, 0, ctz_imm);
2563 __ sub(out, out, ShifterOperand(temp));
2564 }
2565}
2566
2567void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2568 DCHECK(instruction->IsDiv() || instruction->IsRem());
2569 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2570
2571 LocationSummary* locations = instruction->GetLocations();
2572 Location second = locations->InAt(1);
2573 DCHECK(second.IsConstant());
2574
2575 Register out = locations->Out().AsRegister<Register>();
2576 Register dividend = locations->InAt(0).AsRegister<Register>();
2577 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2578 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2579 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2580
2581 int64_t magic;
2582 int shift;
2583 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2584
2585 __ LoadImmediate(temp1, magic);
2586 __ smull(temp2, temp1, dividend, temp1);
2587
2588 if (imm > 0 && magic < 0) {
2589 __ add(temp1, temp1, ShifterOperand(dividend));
2590 } else if (imm < 0 && magic > 0) {
2591 __ sub(temp1, temp1, ShifterOperand(dividend));
2592 }
2593
2594 if (shift != 0) {
2595 __ Asr(temp1, temp1, shift);
2596 }
2597
2598 if (instruction->IsDiv()) {
2599 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2600 } else {
2601 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2602 // TODO: Strength reduction for mls.
2603 __ LoadImmediate(temp2, imm);
2604 __ mls(out, temp1, temp2, dividend);
2605 }
2606}
2607
2608void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2609 DCHECK(instruction->IsDiv() || instruction->IsRem());
2610 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2611
2612 LocationSummary* locations = instruction->GetLocations();
2613 Location second = locations->InAt(1);
2614 DCHECK(second.IsConstant());
2615
2616 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2617 if (imm == 0) {
2618 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2619 } else if (imm == 1 || imm == -1) {
2620 DivRemOneOrMinusOne(instruction);
2621 } else if (IsPowerOfTwo(std::abs(imm))) {
2622 DivRemByPowerOfTwo(instruction);
2623 } else {
2624 DCHECK(imm <= -2 || imm >= 2);
2625 GenerateDivRemWithAnyConstant(instruction);
2626 }
2627}
2628
Calin Juravle7c4954d2014-10-28 16:57:40 +00002629void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002630 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2631 if (div->GetResultType() == Primitive::kPrimLong) {
2632 // pLdiv runtime call.
2633 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002634 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2635 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002636 } else if (div->GetResultType() == Primitive::kPrimInt &&
2637 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2638 // pIdivmod runtime call.
2639 call_kind = LocationSummary::kCall;
2640 }
2641
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2643
Calin Juravle7c4954d2014-10-28 16:57:40 +00002644 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002645 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002646 if (div->InputAt(1)->IsConstant()) {
2647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2650 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2651 if (abs_imm <= 1) {
2652 // No temp register required.
2653 } else {
2654 locations->AddTemp(Location::RequiresRegister());
2655 if (!IsPowerOfTwo(abs_imm)) {
2656 locations->AddTemp(Location::RequiresRegister());
2657 }
2658 }
2659 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002660 locations->SetInAt(0, Location::RequiresRegister());
2661 locations->SetInAt(1, Location::RequiresRegister());
2662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2663 } else {
2664 InvokeRuntimeCallingConvention calling_convention;
2665 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2666 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2667 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2668 // we only need the former.
2669 locations->SetOut(Location::RegisterLocation(R0));
2670 }
Calin Juravled0d48522014-11-04 16:40:20 +00002671 break;
2672 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002673 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002674 InvokeRuntimeCallingConvention calling_convention;
2675 locations->SetInAt(0, Location::RegisterPairLocation(
2676 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2677 locations->SetInAt(1, Location::RegisterPairLocation(
2678 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002679 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002680 break;
2681 }
2682 case Primitive::kPrimFloat:
2683 case Primitive::kPrimDouble: {
2684 locations->SetInAt(0, Location::RequiresFpuRegister());
2685 locations->SetInAt(1, Location::RequiresFpuRegister());
2686 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2687 break;
2688 }
2689
2690 default:
2691 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2692 }
2693}
2694
2695void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2696 LocationSummary* locations = div->GetLocations();
2697 Location out = locations->Out();
2698 Location first = locations->InAt(0);
2699 Location second = locations->InAt(1);
2700
2701 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002702 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002703 if (second.IsConstant()) {
2704 GenerateDivRemConstantIntegral(div);
2705 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002706 __ sdiv(out.AsRegister<Register>(),
2707 first.AsRegister<Register>(),
2708 second.AsRegister<Register>());
2709 } else {
2710 InvokeRuntimeCallingConvention calling_convention;
2711 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2712 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2713 DCHECK_EQ(R0, out.AsRegister<Register>());
2714
2715 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2716 }
Calin Juravled0d48522014-11-04 16:40:20 +00002717 break;
2718 }
2719
Calin Juravle7c4954d2014-10-28 16:57:40 +00002720 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002721 InvokeRuntimeCallingConvention calling_convention;
2722 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2723 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2724 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2725 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2726 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002727 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002728
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002729 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002730 break;
2731 }
2732
2733 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002734 __ vdivs(out.AsFpuRegister<SRegister>(),
2735 first.AsFpuRegister<SRegister>(),
2736 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002737 break;
2738 }
2739
2740 case Primitive::kPrimDouble: {
2741 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2742 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2743 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2744 break;
2745 }
2746
2747 default:
2748 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2749 }
2750}
2751
Calin Juravlebacfec32014-11-14 15:54:36 +00002752void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002753 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002754
2755 // Most remainders are implemented in the runtime.
2756 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002757 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2758 // sdiv will be replaced by other instruction sequence.
2759 call_kind = LocationSummary::kNoCall;
2760 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2761 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002762 // Have hardware divide instruction for int, do it with three instructions.
2763 call_kind = LocationSummary::kNoCall;
2764 }
2765
Calin Juravlebacfec32014-11-14 15:54:36 +00002766 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2767
Calin Juravled2ec87d2014-12-08 14:24:46 +00002768 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002769 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002770 if (rem->InputAt(1)->IsConstant()) {
2771 locations->SetInAt(0, Location::RequiresRegister());
2772 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2774 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2775 if (abs_imm <= 1) {
2776 // No temp register required.
2777 } else {
2778 locations->AddTemp(Location::RequiresRegister());
2779 if (!IsPowerOfTwo(abs_imm)) {
2780 locations->AddTemp(Location::RequiresRegister());
2781 }
2782 }
2783 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002784 locations->SetInAt(0, Location::RequiresRegister());
2785 locations->SetInAt(1, Location::RequiresRegister());
2786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2787 locations->AddTemp(Location::RequiresRegister());
2788 } else {
2789 InvokeRuntimeCallingConvention calling_convention;
2790 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2791 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2792 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2793 // we only need the latter.
2794 locations->SetOut(Location::RegisterLocation(R1));
2795 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002796 break;
2797 }
2798 case Primitive::kPrimLong: {
2799 InvokeRuntimeCallingConvention calling_convention;
2800 locations->SetInAt(0, Location::RegisterPairLocation(
2801 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2802 locations->SetInAt(1, Location::RegisterPairLocation(
2803 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2804 // The runtime helper puts the output in R2,R3.
2805 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2806 break;
2807 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002808 case Primitive::kPrimFloat: {
2809 InvokeRuntimeCallingConvention calling_convention;
2810 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2811 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2812 locations->SetOut(Location::FpuRegisterLocation(S0));
2813 break;
2814 }
2815
Calin Juravlebacfec32014-11-14 15:54:36 +00002816 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002817 InvokeRuntimeCallingConvention calling_convention;
2818 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2819 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2820 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2821 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2822 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002823 break;
2824 }
2825
2826 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002827 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002828 }
2829}
2830
2831void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2832 LocationSummary* locations = rem->GetLocations();
2833 Location out = locations->Out();
2834 Location first = locations->InAt(0);
2835 Location second = locations->InAt(1);
2836
Calin Juravled2ec87d2014-12-08 14:24:46 +00002837 Primitive::Type type = rem->GetResultType();
2838 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002839 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002840 if (second.IsConstant()) {
2841 GenerateDivRemConstantIntegral(rem);
2842 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002843 Register reg1 = first.AsRegister<Register>();
2844 Register reg2 = second.AsRegister<Register>();
2845 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002846
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002847 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002848 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002849 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002850 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002851 } else {
2852 InvokeRuntimeCallingConvention calling_convention;
2853 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2854 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2855 DCHECK_EQ(R1, out.AsRegister<Register>());
2856
2857 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2858 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002859 break;
2860 }
2861
2862 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002863 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002864 break;
2865 }
2866
Calin Juravled2ec87d2014-12-08 14:24:46 +00002867 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002868 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002869 break;
2870 }
2871
Calin Juravlebacfec32014-11-14 15:54:36 +00002872 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002873 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002874 break;
2875 }
2876
2877 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002878 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002879 }
2880}
2881
Calin Juravled0d48522014-11-04 16:40:20 +00002882void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002883 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2884 ? LocationSummary::kCallOnSlowPath
2885 : LocationSummary::kNoCall;
2886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002887 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002888 if (instruction->HasUses()) {
2889 locations->SetOut(Location::SameAsFirstInput());
2890 }
2891}
2892
2893void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002894 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00002895 codegen_->AddSlowPath(slow_path);
2896
2897 LocationSummary* locations = instruction->GetLocations();
2898 Location value = locations->InAt(0);
2899
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002900 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002901 case Primitive::kPrimByte:
2902 case Primitive::kPrimChar:
2903 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002904 case Primitive::kPrimInt: {
2905 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002906 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002907 } else {
2908 DCHECK(value.IsConstant()) << value;
2909 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2910 __ b(slow_path->GetEntryLabel());
2911 }
2912 }
2913 break;
2914 }
2915 case Primitive::kPrimLong: {
2916 if (value.IsRegisterPair()) {
2917 __ orrs(IP,
2918 value.AsRegisterPairLow<Register>(),
2919 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2920 __ b(slow_path->GetEntryLabel(), EQ);
2921 } else {
2922 DCHECK(value.IsConstant()) << value;
2923 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2924 __ b(slow_path->GetEntryLabel());
2925 }
2926 }
2927 break;
2928 default:
2929 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2930 }
2931 }
Calin Juravled0d48522014-11-04 16:40:20 +00002932}
2933
Calin Juravle9aec02f2014-11-18 23:06:35 +00002934void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2935 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2936
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002937 LocationSummary* locations =
2938 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002939
2940 switch (op->GetResultType()) {
2941 case Primitive::kPrimInt: {
2942 locations->SetInAt(0, Location::RequiresRegister());
2943 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002944 // Make the output overlap, as it will be used to hold the masked
2945 // second input.
2946 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002947 break;
2948 }
2949 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetInAt(1, Location::RequiresRegister());
2952 locations->AddTemp(Location::RequiresRegister());
2953 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002954 break;
2955 }
2956 default:
2957 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2958 }
2959}
2960
2961void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2962 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2963
2964 LocationSummary* locations = op->GetLocations();
2965 Location out = locations->Out();
2966 Location first = locations->InAt(0);
2967 Location second = locations->InAt(1);
2968
2969 Primitive::Type type = op->GetResultType();
2970 switch (type) {
2971 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002972 Register out_reg = out.AsRegister<Register>();
2973 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002974 // Arm doesn't mask the shift count so we need to do it ourselves.
2975 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002976 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002977 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002978 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002979 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002980 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002981 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002982 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002983 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002984 }
2985 } else {
2986 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2987 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2988 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2989 __ Mov(out_reg, first_reg);
2990 } else if (op->IsShl()) {
2991 __ Lsl(out_reg, first_reg, shift_value);
2992 } else if (op->IsShr()) {
2993 __ Asr(out_reg, first_reg, shift_value);
2994 } else {
2995 __ Lsr(out_reg, first_reg, shift_value);
2996 }
2997 }
2998 break;
2999 }
3000 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003001 Register o_h = out.AsRegisterPairHigh<Register>();
3002 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003003
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003004 Register temp = locations->GetTemp(0).AsRegister<Register>();
3005
3006 Register high = first.AsRegisterPairHigh<Register>();
3007 Register low = first.AsRegisterPairLow<Register>();
3008
3009 Register second_reg = second.AsRegister<Register>();
3010
Calin Juravle9aec02f2014-11-18 23:06:35 +00003011 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003012 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003013 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003014 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003015 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003016 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003017 __ Lsr(temp, low, temp);
3018 __ orr(o_h, o_h, ShifterOperand(temp));
3019 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003020 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003021 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003022 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003023 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003024 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003025 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003026 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003027 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003028 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003029 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003030 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003031 __ Lsl(temp, high, temp);
3032 __ orr(o_l, o_l, ShifterOperand(temp));
3033 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003034 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003035 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003036 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003037 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003038 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003039 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003040 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003041 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003042 __ Lsr(o_l, low, o_h);
3043 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003044 __ Lsl(temp, high, temp);
3045 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003046 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003047 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003048 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003049 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003050 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003051 break;
3052 }
3053 default:
3054 LOG(FATAL) << "Unexpected operation type " << type;
3055 }
3056}
3057
3058void LocationsBuilderARM::VisitShl(HShl* shl) {
3059 HandleShift(shl);
3060}
3061
3062void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3063 HandleShift(shl);
3064}
3065
3066void LocationsBuilderARM::VisitShr(HShr* shr) {
3067 HandleShift(shr);
3068}
3069
3070void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3071 HandleShift(shr);
3072}
3073
3074void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3075 HandleShift(ushr);
3076}
3077
3078void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3079 HandleShift(ushr);
3080}
3081
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003082void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003083 LocationSummary* locations =
3084 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003085 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003086 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003087 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003088 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003089}
3090
3091void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3092 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003093 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003094 // Note: if heap poisoning is enabled, the entry point takes cares
3095 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003096 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003097 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003098 instruction->GetDexPc(),
3099 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003100}
3101
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003102void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3103 LocationSummary* locations =
3104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3105 InvokeRuntimeCallingConvention calling_convention;
3106 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003107 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003108 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003109 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003110}
3111
3112void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3113 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003114 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003115 // Note: if heap poisoning is enabled, the entry point takes cares
3116 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003117 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003118 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003119 instruction->GetDexPc(),
3120 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003121}
3122
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003123void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003124 LocationSummary* locations =
3125 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003126 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3127 if (location.IsStackSlot()) {
3128 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3129 } else if (location.IsDoubleStackSlot()) {
3130 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003131 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003132 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003133}
3134
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003135void InstructionCodeGeneratorARM::VisitParameterValue(
3136 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003137 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003138}
3139
3140void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3141 LocationSummary* locations =
3142 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3143 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3144}
3145
3146void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3147 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003148}
3149
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003150void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003151 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003152 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003153 locations->SetInAt(0, Location::RequiresRegister());
3154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003155}
3156
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003157void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3158 LocationSummary* locations = not_->GetLocations();
3159 Location out = locations->Out();
3160 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003161 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003162 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003163 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003164 break;
3165
3166 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003167 __ mvn(out.AsRegisterPairLow<Register>(),
3168 ShifterOperand(in.AsRegisterPairLow<Register>()));
3169 __ mvn(out.AsRegisterPairHigh<Register>(),
3170 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003171 break;
3172
3173 default:
3174 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3175 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003176}
3177
David Brazdil66d126e2015-04-03 16:02:44 +01003178void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3179 LocationSummary* locations =
3180 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3181 locations->SetInAt(0, Location::RequiresRegister());
3182 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3183}
3184
3185void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003186 LocationSummary* locations = bool_not->GetLocations();
3187 Location out = locations->Out();
3188 Location in = locations->InAt(0);
3189 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3190}
3191
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003192void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003193 LocationSummary* locations =
3194 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003195 switch (compare->InputAt(0)->GetType()) {
3196 case Primitive::kPrimLong: {
3197 locations->SetInAt(0, Location::RequiresRegister());
3198 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003199 // Output overlaps because it is written before doing the low comparison.
3200 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003201 break;
3202 }
3203 case Primitive::kPrimFloat:
3204 case Primitive::kPrimDouble: {
3205 locations->SetInAt(0, Location::RequiresFpuRegister());
3206 locations->SetInAt(1, Location::RequiresFpuRegister());
3207 locations->SetOut(Location::RequiresRegister());
3208 break;
3209 }
3210 default:
3211 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3212 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003213}
3214
3215void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003216 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003218 Location left = locations->InAt(0);
3219 Location right = locations->InAt(1);
3220
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003221 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003222 Primitive::Type type = compare->InputAt(0)->GetType();
3223 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003224 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003225 __ cmp(left.AsRegisterPairHigh<Register>(),
3226 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003227 __ b(&less, LT);
3228 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003229 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003230 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003231 __ cmp(left.AsRegisterPairLow<Register>(),
3232 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003233 break;
3234 }
3235 case Primitive::kPrimFloat:
3236 case Primitive::kPrimDouble: {
3237 __ LoadImmediate(out, 0);
3238 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003239 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003240 } else {
3241 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3242 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3243 }
3244 __ vmstat(); // transfer FP status register to ARM APSR.
3245 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003246 break;
3247 }
3248 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003249 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003250 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003251 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003252 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003253
3254 __ Bind(&greater);
3255 __ LoadImmediate(out, 1);
3256 __ b(&done);
3257
3258 __ Bind(&less);
3259 __ LoadImmediate(out, -1);
3260
3261 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003262}
3263
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003264void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003265 LocationSummary* locations =
3266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003267 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3268 locations->SetInAt(i, Location::Any());
3269 }
3270 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003271}
3272
3273void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003274 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003275 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003276}
3277
Calin Juravle52c48962014-12-16 17:02:57 +00003278void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3279 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003280 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003281 switch (kind) {
3282 case MemBarrierKind::kAnyStore:
3283 case MemBarrierKind::kLoadAny:
3284 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003285 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003286 break;
3287 }
3288 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003289 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003290 break;
3291 }
3292 default:
3293 LOG(FATAL) << "Unexpected memory barrier " << kind;
3294 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003295 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003296}
3297
3298void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3299 uint32_t offset,
3300 Register out_lo,
3301 Register out_hi) {
3302 if (offset != 0) {
3303 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003304 __ add(IP, addr, ShifterOperand(out_lo));
3305 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003306 }
3307 __ ldrexd(out_lo, out_hi, addr);
3308}
3309
3310void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3311 uint32_t offset,
3312 Register value_lo,
3313 Register value_hi,
3314 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003315 Register temp2,
3316 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003317 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003318 if (offset != 0) {
3319 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003320 __ add(IP, addr, ShifterOperand(temp1));
3321 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003322 }
3323 __ Bind(&fail);
3324 // We need a load followed by store. (The address used in a STREX instruction must
3325 // be the same as the address in the most recently executed LDREX instruction.)
3326 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003327 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003328 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003329 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003330}
3331
3332void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3333 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3334
Nicolas Geoffray39468442014-09-02 15:17:15 +01003335 LocationSummary* locations =
3336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003337 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003338
Calin Juravle52c48962014-12-16 17:02:57 +00003339 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003340 if (Primitive::IsFloatingPointType(field_type)) {
3341 locations->SetInAt(1, Location::RequiresFpuRegister());
3342 } else {
3343 locations->SetInAt(1, Location::RequiresRegister());
3344 }
3345
Calin Juravle52c48962014-12-16 17:02:57 +00003346 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003347 bool generate_volatile = field_info.IsVolatile()
3348 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003349 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003350 bool needs_write_barrier =
3351 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003352 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003353 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003354 if (needs_write_barrier) {
3355 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003356 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003357 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003358 // Arm encoding have some additional constraints for ldrexd/strexd:
3359 // - registers need to be consecutive
3360 // - the first register should be even but not R14.
3361 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3362 // enable Arm encoding.
3363 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3364
3365 locations->AddTemp(Location::RequiresRegister());
3366 locations->AddTemp(Location::RequiresRegister());
3367 if (field_type == Primitive::kPrimDouble) {
3368 // For doubles we need two more registers to copy the value.
3369 locations->AddTemp(Location::RegisterLocation(R2));
3370 locations->AddTemp(Location::RegisterLocation(R3));
3371 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003372 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003373}
3374
Calin Juravle52c48962014-12-16 17:02:57 +00003375void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003376 const FieldInfo& field_info,
3377 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003378 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3379
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003380 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003381 Register base = locations->InAt(0).AsRegister<Register>();
3382 Location value = locations->InAt(1);
3383
3384 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003385 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003386 Primitive::Type field_type = field_info.GetFieldType();
3387 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003388 bool needs_write_barrier =
3389 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003390
3391 if (is_volatile) {
3392 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3393 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003394
3395 switch (field_type) {
3396 case Primitive::kPrimBoolean:
3397 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003398 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003399 break;
3400 }
3401
3402 case Primitive::kPrimShort:
3403 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003404 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003405 break;
3406 }
3407
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003408 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003409 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003410 if (kPoisonHeapReferences && needs_write_barrier) {
3411 // Note that in the case where `value` is a null reference,
3412 // we do not enter this block, as a null reference does not
3413 // need poisoning.
3414 DCHECK_EQ(field_type, Primitive::kPrimNot);
3415 Register temp = locations->GetTemp(0).AsRegister<Register>();
3416 __ Mov(temp, value.AsRegister<Register>());
3417 __ PoisonHeapReference(temp);
3418 __ StoreToOffset(kStoreWord, temp, base, offset);
3419 } else {
3420 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3421 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003422 break;
3423 }
3424
3425 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003426 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003427 GenerateWideAtomicStore(base, offset,
3428 value.AsRegisterPairLow<Register>(),
3429 value.AsRegisterPairHigh<Register>(),
3430 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003431 locations->GetTemp(1).AsRegister<Register>(),
3432 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003433 } else {
3434 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003435 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003436 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003437 break;
3438 }
3439
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003440 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003441 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003442 break;
3443 }
3444
3445 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003446 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003447 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003448 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3449 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3450
3451 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3452
3453 GenerateWideAtomicStore(base, offset,
3454 value_reg_lo,
3455 value_reg_hi,
3456 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003457 locations->GetTemp(3).AsRegister<Register>(),
3458 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003459 } else {
3460 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003461 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003462 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003463 break;
3464 }
3465
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003466 case Primitive::kPrimVoid:
3467 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003468 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003469 }
Calin Juravle52c48962014-12-16 17:02:57 +00003470
Calin Juravle77520bc2015-01-12 18:45:46 +00003471 // Longs and doubles are handled in the switch.
3472 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3473 codegen_->MaybeRecordImplicitNullCheck(instruction);
3474 }
3475
3476 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3477 Register temp = locations->GetTemp(0).AsRegister<Register>();
3478 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003479 codegen_->MarkGCCard(
3480 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003481 }
3482
Calin Juravle52c48962014-12-16 17:02:57 +00003483 if (is_volatile) {
3484 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3485 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003486}
3487
Calin Juravle52c48962014-12-16 17:02:57 +00003488void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3489 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003490 LocationSummary* locations =
3491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003492 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003493
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003494 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003495 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003496 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003497 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003498
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003499 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3500 locations->SetOut(Location::RequiresFpuRegister());
3501 } else {
3502 locations->SetOut(Location::RequiresRegister(),
3503 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3504 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003505 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003506 // Arm encoding have some additional constraints for ldrexd/strexd:
3507 // - registers need to be consecutive
3508 // - the first register should be even but not R14.
3509 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3510 // enable Arm encoding.
3511 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3512 locations->AddTemp(Location::RequiresRegister());
3513 locations->AddTemp(Location::RequiresRegister());
3514 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003515}
3516
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003517Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3518 Opcode opcode) {
3519 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3520 if (constant->IsConstant() &&
3521 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3522 return Location::ConstantLocation(constant->AsConstant());
3523 }
3524 return Location::RequiresRegister();
3525}
3526
3527bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3528 Opcode opcode) {
3529 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3530 if (Primitive::Is64BitType(input_cst->GetType())) {
3531 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3532 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3533 } else {
3534 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3535 }
3536}
3537
3538bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3539 ShifterOperand so;
3540 ArmAssembler* assembler = codegen_->GetAssembler();
3541 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3542 return true;
3543 }
3544 Opcode neg_opcode = kNoOperand;
3545 switch (opcode) {
3546 case AND:
3547 neg_opcode = BIC;
3548 break;
3549 case ORR:
3550 neg_opcode = ORN;
3551 break;
3552 default:
3553 return false;
3554 }
3555 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
3556}
3557
Calin Juravle52c48962014-12-16 17:02:57 +00003558void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3559 const FieldInfo& field_info) {
3560 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003561
Calin Juravle52c48962014-12-16 17:02:57 +00003562 LocationSummary* locations = instruction->GetLocations();
3563 Register base = locations->InAt(0).AsRegister<Register>();
3564 Location out = locations->Out();
3565 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003566 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003567 Primitive::Type field_type = field_info.GetFieldType();
3568 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3569
3570 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003571 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003572 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003573 break;
3574 }
3575
3576 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003577 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003578 break;
3579 }
3580
3581 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003582 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003583 break;
3584 }
3585
3586 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003587 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003588 break;
3589 }
3590
3591 case Primitive::kPrimInt:
3592 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003593 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003594 break;
3595 }
3596
3597 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003598 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003599 GenerateWideAtomicLoad(base, offset,
3600 out.AsRegisterPairLow<Register>(),
3601 out.AsRegisterPairHigh<Register>());
3602 } else {
3603 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3604 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003605 break;
3606 }
3607
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003608 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003609 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003610 break;
3611 }
3612
3613 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003614 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003615 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003616 Register lo = locations->GetTemp(0).AsRegister<Register>();
3617 Register hi = locations->GetTemp(1).AsRegister<Register>();
3618 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003619 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003620 __ vmovdrr(out_reg, lo, hi);
3621 } else {
3622 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003623 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003624 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003625 break;
3626 }
3627
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003628 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003629 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003630 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003631 }
Calin Juravle52c48962014-12-16 17:02:57 +00003632
Calin Juravle77520bc2015-01-12 18:45:46 +00003633 // Doubles are handled in the switch.
3634 if (field_type != Primitive::kPrimDouble) {
3635 codegen_->MaybeRecordImplicitNullCheck(instruction);
3636 }
3637
Calin Juravle52c48962014-12-16 17:02:57 +00003638 if (is_volatile) {
3639 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3640 }
Roland Levillain4d027112015-07-01 15:41:14 +01003641
3642 if (field_type == Primitive::kPrimNot) {
3643 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3644 }
Calin Juravle52c48962014-12-16 17:02:57 +00003645}
3646
3647void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3648 HandleFieldSet(instruction, instruction->GetFieldInfo());
3649}
3650
3651void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003652 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003653}
3654
3655void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3656 HandleFieldGet(instruction, instruction->GetFieldInfo());
3657}
3658
3659void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3660 HandleFieldGet(instruction, instruction->GetFieldInfo());
3661}
3662
3663void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3664 HandleFieldGet(instruction, instruction->GetFieldInfo());
3665}
3666
3667void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3668 HandleFieldGet(instruction, instruction->GetFieldInfo());
3669}
3670
3671void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3672 HandleFieldSet(instruction, instruction->GetFieldInfo());
3673}
3674
3675void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003676 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003677}
3678
Calin Juravlee460d1d2015-09-29 04:52:17 +01003679void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
3680 HUnresolvedInstanceFieldGet* instruction) {
3681 FieldAccessCallingConventionARM calling_convention;
3682 codegen_->CreateUnresolvedFieldLocationSummary(
3683 instruction, instruction->GetFieldType(), calling_convention);
3684}
3685
3686void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
3687 HUnresolvedInstanceFieldGet* instruction) {
3688 FieldAccessCallingConventionARM calling_convention;
3689 codegen_->GenerateUnresolvedFieldAccess(instruction,
3690 instruction->GetFieldType(),
3691 instruction->GetFieldIndex(),
3692 instruction->GetDexPc(),
3693 calling_convention);
3694}
3695
3696void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
3697 HUnresolvedInstanceFieldSet* instruction) {
3698 FieldAccessCallingConventionARM calling_convention;
3699 codegen_->CreateUnresolvedFieldLocationSummary(
3700 instruction, instruction->GetFieldType(), calling_convention);
3701}
3702
3703void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
3704 HUnresolvedInstanceFieldSet* instruction) {
3705 FieldAccessCallingConventionARM calling_convention;
3706 codegen_->GenerateUnresolvedFieldAccess(instruction,
3707 instruction->GetFieldType(),
3708 instruction->GetFieldIndex(),
3709 instruction->GetDexPc(),
3710 calling_convention);
3711}
3712
3713void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
3714 HUnresolvedStaticFieldGet* instruction) {
3715 FieldAccessCallingConventionARM calling_convention;
3716 codegen_->CreateUnresolvedFieldLocationSummary(
3717 instruction, instruction->GetFieldType(), calling_convention);
3718}
3719
3720void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
3721 HUnresolvedStaticFieldGet* instruction) {
3722 FieldAccessCallingConventionARM calling_convention;
3723 codegen_->GenerateUnresolvedFieldAccess(instruction,
3724 instruction->GetFieldType(),
3725 instruction->GetFieldIndex(),
3726 instruction->GetDexPc(),
3727 calling_convention);
3728}
3729
3730void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
3731 HUnresolvedStaticFieldSet* instruction) {
3732 FieldAccessCallingConventionARM calling_convention;
3733 codegen_->CreateUnresolvedFieldLocationSummary(
3734 instruction, instruction->GetFieldType(), calling_convention);
3735}
3736
3737void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
3738 HUnresolvedStaticFieldSet* instruction) {
3739 FieldAccessCallingConventionARM calling_convention;
3740 codegen_->GenerateUnresolvedFieldAccess(instruction,
3741 instruction->GetFieldType(),
3742 instruction->GetFieldIndex(),
3743 instruction->GetDexPc(),
3744 calling_convention);
3745}
3746
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003747void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003748 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3749 ? LocationSummary::kCallOnSlowPath
3750 : LocationSummary::kNoCall;
3751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003752 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003753 if (instruction->HasUses()) {
3754 locations->SetOut(Location::SameAsFirstInput());
3755 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003756}
3757
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003758void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003759 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3760 return;
3761 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003762 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003763
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003764 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3765 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3766}
3767
3768void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003769 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003770 codegen_->AddSlowPath(slow_path);
3771
3772 LocationSummary* locations = instruction->GetLocations();
3773 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003774
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003775 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003776}
3777
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003778void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003779 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003780 GenerateImplicitNullCheck(instruction);
3781 } else {
3782 GenerateExplicitNullCheck(instruction);
3783 }
3784}
3785
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003786void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003787 LocationSummary* locations =
3788 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003789 locations->SetInAt(0, Location::RequiresRegister());
3790 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003791 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3792 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3793 } else {
3794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3795 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003796}
3797
3798void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3799 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003800 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003801 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003802 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003803
Roland Levillain4d027112015-07-01 15:41:14 +01003804 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003805 case Primitive::kPrimBoolean: {
3806 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003807 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003808 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003809 size_t offset =
3810 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003811 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3812 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003813 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003814 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3815 }
3816 break;
3817 }
3818
3819 case Primitive::kPrimByte: {
3820 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003821 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003822 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003823 size_t offset =
3824 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003825 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3826 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003827 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003828 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3829 }
3830 break;
3831 }
3832
3833 case Primitive::kPrimShort: {
3834 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003836 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003837 size_t offset =
3838 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003839 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3840 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003841 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003842 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3843 }
3844 break;
3845 }
3846
3847 case Primitive::kPrimChar: {
3848 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003849 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003850 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003851 size_t offset =
3852 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003853 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3854 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003855 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003856 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3857 }
3858 break;
3859 }
3860
3861 case Primitive::kPrimInt:
3862 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003863 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3864 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003866 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003867 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003868 size_t offset =
3869 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003870 __ LoadFromOffset(kLoadWord, out, obj, offset);
3871 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003872 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003873 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3874 }
3875 break;
3876 }
3877
3878 case Primitive::kPrimLong: {
3879 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003880 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003881 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003882 size_t offset =
3883 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003884 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003885 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003886 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003887 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003888 }
3889 break;
3890 }
3891
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003892 case Primitive::kPrimFloat: {
3893 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3894 Location out = locations->Out();
3895 DCHECK(out.IsFpuRegister());
3896 if (index.IsConstant()) {
3897 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3898 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3899 } else {
3900 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3901 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3902 }
3903 break;
3904 }
3905
3906 case Primitive::kPrimDouble: {
3907 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3908 Location out = locations->Out();
3909 DCHECK(out.IsFpuRegisterPair());
3910 if (index.IsConstant()) {
3911 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3912 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3913 } else {
3914 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3915 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3916 }
3917 break;
3918 }
3919
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003920 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003921 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003922 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003923 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003924 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003925
3926 if (type == Primitive::kPrimNot) {
3927 Register out = locations->Out().AsRegister<Register>();
3928 __ MaybeUnpoisonHeapReference(out);
3929 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003930}
3931
3932void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003933 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003934
3935 bool needs_write_barrier =
3936 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003937 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003938
Nicolas Geoffray39468442014-09-02 15:17:15 +01003939 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003940 instruction,
3941 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
3942 locations->SetInAt(0, Location::RequiresRegister());
3943 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3944 if (Primitive::IsFloatingPointType(value_type)) {
3945 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003946 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003947 locations->SetInAt(2, Location::RequiresRegister());
3948 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003949
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003950 if (needs_write_barrier) {
3951 // Temporary registers for the write barrier.
3952 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
3953 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003954 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003955}
3956
3957void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3958 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003959 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003960 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003961 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003962 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003963 bool needs_write_barrier =
3964 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003965
3966 switch (value_type) {
3967 case Primitive::kPrimBoolean:
3968 case Primitive::kPrimByte: {
3969 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003970 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003971 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003972 size_t offset =
3973 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003974 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003975 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003976 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003977 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3978 }
3979 break;
3980 }
3981
3982 case Primitive::kPrimShort:
3983 case Primitive::kPrimChar: {
3984 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003985 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003986 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003987 size_t offset =
3988 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003989 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003990 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003991 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003992 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3993 }
3994 break;
3995 }
3996
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003997 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003998 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3999 Register value = locations->InAt(2).AsRegister<Register>();
4000 Register source = value;
4001
4002 if (instruction->InputAt(2)->IsNullConstant()) {
4003 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004004 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004005 size_t offset =
4006 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004007 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004008 } else {
4009 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004010 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004011 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004012 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004013 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004014 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004015
4016 DCHECK(needs_write_barrier);
4017 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4018 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4019 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4020 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4021 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4022 Label done;
4023 SlowPathCode* slow_path = nullptr;
4024
4025 if (may_need_runtime_call) {
4026 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4027 codegen_->AddSlowPath(slow_path);
4028 if (instruction->GetValueCanBeNull()) {
4029 Label non_zero;
4030 __ CompareAndBranchIfNonZero(value, &non_zero);
4031 if (index.IsConstant()) {
4032 size_t offset =
4033 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4034 __ StoreToOffset(kStoreWord, value, array, offset);
4035 } else {
4036 DCHECK(index.IsRegister()) << index;
4037 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4038 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4039 }
4040 codegen_->MaybeRecordImplicitNullCheck(instruction);
4041 __ b(&done);
4042 __ Bind(&non_zero);
4043 }
4044
4045 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4046 codegen_->MaybeRecordImplicitNullCheck(instruction);
4047 __ MaybeUnpoisonHeapReference(temp1);
4048 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4049 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4050 // No need to poison/unpoison, we're comparing two poisoined references.
4051 __ cmp(temp1, ShifterOperand(temp2));
4052 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4053 Label do_put;
4054 __ b(&do_put, EQ);
4055 __ MaybeUnpoisonHeapReference(temp1);
4056 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4057 // No need to poison/unpoison, we're comparing against null.
4058 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4059 __ Bind(&do_put);
4060 } else {
4061 __ b(slow_path->GetEntryLabel(), NE);
4062 }
4063 }
4064
4065 if (kPoisonHeapReferences) {
4066 // Note that in the case where `value` is a null reference,
4067 // we do not enter this block, as a null reference does not
4068 // need poisoning.
4069 DCHECK_EQ(value_type, Primitive::kPrimNot);
4070 __ Mov(temp1, value);
4071 __ PoisonHeapReference(temp1);
4072 source = temp1;
4073 }
4074
4075 if (index.IsConstant()) {
4076 size_t offset =
4077 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4078 __ StoreToOffset(kStoreWord, source, array, offset);
4079 } else {
4080 DCHECK(index.IsRegister()) << index;
4081 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4082 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4083 }
4084
4085 if (!may_need_runtime_call) {
4086 codegen_->MaybeRecordImplicitNullCheck(instruction);
4087 }
4088
4089 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4090
4091 if (done.IsLinked()) {
4092 __ Bind(&done);
4093 }
4094
4095 if (slow_path != nullptr) {
4096 __ Bind(slow_path->GetExitLabel());
4097 }
4098
4099 break;
4100 }
4101
4102 case Primitive::kPrimInt: {
4103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4104 Register value = locations->InAt(2).AsRegister<Register>();
4105 if (index.IsConstant()) {
4106 size_t offset =
4107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4108 __ StoreToOffset(kStoreWord, value, array, offset);
4109 } else {
4110 DCHECK(index.IsRegister()) << index;
4111 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4112 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4113 }
4114
4115 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004116 break;
4117 }
4118
4119 case Primitive::kPrimLong: {
4120 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004121 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004122 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004123 size_t offset =
4124 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004125 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004126 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004127 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004128 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004129 }
4130 break;
4131 }
4132
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004133 case Primitive::kPrimFloat: {
4134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4135 Location value = locations->InAt(2);
4136 DCHECK(value.IsFpuRegister());
4137 if (index.IsConstant()) {
4138 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004139 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004140 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004141 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004142 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4143 }
4144 break;
4145 }
4146
4147 case Primitive::kPrimDouble: {
4148 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4149 Location value = locations->InAt(2);
4150 DCHECK(value.IsFpuRegisterPair());
4151 if (index.IsConstant()) {
4152 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004153 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004154 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004155 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004156 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4157 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004158
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004159 break;
4160 }
4161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004162 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004163 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004164 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004165 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004166
4167 // Ints and objects are handled in the switch.
4168 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4169 codegen_->MaybeRecordImplicitNullCheck(instruction);
4170 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004171}
4172
4173void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004174 LocationSummary* locations =
4175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004176 locations->SetInAt(0, Location::RequiresRegister());
4177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004178}
4179
4180void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4181 LocationSummary* locations = instruction->GetLocations();
4182 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004183 Register obj = locations->InAt(0).AsRegister<Register>();
4184 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004185 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004186 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004187}
4188
4189void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004190 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4191 ? LocationSummary::kCallOnSlowPath
4192 : LocationSummary::kNoCall;
4193 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004194 locations->SetInAt(0, Location::RequiresRegister());
4195 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004196 if (instruction->HasUses()) {
4197 locations->SetOut(Location::SameAsFirstInput());
4198 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004199}
4200
4201void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4202 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004203 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004204 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004205 codegen_->AddSlowPath(slow_path);
4206
Roland Levillain271ab9c2014-11-27 15:23:57 +00004207 Register index = locations->InAt(0).AsRegister<Register>();
4208 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004209
4210 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004211 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004212}
4213
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004214void CodeGeneratorARM::MarkGCCard(Register temp,
4215 Register card,
4216 Register object,
4217 Register value,
4218 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004219 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004220 if (can_be_null) {
4221 __ CompareAndBranchIfZero(value, &is_null);
4222 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004223 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4224 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4225 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004226 if (can_be_null) {
4227 __ Bind(&is_null);
4228 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004229}
4230
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004231void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4232 temp->SetLocations(nullptr);
4233}
4234
4235void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
4236 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004237 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004238}
4239
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004240void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004241 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004242 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004243}
4244
4245void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004246 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4247}
4248
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004249void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4251}
4252
4253void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004254 HBasicBlock* block = instruction->GetBlock();
4255 if (block->GetLoopInformation() != nullptr) {
4256 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4257 // The back edge will generate the suspend check.
4258 return;
4259 }
4260 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4261 // The goto will generate the suspend check.
4262 return;
4263 }
4264 GenerateSuspendCheck(instruction, nullptr);
4265}
4266
4267void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4268 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004269 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004270 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4271 if (slow_path == nullptr) {
4272 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4273 instruction->SetSlowPath(slow_path);
4274 codegen_->AddSlowPath(slow_path);
4275 if (successor != nullptr) {
4276 DCHECK(successor->IsLoopHeader());
4277 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4278 }
4279 } else {
4280 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4281 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004282
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004283 __ LoadFromOffset(
4284 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004285 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004286 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004287 __ Bind(slow_path->GetReturnLabel());
4288 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004289 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004290 __ b(slow_path->GetEntryLabel());
4291 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004292}
4293
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004294ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4295 return codegen_->GetAssembler();
4296}
4297
4298void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004299 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004300 Location source = move->GetSource();
4301 Location destination = move->GetDestination();
4302
4303 if (source.IsRegister()) {
4304 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004305 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004306 } else {
4307 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004308 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004309 SP, destination.GetStackIndex());
4310 }
4311 } else if (source.IsStackSlot()) {
4312 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004313 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004314 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004315 } else if (destination.IsFpuRegister()) {
4316 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004317 } else {
4318 DCHECK(destination.IsStackSlot());
4319 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4320 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4321 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004322 } else if (source.IsFpuRegister()) {
4323 if (destination.IsFpuRegister()) {
4324 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004325 } else {
4326 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004327 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4328 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004329 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004330 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004331 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4332 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004333 } else if (destination.IsRegisterPair()) {
4334 DCHECK(ExpectedPairLayout(destination));
4335 __ LoadFromOffset(
4336 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4337 } else {
4338 DCHECK(destination.IsFpuRegisterPair()) << destination;
4339 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4340 SP,
4341 source.GetStackIndex());
4342 }
4343 } else if (source.IsRegisterPair()) {
4344 if (destination.IsRegisterPair()) {
4345 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4346 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4347 } else {
4348 DCHECK(destination.IsDoubleStackSlot()) << destination;
4349 DCHECK(ExpectedPairLayout(source));
4350 __ StoreToOffset(
4351 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4352 }
4353 } else if (source.IsFpuRegisterPair()) {
4354 if (destination.IsFpuRegisterPair()) {
4355 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4356 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4357 } else {
4358 DCHECK(destination.IsDoubleStackSlot()) << destination;
4359 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4360 SP,
4361 destination.GetStackIndex());
4362 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004363 } else {
4364 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004365 HConstant* constant = source.GetConstant();
4366 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4367 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004368 if (destination.IsRegister()) {
4369 __ LoadImmediate(destination.AsRegister<Register>(), value);
4370 } else {
4371 DCHECK(destination.IsStackSlot());
4372 __ LoadImmediate(IP, value);
4373 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4374 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004375 } else if (constant->IsLongConstant()) {
4376 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004377 if (destination.IsRegisterPair()) {
4378 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4379 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004380 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004381 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004382 __ LoadImmediate(IP, Low32Bits(value));
4383 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4384 __ LoadImmediate(IP, High32Bits(value));
4385 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4386 }
4387 } else if (constant->IsDoubleConstant()) {
4388 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004389 if (destination.IsFpuRegisterPair()) {
4390 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004391 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004392 DCHECK(destination.IsDoubleStackSlot()) << destination;
4393 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004394 __ LoadImmediate(IP, Low32Bits(int_value));
4395 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4396 __ LoadImmediate(IP, High32Bits(int_value));
4397 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4398 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004399 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004400 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004401 float value = constant->AsFloatConstant()->GetValue();
4402 if (destination.IsFpuRegister()) {
4403 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4404 } else {
4405 DCHECK(destination.IsStackSlot());
4406 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4407 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4408 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004409 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004410 }
4411}
4412
4413void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4414 __ Mov(IP, reg);
4415 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4416 __ StoreToOffset(kStoreWord, IP, SP, mem);
4417}
4418
4419void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4420 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4421 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4422 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4423 SP, mem1 + stack_offset);
4424 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4425 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4426 SP, mem2 + stack_offset);
4427 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4428}
4429
4430void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004431 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004432 Location source = move->GetSource();
4433 Location destination = move->GetDestination();
4434
4435 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004436 DCHECK_NE(source.AsRegister<Register>(), IP);
4437 DCHECK_NE(destination.AsRegister<Register>(), IP);
4438 __ Mov(IP, source.AsRegister<Register>());
4439 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4440 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004441 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004442 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004443 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004444 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004445 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4446 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004447 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004448 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004449 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004450 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004451 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004452 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004453 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004454 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004455 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4456 destination.AsRegisterPairHigh<Register>(),
4457 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004458 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004459 Register low_reg = source.IsRegisterPair()
4460 ? source.AsRegisterPairLow<Register>()
4461 : destination.AsRegisterPairLow<Register>();
4462 int mem = source.IsRegisterPair()
4463 ? destination.GetStackIndex()
4464 : source.GetStackIndex();
4465 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004466 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004467 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004468 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004469 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004470 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4471 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004472 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004473 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004474 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004475 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4476 DRegister reg = source.IsFpuRegisterPair()
4477 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4478 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4479 int mem = source.IsFpuRegisterPair()
4480 ? destination.GetStackIndex()
4481 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004482 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004483 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004484 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004485 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4486 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4487 : destination.AsFpuRegister<SRegister>();
4488 int mem = source.IsFpuRegister()
4489 ? destination.GetStackIndex()
4490 : source.GetStackIndex();
4491
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004492 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004493 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004494 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004495 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004496 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4497 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004498 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004499 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004500 }
4501}
4502
4503void ParallelMoveResolverARM::SpillScratch(int reg) {
4504 __ Push(static_cast<Register>(reg));
4505}
4506
4507void ParallelMoveResolverARM::RestoreScratch(int reg) {
4508 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004509}
4510
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004511void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004512 InvokeRuntimeCallingConvention calling_convention;
4513 CodeGenerator::CreateLoadClassLocationSummary(
4514 cls,
4515 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4516 Location::RegisterLocation(R0));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004517}
4518
4519void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004520 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004521 if (cls->NeedsAccessCheck()) {
4522 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4523 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4524 cls,
4525 cls->GetDexPc(),
4526 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004527 return;
4528 }
4529
4530 Register out = locations->Out().AsRegister<Register>();
4531 Register current_method = locations->InAt(0).AsRegister<Register>();
4532 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004533 DCHECK(!cls->CanCallRuntime());
4534 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004535 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004536 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004537 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004538 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004539 __ LoadFromOffset(kLoadWord,
4540 out,
4541 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004542 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004543 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004544 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004545
Andreas Gampe85b62f22015-09-09 13:15:38 -07004546 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004547 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4548 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004549 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004550 if (cls->MustGenerateClinitCheck()) {
4551 GenerateClassInitializationCheck(slow_path, out);
4552 } else {
4553 __ Bind(slow_path->GetExitLabel());
4554 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004555 }
4556}
4557
4558void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4559 LocationSummary* locations =
4560 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4561 locations->SetInAt(0, Location::RequiresRegister());
4562 if (check->HasUses()) {
4563 locations->SetOut(Location::SameAsFirstInput());
4564 }
4565}
4566
4567void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004568 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004569 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004570 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004571 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004572 GenerateClassInitializationCheck(slow_path,
4573 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004574}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004575
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004576void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004577 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004578 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4579 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4580 __ b(slow_path->GetEntryLabel(), LT);
4581 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4582 // properly. Therefore, we do a memory fence.
4583 __ dmb(ISH);
4584 __ Bind(slow_path->GetExitLabel());
4585}
4586
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004587void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4588 LocationSummary* locations =
4589 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004590 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004591 locations->SetOut(Location::RequiresRegister());
4592}
4593
4594void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004595 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004596 codegen_->AddSlowPath(slow_path);
4597
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004598 LocationSummary* locations = load->GetLocations();
4599 Register out = locations->Out().AsRegister<Register>();
4600 Register current_method = locations->InAt(0).AsRegister<Register>();
4601 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004602 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004603 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004604 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004605 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004606 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004607 __ Bind(slow_path->GetExitLabel());
4608}
4609
David Brazdilcb1c0552015-08-04 16:22:25 +01004610static int32_t GetExceptionTlsOffset() {
4611 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4612}
4613
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004614void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4615 LocationSummary* locations =
4616 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4617 locations->SetOut(Location::RequiresRegister());
4618}
4619
4620void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004621 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004622 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4623}
4624
4625void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4626 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4627}
4628
4629void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004630 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004631 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004632}
4633
4634void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4635 LocationSummary* locations =
4636 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4637 InvokeRuntimeCallingConvention calling_convention;
4638 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4639}
4640
4641void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4642 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004643 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004644}
4645
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004646void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004647 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4648 switch (instruction->GetTypeCheckKind()) {
4649 case TypeCheckKind::kExactCheck:
4650 case TypeCheckKind::kAbstractClassCheck:
4651 case TypeCheckKind::kClassHierarchyCheck:
4652 case TypeCheckKind::kArrayObjectCheck:
4653 call_kind = LocationSummary::kNoCall;
4654 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004655 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004656 case TypeCheckKind::kInterfaceCheck:
4657 call_kind = LocationSummary::kCall;
4658 break;
4659 case TypeCheckKind::kArrayCheck:
4660 call_kind = LocationSummary::kCallOnSlowPath;
4661 break;
4662 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004664 if (call_kind != LocationSummary::kCall) {
4665 locations->SetInAt(0, Location::RequiresRegister());
4666 locations->SetInAt(1, Location::RequiresRegister());
4667 // The out register is used as a temporary, so it overlaps with the inputs.
4668 // Note that TypeCheckSlowPathARM uses this register too.
4669 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4670 } else {
4671 InvokeRuntimeCallingConvention calling_convention;
4672 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4673 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4674 locations->SetOut(Location::RegisterLocation(R0));
4675 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004676}
4677
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004678void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004679 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004680 Register obj = locations->InAt(0).AsRegister<Register>();
4681 Register cls = locations->InAt(1).AsRegister<Register>();
4682 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004683 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004684 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4685 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4686 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004687 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004688 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004689
4690 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004691 // avoid null check if we know obj is not null.
4692 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004693 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004694 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004695
Calin Juravle98893e12015-10-02 21:05:03 +01004696 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004697 // This is safe, as the register is caller-save, and the object must be in another
4698 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004699 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4700 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004701 ? obj
4702 : out;
4703 __ LoadFromOffset(kLoadWord, target, obj, class_offset);
4704 __ MaybeUnpoisonHeapReference(target);
4705
4706 switch (instruction->GetTypeCheckKind()) {
4707 case TypeCheckKind::kExactCheck: {
4708 __ cmp(out, ShifterOperand(cls));
4709 // Classes must be equal for the instanceof to succeed.
4710 __ b(&zero, NE);
4711 __ LoadImmediate(out, 1);
4712 __ b(&done);
4713 break;
4714 }
4715 case TypeCheckKind::kAbstractClassCheck: {
4716 // If the class is abstract, we eagerly fetch the super class of the
4717 // object to avoid doing a comparison we know will fail.
4718 Label loop;
4719 __ Bind(&loop);
4720 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4721 __ MaybeUnpoisonHeapReference(out);
4722 // If `out` is null, we use it for the result, and jump to `done`.
4723 __ CompareAndBranchIfZero(out, &done);
4724 __ cmp(out, ShifterOperand(cls));
4725 __ b(&loop, NE);
4726 __ LoadImmediate(out, 1);
4727 if (zero.IsLinked()) {
4728 __ b(&done);
4729 }
4730 break;
4731 }
4732 case TypeCheckKind::kClassHierarchyCheck: {
4733 // Walk over the class hierarchy to find a match.
4734 Label loop, success;
4735 __ Bind(&loop);
4736 __ cmp(out, ShifterOperand(cls));
4737 __ b(&success, EQ);
4738 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4739 __ MaybeUnpoisonHeapReference(out);
4740 __ CompareAndBranchIfNonZero(out, &loop);
4741 // If `out` is null, we use it for the result, and jump to `done`.
4742 __ b(&done);
4743 __ Bind(&success);
4744 __ LoadImmediate(out, 1);
4745 if (zero.IsLinked()) {
4746 __ b(&done);
4747 }
4748 break;
4749 }
4750 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004751 // Do an exact check.
4752 Label exact_check;
4753 __ cmp(out, ShifterOperand(cls));
4754 __ b(&exact_check, EQ);
4755 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004756 __ LoadFromOffset(kLoadWord, out, out, component_offset);
4757 __ MaybeUnpoisonHeapReference(out);
4758 // If `out` is null, we use it for the result, and jump to `done`.
4759 __ CompareAndBranchIfZero(out, &done);
4760 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4761 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4762 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004763 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004764 __ LoadImmediate(out, 1);
4765 __ b(&done);
4766 break;
4767 }
4768 case TypeCheckKind::kArrayCheck: {
4769 __ cmp(out, ShifterOperand(cls));
4770 DCHECK(locations->OnlyCallsOnSlowPath());
4771 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4772 instruction, /* is_fatal */ false);
4773 codegen_->AddSlowPath(slow_path);
4774 __ b(slow_path->GetEntryLabel(), NE);
4775 __ LoadImmediate(out, 1);
4776 if (zero.IsLinked()) {
4777 __ b(&done);
4778 }
4779 break;
4780 }
Calin Juravle98893e12015-10-02 21:05:03 +01004781 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004782 case TypeCheckKind::kInterfaceCheck:
4783 default: {
4784 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4785 instruction,
4786 instruction->GetDexPc(),
4787 nullptr);
4788 if (zero.IsLinked()) {
4789 __ b(&done);
4790 }
4791 break;
4792 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004793 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004794
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004795 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004796 __ Bind(&zero);
4797 __ LoadImmediate(out, 0);
4798 }
4799
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004800 if (done.IsLinked()) {
4801 __ Bind(&done);
4802 }
4803
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004804 if (slow_path != nullptr) {
4805 __ Bind(slow_path->GetExitLabel());
4806 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004807}
4808
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004809void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004810 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4811 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4812
4813 switch (instruction->GetTypeCheckKind()) {
4814 case TypeCheckKind::kExactCheck:
4815 case TypeCheckKind::kAbstractClassCheck:
4816 case TypeCheckKind::kClassHierarchyCheck:
4817 case TypeCheckKind::kArrayObjectCheck:
4818 call_kind = throws_into_catch
4819 ? LocationSummary::kCallOnSlowPath
4820 : LocationSummary::kNoCall;
4821 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004822 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004823 case TypeCheckKind::kInterfaceCheck:
4824 call_kind = LocationSummary::kCall;
4825 break;
4826 case TypeCheckKind::kArrayCheck:
4827 call_kind = LocationSummary::kCallOnSlowPath;
4828 break;
4829 }
4830
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004831 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004832 instruction, call_kind);
4833 if (call_kind != LocationSummary::kCall) {
4834 locations->SetInAt(0, Location::RequiresRegister());
4835 locations->SetInAt(1, Location::RequiresRegister());
4836 // Note that TypeCheckSlowPathARM uses this register too.
4837 locations->AddTemp(Location::RequiresRegister());
4838 } else {
4839 InvokeRuntimeCallingConvention calling_convention;
4840 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4841 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4842 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004843}
4844
4845void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4846 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004847 Register obj = locations->InAt(0).AsRegister<Register>();
4848 Register cls = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004849 Register temp = locations->WillCall()
4850 ? Register(kNoRegister)
4851 : locations->GetTemp(0).AsRegister<Register>();
4852
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004853 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004854 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4855 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4856 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4857 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004858
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004859 if (!locations->WillCall()) {
4860 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4861 instruction, !locations->CanCall());
4862 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004863 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004864
4865 Label done;
4866 // Avoid null check if we know obj is not null.
4867 if (instruction->MustDoNullCheck()) {
4868 __ CompareAndBranchIfZero(obj, &done);
4869 }
4870
4871 if (locations->WillCall()) {
4872 __ LoadFromOffset(kLoadWord, obj, obj, class_offset);
4873 __ MaybeUnpoisonHeapReference(obj);
4874 } else {
4875 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4876 __ MaybeUnpoisonHeapReference(temp);
4877 }
4878
4879 switch (instruction->GetTypeCheckKind()) {
4880 case TypeCheckKind::kExactCheck:
4881 case TypeCheckKind::kArrayCheck: {
4882 __ cmp(temp, ShifterOperand(cls));
4883 // Jump to slow path for throwing the exception or doing a
4884 // more involved array check.
4885 __ b(slow_path->GetEntryLabel(), NE);
4886 break;
4887 }
4888 case TypeCheckKind::kAbstractClassCheck: {
4889 // If the class is abstract, we eagerly fetch the super class of the
4890 // object to avoid doing a comparison we know will fail.
4891 Label loop;
4892 __ Bind(&loop);
4893 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4894 __ MaybeUnpoisonHeapReference(temp);
4895 // Jump to the slow path to throw the exception.
4896 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4897 __ cmp(temp, ShifterOperand(cls));
4898 __ b(&loop, NE);
4899 break;
4900 }
4901 case TypeCheckKind::kClassHierarchyCheck: {
4902 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004903 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004904 __ Bind(&loop);
4905 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004906 __ b(&done, EQ);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004907 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4908 __ MaybeUnpoisonHeapReference(temp);
4909 __ CompareAndBranchIfNonZero(temp, &loop);
4910 // Jump to the slow path to throw the exception.
4911 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004912 break;
4913 }
4914 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004915 // Do an exact check.
4916 __ cmp(temp, ShifterOperand(cls));
4917 __ b(&done, EQ);
4918 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004919 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
4920 __ MaybeUnpoisonHeapReference(temp);
4921 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4922 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
4923 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4924 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
4925 break;
4926 }
Calin Juravle98893e12015-10-02 21:05:03 +01004927 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004928 case TypeCheckKind::kInterfaceCheck:
4929 default:
4930 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
4931 instruction,
4932 instruction->GetDexPc(),
4933 nullptr);
4934 break;
4935 }
4936 __ Bind(&done);
4937
4938 if (slow_path != nullptr) {
4939 __ Bind(slow_path->GetExitLabel());
4940 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004941}
4942
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004943void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4944 LocationSummary* locations =
4945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4946 InvokeRuntimeCallingConvention calling_convention;
4947 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4948}
4949
4950void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4951 codegen_->InvokeRuntime(instruction->IsEnter()
4952 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4953 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004954 instruction->GetDexPc(),
4955 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004956}
4957
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004958void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
4959void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
4960void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004961
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004962void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004963 LocationSummary* locations =
4964 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4965 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4966 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004967 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004968 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004969 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004971}
4972
4973void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4974 HandleBitwiseOperation(instruction);
4975}
4976
4977void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4978 HandleBitwiseOperation(instruction);
4979}
4980
4981void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4982 HandleBitwiseOperation(instruction);
4983}
4984
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004985void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
4986 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
4987 if (value == 0xffffffffu) {
4988 if (out != first) {
4989 __ mov(out, ShifterOperand(first));
4990 }
4991 return;
4992 }
4993 if (value == 0u) {
4994 __ mov(out, ShifterOperand(0));
4995 return;
4996 }
4997 ShifterOperand so;
4998 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
4999 __ and_(out, first, so);
5000 } else {
5001 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5002 __ bic(out, first, ShifterOperand(~value));
5003 }
5004}
5005
5006void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5007 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5008 if (value == 0u) {
5009 if (out != first) {
5010 __ mov(out, ShifterOperand(first));
5011 }
5012 return;
5013 }
5014 if (value == 0xffffffffu) {
5015 __ mvn(out, ShifterOperand(0));
5016 return;
5017 }
5018 ShifterOperand so;
5019 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5020 __ orr(out, first, so);
5021 } else {
5022 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5023 __ orn(out, first, ShifterOperand(~value));
5024 }
5025}
5026
5027void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5028 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5029 if (value == 0u) {
5030 if (out != first) {
5031 __ mov(out, ShifterOperand(first));
5032 }
5033 return;
5034 }
5035 __ eor(out, first, ShifterOperand(value));
5036}
5037
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005038void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5039 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005040 Location first = locations->InAt(0);
5041 Location second = locations->InAt(1);
5042 Location out = locations->Out();
5043
5044 if (second.IsConstant()) {
5045 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5046 uint32_t value_low = Low32Bits(value);
5047 if (instruction->GetResultType() == Primitive::kPrimInt) {
5048 Register first_reg = first.AsRegister<Register>();
5049 Register out_reg = out.AsRegister<Register>();
5050 if (instruction->IsAnd()) {
5051 GenerateAndConst(out_reg, first_reg, value_low);
5052 } else if (instruction->IsOr()) {
5053 GenerateOrrConst(out_reg, first_reg, value_low);
5054 } else {
5055 DCHECK(instruction->IsXor());
5056 GenerateEorConst(out_reg, first_reg, value_low);
5057 }
5058 } else {
5059 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5060 uint32_t value_high = High32Bits(value);
5061 Register first_low = first.AsRegisterPairLow<Register>();
5062 Register first_high = first.AsRegisterPairHigh<Register>();
5063 Register out_low = out.AsRegisterPairLow<Register>();
5064 Register out_high = out.AsRegisterPairHigh<Register>();
5065 if (instruction->IsAnd()) {
5066 GenerateAndConst(out_low, first_low, value_low);
5067 GenerateAndConst(out_high, first_high, value_high);
5068 } else if (instruction->IsOr()) {
5069 GenerateOrrConst(out_low, first_low, value_low);
5070 GenerateOrrConst(out_high, first_high, value_high);
5071 } else {
5072 DCHECK(instruction->IsXor());
5073 GenerateEorConst(out_low, first_low, value_low);
5074 GenerateEorConst(out_high, first_high, value_high);
5075 }
5076 }
5077 return;
5078 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005079
5080 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005081 Register first_reg = first.AsRegister<Register>();
5082 ShifterOperand second_reg(second.AsRegister<Register>());
5083 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005084 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005085 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005086 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005087 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005088 } else {
5089 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005090 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005091 }
5092 } else {
5093 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005094 Register first_low = first.AsRegisterPairLow<Register>();
5095 Register first_high = first.AsRegisterPairHigh<Register>();
5096 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5097 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5098 Register out_low = out.AsRegisterPairLow<Register>();
5099 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005100 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005101 __ and_(out_low, first_low, second_low);
5102 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005103 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005104 __ orr(out_low, first_low, second_low);
5105 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005106 } else {
5107 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005108 __ eor(out_low, first_low, second_low);
5109 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005110 }
5111 }
5112}
5113
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005114void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00005115 // For better instruction scheduling we load the direct code pointer before the method pointer.
5116 bool direct_code_loaded = false;
5117 switch (invoke->GetCodePtrLocation()) {
5118 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5119 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
5120 break;
5121 }
5122 // Calls across dex files are more likely to exceed the available BL range,
5123 // so use absolute patch by falling through to kDirectCodeFixup.
5124 FALLTHROUGH_INTENDED;
5125 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5126 // LR = code address from literal pool with link-time patch.
5127 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5128 direct_code_loaded = true;
5129 break;
5130 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5131 // LR = invoke->GetDirectCodePtr();
5132 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
5133 direct_code_loaded = true;
5134 break;
5135 default:
5136 break;
5137 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005138
Vladimir Marko58155012015-08-19 12:49:41 +00005139 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5140 switch (invoke->GetMethodLoadKind()) {
5141 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5142 // temp = thread->string_init_entrypoint
5143 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5144 break;
5145 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
5146 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5147 break;
5148 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5149 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5150 break;
5151 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5152 __ LoadLiteral(temp.AsRegister<Register>(),
5153 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5154 break;
5155 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5156 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5157 FALLTHROUGH_INTENDED;
5158 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5159 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5160 Register method_reg;
5161 Register reg = temp.AsRegister<Register>();
5162 if (current_method.IsRegister()) {
5163 method_reg = current_method.AsRegister<Register>();
5164 } else {
5165 DCHECK(invoke->GetLocations()->Intrinsified());
5166 DCHECK(!current_method.IsValid());
5167 method_reg = reg;
5168 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5169 }
5170 // temp = current_method->dex_cache_resolved_methods_;
5171 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01005172 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
5173 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005174 // temp = temp[index_in_cache]
5175 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5176 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5177 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005178 }
Vladimir Marko58155012015-08-19 12:49:41 +00005179 }
5180
5181 switch (invoke->GetCodePtrLocation()) {
5182 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5183 __ bl(GetFrameEntryLabel());
5184 break;
5185 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5186 if (!direct_code_loaded) {
5187 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
5188 __ Bind(&relative_call_patches_.back().label);
5189 Label label;
5190 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
5191 __ Bind(&label);
5192 break;
5193 }
5194 // If we loaded the direct code above, fall through.
5195 FALLTHROUGH_INTENDED;
5196 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5197 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5198 // LR prepared above for better instruction scheduling.
5199 DCHECK(direct_code_loaded);
5200 // LR()
5201 __ blx(LR);
5202 break;
5203 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5204 // LR = callee_method->entry_point_from_quick_compiled_code_
5205 __ LoadFromOffset(
5206 kLoadWord, LR, callee_method.AsRegister<Register>(),
5207 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5208 // LR()
5209 __ blx(LR);
5210 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005211 }
5212
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005213 DCHECK(!IsLeafMethod());
5214}
5215
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005216void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5217 Register temp = temp_location.AsRegister<Register>();
5218 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5219 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5220 LocationSummary* locations = invoke->GetLocations();
5221 Location receiver = locations->InAt(0);
5222 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5223 // temp = object->GetClass();
5224 DCHECK(receiver.IsRegister());
5225 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5226 MaybeRecordImplicitNullCheck(invoke);
5227 __ MaybeUnpoisonHeapReference(temp);
5228 // temp = temp->GetMethodAt(method_offset);
5229 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5230 kArmWordSize).Int32Value();
5231 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5232 // LR = temp->GetEntryPoint();
5233 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5234 // LR();
5235 __ blx(LR);
5236}
5237
Vladimir Marko58155012015-08-19 12:49:41 +00005238void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5239 DCHECK(linker_patches->empty());
5240 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5241 linker_patches->reserve(size);
5242 for (const auto& entry : method_patches_) {
5243 const MethodReference& target_method = entry.first;
5244 Literal* literal = entry.second;
5245 DCHECK(literal->GetLabel()->IsBound());
5246 uint32_t literal_offset = literal->GetLabel()->Position();
5247 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5248 target_method.dex_file,
5249 target_method.dex_method_index));
5250 }
5251 for (const auto& entry : call_patches_) {
5252 const MethodReference& target_method = entry.first;
5253 Literal* literal = entry.second;
5254 DCHECK(literal->GetLabel()->IsBound());
5255 uint32_t literal_offset = literal->GetLabel()->Position();
5256 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5257 target_method.dex_file,
5258 target_method.dex_method_index));
5259 }
5260 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5261 uint32_t literal_offset = info.label.Position();
5262 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5263 info.target_method.dex_file,
5264 info.target_method.dex_method_index));
5265 }
5266}
5267
5268Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5269 MethodToLiteralMap* map) {
5270 // Look up the literal for target_method.
5271 auto lb = map->lower_bound(target_method);
5272 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5273 return lb->second;
5274 }
5275 // We don't have a literal for this method yet, insert a new one.
5276 Literal* literal = __ NewLiteral<uint32_t>(0u);
5277 map->PutBefore(lb, target_method, literal);
5278 return literal;
5279}
5280
5281Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5282 return DeduplicateMethodLiteral(target_method, &method_patches_);
5283}
5284
5285Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5286 return DeduplicateMethodLiteral(target_method, &call_patches_);
5287}
5288
Calin Juravleb1498f62015-02-16 13:13:29 +00005289void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
5290 // Nothing to do, this should be removed during prepare for register allocator.
5291 UNUSED(instruction);
5292 LOG(FATAL) << "Unreachable";
5293}
5294
5295void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
5296 // Nothing to do, this should be removed during prepare for register allocator.
5297 UNUSED(instruction);
5298 LOG(FATAL) << "Unreachable";
5299}
5300
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005301void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5302 DCHECK(codegen_->IsBaseline());
5303 LocationSummary* locations =
5304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5305 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5306}
5307
5308void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5309 DCHECK(codegen_->IsBaseline());
5310 // Will be generated at use site.
5311}
5312
Mark Mendellfe57faa2015-09-18 09:26:15 -04005313// Simple implementation of packed switch - generate cascaded compare/jumps.
5314void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5315 LocationSummary* locations =
5316 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5317 locations->SetInAt(0, Location::RequiresRegister());
5318}
5319
5320void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5321 int32_t lower_bound = switch_instr->GetStartValue();
5322 int32_t num_entries = switch_instr->GetNumEntries();
5323 LocationSummary* locations = switch_instr->GetLocations();
5324 Register value_reg = locations->InAt(0).AsRegister<Register>();
5325 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5326
5327 // Create a series of compare/jumps.
5328 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5329 for (int32_t i = 0; i < num_entries; i++) {
5330 GenerateCompareWithImmediate(value_reg, lower_bound + i);
Vladimir Markoec7802a2015-10-01 20:57:57 +01005331 __ b(codegen_->GetLabelOf(successors[i]), EQ);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005332 }
5333
5334 // And the default for any other value.
5335 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5336 __ b(codegen_->GetLabelOf(default_block));
5337 }
5338}
5339
Andreas Gampe85b62f22015-09-09 13:15:38 -07005340void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5341 if (!trg.IsValid()) {
5342 DCHECK(type == Primitive::kPrimVoid);
5343 return;
5344 }
5345
5346 DCHECK_NE(type, Primitive::kPrimVoid);
5347
5348 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5349 if (return_loc.Equals(trg)) {
5350 return;
5351 }
5352
5353 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5354 // with the last branch.
5355 if (type == Primitive::kPrimLong) {
5356 HParallelMove parallel_move(GetGraph()->GetArena());
5357 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
5358 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
5359 GetMoveResolver()->EmitNativeCode(&parallel_move);
5360 } else if (type == Primitive::kPrimDouble) {
5361 HParallelMove parallel_move(GetGraph()->GetArena());
5362 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
5363 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
5364 GetMoveResolver()->EmitNativeCode(&parallel_move);
5365 } else {
5366 // Let the parallel move resolver take care of all of this.
5367 HParallelMove parallel_move(GetGraph()->GetArena());
5368 parallel_move.AddMove(return_loc, trg, type, nullptr);
5369 GetMoveResolver()->EmitNativeCode(&parallel_move);
5370 }
5371}
5372
Roland Levillain4d027112015-07-01 15:41:14 +01005373#undef __
5374#undef QUICK_ENTRY_POINT
5375
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005376} // namespace arm
5377} // namespace art