blob: cf7f5f4e0860e7493f8657d5f001ba6d933a7d22 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037namespace arm {
38
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000039static bool ExpectedPairLayout(Location location) {
40 // We expected this for both core and fpu register pairs.
41 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
42}
43
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000047// We unconditionally allocate R5 to ensure we can do long operations
48// with baseline.
49static constexpr Register kCoreSavedRegisterForBaseline = R5;
50static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070051 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000052static constexpr SRegister kFpuCalleeSaves[] =
53 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000055// D31 cannot be split into two S registers, and the register allocator only works on
56// S registers. Therefore there is no need to block it.
57static constexpr DRegister DTMP = D31;
58
Roland Levillain62a46b22015-06-01 18:24:13 +010059#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010060#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Andreas Gampe85b62f22015-09-09 13:15:38 -070062class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010064 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Alexandre Rames67555f72014-11-18 10:55:16 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010067 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000069 if (instruction_->CanThrowIntoCatchBlock()) {
70 // Live registers will be restored in the catch block if caught.
71 SaveLiveRegisters(codegen, instruction_->GetLocations());
72 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010073 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000074 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 }
76
Alexandre Rames8158f282015-08-07 10:26:17 +010077 bool IsFatal() const OVERRIDE { return true; }
78
Alexandre Rames9931f312015-06-19 14:47:01 +010079 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
80
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010082 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
84};
85
Andreas Gampe85b62f22015-09-09 13:15:38 -070086class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000087 public:
88 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
89
Alexandre Rames67555f72014-11-18 10:55:16 +000090 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000091 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
92 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000093 if (instruction_->CanThrowIntoCatchBlock()) {
94 // Live registers will be restored in the catch block if caught.
95 SaveLiveRegisters(codegen, instruction_->GetLocations());
96 }
Calin Juravled0d48522014-11-04 16:40:20 +000097 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000098 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Calin Juravled0d48522014-11-04 16:40:20 +000099 }
100
Alexandre Rames8158f282015-08-07 10:26:17 +0100101 bool IsFatal() const OVERRIDE { return true; }
102
Alexandre Rames9931f312015-06-19 14:47:01 +0100103 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
104
Calin Juravled0d48522014-11-04 16:40:20 +0000105 private:
106 HDivZeroCheck* const instruction_;
107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
108};
109
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000111 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000112 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100113 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000114
Alexandre Rames67555f72014-11-18 10:55:16 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100116 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000117 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000118 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100119 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000120 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000121 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100122 if (successor_ == nullptr) {
123 __ b(GetReturnLabel());
124 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100125 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100126 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000127 }
128
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 Label* GetReturnLabel() {
130 DCHECK(successor_ == nullptr);
131 return &return_label_;
132 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100134 HBasicBlock* GetSuccessor() const {
135 return successor_;
136 }
137
Alexandre Rames9931f312015-06-19 14:47:01 +0100138 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
139
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 private:
141 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 // If not null, the block to branch to after the suspend check.
143 HBasicBlock* const successor_;
144
145 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 Label return_label_;
147
148 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
149};
150
Andreas Gampe85b62f22015-09-09 13:15:38 -0700151class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100153 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
154 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155
Alexandre Rames67555f72014-11-18 10:55:16 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100157 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 LocationSummary* locations = instruction_->GetLocations();
159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000168 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100169 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100171 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100172 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100173 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
174 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100175 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000176 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 }
178
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 bool IsFatal() const OVERRIDE { return true; }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185
186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
187};
188
Andreas Gampe85b62f22015-09-09 13:15:38 -0700189class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000191 LoadClassSlowPathARM(HLoadClass* cls,
192 HInstruction* at,
193 uint32_t dex_pc,
194 bool do_clinit)
195 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
196 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
197 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198
Alexandre Rames67555f72014-11-18 10:55:16 +0000199 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000200 LocationSummary* locations = at_->GetLocations();
201
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100202 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
203 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000204 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100206 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 int32_t entry_point_offset = do_clinit_
209 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
210 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000214 Location out = locations->Out();
215 if (out.IsValid()) {
216 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
218 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000219 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220 __ b(GetExitLabel());
221 }
222
Alexandre Rames9931f312015-06-19 14:47:01 +0100223 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
224
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 // The class this slow path will load.
227 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 // The instruction where this slow path is happening.
230 // (Might be the load class or an initialization check).
231 HInstruction* const at_;
232
233 // The dex PC of `at_`.
234 const uint32_t dex_pc_;
235
236 // Whether to initialize the class.
237 const bool do_clinit_;
238
239 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240};
241
Andreas Gampe85b62f22015-09-09 13:15:38 -0700242class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000243 public:
244 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
245
Alexandre Rames67555f72014-11-18 10:55:16 +0000246 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 LocationSummary* locations = instruction_->GetLocations();
248 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
249
250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
251 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253
254 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000257 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
259
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000260 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000261 __ b(GetExitLabel());
262 }
263
Alexandre Rames9931f312015-06-19 14:47:01 +0100264 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
265
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000266 private:
267 HLoadString* const instruction_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
270};
271
Andreas Gampe85b62f22015-09-09 13:15:38 -0700272class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000274 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
275 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100279 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
280 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 DCHECK(instruction_->IsCheckCast()
282 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
285 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000286
287 if (instruction_->IsCheckCast()) {
288 // The codegen for the instruction overwrites `temp`, so put it back in place.
289 Register obj = locations->InAt(0).AsRegister<Register>();
290 Register temp = locations->GetTemp(0).AsRegister<Register>();
291 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
292 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
293 __ MaybeUnpoisonHeapReference(temp);
294 }
295
296 if (!is_fatal_) {
297 SaveLiveRegisters(codegen, locations);
298 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 // We're moving two locations to locations that could overlap, so we need a parallel
301 // move resolver.
302 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000303 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100304 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
309 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100312 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
313 instruction_,
314 instruction_->GetDexPc(),
315 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
317 } else {
318 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100319 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
320 instruction_,
321 instruction_->GetDexPc(),
322 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000325 if (!is_fatal_) {
326 RestoreLiveRegisters(codegen, locations);
327 __ b(GetExitLabel());
328 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 }
330
Alexandre Rames9931f312015-06-19 14:47:01 +0100331 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000333 bool IsFatal() const OVERRIDE { return is_fatal_; }
334
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338
339 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
340};
341
Andreas Gampe85b62f22015-09-09 13:15:38 -0700342class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700343 public:
344 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
345 : instruction_(instruction) {}
346
347 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, instruction_->GetLocations());
350 DCHECK(instruction_->IsDeoptimize());
351 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
352 uint32_t dex_pc = deoptimize->GetDexPc();
353 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
354 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
355 }
356
Alexandre Rames9931f312015-06-19 14:47:01 +0100357 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
358
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700359 private:
360 HInstruction* const instruction_;
361 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
362};
363
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100364class ArraySetSlowPathARM : public SlowPathCode {
365 public:
366 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
367
368 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
369 LocationSummary* locations = instruction_->GetLocations();
370 __ Bind(GetEntryLabel());
371 SaveLiveRegisters(codegen, locations);
372
373 InvokeRuntimeCallingConvention calling_convention;
374 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
375 parallel_move.AddMove(
376 locations->InAt(0),
377 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
378 Primitive::kPrimNot,
379 nullptr);
380 parallel_move.AddMove(
381 locations->InAt(1),
382 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
383 Primitive::kPrimInt,
384 nullptr);
385 parallel_move.AddMove(
386 locations->InAt(2),
387 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
388 Primitive::kPrimNot,
389 nullptr);
390 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
391
392 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
393 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
394 instruction_,
395 instruction_->GetDexPc(),
396 this);
397 RestoreLiveRegisters(codegen, locations);
398 __ b(GetExitLabel());
399 }
400
401 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
402
403 private:
404 HInstruction* const instruction_;
405
406 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
407};
408
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000409#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100410#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700411
Roland Levillain4fa13f62015-07-06 18:11:54 +0100412inline Condition ARMSignedOrFPCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700413 switch (cond) {
414 case kCondEQ: return EQ;
415 case kCondNE: return NE;
416 case kCondLT: return LT;
417 case kCondLE: return LE;
418 case kCondGT: return GT;
419 case kCondGE: return GE;
Dave Allison20dfc792014-06-16 20:44:29 -0700420 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100421 LOG(FATAL) << "Unreachable";
422 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700423}
424
Roland Levillain4fa13f62015-07-06 18:11:54 +0100425inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700426 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100427 case kCondEQ: return EQ;
428 case kCondNE: return NE;
429 case kCondLT: return LO;
430 case kCondLE: return LS;
431 case kCondGT: return HI;
432 case kCondGE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700433 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100434 LOG(FATAL) << "Unreachable";
435 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700436}
437
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100438void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100439 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100440}
441
442void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100443 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100444}
445
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100446size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
447 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
448 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100449}
450
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100451size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
452 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
453 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100454}
455
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000456size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
457 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
458 return kArmWordSize;
459}
460
461size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
462 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
463 return kArmWordSize;
464}
465
Calin Juravle34166012014-12-19 17:22:29 +0000466CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000467 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100468 const CompilerOptions& compiler_options,
469 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000470 : CodeGenerator(graph,
471 kNumberOfCoreRegisters,
472 kNumberOfSRegisters,
473 kNumberOfRegisterPairs,
474 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
475 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray88a95ba2015-09-30 17:18:14 +0100476 graph->IsDebuggable()
477 // If the graph is debuggable, we need to save the fpu registers ourselves,
478 // as the stubs do not do it.
479 ? 0
480 : ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
481 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100482 compiler_options,
483 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100484 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100485 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100486 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100487 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000488 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000489 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100490 method_patches_(MethodReferenceComparator(),
491 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
492 call_patches_(MethodReferenceComparator(),
493 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
494 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700495 // Always save the LR register to mimic Quick.
496 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100497}
498
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000499void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
500 // Ensure that we fix up branches and literal loads and emit the literal pool.
501 __ FinalizeCode();
502
503 // Adjust native pc offsets in stack maps.
504 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
505 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
506 uint32_t new_position = __ GetAdjustedPosition(old_position);
507 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
508 }
509 // Adjust native pc offsets of block labels.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100510 for (HBasicBlock* block : *block_order_) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000511 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
512 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
Vladimir Marko225b6462015-09-28 12:17:40 +0100513 DCHECK_LT(block->GetBlockId(), GetGraph()->GetBlocks().size());
514 Label* block_label = &block_labels_[block->GetBlockId()];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000515 DCHECK_EQ(block_label->IsBound(), !block->IsSingleJump());
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000516 if (block_label->IsBound()) {
517 __ AdjustLabelPosition(block_label);
518 }
519 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100520 // Adjust pc offsets for the disassembly information.
521 if (disasm_info_ != nullptr) {
522 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
523 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
524 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
525 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
526 it.second.start = __ GetAdjustedPosition(it.second.start);
527 it.second.end = __ GetAdjustedPosition(it.second.end);
528 }
529 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
530 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
531 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
532 }
533 }
Vladimir Marko58155012015-08-19 12:49:41 +0000534 // Adjust pc offsets for relative call patches.
535 for (MethodPatchInfo<Label>& info : relative_call_patches_) {
536 __ AdjustLabelPosition(&info.label);
537 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000538
539 CodeGenerator::Finalize(allocator);
540}
541
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100542Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100543 switch (type) {
544 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100545 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100546 ArmManagedRegister pair =
547 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100548 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
549 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
550
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100551 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
552 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100553 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100554 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100555 }
556
557 case Primitive::kPrimByte:
558 case Primitive::kPrimBoolean:
559 case Primitive::kPrimChar:
560 case Primitive::kPrimShort:
561 case Primitive::kPrimInt:
562 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100563 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100564 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100565 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
566 ArmManagedRegister current =
567 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
568 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100569 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100570 }
571 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100573 }
574
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000575 case Primitive::kPrimFloat: {
576 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100577 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100579
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000580 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000581 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
582 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000583 return Location::FpuRegisterPairLocation(reg, reg + 1);
584 }
585
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100586 case Primitive::kPrimVoid:
587 LOG(FATAL) << "Unreachable type " << type;
588 }
589
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100590 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100591}
592
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000593void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100594 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100595 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100596
597 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100598 blocked_core_registers_[SP] = true;
599 blocked_core_registers_[LR] = true;
600 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100601
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100602 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100603 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100604
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100605 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100606 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100607
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000608 if (is_baseline) {
609 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
610 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
611 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000612
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000613 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
614
615 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
616 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
617 }
618 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100619
620 UpdateBlockedPairRegisters();
621}
622
623void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
624 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
625 ArmManagedRegister current =
626 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
627 if (blocked_core_registers_[current.AsRegisterPairLow()]
628 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
629 blocked_register_pairs_[i] = true;
630 }
631 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100632}
633
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100634InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
635 : HGraphVisitor(graph),
636 assembler_(codegen->GetAssembler()),
637 codegen_(codegen) {}
638
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000639void CodeGeneratorARM::ComputeSpillMask() {
640 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000641 // Save one extra register for baseline. Note that on thumb2, there is no easy
642 // instruction to restore just the PC, so this actually helps both baseline
643 // and non-baseline to save and restore at least two registers at entry and exit.
644 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000645 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
646 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
647 // We use vpush and vpop for saving and restoring floating point registers, which take
648 // a SRegister and the number of registers to save/restore after that SRegister. We
649 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
650 // but in the range.
651 if (fpu_spill_mask_ != 0) {
652 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
653 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
654 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
655 fpu_spill_mask_ |= (1 << i);
656 }
657 }
658}
659
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100660static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100661 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100662}
663
664static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100665 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100666}
667
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000668void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000669 bool skip_overflow_check =
670 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000671 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000672 __ Bind(&frame_entry_label_);
673
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000674 if (HasEmptyFrame()) {
675 return;
676 }
677
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100678 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000679 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
680 __ LoadFromOffset(kLoadWord, IP, IP, 0);
681 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100682 }
683
Andreas Gampe501fd632015-09-10 16:11:06 -0700684 __ PushList(core_spill_mask_);
685 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
686 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000687 if (fpu_spill_mask_ != 0) {
688 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
689 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100690 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100691 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000692 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100693 int adjust = GetFrameSize() - FrameEntrySpillSize();
694 __ AddConstant(SP, -adjust);
695 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100696 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000697}
698
699void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000700 if (HasEmptyFrame()) {
701 __ bx(LR);
702 return;
703 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100704 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100705 int adjust = GetFrameSize() - FrameEntrySpillSize();
706 __ AddConstant(SP, adjust);
707 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000708 if (fpu_spill_mask_ != 0) {
709 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
710 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100711 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
712 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000713 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700714 // Pop LR into PC to return.
715 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
716 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
717 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100718 __ cfi().RestoreState();
719 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000720}
721
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100722void CodeGeneratorARM::Bind(HBasicBlock* block) {
723 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000724}
725
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100726Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
727 switch (load->GetType()) {
728 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100730 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100731
732 case Primitive::kPrimInt:
733 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100734 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100736
737 case Primitive::kPrimBoolean:
738 case Primitive::kPrimByte:
739 case Primitive::kPrimChar:
740 case Primitive::kPrimShort:
741 case Primitive::kPrimVoid:
742 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700743 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100744 }
745
746 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700747 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100748}
749
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100750Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100751 switch (type) {
752 case Primitive::kPrimBoolean:
753 case Primitive::kPrimByte:
754 case Primitive::kPrimChar:
755 case Primitive::kPrimShort:
756 case Primitive::kPrimInt:
757 case Primitive::kPrimNot: {
758 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000759 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100760 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100761 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100762 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000763 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100764 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100765 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100766
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000767 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100768 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000769 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100770 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000771 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100772 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000773 if (calling_convention.GetRegisterAt(index) == R1) {
774 // Skip R1, and use R2_R3 instead.
775 gp_index_++;
776 index++;
777 }
778 }
779 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
780 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000781 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100782
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000783 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000784 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100785 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000786 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
787 }
788 }
789
790 case Primitive::kPrimFloat: {
791 uint32_t stack_index = stack_index_++;
792 if (float_index_ % 2 == 0) {
793 float_index_ = std::max(double_index_, float_index_);
794 }
795 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
796 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
797 } else {
798 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
799 }
800 }
801
802 case Primitive::kPrimDouble: {
803 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
804 uint32_t stack_index = stack_index_;
805 stack_index_ += 2;
806 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
807 uint32_t index = double_index_;
808 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000809 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000810 calling_convention.GetFpuRegisterAt(index),
811 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000812 DCHECK(ExpectedPairLayout(result));
813 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000814 } else {
815 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100816 }
817 }
818
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100819 case Primitive::kPrimVoid:
820 LOG(FATAL) << "Unexpected parameter type " << type;
821 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100822 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100823 return Location();
824}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100825
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100826Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000827 switch (type) {
828 case Primitive::kPrimBoolean:
829 case Primitive::kPrimByte:
830 case Primitive::kPrimChar:
831 case Primitive::kPrimShort:
832 case Primitive::kPrimInt:
833 case Primitive::kPrimNot: {
834 return Location::RegisterLocation(R0);
835 }
836
837 case Primitive::kPrimFloat: {
838 return Location::FpuRegisterLocation(S0);
839 }
840
841 case Primitive::kPrimLong: {
842 return Location::RegisterPairLocation(R0, R1);
843 }
844
845 case Primitive::kPrimDouble: {
846 return Location::FpuRegisterPairLocation(S0, S1);
847 }
848
849 case Primitive::kPrimVoid:
850 return Location();
851 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100852
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000853 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000854}
855
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100856Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
857 return Location::RegisterLocation(kMethodRegisterArgument);
858}
859
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100860void CodeGeneratorARM::Move32(Location destination, Location source) {
861 if (source.Equals(destination)) {
862 return;
863 }
864 if (destination.IsRegister()) {
865 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000866 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100867 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100869 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000870 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100871 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100872 } else if (destination.IsFpuRegister()) {
873 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000874 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100875 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000876 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100877 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100879 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000881 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000883 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100884 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000885 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000887 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100888 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
889 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890 }
891 }
892}
893
894void CodeGeneratorARM::Move64(Location destination, Location source) {
895 if (source.Equals(destination)) {
896 return;
897 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100898 if (destination.IsRegisterPair()) {
899 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000900 EmitParallelMoves(
901 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
902 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100903 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000904 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100905 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
906 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000908 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100909 } else if (source.IsFpuRegisterPair()) {
910 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
911 destination.AsRegisterPairHigh<Register>(),
912 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 } else {
914 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000915 DCHECK(ExpectedPairLayout(destination));
916 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
917 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100918 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000919 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000921 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
922 SP,
923 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100924 } else if (source.IsRegisterPair()) {
925 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
926 source.AsRegisterPairLow<Register>(),
927 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100928 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000929 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100930 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100931 } else {
932 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100933 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000934 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100935 if (source.AsRegisterPairLow<Register>() == R1) {
936 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100937 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
938 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100939 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100941 SP, destination.GetStackIndex());
942 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000943 } else if (source.IsFpuRegisterPair()) {
944 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
945 SP,
946 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100947 } else {
948 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000949 EmitParallelMoves(
950 Location::StackSlot(source.GetStackIndex()),
951 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100952 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000953 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100954 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
955 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 }
957 }
958}
959
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100960void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100961 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100962 if (instruction->IsCurrentMethod()) {
963 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
964 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100965 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100966 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000967 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000968 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
969 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000970 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000971 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000972 } else {
973 DCHECK(location.IsStackSlot());
974 __ LoadImmediate(IP, value);
975 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
976 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000977 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000978 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000979 int64_t value = const_to_move->AsLongConstant()->GetValue();
980 if (location.IsRegisterPair()) {
981 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
982 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
983 } else {
984 DCHECK(location.IsDoubleStackSlot());
985 __ LoadImmediate(IP, Low32Bits(value));
986 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
987 __ LoadImmediate(IP, High32Bits(value));
988 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
989 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100990 }
Roland Levillain476df552014-10-09 17:51:36 +0100991 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
993 switch (instruction->GetType()) {
994 case Primitive::kPrimBoolean:
995 case Primitive::kPrimByte:
996 case Primitive::kPrimChar:
997 case Primitive::kPrimShort:
998 case Primitive::kPrimInt:
999 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001000 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001 Move32(location, Location::StackSlot(stack_slot));
1002 break;
1003
1004 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001006 Move64(location, Location::DoubleStackSlot(stack_slot));
1007 break;
1008
1009 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001012 } else if (instruction->IsTemporary()) {
1013 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001014 if (temp_location.IsStackSlot()) {
1015 Move32(location, temp_location);
1016 } else {
1017 DCHECK(temp_location.IsDoubleStackSlot());
1018 Move64(location, temp_location);
1019 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001020 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001021 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022 switch (instruction->GetType()) {
1023 case Primitive::kPrimBoolean:
1024 case Primitive::kPrimByte:
1025 case Primitive::kPrimChar:
1026 case Primitive::kPrimShort:
1027 case Primitive::kPrimNot:
1028 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001030 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001031 break;
1032
1033 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001034 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001035 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 break;
1037
1038 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001039 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001041 }
1042}
1043
Calin Juravle175dc732015-08-25 15:42:32 +01001044void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1045 DCHECK(location.IsRegister());
1046 __ LoadImmediate(location.AsRegister<Register>(), value);
1047}
1048
Calin Juravlee460d1d2015-09-29 04:52:17 +01001049void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1050 if (Primitive::Is64BitType(dst_type)) {
1051 Move64(dst, src);
1052 } else {
1053 Move32(dst, src);
1054 }
1055}
1056
1057void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1058 if (location.IsRegister()) {
1059 locations->AddTemp(location);
1060 } else if (location.IsRegisterPair()) {
1061 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1062 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1063 } else {
1064 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1065 }
1066}
1067
Calin Juravle175dc732015-08-25 15:42:32 +01001068void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1069 HInstruction* instruction,
1070 uint32_t dex_pc,
1071 SlowPathCode* slow_path) {
1072 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1073 instruction,
1074 dex_pc,
1075 slow_path);
1076}
1077
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001078void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1079 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001080 uint32_t dex_pc,
1081 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001082 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001083 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1084 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001085 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001086}
1087
David Brazdilfc6a86a2015-06-26 10:33:45 +00001088void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001089 DCHECK(!successor->IsExitBlock());
1090
1091 HBasicBlock* block = got->GetBlock();
1092 HInstruction* previous = got->GetPrevious();
1093
1094 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001095 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001096 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1097 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1098 return;
1099 }
1100
1101 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1102 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1103 }
1104 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001105 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001106 }
1107}
1108
David Brazdilfc6a86a2015-06-26 10:33:45 +00001109void LocationsBuilderARM::VisitGoto(HGoto* got) {
1110 got->SetLocations(nullptr);
1111}
1112
1113void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1114 HandleGoto(got, got->GetSuccessor());
1115}
1116
1117void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1118 try_boundary->SetLocations(nullptr);
1119}
1120
1121void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1122 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1123 if (!successor->IsExitBlock()) {
1124 HandleGoto(try_boundary, successor);
1125 }
1126}
1127
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001128void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001129 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001130}
1131
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001132void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001133 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001134}
1135
Roland Levillain4fa13f62015-07-06 18:11:54 +01001136void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1137 ShifterOperand operand;
1138 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1139 __ cmp(left, operand);
1140 } else {
1141 Register temp = IP;
1142 __ LoadImmediate(temp, right);
1143 __ cmp(left, ShifterOperand(temp));
1144 }
1145}
1146
1147void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1148 Label* true_label,
1149 Label* false_label) {
1150 __ vmstat(); // transfer FP status register to ARM APSR.
1151 if (cond->IsFPConditionTrueIfNaN()) {
1152 __ b(true_label, VS); // VS for unordered.
1153 } else if (cond->IsFPConditionFalseIfNaN()) {
1154 __ b(false_label, VS); // VS for unordered.
1155 }
1156 __ b(true_label, ARMSignedOrFPCondition(cond->GetCondition()));
1157}
1158
1159void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1160 Label* true_label,
1161 Label* false_label) {
1162 LocationSummary* locations = cond->GetLocations();
1163 Location left = locations->InAt(0);
1164 Location right = locations->InAt(1);
1165 IfCondition if_cond = cond->GetCondition();
1166
1167 Register left_high = left.AsRegisterPairHigh<Register>();
1168 Register left_low = left.AsRegisterPairLow<Register>();
1169 IfCondition true_high_cond = if_cond;
1170 IfCondition false_high_cond = cond->GetOppositeCondition();
1171 Condition final_condition = ARMUnsignedCondition(if_cond);
1172
1173 // Set the conditions for the test, remembering that == needs to be
1174 // decided using the low words.
1175 switch (if_cond) {
1176 case kCondEQ:
1177 case kCondNE:
1178 // Nothing to do.
1179 break;
1180 case kCondLT:
1181 false_high_cond = kCondGT;
1182 break;
1183 case kCondLE:
1184 true_high_cond = kCondLT;
1185 break;
1186 case kCondGT:
1187 false_high_cond = kCondLT;
1188 break;
1189 case kCondGE:
1190 true_high_cond = kCondGT;
1191 break;
1192 }
1193 if (right.IsConstant()) {
1194 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1195 int32_t val_low = Low32Bits(value);
1196 int32_t val_high = High32Bits(value);
1197
1198 GenerateCompareWithImmediate(left_high, val_high);
1199 if (if_cond == kCondNE) {
1200 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1201 } else if (if_cond == kCondEQ) {
1202 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1203 } else {
1204 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1205 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1206 }
1207 // Must be equal high, so compare the lows.
1208 GenerateCompareWithImmediate(left_low, val_low);
1209 } else {
1210 Register right_high = right.AsRegisterPairHigh<Register>();
1211 Register right_low = right.AsRegisterPairLow<Register>();
1212
1213 __ cmp(left_high, ShifterOperand(right_high));
1214 if (if_cond == kCondNE) {
1215 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1216 } else if (if_cond == kCondEQ) {
1217 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1218 } else {
1219 __ b(true_label, ARMSignedOrFPCondition(true_high_cond));
1220 __ b(false_label, ARMSignedOrFPCondition(false_high_cond));
1221 }
1222 // Must be equal high, so compare the lows.
1223 __ cmp(left_low, ShifterOperand(right_low));
1224 }
1225 // The last comparison might be unsigned.
1226 __ b(true_label, final_condition);
1227}
1228
1229void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1230 HCondition* condition,
1231 Label* true_target,
1232 Label* false_target,
1233 Label* always_true_target) {
1234 LocationSummary* locations = condition->GetLocations();
1235 Location left = locations->InAt(0);
1236 Location right = locations->InAt(1);
1237
1238 // We don't want true_target as a nullptr.
1239 if (true_target == nullptr) {
1240 true_target = always_true_target;
1241 }
1242 bool falls_through = (false_target == nullptr);
1243
1244 // FP compares don't like null false_targets.
1245 if (false_target == nullptr) {
1246 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1247 }
1248
1249 Primitive::Type type = condition->InputAt(0)->GetType();
1250 switch (type) {
1251 case Primitive::kPrimLong:
1252 GenerateLongComparesAndJumps(condition, true_target, false_target);
1253 break;
1254 case Primitive::kPrimFloat:
1255 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1256 GenerateFPJumps(condition, true_target, false_target);
1257 break;
1258 case Primitive::kPrimDouble:
1259 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1260 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1261 GenerateFPJumps(condition, true_target, false_target);
1262 break;
1263 default:
1264 LOG(FATAL) << "Unexpected compare type " << type;
1265 }
1266
1267 if (!falls_through) {
1268 __ b(false_target);
1269 }
1270}
1271
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001272void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1273 Label* true_target,
1274 Label* false_target,
1275 Label* always_true_target) {
1276 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001277 if (cond->IsIntConstant()) {
1278 // Constant condition, statically compared against 1.
1279 int32_t cond_value = cond->AsIntConstant()->GetValue();
1280 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001281 if (always_true_target != nullptr) {
1282 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001283 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001284 return;
1285 } else {
1286 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001287 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001288 } else {
1289 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1290 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001291 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001292 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1293 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001294 } else {
1295 // Condition has not been materialized, use its inputs as the
1296 // comparison and its condition as the branch condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001297 Primitive::Type type =
1298 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
1299 // Is this a long or FP comparison that has been folded into the HCondition?
1300 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1301 // Generate the comparison directly.
1302 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1303 true_target, false_target, always_true_target);
1304 return;
1305 }
1306
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001307 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001308 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001309 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001310 Location right = locations->InAt(1);
1311 if (right.IsRegister()) {
1312 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001313 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001314 DCHECK(right.IsConstant());
1315 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001316 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001317 __ b(true_target, ARMSignedOrFPCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001318 }
Dave Allison20dfc792014-06-16 20:44:29 -07001319 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001320 if (false_target != nullptr) {
1321 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001322 }
1323}
1324
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001325void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1326 LocationSummary* locations =
1327 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1328 HInstruction* cond = if_instr->InputAt(0);
1329 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1330 locations->SetInAt(0, Location::RequiresRegister());
1331 }
1332}
1333
1334void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1335 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1336 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1337 Label* always_true_target = true_target;
1338 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1339 if_instr->IfTrueSuccessor())) {
1340 always_true_target = nullptr;
1341 }
1342 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1343 if_instr->IfFalseSuccessor())) {
1344 false_target = nullptr;
1345 }
1346 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1347}
1348
1349void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1350 LocationSummary* locations = new (GetGraph()->GetArena())
1351 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1352 HInstruction* cond = deoptimize->InputAt(0);
1353 DCHECK(cond->IsCondition());
1354 if (cond->AsCondition()->NeedsMaterialization()) {
1355 locations->SetInAt(0, Location::RequiresRegister());
1356 }
1357}
1358
1359void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001360 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001361 DeoptimizationSlowPathARM(deoptimize);
1362 codegen_->AddSlowPath(slow_path);
1363 Label* slow_path_entry = slow_path->GetEntryLabel();
1364 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1365}
Dave Allison20dfc792014-06-16 20:44:29 -07001366
Roland Levillain0d37cd02015-05-27 16:39:19 +01001367void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001368 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001369 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001370 // Handle the long/FP comparisons made in instruction simplification.
1371 switch (cond->InputAt(0)->GetType()) {
1372 case Primitive::kPrimLong:
1373 locations->SetInAt(0, Location::RequiresRegister());
1374 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1375 if (cond->NeedsMaterialization()) {
1376 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1377 }
1378 break;
1379
1380 case Primitive::kPrimFloat:
1381 case Primitive::kPrimDouble:
1382 locations->SetInAt(0, Location::RequiresFpuRegister());
1383 locations->SetInAt(1, Location::RequiresFpuRegister());
1384 if (cond->NeedsMaterialization()) {
1385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1386 }
1387 break;
1388
1389 default:
1390 locations->SetInAt(0, Location::RequiresRegister());
1391 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1392 if (cond->NeedsMaterialization()) {
1393 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1394 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001395 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001396}
1397
Roland Levillain0d37cd02015-05-27 16:39:19 +01001398void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 if (!cond->NeedsMaterialization()) {
1400 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001401 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402
1403 LocationSummary* locations = cond->GetLocations();
1404 Location left = locations->InAt(0);
1405 Location right = locations->InAt(1);
1406 Register out = locations->Out().AsRegister<Register>();
1407 Label true_label, false_label;
1408
1409 switch (cond->InputAt(0)->GetType()) {
1410 default: {
1411 // Integer case.
1412 if (right.IsRegister()) {
1413 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1414 } else {
1415 DCHECK(right.IsConstant());
1416 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1417 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1418 }
1419 __ it(ARMSignedOrFPCondition(cond->GetCondition()), kItElse);
1420 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
1421 ARMSignedOrFPCondition(cond->GetCondition()));
1422 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
1423 ARMSignedOrFPCondition(cond->GetOppositeCondition()));
1424 return;
1425 }
1426 case Primitive::kPrimLong:
1427 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1428 break;
1429 case Primitive::kPrimFloat:
1430 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1431 GenerateFPJumps(cond, &true_label, &false_label);
1432 break;
1433 case Primitive::kPrimDouble:
1434 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1435 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1436 GenerateFPJumps(cond, &true_label, &false_label);
1437 break;
1438 }
1439
1440 // Convert the jumps into the result.
1441 Label done_label;
1442
1443 // False case: result = 0.
1444 __ Bind(&false_label);
1445 __ LoadImmediate(out, 0);
1446 __ b(&done_label);
1447
1448 // True case: result = 1.
1449 __ Bind(&true_label);
1450 __ LoadImmediate(out, 1);
1451 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001452}
1453
1454void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1455 VisitCondition(comp);
1456}
1457
1458void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1459 VisitCondition(comp);
1460}
1461
1462void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1463 VisitCondition(comp);
1464}
1465
1466void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1467 VisitCondition(comp);
1468}
1469
1470void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1471 VisitCondition(comp);
1472}
1473
1474void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1475 VisitCondition(comp);
1476}
1477
1478void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1479 VisitCondition(comp);
1480}
1481
1482void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1483 VisitCondition(comp);
1484}
1485
1486void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1487 VisitCondition(comp);
1488}
1489
1490void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1491 VisitCondition(comp);
1492}
1493
1494void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1495 VisitCondition(comp);
1496}
1497
1498void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1499 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001500}
1501
1502void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001503 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001504}
1505
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001506void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1507 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001508}
1509
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001510void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001511 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001512}
1513
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001514void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001515 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001516 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001517}
1518
1519void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001520 LocationSummary* locations =
1521 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001522 switch (store->InputAt(1)->GetType()) {
1523 case Primitive::kPrimBoolean:
1524 case Primitive::kPrimByte:
1525 case Primitive::kPrimChar:
1526 case Primitive::kPrimShort:
1527 case Primitive::kPrimInt:
1528 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001529 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001530 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1531 break;
1532
1533 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001534 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001535 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1536 break;
1537
1538 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001539 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001540 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001541}
1542
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001543void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001544 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001545}
1546
1547void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001548 LocationSummary* locations =
1549 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001550 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001551}
1552
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001553void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001554 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001555 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001556}
1557
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001558void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1559 LocationSummary* locations =
1560 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1561 locations->SetOut(Location::ConstantLocation(constant));
1562}
1563
1564void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1565 // Will be generated at use site.
1566 UNUSED(constant);
1567}
1568
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001569void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001570 LocationSummary* locations =
1571 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001572 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573}
1574
1575void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1576 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001577 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578}
1579
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001580void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1581 LocationSummary* locations =
1582 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1583 locations->SetOut(Location::ConstantLocation(constant));
1584}
1585
1586void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1587 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001588 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001589}
1590
1591void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1592 LocationSummary* locations =
1593 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1594 locations->SetOut(Location::ConstantLocation(constant));
1595}
1596
1597void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1598 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001599 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001600}
1601
Calin Juravle27df7582015-04-17 19:12:31 +01001602void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1603 memory_barrier->SetLocations(nullptr);
1604}
1605
1606void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1607 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1608}
1609
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001610void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001611 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001612}
1613
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001614void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001615 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001616 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001617}
1618
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001619void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001620 LocationSummary* locations =
1621 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001622 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001623}
1624
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001625void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001626 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001627 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001628}
1629
Calin Juravle175dc732015-08-25 15:42:32 +01001630void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1631 // The trampoline uses the same calling convention as dex calling conventions,
1632 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1633 // the method_idx.
1634 HandleInvoke(invoke);
1635}
1636
1637void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1638 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1639}
1640
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001641void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001642 // When we do not run baseline, explicit clinit checks triggered by static
1643 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1644 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001645
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001646 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1647 codegen_->GetInstructionSetFeatures());
1648 if (intrinsic.TryDispatch(invoke)) {
1649 return;
1650 }
1651
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001652 HandleInvoke(invoke);
1653}
1654
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001655static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1656 if (invoke->GetLocations()->Intrinsified()) {
1657 IntrinsicCodeGeneratorARM intrinsic(codegen);
1658 intrinsic.Dispatch(invoke);
1659 return true;
1660 }
1661 return false;
1662}
1663
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001664void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001665 // When we do not run baseline, explicit clinit checks triggered by static
1666 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1667 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001668
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001669 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1670 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001671 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001672
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001673 LocationSummary* locations = invoke->GetLocations();
1674 codegen_->GenerateStaticOrDirectCall(
1675 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001676 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001677}
1678
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001679void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001680 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001681 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001682}
1683
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001684void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001685 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
1686 codegen_->GetInstructionSetFeatures());
1687 if (intrinsic.TryDispatch(invoke)) {
1688 return;
1689 }
1690
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001691 HandleInvoke(invoke);
1692}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001693
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001694void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001695 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1696 return;
1697 }
1698
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001699 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001700 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001701 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001702}
1703
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001704void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1705 HandleInvoke(invoke);
1706 // Add the hidden argument.
1707 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1708}
1709
1710void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1711 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001712 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001713 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1714 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001715 LocationSummary* locations = invoke->GetLocations();
1716 Location receiver = locations->InAt(0);
1717 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1718
1719 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001720 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1721 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001722
1723 // temp = object->GetClass();
1724 if (receiver.IsStackSlot()) {
1725 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1726 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1727 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001729 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001730 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001731 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001732 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001733 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001734 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001735 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1736 // LR = temp->GetEntryPoint();
1737 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1738 // LR();
1739 __ blx(LR);
1740 DCHECK(!codegen_->IsLeafMethod());
1741 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1742}
1743
Roland Levillain88cb1752014-10-20 16:36:47 +01001744void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1745 LocationSummary* locations =
1746 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1747 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001748 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001749 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001750 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1751 break;
1752 }
1753 case Primitive::kPrimLong: {
1754 locations->SetInAt(0, Location::RequiresRegister());
1755 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001756 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001757 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001758
Roland Levillain88cb1752014-10-20 16:36:47 +01001759 case Primitive::kPrimFloat:
1760 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001761 locations->SetInAt(0, Location::RequiresFpuRegister());
1762 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001763 break;
1764
1765 default:
1766 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1767 }
1768}
1769
1770void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1771 LocationSummary* locations = neg->GetLocations();
1772 Location out = locations->Out();
1773 Location in = locations->InAt(0);
1774 switch (neg->GetResultType()) {
1775 case Primitive::kPrimInt:
1776 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001777 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001778 break;
1779
1780 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001781 DCHECK(in.IsRegisterPair());
1782 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1783 __ rsbs(out.AsRegisterPairLow<Register>(),
1784 in.AsRegisterPairLow<Register>(),
1785 ShifterOperand(0));
1786 // We cannot emit an RSC (Reverse Subtract with Carry)
1787 // instruction here, as it does not exist in the Thumb-2
1788 // instruction set. We use the following approach
1789 // using SBC and SUB instead.
1790 //
1791 // out.hi = -C
1792 __ sbc(out.AsRegisterPairHigh<Register>(),
1793 out.AsRegisterPairHigh<Register>(),
1794 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1795 // out.hi = out.hi - in.hi
1796 __ sub(out.AsRegisterPairHigh<Register>(),
1797 out.AsRegisterPairHigh<Register>(),
1798 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1799 break;
1800
Roland Levillain88cb1752014-10-20 16:36:47 +01001801 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001802 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001803 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001804 break;
1805
Roland Levillain88cb1752014-10-20 16:36:47 +01001806 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001807 DCHECK(in.IsFpuRegisterPair());
1808 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1809 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001810 break;
1811
1812 default:
1813 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1814 }
1815}
1816
Roland Levillaindff1f282014-11-05 14:15:05 +00001817void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001818 Primitive::Type result_type = conversion->GetResultType();
1819 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001820 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001821
Roland Levillain5b3ee562015-04-14 16:02:41 +01001822 // The float-to-long, double-to-long and long-to-float type conversions
1823 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001824 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001825 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1826 && result_type == Primitive::kPrimLong)
1827 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001828 ? LocationSummary::kCall
1829 : LocationSummary::kNoCall;
1830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1832
David Brazdilb2bd1c52015-03-25 11:17:37 +00001833 // The Java language does not allow treating boolean as an integral type but
1834 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001835
Roland Levillaindff1f282014-11-05 14:15:05 +00001836 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001837 case Primitive::kPrimByte:
1838 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001839 case Primitive::kPrimBoolean:
1840 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001841 case Primitive::kPrimShort:
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001844 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001845 locations->SetInAt(0, Location::RequiresRegister());
1846 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1847 break;
1848
1849 default:
1850 LOG(FATAL) << "Unexpected type conversion from " << input_type
1851 << " to " << result_type;
1852 }
1853 break;
1854
Roland Levillain01a8d712014-11-14 16:27:39 +00001855 case Primitive::kPrimShort:
1856 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001857 case Primitive::kPrimBoolean:
1858 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001859 case Primitive::kPrimByte:
1860 case Primitive::kPrimInt:
1861 case Primitive::kPrimChar:
1862 // Processing a Dex `int-to-short' instruction.
1863 locations->SetInAt(0, Location::RequiresRegister());
1864 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1865 break;
1866
1867 default:
1868 LOG(FATAL) << "Unexpected type conversion from " << input_type
1869 << " to " << result_type;
1870 }
1871 break;
1872
Roland Levillain946e1432014-11-11 17:35:19 +00001873 case Primitive::kPrimInt:
1874 switch (input_type) {
1875 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001876 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001877 locations->SetInAt(0, Location::Any());
1878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1879 break;
1880
1881 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001882 // Processing a Dex `float-to-int' instruction.
1883 locations->SetInAt(0, Location::RequiresFpuRegister());
1884 locations->SetOut(Location::RequiresRegister());
1885 locations->AddTemp(Location::RequiresFpuRegister());
1886 break;
1887
Roland Levillain946e1432014-11-11 17:35:19 +00001888 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001889 // Processing a Dex `double-to-int' instruction.
1890 locations->SetInAt(0, Location::RequiresFpuRegister());
1891 locations->SetOut(Location::RequiresRegister());
1892 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001893 break;
1894
1895 default:
1896 LOG(FATAL) << "Unexpected type conversion from " << input_type
1897 << " to " << result_type;
1898 }
1899 break;
1900
Roland Levillaindff1f282014-11-05 14:15:05 +00001901 case Primitive::kPrimLong:
1902 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001903 case Primitive::kPrimBoolean:
1904 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001905 case Primitive::kPrimByte:
1906 case Primitive::kPrimShort:
1907 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001908 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001909 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001910 locations->SetInAt(0, Location::RequiresRegister());
1911 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1912 break;
1913
Roland Levillain624279f2014-12-04 11:54:28 +00001914 case Primitive::kPrimFloat: {
1915 // Processing a Dex `float-to-long' instruction.
1916 InvokeRuntimeCallingConvention calling_convention;
1917 locations->SetInAt(0, Location::FpuRegisterLocation(
1918 calling_convention.GetFpuRegisterAt(0)));
1919 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1920 break;
1921 }
1922
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001923 case Primitive::kPrimDouble: {
1924 // Processing a Dex `double-to-long' instruction.
1925 InvokeRuntimeCallingConvention calling_convention;
1926 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1927 calling_convention.GetFpuRegisterAt(0),
1928 calling_convention.GetFpuRegisterAt(1)));
1929 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001930 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001931 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001932
1933 default:
1934 LOG(FATAL) << "Unexpected type conversion from " << input_type
1935 << " to " << result_type;
1936 }
1937 break;
1938
Roland Levillain981e4542014-11-14 11:47:14 +00001939 case Primitive::kPrimChar:
1940 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001941 case Primitive::kPrimBoolean:
1942 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001943 case Primitive::kPrimByte:
1944 case Primitive::kPrimShort:
1945 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001946 // Processing a Dex `int-to-char' instruction.
1947 locations->SetInAt(0, Location::RequiresRegister());
1948 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1949 break;
1950
1951 default:
1952 LOG(FATAL) << "Unexpected type conversion from " << input_type
1953 << " to " << result_type;
1954 }
1955 break;
1956
Roland Levillaindff1f282014-11-05 14:15:05 +00001957 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001958 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001959 case Primitive::kPrimBoolean:
1960 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001961 case Primitive::kPrimByte:
1962 case Primitive::kPrimShort:
1963 case Primitive::kPrimInt:
1964 case Primitive::kPrimChar:
1965 // Processing a Dex `int-to-float' instruction.
1966 locations->SetInAt(0, Location::RequiresRegister());
1967 locations->SetOut(Location::RequiresFpuRegister());
1968 break;
1969
Roland Levillain5b3ee562015-04-14 16:02:41 +01001970 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00001971 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01001972 InvokeRuntimeCallingConvention calling_convention;
1973 locations->SetInAt(0, Location::RegisterPairLocation(
1974 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1975 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001976 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01001977 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00001978
Roland Levillaincff13742014-11-17 14:32:17 +00001979 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001980 // Processing a Dex `double-to-float' instruction.
1981 locations->SetInAt(0, Location::RequiresFpuRegister());
1982 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001983 break;
1984
1985 default:
1986 LOG(FATAL) << "Unexpected type conversion from " << input_type
1987 << " to " << result_type;
1988 };
1989 break;
1990
Roland Levillaindff1f282014-11-05 14:15:05 +00001991 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001992 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001993 case Primitive::kPrimBoolean:
1994 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001995 case Primitive::kPrimByte:
1996 case Primitive::kPrimShort:
1997 case Primitive::kPrimInt:
1998 case Primitive::kPrimChar:
1999 // Processing a Dex `int-to-double' instruction.
2000 locations->SetInAt(0, Location::RequiresRegister());
2001 locations->SetOut(Location::RequiresFpuRegister());
2002 break;
2003
2004 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002005 // Processing a Dex `long-to-double' instruction.
2006 locations->SetInAt(0, Location::RequiresRegister());
2007 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002008 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002009 locations->AddTemp(Location::RequiresFpuRegister());
2010 break;
2011
Roland Levillaincff13742014-11-17 14:32:17 +00002012 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002013 // Processing a Dex `float-to-double' instruction.
2014 locations->SetInAt(0, Location::RequiresFpuRegister());
2015 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002016 break;
2017
2018 default:
2019 LOG(FATAL) << "Unexpected type conversion from " << input_type
2020 << " to " << result_type;
2021 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002022 break;
2023
2024 default:
2025 LOG(FATAL) << "Unexpected type conversion from " << input_type
2026 << " to " << result_type;
2027 }
2028}
2029
2030void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2031 LocationSummary* locations = conversion->GetLocations();
2032 Location out = locations->Out();
2033 Location in = locations->InAt(0);
2034 Primitive::Type result_type = conversion->GetResultType();
2035 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002036 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002037 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002038 case Primitive::kPrimByte:
2039 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002040 case Primitive::kPrimBoolean:
2041 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002042 case Primitive::kPrimShort:
2043 case Primitive::kPrimInt:
2044 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002045 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002047 break;
2048
2049 default:
2050 LOG(FATAL) << "Unexpected type conversion from " << input_type
2051 << " to " << result_type;
2052 }
2053 break;
2054
Roland Levillain01a8d712014-11-14 16:27:39 +00002055 case Primitive::kPrimShort:
2056 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002057 case Primitive::kPrimBoolean:
2058 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002059 case Primitive::kPrimByte:
2060 case Primitive::kPrimInt:
2061 case Primitive::kPrimChar:
2062 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002064 break;
2065
2066 default:
2067 LOG(FATAL) << "Unexpected type conversion from " << input_type
2068 << " to " << result_type;
2069 }
2070 break;
2071
Roland Levillain946e1432014-11-11 17:35:19 +00002072 case Primitive::kPrimInt:
2073 switch (input_type) {
2074 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002075 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002076 DCHECK(out.IsRegister());
2077 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002078 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002079 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002080 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002081 } else {
2082 DCHECK(in.IsConstant());
2083 DCHECK(in.GetConstant()->IsLongConstant());
2084 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002085 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002086 }
2087 break;
2088
Roland Levillain3f8f9362014-12-02 17:45:01 +00002089 case Primitive::kPrimFloat: {
2090 // Processing a Dex `float-to-int' instruction.
2091 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2092 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2093 __ vcvtis(temp, temp);
2094 __ vmovrs(out.AsRegister<Register>(), temp);
2095 break;
2096 }
2097
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002098 case Primitive::kPrimDouble: {
2099 // Processing a Dex `double-to-int' instruction.
2100 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2101 DRegister temp_d = FromLowSToD(temp_s);
2102 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2103 __ vcvtid(temp_s, temp_d);
2104 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002105 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002106 }
Roland Levillain946e1432014-11-11 17:35:19 +00002107
2108 default:
2109 LOG(FATAL) << "Unexpected type conversion from " << input_type
2110 << " to " << result_type;
2111 }
2112 break;
2113
Roland Levillaindff1f282014-11-05 14:15:05 +00002114 case Primitive::kPrimLong:
2115 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002116 case Primitive::kPrimBoolean:
2117 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002118 case Primitive::kPrimByte:
2119 case Primitive::kPrimShort:
2120 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002121 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002122 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002123 DCHECK(out.IsRegisterPair());
2124 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002125 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002126 // Sign extension.
2127 __ Asr(out.AsRegisterPairHigh<Register>(),
2128 out.AsRegisterPairLow<Register>(),
2129 31);
2130 break;
2131
2132 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002133 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002134 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2135 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002136 conversion->GetDexPc(),
2137 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002138 break;
2139
Roland Levillaindff1f282014-11-05 14:15:05 +00002140 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002141 // Processing a Dex `double-to-long' instruction.
2142 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2143 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002144 conversion->GetDexPc(),
2145 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002146 break;
2147
2148 default:
2149 LOG(FATAL) << "Unexpected type conversion from " << input_type
2150 << " to " << result_type;
2151 }
2152 break;
2153
Roland Levillain981e4542014-11-14 11:47:14 +00002154 case Primitive::kPrimChar:
2155 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002156 case Primitive::kPrimBoolean:
2157 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002158 case Primitive::kPrimByte:
2159 case Primitive::kPrimShort:
2160 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002161 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002162 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002163 break;
2164
2165 default:
2166 LOG(FATAL) << "Unexpected type conversion from " << input_type
2167 << " to " << result_type;
2168 }
2169 break;
2170
Roland Levillaindff1f282014-11-05 14:15:05 +00002171 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002172 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002173 case Primitive::kPrimBoolean:
2174 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002175 case Primitive::kPrimByte:
2176 case Primitive::kPrimShort:
2177 case Primitive::kPrimInt:
2178 case Primitive::kPrimChar: {
2179 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002180 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2181 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002182 break;
2183 }
2184
Roland Levillain5b3ee562015-04-14 16:02:41 +01002185 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002186 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002187 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2188 conversion,
2189 conversion->GetDexPc(),
2190 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002191 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002192
Roland Levillaincff13742014-11-17 14:32:17 +00002193 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002194 // Processing a Dex `double-to-float' instruction.
2195 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2196 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002197 break;
2198
2199 default:
2200 LOG(FATAL) << "Unexpected type conversion from " << input_type
2201 << " to " << result_type;
2202 };
2203 break;
2204
Roland Levillaindff1f282014-11-05 14:15:05 +00002205 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002206 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002207 case Primitive::kPrimBoolean:
2208 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002209 case Primitive::kPrimByte:
2210 case Primitive::kPrimShort:
2211 case Primitive::kPrimInt:
2212 case Primitive::kPrimChar: {
2213 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002214 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002215 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2216 out.AsFpuRegisterPairLow<SRegister>());
2217 break;
2218 }
2219
Roland Levillain647b9ed2014-11-27 12:06:00 +00002220 case Primitive::kPrimLong: {
2221 // Processing a Dex `long-to-double' instruction.
2222 Register low = in.AsRegisterPairLow<Register>();
2223 Register high = in.AsRegisterPairHigh<Register>();
2224 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2225 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002226 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002227 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002228 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2229 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002230
Roland Levillain682393c2015-04-14 15:57:52 +01002231 // temp_d = int-to-double(high)
2232 __ vmovsr(temp_s, high);
2233 __ vcvtdi(temp_d, temp_s);
2234 // constant_d = k2Pow32EncodingForDouble
2235 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2236 // out_d = unsigned-to-double(low)
2237 __ vmovsr(out_s, low);
2238 __ vcvtdu(out_d, out_s);
2239 // out_d += temp_d * constant_d
2240 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002241 break;
2242 }
2243
Roland Levillaincff13742014-11-17 14:32:17 +00002244 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002245 // Processing a Dex `float-to-double' instruction.
2246 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2247 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002248 break;
2249
2250 default:
2251 LOG(FATAL) << "Unexpected type conversion from " << input_type
2252 << " to " << result_type;
2253 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002254 break;
2255
2256 default:
2257 LOG(FATAL) << "Unexpected type conversion from " << input_type
2258 << " to " << result_type;
2259 }
2260}
2261
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002262void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002263 LocationSummary* locations =
2264 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002265 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002266 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002267 locations->SetInAt(0, Location::RequiresRegister());
2268 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2270 break;
2271 }
2272
2273 case Primitive::kPrimLong: {
2274 locations->SetInAt(0, Location::RequiresRegister());
2275 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002277 break;
2278 }
2279
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002280 case Primitive::kPrimFloat:
2281 case Primitive::kPrimDouble: {
2282 locations->SetInAt(0, Location::RequiresFpuRegister());
2283 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002284 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002285 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002286 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002287
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002288 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002289 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002290 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002291}
2292
2293void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2294 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002295 Location out = locations->Out();
2296 Location first = locations->InAt(0);
2297 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002298 switch (add->GetResultType()) {
2299 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002300 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002301 __ add(out.AsRegister<Register>(),
2302 first.AsRegister<Register>(),
2303 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002304 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002305 __ AddConstant(out.AsRegister<Register>(),
2306 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002307 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002308 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002309 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002311 case Primitive::kPrimLong: {
2312 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002313 __ adds(out.AsRegisterPairLow<Register>(),
2314 first.AsRegisterPairLow<Register>(),
2315 ShifterOperand(second.AsRegisterPairLow<Register>()));
2316 __ adc(out.AsRegisterPairHigh<Register>(),
2317 first.AsRegisterPairHigh<Register>(),
2318 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002319 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002320 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002321
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002322 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002323 __ vadds(out.AsFpuRegister<SRegister>(),
2324 first.AsFpuRegister<SRegister>(),
2325 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002326 break;
2327
2328 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002329 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2330 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2331 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002332 break;
2333
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002334 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002335 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002336 }
2337}
2338
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002339void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002340 LocationSummary* locations =
2341 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002342 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002343 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002344 locations->SetInAt(0, Location::RequiresRegister());
2345 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2347 break;
2348 }
2349
2350 case Primitive::kPrimLong: {
2351 locations->SetInAt(0, Location::RequiresRegister());
2352 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002354 break;
2355 }
Calin Juravle11351682014-10-23 15:38:15 +01002356 case Primitive::kPrimFloat:
2357 case Primitive::kPrimDouble: {
2358 locations->SetInAt(0, Location::RequiresFpuRegister());
2359 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002360 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002361 break;
Calin Juravle11351682014-10-23 15:38:15 +01002362 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002363 default:
Calin Juravle11351682014-10-23 15:38:15 +01002364 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002365 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002366}
2367
2368void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2369 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002370 Location out = locations->Out();
2371 Location first = locations->InAt(0);
2372 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002373 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002374 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002375 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002376 __ sub(out.AsRegister<Register>(),
2377 first.AsRegister<Register>(),
2378 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002379 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002380 __ AddConstant(out.AsRegister<Register>(),
2381 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002382 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002383 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002384 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002385 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002386
Calin Juravle11351682014-10-23 15:38:15 +01002387 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002388 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002389 __ subs(out.AsRegisterPairLow<Register>(),
2390 first.AsRegisterPairLow<Register>(),
2391 ShifterOperand(second.AsRegisterPairLow<Register>()));
2392 __ sbc(out.AsRegisterPairHigh<Register>(),
2393 first.AsRegisterPairHigh<Register>(),
2394 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002395 break;
Calin Juravle11351682014-10-23 15:38:15 +01002396 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002397
Calin Juravle11351682014-10-23 15:38:15 +01002398 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002399 __ vsubs(out.AsFpuRegister<SRegister>(),
2400 first.AsFpuRegister<SRegister>(),
2401 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002402 break;
Calin Juravle11351682014-10-23 15:38:15 +01002403 }
2404
2405 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002406 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2407 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2408 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002409 break;
2410 }
2411
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002412
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002413 default:
Calin Juravle11351682014-10-23 15:38:15 +01002414 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002415 }
2416}
2417
Calin Juravle34bacdf2014-10-07 20:23:36 +01002418void LocationsBuilderARM::VisitMul(HMul* mul) {
2419 LocationSummary* locations =
2420 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2421 switch (mul->GetResultType()) {
2422 case Primitive::kPrimInt:
2423 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002424 locations->SetInAt(0, Location::RequiresRegister());
2425 locations->SetInAt(1, Location::RequiresRegister());
2426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002427 break;
2428 }
2429
Calin Juravleb5bfa962014-10-21 18:02:24 +01002430 case Primitive::kPrimFloat:
2431 case Primitive::kPrimDouble: {
2432 locations->SetInAt(0, Location::RequiresFpuRegister());
2433 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002434 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002435 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002436 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002437
2438 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002439 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002440 }
2441}
2442
2443void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2444 LocationSummary* locations = mul->GetLocations();
2445 Location out = locations->Out();
2446 Location first = locations->InAt(0);
2447 Location second = locations->InAt(1);
2448 switch (mul->GetResultType()) {
2449 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002450 __ mul(out.AsRegister<Register>(),
2451 first.AsRegister<Register>(),
2452 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002453 break;
2454 }
2455 case Primitive::kPrimLong: {
2456 Register out_hi = out.AsRegisterPairHigh<Register>();
2457 Register out_lo = out.AsRegisterPairLow<Register>();
2458 Register in1_hi = first.AsRegisterPairHigh<Register>();
2459 Register in1_lo = first.AsRegisterPairLow<Register>();
2460 Register in2_hi = second.AsRegisterPairHigh<Register>();
2461 Register in2_lo = second.AsRegisterPairLow<Register>();
2462
2463 // Extra checks to protect caused by the existence of R1_R2.
2464 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2465 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2466 DCHECK_NE(out_hi, in1_lo);
2467 DCHECK_NE(out_hi, in2_lo);
2468
2469 // input: in1 - 64 bits, in2 - 64 bits
2470 // output: out
2471 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2472 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2473 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2474
2475 // IP <- in1.lo * in2.hi
2476 __ mul(IP, in1_lo, in2_hi);
2477 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2478 __ mla(out_hi, in1_hi, in2_lo, IP);
2479 // out.lo <- (in1.lo * in2.lo)[31:0];
2480 __ umull(out_lo, IP, in1_lo, in2_lo);
2481 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2482 __ add(out_hi, out_hi, ShifterOperand(IP));
2483 break;
2484 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002485
2486 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002487 __ vmuls(out.AsFpuRegister<SRegister>(),
2488 first.AsFpuRegister<SRegister>(),
2489 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002490 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002491 }
2492
2493 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002494 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2495 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2496 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002497 break;
2498 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002499
2500 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002501 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002502 }
2503}
2504
Zheng Xuc6667102015-05-15 16:08:45 +08002505void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2506 DCHECK(instruction->IsDiv() || instruction->IsRem());
2507 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2508
2509 LocationSummary* locations = instruction->GetLocations();
2510 Location second = locations->InAt(1);
2511 DCHECK(second.IsConstant());
2512
2513 Register out = locations->Out().AsRegister<Register>();
2514 Register dividend = locations->InAt(0).AsRegister<Register>();
2515 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2516 DCHECK(imm == 1 || imm == -1);
2517
2518 if (instruction->IsRem()) {
2519 __ LoadImmediate(out, 0);
2520 } else {
2521 if (imm == 1) {
2522 __ Mov(out, dividend);
2523 } else {
2524 __ rsb(out, dividend, ShifterOperand(0));
2525 }
2526 }
2527}
2528
2529void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2530 DCHECK(instruction->IsDiv() || instruction->IsRem());
2531 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2532
2533 LocationSummary* locations = instruction->GetLocations();
2534 Location second = locations->InAt(1);
2535 DCHECK(second.IsConstant());
2536
2537 Register out = locations->Out().AsRegister<Register>();
2538 Register dividend = locations->InAt(0).AsRegister<Register>();
2539 Register temp = locations->GetTemp(0).AsRegister<Register>();
2540 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002541 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002542 DCHECK(IsPowerOfTwo(abs_imm));
2543 int ctz_imm = CTZ(abs_imm);
2544
2545 if (ctz_imm == 1) {
2546 __ Lsr(temp, dividend, 32 - ctz_imm);
2547 } else {
2548 __ Asr(temp, dividend, 31);
2549 __ Lsr(temp, temp, 32 - ctz_imm);
2550 }
2551 __ add(out, temp, ShifterOperand(dividend));
2552
2553 if (instruction->IsDiv()) {
2554 __ Asr(out, out, ctz_imm);
2555 if (imm < 0) {
2556 __ rsb(out, out, ShifterOperand(0));
2557 }
2558 } else {
2559 __ ubfx(out, out, 0, ctz_imm);
2560 __ sub(out, out, ShifterOperand(temp));
2561 }
2562}
2563
2564void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2565 DCHECK(instruction->IsDiv() || instruction->IsRem());
2566 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2567
2568 LocationSummary* locations = instruction->GetLocations();
2569 Location second = locations->InAt(1);
2570 DCHECK(second.IsConstant());
2571
2572 Register out = locations->Out().AsRegister<Register>();
2573 Register dividend = locations->InAt(0).AsRegister<Register>();
2574 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2575 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2576 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2577
2578 int64_t magic;
2579 int shift;
2580 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2581
2582 __ LoadImmediate(temp1, magic);
2583 __ smull(temp2, temp1, dividend, temp1);
2584
2585 if (imm > 0 && magic < 0) {
2586 __ add(temp1, temp1, ShifterOperand(dividend));
2587 } else if (imm < 0 && magic > 0) {
2588 __ sub(temp1, temp1, ShifterOperand(dividend));
2589 }
2590
2591 if (shift != 0) {
2592 __ Asr(temp1, temp1, shift);
2593 }
2594
2595 if (instruction->IsDiv()) {
2596 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2597 } else {
2598 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2599 // TODO: Strength reduction for mls.
2600 __ LoadImmediate(temp2, imm);
2601 __ mls(out, temp1, temp2, dividend);
2602 }
2603}
2604
2605void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2606 DCHECK(instruction->IsDiv() || instruction->IsRem());
2607 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2608
2609 LocationSummary* locations = instruction->GetLocations();
2610 Location second = locations->InAt(1);
2611 DCHECK(second.IsConstant());
2612
2613 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2614 if (imm == 0) {
2615 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2616 } else if (imm == 1 || imm == -1) {
2617 DivRemOneOrMinusOne(instruction);
2618 } else if (IsPowerOfTwo(std::abs(imm))) {
2619 DivRemByPowerOfTwo(instruction);
2620 } else {
2621 DCHECK(imm <= -2 || imm >= 2);
2622 GenerateDivRemWithAnyConstant(instruction);
2623 }
2624}
2625
Calin Juravle7c4954d2014-10-28 16:57:40 +00002626void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002627 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2628 if (div->GetResultType() == Primitive::kPrimLong) {
2629 // pLdiv runtime call.
2630 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002631 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2632 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002633 } else if (div->GetResultType() == Primitive::kPrimInt &&
2634 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2635 // pIdivmod runtime call.
2636 call_kind = LocationSummary::kCall;
2637 }
2638
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2640
Calin Juravle7c4954d2014-10-28 16:57:40 +00002641 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002642 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002643 if (div->InputAt(1)->IsConstant()) {
2644 locations->SetInAt(0, Location::RequiresRegister());
2645 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2647 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2648 if (abs_imm <= 1) {
2649 // No temp register required.
2650 } else {
2651 locations->AddTemp(Location::RequiresRegister());
2652 if (!IsPowerOfTwo(abs_imm)) {
2653 locations->AddTemp(Location::RequiresRegister());
2654 }
2655 }
2656 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002657 locations->SetInAt(0, Location::RequiresRegister());
2658 locations->SetInAt(1, Location::RequiresRegister());
2659 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2660 } else {
2661 InvokeRuntimeCallingConvention calling_convention;
2662 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2663 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2664 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2665 // we only need the former.
2666 locations->SetOut(Location::RegisterLocation(R0));
2667 }
Calin Juravled0d48522014-11-04 16:40:20 +00002668 break;
2669 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002670 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002671 InvokeRuntimeCallingConvention calling_convention;
2672 locations->SetInAt(0, Location::RegisterPairLocation(
2673 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2674 locations->SetInAt(1, Location::RegisterPairLocation(
2675 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002676 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002677 break;
2678 }
2679 case Primitive::kPrimFloat:
2680 case Primitive::kPrimDouble: {
2681 locations->SetInAt(0, Location::RequiresFpuRegister());
2682 locations->SetInAt(1, Location::RequiresFpuRegister());
2683 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2684 break;
2685 }
2686
2687 default:
2688 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2689 }
2690}
2691
2692void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2693 LocationSummary* locations = div->GetLocations();
2694 Location out = locations->Out();
2695 Location first = locations->InAt(0);
2696 Location second = locations->InAt(1);
2697
2698 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002699 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002700 if (second.IsConstant()) {
2701 GenerateDivRemConstantIntegral(div);
2702 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002703 __ sdiv(out.AsRegister<Register>(),
2704 first.AsRegister<Register>(),
2705 second.AsRegister<Register>());
2706 } else {
2707 InvokeRuntimeCallingConvention calling_convention;
2708 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2709 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2710 DCHECK_EQ(R0, out.AsRegister<Register>());
2711
2712 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2713 }
Calin Juravled0d48522014-11-04 16:40:20 +00002714 break;
2715 }
2716
Calin Juravle7c4954d2014-10-28 16:57:40 +00002717 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002718 InvokeRuntimeCallingConvention calling_convention;
2719 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2720 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2721 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2722 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2723 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002724 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002725
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002726 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002727 break;
2728 }
2729
2730 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002731 __ vdivs(out.AsFpuRegister<SRegister>(),
2732 first.AsFpuRegister<SRegister>(),
2733 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002734 break;
2735 }
2736
2737 case Primitive::kPrimDouble: {
2738 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2739 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2740 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2741 break;
2742 }
2743
2744 default:
2745 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2746 }
2747}
2748
Calin Juravlebacfec32014-11-14 15:54:36 +00002749void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002750 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002751
2752 // Most remainders are implemented in the runtime.
2753 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002754 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2755 // sdiv will be replaced by other instruction sequence.
2756 call_kind = LocationSummary::kNoCall;
2757 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2758 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002759 // Have hardware divide instruction for int, do it with three instructions.
2760 call_kind = LocationSummary::kNoCall;
2761 }
2762
Calin Juravlebacfec32014-11-14 15:54:36 +00002763 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2764
Calin Juravled2ec87d2014-12-08 14:24:46 +00002765 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002766 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002767 if (rem->InputAt(1)->IsConstant()) {
2768 locations->SetInAt(0, Location::RequiresRegister());
2769 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2771 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2772 if (abs_imm <= 1) {
2773 // No temp register required.
2774 } else {
2775 locations->AddTemp(Location::RequiresRegister());
2776 if (!IsPowerOfTwo(abs_imm)) {
2777 locations->AddTemp(Location::RequiresRegister());
2778 }
2779 }
2780 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002781 locations->SetInAt(0, Location::RequiresRegister());
2782 locations->SetInAt(1, Location::RequiresRegister());
2783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2784 locations->AddTemp(Location::RequiresRegister());
2785 } else {
2786 InvokeRuntimeCallingConvention calling_convention;
2787 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2788 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2789 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2790 // we only need the latter.
2791 locations->SetOut(Location::RegisterLocation(R1));
2792 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002793 break;
2794 }
2795 case Primitive::kPrimLong: {
2796 InvokeRuntimeCallingConvention calling_convention;
2797 locations->SetInAt(0, Location::RegisterPairLocation(
2798 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2799 locations->SetInAt(1, Location::RegisterPairLocation(
2800 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2801 // The runtime helper puts the output in R2,R3.
2802 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2803 break;
2804 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002805 case Primitive::kPrimFloat: {
2806 InvokeRuntimeCallingConvention calling_convention;
2807 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2808 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2809 locations->SetOut(Location::FpuRegisterLocation(S0));
2810 break;
2811 }
2812
Calin Juravlebacfec32014-11-14 15:54:36 +00002813 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002814 InvokeRuntimeCallingConvention calling_convention;
2815 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2816 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2817 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2818 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2819 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002820 break;
2821 }
2822
2823 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002824 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002825 }
2826}
2827
2828void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2829 LocationSummary* locations = rem->GetLocations();
2830 Location out = locations->Out();
2831 Location first = locations->InAt(0);
2832 Location second = locations->InAt(1);
2833
Calin Juravled2ec87d2014-12-08 14:24:46 +00002834 Primitive::Type type = rem->GetResultType();
2835 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002836 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002837 if (second.IsConstant()) {
2838 GenerateDivRemConstantIntegral(rem);
2839 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002840 Register reg1 = first.AsRegister<Register>();
2841 Register reg2 = second.AsRegister<Register>();
2842 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002843
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002844 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002845 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002846 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002847 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002848 } else {
2849 InvokeRuntimeCallingConvention calling_convention;
2850 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2851 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2852 DCHECK_EQ(R1, out.AsRegister<Register>());
2853
2854 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2855 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002856 break;
2857 }
2858
2859 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002860 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002861 break;
2862 }
2863
Calin Juravled2ec87d2014-12-08 14:24:46 +00002864 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002865 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002866 break;
2867 }
2868
Calin Juravlebacfec32014-11-14 15:54:36 +00002869 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002870 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002871 break;
2872 }
2873
2874 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002875 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002876 }
2877}
2878
Calin Juravled0d48522014-11-04 16:40:20 +00002879void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002880 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2881 ? LocationSummary::kCallOnSlowPath
2882 : LocationSummary::kNoCall;
2883 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002884 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002885 if (instruction->HasUses()) {
2886 locations->SetOut(Location::SameAsFirstInput());
2887 }
2888}
2889
2890void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002891 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00002892 codegen_->AddSlowPath(slow_path);
2893
2894 LocationSummary* locations = instruction->GetLocations();
2895 Location value = locations->InAt(0);
2896
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002897 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002898 case Primitive::kPrimByte:
2899 case Primitive::kPrimChar:
2900 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002901 case Primitive::kPrimInt: {
2902 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002903 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002904 } else {
2905 DCHECK(value.IsConstant()) << value;
2906 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2907 __ b(slow_path->GetEntryLabel());
2908 }
2909 }
2910 break;
2911 }
2912 case Primitive::kPrimLong: {
2913 if (value.IsRegisterPair()) {
2914 __ orrs(IP,
2915 value.AsRegisterPairLow<Register>(),
2916 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2917 __ b(slow_path->GetEntryLabel(), EQ);
2918 } else {
2919 DCHECK(value.IsConstant()) << value;
2920 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2921 __ b(slow_path->GetEntryLabel());
2922 }
2923 }
2924 break;
2925 default:
2926 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2927 }
2928 }
Calin Juravled0d48522014-11-04 16:40:20 +00002929}
2930
Calin Juravle9aec02f2014-11-18 23:06:35 +00002931void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2932 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2933
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002934 LocationSummary* locations =
2935 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002936
2937 switch (op->GetResultType()) {
2938 case Primitive::kPrimInt: {
2939 locations->SetInAt(0, Location::RequiresRegister());
2940 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002941 // Make the output overlap, as it will be used to hold the masked
2942 // second input.
2943 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002944 break;
2945 }
2946 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::RequiresRegister());
2949 locations->AddTemp(Location::RequiresRegister());
2950 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00002951 break;
2952 }
2953 default:
2954 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2955 }
2956}
2957
2958void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
2959 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2960
2961 LocationSummary* locations = op->GetLocations();
2962 Location out = locations->Out();
2963 Location first = locations->InAt(0);
2964 Location second = locations->InAt(1);
2965
2966 Primitive::Type type = op->GetResultType();
2967 switch (type) {
2968 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 Register out_reg = out.AsRegister<Register>();
2970 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002971 // Arm doesn't mask the shift count so we need to do it ourselves.
2972 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002973 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002974 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00002975 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002976 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002977 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002978 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002979 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01002980 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002981 }
2982 } else {
2983 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
2984 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
2985 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
2986 __ Mov(out_reg, first_reg);
2987 } else if (op->IsShl()) {
2988 __ Lsl(out_reg, first_reg, shift_value);
2989 } else if (op->IsShr()) {
2990 __ Asr(out_reg, first_reg, shift_value);
2991 } else {
2992 __ Lsr(out_reg, first_reg, shift_value);
2993 }
2994 }
2995 break;
2996 }
2997 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002998 Register o_h = out.AsRegisterPairHigh<Register>();
2999 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003000
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003001 Register temp = locations->GetTemp(0).AsRegister<Register>();
3002
3003 Register high = first.AsRegisterPairHigh<Register>();
3004 Register low = first.AsRegisterPairLow<Register>();
3005
3006 Register second_reg = second.AsRegister<Register>();
3007
Calin Juravle9aec02f2014-11-18 23:06:35 +00003008 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003009 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003010 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003011 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003012 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003013 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003014 __ Lsr(temp, low, temp);
3015 __ orr(o_h, o_h, ShifterOperand(temp));
3016 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003017 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003018 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003019 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003020 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003021 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003022 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003023 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003024 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003025 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003026 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003027 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003028 __ Lsl(temp, high, temp);
3029 __ orr(o_l, o_l, ShifterOperand(temp));
3030 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003031 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003032 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003033 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003034 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003035 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003036 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003037 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003038 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003039 __ Lsr(o_l, low, o_h);
3040 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003041 __ Lsl(temp, high, temp);
3042 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003043 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003044 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003045 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003046 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003047 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003048 break;
3049 }
3050 default:
3051 LOG(FATAL) << "Unexpected operation type " << type;
3052 }
3053}
3054
3055void LocationsBuilderARM::VisitShl(HShl* shl) {
3056 HandleShift(shl);
3057}
3058
3059void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3060 HandleShift(shl);
3061}
3062
3063void LocationsBuilderARM::VisitShr(HShr* shr) {
3064 HandleShift(shr);
3065}
3066
3067void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3068 HandleShift(shr);
3069}
3070
3071void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3072 HandleShift(ushr);
3073}
3074
3075void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3076 HandleShift(ushr);
3077}
3078
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003079void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003080 LocationSummary* locations =
3081 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003082 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003083 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003084 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003085 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003086}
3087
3088void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3089 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003090 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003091 // Note: if heap poisoning is enabled, the entry point takes cares
3092 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003093 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003094 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003095 instruction->GetDexPc(),
3096 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003097}
3098
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003099void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3100 LocationSummary* locations =
3101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3102 InvokeRuntimeCallingConvention calling_convention;
3103 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003104 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003105 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003106 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003107}
3108
3109void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3110 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003111 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003112 // Note: if heap poisoning is enabled, the entry point takes cares
3113 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003114 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003115 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003116 instruction->GetDexPc(),
3117 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003118}
3119
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003120void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003121 LocationSummary* locations =
3122 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003123 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3124 if (location.IsStackSlot()) {
3125 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3126 } else if (location.IsDoubleStackSlot()) {
3127 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003128 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003129 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003130}
3131
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003132void InstructionCodeGeneratorARM::VisitParameterValue(
3133 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003134 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003135}
3136
3137void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3138 LocationSummary* locations =
3139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3140 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3141}
3142
3143void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3144 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003145}
3146
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003147void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003148 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003149 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003150 locations->SetInAt(0, Location::RequiresRegister());
3151 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003152}
3153
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003154void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3155 LocationSummary* locations = not_->GetLocations();
3156 Location out = locations->Out();
3157 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003158 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003159 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003160 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003161 break;
3162
3163 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003164 __ mvn(out.AsRegisterPairLow<Register>(),
3165 ShifterOperand(in.AsRegisterPairLow<Register>()));
3166 __ mvn(out.AsRegisterPairHigh<Register>(),
3167 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003168 break;
3169
3170 default:
3171 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3172 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003173}
3174
David Brazdil66d126e2015-04-03 16:02:44 +01003175void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3176 LocationSummary* locations =
3177 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3178 locations->SetInAt(0, Location::RequiresRegister());
3179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3180}
3181
3182void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003183 LocationSummary* locations = bool_not->GetLocations();
3184 Location out = locations->Out();
3185 Location in = locations->InAt(0);
3186 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3187}
3188
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003189void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003190 LocationSummary* locations =
3191 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003192 switch (compare->InputAt(0)->GetType()) {
3193 case Primitive::kPrimLong: {
3194 locations->SetInAt(0, Location::RequiresRegister());
3195 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003196 // Output overlaps because it is written before doing the low comparison.
3197 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003198 break;
3199 }
3200 case Primitive::kPrimFloat:
3201 case Primitive::kPrimDouble: {
3202 locations->SetInAt(0, Location::RequiresFpuRegister());
3203 locations->SetInAt(1, Location::RequiresFpuRegister());
3204 locations->SetOut(Location::RequiresRegister());
3205 break;
3206 }
3207 default:
3208 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3209 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003210}
3211
3212void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003213 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003215 Location left = locations->InAt(0);
3216 Location right = locations->InAt(1);
3217
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003218 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003219 Primitive::Type type = compare->InputAt(0)->GetType();
3220 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003221 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003222 __ cmp(left.AsRegisterPairHigh<Register>(),
3223 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003224 __ b(&less, LT);
3225 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003226 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003227 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003228 __ cmp(left.AsRegisterPairLow<Register>(),
3229 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003230 break;
3231 }
3232 case Primitive::kPrimFloat:
3233 case Primitive::kPrimDouble: {
3234 __ LoadImmediate(out, 0);
3235 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003236 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003237 } else {
3238 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3239 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3240 }
3241 __ vmstat(); // transfer FP status register to ARM APSR.
3242 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003243 break;
3244 }
3245 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003246 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003247 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003248 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003249 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003250
3251 __ Bind(&greater);
3252 __ LoadImmediate(out, 1);
3253 __ b(&done);
3254
3255 __ Bind(&less);
3256 __ LoadImmediate(out, -1);
3257
3258 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003259}
3260
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003261void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003262 LocationSummary* locations =
3263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003264 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3265 locations->SetInAt(i, Location::Any());
3266 }
3267 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003268}
3269
3270void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003271 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003272 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003273}
3274
Calin Juravle52c48962014-12-16 17:02:57 +00003275void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3276 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003277 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003278 switch (kind) {
3279 case MemBarrierKind::kAnyStore:
3280 case MemBarrierKind::kLoadAny:
3281 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003282 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003283 break;
3284 }
3285 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003286 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003287 break;
3288 }
3289 default:
3290 LOG(FATAL) << "Unexpected memory barrier " << kind;
3291 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003292 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003293}
3294
3295void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3296 uint32_t offset,
3297 Register out_lo,
3298 Register out_hi) {
3299 if (offset != 0) {
3300 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003301 __ add(IP, addr, ShifterOperand(out_lo));
3302 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003303 }
3304 __ ldrexd(out_lo, out_hi, addr);
3305}
3306
3307void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3308 uint32_t offset,
3309 Register value_lo,
3310 Register value_hi,
3311 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003312 Register temp2,
3313 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003314 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003315 if (offset != 0) {
3316 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003317 __ add(IP, addr, ShifterOperand(temp1));
3318 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003319 }
3320 __ Bind(&fail);
3321 // We need a load followed by store. (The address used in a STREX instruction must
3322 // be the same as the address in the most recently executed LDREX instruction.)
3323 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003324 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003325 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003326 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003327}
3328
3329void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3330 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3331
Nicolas Geoffray39468442014-09-02 15:17:15 +01003332 LocationSummary* locations =
3333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003334 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003335
Calin Juravle52c48962014-12-16 17:02:57 +00003336 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003337 if (Primitive::IsFloatingPointType(field_type)) {
3338 locations->SetInAt(1, Location::RequiresFpuRegister());
3339 } else {
3340 locations->SetInAt(1, Location::RequiresRegister());
3341 }
3342
Calin Juravle52c48962014-12-16 17:02:57 +00003343 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003344 bool generate_volatile = field_info.IsVolatile()
3345 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003346 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003347 bool needs_write_barrier =
3348 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003349 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003350 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003351 if (needs_write_barrier) {
3352 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003353 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003354 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003355 // Arm encoding have some additional constraints for ldrexd/strexd:
3356 // - registers need to be consecutive
3357 // - the first register should be even but not R14.
3358 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3359 // enable Arm encoding.
3360 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3361
3362 locations->AddTemp(Location::RequiresRegister());
3363 locations->AddTemp(Location::RequiresRegister());
3364 if (field_type == Primitive::kPrimDouble) {
3365 // For doubles we need two more registers to copy the value.
3366 locations->AddTemp(Location::RegisterLocation(R2));
3367 locations->AddTemp(Location::RegisterLocation(R3));
3368 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003369 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003370}
3371
Calin Juravle52c48962014-12-16 17:02:57 +00003372void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003373 const FieldInfo& field_info,
3374 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003375 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3376
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003377 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003378 Register base = locations->InAt(0).AsRegister<Register>();
3379 Location value = locations->InAt(1);
3380
3381 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003382 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003383 Primitive::Type field_type = field_info.GetFieldType();
3384 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003385 bool needs_write_barrier =
3386 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003387
3388 if (is_volatile) {
3389 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3390 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003391
3392 switch (field_type) {
3393 case Primitive::kPrimBoolean:
3394 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003395 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003396 break;
3397 }
3398
3399 case Primitive::kPrimShort:
3400 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003401 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003402 break;
3403 }
3404
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003406 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003407 if (kPoisonHeapReferences && needs_write_barrier) {
3408 // Note that in the case where `value` is a null reference,
3409 // we do not enter this block, as a null reference does not
3410 // need poisoning.
3411 DCHECK_EQ(field_type, Primitive::kPrimNot);
3412 Register temp = locations->GetTemp(0).AsRegister<Register>();
3413 __ Mov(temp, value.AsRegister<Register>());
3414 __ PoisonHeapReference(temp);
3415 __ StoreToOffset(kStoreWord, temp, base, offset);
3416 } else {
3417 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3418 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003419 break;
3420 }
3421
3422 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003423 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003424 GenerateWideAtomicStore(base, offset,
3425 value.AsRegisterPairLow<Register>(),
3426 value.AsRegisterPairHigh<Register>(),
3427 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003428 locations->GetTemp(1).AsRegister<Register>(),
3429 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003430 } else {
3431 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003432 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003433 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003434 break;
3435 }
3436
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003437 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003438 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003439 break;
3440 }
3441
3442 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003443 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003444 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003445 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3446 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3447
3448 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3449
3450 GenerateWideAtomicStore(base, offset,
3451 value_reg_lo,
3452 value_reg_hi,
3453 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003454 locations->GetTemp(3).AsRegister<Register>(),
3455 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003456 } else {
3457 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003458 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003459 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003460 break;
3461 }
3462
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003463 case Primitive::kPrimVoid:
3464 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003465 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003466 }
Calin Juravle52c48962014-12-16 17:02:57 +00003467
Calin Juravle77520bc2015-01-12 18:45:46 +00003468 // Longs and doubles are handled in the switch.
3469 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3470 codegen_->MaybeRecordImplicitNullCheck(instruction);
3471 }
3472
3473 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3474 Register temp = locations->GetTemp(0).AsRegister<Register>();
3475 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003476 codegen_->MarkGCCard(
3477 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003478 }
3479
Calin Juravle52c48962014-12-16 17:02:57 +00003480 if (is_volatile) {
3481 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3482 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003483}
3484
Calin Juravle52c48962014-12-16 17:02:57 +00003485void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3486 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003487 LocationSummary* locations =
3488 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003489 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003490
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003491 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003492 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003493 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003494 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003495
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003496 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3497 locations->SetOut(Location::RequiresFpuRegister());
3498 } else {
3499 locations->SetOut(Location::RequiresRegister(),
3500 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3501 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003502 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003503 // Arm encoding have some additional constraints for ldrexd/strexd:
3504 // - registers need to be consecutive
3505 // - the first register should be even but not R14.
3506 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3507 // enable Arm encoding.
3508 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3509 locations->AddTemp(Location::RequiresRegister());
3510 locations->AddTemp(Location::RequiresRegister());
3511 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003512}
3513
Calin Juravle52c48962014-12-16 17:02:57 +00003514void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3515 const FieldInfo& field_info) {
3516 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003517
Calin Juravle52c48962014-12-16 17:02:57 +00003518 LocationSummary* locations = instruction->GetLocations();
3519 Register base = locations->InAt(0).AsRegister<Register>();
3520 Location out = locations->Out();
3521 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003522 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003523 Primitive::Type field_type = field_info.GetFieldType();
3524 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3525
3526 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003527 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003528 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003529 break;
3530 }
3531
3532 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003533 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003534 break;
3535 }
3536
3537 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003538 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003539 break;
3540 }
3541
3542 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003543 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544 break;
3545 }
3546
3547 case Primitive::kPrimInt:
3548 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003549 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003550 break;
3551 }
3552
3553 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003554 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003555 GenerateWideAtomicLoad(base, offset,
3556 out.AsRegisterPairLow<Register>(),
3557 out.AsRegisterPairHigh<Register>());
3558 } else {
3559 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3560 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003561 break;
3562 }
3563
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003564 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003565 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003566 break;
3567 }
3568
3569 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003570 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003571 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003572 Register lo = locations->GetTemp(0).AsRegister<Register>();
3573 Register hi = locations->GetTemp(1).AsRegister<Register>();
3574 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003575 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003576 __ vmovdrr(out_reg, lo, hi);
3577 } else {
3578 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003579 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003580 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003581 break;
3582 }
3583
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003584 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003585 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003586 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003587 }
Calin Juravle52c48962014-12-16 17:02:57 +00003588
Calin Juravle77520bc2015-01-12 18:45:46 +00003589 // Doubles are handled in the switch.
3590 if (field_type != Primitive::kPrimDouble) {
3591 codegen_->MaybeRecordImplicitNullCheck(instruction);
3592 }
3593
Calin Juravle52c48962014-12-16 17:02:57 +00003594 if (is_volatile) {
3595 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3596 }
Roland Levillain4d027112015-07-01 15:41:14 +01003597
3598 if (field_type == Primitive::kPrimNot) {
3599 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3600 }
Calin Juravle52c48962014-12-16 17:02:57 +00003601}
3602
3603void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3604 HandleFieldSet(instruction, instruction->GetFieldInfo());
3605}
3606
3607void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003608 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003609}
3610
3611void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3612 HandleFieldGet(instruction, instruction->GetFieldInfo());
3613}
3614
3615void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3616 HandleFieldGet(instruction, instruction->GetFieldInfo());
3617}
3618
3619void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3620 HandleFieldGet(instruction, instruction->GetFieldInfo());
3621}
3622
3623void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3624 HandleFieldGet(instruction, instruction->GetFieldInfo());
3625}
3626
3627void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3628 HandleFieldSet(instruction, instruction->GetFieldInfo());
3629}
3630
3631void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003632 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003633}
3634
Calin Juravlee460d1d2015-09-29 04:52:17 +01003635void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
3636 HUnresolvedInstanceFieldGet* instruction) {
3637 FieldAccessCallingConventionARM calling_convention;
3638 codegen_->CreateUnresolvedFieldLocationSummary(
3639 instruction, instruction->GetFieldType(), calling_convention);
3640}
3641
3642void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
3643 HUnresolvedInstanceFieldGet* instruction) {
3644 FieldAccessCallingConventionARM calling_convention;
3645 codegen_->GenerateUnresolvedFieldAccess(instruction,
3646 instruction->GetFieldType(),
3647 instruction->GetFieldIndex(),
3648 instruction->GetDexPc(),
3649 calling_convention);
3650}
3651
3652void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
3653 HUnresolvedInstanceFieldSet* instruction) {
3654 FieldAccessCallingConventionARM calling_convention;
3655 codegen_->CreateUnresolvedFieldLocationSummary(
3656 instruction, instruction->GetFieldType(), calling_convention);
3657}
3658
3659void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
3660 HUnresolvedInstanceFieldSet* instruction) {
3661 FieldAccessCallingConventionARM calling_convention;
3662 codegen_->GenerateUnresolvedFieldAccess(instruction,
3663 instruction->GetFieldType(),
3664 instruction->GetFieldIndex(),
3665 instruction->GetDexPc(),
3666 calling_convention);
3667}
3668
3669void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
3670 HUnresolvedStaticFieldGet* instruction) {
3671 FieldAccessCallingConventionARM calling_convention;
3672 codegen_->CreateUnresolvedFieldLocationSummary(
3673 instruction, instruction->GetFieldType(), calling_convention);
3674}
3675
3676void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
3677 HUnresolvedStaticFieldGet* instruction) {
3678 FieldAccessCallingConventionARM calling_convention;
3679 codegen_->GenerateUnresolvedFieldAccess(instruction,
3680 instruction->GetFieldType(),
3681 instruction->GetFieldIndex(),
3682 instruction->GetDexPc(),
3683 calling_convention);
3684}
3685
3686void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
3687 HUnresolvedStaticFieldSet* instruction) {
3688 FieldAccessCallingConventionARM calling_convention;
3689 codegen_->CreateUnresolvedFieldLocationSummary(
3690 instruction, instruction->GetFieldType(), calling_convention);
3691}
3692
3693void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
3694 HUnresolvedStaticFieldSet* instruction) {
3695 FieldAccessCallingConventionARM calling_convention;
3696 codegen_->GenerateUnresolvedFieldAccess(instruction,
3697 instruction->GetFieldType(),
3698 instruction->GetFieldIndex(),
3699 instruction->GetDexPc(),
3700 calling_convention);
3701}
3702
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003703void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003704 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3705 ? LocationSummary::kCallOnSlowPath
3706 : LocationSummary::kNoCall;
3707 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003708 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003709 if (instruction->HasUses()) {
3710 locations->SetOut(Location::SameAsFirstInput());
3711 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003712}
3713
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003714void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003715 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3716 return;
3717 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003718 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003719
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003720 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3721 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3722}
3723
3724void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003725 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003726 codegen_->AddSlowPath(slow_path);
3727
3728 LocationSummary* locations = instruction->GetLocations();
3729 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003730
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003731 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003732}
3733
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003734void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003735 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003736 GenerateImplicitNullCheck(instruction);
3737 } else {
3738 GenerateExplicitNullCheck(instruction);
3739 }
3740}
3741
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003742void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003743 LocationSummary* locations =
3744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003745 locations->SetInAt(0, Location::RequiresRegister());
3746 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003747 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3748 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3749 } else {
3750 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3751 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003752}
3753
3754void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3755 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003757 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003758 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003759
Roland Levillain4d027112015-07-01 15:41:14 +01003760 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003761 case Primitive::kPrimBoolean: {
3762 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003763 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003764 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003765 size_t offset =
3766 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003767 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3768 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003769 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003770 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3771 }
3772 break;
3773 }
3774
3775 case Primitive::kPrimByte: {
3776 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003777 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003778 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003779 size_t offset =
3780 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003781 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3782 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003784 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3785 }
3786 break;
3787 }
3788
3789 case Primitive::kPrimShort: {
3790 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003791 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003792 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003793 size_t offset =
3794 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003795 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3796 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003797 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003798 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3799 }
3800 break;
3801 }
3802
3803 case Primitive::kPrimChar: {
3804 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003805 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003806 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003807 size_t offset =
3808 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003809 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3810 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003812 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3813 }
3814 break;
3815 }
3816
3817 case Primitive::kPrimInt:
3818 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003819 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3820 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003821 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003822 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003823 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003824 size_t offset =
3825 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003826 __ LoadFromOffset(kLoadWord, out, obj, offset);
3827 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003829 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3830 }
3831 break;
3832 }
3833
3834 case Primitive::kPrimLong: {
3835 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003836 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003837 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003838 size_t offset =
3839 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003840 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003841 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003842 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003843 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003844 }
3845 break;
3846 }
3847
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003848 case Primitive::kPrimFloat: {
3849 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3850 Location out = locations->Out();
3851 DCHECK(out.IsFpuRegister());
3852 if (index.IsConstant()) {
3853 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3854 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3855 } else {
3856 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3857 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3858 }
3859 break;
3860 }
3861
3862 case Primitive::kPrimDouble: {
3863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3864 Location out = locations->Out();
3865 DCHECK(out.IsFpuRegisterPair());
3866 if (index.IsConstant()) {
3867 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3868 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3869 } else {
3870 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3871 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3872 }
3873 break;
3874 }
3875
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003876 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003877 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003878 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003879 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003880 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003881
3882 if (type == Primitive::kPrimNot) {
3883 Register out = locations->Out().AsRegister<Register>();
3884 __ MaybeUnpoisonHeapReference(out);
3885 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003886}
3887
3888void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003889 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003890
3891 bool needs_write_barrier =
3892 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003893 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003894
Nicolas Geoffray39468442014-09-02 15:17:15 +01003895 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003896 instruction,
3897 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
3898 locations->SetInAt(0, Location::RequiresRegister());
3899 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3900 if (Primitive::IsFloatingPointType(value_type)) {
3901 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003902 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003903 locations->SetInAt(2, Location::RequiresRegister());
3904 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003905
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003906 if (needs_write_barrier) {
3907 // Temporary registers for the write barrier.
3908 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
3909 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003910 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911}
3912
3913void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
3914 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003915 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003916 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003917 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003918 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003919 bool needs_write_barrier =
3920 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003921
3922 switch (value_type) {
3923 case Primitive::kPrimBoolean:
3924 case Primitive::kPrimByte: {
3925 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003926 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003927 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003928 size_t offset =
3929 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003930 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003932 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003933 __ StoreToOffset(kStoreByte, value, IP, data_offset);
3934 }
3935 break;
3936 }
3937
3938 case Primitive::kPrimShort:
3939 case Primitive::kPrimChar: {
3940 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003941 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003942 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003943 size_t offset =
3944 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003945 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003946 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003947 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003948 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
3949 }
3950 break;
3951 }
3952
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003953 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003954 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3955 Register value = locations->InAt(2).AsRegister<Register>();
3956 Register source = value;
3957
3958 if (instruction->InputAt(2)->IsNullConstant()) {
3959 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003960 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003961 size_t offset =
3962 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003963 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003964 } else {
3965 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003966 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01003967 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003968 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003969 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003970 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003971
3972 DCHECK(needs_write_barrier);
3973 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3974 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3975 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3976 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3977 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3978 Label done;
3979 SlowPathCode* slow_path = nullptr;
3980
3981 if (may_need_runtime_call) {
3982 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
3983 codegen_->AddSlowPath(slow_path);
3984 if (instruction->GetValueCanBeNull()) {
3985 Label non_zero;
3986 __ CompareAndBranchIfNonZero(value, &non_zero);
3987 if (index.IsConstant()) {
3988 size_t offset =
3989 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3990 __ StoreToOffset(kStoreWord, value, array, offset);
3991 } else {
3992 DCHECK(index.IsRegister()) << index;
3993 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3994 __ StoreToOffset(kStoreWord, value, IP, data_offset);
3995 }
3996 codegen_->MaybeRecordImplicitNullCheck(instruction);
3997 __ b(&done);
3998 __ Bind(&non_zero);
3999 }
4000
4001 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4002 codegen_->MaybeRecordImplicitNullCheck(instruction);
4003 __ MaybeUnpoisonHeapReference(temp1);
4004 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4005 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4006 // No need to poison/unpoison, we're comparing two poisoined references.
4007 __ cmp(temp1, ShifterOperand(temp2));
4008 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4009 Label do_put;
4010 __ b(&do_put, EQ);
4011 __ MaybeUnpoisonHeapReference(temp1);
4012 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4013 // No need to poison/unpoison, we're comparing against null.
4014 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4015 __ Bind(&do_put);
4016 } else {
4017 __ b(slow_path->GetEntryLabel(), NE);
4018 }
4019 }
4020
4021 if (kPoisonHeapReferences) {
4022 // Note that in the case where `value` is a null reference,
4023 // we do not enter this block, as a null reference does not
4024 // need poisoning.
4025 DCHECK_EQ(value_type, Primitive::kPrimNot);
4026 __ Mov(temp1, value);
4027 __ PoisonHeapReference(temp1);
4028 source = temp1;
4029 }
4030
4031 if (index.IsConstant()) {
4032 size_t offset =
4033 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4034 __ StoreToOffset(kStoreWord, source, array, offset);
4035 } else {
4036 DCHECK(index.IsRegister()) << index;
4037 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4038 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4039 }
4040
4041 if (!may_need_runtime_call) {
4042 codegen_->MaybeRecordImplicitNullCheck(instruction);
4043 }
4044
4045 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4046
4047 if (done.IsLinked()) {
4048 __ Bind(&done);
4049 }
4050
4051 if (slow_path != nullptr) {
4052 __ Bind(slow_path->GetExitLabel());
4053 }
4054
4055 break;
4056 }
4057
4058 case Primitive::kPrimInt: {
4059 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4060 Register value = locations->InAt(2).AsRegister<Register>();
4061 if (index.IsConstant()) {
4062 size_t offset =
4063 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4064 __ StoreToOffset(kStoreWord, value, array, offset);
4065 } else {
4066 DCHECK(index.IsRegister()) << index;
4067 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4068 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4069 }
4070
4071 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004072 break;
4073 }
4074
4075 case Primitive::kPrimLong: {
4076 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004077 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004078 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004079 size_t offset =
4080 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004081 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004082 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004083 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004084 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004085 }
4086 break;
4087 }
4088
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004089 case Primitive::kPrimFloat: {
4090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4091 Location value = locations->InAt(2);
4092 DCHECK(value.IsFpuRegister());
4093 if (index.IsConstant()) {
4094 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004095 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004096 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004097 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004098 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4099 }
4100 break;
4101 }
4102
4103 case Primitive::kPrimDouble: {
4104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4105 Location value = locations->InAt(2);
4106 DCHECK(value.IsFpuRegisterPair());
4107 if (index.IsConstant()) {
4108 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004109 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004110 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004111 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004112 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4113 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004114
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004115 break;
4116 }
4117
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004118 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004119 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004120 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004121 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004122
4123 // Ints and objects are handled in the switch.
4124 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4125 codegen_->MaybeRecordImplicitNullCheck(instruction);
4126 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004127}
4128
4129void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004130 LocationSummary* locations =
4131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004132 locations->SetInAt(0, Location::RequiresRegister());
4133 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004134}
4135
4136void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4137 LocationSummary* locations = instruction->GetLocations();
4138 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004139 Register obj = locations->InAt(0).AsRegister<Register>();
4140 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004141 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004142 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004143}
4144
4145void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004146 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4147 ? LocationSummary::kCallOnSlowPath
4148 : LocationSummary::kNoCall;
4149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004150 locations->SetInAt(0, Location::RequiresRegister());
4151 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004152 if (instruction->HasUses()) {
4153 locations->SetOut(Location::SameAsFirstInput());
4154 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004155}
4156
4157void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4158 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004159 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004160 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004161 codegen_->AddSlowPath(slow_path);
4162
Roland Levillain271ab9c2014-11-27 15:23:57 +00004163 Register index = locations->InAt(0).AsRegister<Register>();
4164 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004165
4166 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004167 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004168}
4169
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004170void CodeGeneratorARM::MarkGCCard(Register temp,
4171 Register card,
4172 Register object,
4173 Register value,
4174 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004175 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004176 if (can_be_null) {
4177 __ CompareAndBranchIfZero(value, &is_null);
4178 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004179 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4180 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4181 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004182 if (can_be_null) {
4183 __ Bind(&is_null);
4184 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004185}
4186
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004187void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4188 temp->SetLocations(nullptr);
4189}
4190
4191void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
4192 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004193 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004194}
4195
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004196void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004197 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004198 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004199}
4200
4201void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004202 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4203}
4204
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004205void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4207}
4208
4209void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004210 HBasicBlock* block = instruction->GetBlock();
4211 if (block->GetLoopInformation() != nullptr) {
4212 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4213 // The back edge will generate the suspend check.
4214 return;
4215 }
4216 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4217 // The goto will generate the suspend check.
4218 return;
4219 }
4220 GenerateSuspendCheck(instruction, nullptr);
4221}
4222
4223void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4224 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004225 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004226 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4227 if (slow_path == nullptr) {
4228 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4229 instruction->SetSlowPath(slow_path);
4230 codegen_->AddSlowPath(slow_path);
4231 if (successor != nullptr) {
4232 DCHECK(successor->IsLoopHeader());
4233 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4234 }
4235 } else {
4236 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4237 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004238
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004239 __ LoadFromOffset(
4240 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004241 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004242 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004243 __ Bind(slow_path->GetReturnLabel());
4244 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004245 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004246 __ b(slow_path->GetEntryLabel());
4247 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004248}
4249
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004250ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4251 return codegen_->GetAssembler();
4252}
4253
4254void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004255 DCHECK_LT(index, moves_.size());
4256 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004257 Location source = move->GetSource();
4258 Location destination = move->GetDestination();
4259
4260 if (source.IsRegister()) {
4261 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004262 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004263 } else {
4264 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004265 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004266 SP, destination.GetStackIndex());
4267 }
4268 } else if (source.IsStackSlot()) {
4269 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004270 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004271 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004272 } else if (destination.IsFpuRegister()) {
4273 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004274 } else {
4275 DCHECK(destination.IsStackSlot());
4276 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4277 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4278 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004279 } else if (source.IsFpuRegister()) {
4280 if (destination.IsFpuRegister()) {
4281 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004282 } else {
4283 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004284 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4285 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004286 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004287 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004288 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4289 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004290 } else if (destination.IsRegisterPair()) {
4291 DCHECK(ExpectedPairLayout(destination));
4292 __ LoadFromOffset(
4293 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4294 } else {
4295 DCHECK(destination.IsFpuRegisterPair()) << destination;
4296 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4297 SP,
4298 source.GetStackIndex());
4299 }
4300 } else if (source.IsRegisterPair()) {
4301 if (destination.IsRegisterPair()) {
4302 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4303 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4304 } else {
4305 DCHECK(destination.IsDoubleStackSlot()) << destination;
4306 DCHECK(ExpectedPairLayout(source));
4307 __ StoreToOffset(
4308 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4309 }
4310 } else if (source.IsFpuRegisterPair()) {
4311 if (destination.IsFpuRegisterPair()) {
4312 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4313 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4314 } else {
4315 DCHECK(destination.IsDoubleStackSlot()) << destination;
4316 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4317 SP,
4318 destination.GetStackIndex());
4319 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004320 } else {
4321 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004322 HConstant* constant = source.GetConstant();
4323 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4324 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004325 if (destination.IsRegister()) {
4326 __ LoadImmediate(destination.AsRegister<Register>(), value);
4327 } else {
4328 DCHECK(destination.IsStackSlot());
4329 __ LoadImmediate(IP, value);
4330 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4331 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004332 } else if (constant->IsLongConstant()) {
4333 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004334 if (destination.IsRegisterPair()) {
4335 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4336 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004337 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004338 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004339 __ LoadImmediate(IP, Low32Bits(value));
4340 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4341 __ LoadImmediate(IP, High32Bits(value));
4342 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4343 }
4344 } else if (constant->IsDoubleConstant()) {
4345 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004346 if (destination.IsFpuRegisterPair()) {
4347 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004348 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004349 DCHECK(destination.IsDoubleStackSlot()) << destination;
4350 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004351 __ LoadImmediate(IP, Low32Bits(int_value));
4352 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4353 __ LoadImmediate(IP, High32Bits(int_value));
4354 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4355 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004356 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004357 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004358 float value = constant->AsFloatConstant()->GetValue();
4359 if (destination.IsFpuRegister()) {
4360 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4361 } else {
4362 DCHECK(destination.IsStackSlot());
4363 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4364 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4365 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004366 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004367 }
4368}
4369
4370void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4371 __ Mov(IP, reg);
4372 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4373 __ StoreToOffset(kStoreWord, IP, SP, mem);
4374}
4375
4376void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4377 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4378 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4379 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4380 SP, mem1 + stack_offset);
4381 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4382 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4383 SP, mem2 + stack_offset);
4384 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4385}
4386
4387void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004388 DCHECK_LT(index, moves_.size());
4389 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004390 Location source = move->GetSource();
4391 Location destination = move->GetDestination();
4392
4393 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004394 DCHECK_NE(source.AsRegister<Register>(), IP);
4395 DCHECK_NE(destination.AsRegister<Register>(), IP);
4396 __ Mov(IP, source.AsRegister<Register>());
4397 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4398 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004399 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004400 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004401 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004402 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004403 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4404 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004405 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004406 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004407 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004408 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004409 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004410 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004411 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004412 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004413 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4414 destination.AsRegisterPairHigh<Register>(),
4415 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004416 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004417 Register low_reg = source.IsRegisterPair()
4418 ? source.AsRegisterPairLow<Register>()
4419 : destination.AsRegisterPairLow<Register>();
4420 int mem = source.IsRegisterPair()
4421 ? destination.GetStackIndex()
4422 : source.GetStackIndex();
4423 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004424 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004425 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004426 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004427 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004428 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4429 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004430 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004431 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004432 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004433 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4434 DRegister reg = source.IsFpuRegisterPair()
4435 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4436 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4437 int mem = source.IsFpuRegisterPair()
4438 ? destination.GetStackIndex()
4439 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004440 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004441 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004442 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004443 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4444 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4445 : destination.AsFpuRegister<SRegister>();
4446 int mem = source.IsFpuRegister()
4447 ? destination.GetStackIndex()
4448 : source.GetStackIndex();
4449
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004450 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004451 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004452 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004453 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004454 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4455 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004456 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004457 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004458 }
4459}
4460
4461void ParallelMoveResolverARM::SpillScratch(int reg) {
4462 __ Push(static_cast<Register>(reg));
4463}
4464
4465void ParallelMoveResolverARM::RestoreScratch(int reg) {
4466 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004467}
4468
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004469void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004470 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4471 ? LocationSummary::kCallOnSlowPath
4472 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004473 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004474 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004475 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004476 locations->SetOut(Location::RequiresRegister());
4477}
4478
4479void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004480 LocationSummary* locations = cls->GetLocations();
4481 Register out = locations->Out().AsRegister<Register>();
4482 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004483 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004484 DCHECK(!cls->CanCallRuntime());
4485 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004486 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004487 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004488 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004489 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004490 __ LoadFromOffset(kLoadWord,
4491 out,
4492 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004493 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004494 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004495 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004496
Andreas Gampe85b62f22015-09-09 13:15:38 -07004497 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004498 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4499 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004500 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004501 if (cls->MustGenerateClinitCheck()) {
4502 GenerateClassInitializationCheck(slow_path, out);
4503 } else {
4504 __ Bind(slow_path->GetExitLabel());
4505 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004506 }
4507}
4508
4509void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4510 LocationSummary* locations =
4511 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4512 locations->SetInAt(0, Location::RequiresRegister());
4513 if (check->HasUses()) {
4514 locations->SetOut(Location::SameAsFirstInput());
4515 }
4516}
4517
4518void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004519 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004520 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004521 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004522 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004523 GenerateClassInitializationCheck(slow_path,
4524 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004525}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004526
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004527void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004528 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004529 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4530 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4531 __ b(slow_path->GetEntryLabel(), LT);
4532 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4533 // properly. Therefore, we do a memory fence.
4534 __ dmb(ISH);
4535 __ Bind(slow_path->GetExitLabel());
4536}
4537
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004538void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4539 LocationSummary* locations =
4540 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004541 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004542 locations->SetOut(Location::RequiresRegister());
4543}
4544
4545void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004546 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004547 codegen_->AddSlowPath(slow_path);
4548
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004549 LocationSummary* locations = load->GetLocations();
4550 Register out = locations->Out().AsRegister<Register>();
4551 Register current_method = locations->InAt(0).AsRegister<Register>();
4552 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004553 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004554 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004555 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004556 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004557 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004558 __ Bind(slow_path->GetExitLabel());
4559}
4560
David Brazdilcb1c0552015-08-04 16:22:25 +01004561static int32_t GetExceptionTlsOffset() {
4562 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4563}
4564
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004565void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4566 LocationSummary* locations =
4567 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4568 locations->SetOut(Location::RequiresRegister());
4569}
4570
4571void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004572 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004573 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4574}
4575
4576void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4577 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4578}
4579
4580void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004581 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004582 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004583}
4584
4585void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4586 LocationSummary* locations =
4587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4588 InvokeRuntimeCallingConvention calling_convention;
4589 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4590}
4591
4592void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4593 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004594 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004595}
4596
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004597void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004598 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4599 switch (instruction->GetTypeCheckKind()) {
4600 case TypeCheckKind::kExactCheck:
4601 case TypeCheckKind::kAbstractClassCheck:
4602 case TypeCheckKind::kClassHierarchyCheck:
4603 case TypeCheckKind::kArrayObjectCheck:
4604 call_kind = LocationSummary::kNoCall;
4605 break;
4606 case TypeCheckKind::kInterfaceCheck:
4607 call_kind = LocationSummary::kCall;
4608 break;
4609 case TypeCheckKind::kArrayCheck:
4610 call_kind = LocationSummary::kCallOnSlowPath;
4611 break;
4612 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004614 if (call_kind != LocationSummary::kCall) {
4615 locations->SetInAt(0, Location::RequiresRegister());
4616 locations->SetInAt(1, Location::RequiresRegister());
4617 // The out register is used as a temporary, so it overlaps with the inputs.
4618 // Note that TypeCheckSlowPathARM uses this register too.
4619 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4620 } else {
4621 InvokeRuntimeCallingConvention calling_convention;
4622 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4623 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4624 locations->SetOut(Location::RegisterLocation(R0));
4625 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004626}
4627
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004628void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004629 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004630 Register obj = locations->InAt(0).AsRegister<Register>();
4631 Register cls = locations->InAt(1).AsRegister<Register>();
4632 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004633 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004634 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4635 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4636 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004637 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004638 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004639
4640 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004641 // avoid null check if we know obj is not null.
4642 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004643 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004644 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004645
4646 // In case of an interface check, we put the object class into the object register.
4647 // This is safe, as the register is caller-save, and the object must be in another
4648 // register if it survives the runtime call.
4649 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
4650 ? obj
4651 : out;
4652 __ LoadFromOffset(kLoadWord, target, obj, class_offset);
4653 __ MaybeUnpoisonHeapReference(target);
4654
4655 switch (instruction->GetTypeCheckKind()) {
4656 case TypeCheckKind::kExactCheck: {
4657 __ cmp(out, ShifterOperand(cls));
4658 // Classes must be equal for the instanceof to succeed.
4659 __ b(&zero, NE);
4660 __ LoadImmediate(out, 1);
4661 __ b(&done);
4662 break;
4663 }
4664 case TypeCheckKind::kAbstractClassCheck: {
4665 // If the class is abstract, we eagerly fetch the super class of the
4666 // object to avoid doing a comparison we know will fail.
4667 Label loop;
4668 __ Bind(&loop);
4669 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4670 __ MaybeUnpoisonHeapReference(out);
4671 // If `out` is null, we use it for the result, and jump to `done`.
4672 __ CompareAndBranchIfZero(out, &done);
4673 __ cmp(out, ShifterOperand(cls));
4674 __ b(&loop, NE);
4675 __ LoadImmediate(out, 1);
4676 if (zero.IsLinked()) {
4677 __ b(&done);
4678 }
4679 break;
4680 }
4681 case TypeCheckKind::kClassHierarchyCheck: {
4682 // Walk over the class hierarchy to find a match.
4683 Label loop, success;
4684 __ Bind(&loop);
4685 __ cmp(out, ShifterOperand(cls));
4686 __ b(&success, EQ);
4687 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4688 __ MaybeUnpoisonHeapReference(out);
4689 __ CompareAndBranchIfNonZero(out, &loop);
4690 // If `out` is null, we use it for the result, and jump to `done`.
4691 __ b(&done);
4692 __ Bind(&success);
4693 __ LoadImmediate(out, 1);
4694 if (zero.IsLinked()) {
4695 __ b(&done);
4696 }
4697 break;
4698 }
4699 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004700 // Do an exact check.
4701 Label exact_check;
4702 __ cmp(out, ShifterOperand(cls));
4703 __ b(&exact_check, EQ);
4704 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004705 __ LoadFromOffset(kLoadWord, out, out, component_offset);
4706 __ MaybeUnpoisonHeapReference(out);
4707 // If `out` is null, we use it for the result, and jump to `done`.
4708 __ CompareAndBranchIfZero(out, &done);
4709 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4710 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4711 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004712 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004713 __ LoadImmediate(out, 1);
4714 __ b(&done);
4715 break;
4716 }
4717 case TypeCheckKind::kArrayCheck: {
4718 __ cmp(out, ShifterOperand(cls));
4719 DCHECK(locations->OnlyCallsOnSlowPath());
4720 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4721 instruction, /* is_fatal */ false);
4722 codegen_->AddSlowPath(slow_path);
4723 __ b(slow_path->GetEntryLabel(), NE);
4724 __ LoadImmediate(out, 1);
4725 if (zero.IsLinked()) {
4726 __ b(&done);
4727 }
4728 break;
4729 }
4730
4731 case TypeCheckKind::kInterfaceCheck:
4732 default: {
4733 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4734 instruction,
4735 instruction->GetDexPc(),
4736 nullptr);
4737 if (zero.IsLinked()) {
4738 __ b(&done);
4739 }
4740 break;
4741 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004742 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004743
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004744 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004745 __ Bind(&zero);
4746 __ LoadImmediate(out, 0);
4747 }
4748
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004749 if (done.IsLinked()) {
4750 __ Bind(&done);
4751 }
4752
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004753 if (slow_path != nullptr) {
4754 __ Bind(slow_path->GetExitLabel());
4755 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004756}
4757
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004758void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004759 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4760 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4761
4762 switch (instruction->GetTypeCheckKind()) {
4763 case TypeCheckKind::kExactCheck:
4764 case TypeCheckKind::kAbstractClassCheck:
4765 case TypeCheckKind::kClassHierarchyCheck:
4766 case TypeCheckKind::kArrayObjectCheck:
4767 call_kind = throws_into_catch
4768 ? LocationSummary::kCallOnSlowPath
4769 : LocationSummary::kNoCall;
4770 break;
4771 case TypeCheckKind::kInterfaceCheck:
4772 call_kind = LocationSummary::kCall;
4773 break;
4774 case TypeCheckKind::kArrayCheck:
4775 call_kind = LocationSummary::kCallOnSlowPath;
4776 break;
4777 }
4778
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004780 instruction, call_kind);
4781 if (call_kind != LocationSummary::kCall) {
4782 locations->SetInAt(0, Location::RequiresRegister());
4783 locations->SetInAt(1, Location::RequiresRegister());
4784 // Note that TypeCheckSlowPathARM uses this register too.
4785 locations->AddTemp(Location::RequiresRegister());
4786 } else {
4787 InvokeRuntimeCallingConvention calling_convention;
4788 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4789 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4790 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004791}
4792
4793void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4794 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004795 Register obj = locations->InAt(0).AsRegister<Register>();
4796 Register cls = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004797 Register temp = locations->WillCall()
4798 ? Register(kNoRegister)
4799 : locations->GetTemp(0).AsRegister<Register>();
4800
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004801 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004802 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4803 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4804 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4805 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004806
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004807 if (!locations->WillCall()) {
4808 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4809 instruction, !locations->CanCall());
4810 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004811 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004812
4813 Label done;
4814 // Avoid null check if we know obj is not null.
4815 if (instruction->MustDoNullCheck()) {
4816 __ CompareAndBranchIfZero(obj, &done);
4817 }
4818
4819 if (locations->WillCall()) {
4820 __ LoadFromOffset(kLoadWord, obj, obj, class_offset);
4821 __ MaybeUnpoisonHeapReference(obj);
4822 } else {
4823 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4824 __ MaybeUnpoisonHeapReference(temp);
4825 }
4826
4827 switch (instruction->GetTypeCheckKind()) {
4828 case TypeCheckKind::kExactCheck:
4829 case TypeCheckKind::kArrayCheck: {
4830 __ cmp(temp, ShifterOperand(cls));
4831 // Jump to slow path for throwing the exception or doing a
4832 // more involved array check.
4833 __ b(slow_path->GetEntryLabel(), NE);
4834 break;
4835 }
4836 case TypeCheckKind::kAbstractClassCheck: {
4837 // If the class is abstract, we eagerly fetch the super class of the
4838 // object to avoid doing a comparison we know will fail.
4839 Label loop;
4840 __ Bind(&loop);
4841 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4842 __ MaybeUnpoisonHeapReference(temp);
4843 // Jump to the slow path to throw the exception.
4844 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4845 __ cmp(temp, ShifterOperand(cls));
4846 __ b(&loop, NE);
4847 break;
4848 }
4849 case TypeCheckKind::kClassHierarchyCheck: {
4850 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004851 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004852 __ Bind(&loop);
4853 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004854 __ b(&done, EQ);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004855 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4856 __ MaybeUnpoisonHeapReference(temp);
4857 __ CompareAndBranchIfNonZero(temp, &loop);
4858 // Jump to the slow path to throw the exception.
4859 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004860 break;
4861 }
4862 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004863 // Do an exact check.
4864 __ cmp(temp, ShifterOperand(cls));
4865 __ b(&done, EQ);
4866 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004867 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
4868 __ MaybeUnpoisonHeapReference(temp);
4869 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4870 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
4871 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4872 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
4873 break;
4874 }
4875 case TypeCheckKind::kInterfaceCheck:
4876 default:
4877 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
4878 instruction,
4879 instruction->GetDexPc(),
4880 nullptr);
4881 break;
4882 }
4883 __ Bind(&done);
4884
4885 if (slow_path != nullptr) {
4886 __ Bind(slow_path->GetExitLabel());
4887 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004888}
4889
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004890void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4891 LocationSummary* locations =
4892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4893 InvokeRuntimeCallingConvention calling_convention;
4894 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4895}
4896
4897void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
4898 codegen_->InvokeRuntime(instruction->IsEnter()
4899 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4900 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004901 instruction->GetDexPc(),
4902 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004903}
4904
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004905void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4906void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4907void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4908
4909void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4910 LocationSummary* locations =
4911 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4912 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4913 || instruction->GetResultType() == Primitive::kPrimLong);
4914 locations->SetInAt(0, Location::RequiresRegister());
4915 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004917}
4918
4919void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
4920 HandleBitwiseOperation(instruction);
4921}
4922
4923void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
4924 HandleBitwiseOperation(instruction);
4925}
4926
4927void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
4928 HandleBitwiseOperation(instruction);
4929}
4930
4931void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
4932 LocationSummary* locations = instruction->GetLocations();
4933
4934 if (instruction->GetResultType() == Primitive::kPrimInt) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004935 Register first = locations->InAt(0).AsRegister<Register>();
4936 Register second = locations->InAt(1).AsRegister<Register>();
4937 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004938 if (instruction->IsAnd()) {
4939 __ and_(out, first, ShifterOperand(second));
4940 } else if (instruction->IsOr()) {
4941 __ orr(out, first, ShifterOperand(second));
4942 } else {
4943 DCHECK(instruction->IsXor());
4944 __ eor(out, first, ShifterOperand(second));
4945 }
4946 } else {
4947 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4948 Location first = locations->InAt(0);
4949 Location second = locations->InAt(1);
4950 Location out = locations->Out();
4951 if (instruction->IsAnd()) {
4952 __ and_(out.AsRegisterPairLow<Register>(),
4953 first.AsRegisterPairLow<Register>(),
4954 ShifterOperand(second.AsRegisterPairLow<Register>()));
4955 __ and_(out.AsRegisterPairHigh<Register>(),
4956 first.AsRegisterPairHigh<Register>(),
4957 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4958 } else if (instruction->IsOr()) {
4959 __ orr(out.AsRegisterPairLow<Register>(),
4960 first.AsRegisterPairLow<Register>(),
4961 ShifterOperand(second.AsRegisterPairLow<Register>()));
4962 __ orr(out.AsRegisterPairHigh<Register>(),
4963 first.AsRegisterPairHigh<Register>(),
4964 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4965 } else {
4966 DCHECK(instruction->IsXor());
4967 __ eor(out.AsRegisterPairLow<Register>(),
4968 first.AsRegisterPairLow<Register>(),
4969 ShifterOperand(second.AsRegisterPairLow<Register>()));
4970 __ eor(out.AsRegisterPairHigh<Register>(),
4971 first.AsRegisterPairHigh<Register>(),
4972 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4973 }
4974 }
4975}
4976
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004977void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004978 // For better instruction scheduling we load the direct code pointer before the method pointer.
4979 bool direct_code_loaded = false;
4980 switch (invoke->GetCodePtrLocation()) {
4981 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
4982 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
4983 break;
4984 }
4985 // Calls across dex files are more likely to exceed the available BL range,
4986 // so use absolute patch by falling through to kDirectCodeFixup.
4987 FALLTHROUGH_INTENDED;
4988 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4989 // LR = code address from literal pool with link-time patch.
4990 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4991 direct_code_loaded = true;
4992 break;
4993 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4994 // LR = invoke->GetDirectCodePtr();
4995 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
4996 direct_code_loaded = true;
4997 break;
4998 default:
4999 break;
5000 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005001
Vladimir Marko58155012015-08-19 12:49:41 +00005002 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5003 switch (invoke->GetMethodLoadKind()) {
5004 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5005 // temp = thread->string_init_entrypoint
5006 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5007 break;
5008 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
5009 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5010 break;
5011 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5012 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5013 break;
5014 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5015 __ LoadLiteral(temp.AsRegister<Register>(),
5016 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5017 break;
5018 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5019 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5020 FALLTHROUGH_INTENDED;
5021 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5022 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5023 Register method_reg;
5024 Register reg = temp.AsRegister<Register>();
5025 if (current_method.IsRegister()) {
5026 method_reg = current_method.AsRegister<Register>();
5027 } else {
5028 DCHECK(invoke->GetLocations()->Intrinsified());
5029 DCHECK(!current_method.IsValid());
5030 method_reg = reg;
5031 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5032 }
5033 // temp = current_method->dex_cache_resolved_methods_;
5034 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01005035 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
5036 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005037 // temp = temp[index_in_cache]
5038 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5039 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5040 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005041 }
Vladimir Marko58155012015-08-19 12:49:41 +00005042 }
5043
5044 switch (invoke->GetCodePtrLocation()) {
5045 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5046 __ bl(GetFrameEntryLabel());
5047 break;
5048 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5049 if (!direct_code_loaded) {
5050 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
5051 __ Bind(&relative_call_patches_.back().label);
5052 Label label;
5053 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
5054 __ Bind(&label);
5055 break;
5056 }
5057 // If we loaded the direct code above, fall through.
5058 FALLTHROUGH_INTENDED;
5059 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5060 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5061 // LR prepared above for better instruction scheduling.
5062 DCHECK(direct_code_loaded);
5063 // LR()
5064 __ blx(LR);
5065 break;
5066 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5067 // LR = callee_method->entry_point_from_quick_compiled_code_
5068 __ LoadFromOffset(
5069 kLoadWord, LR, callee_method.AsRegister<Register>(),
5070 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5071 // LR()
5072 __ blx(LR);
5073 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005074 }
5075
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005076 DCHECK(!IsLeafMethod());
5077}
5078
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005079void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5080 Register temp = temp_location.AsRegister<Register>();
5081 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5082 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5083 LocationSummary* locations = invoke->GetLocations();
5084 Location receiver = locations->InAt(0);
5085 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5086 // temp = object->GetClass();
5087 DCHECK(receiver.IsRegister());
5088 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5089 MaybeRecordImplicitNullCheck(invoke);
5090 __ MaybeUnpoisonHeapReference(temp);
5091 // temp = temp->GetMethodAt(method_offset);
5092 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5093 kArmWordSize).Int32Value();
5094 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5095 // LR = temp->GetEntryPoint();
5096 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5097 // LR();
5098 __ blx(LR);
5099}
5100
Vladimir Marko58155012015-08-19 12:49:41 +00005101void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5102 DCHECK(linker_patches->empty());
5103 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5104 linker_patches->reserve(size);
5105 for (const auto& entry : method_patches_) {
5106 const MethodReference& target_method = entry.first;
5107 Literal* literal = entry.second;
5108 DCHECK(literal->GetLabel()->IsBound());
5109 uint32_t literal_offset = literal->GetLabel()->Position();
5110 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5111 target_method.dex_file,
5112 target_method.dex_method_index));
5113 }
5114 for (const auto& entry : call_patches_) {
5115 const MethodReference& target_method = entry.first;
5116 Literal* literal = entry.second;
5117 DCHECK(literal->GetLabel()->IsBound());
5118 uint32_t literal_offset = literal->GetLabel()->Position();
5119 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5120 target_method.dex_file,
5121 target_method.dex_method_index));
5122 }
5123 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5124 uint32_t literal_offset = info.label.Position();
5125 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5126 info.target_method.dex_file,
5127 info.target_method.dex_method_index));
5128 }
5129}
5130
5131Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5132 MethodToLiteralMap* map) {
5133 // Look up the literal for target_method.
5134 auto lb = map->lower_bound(target_method);
5135 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5136 return lb->second;
5137 }
5138 // We don't have a literal for this method yet, insert a new one.
5139 Literal* literal = __ NewLiteral<uint32_t>(0u);
5140 map->PutBefore(lb, target_method, literal);
5141 return literal;
5142}
5143
5144Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5145 return DeduplicateMethodLiteral(target_method, &method_patches_);
5146}
5147
5148Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5149 return DeduplicateMethodLiteral(target_method, &call_patches_);
5150}
5151
Calin Juravleb1498f62015-02-16 13:13:29 +00005152void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
5153 // Nothing to do, this should be removed during prepare for register allocator.
5154 UNUSED(instruction);
5155 LOG(FATAL) << "Unreachable";
5156}
5157
5158void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
5159 // Nothing to do, this should be removed during prepare for register allocator.
5160 UNUSED(instruction);
5161 LOG(FATAL) << "Unreachable";
5162}
5163
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005164void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5165 DCHECK(codegen_->IsBaseline());
5166 LocationSummary* locations =
5167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5168 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5169}
5170
5171void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5172 DCHECK(codegen_->IsBaseline());
5173 // Will be generated at use site.
5174}
5175
Mark Mendellfe57faa2015-09-18 09:26:15 -04005176// Simple implementation of packed switch - generate cascaded compare/jumps.
5177void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5178 LocationSummary* locations =
5179 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5180 locations->SetInAt(0, Location::RequiresRegister());
5181}
5182
5183void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5184 int32_t lower_bound = switch_instr->GetStartValue();
5185 int32_t num_entries = switch_instr->GetNumEntries();
5186 LocationSummary* locations = switch_instr->GetLocations();
5187 Register value_reg = locations->InAt(0).AsRegister<Register>();
5188 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5189
5190 // Create a series of compare/jumps.
5191 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5192 for (int32_t i = 0; i < num_entries; i++) {
5193 GenerateCompareWithImmediate(value_reg, lower_bound + i);
5194 __ b(codegen_->GetLabelOf(successors.at(i)), EQ);
5195 }
5196
5197 // And the default for any other value.
5198 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5199 __ b(codegen_->GetLabelOf(default_block));
5200 }
5201}
5202
Andreas Gampe85b62f22015-09-09 13:15:38 -07005203void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5204 if (!trg.IsValid()) {
5205 DCHECK(type == Primitive::kPrimVoid);
5206 return;
5207 }
5208
5209 DCHECK_NE(type, Primitive::kPrimVoid);
5210
5211 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5212 if (return_loc.Equals(trg)) {
5213 return;
5214 }
5215
5216 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5217 // with the last branch.
5218 if (type == Primitive::kPrimLong) {
5219 HParallelMove parallel_move(GetGraph()->GetArena());
5220 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
5221 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
5222 GetMoveResolver()->EmitNativeCode(&parallel_move);
5223 } else if (type == Primitive::kPrimDouble) {
5224 HParallelMove parallel_move(GetGraph()->GetArena());
5225 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
5226 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
5227 GetMoveResolver()->EmitNativeCode(&parallel_move);
5228 } else {
5229 // Let the parallel move resolver take care of all of this.
5230 HParallelMove parallel_move(GetGraph()->GetArena());
5231 parallel_move.AddMove(return_loc, trg, type, nullptr);
5232 GetMoveResolver()->EmitNativeCode(&parallel_move);
5233 }
5234}
5235
Roland Levillain4d027112015-07-01 15:41:14 +01005236#undef __
5237#undef QUICK_ENTRY_POINT
5238
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005239} // namespace arm
5240} // namespace art