blob: 358a35c483da9f221e11daa7e429c921584d7605 [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
Aart Bike9f37602015-10-09 11:15:55 -0700412inline Condition ARMCondition(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;
Aart Bike9f37602015-10-09 11:15:55 -0700420 case kCondB: return LO;
421 case kCondBE: return LS;
422 case kCondA: return HI;
423 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700424 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100425 LOG(FATAL) << "Unreachable";
426 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700427}
428
Aart Bike9f37602015-10-09 11:15:55 -0700429// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100430inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700431 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100432 case kCondEQ: return EQ;
433 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700434 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100435 case kCondLT: return LO;
436 case kCondLE: return LS;
437 case kCondGT: return HI;
438 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700439 // Unsigned remain unchanged.
440 case kCondB: return LO;
441 case kCondBE: return LS;
442 case kCondA: return HI;
443 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700444 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100445 LOG(FATAL) << "Unreachable";
446 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700447}
448
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100449void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100450 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100451}
452
453void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100454 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100455}
456
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100457size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
458 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
459 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100460}
461
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100462size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
463 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
464 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100465}
466
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000467size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
468 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
469 return kArmWordSize;
470}
471
472size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
473 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
474 return kArmWordSize;
475}
476
Calin Juravle34166012014-12-19 17:22:29 +0000477CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000478 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100479 const CompilerOptions& compiler_options,
480 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000481 : CodeGenerator(graph,
482 kNumberOfCoreRegisters,
483 kNumberOfSRegisters,
484 kNumberOfRegisterPairs,
485 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
486 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000487 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
488 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100489 compiler_options,
490 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100491 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100492 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100493 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100494 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000495 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000496 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100497 method_patches_(MethodReferenceComparator(),
498 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
499 call_patches_(MethodReferenceComparator(),
500 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
501 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700502 // Always save the LR register to mimic Quick.
503 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100504}
505
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000506void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
507 // Ensure that we fix up branches and literal loads and emit the literal pool.
508 __ FinalizeCode();
509
510 // Adjust native pc offsets in stack maps.
511 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
512 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
513 uint32_t new_position = __ GetAdjustedPosition(old_position);
514 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
515 }
516 // Adjust native pc offsets of block labels.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100517 for (HBasicBlock* block : *block_order_) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000518 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
519 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
Vladimir Marko225b6462015-09-28 12:17:40 +0100520 DCHECK_LT(block->GetBlockId(), GetGraph()->GetBlocks().size());
521 Label* block_label = &block_labels_[block->GetBlockId()];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000522 DCHECK_EQ(block_label->IsBound(), !block->IsSingleJump());
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000523 if (block_label->IsBound()) {
524 __ AdjustLabelPosition(block_label);
525 }
526 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100527 // Adjust pc offsets for the disassembly information.
528 if (disasm_info_ != nullptr) {
529 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
530 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
531 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
532 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
533 it.second.start = __ GetAdjustedPosition(it.second.start);
534 it.second.end = __ GetAdjustedPosition(it.second.end);
535 }
536 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
537 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
538 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
539 }
540 }
Vladimir Marko58155012015-08-19 12:49:41 +0000541 // Adjust pc offsets for relative call patches.
542 for (MethodPatchInfo<Label>& info : relative_call_patches_) {
543 __ AdjustLabelPosition(&info.label);
544 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000545
546 CodeGenerator::Finalize(allocator);
547}
548
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100549Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100550 switch (type) {
551 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100552 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553 ArmManagedRegister pair =
554 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100555 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
556 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
557
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100558 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
559 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100560 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100561 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100562 }
563
564 case Primitive::kPrimByte:
565 case Primitive::kPrimBoolean:
566 case Primitive::kPrimChar:
567 case Primitive::kPrimShort:
568 case Primitive::kPrimInt:
569 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100570 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100571 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100572 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
573 ArmManagedRegister current =
574 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
575 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100576 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100577 }
578 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100580 }
581
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000582 case Primitive::kPrimFloat: {
583 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100586
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000587 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000588 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
589 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000590 return Location::FpuRegisterPairLocation(reg, reg + 1);
591 }
592
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100593 case Primitive::kPrimVoid:
594 LOG(FATAL) << "Unreachable type " << type;
595 }
596
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100597 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598}
599
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000600void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100601 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100602 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100603
604 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100605 blocked_core_registers_[SP] = true;
606 blocked_core_registers_[LR] = true;
607 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100608
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100609 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100610 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100611
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100612 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100613 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100614
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000615 if (is_baseline) {
616 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
617 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
618 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000619
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000620 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100621 }
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000622
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100623 if (is_baseline || GetGraph()->IsDebuggable()) {
624 // Stubs do not save callee-save floating point registers. If the graph
625 // is debuggable, we need to deal with these registers differently. For
626 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000627 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
628 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
629 }
630 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100631
632 UpdateBlockedPairRegisters();
633}
634
635void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
636 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
637 ArmManagedRegister current =
638 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
639 if (blocked_core_registers_[current.AsRegisterPairLow()]
640 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
641 blocked_register_pairs_[i] = true;
642 }
643 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100644}
645
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100646InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
647 : HGraphVisitor(graph),
648 assembler_(codegen->GetAssembler()),
649 codegen_(codegen) {}
650
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000651void CodeGeneratorARM::ComputeSpillMask() {
652 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000653 // Save one extra register for baseline. Note that on thumb2, there is no easy
654 // instruction to restore just the PC, so this actually helps both baseline
655 // and non-baseline to save and restore at least two registers at entry and exit.
656 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000657 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
658 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
659 // We use vpush and vpop for saving and restoring floating point registers, which take
660 // a SRegister and the number of registers to save/restore after that SRegister. We
661 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
662 // but in the range.
663 if (fpu_spill_mask_ != 0) {
664 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
665 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
666 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
667 fpu_spill_mask_ |= (1 << i);
668 }
669 }
670}
671
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100672static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100673 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100674}
675
676static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100677 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100678}
679
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000680void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000681 bool skip_overflow_check =
682 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000683 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000684 __ Bind(&frame_entry_label_);
685
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000686 if (HasEmptyFrame()) {
687 return;
688 }
689
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100690 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000691 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
692 __ LoadFromOffset(kLoadWord, IP, IP, 0);
693 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100694 }
695
Andreas Gampe501fd632015-09-10 16:11:06 -0700696 __ PushList(core_spill_mask_);
697 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
698 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000699 if (fpu_spill_mask_ != 0) {
700 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
701 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100702 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100703 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000704 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100705 int adjust = GetFrameSize() - FrameEntrySpillSize();
706 __ AddConstant(SP, -adjust);
707 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100708 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000709}
710
711void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000712 if (HasEmptyFrame()) {
713 __ bx(LR);
714 return;
715 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100716 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100717 int adjust = GetFrameSize() - FrameEntrySpillSize();
718 __ AddConstant(SP, adjust);
719 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000720 if (fpu_spill_mask_ != 0) {
721 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
722 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100723 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
724 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000725 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700726 // Pop LR into PC to return.
727 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
728 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
729 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100730 __ cfi().RestoreState();
731 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000732}
733
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100734void CodeGeneratorARM::Bind(HBasicBlock* block) {
735 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000736}
737
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100738Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
739 switch (load->GetType()) {
740 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100742 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100743
744 case Primitive::kPrimInt:
745 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100746 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100748
749 case Primitive::kPrimBoolean:
750 case Primitive::kPrimByte:
751 case Primitive::kPrimChar:
752 case Primitive::kPrimShort:
753 case Primitive::kPrimVoid:
754 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700755 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100756 }
757
758 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700759 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100760}
761
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100762Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100763 switch (type) {
764 case Primitive::kPrimBoolean:
765 case Primitive::kPrimByte:
766 case Primitive::kPrimChar:
767 case Primitive::kPrimShort:
768 case Primitive::kPrimInt:
769 case Primitive::kPrimNot: {
770 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000771 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100772 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100773 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100774 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000775 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100776 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100777 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100778
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000779 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100780 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000781 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100782 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000783 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100784 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000785 if (calling_convention.GetRegisterAt(index) == R1) {
786 // Skip R1, and use R2_R3 instead.
787 gp_index_++;
788 index++;
789 }
790 }
791 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
792 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000793 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100794
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000795 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000796 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100797 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000798 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
799 }
800 }
801
802 case Primitive::kPrimFloat: {
803 uint32_t stack_index = stack_index_++;
804 if (float_index_ % 2 == 0) {
805 float_index_ = std::max(double_index_, float_index_);
806 }
807 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
808 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
809 } else {
810 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
811 }
812 }
813
814 case Primitive::kPrimDouble: {
815 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
816 uint32_t stack_index = stack_index_;
817 stack_index_ += 2;
818 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
819 uint32_t index = double_index_;
820 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000821 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000822 calling_convention.GetFpuRegisterAt(index),
823 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000824 DCHECK(ExpectedPairLayout(result));
825 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000826 } else {
827 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100828 }
829 }
830
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100831 case Primitive::kPrimVoid:
832 LOG(FATAL) << "Unexpected parameter type " << type;
833 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100834 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100835 return Location();
836}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100837
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100838Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000839 switch (type) {
840 case Primitive::kPrimBoolean:
841 case Primitive::kPrimByte:
842 case Primitive::kPrimChar:
843 case Primitive::kPrimShort:
844 case Primitive::kPrimInt:
845 case Primitive::kPrimNot: {
846 return Location::RegisterLocation(R0);
847 }
848
849 case Primitive::kPrimFloat: {
850 return Location::FpuRegisterLocation(S0);
851 }
852
853 case Primitive::kPrimLong: {
854 return Location::RegisterPairLocation(R0, R1);
855 }
856
857 case Primitive::kPrimDouble: {
858 return Location::FpuRegisterPairLocation(S0, S1);
859 }
860
861 case Primitive::kPrimVoid:
862 return Location();
863 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100864
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000865 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000866}
867
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100868Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
869 return Location::RegisterLocation(kMethodRegisterArgument);
870}
871
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872void CodeGeneratorARM::Move32(Location destination, Location source) {
873 if (source.Equals(destination)) {
874 return;
875 }
876 if (destination.IsRegister()) {
877 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100879 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000880 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100881 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000882 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100884 } else if (destination.IsFpuRegister()) {
885 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000886 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000888 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100889 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000890 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000893 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000895 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100896 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000897 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000899 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100900 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
901 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 }
903 }
904}
905
906void CodeGeneratorARM::Move64(Location destination, Location source) {
907 if (source.Equals(destination)) {
908 return;
909 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100910 if (destination.IsRegisterPair()) {
911 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000912 EmitParallelMoves(
913 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
914 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100915 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000916 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100917 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
918 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100919 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000920 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100921 } else if (source.IsFpuRegisterPair()) {
922 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
923 destination.AsRegisterPairHigh<Register>(),
924 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100925 } else {
926 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000927 DCHECK(ExpectedPairLayout(destination));
928 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
929 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100930 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000931 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100932 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000933 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
934 SP,
935 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100936 } else if (source.IsRegisterPair()) {
937 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
938 source.AsRegisterPairLow<Register>(),
939 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100940 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000941 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943 } else {
944 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100945 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000946 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100947 if (source.AsRegisterPairLow<Register>() == R1) {
948 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100949 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
950 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100951 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100952 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100953 SP, destination.GetStackIndex());
954 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000955 } else if (source.IsFpuRegisterPair()) {
956 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
957 SP,
958 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 } else {
960 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000961 EmitParallelMoves(
962 Location::StackSlot(source.GetStackIndex()),
963 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100964 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000965 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100966 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
967 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100968 }
969 }
970}
971
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100972void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100973 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100974 if (instruction->IsCurrentMethod()) {
975 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
976 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100977 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100978 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000979 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000980 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
981 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000982 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000983 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000984 } else {
985 DCHECK(location.IsStackSlot());
986 __ LoadImmediate(IP, value);
987 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
988 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000989 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000990 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000991 int64_t value = const_to_move->AsLongConstant()->GetValue();
992 if (location.IsRegisterPair()) {
993 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
994 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
995 } else {
996 DCHECK(location.IsDoubleStackSlot());
997 __ LoadImmediate(IP, Low32Bits(value));
998 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
999 __ LoadImmediate(IP, High32Bits(value));
1000 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
1001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 }
Roland Levillain476df552014-10-09 17:51:36 +01001003 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1005 switch (instruction->GetType()) {
1006 case Primitive::kPrimBoolean:
1007 case Primitive::kPrimByte:
1008 case Primitive::kPrimChar:
1009 case Primitive::kPrimShort:
1010 case Primitive::kPrimInt:
1011 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001012 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013 Move32(location, Location::StackSlot(stack_slot));
1014 break;
1015
1016 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001017 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018 Move64(location, Location::DoubleStackSlot(stack_slot));
1019 break;
1020
1021 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001024 } else if (instruction->IsTemporary()) {
1025 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001026 if (temp_location.IsStackSlot()) {
1027 Move32(location, temp_location);
1028 } else {
1029 DCHECK(temp_location.IsDoubleStackSlot());
1030 Move64(location, temp_location);
1031 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001032 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001033 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 switch (instruction->GetType()) {
1035 case Primitive::kPrimBoolean:
1036 case Primitive::kPrimByte:
1037 case Primitive::kPrimChar:
1038 case Primitive::kPrimShort:
1039 case Primitive::kPrimNot:
1040 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001042 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 break;
1044
1045 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001046 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001047 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 break;
1049
1050 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053 }
1054}
1055
Calin Juravle175dc732015-08-25 15:42:32 +01001056void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1057 DCHECK(location.IsRegister());
1058 __ LoadImmediate(location.AsRegister<Register>(), value);
1059}
1060
Calin Juravlee460d1d2015-09-29 04:52:17 +01001061void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1062 if (Primitive::Is64BitType(dst_type)) {
1063 Move64(dst, src);
1064 } else {
1065 Move32(dst, src);
1066 }
1067}
1068
1069void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1070 if (location.IsRegister()) {
1071 locations->AddTemp(location);
1072 } else if (location.IsRegisterPair()) {
1073 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1074 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1075 } else {
1076 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1077 }
1078}
1079
Calin Juravle175dc732015-08-25 15:42:32 +01001080void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1081 HInstruction* instruction,
1082 uint32_t dex_pc,
1083 SlowPathCode* slow_path) {
1084 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1085 instruction,
1086 dex_pc,
1087 slow_path);
1088}
1089
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001090void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1091 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001092 uint32_t dex_pc,
1093 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001094 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001095 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1096 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001097 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001098}
1099
David Brazdilfc6a86a2015-06-26 10:33:45 +00001100void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001101 DCHECK(!successor->IsExitBlock());
1102
1103 HBasicBlock* block = got->GetBlock();
1104 HInstruction* previous = got->GetPrevious();
1105
1106 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001107 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001108 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1109 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1110 return;
1111 }
1112
1113 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1114 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1115 }
1116 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001117 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001118 }
1119}
1120
David Brazdilfc6a86a2015-06-26 10:33:45 +00001121void LocationsBuilderARM::VisitGoto(HGoto* got) {
1122 got->SetLocations(nullptr);
1123}
1124
1125void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1126 HandleGoto(got, got->GetSuccessor());
1127}
1128
1129void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1130 try_boundary->SetLocations(nullptr);
1131}
1132
1133void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1134 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1135 if (!successor->IsExitBlock()) {
1136 HandleGoto(try_boundary, successor);
1137 }
1138}
1139
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001140void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001141 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001142}
1143
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001144void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001145 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001146}
1147
Roland Levillain4fa13f62015-07-06 18:11:54 +01001148void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1149 ShifterOperand operand;
1150 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1151 __ cmp(left, operand);
1152 } else {
1153 Register temp = IP;
1154 __ LoadImmediate(temp, right);
1155 __ cmp(left, ShifterOperand(temp));
1156 }
1157}
1158
1159void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1160 Label* true_label,
1161 Label* false_label) {
1162 __ vmstat(); // transfer FP status register to ARM APSR.
Aart Bike9f37602015-10-09 11:15:55 -07001163 // TODO: merge into a single branch (except "equal or unordered" and "not equal")
Roland Levillain4fa13f62015-07-06 18:11:54 +01001164 if (cond->IsFPConditionTrueIfNaN()) {
1165 __ b(true_label, VS); // VS for unordered.
1166 } else if (cond->IsFPConditionFalseIfNaN()) {
1167 __ b(false_label, VS); // VS for unordered.
1168 }
Aart Bike9f37602015-10-09 11:15:55 -07001169 __ b(true_label, ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001170}
1171
1172void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1173 Label* true_label,
1174 Label* false_label) {
1175 LocationSummary* locations = cond->GetLocations();
1176 Location left = locations->InAt(0);
1177 Location right = locations->InAt(1);
1178 IfCondition if_cond = cond->GetCondition();
1179
1180 Register left_high = left.AsRegisterPairHigh<Register>();
1181 Register left_low = left.AsRegisterPairLow<Register>();
1182 IfCondition true_high_cond = if_cond;
1183 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001184 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001185
1186 // Set the conditions for the test, remembering that == needs to be
1187 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001188 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001189 switch (if_cond) {
1190 case kCondEQ:
1191 case kCondNE:
1192 // Nothing to do.
1193 break;
1194 case kCondLT:
1195 false_high_cond = kCondGT;
1196 break;
1197 case kCondLE:
1198 true_high_cond = kCondLT;
1199 break;
1200 case kCondGT:
1201 false_high_cond = kCondLT;
1202 break;
1203 case kCondGE:
1204 true_high_cond = kCondGT;
1205 break;
Aart Bike9f37602015-10-09 11:15:55 -07001206 case kCondB:
1207 false_high_cond = kCondA;
1208 break;
1209 case kCondBE:
1210 true_high_cond = kCondB;
1211 break;
1212 case kCondA:
1213 false_high_cond = kCondB;
1214 break;
1215 case kCondAE:
1216 true_high_cond = kCondA;
1217 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001218 }
1219 if (right.IsConstant()) {
1220 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1221 int32_t val_low = Low32Bits(value);
1222 int32_t val_high = High32Bits(value);
1223
1224 GenerateCompareWithImmediate(left_high, val_high);
1225 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001226 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001227 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001228 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001229 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001230 __ b(true_label, ARMCondition(true_high_cond));
1231 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001232 }
1233 // Must be equal high, so compare the lows.
1234 GenerateCompareWithImmediate(left_low, val_low);
1235 } else {
1236 Register right_high = right.AsRegisterPairHigh<Register>();
1237 Register right_low = right.AsRegisterPairLow<Register>();
1238
1239 __ cmp(left_high, ShifterOperand(right_high));
1240 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001241 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001242 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001243 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001244 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001245 __ b(true_label, ARMCondition(true_high_cond));
1246 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001247 }
1248 // Must be equal high, so compare the lows.
1249 __ cmp(left_low, ShifterOperand(right_low));
1250 }
1251 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001252 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001253 __ b(true_label, final_condition);
1254}
1255
1256void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1257 HCondition* condition,
1258 Label* true_target,
1259 Label* false_target,
1260 Label* always_true_target) {
1261 LocationSummary* locations = condition->GetLocations();
1262 Location left = locations->InAt(0);
1263 Location right = locations->InAt(1);
1264
1265 // We don't want true_target as a nullptr.
1266 if (true_target == nullptr) {
1267 true_target = always_true_target;
1268 }
1269 bool falls_through = (false_target == nullptr);
1270
1271 // FP compares don't like null false_targets.
1272 if (false_target == nullptr) {
1273 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1274 }
1275
1276 Primitive::Type type = condition->InputAt(0)->GetType();
1277 switch (type) {
1278 case Primitive::kPrimLong:
1279 GenerateLongComparesAndJumps(condition, true_target, false_target);
1280 break;
1281 case Primitive::kPrimFloat:
1282 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1283 GenerateFPJumps(condition, true_target, false_target);
1284 break;
1285 case Primitive::kPrimDouble:
1286 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1287 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1288 GenerateFPJumps(condition, true_target, false_target);
1289 break;
1290 default:
1291 LOG(FATAL) << "Unexpected compare type " << type;
1292 }
1293
1294 if (!falls_through) {
1295 __ b(false_target);
1296 }
1297}
1298
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001299void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1300 Label* true_target,
1301 Label* false_target,
1302 Label* always_true_target) {
1303 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001304 if (cond->IsIntConstant()) {
1305 // Constant condition, statically compared against 1.
1306 int32_t cond_value = cond->AsIntConstant()->GetValue();
1307 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001308 if (always_true_target != nullptr) {
1309 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001310 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001311 return;
1312 } else {
1313 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001314 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001315 } else {
1316 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1317 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001318 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001319 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1320 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001321 } else {
1322 // Condition has not been materialized, use its inputs as the
1323 // comparison and its condition as the branch condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001324 Primitive::Type type =
1325 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
1326 // Is this a long or FP comparison that has been folded into the HCondition?
1327 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1328 // Generate the comparison directly.
1329 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1330 true_target, false_target, always_true_target);
1331 return;
1332 }
1333
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001334 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001335 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001336 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001337 Location right = locations->InAt(1);
1338 if (right.IsRegister()) {
1339 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001340 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001341 DCHECK(right.IsConstant());
1342 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001343 }
Aart Bike9f37602015-10-09 11:15:55 -07001344 __ b(true_target, ARMCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001345 }
Dave Allison20dfc792014-06-16 20:44:29 -07001346 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001347 if (false_target != nullptr) {
1348 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001349 }
1350}
1351
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001352void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1353 LocationSummary* locations =
1354 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1355 HInstruction* cond = if_instr->InputAt(0);
1356 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1357 locations->SetInAt(0, Location::RequiresRegister());
1358 }
1359}
1360
1361void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1362 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1363 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1364 Label* always_true_target = true_target;
1365 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1366 if_instr->IfTrueSuccessor())) {
1367 always_true_target = nullptr;
1368 }
1369 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1370 if_instr->IfFalseSuccessor())) {
1371 false_target = nullptr;
1372 }
1373 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1374}
1375
1376void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1377 LocationSummary* locations = new (GetGraph()->GetArena())
1378 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1379 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001380 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001381 locations->SetInAt(0, Location::RequiresRegister());
1382 }
1383}
1384
1385void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001386 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001387 DeoptimizationSlowPathARM(deoptimize);
1388 codegen_->AddSlowPath(slow_path);
1389 Label* slow_path_entry = slow_path->GetEntryLabel();
1390 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1391}
Dave Allison20dfc792014-06-16 20:44:29 -07001392
Roland Levillain0d37cd02015-05-27 16:39:19 +01001393void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001394 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001395 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001396 // Handle the long/FP comparisons made in instruction simplification.
1397 switch (cond->InputAt(0)->GetType()) {
1398 case Primitive::kPrimLong:
1399 locations->SetInAt(0, Location::RequiresRegister());
1400 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1401 if (cond->NeedsMaterialization()) {
1402 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1403 }
1404 break;
1405
1406 case Primitive::kPrimFloat:
1407 case Primitive::kPrimDouble:
1408 locations->SetInAt(0, Location::RequiresFpuRegister());
1409 locations->SetInAt(1, Location::RequiresFpuRegister());
1410 if (cond->NeedsMaterialization()) {
1411 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1412 }
1413 break;
1414
1415 default:
1416 locations->SetInAt(0, Location::RequiresRegister());
1417 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1418 if (cond->NeedsMaterialization()) {
1419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1420 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001421 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001422}
1423
Roland Levillain0d37cd02015-05-27 16:39:19 +01001424void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001425 if (!cond->NeedsMaterialization()) {
1426 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001427 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001428
1429 LocationSummary* locations = cond->GetLocations();
1430 Location left = locations->InAt(0);
1431 Location right = locations->InAt(1);
1432 Register out = locations->Out().AsRegister<Register>();
1433 Label true_label, false_label;
1434
1435 switch (cond->InputAt(0)->GetType()) {
1436 default: {
1437 // Integer case.
1438 if (right.IsRegister()) {
1439 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1440 } else {
1441 DCHECK(right.IsConstant());
1442 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1443 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1444 }
Aart Bike9f37602015-10-09 11:15:55 -07001445 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001446 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001447 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001448 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001449 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001450 return;
1451 }
1452 case Primitive::kPrimLong:
1453 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1454 break;
1455 case Primitive::kPrimFloat:
1456 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1457 GenerateFPJumps(cond, &true_label, &false_label);
1458 break;
1459 case Primitive::kPrimDouble:
1460 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1461 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1462 GenerateFPJumps(cond, &true_label, &false_label);
1463 break;
1464 }
1465
1466 // Convert the jumps into the result.
1467 Label done_label;
1468
1469 // False case: result = 0.
1470 __ Bind(&false_label);
1471 __ LoadImmediate(out, 0);
1472 __ b(&done_label);
1473
1474 // True case: result = 1.
1475 __ Bind(&true_label);
1476 __ LoadImmediate(out, 1);
1477 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001478}
1479
1480void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1481 VisitCondition(comp);
1482}
1483
1484void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1485 VisitCondition(comp);
1486}
1487
1488void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1489 VisitCondition(comp);
1490}
1491
1492void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1493 VisitCondition(comp);
1494}
1495
1496void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1497 VisitCondition(comp);
1498}
1499
1500void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1501 VisitCondition(comp);
1502}
1503
1504void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1505 VisitCondition(comp);
1506}
1507
1508void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1509 VisitCondition(comp);
1510}
1511
1512void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1513 VisitCondition(comp);
1514}
1515
1516void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1517 VisitCondition(comp);
1518}
1519
1520void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1521 VisitCondition(comp);
1522}
1523
1524void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1525 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001526}
1527
Aart Bike9f37602015-10-09 11:15:55 -07001528void LocationsBuilderARM::VisitBelow(HBelow* comp) {
1529 VisitCondition(comp);
1530}
1531
1532void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
1533 VisitCondition(comp);
1534}
1535
1536void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
1537 VisitCondition(comp);
1538}
1539
1540void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
1541 VisitCondition(comp);
1542}
1543
1544void LocationsBuilderARM::VisitAbove(HAbove* comp) {
1545 VisitCondition(comp);
1546}
1547
1548void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
1549 VisitCondition(comp);
1550}
1551
1552void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
1553 VisitCondition(comp);
1554}
1555
1556void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
1557 VisitCondition(comp);
1558}
1559
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001560void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001561 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562}
1563
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001564void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1565 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001566}
1567
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001568void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001569 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001570}
1571
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001572void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001573 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001574 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001575}
1576
1577void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001578 LocationSummary* locations =
1579 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001580 switch (store->InputAt(1)->GetType()) {
1581 case Primitive::kPrimBoolean:
1582 case Primitive::kPrimByte:
1583 case Primitive::kPrimChar:
1584 case Primitive::kPrimShort:
1585 case Primitive::kPrimInt:
1586 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001587 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001588 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1589 break;
1590
1591 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001592 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001593 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1594 break;
1595
1596 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001597 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001598 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001599}
1600
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001601void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001602 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001603}
1604
1605void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001606 LocationSummary* locations =
1607 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001608 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001609}
1610
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001611void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001612 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001613 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001614}
1615
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001616void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1617 LocationSummary* locations =
1618 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1619 locations->SetOut(Location::ConstantLocation(constant));
1620}
1621
1622void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1623 // Will be generated at use site.
1624 UNUSED(constant);
1625}
1626
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001627void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001628 LocationSummary* locations =
1629 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001630 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001631}
1632
1633void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1634 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001635 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001636}
1637
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001638void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1639 LocationSummary* locations =
1640 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1641 locations->SetOut(Location::ConstantLocation(constant));
1642}
1643
1644void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1645 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001646 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001647}
1648
1649void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1650 LocationSummary* locations =
1651 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1652 locations->SetOut(Location::ConstantLocation(constant));
1653}
1654
1655void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1656 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001657 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001658}
1659
Calin Juravle27df7582015-04-17 19:12:31 +01001660void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1661 memory_barrier->SetLocations(nullptr);
1662}
1663
1664void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1665 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1666}
1667
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001668void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001669 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001670}
1671
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001672void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001673 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001674 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001675}
1676
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001677void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001678 LocationSummary* locations =
1679 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001680 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001681}
1682
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001683void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001684 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001685 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001686}
1687
Calin Juravle175dc732015-08-25 15:42:32 +01001688void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1689 // The trampoline uses the same calling convention as dex calling conventions,
1690 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1691 // the method_idx.
1692 HandleInvoke(invoke);
1693}
1694
1695void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1696 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1697}
1698
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001699void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001700 // When we do not run baseline, explicit clinit checks triggered by static
1701 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1702 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001703
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001704 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001705 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001706 codegen_->GetInstructionSetFeatures());
1707 if (intrinsic.TryDispatch(invoke)) {
1708 return;
1709 }
1710
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001711 HandleInvoke(invoke);
1712}
1713
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001714static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1715 if (invoke->GetLocations()->Intrinsified()) {
1716 IntrinsicCodeGeneratorARM intrinsic(codegen);
1717 intrinsic.Dispatch(invoke);
1718 return true;
1719 }
1720 return false;
1721}
1722
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001723void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001724 // When we do not run baseline, explicit clinit checks triggered by static
1725 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1726 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001727
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001728 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1729 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001730 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001731
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001732 LocationSummary* locations = invoke->GetLocations();
1733 codegen_->GenerateStaticOrDirectCall(
1734 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001735 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001736}
1737
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001738void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001739 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001740 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001741}
1742
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001743void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001744 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001745 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001746 codegen_->GetInstructionSetFeatures());
1747 if (intrinsic.TryDispatch(invoke)) {
1748 return;
1749 }
1750
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001751 HandleInvoke(invoke);
1752}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001753
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001754void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001755 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1756 return;
1757 }
1758
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001759 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001760 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001761 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001762}
1763
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001764void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1765 HandleInvoke(invoke);
1766 // Add the hidden argument.
1767 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1768}
1769
1770void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1771 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001772 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001773 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1774 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001775 LocationSummary* locations = invoke->GetLocations();
1776 Location receiver = locations->InAt(0);
1777 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1778
1779 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001780 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1781 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001782
1783 // temp = object->GetClass();
1784 if (receiver.IsStackSlot()) {
1785 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1786 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1787 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001788 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001789 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001790 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001791 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001792 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001793 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001794 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001795 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1796 // LR = temp->GetEntryPoint();
1797 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1798 // LR();
1799 __ blx(LR);
1800 DCHECK(!codegen_->IsLeafMethod());
1801 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1802}
1803
Roland Levillain88cb1752014-10-20 16:36:47 +01001804void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1805 LocationSummary* locations =
1806 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1807 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001808 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001809 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1811 break;
1812 }
1813 case Primitive::kPrimLong: {
1814 locations->SetInAt(0, Location::RequiresRegister());
1815 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001816 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001817 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001818
Roland Levillain88cb1752014-10-20 16:36:47 +01001819 case Primitive::kPrimFloat:
1820 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001821 locations->SetInAt(0, Location::RequiresFpuRegister());
1822 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001823 break;
1824
1825 default:
1826 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1827 }
1828}
1829
1830void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1831 LocationSummary* locations = neg->GetLocations();
1832 Location out = locations->Out();
1833 Location in = locations->InAt(0);
1834 switch (neg->GetResultType()) {
1835 case Primitive::kPrimInt:
1836 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001837 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001838 break;
1839
1840 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001841 DCHECK(in.IsRegisterPair());
1842 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1843 __ rsbs(out.AsRegisterPairLow<Register>(),
1844 in.AsRegisterPairLow<Register>(),
1845 ShifterOperand(0));
1846 // We cannot emit an RSC (Reverse Subtract with Carry)
1847 // instruction here, as it does not exist in the Thumb-2
1848 // instruction set. We use the following approach
1849 // using SBC and SUB instead.
1850 //
1851 // out.hi = -C
1852 __ sbc(out.AsRegisterPairHigh<Register>(),
1853 out.AsRegisterPairHigh<Register>(),
1854 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1855 // out.hi = out.hi - in.hi
1856 __ sub(out.AsRegisterPairHigh<Register>(),
1857 out.AsRegisterPairHigh<Register>(),
1858 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1859 break;
1860
Roland Levillain88cb1752014-10-20 16:36:47 +01001861 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001862 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001863 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001864 break;
1865
Roland Levillain88cb1752014-10-20 16:36:47 +01001866 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001867 DCHECK(in.IsFpuRegisterPair());
1868 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1869 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001870 break;
1871
1872 default:
1873 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1874 }
1875}
1876
Roland Levillaindff1f282014-11-05 14:15:05 +00001877void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001878 Primitive::Type result_type = conversion->GetResultType();
1879 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001880 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001881
Roland Levillain5b3ee562015-04-14 16:02:41 +01001882 // The float-to-long, double-to-long and long-to-float type conversions
1883 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001884 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001885 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1886 && result_type == Primitive::kPrimLong)
1887 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001888 ? LocationSummary::kCall
1889 : LocationSummary::kNoCall;
1890 LocationSummary* locations =
1891 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1892
David Brazdilb2bd1c52015-03-25 11:17:37 +00001893 // The Java language does not allow treating boolean as an integral type but
1894 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001895
Roland Levillaindff1f282014-11-05 14:15:05 +00001896 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001897 case Primitive::kPrimByte:
1898 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001899 case Primitive::kPrimBoolean:
1900 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001901 case Primitive::kPrimShort:
1902 case Primitive::kPrimInt:
1903 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001904 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001905 locations->SetInAt(0, Location::RequiresRegister());
1906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1907 break;
1908
1909 default:
1910 LOG(FATAL) << "Unexpected type conversion from " << input_type
1911 << " to " << result_type;
1912 }
1913 break;
1914
Roland Levillain01a8d712014-11-14 16:27:39 +00001915 case Primitive::kPrimShort:
1916 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001917 case Primitive::kPrimBoolean:
1918 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001919 case Primitive::kPrimByte:
1920 case Primitive::kPrimInt:
1921 case Primitive::kPrimChar:
1922 // Processing a Dex `int-to-short' instruction.
1923 locations->SetInAt(0, Location::RequiresRegister());
1924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1925 break;
1926
1927 default:
1928 LOG(FATAL) << "Unexpected type conversion from " << input_type
1929 << " to " << result_type;
1930 }
1931 break;
1932
Roland Levillain946e1432014-11-11 17:35:19 +00001933 case Primitive::kPrimInt:
1934 switch (input_type) {
1935 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001936 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001937 locations->SetInAt(0, Location::Any());
1938 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1939 break;
1940
1941 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001942 // Processing a Dex `float-to-int' instruction.
1943 locations->SetInAt(0, Location::RequiresFpuRegister());
1944 locations->SetOut(Location::RequiresRegister());
1945 locations->AddTemp(Location::RequiresFpuRegister());
1946 break;
1947
Roland Levillain946e1432014-11-11 17:35:19 +00001948 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001949 // Processing a Dex `double-to-int' instruction.
1950 locations->SetInAt(0, Location::RequiresFpuRegister());
1951 locations->SetOut(Location::RequiresRegister());
1952 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001953 break;
1954
1955 default:
1956 LOG(FATAL) << "Unexpected type conversion from " << input_type
1957 << " to " << result_type;
1958 }
1959 break;
1960
Roland Levillaindff1f282014-11-05 14:15:05 +00001961 case Primitive::kPrimLong:
1962 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001963 case Primitive::kPrimBoolean:
1964 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001965 case Primitive::kPrimByte:
1966 case Primitive::kPrimShort:
1967 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001968 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001969 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001970 locations->SetInAt(0, Location::RequiresRegister());
1971 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1972 break;
1973
Roland Levillain624279f2014-12-04 11:54:28 +00001974 case Primitive::kPrimFloat: {
1975 // Processing a Dex `float-to-long' instruction.
1976 InvokeRuntimeCallingConvention calling_convention;
1977 locations->SetInAt(0, Location::FpuRegisterLocation(
1978 calling_convention.GetFpuRegisterAt(0)));
1979 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1980 break;
1981 }
1982
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001983 case Primitive::kPrimDouble: {
1984 // Processing a Dex `double-to-long' instruction.
1985 InvokeRuntimeCallingConvention calling_convention;
1986 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1987 calling_convention.GetFpuRegisterAt(0),
1988 calling_convention.GetFpuRegisterAt(1)));
1989 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001990 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001991 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001992
1993 default:
1994 LOG(FATAL) << "Unexpected type conversion from " << input_type
1995 << " to " << result_type;
1996 }
1997 break;
1998
Roland Levillain981e4542014-11-14 11:47:14 +00001999 case Primitive::kPrimChar:
2000 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002001 case Primitive::kPrimBoolean:
2002 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002003 case Primitive::kPrimByte:
2004 case Primitive::kPrimShort:
2005 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002006 // Processing a Dex `int-to-char' instruction.
2007 locations->SetInAt(0, Location::RequiresRegister());
2008 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2009 break;
2010
2011 default:
2012 LOG(FATAL) << "Unexpected type conversion from " << input_type
2013 << " to " << result_type;
2014 }
2015 break;
2016
Roland Levillaindff1f282014-11-05 14:15:05 +00002017 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002018 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002019 case Primitive::kPrimBoolean:
2020 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002021 case Primitive::kPrimByte:
2022 case Primitive::kPrimShort:
2023 case Primitive::kPrimInt:
2024 case Primitive::kPrimChar:
2025 // Processing a Dex `int-to-float' instruction.
2026 locations->SetInAt(0, Location::RequiresRegister());
2027 locations->SetOut(Location::RequiresFpuRegister());
2028 break;
2029
Roland Levillain5b3ee562015-04-14 16:02:41 +01002030 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002031 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002032 InvokeRuntimeCallingConvention calling_convention;
2033 locations->SetInAt(0, Location::RegisterPairLocation(
2034 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2035 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002036 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002037 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002038
Roland Levillaincff13742014-11-17 14:32:17 +00002039 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002040 // Processing a Dex `double-to-float' instruction.
2041 locations->SetInAt(0, Location::RequiresFpuRegister());
2042 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002043 break;
2044
2045 default:
2046 LOG(FATAL) << "Unexpected type conversion from " << input_type
2047 << " to " << result_type;
2048 };
2049 break;
2050
Roland Levillaindff1f282014-11-05 14:15:05 +00002051 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002052 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002053 case Primitive::kPrimBoolean:
2054 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002055 case Primitive::kPrimByte:
2056 case Primitive::kPrimShort:
2057 case Primitive::kPrimInt:
2058 case Primitive::kPrimChar:
2059 // Processing a Dex `int-to-double' instruction.
2060 locations->SetInAt(0, Location::RequiresRegister());
2061 locations->SetOut(Location::RequiresFpuRegister());
2062 break;
2063
2064 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002065 // Processing a Dex `long-to-double' instruction.
2066 locations->SetInAt(0, Location::RequiresRegister());
2067 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002068 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002069 locations->AddTemp(Location::RequiresFpuRegister());
2070 break;
2071
Roland Levillaincff13742014-11-17 14:32:17 +00002072 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002073 // Processing a Dex `float-to-double' instruction.
2074 locations->SetInAt(0, Location::RequiresFpuRegister());
2075 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002076 break;
2077
2078 default:
2079 LOG(FATAL) << "Unexpected type conversion from " << input_type
2080 << " to " << result_type;
2081 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002082 break;
2083
2084 default:
2085 LOG(FATAL) << "Unexpected type conversion from " << input_type
2086 << " to " << result_type;
2087 }
2088}
2089
2090void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2091 LocationSummary* locations = conversion->GetLocations();
2092 Location out = locations->Out();
2093 Location in = locations->InAt(0);
2094 Primitive::Type result_type = conversion->GetResultType();
2095 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002096 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002097 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002098 case Primitive::kPrimByte:
2099 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002100 case Primitive::kPrimBoolean:
2101 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002102 case Primitive::kPrimShort:
2103 case Primitive::kPrimInt:
2104 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002105 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002106 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002107 break;
2108
2109 default:
2110 LOG(FATAL) << "Unexpected type conversion from " << input_type
2111 << " to " << result_type;
2112 }
2113 break;
2114
Roland Levillain01a8d712014-11-14 16:27:39 +00002115 case Primitive::kPrimShort:
2116 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002117 case Primitive::kPrimBoolean:
2118 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002119 case Primitive::kPrimByte:
2120 case Primitive::kPrimInt:
2121 case Primitive::kPrimChar:
2122 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002124 break;
2125
2126 default:
2127 LOG(FATAL) << "Unexpected type conversion from " << input_type
2128 << " to " << result_type;
2129 }
2130 break;
2131
Roland Levillain946e1432014-11-11 17:35:19 +00002132 case Primitive::kPrimInt:
2133 switch (input_type) {
2134 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002135 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002136 DCHECK(out.IsRegister());
2137 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002139 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002141 } else {
2142 DCHECK(in.IsConstant());
2143 DCHECK(in.GetConstant()->IsLongConstant());
2144 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002146 }
2147 break;
2148
Roland Levillain3f8f9362014-12-02 17:45:01 +00002149 case Primitive::kPrimFloat: {
2150 // Processing a Dex `float-to-int' instruction.
2151 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2152 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2153 __ vcvtis(temp, temp);
2154 __ vmovrs(out.AsRegister<Register>(), temp);
2155 break;
2156 }
2157
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002158 case Primitive::kPrimDouble: {
2159 // Processing a Dex `double-to-int' instruction.
2160 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2161 DRegister temp_d = FromLowSToD(temp_s);
2162 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2163 __ vcvtid(temp_s, temp_d);
2164 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002165 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002166 }
Roland Levillain946e1432014-11-11 17:35:19 +00002167
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::kPrimLong:
2175 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002176 case Primitive::kPrimBoolean:
2177 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002178 case Primitive::kPrimByte:
2179 case Primitive::kPrimShort:
2180 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002181 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002182 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002183 DCHECK(out.IsRegisterPair());
2184 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002185 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002186 // Sign extension.
2187 __ Asr(out.AsRegisterPairHigh<Register>(),
2188 out.AsRegisterPairLow<Register>(),
2189 31);
2190 break;
2191
2192 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002193 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002194 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2195 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002196 conversion->GetDexPc(),
2197 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002198 break;
2199
Roland Levillaindff1f282014-11-05 14:15:05 +00002200 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002201 // Processing a Dex `double-to-long' instruction.
2202 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2203 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002204 conversion->GetDexPc(),
2205 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002206 break;
2207
2208 default:
2209 LOG(FATAL) << "Unexpected type conversion from " << input_type
2210 << " to " << result_type;
2211 }
2212 break;
2213
Roland Levillain981e4542014-11-14 11:47:14 +00002214 case Primitive::kPrimChar:
2215 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002216 case Primitive::kPrimBoolean:
2217 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002218 case Primitive::kPrimByte:
2219 case Primitive::kPrimShort:
2220 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002221 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002222 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002223 break;
2224
2225 default:
2226 LOG(FATAL) << "Unexpected type conversion from " << input_type
2227 << " to " << result_type;
2228 }
2229 break;
2230
Roland Levillaindff1f282014-11-05 14:15:05 +00002231 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002232 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002233 case Primitive::kPrimBoolean:
2234 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002235 case Primitive::kPrimByte:
2236 case Primitive::kPrimShort:
2237 case Primitive::kPrimInt:
2238 case Primitive::kPrimChar: {
2239 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002240 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2241 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002242 break;
2243 }
2244
Roland Levillain5b3ee562015-04-14 16:02:41 +01002245 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002246 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002247 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2248 conversion,
2249 conversion->GetDexPc(),
2250 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002251 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002252
Roland Levillaincff13742014-11-17 14:32:17 +00002253 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002254 // Processing a Dex `double-to-float' instruction.
2255 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2256 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002257 break;
2258
2259 default:
2260 LOG(FATAL) << "Unexpected type conversion from " << input_type
2261 << " to " << result_type;
2262 };
2263 break;
2264
Roland Levillaindff1f282014-11-05 14:15:05 +00002265 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002266 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002267 case Primitive::kPrimBoolean:
2268 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002269 case Primitive::kPrimByte:
2270 case Primitive::kPrimShort:
2271 case Primitive::kPrimInt:
2272 case Primitive::kPrimChar: {
2273 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002274 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002275 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2276 out.AsFpuRegisterPairLow<SRegister>());
2277 break;
2278 }
2279
Roland Levillain647b9ed2014-11-27 12:06:00 +00002280 case Primitive::kPrimLong: {
2281 // Processing a Dex `long-to-double' instruction.
2282 Register low = in.AsRegisterPairLow<Register>();
2283 Register high = in.AsRegisterPairHigh<Register>();
2284 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2285 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002286 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002287 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002288 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2289 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002290
Roland Levillain682393c2015-04-14 15:57:52 +01002291 // temp_d = int-to-double(high)
2292 __ vmovsr(temp_s, high);
2293 __ vcvtdi(temp_d, temp_s);
2294 // constant_d = k2Pow32EncodingForDouble
2295 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2296 // out_d = unsigned-to-double(low)
2297 __ vmovsr(out_s, low);
2298 __ vcvtdu(out_d, out_s);
2299 // out_d += temp_d * constant_d
2300 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002301 break;
2302 }
2303
Roland Levillaincff13742014-11-17 14:32:17 +00002304 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002305 // Processing a Dex `float-to-double' instruction.
2306 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2307 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002308 break;
2309
2310 default:
2311 LOG(FATAL) << "Unexpected type conversion from " << input_type
2312 << " to " << result_type;
2313 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002314 break;
2315
2316 default:
2317 LOG(FATAL) << "Unexpected type conversion from " << input_type
2318 << " to " << result_type;
2319 }
2320}
2321
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002322void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002323 LocationSummary* locations =
2324 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002325 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002326 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002327 locations->SetInAt(0, Location::RequiresRegister());
2328 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2330 break;
2331 }
2332
2333 case Primitive::kPrimLong: {
2334 locations->SetInAt(0, Location::RequiresRegister());
2335 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002336 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002337 break;
2338 }
2339
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002340 case Primitive::kPrimFloat:
2341 case Primitive::kPrimDouble: {
2342 locations->SetInAt(0, Location::RequiresFpuRegister());
2343 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002344 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002345 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002346 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002347
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002348 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002349 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002350 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002351}
2352
2353void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2354 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002355 Location out = locations->Out();
2356 Location first = locations->InAt(0);
2357 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002358 switch (add->GetResultType()) {
2359 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002360 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002361 __ add(out.AsRegister<Register>(),
2362 first.AsRegister<Register>(),
2363 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002364 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002365 __ AddConstant(out.AsRegister<Register>(),
2366 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002367 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002368 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002369 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002370
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002371 case Primitive::kPrimLong: {
2372 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002373 __ adds(out.AsRegisterPairLow<Register>(),
2374 first.AsRegisterPairLow<Register>(),
2375 ShifterOperand(second.AsRegisterPairLow<Register>()));
2376 __ adc(out.AsRegisterPairHigh<Register>(),
2377 first.AsRegisterPairHigh<Register>(),
2378 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002379 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002380 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002381
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002382 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002383 __ vadds(out.AsFpuRegister<SRegister>(),
2384 first.AsFpuRegister<SRegister>(),
2385 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002386 break;
2387
2388 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002389 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2390 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2391 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002392 break;
2393
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002394 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002395 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002396 }
2397}
2398
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002399void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002400 LocationSummary* locations =
2401 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002402 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002403 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002404 locations->SetInAt(0, Location::RequiresRegister());
2405 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002406 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2407 break;
2408 }
2409
2410 case Primitive::kPrimLong: {
2411 locations->SetInAt(0, Location::RequiresRegister());
2412 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002414 break;
2415 }
Calin Juravle11351682014-10-23 15:38:15 +01002416 case Primitive::kPrimFloat:
2417 case Primitive::kPrimDouble: {
2418 locations->SetInAt(0, Location::RequiresFpuRegister());
2419 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002420 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002421 break;
Calin Juravle11351682014-10-23 15:38:15 +01002422 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002423 default:
Calin Juravle11351682014-10-23 15:38:15 +01002424 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002425 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002426}
2427
2428void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2429 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002430 Location out = locations->Out();
2431 Location first = locations->InAt(0);
2432 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002433 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002434 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002435 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002436 __ sub(out.AsRegister<Register>(),
2437 first.AsRegister<Register>(),
2438 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002439 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002440 __ AddConstant(out.AsRegister<Register>(),
2441 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002442 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002443 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002444 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002445 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002446
Calin Juravle11351682014-10-23 15:38:15 +01002447 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002448 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002449 __ subs(out.AsRegisterPairLow<Register>(),
2450 first.AsRegisterPairLow<Register>(),
2451 ShifterOperand(second.AsRegisterPairLow<Register>()));
2452 __ sbc(out.AsRegisterPairHigh<Register>(),
2453 first.AsRegisterPairHigh<Register>(),
2454 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002455 break;
Calin Juravle11351682014-10-23 15:38:15 +01002456 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002457
Calin Juravle11351682014-10-23 15:38:15 +01002458 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002459 __ vsubs(out.AsFpuRegister<SRegister>(),
2460 first.AsFpuRegister<SRegister>(),
2461 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002462 break;
Calin Juravle11351682014-10-23 15:38:15 +01002463 }
2464
2465 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002466 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2467 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2468 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002469 break;
2470 }
2471
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002472
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002473 default:
Calin Juravle11351682014-10-23 15:38:15 +01002474 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002475 }
2476}
2477
Calin Juravle34bacdf2014-10-07 20:23:36 +01002478void LocationsBuilderARM::VisitMul(HMul* mul) {
2479 LocationSummary* locations =
2480 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2481 switch (mul->GetResultType()) {
2482 case Primitive::kPrimInt:
2483 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002484 locations->SetInAt(0, Location::RequiresRegister());
2485 locations->SetInAt(1, Location::RequiresRegister());
2486 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002487 break;
2488 }
2489
Calin Juravleb5bfa962014-10-21 18:02:24 +01002490 case Primitive::kPrimFloat:
2491 case Primitive::kPrimDouble: {
2492 locations->SetInAt(0, Location::RequiresFpuRegister());
2493 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002495 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002496 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002497
2498 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002499 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002500 }
2501}
2502
2503void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2504 LocationSummary* locations = mul->GetLocations();
2505 Location out = locations->Out();
2506 Location first = locations->InAt(0);
2507 Location second = locations->InAt(1);
2508 switch (mul->GetResultType()) {
2509 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002510 __ mul(out.AsRegister<Register>(),
2511 first.AsRegister<Register>(),
2512 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002513 break;
2514 }
2515 case Primitive::kPrimLong: {
2516 Register out_hi = out.AsRegisterPairHigh<Register>();
2517 Register out_lo = out.AsRegisterPairLow<Register>();
2518 Register in1_hi = first.AsRegisterPairHigh<Register>();
2519 Register in1_lo = first.AsRegisterPairLow<Register>();
2520 Register in2_hi = second.AsRegisterPairHigh<Register>();
2521 Register in2_lo = second.AsRegisterPairLow<Register>();
2522
2523 // Extra checks to protect caused by the existence of R1_R2.
2524 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2525 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2526 DCHECK_NE(out_hi, in1_lo);
2527 DCHECK_NE(out_hi, in2_lo);
2528
2529 // input: in1 - 64 bits, in2 - 64 bits
2530 // output: out
2531 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2532 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2533 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2534
2535 // IP <- in1.lo * in2.hi
2536 __ mul(IP, in1_lo, in2_hi);
2537 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2538 __ mla(out_hi, in1_hi, in2_lo, IP);
2539 // out.lo <- (in1.lo * in2.lo)[31:0];
2540 __ umull(out_lo, IP, in1_lo, in2_lo);
2541 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2542 __ add(out_hi, out_hi, ShifterOperand(IP));
2543 break;
2544 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002545
2546 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002547 __ vmuls(out.AsFpuRegister<SRegister>(),
2548 first.AsFpuRegister<SRegister>(),
2549 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002550 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002551 }
2552
2553 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002554 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2555 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2556 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002557 break;
2558 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002559
2560 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002561 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002562 }
2563}
2564
Zheng Xuc6667102015-05-15 16:08:45 +08002565void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2566 DCHECK(instruction->IsDiv() || instruction->IsRem());
2567 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2568
2569 LocationSummary* locations = instruction->GetLocations();
2570 Location second = locations->InAt(1);
2571 DCHECK(second.IsConstant());
2572
2573 Register out = locations->Out().AsRegister<Register>();
2574 Register dividend = locations->InAt(0).AsRegister<Register>();
2575 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2576 DCHECK(imm == 1 || imm == -1);
2577
2578 if (instruction->IsRem()) {
2579 __ LoadImmediate(out, 0);
2580 } else {
2581 if (imm == 1) {
2582 __ Mov(out, dividend);
2583 } else {
2584 __ rsb(out, dividend, ShifterOperand(0));
2585 }
2586 }
2587}
2588
2589void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2590 DCHECK(instruction->IsDiv() || instruction->IsRem());
2591 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2592
2593 LocationSummary* locations = instruction->GetLocations();
2594 Location second = locations->InAt(1);
2595 DCHECK(second.IsConstant());
2596
2597 Register out = locations->Out().AsRegister<Register>();
2598 Register dividend = locations->InAt(0).AsRegister<Register>();
2599 Register temp = locations->GetTemp(0).AsRegister<Register>();
2600 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002601 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002602 DCHECK(IsPowerOfTwo(abs_imm));
2603 int ctz_imm = CTZ(abs_imm);
2604
2605 if (ctz_imm == 1) {
2606 __ Lsr(temp, dividend, 32 - ctz_imm);
2607 } else {
2608 __ Asr(temp, dividend, 31);
2609 __ Lsr(temp, temp, 32 - ctz_imm);
2610 }
2611 __ add(out, temp, ShifterOperand(dividend));
2612
2613 if (instruction->IsDiv()) {
2614 __ Asr(out, out, ctz_imm);
2615 if (imm < 0) {
2616 __ rsb(out, out, ShifterOperand(0));
2617 }
2618 } else {
2619 __ ubfx(out, out, 0, ctz_imm);
2620 __ sub(out, out, ShifterOperand(temp));
2621 }
2622}
2623
2624void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2625 DCHECK(instruction->IsDiv() || instruction->IsRem());
2626 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2627
2628 LocationSummary* locations = instruction->GetLocations();
2629 Location second = locations->InAt(1);
2630 DCHECK(second.IsConstant());
2631
2632 Register out = locations->Out().AsRegister<Register>();
2633 Register dividend = locations->InAt(0).AsRegister<Register>();
2634 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2635 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2636 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2637
2638 int64_t magic;
2639 int shift;
2640 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2641
2642 __ LoadImmediate(temp1, magic);
2643 __ smull(temp2, temp1, dividend, temp1);
2644
2645 if (imm > 0 && magic < 0) {
2646 __ add(temp1, temp1, ShifterOperand(dividend));
2647 } else if (imm < 0 && magic > 0) {
2648 __ sub(temp1, temp1, ShifterOperand(dividend));
2649 }
2650
2651 if (shift != 0) {
2652 __ Asr(temp1, temp1, shift);
2653 }
2654
2655 if (instruction->IsDiv()) {
2656 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2657 } else {
2658 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2659 // TODO: Strength reduction for mls.
2660 __ LoadImmediate(temp2, imm);
2661 __ mls(out, temp1, temp2, dividend);
2662 }
2663}
2664
2665void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2666 DCHECK(instruction->IsDiv() || instruction->IsRem());
2667 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2668
2669 LocationSummary* locations = instruction->GetLocations();
2670 Location second = locations->InAt(1);
2671 DCHECK(second.IsConstant());
2672
2673 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2674 if (imm == 0) {
2675 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2676 } else if (imm == 1 || imm == -1) {
2677 DivRemOneOrMinusOne(instruction);
2678 } else if (IsPowerOfTwo(std::abs(imm))) {
2679 DivRemByPowerOfTwo(instruction);
2680 } else {
2681 DCHECK(imm <= -2 || imm >= 2);
2682 GenerateDivRemWithAnyConstant(instruction);
2683 }
2684}
2685
Calin Juravle7c4954d2014-10-28 16:57:40 +00002686void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002687 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2688 if (div->GetResultType() == Primitive::kPrimLong) {
2689 // pLdiv runtime call.
2690 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002691 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2692 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002693 } else if (div->GetResultType() == Primitive::kPrimInt &&
2694 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2695 // pIdivmod runtime call.
2696 call_kind = LocationSummary::kCall;
2697 }
2698
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002699 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2700
Calin Juravle7c4954d2014-10-28 16:57:40 +00002701 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002702 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002703 if (div->InputAt(1)->IsConstant()) {
2704 locations->SetInAt(0, Location::RequiresRegister());
2705 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2706 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2707 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2708 if (abs_imm <= 1) {
2709 // No temp register required.
2710 } else {
2711 locations->AddTemp(Location::RequiresRegister());
2712 if (!IsPowerOfTwo(abs_imm)) {
2713 locations->AddTemp(Location::RequiresRegister());
2714 }
2715 }
2716 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002717 locations->SetInAt(0, Location::RequiresRegister());
2718 locations->SetInAt(1, Location::RequiresRegister());
2719 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2720 } else {
2721 InvokeRuntimeCallingConvention calling_convention;
2722 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2723 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2724 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2725 // we only need the former.
2726 locations->SetOut(Location::RegisterLocation(R0));
2727 }
Calin Juravled0d48522014-11-04 16:40:20 +00002728 break;
2729 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002730 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002731 InvokeRuntimeCallingConvention calling_convention;
2732 locations->SetInAt(0, Location::RegisterPairLocation(
2733 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2734 locations->SetInAt(1, Location::RegisterPairLocation(
2735 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002736 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002737 break;
2738 }
2739 case Primitive::kPrimFloat:
2740 case Primitive::kPrimDouble: {
2741 locations->SetInAt(0, Location::RequiresFpuRegister());
2742 locations->SetInAt(1, Location::RequiresFpuRegister());
2743 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2744 break;
2745 }
2746
2747 default:
2748 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2749 }
2750}
2751
2752void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2753 LocationSummary* locations = div->GetLocations();
2754 Location out = locations->Out();
2755 Location first = locations->InAt(0);
2756 Location second = locations->InAt(1);
2757
2758 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002759 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002760 if (second.IsConstant()) {
2761 GenerateDivRemConstantIntegral(div);
2762 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002763 __ sdiv(out.AsRegister<Register>(),
2764 first.AsRegister<Register>(),
2765 second.AsRegister<Register>());
2766 } else {
2767 InvokeRuntimeCallingConvention calling_convention;
2768 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2769 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2770 DCHECK_EQ(R0, out.AsRegister<Register>());
2771
2772 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2773 }
Calin Juravled0d48522014-11-04 16:40:20 +00002774 break;
2775 }
2776
Calin Juravle7c4954d2014-10-28 16:57:40 +00002777 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002778 InvokeRuntimeCallingConvention calling_convention;
2779 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2780 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2781 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2782 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2783 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002784 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002785
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002786 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002787 break;
2788 }
2789
2790 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002791 __ vdivs(out.AsFpuRegister<SRegister>(),
2792 first.AsFpuRegister<SRegister>(),
2793 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002794 break;
2795 }
2796
2797 case Primitive::kPrimDouble: {
2798 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2799 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2800 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2801 break;
2802 }
2803
2804 default:
2805 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2806 }
2807}
2808
Calin Juravlebacfec32014-11-14 15:54:36 +00002809void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002810 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002811
2812 // Most remainders are implemented in the runtime.
2813 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002814 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2815 // sdiv will be replaced by other instruction sequence.
2816 call_kind = LocationSummary::kNoCall;
2817 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2818 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002819 // Have hardware divide instruction for int, do it with three instructions.
2820 call_kind = LocationSummary::kNoCall;
2821 }
2822
Calin Juravlebacfec32014-11-14 15:54:36 +00002823 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2824
Calin Juravled2ec87d2014-12-08 14:24:46 +00002825 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002826 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002827 if (rem->InputAt(1)->IsConstant()) {
2828 locations->SetInAt(0, Location::RequiresRegister());
2829 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2831 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2832 if (abs_imm <= 1) {
2833 // No temp register required.
2834 } else {
2835 locations->AddTemp(Location::RequiresRegister());
2836 if (!IsPowerOfTwo(abs_imm)) {
2837 locations->AddTemp(Location::RequiresRegister());
2838 }
2839 }
2840 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002841 locations->SetInAt(0, Location::RequiresRegister());
2842 locations->SetInAt(1, Location::RequiresRegister());
2843 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2844 locations->AddTemp(Location::RequiresRegister());
2845 } else {
2846 InvokeRuntimeCallingConvention calling_convention;
2847 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2848 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2849 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2850 // we only need the latter.
2851 locations->SetOut(Location::RegisterLocation(R1));
2852 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002853 break;
2854 }
2855 case Primitive::kPrimLong: {
2856 InvokeRuntimeCallingConvention calling_convention;
2857 locations->SetInAt(0, Location::RegisterPairLocation(
2858 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2859 locations->SetInAt(1, Location::RegisterPairLocation(
2860 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2861 // The runtime helper puts the output in R2,R3.
2862 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2863 break;
2864 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002865 case Primitive::kPrimFloat: {
2866 InvokeRuntimeCallingConvention calling_convention;
2867 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2868 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2869 locations->SetOut(Location::FpuRegisterLocation(S0));
2870 break;
2871 }
2872
Calin Juravlebacfec32014-11-14 15:54:36 +00002873 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002874 InvokeRuntimeCallingConvention calling_convention;
2875 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2876 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2877 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2878 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2879 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002880 break;
2881 }
2882
2883 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002884 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002885 }
2886}
2887
2888void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2889 LocationSummary* locations = rem->GetLocations();
2890 Location out = locations->Out();
2891 Location first = locations->InAt(0);
2892 Location second = locations->InAt(1);
2893
Calin Juravled2ec87d2014-12-08 14:24:46 +00002894 Primitive::Type type = rem->GetResultType();
2895 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002896 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002897 if (second.IsConstant()) {
2898 GenerateDivRemConstantIntegral(rem);
2899 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002900 Register reg1 = first.AsRegister<Register>();
2901 Register reg2 = second.AsRegister<Register>();
2902 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002903
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002904 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002905 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002906 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002907 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002908 } else {
2909 InvokeRuntimeCallingConvention calling_convention;
2910 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2911 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2912 DCHECK_EQ(R1, out.AsRegister<Register>());
2913
2914 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2915 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002916 break;
2917 }
2918
2919 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002920 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002921 break;
2922 }
2923
Calin Juravled2ec87d2014-12-08 14:24:46 +00002924 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002925 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002926 break;
2927 }
2928
Calin Juravlebacfec32014-11-14 15:54:36 +00002929 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002930 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002931 break;
2932 }
2933
2934 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002935 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002936 }
2937}
2938
Calin Juravled0d48522014-11-04 16:40:20 +00002939void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002940 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2941 ? LocationSummary::kCallOnSlowPath
2942 : LocationSummary::kNoCall;
2943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002944 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002945 if (instruction->HasUses()) {
2946 locations->SetOut(Location::SameAsFirstInput());
2947 }
2948}
2949
2950void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002951 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00002952 codegen_->AddSlowPath(slow_path);
2953
2954 LocationSummary* locations = instruction->GetLocations();
2955 Location value = locations->InAt(0);
2956
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002957 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002958 case Primitive::kPrimByte:
2959 case Primitive::kPrimChar:
2960 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002961 case Primitive::kPrimInt: {
2962 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002963 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002964 } else {
2965 DCHECK(value.IsConstant()) << value;
2966 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2967 __ b(slow_path->GetEntryLabel());
2968 }
2969 }
2970 break;
2971 }
2972 case Primitive::kPrimLong: {
2973 if (value.IsRegisterPair()) {
2974 __ orrs(IP,
2975 value.AsRegisterPairLow<Register>(),
2976 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2977 __ b(slow_path->GetEntryLabel(), EQ);
2978 } else {
2979 DCHECK(value.IsConstant()) << value;
2980 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2981 __ b(slow_path->GetEntryLabel());
2982 }
2983 }
2984 break;
2985 default:
2986 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2987 }
2988 }
Calin Juravled0d48522014-11-04 16:40:20 +00002989}
2990
Calin Juravle9aec02f2014-11-18 23:06:35 +00002991void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2992 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2993
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002994 LocationSummary* locations =
2995 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002996
2997 switch (op->GetResultType()) {
2998 case Primitive::kPrimInt: {
2999 locations->SetInAt(0, Location::RequiresRegister());
3000 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003001 // Make the output overlap, as it will be used to hold the masked
3002 // second input.
3003 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003004 break;
3005 }
3006 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003007 locations->SetInAt(0, Location::RequiresRegister());
3008 locations->SetInAt(1, Location::RequiresRegister());
3009 locations->AddTemp(Location::RequiresRegister());
3010 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003011 break;
3012 }
3013 default:
3014 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3015 }
3016}
3017
3018void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3019 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3020
3021 LocationSummary* locations = op->GetLocations();
3022 Location out = locations->Out();
3023 Location first = locations->InAt(0);
3024 Location second = locations->InAt(1);
3025
3026 Primitive::Type type = op->GetResultType();
3027 switch (type) {
3028 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003029 Register out_reg = out.AsRegister<Register>();
3030 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003031 // Arm doesn't mask the shift count so we need to do it ourselves.
3032 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003034 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003035 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003036 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003037 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003038 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003039 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003040 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003041 }
3042 } else {
3043 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3044 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
3045 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
3046 __ Mov(out_reg, first_reg);
3047 } else if (op->IsShl()) {
3048 __ Lsl(out_reg, first_reg, shift_value);
3049 } else if (op->IsShr()) {
3050 __ Asr(out_reg, first_reg, shift_value);
3051 } else {
3052 __ Lsr(out_reg, first_reg, shift_value);
3053 }
3054 }
3055 break;
3056 }
3057 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003058 Register o_h = out.AsRegisterPairHigh<Register>();
3059 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003060
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003061 Register temp = locations->GetTemp(0).AsRegister<Register>();
3062
3063 Register high = first.AsRegisterPairHigh<Register>();
3064 Register low = first.AsRegisterPairLow<Register>();
3065
3066 Register second_reg = second.AsRegister<Register>();
3067
Calin Juravle9aec02f2014-11-18 23:06:35 +00003068 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003069 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003070 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003071 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003072 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003073 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003074 __ Lsr(temp, low, temp);
3075 __ orr(o_h, o_h, ShifterOperand(temp));
3076 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003077 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003078 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003079 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003080 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003081 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003082 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003083 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003084 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003085 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003086 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003087 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003088 __ Lsl(temp, high, temp);
3089 __ orr(o_l, o_l, ShifterOperand(temp));
3090 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003091 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003092 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003093 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003094 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003095 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003096 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003097 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003098 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003099 __ Lsr(o_l, low, o_h);
3100 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003101 __ Lsl(temp, high, temp);
3102 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003103 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003104 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003105 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003106 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003107 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003108 break;
3109 }
3110 default:
3111 LOG(FATAL) << "Unexpected operation type " << type;
3112 }
3113}
3114
3115void LocationsBuilderARM::VisitShl(HShl* shl) {
3116 HandleShift(shl);
3117}
3118
3119void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3120 HandleShift(shl);
3121}
3122
3123void LocationsBuilderARM::VisitShr(HShr* shr) {
3124 HandleShift(shr);
3125}
3126
3127void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3128 HandleShift(shr);
3129}
3130
3131void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3132 HandleShift(ushr);
3133}
3134
3135void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3136 HandleShift(ushr);
3137}
3138
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003139void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003140 LocationSummary* locations =
3141 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003142 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003143 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003144 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003145 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003146}
3147
3148void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3149 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003150 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003151 // Note: if heap poisoning is enabled, the entry point takes cares
3152 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003153 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003154 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003155 instruction->GetDexPc(),
3156 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003157}
3158
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003159void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3160 LocationSummary* locations =
3161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3162 InvokeRuntimeCallingConvention calling_convention;
3163 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003164 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003165 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003166 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003167}
3168
3169void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3170 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003171 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003172 // Note: if heap poisoning is enabled, the entry point takes cares
3173 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003174 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003175 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003176 instruction->GetDexPc(),
3177 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003178}
3179
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003180void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003181 LocationSummary* locations =
3182 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003183 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3184 if (location.IsStackSlot()) {
3185 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3186 } else if (location.IsDoubleStackSlot()) {
3187 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003188 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003189 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003190}
3191
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003192void InstructionCodeGeneratorARM::VisitParameterValue(
3193 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003194 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003195}
3196
3197void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3198 LocationSummary* locations =
3199 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3200 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3201}
3202
3203void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3204 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003205}
3206
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003207void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003208 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003209 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003210 locations->SetInAt(0, Location::RequiresRegister());
3211 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003212}
3213
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003214void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3215 LocationSummary* locations = not_->GetLocations();
3216 Location out = locations->Out();
3217 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003218 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003219 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003220 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003221 break;
3222
3223 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003224 __ mvn(out.AsRegisterPairLow<Register>(),
3225 ShifterOperand(in.AsRegisterPairLow<Register>()));
3226 __ mvn(out.AsRegisterPairHigh<Register>(),
3227 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003228 break;
3229
3230 default:
3231 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3232 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003233}
3234
David Brazdil66d126e2015-04-03 16:02:44 +01003235void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3236 LocationSummary* locations =
3237 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3238 locations->SetInAt(0, Location::RequiresRegister());
3239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3240}
3241
3242void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003243 LocationSummary* locations = bool_not->GetLocations();
3244 Location out = locations->Out();
3245 Location in = locations->InAt(0);
3246 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3247}
3248
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003249void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003250 LocationSummary* locations =
3251 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003252 switch (compare->InputAt(0)->GetType()) {
3253 case Primitive::kPrimLong: {
3254 locations->SetInAt(0, Location::RequiresRegister());
3255 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003256 // Output overlaps because it is written before doing the low comparison.
3257 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003258 break;
3259 }
3260 case Primitive::kPrimFloat:
3261 case Primitive::kPrimDouble: {
3262 locations->SetInAt(0, Location::RequiresFpuRegister());
3263 locations->SetInAt(1, Location::RequiresFpuRegister());
3264 locations->SetOut(Location::RequiresRegister());
3265 break;
3266 }
3267 default:
3268 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3269 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003270}
3271
3272void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003273 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003274 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003275 Location left = locations->InAt(0);
3276 Location right = locations->InAt(1);
3277
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003278 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003279 Primitive::Type type = compare->InputAt(0)->GetType();
3280 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003281 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003282 __ cmp(left.AsRegisterPairHigh<Register>(),
3283 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003284 __ b(&less, LT);
3285 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003286 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003287 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003288 __ cmp(left.AsRegisterPairLow<Register>(),
3289 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003290 break;
3291 }
3292 case Primitive::kPrimFloat:
3293 case Primitive::kPrimDouble: {
3294 __ LoadImmediate(out, 0);
3295 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003296 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003297 } else {
3298 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3299 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3300 }
3301 __ vmstat(); // transfer FP status register to ARM APSR.
3302 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003303 break;
3304 }
3305 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003306 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003307 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003308 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003309 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003310
3311 __ Bind(&greater);
3312 __ LoadImmediate(out, 1);
3313 __ b(&done);
3314
3315 __ Bind(&less);
3316 __ LoadImmediate(out, -1);
3317
3318 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003319}
3320
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003321void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003322 LocationSummary* locations =
3323 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003324 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3325 locations->SetInAt(i, Location::Any());
3326 }
3327 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003328}
3329
3330void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003331 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003332 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003333}
3334
Calin Juravle52c48962014-12-16 17:02:57 +00003335void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3336 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003337 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003338 switch (kind) {
3339 case MemBarrierKind::kAnyStore:
3340 case MemBarrierKind::kLoadAny:
3341 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003342 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003343 break;
3344 }
3345 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003346 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003347 break;
3348 }
3349 default:
3350 LOG(FATAL) << "Unexpected memory barrier " << kind;
3351 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003352 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003353}
3354
3355void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3356 uint32_t offset,
3357 Register out_lo,
3358 Register out_hi) {
3359 if (offset != 0) {
3360 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003361 __ add(IP, addr, ShifterOperand(out_lo));
3362 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003363 }
3364 __ ldrexd(out_lo, out_hi, addr);
3365}
3366
3367void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3368 uint32_t offset,
3369 Register value_lo,
3370 Register value_hi,
3371 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003372 Register temp2,
3373 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003374 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003375 if (offset != 0) {
3376 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003377 __ add(IP, addr, ShifterOperand(temp1));
3378 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003379 }
3380 __ Bind(&fail);
3381 // We need a load followed by store. (The address used in a STREX instruction must
3382 // be the same as the address in the most recently executed LDREX instruction.)
3383 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003384 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003385 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003386 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003387}
3388
3389void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3390 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3391
Nicolas Geoffray39468442014-09-02 15:17:15 +01003392 LocationSummary* locations =
3393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003394 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003395
Calin Juravle52c48962014-12-16 17:02:57 +00003396 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003397 if (Primitive::IsFloatingPointType(field_type)) {
3398 locations->SetInAt(1, Location::RequiresFpuRegister());
3399 } else {
3400 locations->SetInAt(1, Location::RequiresRegister());
3401 }
3402
Calin Juravle52c48962014-12-16 17:02:57 +00003403 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003404 bool generate_volatile = field_info.IsVolatile()
3405 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003406 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003407 bool needs_write_barrier =
3408 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003409 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003410 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003411 if (needs_write_barrier) {
3412 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003413 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003414 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003415 // Arm encoding have some additional constraints for ldrexd/strexd:
3416 // - registers need to be consecutive
3417 // - the first register should be even but not R14.
3418 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3419 // enable Arm encoding.
3420 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3421
3422 locations->AddTemp(Location::RequiresRegister());
3423 locations->AddTemp(Location::RequiresRegister());
3424 if (field_type == Primitive::kPrimDouble) {
3425 // For doubles we need two more registers to copy the value.
3426 locations->AddTemp(Location::RegisterLocation(R2));
3427 locations->AddTemp(Location::RegisterLocation(R3));
3428 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003429 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003430}
3431
Calin Juravle52c48962014-12-16 17:02:57 +00003432void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003433 const FieldInfo& field_info,
3434 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003435 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3436
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003437 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003438 Register base = locations->InAt(0).AsRegister<Register>();
3439 Location value = locations->InAt(1);
3440
3441 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003442 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003443 Primitive::Type field_type = field_info.GetFieldType();
3444 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003445 bool needs_write_barrier =
3446 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003447
3448 if (is_volatile) {
3449 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3450 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003451
3452 switch (field_type) {
3453 case Primitive::kPrimBoolean:
3454 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003455 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003456 break;
3457 }
3458
3459 case Primitive::kPrimShort:
3460 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003461 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003462 break;
3463 }
3464
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003465 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003466 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003467 if (kPoisonHeapReferences && needs_write_barrier) {
3468 // Note that in the case where `value` is a null reference,
3469 // we do not enter this block, as a null reference does not
3470 // need poisoning.
3471 DCHECK_EQ(field_type, Primitive::kPrimNot);
3472 Register temp = locations->GetTemp(0).AsRegister<Register>();
3473 __ Mov(temp, value.AsRegister<Register>());
3474 __ PoisonHeapReference(temp);
3475 __ StoreToOffset(kStoreWord, temp, base, offset);
3476 } else {
3477 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3478 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003479 break;
3480 }
3481
3482 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003483 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003484 GenerateWideAtomicStore(base, offset,
3485 value.AsRegisterPairLow<Register>(),
3486 value.AsRegisterPairHigh<Register>(),
3487 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003488 locations->GetTemp(1).AsRegister<Register>(),
3489 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003490 } else {
3491 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003492 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003493 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003494 break;
3495 }
3496
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003497 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003498 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003499 break;
3500 }
3501
3502 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003503 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003504 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003505 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3506 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3507
3508 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3509
3510 GenerateWideAtomicStore(base, offset,
3511 value_reg_lo,
3512 value_reg_hi,
3513 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003514 locations->GetTemp(3).AsRegister<Register>(),
3515 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003516 } else {
3517 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003518 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003519 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003520 break;
3521 }
3522
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003523 case Primitive::kPrimVoid:
3524 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003525 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003526 }
Calin Juravle52c48962014-12-16 17:02:57 +00003527
Calin Juravle77520bc2015-01-12 18:45:46 +00003528 // Longs and doubles are handled in the switch.
3529 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3530 codegen_->MaybeRecordImplicitNullCheck(instruction);
3531 }
3532
3533 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3534 Register temp = locations->GetTemp(0).AsRegister<Register>();
3535 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003536 codegen_->MarkGCCard(
3537 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003538 }
3539
Calin Juravle52c48962014-12-16 17:02:57 +00003540 if (is_volatile) {
3541 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3542 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003543}
3544
Calin Juravle52c48962014-12-16 17:02:57 +00003545void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3546 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003547 LocationSummary* locations =
3548 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003549 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003550
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003551 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003552 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003553 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003554 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003555
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003556 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3557 locations->SetOut(Location::RequiresFpuRegister());
3558 } else {
3559 locations->SetOut(Location::RequiresRegister(),
3560 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3561 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003562 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003563 // Arm encoding have some additional constraints for ldrexd/strexd:
3564 // - registers need to be consecutive
3565 // - the first register should be even but not R14.
3566 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3567 // enable Arm encoding.
3568 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3569 locations->AddTemp(Location::RequiresRegister());
3570 locations->AddTemp(Location::RequiresRegister());
3571 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003572}
3573
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003574Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3575 Opcode opcode) {
3576 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3577 if (constant->IsConstant() &&
3578 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3579 return Location::ConstantLocation(constant->AsConstant());
3580 }
3581 return Location::RequiresRegister();
3582}
3583
3584bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3585 Opcode opcode) {
3586 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3587 if (Primitive::Is64BitType(input_cst->GetType())) {
3588 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3589 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3590 } else {
3591 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3592 }
3593}
3594
3595bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3596 ShifterOperand so;
3597 ArmAssembler* assembler = codegen_->GetAssembler();
3598 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3599 return true;
3600 }
3601 Opcode neg_opcode = kNoOperand;
3602 switch (opcode) {
3603 case AND:
3604 neg_opcode = BIC;
3605 break;
3606 case ORR:
3607 neg_opcode = ORN;
3608 break;
3609 default:
3610 return false;
3611 }
3612 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
3613}
3614
Calin Juravle52c48962014-12-16 17:02:57 +00003615void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3616 const FieldInfo& field_info) {
3617 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003618
Calin Juravle52c48962014-12-16 17:02:57 +00003619 LocationSummary* locations = instruction->GetLocations();
3620 Register base = locations->InAt(0).AsRegister<Register>();
3621 Location out = locations->Out();
3622 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003623 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003624 Primitive::Type field_type = field_info.GetFieldType();
3625 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3626
3627 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003628 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003629 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003630 break;
3631 }
3632
3633 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003634 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003635 break;
3636 }
3637
3638 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003639 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003640 break;
3641 }
3642
3643 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003644 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003645 break;
3646 }
3647
3648 case Primitive::kPrimInt:
3649 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003650 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003651 break;
3652 }
3653
3654 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003655 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003656 GenerateWideAtomicLoad(base, offset,
3657 out.AsRegisterPairLow<Register>(),
3658 out.AsRegisterPairHigh<Register>());
3659 } else {
3660 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3661 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003662 break;
3663 }
3664
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003665 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003666 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003667 break;
3668 }
3669
3670 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003671 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003672 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003673 Register lo = locations->GetTemp(0).AsRegister<Register>();
3674 Register hi = locations->GetTemp(1).AsRegister<Register>();
3675 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003676 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003677 __ vmovdrr(out_reg, lo, hi);
3678 } else {
3679 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003680 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003681 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003682 break;
3683 }
3684
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003685 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003686 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003687 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003688 }
Calin Juravle52c48962014-12-16 17:02:57 +00003689
Calin Juravle77520bc2015-01-12 18:45:46 +00003690 // Doubles are handled in the switch.
3691 if (field_type != Primitive::kPrimDouble) {
3692 codegen_->MaybeRecordImplicitNullCheck(instruction);
3693 }
3694
Calin Juravle52c48962014-12-16 17:02:57 +00003695 if (is_volatile) {
3696 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3697 }
Roland Levillain4d027112015-07-01 15:41:14 +01003698
3699 if (field_type == Primitive::kPrimNot) {
3700 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3701 }
Calin Juravle52c48962014-12-16 17:02:57 +00003702}
3703
3704void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3705 HandleFieldSet(instruction, instruction->GetFieldInfo());
3706}
3707
3708void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003709 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003710}
3711
3712void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3713 HandleFieldGet(instruction, instruction->GetFieldInfo());
3714}
3715
3716void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3717 HandleFieldGet(instruction, instruction->GetFieldInfo());
3718}
3719
3720void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3721 HandleFieldGet(instruction, instruction->GetFieldInfo());
3722}
3723
3724void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3725 HandleFieldGet(instruction, instruction->GetFieldInfo());
3726}
3727
3728void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3729 HandleFieldSet(instruction, instruction->GetFieldInfo());
3730}
3731
3732void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003733 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003734}
3735
Calin Juravlee460d1d2015-09-29 04:52:17 +01003736void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
3737 HUnresolvedInstanceFieldGet* instruction) {
3738 FieldAccessCallingConventionARM calling_convention;
3739 codegen_->CreateUnresolvedFieldLocationSummary(
3740 instruction, instruction->GetFieldType(), calling_convention);
3741}
3742
3743void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
3744 HUnresolvedInstanceFieldGet* instruction) {
3745 FieldAccessCallingConventionARM calling_convention;
3746 codegen_->GenerateUnresolvedFieldAccess(instruction,
3747 instruction->GetFieldType(),
3748 instruction->GetFieldIndex(),
3749 instruction->GetDexPc(),
3750 calling_convention);
3751}
3752
3753void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
3754 HUnresolvedInstanceFieldSet* instruction) {
3755 FieldAccessCallingConventionARM calling_convention;
3756 codegen_->CreateUnresolvedFieldLocationSummary(
3757 instruction, instruction->GetFieldType(), calling_convention);
3758}
3759
3760void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
3761 HUnresolvedInstanceFieldSet* instruction) {
3762 FieldAccessCallingConventionARM calling_convention;
3763 codegen_->GenerateUnresolvedFieldAccess(instruction,
3764 instruction->GetFieldType(),
3765 instruction->GetFieldIndex(),
3766 instruction->GetDexPc(),
3767 calling_convention);
3768}
3769
3770void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
3771 HUnresolvedStaticFieldGet* instruction) {
3772 FieldAccessCallingConventionARM calling_convention;
3773 codegen_->CreateUnresolvedFieldLocationSummary(
3774 instruction, instruction->GetFieldType(), calling_convention);
3775}
3776
3777void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
3778 HUnresolvedStaticFieldGet* instruction) {
3779 FieldAccessCallingConventionARM calling_convention;
3780 codegen_->GenerateUnresolvedFieldAccess(instruction,
3781 instruction->GetFieldType(),
3782 instruction->GetFieldIndex(),
3783 instruction->GetDexPc(),
3784 calling_convention);
3785}
3786
3787void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
3788 HUnresolvedStaticFieldSet* instruction) {
3789 FieldAccessCallingConventionARM calling_convention;
3790 codegen_->CreateUnresolvedFieldLocationSummary(
3791 instruction, instruction->GetFieldType(), calling_convention);
3792}
3793
3794void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
3795 HUnresolvedStaticFieldSet* instruction) {
3796 FieldAccessCallingConventionARM calling_convention;
3797 codegen_->GenerateUnresolvedFieldAccess(instruction,
3798 instruction->GetFieldType(),
3799 instruction->GetFieldIndex(),
3800 instruction->GetDexPc(),
3801 calling_convention);
3802}
3803
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003804void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003805 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3806 ? LocationSummary::kCallOnSlowPath
3807 : LocationSummary::kNoCall;
3808 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003809 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003810 if (instruction->HasUses()) {
3811 locations->SetOut(Location::SameAsFirstInput());
3812 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003813}
3814
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003815void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003816 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3817 return;
3818 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003819 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003820
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003821 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3822 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3823}
3824
3825void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003826 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003827 codegen_->AddSlowPath(slow_path);
3828
3829 LocationSummary* locations = instruction->GetLocations();
3830 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003831
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003832 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003833}
3834
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003835void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003836 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003837 GenerateImplicitNullCheck(instruction);
3838 } else {
3839 GenerateExplicitNullCheck(instruction);
3840 }
3841}
3842
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003843void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003844 LocationSummary* locations =
3845 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003846 locations->SetInAt(0, Location::RequiresRegister());
3847 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003848 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3849 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3850 } else {
3851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3852 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003853}
3854
3855void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3856 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003857 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003858 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003859 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003860
Roland Levillain4d027112015-07-01 15:41:14 +01003861 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003862 case Primitive::kPrimBoolean: {
3863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003864 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003865 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003866 size_t offset =
3867 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003868 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3869 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003870 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003871 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3872 }
3873 break;
3874 }
3875
3876 case Primitive::kPrimByte: {
3877 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003878 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003880 size_t offset =
3881 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003882 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3883 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003884 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003885 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3886 }
3887 break;
3888 }
3889
3890 case Primitive::kPrimShort: {
3891 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003892 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003893 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003894 size_t offset =
3895 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003896 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3897 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003898 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003899 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3900 }
3901 break;
3902 }
3903
3904 case Primitive::kPrimChar: {
3905 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003907 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003908 size_t offset =
3909 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003910 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3911 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003912 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3914 }
3915 break;
3916 }
3917
3918 case Primitive::kPrimInt:
3919 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003920 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3921 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003922 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003923 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003924 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003925 size_t offset =
3926 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003927 __ LoadFromOffset(kLoadWord, out, obj, offset);
3928 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003929 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003930 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3931 }
3932 break;
3933 }
3934
3935 case Primitive::kPrimLong: {
3936 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003937 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003938 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003939 size_t offset =
3940 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003941 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003942 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003943 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003944 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945 }
3946 break;
3947 }
3948
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003949 case Primitive::kPrimFloat: {
3950 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3951 Location out = locations->Out();
3952 DCHECK(out.IsFpuRegister());
3953 if (index.IsConstant()) {
3954 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3955 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3956 } else {
3957 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3958 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3959 }
3960 break;
3961 }
3962
3963 case Primitive::kPrimDouble: {
3964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3965 Location out = locations->Out();
3966 DCHECK(out.IsFpuRegisterPair());
3967 if (index.IsConstant()) {
3968 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3969 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3970 } else {
3971 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3972 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3973 }
3974 break;
3975 }
3976
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003977 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003978 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003979 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003980 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003981 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003982
3983 if (type == Primitive::kPrimNot) {
3984 Register out = locations->Out().AsRegister<Register>();
3985 __ MaybeUnpoisonHeapReference(out);
3986 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003987}
3988
3989void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003990 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003991
3992 bool needs_write_barrier =
3993 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003994 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003995
Nicolas Geoffray39468442014-09-02 15:17:15 +01003996 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003997 instruction,
3998 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
3999 locations->SetInAt(0, Location::RequiresRegister());
4000 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4001 if (Primitive::IsFloatingPointType(value_type)) {
4002 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004003 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004004 locations->SetInAt(2, Location::RequiresRegister());
4005 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004006
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004007 if (needs_write_barrier) {
4008 // Temporary registers for the write barrier.
4009 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4010 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004011 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004012}
4013
4014void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4015 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004016 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004017 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004018 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004019 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004020 bool needs_write_barrier =
4021 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004022
4023 switch (value_type) {
4024 case Primitive::kPrimBoolean:
4025 case Primitive::kPrimByte: {
4026 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004027 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004028 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004029 size_t offset =
4030 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004031 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004032 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004033 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004034 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4035 }
4036 break;
4037 }
4038
4039 case Primitive::kPrimShort:
4040 case Primitive::kPrimChar: {
4041 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004042 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004043 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004044 size_t offset =
4045 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004046 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004047 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004048 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004049 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4050 }
4051 break;
4052 }
4053
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004054 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004055 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4056 Register value = locations->InAt(2).AsRegister<Register>();
4057 Register source = value;
4058
4059 if (instruction->InputAt(2)->IsNullConstant()) {
4060 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004061 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004062 size_t offset =
4063 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004064 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004065 } else {
4066 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004067 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004068 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004069 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004070 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004071 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004072
4073 DCHECK(needs_write_barrier);
4074 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4075 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4076 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4077 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4078 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4079 Label done;
4080 SlowPathCode* slow_path = nullptr;
4081
4082 if (may_need_runtime_call) {
4083 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4084 codegen_->AddSlowPath(slow_path);
4085 if (instruction->GetValueCanBeNull()) {
4086 Label non_zero;
4087 __ CompareAndBranchIfNonZero(value, &non_zero);
4088 if (index.IsConstant()) {
4089 size_t offset =
4090 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4091 __ StoreToOffset(kStoreWord, value, array, offset);
4092 } else {
4093 DCHECK(index.IsRegister()) << index;
4094 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4095 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4096 }
4097 codegen_->MaybeRecordImplicitNullCheck(instruction);
4098 __ b(&done);
4099 __ Bind(&non_zero);
4100 }
4101
4102 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4103 codegen_->MaybeRecordImplicitNullCheck(instruction);
4104 __ MaybeUnpoisonHeapReference(temp1);
4105 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4106 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4107 // No need to poison/unpoison, we're comparing two poisoined references.
4108 __ cmp(temp1, ShifterOperand(temp2));
4109 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4110 Label do_put;
4111 __ b(&do_put, EQ);
4112 __ MaybeUnpoisonHeapReference(temp1);
4113 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4114 // No need to poison/unpoison, we're comparing against null.
4115 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4116 __ Bind(&do_put);
4117 } else {
4118 __ b(slow_path->GetEntryLabel(), NE);
4119 }
4120 }
4121
4122 if (kPoisonHeapReferences) {
4123 // Note that in the case where `value` is a null reference,
4124 // we do not enter this block, as a null reference does not
4125 // need poisoning.
4126 DCHECK_EQ(value_type, Primitive::kPrimNot);
4127 __ Mov(temp1, value);
4128 __ PoisonHeapReference(temp1);
4129 source = temp1;
4130 }
4131
4132 if (index.IsConstant()) {
4133 size_t offset =
4134 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4135 __ StoreToOffset(kStoreWord, source, array, offset);
4136 } else {
4137 DCHECK(index.IsRegister()) << index;
4138 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4139 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4140 }
4141
4142 if (!may_need_runtime_call) {
4143 codegen_->MaybeRecordImplicitNullCheck(instruction);
4144 }
4145
4146 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4147
4148 if (done.IsLinked()) {
4149 __ Bind(&done);
4150 }
4151
4152 if (slow_path != nullptr) {
4153 __ Bind(slow_path->GetExitLabel());
4154 }
4155
4156 break;
4157 }
4158
4159 case Primitive::kPrimInt: {
4160 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4161 Register value = locations->InAt(2).AsRegister<Register>();
4162 if (index.IsConstant()) {
4163 size_t offset =
4164 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4165 __ StoreToOffset(kStoreWord, value, array, offset);
4166 } else {
4167 DCHECK(index.IsRegister()) << index;
4168 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4169 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4170 }
4171
4172 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004173 break;
4174 }
4175
4176 case Primitive::kPrimLong: {
4177 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004178 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004180 size_t offset =
4181 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004182 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004183 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004184 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004185 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004186 }
4187 break;
4188 }
4189
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004190 case Primitive::kPrimFloat: {
4191 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4192 Location value = locations->InAt(2);
4193 DCHECK(value.IsFpuRegister());
4194 if (index.IsConstant()) {
4195 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004196 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004197 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004198 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004199 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4200 }
4201 break;
4202 }
4203
4204 case Primitive::kPrimDouble: {
4205 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4206 Location value = locations->InAt(2);
4207 DCHECK(value.IsFpuRegisterPair());
4208 if (index.IsConstant()) {
4209 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004210 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004211 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004212 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004213 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4214 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004215
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004216 break;
4217 }
4218
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004219 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004220 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004221 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004222 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004223
4224 // Ints and objects are handled in the switch.
4225 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4226 codegen_->MaybeRecordImplicitNullCheck(instruction);
4227 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004228}
4229
4230void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004231 LocationSummary* locations =
4232 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004233 locations->SetInAt(0, Location::RequiresRegister());
4234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004235}
4236
4237void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4238 LocationSummary* locations = instruction->GetLocations();
4239 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004240 Register obj = locations->InAt(0).AsRegister<Register>();
4241 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004242 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004243 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004244}
4245
4246void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004247 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4248 ? LocationSummary::kCallOnSlowPath
4249 : LocationSummary::kNoCall;
4250 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004251 locations->SetInAt(0, Location::RequiresRegister());
4252 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004253 if (instruction->HasUses()) {
4254 locations->SetOut(Location::SameAsFirstInput());
4255 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004256}
4257
4258void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4259 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004260 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004261 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004262 codegen_->AddSlowPath(slow_path);
4263
Roland Levillain271ab9c2014-11-27 15:23:57 +00004264 Register index = locations->InAt(0).AsRegister<Register>();
4265 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004266
4267 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004268 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004269}
4270
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004271void CodeGeneratorARM::MarkGCCard(Register temp,
4272 Register card,
4273 Register object,
4274 Register value,
4275 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004276 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004277 if (can_be_null) {
4278 __ CompareAndBranchIfZero(value, &is_null);
4279 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004280 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4281 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4282 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004283 if (can_be_null) {
4284 __ Bind(&is_null);
4285 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004286}
4287
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4289 temp->SetLocations(nullptr);
4290}
4291
4292void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
4293 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004294 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295}
4296
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004297void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004298 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004299 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004300}
4301
4302void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004303 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4304}
4305
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004306void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4307 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4308}
4309
4310void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004311 HBasicBlock* block = instruction->GetBlock();
4312 if (block->GetLoopInformation() != nullptr) {
4313 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4314 // The back edge will generate the suspend check.
4315 return;
4316 }
4317 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4318 // The goto will generate the suspend check.
4319 return;
4320 }
4321 GenerateSuspendCheck(instruction, nullptr);
4322}
4323
4324void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4325 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004326 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004327 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4328 if (slow_path == nullptr) {
4329 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4330 instruction->SetSlowPath(slow_path);
4331 codegen_->AddSlowPath(slow_path);
4332 if (successor != nullptr) {
4333 DCHECK(successor->IsLoopHeader());
4334 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4335 }
4336 } else {
4337 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4338 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004339
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004340 __ LoadFromOffset(
4341 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004342 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004343 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004344 __ Bind(slow_path->GetReturnLabel());
4345 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004346 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004347 __ b(slow_path->GetEntryLabel());
4348 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004349}
4350
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004351ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4352 return codegen_->GetAssembler();
4353}
4354
4355void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004356 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004357 Location source = move->GetSource();
4358 Location destination = move->GetDestination();
4359
4360 if (source.IsRegister()) {
4361 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004362 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004363 } else {
4364 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004365 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004366 SP, destination.GetStackIndex());
4367 }
4368 } else if (source.IsStackSlot()) {
4369 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004370 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004371 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004372 } else if (destination.IsFpuRegister()) {
4373 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004374 } else {
4375 DCHECK(destination.IsStackSlot());
4376 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4377 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4378 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004379 } else if (source.IsFpuRegister()) {
4380 if (destination.IsFpuRegister()) {
4381 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004382 } else {
4383 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004384 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4385 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004386 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004387 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004388 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4389 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004390 } else if (destination.IsRegisterPair()) {
4391 DCHECK(ExpectedPairLayout(destination));
4392 __ LoadFromOffset(
4393 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4394 } else {
4395 DCHECK(destination.IsFpuRegisterPair()) << destination;
4396 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4397 SP,
4398 source.GetStackIndex());
4399 }
4400 } else if (source.IsRegisterPair()) {
4401 if (destination.IsRegisterPair()) {
4402 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4403 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4404 } else {
4405 DCHECK(destination.IsDoubleStackSlot()) << destination;
4406 DCHECK(ExpectedPairLayout(source));
4407 __ StoreToOffset(
4408 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4409 }
4410 } else if (source.IsFpuRegisterPair()) {
4411 if (destination.IsFpuRegisterPair()) {
4412 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4413 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4414 } else {
4415 DCHECK(destination.IsDoubleStackSlot()) << destination;
4416 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4417 SP,
4418 destination.GetStackIndex());
4419 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004420 } else {
4421 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004422 HConstant* constant = source.GetConstant();
4423 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4424 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004425 if (destination.IsRegister()) {
4426 __ LoadImmediate(destination.AsRegister<Register>(), value);
4427 } else {
4428 DCHECK(destination.IsStackSlot());
4429 __ LoadImmediate(IP, value);
4430 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4431 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004432 } else if (constant->IsLongConstant()) {
4433 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004434 if (destination.IsRegisterPair()) {
4435 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4436 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004437 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004438 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004439 __ LoadImmediate(IP, Low32Bits(value));
4440 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4441 __ LoadImmediate(IP, High32Bits(value));
4442 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4443 }
4444 } else if (constant->IsDoubleConstant()) {
4445 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004446 if (destination.IsFpuRegisterPair()) {
4447 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004448 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004449 DCHECK(destination.IsDoubleStackSlot()) << destination;
4450 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004451 __ LoadImmediate(IP, Low32Bits(int_value));
4452 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4453 __ LoadImmediate(IP, High32Bits(int_value));
4454 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4455 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004456 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004457 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004458 float value = constant->AsFloatConstant()->GetValue();
4459 if (destination.IsFpuRegister()) {
4460 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4461 } else {
4462 DCHECK(destination.IsStackSlot());
4463 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4464 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4465 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004466 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004467 }
4468}
4469
4470void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4471 __ Mov(IP, reg);
4472 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4473 __ StoreToOffset(kStoreWord, IP, SP, mem);
4474}
4475
4476void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4477 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4478 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4479 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4480 SP, mem1 + stack_offset);
4481 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4482 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4483 SP, mem2 + stack_offset);
4484 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4485}
4486
4487void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004488 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004489 Location source = move->GetSource();
4490 Location destination = move->GetDestination();
4491
4492 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004493 DCHECK_NE(source.AsRegister<Register>(), IP);
4494 DCHECK_NE(destination.AsRegister<Register>(), IP);
4495 __ Mov(IP, source.AsRegister<Register>());
4496 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4497 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004498 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004499 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004500 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004501 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004502 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4503 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004504 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004505 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004506 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004507 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004508 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004509 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004510 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004511 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004512 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4513 destination.AsRegisterPairHigh<Register>(),
4514 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004515 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004516 Register low_reg = source.IsRegisterPair()
4517 ? source.AsRegisterPairLow<Register>()
4518 : destination.AsRegisterPairLow<Register>();
4519 int mem = source.IsRegisterPair()
4520 ? destination.GetStackIndex()
4521 : source.GetStackIndex();
4522 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004523 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004524 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004525 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004526 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004527 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4528 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004529 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004530 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004531 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004532 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4533 DRegister reg = source.IsFpuRegisterPair()
4534 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4535 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4536 int mem = source.IsFpuRegisterPair()
4537 ? destination.GetStackIndex()
4538 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004539 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004540 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004541 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004542 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4543 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4544 : destination.AsFpuRegister<SRegister>();
4545 int mem = source.IsFpuRegister()
4546 ? destination.GetStackIndex()
4547 : source.GetStackIndex();
4548
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004549 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004550 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004551 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004552 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004553 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4554 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004555 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004556 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004557 }
4558}
4559
4560void ParallelMoveResolverARM::SpillScratch(int reg) {
4561 __ Push(static_cast<Register>(reg));
4562}
4563
4564void ParallelMoveResolverARM::RestoreScratch(int reg) {
4565 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004566}
4567
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004568void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004569 InvokeRuntimeCallingConvention calling_convention;
4570 CodeGenerator::CreateLoadClassLocationSummary(
4571 cls,
4572 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4573 Location::RegisterLocation(R0));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004574}
4575
4576void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004577 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004578 if (cls->NeedsAccessCheck()) {
4579 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4580 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4581 cls,
4582 cls->GetDexPc(),
4583 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004584 return;
4585 }
4586
4587 Register out = locations->Out().AsRegister<Register>();
4588 Register current_method = locations->InAt(0).AsRegister<Register>();
4589 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004590 DCHECK(!cls->CanCallRuntime());
4591 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004592 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004593 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004594 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004595 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004596 __ LoadFromOffset(kLoadWord,
4597 out,
4598 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004599 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004600 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004601 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004602
Andreas Gampe85b62f22015-09-09 13:15:38 -07004603 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004604 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4605 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004606 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004607 if (cls->MustGenerateClinitCheck()) {
4608 GenerateClassInitializationCheck(slow_path, out);
4609 } else {
4610 __ Bind(slow_path->GetExitLabel());
4611 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004612 }
4613}
4614
4615void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4616 LocationSummary* locations =
4617 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4618 locations->SetInAt(0, Location::RequiresRegister());
4619 if (check->HasUses()) {
4620 locations->SetOut(Location::SameAsFirstInput());
4621 }
4622}
4623
4624void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004625 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004626 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004627 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004628 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004629 GenerateClassInitializationCheck(slow_path,
4630 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004631}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004632
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004633void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004634 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004635 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4636 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4637 __ b(slow_path->GetEntryLabel(), LT);
4638 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4639 // properly. Therefore, we do a memory fence.
4640 __ dmb(ISH);
4641 __ Bind(slow_path->GetExitLabel());
4642}
4643
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004644void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4645 LocationSummary* locations =
4646 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004647 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004648 locations->SetOut(Location::RequiresRegister());
4649}
4650
4651void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004652 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004653 codegen_->AddSlowPath(slow_path);
4654
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004655 LocationSummary* locations = load->GetLocations();
4656 Register out = locations->Out().AsRegister<Register>();
4657 Register current_method = locations->InAt(0).AsRegister<Register>();
4658 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004659 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004660 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004661 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004662 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004663 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004664 __ Bind(slow_path->GetExitLabel());
4665}
4666
David Brazdilcb1c0552015-08-04 16:22:25 +01004667static int32_t GetExceptionTlsOffset() {
4668 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4669}
4670
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004671void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4672 LocationSummary* locations =
4673 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4674 locations->SetOut(Location::RequiresRegister());
4675}
4676
4677void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004678 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004679 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4680}
4681
4682void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4683 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4684}
4685
4686void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004687 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004688 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004689}
4690
4691void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4692 LocationSummary* locations =
4693 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4694 InvokeRuntimeCallingConvention calling_convention;
4695 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4696}
4697
4698void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4699 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004700 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004701}
4702
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004703void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004704 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4705 switch (instruction->GetTypeCheckKind()) {
4706 case TypeCheckKind::kExactCheck:
4707 case TypeCheckKind::kAbstractClassCheck:
4708 case TypeCheckKind::kClassHierarchyCheck:
4709 case TypeCheckKind::kArrayObjectCheck:
4710 call_kind = LocationSummary::kNoCall;
4711 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004712 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004713 case TypeCheckKind::kInterfaceCheck:
4714 call_kind = LocationSummary::kCall;
4715 break;
4716 case TypeCheckKind::kArrayCheck:
4717 call_kind = LocationSummary::kCallOnSlowPath;
4718 break;
4719 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004721 if (call_kind != LocationSummary::kCall) {
4722 locations->SetInAt(0, Location::RequiresRegister());
4723 locations->SetInAt(1, Location::RequiresRegister());
4724 // The out register is used as a temporary, so it overlaps with the inputs.
4725 // Note that TypeCheckSlowPathARM uses this register too.
4726 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4727 } else {
4728 InvokeRuntimeCallingConvention calling_convention;
4729 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4730 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4731 locations->SetOut(Location::RegisterLocation(R0));
4732 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004733}
4734
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004735void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004736 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004737 Register obj = locations->InAt(0).AsRegister<Register>();
4738 Register cls = locations->InAt(1).AsRegister<Register>();
4739 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004740 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004741 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4742 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4743 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004744 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004745 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004746
4747 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004748 // avoid null check if we know obj is not null.
4749 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004750 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004751 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004752
Calin Juravle98893e12015-10-02 21:05:03 +01004753 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004754 // This is safe, as the register is caller-save, and the object must be in another
4755 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004756 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4757 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004758 ? obj
4759 : out;
4760 __ LoadFromOffset(kLoadWord, target, obj, class_offset);
4761 __ MaybeUnpoisonHeapReference(target);
4762
4763 switch (instruction->GetTypeCheckKind()) {
4764 case TypeCheckKind::kExactCheck: {
4765 __ cmp(out, ShifterOperand(cls));
4766 // Classes must be equal for the instanceof to succeed.
4767 __ b(&zero, NE);
4768 __ LoadImmediate(out, 1);
4769 __ b(&done);
4770 break;
4771 }
4772 case TypeCheckKind::kAbstractClassCheck: {
4773 // If the class is abstract, we eagerly fetch the super class of the
4774 // object to avoid doing a comparison we know will fail.
4775 Label loop;
4776 __ Bind(&loop);
4777 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4778 __ MaybeUnpoisonHeapReference(out);
4779 // If `out` is null, we use it for the result, and jump to `done`.
4780 __ CompareAndBranchIfZero(out, &done);
4781 __ cmp(out, ShifterOperand(cls));
4782 __ b(&loop, NE);
4783 __ LoadImmediate(out, 1);
4784 if (zero.IsLinked()) {
4785 __ b(&done);
4786 }
4787 break;
4788 }
4789 case TypeCheckKind::kClassHierarchyCheck: {
4790 // Walk over the class hierarchy to find a match.
4791 Label loop, success;
4792 __ Bind(&loop);
4793 __ cmp(out, ShifterOperand(cls));
4794 __ b(&success, EQ);
4795 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4796 __ MaybeUnpoisonHeapReference(out);
4797 __ CompareAndBranchIfNonZero(out, &loop);
4798 // If `out` is null, we use it for the result, and jump to `done`.
4799 __ b(&done);
4800 __ Bind(&success);
4801 __ LoadImmediate(out, 1);
4802 if (zero.IsLinked()) {
4803 __ b(&done);
4804 }
4805 break;
4806 }
4807 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004808 // Do an exact check.
4809 Label exact_check;
4810 __ cmp(out, ShifterOperand(cls));
4811 __ b(&exact_check, EQ);
4812 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004813 __ LoadFromOffset(kLoadWord, out, out, component_offset);
4814 __ MaybeUnpoisonHeapReference(out);
4815 // If `out` is null, we use it for the result, and jump to `done`.
4816 __ CompareAndBranchIfZero(out, &done);
4817 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4818 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4819 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004820 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004821 __ LoadImmediate(out, 1);
4822 __ b(&done);
4823 break;
4824 }
4825 case TypeCheckKind::kArrayCheck: {
4826 __ cmp(out, ShifterOperand(cls));
4827 DCHECK(locations->OnlyCallsOnSlowPath());
4828 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4829 instruction, /* is_fatal */ false);
4830 codegen_->AddSlowPath(slow_path);
4831 __ b(slow_path->GetEntryLabel(), NE);
4832 __ LoadImmediate(out, 1);
4833 if (zero.IsLinked()) {
4834 __ b(&done);
4835 }
4836 break;
4837 }
Calin Juravle98893e12015-10-02 21:05:03 +01004838 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004839 case TypeCheckKind::kInterfaceCheck:
4840 default: {
4841 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4842 instruction,
4843 instruction->GetDexPc(),
4844 nullptr);
4845 if (zero.IsLinked()) {
4846 __ b(&done);
4847 }
4848 break;
4849 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004850 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004851
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004852 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004853 __ Bind(&zero);
4854 __ LoadImmediate(out, 0);
4855 }
4856
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004857 if (done.IsLinked()) {
4858 __ Bind(&done);
4859 }
4860
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004861 if (slow_path != nullptr) {
4862 __ Bind(slow_path->GetExitLabel());
4863 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004864}
4865
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004866void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004867 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4868 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4869
4870 switch (instruction->GetTypeCheckKind()) {
4871 case TypeCheckKind::kExactCheck:
4872 case TypeCheckKind::kAbstractClassCheck:
4873 case TypeCheckKind::kClassHierarchyCheck:
4874 case TypeCheckKind::kArrayObjectCheck:
4875 call_kind = throws_into_catch
4876 ? LocationSummary::kCallOnSlowPath
4877 : LocationSummary::kNoCall;
4878 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004879 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004880 case TypeCheckKind::kInterfaceCheck:
4881 call_kind = LocationSummary::kCall;
4882 break;
4883 case TypeCheckKind::kArrayCheck:
4884 call_kind = LocationSummary::kCallOnSlowPath;
4885 break;
4886 }
4887
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004888 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004889 instruction, call_kind);
4890 if (call_kind != LocationSummary::kCall) {
4891 locations->SetInAt(0, Location::RequiresRegister());
4892 locations->SetInAt(1, Location::RequiresRegister());
4893 // Note that TypeCheckSlowPathARM uses this register too.
4894 locations->AddTemp(Location::RequiresRegister());
4895 } else {
4896 InvokeRuntimeCallingConvention calling_convention;
4897 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4898 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4899 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004900}
4901
4902void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4903 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004904 Register obj = locations->InAt(0).AsRegister<Register>();
4905 Register cls = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004906 Register temp = locations->WillCall()
4907 ? Register(kNoRegister)
4908 : locations->GetTemp(0).AsRegister<Register>();
4909
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004910 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004911 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4912 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4913 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4914 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004915
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004916 if (!locations->WillCall()) {
4917 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4918 instruction, !locations->CanCall());
4919 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004920 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004921
4922 Label done;
4923 // Avoid null check if we know obj is not null.
4924 if (instruction->MustDoNullCheck()) {
4925 __ CompareAndBranchIfZero(obj, &done);
4926 }
4927
4928 if (locations->WillCall()) {
4929 __ LoadFromOffset(kLoadWord, obj, obj, class_offset);
4930 __ MaybeUnpoisonHeapReference(obj);
4931 } else {
4932 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4933 __ MaybeUnpoisonHeapReference(temp);
4934 }
4935
4936 switch (instruction->GetTypeCheckKind()) {
4937 case TypeCheckKind::kExactCheck:
4938 case TypeCheckKind::kArrayCheck: {
4939 __ cmp(temp, ShifterOperand(cls));
4940 // Jump to slow path for throwing the exception or doing a
4941 // more involved array check.
4942 __ b(slow_path->GetEntryLabel(), NE);
4943 break;
4944 }
4945 case TypeCheckKind::kAbstractClassCheck: {
4946 // If the class is abstract, we eagerly fetch the super class of the
4947 // object to avoid doing a comparison we know will fail.
4948 Label loop;
4949 __ Bind(&loop);
4950 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4951 __ MaybeUnpoisonHeapReference(temp);
4952 // Jump to the slow path to throw the exception.
4953 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4954 __ cmp(temp, ShifterOperand(cls));
4955 __ b(&loop, NE);
4956 break;
4957 }
4958 case TypeCheckKind::kClassHierarchyCheck: {
4959 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004960 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004961 __ Bind(&loop);
4962 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004963 __ b(&done, EQ);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004964 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4965 __ MaybeUnpoisonHeapReference(temp);
4966 __ CompareAndBranchIfNonZero(temp, &loop);
4967 // Jump to the slow path to throw the exception.
4968 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004969 break;
4970 }
4971 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004972 // Do an exact check.
4973 __ cmp(temp, ShifterOperand(cls));
4974 __ b(&done, EQ);
4975 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004976 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
4977 __ MaybeUnpoisonHeapReference(temp);
4978 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4979 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
4980 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4981 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
4982 break;
4983 }
Calin Juravle98893e12015-10-02 21:05:03 +01004984 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004985 case TypeCheckKind::kInterfaceCheck:
4986 default:
4987 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
4988 instruction,
4989 instruction->GetDexPc(),
4990 nullptr);
4991 break;
4992 }
4993 __ Bind(&done);
4994
4995 if (slow_path != nullptr) {
4996 __ Bind(slow_path->GetExitLabel());
4997 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004998}
4999
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005000void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5001 LocationSummary* locations =
5002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5003 InvokeRuntimeCallingConvention calling_convention;
5004 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5005}
5006
5007void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5008 codegen_->InvokeRuntime(instruction->IsEnter()
5009 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5010 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005011 instruction->GetDexPc(),
5012 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005013}
5014
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005015void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5016void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5017void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005018
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005019void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005020 LocationSummary* locations =
5021 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5022 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5023 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005024 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005025 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005026 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005028}
5029
5030void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5031 HandleBitwiseOperation(instruction);
5032}
5033
5034void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5035 HandleBitwiseOperation(instruction);
5036}
5037
5038void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5039 HandleBitwiseOperation(instruction);
5040}
5041
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005042void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5043 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5044 if (value == 0xffffffffu) {
5045 if (out != first) {
5046 __ mov(out, ShifterOperand(first));
5047 }
5048 return;
5049 }
5050 if (value == 0u) {
5051 __ mov(out, ShifterOperand(0));
5052 return;
5053 }
5054 ShifterOperand so;
5055 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5056 __ and_(out, first, so);
5057 } else {
5058 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5059 __ bic(out, first, ShifterOperand(~value));
5060 }
5061}
5062
5063void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5064 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5065 if (value == 0u) {
5066 if (out != first) {
5067 __ mov(out, ShifterOperand(first));
5068 }
5069 return;
5070 }
5071 if (value == 0xffffffffu) {
5072 __ mvn(out, ShifterOperand(0));
5073 return;
5074 }
5075 ShifterOperand so;
5076 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5077 __ orr(out, first, so);
5078 } else {
5079 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5080 __ orn(out, first, ShifterOperand(~value));
5081 }
5082}
5083
5084void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5085 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5086 if (value == 0u) {
5087 if (out != first) {
5088 __ mov(out, ShifterOperand(first));
5089 }
5090 return;
5091 }
5092 __ eor(out, first, ShifterOperand(value));
5093}
5094
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005095void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5096 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005097 Location first = locations->InAt(0);
5098 Location second = locations->InAt(1);
5099 Location out = locations->Out();
5100
5101 if (second.IsConstant()) {
5102 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5103 uint32_t value_low = Low32Bits(value);
5104 if (instruction->GetResultType() == Primitive::kPrimInt) {
5105 Register first_reg = first.AsRegister<Register>();
5106 Register out_reg = out.AsRegister<Register>();
5107 if (instruction->IsAnd()) {
5108 GenerateAndConst(out_reg, first_reg, value_low);
5109 } else if (instruction->IsOr()) {
5110 GenerateOrrConst(out_reg, first_reg, value_low);
5111 } else {
5112 DCHECK(instruction->IsXor());
5113 GenerateEorConst(out_reg, first_reg, value_low);
5114 }
5115 } else {
5116 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5117 uint32_t value_high = High32Bits(value);
5118 Register first_low = first.AsRegisterPairLow<Register>();
5119 Register first_high = first.AsRegisterPairHigh<Register>();
5120 Register out_low = out.AsRegisterPairLow<Register>();
5121 Register out_high = out.AsRegisterPairHigh<Register>();
5122 if (instruction->IsAnd()) {
5123 GenerateAndConst(out_low, first_low, value_low);
5124 GenerateAndConst(out_high, first_high, value_high);
5125 } else if (instruction->IsOr()) {
5126 GenerateOrrConst(out_low, first_low, value_low);
5127 GenerateOrrConst(out_high, first_high, value_high);
5128 } else {
5129 DCHECK(instruction->IsXor());
5130 GenerateEorConst(out_low, first_low, value_low);
5131 GenerateEorConst(out_high, first_high, value_high);
5132 }
5133 }
5134 return;
5135 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005136
5137 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005138 Register first_reg = first.AsRegister<Register>();
5139 ShifterOperand second_reg(second.AsRegister<Register>());
5140 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005141 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005142 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005143 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005144 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005145 } else {
5146 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005147 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005148 }
5149 } else {
5150 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005151 Register first_low = first.AsRegisterPairLow<Register>();
5152 Register first_high = first.AsRegisterPairHigh<Register>();
5153 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5154 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5155 Register out_low = out.AsRegisterPairLow<Register>();
5156 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005157 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005158 __ and_(out_low, first_low, second_low);
5159 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005160 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005161 __ orr(out_low, first_low, second_low);
5162 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005163 } else {
5164 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005165 __ eor(out_low, first_low, second_low);
5166 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005167 }
5168 }
5169}
5170
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005171void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00005172 // For better instruction scheduling we load the direct code pointer before the method pointer.
5173 bool direct_code_loaded = false;
5174 switch (invoke->GetCodePtrLocation()) {
5175 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5176 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
5177 break;
5178 }
5179 // Calls across dex files are more likely to exceed the available BL range,
5180 // so use absolute patch by falling through to kDirectCodeFixup.
5181 FALLTHROUGH_INTENDED;
5182 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5183 // LR = code address from literal pool with link-time patch.
5184 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5185 direct_code_loaded = true;
5186 break;
5187 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5188 // LR = invoke->GetDirectCodePtr();
5189 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
5190 direct_code_loaded = true;
5191 break;
5192 default:
5193 break;
5194 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005195
Vladimir Marko58155012015-08-19 12:49:41 +00005196 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5197 switch (invoke->GetMethodLoadKind()) {
5198 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5199 // temp = thread->string_init_entrypoint
5200 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5201 break;
5202 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
5203 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5204 break;
5205 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5206 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5207 break;
5208 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5209 __ LoadLiteral(temp.AsRegister<Register>(),
5210 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5211 break;
5212 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5213 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5214 FALLTHROUGH_INTENDED;
5215 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5216 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5217 Register method_reg;
5218 Register reg = temp.AsRegister<Register>();
5219 if (current_method.IsRegister()) {
5220 method_reg = current_method.AsRegister<Register>();
5221 } else {
5222 DCHECK(invoke->GetLocations()->Intrinsified());
5223 DCHECK(!current_method.IsValid());
5224 method_reg = reg;
5225 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5226 }
5227 // temp = current_method->dex_cache_resolved_methods_;
5228 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01005229 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
5230 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005231 // temp = temp[index_in_cache]
5232 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5233 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5234 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005235 }
Vladimir Marko58155012015-08-19 12:49:41 +00005236 }
5237
5238 switch (invoke->GetCodePtrLocation()) {
5239 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5240 __ bl(GetFrameEntryLabel());
5241 break;
5242 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5243 if (!direct_code_loaded) {
5244 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
5245 __ Bind(&relative_call_patches_.back().label);
5246 Label label;
5247 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
5248 __ Bind(&label);
5249 break;
5250 }
5251 // If we loaded the direct code above, fall through.
5252 FALLTHROUGH_INTENDED;
5253 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5254 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5255 // LR prepared above for better instruction scheduling.
5256 DCHECK(direct_code_loaded);
5257 // LR()
5258 __ blx(LR);
5259 break;
5260 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5261 // LR = callee_method->entry_point_from_quick_compiled_code_
5262 __ LoadFromOffset(
5263 kLoadWord, LR, callee_method.AsRegister<Register>(),
5264 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5265 // LR()
5266 __ blx(LR);
5267 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005268 }
5269
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005270 DCHECK(!IsLeafMethod());
5271}
5272
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005273void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5274 Register temp = temp_location.AsRegister<Register>();
5275 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5276 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5277 LocationSummary* locations = invoke->GetLocations();
5278 Location receiver = locations->InAt(0);
5279 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5280 // temp = object->GetClass();
5281 DCHECK(receiver.IsRegister());
5282 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5283 MaybeRecordImplicitNullCheck(invoke);
5284 __ MaybeUnpoisonHeapReference(temp);
5285 // temp = temp->GetMethodAt(method_offset);
5286 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5287 kArmWordSize).Int32Value();
5288 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5289 // LR = temp->GetEntryPoint();
5290 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5291 // LR();
5292 __ blx(LR);
5293}
5294
Vladimir Marko58155012015-08-19 12:49:41 +00005295void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5296 DCHECK(linker_patches->empty());
5297 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5298 linker_patches->reserve(size);
5299 for (const auto& entry : method_patches_) {
5300 const MethodReference& target_method = entry.first;
5301 Literal* literal = entry.second;
5302 DCHECK(literal->GetLabel()->IsBound());
5303 uint32_t literal_offset = literal->GetLabel()->Position();
5304 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5305 target_method.dex_file,
5306 target_method.dex_method_index));
5307 }
5308 for (const auto& entry : call_patches_) {
5309 const MethodReference& target_method = entry.first;
5310 Literal* literal = entry.second;
5311 DCHECK(literal->GetLabel()->IsBound());
5312 uint32_t literal_offset = literal->GetLabel()->Position();
5313 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5314 target_method.dex_file,
5315 target_method.dex_method_index));
5316 }
5317 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5318 uint32_t literal_offset = info.label.Position();
5319 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5320 info.target_method.dex_file,
5321 info.target_method.dex_method_index));
5322 }
5323}
5324
5325Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5326 MethodToLiteralMap* map) {
5327 // Look up the literal for target_method.
5328 auto lb = map->lower_bound(target_method);
5329 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5330 return lb->second;
5331 }
5332 // We don't have a literal for this method yet, insert a new one.
5333 Literal* literal = __ NewLiteral<uint32_t>(0u);
5334 map->PutBefore(lb, target_method, literal);
5335 return literal;
5336}
5337
5338Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5339 return DeduplicateMethodLiteral(target_method, &method_patches_);
5340}
5341
5342Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5343 return DeduplicateMethodLiteral(target_method, &call_patches_);
5344}
5345
Calin Juravleb1498f62015-02-16 13:13:29 +00005346void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
5347 // Nothing to do, this should be removed during prepare for register allocator.
5348 UNUSED(instruction);
5349 LOG(FATAL) << "Unreachable";
5350}
5351
5352void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
5353 // Nothing to do, this should be removed during prepare for register allocator.
5354 UNUSED(instruction);
5355 LOG(FATAL) << "Unreachable";
5356}
5357
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005358void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5359 DCHECK(codegen_->IsBaseline());
5360 LocationSummary* locations =
5361 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5362 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5363}
5364
5365void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5366 DCHECK(codegen_->IsBaseline());
5367 // Will be generated at use site.
5368}
5369
Mark Mendellfe57faa2015-09-18 09:26:15 -04005370// Simple implementation of packed switch - generate cascaded compare/jumps.
5371void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5372 LocationSummary* locations =
5373 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5374 locations->SetInAt(0, Location::RequiresRegister());
5375}
5376
5377void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5378 int32_t lower_bound = switch_instr->GetStartValue();
5379 int32_t num_entries = switch_instr->GetNumEntries();
5380 LocationSummary* locations = switch_instr->GetLocations();
5381 Register value_reg = locations->InAt(0).AsRegister<Register>();
5382 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5383
5384 // Create a series of compare/jumps.
5385 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5386 for (int32_t i = 0; i < num_entries; i++) {
5387 GenerateCompareWithImmediate(value_reg, lower_bound + i);
Vladimir Markoec7802a2015-10-01 20:57:57 +01005388 __ b(codegen_->GetLabelOf(successors[i]), EQ);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005389 }
5390
5391 // And the default for any other value.
5392 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5393 __ b(codegen_->GetLabelOf(default_block));
5394 }
5395}
5396
Andreas Gampe85b62f22015-09-09 13:15:38 -07005397void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5398 if (!trg.IsValid()) {
5399 DCHECK(type == Primitive::kPrimVoid);
5400 return;
5401 }
5402
5403 DCHECK_NE(type, Primitive::kPrimVoid);
5404
5405 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5406 if (return_loc.Equals(trg)) {
5407 return;
5408 }
5409
5410 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5411 // with the last branch.
5412 if (type == Primitive::kPrimLong) {
5413 HParallelMove parallel_move(GetGraph()->GetArena());
5414 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
5415 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
5416 GetMoveResolver()->EmitNativeCode(&parallel_move);
5417 } else if (type == Primitive::kPrimDouble) {
5418 HParallelMove parallel_move(GetGraph()->GetArena());
5419 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
5420 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
5421 GetMoveResolver()->EmitNativeCode(&parallel_move);
5422 } else {
5423 // Let the parallel move resolver take care of all of this.
5424 HParallelMove parallel_move(GetGraph()->GetArena());
5425 parallel_move.AddMove(return_loc, trg, type, nullptr);
5426 GetMoveResolver()->EmitNativeCode(&parallel_move);
5427 }
5428}
5429
Roland Levillain4d027112015-07-01 15:41:14 +01005430#undef __
5431#undef QUICK_ENTRY_POINT
5432
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005433} // namespace arm
5434} // namespace art