blob: 1df68185497b1b045f481d5fb4c093adce24caee [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037namespace arm {
38
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000039static bool ExpectedPairLayout(Location location) {
40 // We expected this for both core and fpu register pairs.
41 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
42}
43
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000047// We unconditionally allocate R5 to ensure we can do long operations
48// with baseline.
49static constexpr Register kCoreSavedRegisterForBaseline = R5;
50static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070051 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000052static constexpr SRegister kFpuCalleeSaves[] =
53 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000055// D31 cannot be split into two S registers, and the register allocator only works on
56// S registers. Therefore there is no need to block it.
57static constexpr DRegister DTMP = D31;
58
Roland Levillain62a46b22015-06-01 18:24:13 +010059#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010060#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Andreas Gampe85b62f22015-09-09 13:15:38 -070062class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010064 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Alexandre Rames67555f72014-11-18 10:55:16 +000066 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010067 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000069 if (instruction_->CanThrowIntoCatchBlock()) {
70 // Live registers will be restored in the catch block if caught.
71 SaveLiveRegisters(codegen, instruction_->GetLocations());
72 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010073 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000074 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 }
76
Alexandre Rames8158f282015-08-07 10:26:17 +010077 bool IsFatal() const OVERRIDE { return true; }
78
Alexandre Rames9931f312015-06-19 14:47:01 +010079 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
80
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010082 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
84};
85
Andreas Gampe85b62f22015-09-09 13:15:38 -070086class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000087 public:
88 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
89
Alexandre Rames67555f72014-11-18 10:55:16 +000090 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000091 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
92 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000093 if (instruction_->CanThrowIntoCatchBlock()) {
94 // Live registers will be restored in the catch block if caught.
95 SaveLiveRegisters(codegen, instruction_->GetLocations());
96 }
Calin Juravled0d48522014-11-04 16:40:20 +000097 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000098 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Calin Juravled0d48522014-11-04 16:40:20 +000099 }
100
Alexandre Rames8158f282015-08-07 10:26:17 +0100101 bool IsFatal() const OVERRIDE { return true; }
102
Alexandre Rames9931f312015-06-19 14:47:01 +0100103 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
104
Calin Juravled0d48522014-11-04 16:40:20 +0000105 private:
106 HDivZeroCheck* const instruction_;
107 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
108};
109
Andreas Gampe85b62f22015-09-09 13:15:38 -0700110class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000111 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000112 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100113 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000114
Alexandre Rames67555f72014-11-18 10:55:16 +0000115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100116 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000117 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000118 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100119 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000120 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000121 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100122 if (successor_ == nullptr) {
123 __ b(GetReturnLabel());
124 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100125 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100126 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000127 }
128
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 Label* GetReturnLabel() {
130 DCHECK(successor_ == nullptr);
131 return &return_label_;
132 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100134 HBasicBlock* GetSuccessor() const {
135 return successor_;
136 }
137
Alexandre Rames9931f312015-06-19 14:47:01 +0100138 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
139
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 private:
141 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 // If not null, the block to branch to after the suspend check.
143 HBasicBlock* const successor_;
144
145 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 Label return_label_;
147
148 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
149};
150
Andreas Gampe85b62f22015-09-09 13:15:38 -0700151class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100153 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
154 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155
Alexandre Rames67555f72014-11-18 10:55:16 +0000156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100157 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 LocationSummary* locations = instruction_->GetLocations();
159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000168 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100169 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100171 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100172 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100173 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
174 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100175 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000176 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 }
178
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 bool IsFatal() const OVERRIDE { return true; }
180
Alexandre Rames9931f312015-06-19 14:47:01 +0100181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185
186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
187};
188
Andreas Gampe85b62f22015-09-09 13:15:38 -0700189class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000191 LoadClassSlowPathARM(HLoadClass* cls,
192 HInstruction* at,
193 uint32_t dex_pc,
194 bool do_clinit)
195 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
196 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
197 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100198
Alexandre Rames67555f72014-11-18 10:55:16 +0000199 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000200 LocationSummary* locations = at_->GetLocations();
201
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100202 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
203 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000204 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100206 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000208 int32_t entry_point_offset = do_clinit_
209 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
210 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000211 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000212
213 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000214 Location out = locations->Out();
215 if (out.IsValid()) {
216 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
218 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000219 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220 __ b(GetExitLabel());
221 }
222
Alexandre Rames9931f312015-06-19 14:47:01 +0100223 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
224
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 // The class this slow path will load.
227 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 // The instruction where this slow path is happening.
230 // (Might be the load class or an initialization check).
231 HInstruction* const at_;
232
233 // The dex PC of `at_`.
234 const uint32_t dex_pc_;
235
236 // Whether to initialize the class.
237 const bool do_clinit_;
238
239 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240};
241
Andreas Gampe85b62f22015-09-09 13:15:38 -0700242class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000243 public:
244 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
245
Alexandre Rames67555f72014-11-18 10:55:16 +0000246 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000247 LocationSummary* locations = instruction_->GetLocations();
248 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
249
250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
251 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000253
254 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800255 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000256 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000257 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
259
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000260 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000261 __ b(GetExitLabel());
262 }
263
Alexandre Rames9931f312015-06-19 14:47:01 +0100264 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
265
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000266 private:
267 HLoadString* const instruction_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
270};
271
Andreas Gampe85b62f22015-09-09 13:15:38 -0700272class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000274 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
275 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Alexandre Rames67555f72014-11-18 10:55:16 +0000277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100279 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
280 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000281 DCHECK(instruction_->IsCheckCast()
282 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
285 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000286
287 if (instruction_->IsCheckCast()) {
288 // The codegen for the instruction overwrites `temp`, so put it back in place.
289 Register obj = locations->InAt(0).AsRegister<Register>();
290 Register temp = locations->GetTemp(0).AsRegister<Register>();
291 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
292 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
293 __ MaybeUnpoisonHeapReference(temp);
294 }
295
296 if (!is_fatal_) {
297 SaveLiveRegisters(codegen, locations);
298 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 // We're moving two locations to locations that could overlap, so we need a parallel
301 // move resolver.
302 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000303 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100304 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100306 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
309 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100312 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
313 instruction_,
314 instruction_->GetDexPc(),
315 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
317 } else {
318 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100319 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
320 instruction_,
321 instruction_->GetDexPc(),
322 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000325 if (!is_fatal_) {
326 RestoreLiveRegisters(codegen, locations);
327 __ b(GetExitLabel());
328 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 }
330
Alexandre Rames9931f312015-06-19 14:47:01 +0100331 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000333 bool IsFatal() const OVERRIDE { return is_fatal_; }
334
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338
339 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
340};
341
Andreas Gampe85b62f22015-09-09 13:15:38 -0700342class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700343 public:
344 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
345 : instruction_(instruction) {}
346
347 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
348 __ Bind(GetEntryLabel());
349 SaveLiveRegisters(codegen, instruction_->GetLocations());
350 DCHECK(instruction_->IsDeoptimize());
351 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
352 uint32_t dex_pc = deoptimize->GetDexPc();
353 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
354 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
355 }
356
Alexandre Rames9931f312015-06-19 14:47:01 +0100357 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
358
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700359 private:
360 HInstruction* const instruction_;
361 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
362};
363
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100364class ArraySetSlowPathARM : public SlowPathCode {
365 public:
366 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
367
368 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
369 LocationSummary* locations = instruction_->GetLocations();
370 __ Bind(GetEntryLabel());
371 SaveLiveRegisters(codegen, locations);
372
373 InvokeRuntimeCallingConvention calling_convention;
374 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
375 parallel_move.AddMove(
376 locations->InAt(0),
377 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
378 Primitive::kPrimNot,
379 nullptr);
380 parallel_move.AddMove(
381 locations->InAt(1),
382 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
383 Primitive::kPrimInt,
384 nullptr);
385 parallel_move.AddMove(
386 locations->InAt(2),
387 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
388 Primitive::kPrimNot,
389 nullptr);
390 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
391
392 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
393 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
394 instruction_,
395 instruction_->GetDexPc(),
396 this);
397 RestoreLiveRegisters(codegen, locations);
398 __ b(GetExitLabel());
399 }
400
401 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
402
403 private:
404 HInstruction* const instruction_;
405
406 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
407};
408
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000409#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100410#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700411
Aart Bike9f37602015-10-09 11:15:55 -0700412inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700413 switch (cond) {
414 case kCondEQ: return EQ;
415 case kCondNE: return NE;
416 case kCondLT: return LT;
417 case kCondLE: return LE;
418 case kCondGT: return GT;
419 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700420 case kCondB: return LO;
421 case kCondBE: return LS;
422 case kCondA: return HI;
423 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700424 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100425 LOG(FATAL) << "Unreachable";
426 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700427}
428
Aart Bike9f37602015-10-09 11:15:55 -0700429// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100430inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700431 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100432 case kCondEQ: return EQ;
433 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700434 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100435 case kCondLT: return LO;
436 case kCondLE: return LS;
437 case kCondGT: return HI;
438 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700439 // Unsigned remain unchanged.
440 case kCondB: return LO;
441 case kCondBE: return LS;
442 case kCondA: return HI;
443 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700444 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100445 LOG(FATAL) << "Unreachable";
446 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700447}
448
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100449void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100450 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100451}
452
453void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100454 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100455}
456
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100457size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
458 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
459 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100460}
461
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100462size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
463 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
464 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100465}
466
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000467size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
468 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
469 return kArmWordSize;
470}
471
472size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
473 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
474 return kArmWordSize;
475}
476
Calin Juravle34166012014-12-19 17:22:29 +0000477CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000478 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100479 const CompilerOptions& compiler_options,
480 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000481 : CodeGenerator(graph,
482 kNumberOfCoreRegisters,
483 kNumberOfSRegisters,
484 kNumberOfRegisterPairs,
485 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
486 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000487 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
488 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100489 compiler_options,
490 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100491 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100492 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100493 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100494 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000495 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000496 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100497 method_patches_(MethodReferenceComparator(),
498 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
499 call_patches_(MethodReferenceComparator(),
500 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
501 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700502 // Always save the LR register to mimic Quick.
503 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100504}
505
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000506void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
507 // Ensure that we fix up branches and literal loads and emit the literal pool.
508 __ FinalizeCode();
509
510 // Adjust native pc offsets in stack maps.
511 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
512 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
513 uint32_t new_position = __ GetAdjustedPosition(old_position);
514 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
515 }
516 // Adjust native pc offsets of block labels.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100517 for (HBasicBlock* block : *block_order_) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000518 // Get the label directly from block_labels_ rather than through GetLabelOf() to avoid
519 // FirstNonEmptyBlock() which could lead to adjusting a label more than once.
Vladimir Marko225b6462015-09-28 12:17:40 +0100520 DCHECK_LT(block->GetBlockId(), GetGraph()->GetBlocks().size());
521 Label* block_label = &block_labels_[block->GetBlockId()];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000522 DCHECK_EQ(block_label->IsBound(), !block->IsSingleJump());
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000523 if (block_label->IsBound()) {
524 __ AdjustLabelPosition(block_label);
525 }
526 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100527 // Adjust pc offsets for the disassembly information.
528 if (disasm_info_ != nullptr) {
529 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
530 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
531 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
532 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
533 it.second.start = __ GetAdjustedPosition(it.second.start);
534 it.second.end = __ GetAdjustedPosition(it.second.end);
535 }
536 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
537 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
538 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
539 }
540 }
Vladimir Marko58155012015-08-19 12:49:41 +0000541 // Adjust pc offsets for relative call patches.
542 for (MethodPatchInfo<Label>& info : relative_call_patches_) {
543 __ AdjustLabelPosition(&info.label);
544 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000545
546 CodeGenerator::Finalize(allocator);
547}
548
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100549Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100550 switch (type) {
551 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100552 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100553 ArmManagedRegister pair =
554 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100555 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
556 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
557
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100558 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
559 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100560 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100561 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100562 }
563
564 case Primitive::kPrimByte:
565 case Primitive::kPrimBoolean:
566 case Primitive::kPrimChar:
567 case Primitive::kPrimShort:
568 case Primitive::kPrimInt:
569 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100570 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100571 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100572 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
573 ArmManagedRegister current =
574 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
575 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100576 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100577 }
578 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100580 }
581
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000582 case Primitive::kPrimFloat: {
583 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100586
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000587 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000588 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
589 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000590 return Location::FpuRegisterPairLocation(reg, reg + 1);
591 }
592
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100593 case Primitive::kPrimVoid:
594 LOG(FATAL) << "Unreachable type " << type;
595 }
596
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100597 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598}
599
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000600void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100601 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100602 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100603
604 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100605 blocked_core_registers_[SP] = true;
606 blocked_core_registers_[LR] = true;
607 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100608
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100609 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100610 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100611
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100612 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100613 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100614
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000615 if (is_baseline) {
616 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
617 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
618 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000619
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000620 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100621 }
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000622
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100623 if (is_baseline || GetGraph()->IsDebuggable()) {
624 // Stubs do not save callee-save floating point registers. If the graph
625 // is debuggable, we need to deal with these registers differently. For
626 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000627 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
628 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
629 }
630 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100631
632 UpdateBlockedPairRegisters();
633}
634
635void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
636 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
637 ArmManagedRegister current =
638 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
639 if (blocked_core_registers_[current.AsRegisterPairLow()]
640 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
641 blocked_register_pairs_[i] = true;
642 }
643 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100644}
645
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100646InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
647 : HGraphVisitor(graph),
648 assembler_(codegen->GetAssembler()),
649 codegen_(codegen) {}
650
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000651void CodeGeneratorARM::ComputeSpillMask() {
652 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000653 // Save one extra register for baseline. Note that on thumb2, there is no easy
654 // instruction to restore just the PC, so this actually helps both baseline
655 // and non-baseline to save and restore at least two registers at entry and exit.
656 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000657 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
658 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
659 // We use vpush and vpop for saving and restoring floating point registers, which take
660 // a SRegister and the number of registers to save/restore after that SRegister. We
661 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
662 // but in the range.
663 if (fpu_spill_mask_ != 0) {
664 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
665 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
666 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
667 fpu_spill_mask_ |= (1 << i);
668 }
669 }
670}
671
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100672static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100673 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100674}
675
676static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100677 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100678}
679
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000680void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000681 bool skip_overflow_check =
682 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000683 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000684 __ Bind(&frame_entry_label_);
685
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000686 if (HasEmptyFrame()) {
687 return;
688 }
689
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100690 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000691 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
692 __ LoadFromOffset(kLoadWord, IP, IP, 0);
693 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100694 }
695
Andreas Gampe501fd632015-09-10 16:11:06 -0700696 __ PushList(core_spill_mask_);
697 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
698 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000699 if (fpu_spill_mask_ != 0) {
700 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
701 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100702 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100703 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000704 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100705 int adjust = GetFrameSize() - FrameEntrySpillSize();
706 __ AddConstant(SP, -adjust);
707 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100708 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000709}
710
711void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000712 if (HasEmptyFrame()) {
713 __ bx(LR);
714 return;
715 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100716 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100717 int adjust = GetFrameSize() - FrameEntrySpillSize();
718 __ AddConstant(SP, adjust);
719 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000720 if (fpu_spill_mask_ != 0) {
721 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
722 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100723 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
724 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000725 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700726 // Pop LR into PC to return.
727 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
728 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
729 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100730 __ cfi().RestoreState();
731 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000732}
733
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100734void CodeGeneratorARM::Bind(HBasicBlock* block) {
735 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000736}
737
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100738Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
739 switch (load->GetType()) {
740 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100742 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100743
744 case Primitive::kPrimInt:
745 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100746 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100748
749 case Primitive::kPrimBoolean:
750 case Primitive::kPrimByte:
751 case Primitive::kPrimChar:
752 case Primitive::kPrimShort:
753 case Primitive::kPrimVoid:
754 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700755 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100756 }
757
758 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700759 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100760}
761
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100762Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100763 switch (type) {
764 case Primitive::kPrimBoolean:
765 case Primitive::kPrimByte:
766 case Primitive::kPrimChar:
767 case Primitive::kPrimShort:
768 case Primitive::kPrimInt:
769 case Primitive::kPrimNot: {
770 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000771 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100772 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100773 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100774 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000775 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100776 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100777 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100778
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000779 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100780 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000781 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100782 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000783 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100784 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000785 if (calling_convention.GetRegisterAt(index) == R1) {
786 // Skip R1, and use R2_R3 instead.
787 gp_index_++;
788 index++;
789 }
790 }
791 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
792 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000793 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100794
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000795 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000796 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100797 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000798 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
799 }
800 }
801
802 case Primitive::kPrimFloat: {
803 uint32_t stack_index = stack_index_++;
804 if (float_index_ % 2 == 0) {
805 float_index_ = std::max(double_index_, float_index_);
806 }
807 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
808 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
809 } else {
810 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
811 }
812 }
813
814 case Primitive::kPrimDouble: {
815 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
816 uint32_t stack_index = stack_index_;
817 stack_index_ += 2;
818 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
819 uint32_t index = double_index_;
820 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000821 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000822 calling_convention.GetFpuRegisterAt(index),
823 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000824 DCHECK(ExpectedPairLayout(result));
825 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000826 } else {
827 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100828 }
829 }
830
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100831 case Primitive::kPrimVoid:
832 LOG(FATAL) << "Unexpected parameter type " << type;
833 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100834 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100835 return Location();
836}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100837
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100838Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000839 switch (type) {
840 case Primitive::kPrimBoolean:
841 case Primitive::kPrimByte:
842 case Primitive::kPrimChar:
843 case Primitive::kPrimShort:
844 case Primitive::kPrimInt:
845 case Primitive::kPrimNot: {
846 return Location::RegisterLocation(R0);
847 }
848
849 case Primitive::kPrimFloat: {
850 return Location::FpuRegisterLocation(S0);
851 }
852
853 case Primitive::kPrimLong: {
854 return Location::RegisterPairLocation(R0, R1);
855 }
856
857 case Primitive::kPrimDouble: {
858 return Location::FpuRegisterPairLocation(S0, S1);
859 }
860
861 case Primitive::kPrimVoid:
862 return Location();
863 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100864
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000865 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000866}
867
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100868Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
869 return Location::RegisterLocation(kMethodRegisterArgument);
870}
871
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872void CodeGeneratorARM::Move32(Location destination, Location source) {
873 if (source.Equals(destination)) {
874 return;
875 }
876 if (destination.IsRegister()) {
877 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100879 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000880 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100881 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000882 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100884 } else if (destination.IsFpuRegister()) {
885 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000886 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000888 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100889 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000890 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000893 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000895 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100896 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000897 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000899 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100900 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
901 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 }
903 }
904}
905
906void CodeGeneratorARM::Move64(Location destination, Location source) {
907 if (source.Equals(destination)) {
908 return;
909 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100910 if (destination.IsRegisterPair()) {
911 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000912 EmitParallelMoves(
913 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
914 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100915 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000916 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100917 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
918 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100919 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000920 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100921 } else if (source.IsFpuRegisterPair()) {
922 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
923 destination.AsRegisterPairHigh<Register>(),
924 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100925 } else {
926 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000927 DCHECK(ExpectedPairLayout(destination));
928 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
929 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100930 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000931 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100932 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000933 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
934 SP,
935 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100936 } else if (source.IsRegisterPair()) {
937 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
938 source.AsRegisterPairLow<Register>(),
939 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100940 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000941 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943 } else {
944 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100945 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000946 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100947 if (source.AsRegisterPairLow<Register>() == R1) {
948 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100949 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
950 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100951 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100952 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100953 SP, destination.GetStackIndex());
954 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000955 } else if (source.IsFpuRegisterPair()) {
956 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
957 SP,
958 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 } else {
960 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000961 EmitParallelMoves(
962 Location::StackSlot(source.GetStackIndex()),
963 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100964 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000965 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100966 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
967 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100968 }
969 }
970}
971
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100972void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100973 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100974 if (instruction->IsCurrentMethod()) {
975 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
976 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100977 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100978 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000979 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000980 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
981 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000982 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000983 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000984 } else {
985 DCHECK(location.IsStackSlot());
986 __ LoadImmediate(IP, value);
987 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
988 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000989 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000990 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000991 int64_t value = const_to_move->AsLongConstant()->GetValue();
992 if (location.IsRegisterPair()) {
993 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
994 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
995 } else {
996 DCHECK(location.IsDoubleStackSlot());
997 __ LoadImmediate(IP, Low32Bits(value));
998 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
999 __ LoadImmediate(IP, High32Bits(value));
1000 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
1001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 }
Roland Levillain476df552014-10-09 17:51:36 +01001003 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1005 switch (instruction->GetType()) {
1006 case Primitive::kPrimBoolean:
1007 case Primitive::kPrimByte:
1008 case Primitive::kPrimChar:
1009 case Primitive::kPrimShort:
1010 case Primitive::kPrimInt:
1011 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001012 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013 Move32(location, Location::StackSlot(stack_slot));
1014 break;
1015
1016 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001017 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018 Move64(location, Location::DoubleStackSlot(stack_slot));
1019 break;
1020
1021 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001024 } else if (instruction->IsTemporary()) {
1025 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001026 if (temp_location.IsStackSlot()) {
1027 Move32(location, temp_location);
1028 } else {
1029 DCHECK(temp_location.IsDoubleStackSlot());
1030 Move64(location, temp_location);
1031 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001032 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001033 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 switch (instruction->GetType()) {
1035 case Primitive::kPrimBoolean:
1036 case Primitive::kPrimByte:
1037 case Primitive::kPrimChar:
1038 case Primitive::kPrimShort:
1039 case Primitive::kPrimNot:
1040 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001042 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 break;
1044
1045 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001046 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001047 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 break;
1049
1050 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053 }
1054}
1055
Calin Juravle175dc732015-08-25 15:42:32 +01001056void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1057 DCHECK(location.IsRegister());
1058 __ LoadImmediate(location.AsRegister<Register>(), value);
1059}
1060
Calin Juravlee460d1d2015-09-29 04:52:17 +01001061void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1062 if (Primitive::Is64BitType(dst_type)) {
1063 Move64(dst, src);
1064 } else {
1065 Move32(dst, src);
1066 }
1067}
1068
1069void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1070 if (location.IsRegister()) {
1071 locations->AddTemp(location);
1072 } else if (location.IsRegisterPair()) {
1073 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1074 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1075 } else {
1076 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1077 }
1078}
1079
Calin Juravle175dc732015-08-25 15:42:32 +01001080void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1081 HInstruction* instruction,
1082 uint32_t dex_pc,
1083 SlowPathCode* slow_path) {
1084 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1085 instruction,
1086 dex_pc,
1087 slow_path);
1088}
1089
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001090void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1091 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001092 uint32_t dex_pc,
1093 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001094 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001095 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1096 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001097 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001098}
1099
David Brazdilfc6a86a2015-06-26 10:33:45 +00001100void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001101 DCHECK(!successor->IsExitBlock());
1102
1103 HBasicBlock* block = got->GetBlock();
1104 HInstruction* previous = got->GetPrevious();
1105
1106 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001107 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001108 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1109 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1110 return;
1111 }
1112
1113 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1114 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1115 }
1116 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001117 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001118 }
1119}
1120
David Brazdilfc6a86a2015-06-26 10:33:45 +00001121void LocationsBuilderARM::VisitGoto(HGoto* got) {
1122 got->SetLocations(nullptr);
1123}
1124
1125void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1126 HandleGoto(got, got->GetSuccessor());
1127}
1128
1129void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1130 try_boundary->SetLocations(nullptr);
1131}
1132
1133void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1134 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1135 if (!successor->IsExitBlock()) {
1136 HandleGoto(try_boundary, successor);
1137 }
1138}
1139
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001140void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001141 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001142}
1143
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001144void InstructionCodeGeneratorARM::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001145 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001146}
1147
Roland Levillain4fa13f62015-07-06 18:11:54 +01001148void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1149 ShifterOperand operand;
1150 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1151 __ cmp(left, operand);
1152 } else {
1153 Register temp = IP;
1154 __ LoadImmediate(temp, right);
1155 __ cmp(left, ShifterOperand(temp));
1156 }
1157}
1158
1159void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1160 Label* true_label,
1161 Label* false_label) {
1162 __ vmstat(); // transfer FP status register to ARM APSR.
Aart Bike9f37602015-10-09 11:15:55 -07001163 // TODO: merge into a single branch (except "equal or unordered" and "not equal")
Roland Levillain4fa13f62015-07-06 18:11:54 +01001164 if (cond->IsFPConditionTrueIfNaN()) {
1165 __ b(true_label, VS); // VS for unordered.
1166 } else if (cond->IsFPConditionFalseIfNaN()) {
1167 __ b(false_label, VS); // VS for unordered.
1168 }
Aart Bike9f37602015-10-09 11:15:55 -07001169 __ b(true_label, ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001170}
1171
1172void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1173 Label* true_label,
1174 Label* false_label) {
1175 LocationSummary* locations = cond->GetLocations();
1176 Location left = locations->InAt(0);
1177 Location right = locations->InAt(1);
1178 IfCondition if_cond = cond->GetCondition();
1179
1180 Register left_high = left.AsRegisterPairHigh<Register>();
1181 Register left_low = left.AsRegisterPairLow<Register>();
1182 IfCondition true_high_cond = if_cond;
1183 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001184 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001185
1186 // Set the conditions for the test, remembering that == needs to be
1187 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001188 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001189 switch (if_cond) {
1190 case kCondEQ:
1191 case kCondNE:
1192 // Nothing to do.
1193 break;
1194 case kCondLT:
1195 false_high_cond = kCondGT;
1196 break;
1197 case kCondLE:
1198 true_high_cond = kCondLT;
1199 break;
1200 case kCondGT:
1201 false_high_cond = kCondLT;
1202 break;
1203 case kCondGE:
1204 true_high_cond = kCondGT;
1205 break;
Aart Bike9f37602015-10-09 11:15:55 -07001206 case kCondB:
1207 false_high_cond = kCondA;
1208 break;
1209 case kCondBE:
1210 true_high_cond = kCondB;
1211 break;
1212 case kCondA:
1213 false_high_cond = kCondB;
1214 break;
1215 case kCondAE:
1216 true_high_cond = kCondA;
1217 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001218 }
1219 if (right.IsConstant()) {
1220 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1221 int32_t val_low = Low32Bits(value);
1222 int32_t val_high = High32Bits(value);
1223
1224 GenerateCompareWithImmediate(left_high, val_high);
1225 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001226 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001227 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001228 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001229 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001230 __ b(true_label, ARMCondition(true_high_cond));
1231 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001232 }
1233 // Must be equal high, so compare the lows.
1234 GenerateCompareWithImmediate(left_low, val_low);
1235 } else {
1236 Register right_high = right.AsRegisterPairHigh<Register>();
1237 Register right_low = right.AsRegisterPairLow<Register>();
1238
1239 __ cmp(left_high, ShifterOperand(right_high));
1240 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001241 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001242 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001243 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001244 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001245 __ b(true_label, ARMCondition(true_high_cond));
1246 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001247 }
1248 // Must be equal high, so compare the lows.
1249 __ cmp(left_low, ShifterOperand(right_low));
1250 }
1251 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001252 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001253 __ b(true_label, final_condition);
1254}
1255
1256void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1257 HCondition* condition,
1258 Label* true_target,
1259 Label* false_target,
1260 Label* always_true_target) {
1261 LocationSummary* locations = condition->GetLocations();
1262 Location left = locations->InAt(0);
1263 Location right = locations->InAt(1);
1264
1265 // We don't want true_target as a nullptr.
1266 if (true_target == nullptr) {
1267 true_target = always_true_target;
1268 }
1269 bool falls_through = (false_target == nullptr);
1270
1271 // FP compares don't like null false_targets.
1272 if (false_target == nullptr) {
1273 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1274 }
1275
1276 Primitive::Type type = condition->InputAt(0)->GetType();
1277 switch (type) {
1278 case Primitive::kPrimLong:
1279 GenerateLongComparesAndJumps(condition, true_target, false_target);
1280 break;
1281 case Primitive::kPrimFloat:
1282 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1283 GenerateFPJumps(condition, true_target, false_target);
1284 break;
1285 case Primitive::kPrimDouble:
1286 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1287 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1288 GenerateFPJumps(condition, true_target, false_target);
1289 break;
1290 default:
1291 LOG(FATAL) << "Unexpected compare type " << type;
1292 }
1293
1294 if (!falls_through) {
1295 __ b(false_target);
1296 }
1297}
1298
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001299void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1300 Label* true_target,
1301 Label* false_target,
1302 Label* always_true_target) {
1303 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001304 if (cond->IsIntConstant()) {
1305 // Constant condition, statically compared against 1.
1306 int32_t cond_value = cond->AsIntConstant()->GetValue();
1307 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001308 if (always_true_target != nullptr) {
1309 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001310 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001311 return;
1312 } else {
1313 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001314 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001315 } else {
1316 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1317 // Condition has been materialized, compare the output to 0
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001318 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001319 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1320 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001321 } else {
1322 // Condition has not been materialized, use its inputs as the
1323 // comparison and its condition as the branch condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001324 Primitive::Type type =
1325 cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
1326 // Is this a long or FP comparison that has been folded into the HCondition?
1327 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1328 // Generate the comparison directly.
1329 GenerateCompareTestAndBranch(instruction->AsIf(), cond->AsCondition(),
1330 true_target, false_target, always_true_target);
1331 return;
1332 }
1333
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001334 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001335 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001336 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001337 Location right = locations->InAt(1);
1338 if (right.IsRegister()) {
1339 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001340 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001341 DCHECK(right.IsConstant());
1342 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001343 }
Aart Bike9f37602015-10-09 11:15:55 -07001344 __ b(true_target, ARMCondition(cond->AsCondition()->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001345 }
Dave Allison20dfc792014-06-16 20:44:29 -07001346 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001347 if (false_target != nullptr) {
1348 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001349 }
1350}
1351
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001352void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1353 LocationSummary* locations =
1354 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1355 HInstruction* cond = if_instr->InputAt(0);
1356 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1357 locations->SetInAt(0, Location::RequiresRegister());
1358 }
1359}
1360
1361void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1362 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1363 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1364 Label* always_true_target = true_target;
1365 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1366 if_instr->IfTrueSuccessor())) {
1367 always_true_target = nullptr;
1368 }
1369 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1370 if_instr->IfFalseSuccessor())) {
1371 false_target = nullptr;
1372 }
1373 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1374}
1375
1376void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1377 LocationSummary* locations = new (GetGraph()->GetArena())
1378 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1379 HInstruction* cond = deoptimize->InputAt(0);
1380 DCHECK(cond->IsCondition());
1381 if (cond->AsCondition()->NeedsMaterialization()) {
1382 locations->SetInAt(0, Location::RequiresRegister());
1383 }
1384}
1385
1386void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001387 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001388 DeoptimizationSlowPathARM(deoptimize);
1389 codegen_->AddSlowPath(slow_path);
1390 Label* slow_path_entry = slow_path->GetEntryLabel();
1391 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1392}
Dave Allison20dfc792014-06-16 20:44:29 -07001393
Roland Levillain0d37cd02015-05-27 16:39:19 +01001394void LocationsBuilderARM::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001395 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001396 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397 // Handle the long/FP comparisons made in instruction simplification.
1398 switch (cond->InputAt(0)->GetType()) {
1399 case Primitive::kPrimLong:
1400 locations->SetInAt(0, Location::RequiresRegister());
1401 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1402 if (cond->NeedsMaterialization()) {
1403 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1404 }
1405 break;
1406
1407 case Primitive::kPrimFloat:
1408 case Primitive::kPrimDouble:
1409 locations->SetInAt(0, Location::RequiresFpuRegister());
1410 locations->SetInAt(1, Location::RequiresFpuRegister());
1411 if (cond->NeedsMaterialization()) {
1412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1413 }
1414 break;
1415
1416 default:
1417 locations->SetInAt(0, Location::RequiresRegister());
1418 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1419 if (cond->NeedsMaterialization()) {
1420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1421 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001422 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001423}
1424
Roland Levillain0d37cd02015-05-27 16:39:19 +01001425void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001426 if (!cond->NeedsMaterialization()) {
1427 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001428 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001429
1430 LocationSummary* locations = cond->GetLocations();
1431 Location left = locations->InAt(0);
1432 Location right = locations->InAt(1);
1433 Register out = locations->Out().AsRegister<Register>();
1434 Label true_label, false_label;
1435
1436 switch (cond->InputAt(0)->GetType()) {
1437 default: {
1438 // Integer case.
1439 if (right.IsRegister()) {
1440 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1441 } else {
1442 DCHECK(right.IsConstant());
1443 GenerateCompareWithImmediate(left.AsRegister<Register>(),
1444 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1445 }
Aart Bike9f37602015-10-09 11:15:55 -07001446 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001447 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001448 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001449 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001450 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001451 return;
1452 }
1453 case Primitive::kPrimLong:
1454 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1455 break;
1456 case Primitive::kPrimFloat:
1457 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1458 GenerateFPJumps(cond, &true_label, &false_label);
1459 break;
1460 case Primitive::kPrimDouble:
1461 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1462 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1463 GenerateFPJumps(cond, &true_label, &false_label);
1464 break;
1465 }
1466
1467 // Convert the jumps into the result.
1468 Label done_label;
1469
1470 // False case: result = 0.
1471 __ Bind(&false_label);
1472 __ LoadImmediate(out, 0);
1473 __ b(&done_label);
1474
1475 // True case: result = 1.
1476 __ Bind(&true_label);
1477 __ LoadImmediate(out, 1);
1478 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001479}
1480
1481void LocationsBuilderARM::VisitEqual(HEqual* comp) {
1482 VisitCondition(comp);
1483}
1484
1485void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
1486 VisitCondition(comp);
1487}
1488
1489void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
1490 VisitCondition(comp);
1491}
1492
1493void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
1494 VisitCondition(comp);
1495}
1496
1497void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
1498 VisitCondition(comp);
1499}
1500
1501void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
1502 VisitCondition(comp);
1503}
1504
1505void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1506 VisitCondition(comp);
1507}
1508
1509void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1510 VisitCondition(comp);
1511}
1512
1513void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
1514 VisitCondition(comp);
1515}
1516
1517void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
1518 VisitCondition(comp);
1519}
1520
1521void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1522 VisitCondition(comp);
1523}
1524
1525void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1526 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001527}
1528
Aart Bike9f37602015-10-09 11:15:55 -07001529void LocationsBuilderARM::VisitBelow(HBelow* comp) {
1530 VisitCondition(comp);
1531}
1532
1533void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
1534 VisitCondition(comp);
1535}
1536
1537void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
1538 VisitCondition(comp);
1539}
1540
1541void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
1542 VisitCondition(comp);
1543}
1544
1545void LocationsBuilderARM::VisitAbove(HAbove* comp) {
1546 VisitCondition(comp);
1547}
1548
1549void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
1550 VisitCondition(comp);
1551}
1552
1553void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
1554 VisitCondition(comp);
1555}
1556
1557void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
1558 VisitCondition(comp);
1559}
1560
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001561void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001562 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001563}
1564
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001565void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1566 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001567}
1568
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001569void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001570 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001571}
1572
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001573void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001574 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001575 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001576}
1577
1578void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001579 LocationSummary* locations =
1580 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001581 switch (store->InputAt(1)->GetType()) {
1582 case Primitive::kPrimBoolean:
1583 case Primitive::kPrimByte:
1584 case Primitive::kPrimChar:
1585 case Primitive::kPrimShort:
1586 case Primitive::kPrimInt:
1587 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001588 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001589 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1590 break;
1591
1592 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001593 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001594 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1595 break;
1596
1597 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001598 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001599 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001600}
1601
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001602void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001603 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604}
1605
1606void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001607 LocationSummary* locations =
1608 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001609 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001610}
1611
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001612void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001613 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001614 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001615}
1616
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001617void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1618 LocationSummary* locations =
1619 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1620 locations->SetOut(Location::ConstantLocation(constant));
1621}
1622
1623void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant) {
1624 // Will be generated at use site.
1625 UNUSED(constant);
1626}
1627
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001628void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001629 LocationSummary* locations =
1630 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001631 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001632}
1633
1634void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant) {
1635 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001636 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001637}
1638
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001639void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1640 LocationSummary* locations =
1641 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1642 locations->SetOut(Location::ConstantLocation(constant));
1643}
1644
1645void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant) {
1646 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001647 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001648}
1649
1650void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1651 LocationSummary* locations =
1652 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1653 locations->SetOut(Location::ConstantLocation(constant));
1654}
1655
1656void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant) {
1657 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001658 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001659}
1660
Calin Juravle27df7582015-04-17 19:12:31 +01001661void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1662 memory_barrier->SetLocations(nullptr);
1663}
1664
1665void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1666 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1667}
1668
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001669void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001670 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001671}
1672
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001673void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001674 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001675 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001676}
1677
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001678void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001679 LocationSummary* locations =
1680 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001681 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001682}
1683
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001684void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001685 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001686 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001687}
1688
Calin Juravle175dc732015-08-25 15:42:32 +01001689void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1690 // The trampoline uses the same calling convention as dex calling conventions,
1691 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1692 // the method_idx.
1693 HandleInvoke(invoke);
1694}
1695
1696void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1697 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1698}
1699
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001700void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001701 // When we do not run baseline, explicit clinit checks triggered by static
1702 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1703 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001704
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001705 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001706 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001707 codegen_->GetInstructionSetFeatures());
1708 if (intrinsic.TryDispatch(invoke)) {
1709 return;
1710 }
1711
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001712 HandleInvoke(invoke);
1713}
1714
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001715static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1716 if (invoke->GetLocations()->Intrinsified()) {
1717 IntrinsicCodeGeneratorARM intrinsic(codegen);
1718 intrinsic.Dispatch(invoke);
1719 return true;
1720 }
1721 return false;
1722}
1723
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001724void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001725 // When we do not run baseline, explicit clinit checks triggered by static
1726 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1727 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001728
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001729 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1730 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001731 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001732
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001733 LocationSummary* locations = invoke->GetLocations();
1734 codegen_->GenerateStaticOrDirectCall(
1735 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001736 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001737}
1738
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001739void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001740 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001741 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001742}
1743
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001744void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001745 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001746 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001747 codegen_->GetInstructionSetFeatures());
1748 if (intrinsic.TryDispatch(invoke)) {
1749 return;
1750 }
1751
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001752 HandleInvoke(invoke);
1753}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001754
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001755void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001756 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1757 return;
1758 }
1759
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001760 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001761 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001762 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001763}
1764
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001765void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1766 HandleInvoke(invoke);
1767 // Add the hidden argument.
1768 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1769}
1770
1771void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1772 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001773 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001774 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1775 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001776 LocationSummary* locations = invoke->GetLocations();
1777 Location receiver = locations->InAt(0);
1778 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1779
1780 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001781 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1782 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001783
1784 // temp = object->GetClass();
1785 if (receiver.IsStackSlot()) {
1786 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1787 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1788 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001789 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001790 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001791 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001792 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001793 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001794 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001795 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001796 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1797 // LR = temp->GetEntryPoint();
1798 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1799 // LR();
1800 __ blx(LR);
1801 DCHECK(!codegen_->IsLeafMethod());
1802 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1803}
1804
Roland Levillain88cb1752014-10-20 16:36:47 +01001805void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1806 LocationSummary* locations =
1807 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1808 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001809 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001810 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1812 break;
1813 }
1814 case Primitive::kPrimLong: {
1815 locations->SetInAt(0, Location::RequiresRegister());
1816 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001817 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001818 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001819
Roland Levillain88cb1752014-10-20 16:36:47 +01001820 case Primitive::kPrimFloat:
1821 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001822 locations->SetInAt(0, Location::RequiresFpuRegister());
1823 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001824 break;
1825
1826 default:
1827 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1828 }
1829}
1830
1831void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1832 LocationSummary* locations = neg->GetLocations();
1833 Location out = locations->Out();
1834 Location in = locations->InAt(0);
1835 switch (neg->GetResultType()) {
1836 case Primitive::kPrimInt:
1837 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001838 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001839 break;
1840
1841 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001842 DCHECK(in.IsRegisterPair());
1843 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1844 __ rsbs(out.AsRegisterPairLow<Register>(),
1845 in.AsRegisterPairLow<Register>(),
1846 ShifterOperand(0));
1847 // We cannot emit an RSC (Reverse Subtract with Carry)
1848 // instruction here, as it does not exist in the Thumb-2
1849 // instruction set. We use the following approach
1850 // using SBC and SUB instead.
1851 //
1852 // out.hi = -C
1853 __ sbc(out.AsRegisterPairHigh<Register>(),
1854 out.AsRegisterPairHigh<Register>(),
1855 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1856 // out.hi = out.hi - in.hi
1857 __ sub(out.AsRegisterPairHigh<Register>(),
1858 out.AsRegisterPairHigh<Register>(),
1859 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1860 break;
1861
Roland Levillain88cb1752014-10-20 16:36:47 +01001862 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001863 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001864 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001865 break;
1866
Roland Levillain88cb1752014-10-20 16:36:47 +01001867 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001868 DCHECK(in.IsFpuRegisterPair());
1869 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1870 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001871 break;
1872
1873 default:
1874 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1875 }
1876}
1877
Roland Levillaindff1f282014-11-05 14:15:05 +00001878void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001879 Primitive::Type result_type = conversion->GetResultType();
1880 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001881 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001882
Roland Levillain5b3ee562015-04-14 16:02:41 +01001883 // The float-to-long, double-to-long and long-to-float type conversions
1884 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001885 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001886 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1887 && result_type == Primitive::kPrimLong)
1888 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001889 ? LocationSummary::kCall
1890 : LocationSummary::kNoCall;
1891 LocationSummary* locations =
1892 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1893
David Brazdilb2bd1c52015-03-25 11:17:37 +00001894 // The Java language does not allow treating boolean as an integral type but
1895 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001896
Roland Levillaindff1f282014-11-05 14:15:05 +00001897 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001898 case Primitive::kPrimByte:
1899 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001900 case Primitive::kPrimBoolean:
1901 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001902 case Primitive::kPrimShort:
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001905 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001906 locations->SetInAt(0, Location::RequiresRegister());
1907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1908 break;
1909
1910 default:
1911 LOG(FATAL) << "Unexpected type conversion from " << input_type
1912 << " to " << result_type;
1913 }
1914 break;
1915
Roland Levillain01a8d712014-11-14 16:27:39 +00001916 case Primitive::kPrimShort:
1917 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001918 case Primitive::kPrimBoolean:
1919 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001920 case Primitive::kPrimByte:
1921 case Primitive::kPrimInt:
1922 case Primitive::kPrimChar:
1923 // Processing a Dex `int-to-short' instruction.
1924 locations->SetInAt(0, Location::RequiresRegister());
1925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1926 break;
1927
1928 default:
1929 LOG(FATAL) << "Unexpected type conversion from " << input_type
1930 << " to " << result_type;
1931 }
1932 break;
1933
Roland Levillain946e1432014-11-11 17:35:19 +00001934 case Primitive::kPrimInt:
1935 switch (input_type) {
1936 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001937 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001938 locations->SetInAt(0, Location::Any());
1939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1940 break;
1941
1942 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001943 // Processing a Dex `float-to-int' instruction.
1944 locations->SetInAt(0, Location::RequiresFpuRegister());
1945 locations->SetOut(Location::RequiresRegister());
1946 locations->AddTemp(Location::RequiresFpuRegister());
1947 break;
1948
Roland Levillain946e1432014-11-11 17:35:19 +00001949 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001950 // Processing a Dex `double-to-int' instruction.
1951 locations->SetInAt(0, Location::RequiresFpuRegister());
1952 locations->SetOut(Location::RequiresRegister());
1953 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001954 break;
1955
1956 default:
1957 LOG(FATAL) << "Unexpected type conversion from " << input_type
1958 << " to " << result_type;
1959 }
1960 break;
1961
Roland Levillaindff1f282014-11-05 14:15:05 +00001962 case Primitive::kPrimLong:
1963 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001964 case Primitive::kPrimBoolean:
1965 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001966 case Primitive::kPrimByte:
1967 case Primitive::kPrimShort:
1968 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001969 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001970 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001971 locations->SetInAt(0, Location::RequiresRegister());
1972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1973 break;
1974
Roland Levillain624279f2014-12-04 11:54:28 +00001975 case Primitive::kPrimFloat: {
1976 // Processing a Dex `float-to-long' instruction.
1977 InvokeRuntimeCallingConvention calling_convention;
1978 locations->SetInAt(0, Location::FpuRegisterLocation(
1979 calling_convention.GetFpuRegisterAt(0)));
1980 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1981 break;
1982 }
1983
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001984 case Primitive::kPrimDouble: {
1985 // Processing a Dex `double-to-long' instruction.
1986 InvokeRuntimeCallingConvention calling_convention;
1987 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1988 calling_convention.GetFpuRegisterAt(0),
1989 calling_convention.GetFpuRegisterAt(1)));
1990 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001991 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001992 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001993
1994 default:
1995 LOG(FATAL) << "Unexpected type conversion from " << input_type
1996 << " to " << result_type;
1997 }
1998 break;
1999
Roland Levillain981e4542014-11-14 11:47:14 +00002000 case Primitive::kPrimChar:
2001 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002002 case Primitive::kPrimBoolean:
2003 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002004 case Primitive::kPrimByte:
2005 case Primitive::kPrimShort:
2006 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002007 // Processing a Dex `int-to-char' instruction.
2008 locations->SetInAt(0, Location::RequiresRegister());
2009 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2010 break;
2011
2012 default:
2013 LOG(FATAL) << "Unexpected type conversion from " << input_type
2014 << " to " << result_type;
2015 }
2016 break;
2017
Roland Levillaindff1f282014-11-05 14:15:05 +00002018 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002019 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002020 case Primitive::kPrimBoolean:
2021 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002022 case Primitive::kPrimByte:
2023 case Primitive::kPrimShort:
2024 case Primitive::kPrimInt:
2025 case Primitive::kPrimChar:
2026 // Processing a Dex `int-to-float' instruction.
2027 locations->SetInAt(0, Location::RequiresRegister());
2028 locations->SetOut(Location::RequiresFpuRegister());
2029 break;
2030
Roland Levillain5b3ee562015-04-14 16:02:41 +01002031 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002032 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002033 InvokeRuntimeCallingConvention calling_convention;
2034 locations->SetInAt(0, Location::RegisterPairLocation(
2035 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2036 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002037 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002038 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002039
Roland Levillaincff13742014-11-17 14:32:17 +00002040 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002041 // Processing a Dex `double-to-float' instruction.
2042 locations->SetInAt(0, Location::RequiresFpuRegister());
2043 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002044 break;
2045
2046 default:
2047 LOG(FATAL) << "Unexpected type conversion from " << input_type
2048 << " to " << result_type;
2049 };
2050 break;
2051
Roland Levillaindff1f282014-11-05 14:15:05 +00002052 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002053 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002054 case Primitive::kPrimBoolean:
2055 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002056 case Primitive::kPrimByte:
2057 case Primitive::kPrimShort:
2058 case Primitive::kPrimInt:
2059 case Primitive::kPrimChar:
2060 // Processing a Dex `int-to-double' instruction.
2061 locations->SetInAt(0, Location::RequiresRegister());
2062 locations->SetOut(Location::RequiresFpuRegister());
2063 break;
2064
2065 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002066 // Processing a Dex `long-to-double' instruction.
2067 locations->SetInAt(0, Location::RequiresRegister());
2068 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002069 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002070 locations->AddTemp(Location::RequiresFpuRegister());
2071 break;
2072
Roland Levillaincff13742014-11-17 14:32:17 +00002073 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002074 // Processing a Dex `float-to-double' instruction.
2075 locations->SetInAt(0, Location::RequiresFpuRegister());
2076 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002077 break;
2078
2079 default:
2080 LOG(FATAL) << "Unexpected type conversion from " << input_type
2081 << " to " << result_type;
2082 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002083 break;
2084
2085 default:
2086 LOG(FATAL) << "Unexpected type conversion from " << input_type
2087 << " to " << result_type;
2088 }
2089}
2090
2091void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2092 LocationSummary* locations = conversion->GetLocations();
2093 Location out = locations->Out();
2094 Location in = locations->InAt(0);
2095 Primitive::Type result_type = conversion->GetResultType();
2096 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002097 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002098 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002099 case Primitive::kPrimByte:
2100 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002101 case Primitive::kPrimBoolean:
2102 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002103 case Primitive::kPrimShort:
2104 case Primitive::kPrimInt:
2105 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002106 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002108 break;
2109
2110 default:
2111 LOG(FATAL) << "Unexpected type conversion from " << input_type
2112 << " to " << result_type;
2113 }
2114 break;
2115
Roland Levillain01a8d712014-11-14 16:27:39 +00002116 case Primitive::kPrimShort:
2117 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002118 case Primitive::kPrimBoolean:
2119 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002120 case Primitive::kPrimByte:
2121 case Primitive::kPrimInt:
2122 case Primitive::kPrimChar:
2123 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002125 break;
2126
2127 default:
2128 LOG(FATAL) << "Unexpected type conversion from " << input_type
2129 << " to " << result_type;
2130 }
2131 break;
2132
Roland Levillain946e1432014-11-11 17:35:19 +00002133 case Primitive::kPrimInt:
2134 switch (input_type) {
2135 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002136 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002137 DCHECK(out.IsRegister());
2138 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002140 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002142 } else {
2143 DCHECK(in.IsConstant());
2144 DCHECK(in.GetConstant()->IsLongConstant());
2145 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002146 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002147 }
2148 break;
2149
Roland Levillain3f8f9362014-12-02 17:45:01 +00002150 case Primitive::kPrimFloat: {
2151 // Processing a Dex `float-to-int' instruction.
2152 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2153 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2154 __ vcvtis(temp, temp);
2155 __ vmovrs(out.AsRegister<Register>(), temp);
2156 break;
2157 }
2158
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002159 case Primitive::kPrimDouble: {
2160 // Processing a Dex `double-to-int' instruction.
2161 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2162 DRegister temp_d = FromLowSToD(temp_s);
2163 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2164 __ vcvtid(temp_s, temp_d);
2165 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002166 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002167 }
Roland Levillain946e1432014-11-11 17:35:19 +00002168
2169 default:
2170 LOG(FATAL) << "Unexpected type conversion from " << input_type
2171 << " to " << result_type;
2172 }
2173 break;
2174
Roland Levillaindff1f282014-11-05 14:15:05 +00002175 case Primitive::kPrimLong:
2176 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002177 case Primitive::kPrimBoolean:
2178 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002179 case Primitive::kPrimByte:
2180 case Primitive::kPrimShort:
2181 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002182 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002183 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002184 DCHECK(out.IsRegisterPair());
2185 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002186 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002187 // Sign extension.
2188 __ Asr(out.AsRegisterPairHigh<Register>(),
2189 out.AsRegisterPairLow<Register>(),
2190 31);
2191 break;
2192
2193 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002194 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002195 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2196 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002197 conversion->GetDexPc(),
2198 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002199 break;
2200
Roland Levillaindff1f282014-11-05 14:15:05 +00002201 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002202 // Processing a Dex `double-to-long' instruction.
2203 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2204 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002205 conversion->GetDexPc(),
2206 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002207 break;
2208
2209 default:
2210 LOG(FATAL) << "Unexpected type conversion from " << input_type
2211 << " to " << result_type;
2212 }
2213 break;
2214
Roland Levillain981e4542014-11-14 11:47:14 +00002215 case Primitive::kPrimChar:
2216 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002217 case Primitive::kPrimBoolean:
2218 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002219 case Primitive::kPrimByte:
2220 case Primitive::kPrimShort:
2221 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002222 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002223 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002224 break;
2225
2226 default:
2227 LOG(FATAL) << "Unexpected type conversion from " << input_type
2228 << " to " << result_type;
2229 }
2230 break;
2231
Roland Levillaindff1f282014-11-05 14:15:05 +00002232 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002233 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002234 case Primitive::kPrimBoolean:
2235 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002236 case Primitive::kPrimByte:
2237 case Primitive::kPrimShort:
2238 case Primitive::kPrimInt:
2239 case Primitive::kPrimChar: {
2240 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2242 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002243 break;
2244 }
2245
Roland Levillain5b3ee562015-04-14 16:02:41 +01002246 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002247 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002248 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2249 conversion,
2250 conversion->GetDexPc(),
2251 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002252 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002253
Roland Levillaincff13742014-11-17 14:32:17 +00002254 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002255 // Processing a Dex `double-to-float' instruction.
2256 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2257 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 };
2264 break;
2265
Roland Levillaindff1f282014-11-05 14:15:05 +00002266 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002267 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002268 case Primitive::kPrimBoolean:
2269 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002270 case Primitive::kPrimByte:
2271 case Primitive::kPrimShort:
2272 case Primitive::kPrimInt:
2273 case Primitive::kPrimChar: {
2274 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002275 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002276 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2277 out.AsFpuRegisterPairLow<SRegister>());
2278 break;
2279 }
2280
Roland Levillain647b9ed2014-11-27 12:06:00 +00002281 case Primitive::kPrimLong: {
2282 // Processing a Dex `long-to-double' instruction.
2283 Register low = in.AsRegisterPairLow<Register>();
2284 Register high = in.AsRegisterPairHigh<Register>();
2285 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2286 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002287 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002288 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002289 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2290 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002291
Roland Levillain682393c2015-04-14 15:57:52 +01002292 // temp_d = int-to-double(high)
2293 __ vmovsr(temp_s, high);
2294 __ vcvtdi(temp_d, temp_s);
2295 // constant_d = k2Pow32EncodingForDouble
2296 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2297 // out_d = unsigned-to-double(low)
2298 __ vmovsr(out_s, low);
2299 __ vcvtdu(out_d, out_s);
2300 // out_d += temp_d * constant_d
2301 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002302 break;
2303 }
2304
Roland Levillaincff13742014-11-17 14:32:17 +00002305 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002306 // Processing a Dex `float-to-double' instruction.
2307 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2308 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002309 break;
2310
2311 default:
2312 LOG(FATAL) << "Unexpected type conversion from " << input_type
2313 << " to " << result_type;
2314 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002315 break;
2316
2317 default:
2318 LOG(FATAL) << "Unexpected type conversion from " << input_type
2319 << " to " << result_type;
2320 }
2321}
2322
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002323void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002324 LocationSummary* locations =
2325 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002326 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002327 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002328 locations->SetInAt(0, Location::RequiresRegister());
2329 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2331 break;
2332 }
2333
2334 case Primitive::kPrimLong: {
2335 locations->SetInAt(0, Location::RequiresRegister());
2336 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002338 break;
2339 }
2340
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002341 case Primitive::kPrimFloat:
2342 case Primitive::kPrimDouble: {
2343 locations->SetInAt(0, Location::RequiresFpuRegister());
2344 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002345 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002346 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002347 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002348
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002349 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002350 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002351 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002352}
2353
2354void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2355 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002356 Location out = locations->Out();
2357 Location first = locations->InAt(0);
2358 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002359 switch (add->GetResultType()) {
2360 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002361 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002362 __ add(out.AsRegister<Register>(),
2363 first.AsRegister<Register>(),
2364 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002365 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002366 __ AddConstant(out.AsRegister<Register>(),
2367 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002368 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002369 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002370 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002371
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002372 case Primitive::kPrimLong: {
2373 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002374 __ adds(out.AsRegisterPairLow<Register>(),
2375 first.AsRegisterPairLow<Register>(),
2376 ShifterOperand(second.AsRegisterPairLow<Register>()));
2377 __ adc(out.AsRegisterPairHigh<Register>(),
2378 first.AsRegisterPairHigh<Register>(),
2379 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002380 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002381 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002382
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002383 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002384 __ vadds(out.AsFpuRegister<SRegister>(),
2385 first.AsFpuRegister<SRegister>(),
2386 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002387 break;
2388
2389 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002390 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2391 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2392 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002393 break;
2394
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002395 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002396 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002397 }
2398}
2399
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002400void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002401 LocationSummary* locations =
2402 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002403 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002404 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002405 locations->SetInAt(0, Location::RequiresRegister());
2406 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002407 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2408 break;
2409 }
2410
2411 case Primitive::kPrimLong: {
2412 locations->SetInAt(0, Location::RequiresRegister());
2413 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002415 break;
2416 }
Calin Juravle11351682014-10-23 15:38:15 +01002417 case Primitive::kPrimFloat:
2418 case Primitive::kPrimDouble: {
2419 locations->SetInAt(0, Location::RequiresFpuRegister());
2420 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002421 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002422 break;
Calin Juravle11351682014-10-23 15:38:15 +01002423 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002424 default:
Calin Juravle11351682014-10-23 15:38:15 +01002425 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002426 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002427}
2428
2429void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2430 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002431 Location out = locations->Out();
2432 Location first = locations->InAt(0);
2433 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002434 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002435 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002436 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002437 __ sub(out.AsRegister<Register>(),
2438 first.AsRegister<Register>(),
2439 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002440 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 __ AddConstant(out.AsRegister<Register>(),
2442 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002443 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002444 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002445 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002446 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002447
Calin Juravle11351682014-10-23 15:38:15 +01002448 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002449 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002450 __ subs(out.AsRegisterPairLow<Register>(),
2451 first.AsRegisterPairLow<Register>(),
2452 ShifterOperand(second.AsRegisterPairLow<Register>()));
2453 __ sbc(out.AsRegisterPairHigh<Register>(),
2454 first.AsRegisterPairHigh<Register>(),
2455 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002456 break;
Calin Juravle11351682014-10-23 15:38:15 +01002457 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002458
Calin Juravle11351682014-10-23 15:38:15 +01002459 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002460 __ vsubs(out.AsFpuRegister<SRegister>(),
2461 first.AsFpuRegister<SRegister>(),
2462 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002463 break;
Calin Juravle11351682014-10-23 15:38:15 +01002464 }
2465
2466 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002467 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2468 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2469 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002470 break;
2471 }
2472
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002474 default:
Calin Juravle11351682014-10-23 15:38:15 +01002475 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002476 }
2477}
2478
Calin Juravle34bacdf2014-10-07 20:23:36 +01002479void LocationsBuilderARM::VisitMul(HMul* mul) {
2480 LocationSummary* locations =
2481 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2482 switch (mul->GetResultType()) {
2483 case Primitive::kPrimInt:
2484 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002485 locations->SetInAt(0, Location::RequiresRegister());
2486 locations->SetInAt(1, Location::RequiresRegister());
2487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002488 break;
2489 }
2490
Calin Juravleb5bfa962014-10-21 18:02:24 +01002491 case Primitive::kPrimFloat:
2492 case Primitive::kPrimDouble: {
2493 locations->SetInAt(0, Location::RequiresFpuRegister());
2494 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002495 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002496 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002497 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002498
2499 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002500 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002501 }
2502}
2503
2504void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2505 LocationSummary* locations = mul->GetLocations();
2506 Location out = locations->Out();
2507 Location first = locations->InAt(0);
2508 Location second = locations->InAt(1);
2509 switch (mul->GetResultType()) {
2510 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002511 __ mul(out.AsRegister<Register>(),
2512 first.AsRegister<Register>(),
2513 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002514 break;
2515 }
2516 case Primitive::kPrimLong: {
2517 Register out_hi = out.AsRegisterPairHigh<Register>();
2518 Register out_lo = out.AsRegisterPairLow<Register>();
2519 Register in1_hi = first.AsRegisterPairHigh<Register>();
2520 Register in1_lo = first.AsRegisterPairLow<Register>();
2521 Register in2_hi = second.AsRegisterPairHigh<Register>();
2522 Register in2_lo = second.AsRegisterPairLow<Register>();
2523
2524 // Extra checks to protect caused by the existence of R1_R2.
2525 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2526 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2527 DCHECK_NE(out_hi, in1_lo);
2528 DCHECK_NE(out_hi, in2_lo);
2529
2530 // input: in1 - 64 bits, in2 - 64 bits
2531 // output: out
2532 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2533 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2534 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2535
2536 // IP <- in1.lo * in2.hi
2537 __ mul(IP, in1_lo, in2_hi);
2538 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2539 __ mla(out_hi, in1_hi, in2_lo, IP);
2540 // out.lo <- (in1.lo * in2.lo)[31:0];
2541 __ umull(out_lo, IP, in1_lo, in2_lo);
2542 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2543 __ add(out_hi, out_hi, ShifterOperand(IP));
2544 break;
2545 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002546
2547 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002548 __ vmuls(out.AsFpuRegister<SRegister>(),
2549 first.AsFpuRegister<SRegister>(),
2550 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002551 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002552 }
2553
2554 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002555 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2556 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2557 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002558 break;
2559 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002560
2561 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002562 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002563 }
2564}
2565
Zheng Xuc6667102015-05-15 16:08:45 +08002566void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2567 DCHECK(instruction->IsDiv() || instruction->IsRem());
2568 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2569
2570 LocationSummary* locations = instruction->GetLocations();
2571 Location second = locations->InAt(1);
2572 DCHECK(second.IsConstant());
2573
2574 Register out = locations->Out().AsRegister<Register>();
2575 Register dividend = locations->InAt(0).AsRegister<Register>();
2576 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2577 DCHECK(imm == 1 || imm == -1);
2578
2579 if (instruction->IsRem()) {
2580 __ LoadImmediate(out, 0);
2581 } else {
2582 if (imm == 1) {
2583 __ Mov(out, dividend);
2584 } else {
2585 __ rsb(out, dividend, ShifterOperand(0));
2586 }
2587 }
2588}
2589
2590void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2591 DCHECK(instruction->IsDiv() || instruction->IsRem());
2592 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2593
2594 LocationSummary* locations = instruction->GetLocations();
2595 Location second = locations->InAt(1);
2596 DCHECK(second.IsConstant());
2597
2598 Register out = locations->Out().AsRegister<Register>();
2599 Register dividend = locations->InAt(0).AsRegister<Register>();
2600 Register temp = locations->GetTemp(0).AsRegister<Register>();
2601 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002602 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002603 DCHECK(IsPowerOfTwo(abs_imm));
2604 int ctz_imm = CTZ(abs_imm);
2605
2606 if (ctz_imm == 1) {
2607 __ Lsr(temp, dividend, 32 - ctz_imm);
2608 } else {
2609 __ Asr(temp, dividend, 31);
2610 __ Lsr(temp, temp, 32 - ctz_imm);
2611 }
2612 __ add(out, temp, ShifterOperand(dividend));
2613
2614 if (instruction->IsDiv()) {
2615 __ Asr(out, out, ctz_imm);
2616 if (imm < 0) {
2617 __ rsb(out, out, ShifterOperand(0));
2618 }
2619 } else {
2620 __ ubfx(out, out, 0, ctz_imm);
2621 __ sub(out, out, ShifterOperand(temp));
2622 }
2623}
2624
2625void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2626 DCHECK(instruction->IsDiv() || instruction->IsRem());
2627 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2628
2629 LocationSummary* locations = instruction->GetLocations();
2630 Location second = locations->InAt(1);
2631 DCHECK(second.IsConstant());
2632
2633 Register out = locations->Out().AsRegister<Register>();
2634 Register dividend = locations->InAt(0).AsRegister<Register>();
2635 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2636 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2637 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2638
2639 int64_t magic;
2640 int shift;
2641 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2642
2643 __ LoadImmediate(temp1, magic);
2644 __ smull(temp2, temp1, dividend, temp1);
2645
2646 if (imm > 0 && magic < 0) {
2647 __ add(temp1, temp1, ShifterOperand(dividend));
2648 } else if (imm < 0 && magic > 0) {
2649 __ sub(temp1, temp1, ShifterOperand(dividend));
2650 }
2651
2652 if (shift != 0) {
2653 __ Asr(temp1, temp1, shift);
2654 }
2655
2656 if (instruction->IsDiv()) {
2657 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2658 } else {
2659 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2660 // TODO: Strength reduction for mls.
2661 __ LoadImmediate(temp2, imm);
2662 __ mls(out, temp1, temp2, dividend);
2663 }
2664}
2665
2666void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2667 DCHECK(instruction->IsDiv() || instruction->IsRem());
2668 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2669
2670 LocationSummary* locations = instruction->GetLocations();
2671 Location second = locations->InAt(1);
2672 DCHECK(second.IsConstant());
2673
2674 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2675 if (imm == 0) {
2676 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2677 } else if (imm == 1 || imm == -1) {
2678 DivRemOneOrMinusOne(instruction);
2679 } else if (IsPowerOfTwo(std::abs(imm))) {
2680 DivRemByPowerOfTwo(instruction);
2681 } else {
2682 DCHECK(imm <= -2 || imm >= 2);
2683 GenerateDivRemWithAnyConstant(instruction);
2684 }
2685}
2686
Calin Juravle7c4954d2014-10-28 16:57:40 +00002687void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002688 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2689 if (div->GetResultType() == Primitive::kPrimLong) {
2690 // pLdiv runtime call.
2691 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002692 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2693 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002694 } else if (div->GetResultType() == Primitive::kPrimInt &&
2695 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2696 // pIdivmod runtime call.
2697 call_kind = LocationSummary::kCall;
2698 }
2699
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002700 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2701
Calin Juravle7c4954d2014-10-28 16:57:40 +00002702 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002703 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002704 if (div->InputAt(1)->IsConstant()) {
2705 locations->SetInAt(0, Location::RequiresRegister());
2706 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2708 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2709 if (abs_imm <= 1) {
2710 // No temp register required.
2711 } else {
2712 locations->AddTemp(Location::RequiresRegister());
2713 if (!IsPowerOfTwo(abs_imm)) {
2714 locations->AddTemp(Location::RequiresRegister());
2715 }
2716 }
2717 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002718 locations->SetInAt(0, Location::RequiresRegister());
2719 locations->SetInAt(1, Location::RequiresRegister());
2720 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2721 } else {
2722 InvokeRuntimeCallingConvention calling_convention;
2723 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2724 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2725 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2726 // we only need the former.
2727 locations->SetOut(Location::RegisterLocation(R0));
2728 }
Calin Juravled0d48522014-11-04 16:40:20 +00002729 break;
2730 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002731 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002732 InvokeRuntimeCallingConvention calling_convention;
2733 locations->SetInAt(0, Location::RegisterPairLocation(
2734 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2735 locations->SetInAt(1, Location::RegisterPairLocation(
2736 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002737 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002738 break;
2739 }
2740 case Primitive::kPrimFloat:
2741 case Primitive::kPrimDouble: {
2742 locations->SetInAt(0, Location::RequiresFpuRegister());
2743 locations->SetInAt(1, Location::RequiresFpuRegister());
2744 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2745 break;
2746 }
2747
2748 default:
2749 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2750 }
2751}
2752
2753void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2754 LocationSummary* locations = div->GetLocations();
2755 Location out = locations->Out();
2756 Location first = locations->InAt(0);
2757 Location second = locations->InAt(1);
2758
2759 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002760 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002761 if (second.IsConstant()) {
2762 GenerateDivRemConstantIntegral(div);
2763 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002764 __ sdiv(out.AsRegister<Register>(),
2765 first.AsRegister<Register>(),
2766 second.AsRegister<Register>());
2767 } else {
2768 InvokeRuntimeCallingConvention calling_convention;
2769 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2770 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2771 DCHECK_EQ(R0, out.AsRegister<Register>());
2772
2773 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2774 }
Calin Juravled0d48522014-11-04 16:40:20 +00002775 break;
2776 }
2777
Calin Juravle7c4954d2014-10-28 16:57:40 +00002778 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002779 InvokeRuntimeCallingConvention calling_convention;
2780 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2781 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2782 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2783 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2784 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002785 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002786
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002787 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002788 break;
2789 }
2790
2791 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002792 __ vdivs(out.AsFpuRegister<SRegister>(),
2793 first.AsFpuRegister<SRegister>(),
2794 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002795 break;
2796 }
2797
2798 case Primitive::kPrimDouble: {
2799 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2800 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2801 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2802 break;
2803 }
2804
2805 default:
2806 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2807 }
2808}
2809
Calin Juravlebacfec32014-11-14 15:54:36 +00002810void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002811 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002812
2813 // Most remainders are implemented in the runtime.
2814 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002815 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2816 // sdiv will be replaced by other instruction sequence.
2817 call_kind = LocationSummary::kNoCall;
2818 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2819 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002820 // Have hardware divide instruction for int, do it with three instructions.
2821 call_kind = LocationSummary::kNoCall;
2822 }
2823
Calin Juravlebacfec32014-11-14 15:54:36 +00002824 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2825
Calin Juravled2ec87d2014-12-08 14:24:46 +00002826 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002827 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002828 if (rem->InputAt(1)->IsConstant()) {
2829 locations->SetInAt(0, Location::RequiresRegister());
2830 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2831 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2832 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2833 if (abs_imm <= 1) {
2834 // No temp register required.
2835 } else {
2836 locations->AddTemp(Location::RequiresRegister());
2837 if (!IsPowerOfTwo(abs_imm)) {
2838 locations->AddTemp(Location::RequiresRegister());
2839 }
2840 }
2841 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002842 locations->SetInAt(0, Location::RequiresRegister());
2843 locations->SetInAt(1, Location::RequiresRegister());
2844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2845 locations->AddTemp(Location::RequiresRegister());
2846 } else {
2847 InvokeRuntimeCallingConvention calling_convention;
2848 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2849 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2850 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2851 // we only need the latter.
2852 locations->SetOut(Location::RegisterLocation(R1));
2853 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002854 break;
2855 }
2856 case Primitive::kPrimLong: {
2857 InvokeRuntimeCallingConvention calling_convention;
2858 locations->SetInAt(0, Location::RegisterPairLocation(
2859 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2860 locations->SetInAt(1, Location::RegisterPairLocation(
2861 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2862 // The runtime helper puts the output in R2,R3.
2863 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2864 break;
2865 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002866 case Primitive::kPrimFloat: {
2867 InvokeRuntimeCallingConvention calling_convention;
2868 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2869 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2870 locations->SetOut(Location::FpuRegisterLocation(S0));
2871 break;
2872 }
2873
Calin Juravlebacfec32014-11-14 15:54:36 +00002874 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002875 InvokeRuntimeCallingConvention calling_convention;
2876 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2877 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2878 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2879 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2880 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002881 break;
2882 }
2883
2884 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002885 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002886 }
2887}
2888
2889void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2890 LocationSummary* locations = rem->GetLocations();
2891 Location out = locations->Out();
2892 Location first = locations->InAt(0);
2893 Location second = locations->InAt(1);
2894
Calin Juravled2ec87d2014-12-08 14:24:46 +00002895 Primitive::Type type = rem->GetResultType();
2896 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002897 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002898 if (second.IsConstant()) {
2899 GenerateDivRemConstantIntegral(rem);
2900 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002901 Register reg1 = first.AsRegister<Register>();
2902 Register reg2 = second.AsRegister<Register>();
2903 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002904
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002905 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002906 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002907 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002908 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002909 } else {
2910 InvokeRuntimeCallingConvention calling_convention;
2911 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2912 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2913 DCHECK_EQ(R1, out.AsRegister<Register>());
2914
2915 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2916 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002917 break;
2918 }
2919
2920 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002921 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002922 break;
2923 }
2924
Calin Juravled2ec87d2014-12-08 14:24:46 +00002925 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002926 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002927 break;
2928 }
2929
Calin Juravlebacfec32014-11-14 15:54:36 +00002930 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002931 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002932 break;
2933 }
2934
2935 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002936 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002937 }
2938}
2939
Calin Juravled0d48522014-11-04 16:40:20 +00002940void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002941 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2942 ? LocationSummary::kCallOnSlowPath
2943 : LocationSummary::kNoCall;
2944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002945 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002946 if (instruction->HasUses()) {
2947 locations->SetOut(Location::SameAsFirstInput());
2948 }
2949}
2950
2951void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002952 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00002953 codegen_->AddSlowPath(slow_path);
2954
2955 LocationSummary* locations = instruction->GetLocations();
2956 Location value = locations->InAt(0);
2957
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002958 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002959 case Primitive::kPrimByte:
2960 case Primitive::kPrimChar:
2961 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002962 case Primitive::kPrimInt: {
2963 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002964 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002965 } else {
2966 DCHECK(value.IsConstant()) << value;
2967 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2968 __ b(slow_path->GetEntryLabel());
2969 }
2970 }
2971 break;
2972 }
2973 case Primitive::kPrimLong: {
2974 if (value.IsRegisterPair()) {
2975 __ orrs(IP,
2976 value.AsRegisterPairLow<Register>(),
2977 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2978 __ b(slow_path->GetEntryLabel(), EQ);
2979 } else {
2980 DCHECK(value.IsConstant()) << value;
2981 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2982 __ b(slow_path->GetEntryLabel());
2983 }
2984 }
2985 break;
2986 default:
2987 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2988 }
2989 }
Calin Juravled0d48522014-11-04 16:40:20 +00002990}
2991
Calin Juravle9aec02f2014-11-18 23:06:35 +00002992void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2993 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2994
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002995 LocationSummary* locations =
2996 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002997
2998 switch (op->GetResultType()) {
2999 case Primitive::kPrimInt: {
3000 locations->SetInAt(0, Location::RequiresRegister());
3001 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1)));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003002 // Make the output overlap, as it will be used to hold the masked
3003 // second input.
3004 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003005 break;
3006 }
3007 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003008 locations->SetInAt(0, Location::RequiresRegister());
3009 locations->SetInAt(1, Location::RequiresRegister());
3010 locations->AddTemp(Location::RequiresRegister());
3011 locations->SetOut(Location::RequiresRegister());
Calin Juravle9aec02f2014-11-18 23:06:35 +00003012 break;
3013 }
3014 default:
3015 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3016 }
3017}
3018
3019void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3020 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3021
3022 LocationSummary* locations = op->GetLocations();
3023 Location out = locations->Out();
3024 Location first = locations->InAt(0);
3025 Location second = locations->InAt(1);
3026
3027 Primitive::Type type = op->GetResultType();
3028 switch (type) {
3029 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 Register out_reg = out.AsRegister<Register>();
3031 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003032 // Arm doesn't mask the shift count so we need to do it ourselves.
3033 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 Register second_reg = second.AsRegister<Register>();
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003035 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003036 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003037 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003038 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003039 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003040 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003041 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003042 }
3043 } else {
3044 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3045 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
3046 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
3047 __ Mov(out_reg, first_reg);
3048 } else if (op->IsShl()) {
3049 __ Lsl(out_reg, first_reg, shift_value);
3050 } else if (op->IsShr()) {
3051 __ Asr(out_reg, first_reg, shift_value);
3052 } else {
3053 __ Lsr(out_reg, first_reg, shift_value);
3054 }
3055 }
3056 break;
3057 }
3058 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003059 Register o_h = out.AsRegisterPairHigh<Register>();
3060 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003061
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003062 Register temp = locations->GetTemp(0).AsRegister<Register>();
3063
3064 Register high = first.AsRegisterPairHigh<Register>();
3065 Register low = first.AsRegisterPairLow<Register>();
3066
3067 Register second_reg = second.AsRegister<Register>();
3068
Calin Juravle9aec02f2014-11-18 23:06:35 +00003069 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003070 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003071 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003072 __ Lsl(o_h, high, o_l);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003073 // Shift the low part and `or` what overflew on the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003074 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003075 __ Lsr(temp, low, temp);
3076 __ orr(o_h, o_h, ShifterOperand(temp));
3077 // If the shift is > 32 bits, override the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003078 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003079 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003080 __ Lsl(o_h, low, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003081 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003082 __ Lsl(o_l, low, o_l);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003083 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003084 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003085 // Shift the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003086 __ Lsr(o_l, low, o_h);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003087 // Shift the high part and `or` what underflew on the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003088 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003089 __ Lsl(temp, high, temp);
3090 __ orr(o_l, o_l, ShifterOperand(temp));
3091 // If the shift is > 32 bits, override the low part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003092 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003093 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003094 __ Asr(o_l, high, temp, PL);
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003095 // Shift the high part
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003096 __ Asr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003097 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003098 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003099 // same as Shr except we use `Lsr`s and not `Asr`s
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003100 __ Lsr(o_l, low, o_h);
3101 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003102 __ Lsl(temp, high, temp);
3103 __ orr(o_l, o_l, ShifterOperand(temp));
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003104 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003105 __ it(PL);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003106 __ Lsr(o_l, high, temp, PL);
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003107 __ Lsr(o_h, high, o_h);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003108 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003109 break;
3110 }
3111 default:
3112 LOG(FATAL) << "Unexpected operation type " << type;
3113 }
3114}
3115
3116void LocationsBuilderARM::VisitShl(HShl* shl) {
3117 HandleShift(shl);
3118}
3119
3120void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3121 HandleShift(shl);
3122}
3123
3124void LocationsBuilderARM::VisitShr(HShr* shr) {
3125 HandleShift(shr);
3126}
3127
3128void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3129 HandleShift(shr);
3130}
3131
3132void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3133 HandleShift(ushr);
3134}
3135
3136void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3137 HandleShift(ushr);
3138}
3139
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003140void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003141 LocationSummary* locations =
3142 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003144 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003145 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003146 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003147}
3148
3149void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3150 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003151 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003152 // Note: if heap poisoning is enabled, the entry point takes cares
3153 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003154 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003155 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003156 instruction->GetDexPc(),
3157 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003158}
3159
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003160void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3161 LocationSummary* locations =
3162 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3163 InvokeRuntimeCallingConvention calling_convention;
3164 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003165 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003166 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003167 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003168}
3169
3170void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3171 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003172 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003173 // Note: if heap poisoning is enabled, the entry point takes cares
3174 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003175 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003176 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003177 instruction->GetDexPc(),
3178 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003179}
3180
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003181void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003182 LocationSummary* locations =
3183 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003184 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3185 if (location.IsStackSlot()) {
3186 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3187 } else if (location.IsDoubleStackSlot()) {
3188 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003189 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003190 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003191}
3192
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003193void InstructionCodeGeneratorARM::VisitParameterValue(
3194 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003195 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003196}
3197
3198void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3199 LocationSummary* locations =
3200 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3201 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3202}
3203
3204void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3205 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003206}
3207
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003208void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003209 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003210 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003211 locations->SetInAt(0, Location::RequiresRegister());
3212 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003213}
3214
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003215void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3216 LocationSummary* locations = not_->GetLocations();
3217 Location out = locations->Out();
3218 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003219 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003220 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003221 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003222 break;
3223
3224 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003225 __ mvn(out.AsRegisterPairLow<Register>(),
3226 ShifterOperand(in.AsRegisterPairLow<Register>()));
3227 __ mvn(out.AsRegisterPairHigh<Register>(),
3228 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003229 break;
3230
3231 default:
3232 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3233 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003234}
3235
David Brazdil66d126e2015-04-03 16:02:44 +01003236void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3237 LocationSummary* locations =
3238 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3239 locations->SetInAt(0, Location::RequiresRegister());
3240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3241}
3242
3243void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003244 LocationSummary* locations = bool_not->GetLocations();
3245 Location out = locations->Out();
3246 Location in = locations->InAt(0);
3247 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3248}
3249
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003250void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003251 LocationSummary* locations =
3252 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003253 switch (compare->InputAt(0)->GetType()) {
3254 case Primitive::kPrimLong: {
3255 locations->SetInAt(0, Location::RequiresRegister());
3256 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003257 // Output overlaps because it is written before doing the low comparison.
3258 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003259 break;
3260 }
3261 case Primitive::kPrimFloat:
3262 case Primitive::kPrimDouble: {
3263 locations->SetInAt(0, Location::RequiresFpuRegister());
3264 locations->SetInAt(1, Location::RequiresFpuRegister());
3265 locations->SetOut(Location::RequiresRegister());
3266 break;
3267 }
3268 default:
3269 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3270 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003271}
3272
3273void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003274 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003275 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003276 Location left = locations->InAt(0);
3277 Location right = locations->InAt(1);
3278
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003279 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003280 Primitive::Type type = compare->InputAt(0)->GetType();
3281 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003282 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003283 __ cmp(left.AsRegisterPairHigh<Register>(),
3284 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003285 __ b(&less, LT);
3286 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003287 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003288 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003289 __ cmp(left.AsRegisterPairLow<Register>(),
3290 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003291 break;
3292 }
3293 case Primitive::kPrimFloat:
3294 case Primitive::kPrimDouble: {
3295 __ LoadImmediate(out, 0);
3296 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003297 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003298 } else {
3299 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3300 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3301 }
3302 __ vmstat(); // transfer FP status register to ARM APSR.
3303 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003304 break;
3305 }
3306 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003307 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003308 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003309 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003310 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003311
3312 __ Bind(&greater);
3313 __ LoadImmediate(out, 1);
3314 __ b(&done);
3315
3316 __ Bind(&less);
3317 __ LoadImmediate(out, -1);
3318
3319 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003320}
3321
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003322void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003323 LocationSummary* locations =
3324 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003325 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3326 locations->SetInAt(i, Location::Any());
3327 }
3328 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003329}
3330
3331void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003332 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003333 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003334}
3335
Calin Juravle52c48962014-12-16 17:02:57 +00003336void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3337 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003338 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003339 switch (kind) {
3340 case MemBarrierKind::kAnyStore:
3341 case MemBarrierKind::kLoadAny:
3342 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003343 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003344 break;
3345 }
3346 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003347 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003348 break;
3349 }
3350 default:
3351 LOG(FATAL) << "Unexpected memory barrier " << kind;
3352 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003353 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003354}
3355
3356void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3357 uint32_t offset,
3358 Register out_lo,
3359 Register out_hi) {
3360 if (offset != 0) {
3361 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003362 __ add(IP, addr, ShifterOperand(out_lo));
3363 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003364 }
3365 __ ldrexd(out_lo, out_hi, addr);
3366}
3367
3368void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3369 uint32_t offset,
3370 Register value_lo,
3371 Register value_hi,
3372 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003373 Register temp2,
3374 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003375 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003376 if (offset != 0) {
3377 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003378 __ add(IP, addr, ShifterOperand(temp1));
3379 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003380 }
3381 __ Bind(&fail);
3382 // We need a load followed by store. (The address used in a STREX instruction must
3383 // be the same as the address in the most recently executed LDREX instruction.)
3384 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003385 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003386 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003387 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003388}
3389
3390void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3391 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3392
Nicolas Geoffray39468442014-09-02 15:17:15 +01003393 LocationSummary* locations =
3394 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003395 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003396
Calin Juravle52c48962014-12-16 17:02:57 +00003397 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003398 if (Primitive::IsFloatingPointType(field_type)) {
3399 locations->SetInAt(1, Location::RequiresFpuRegister());
3400 } else {
3401 locations->SetInAt(1, Location::RequiresRegister());
3402 }
3403
Calin Juravle52c48962014-12-16 17:02:57 +00003404 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003405 bool generate_volatile = field_info.IsVolatile()
3406 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003407 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003408 bool needs_write_barrier =
3409 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003410 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003411 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003412 if (needs_write_barrier) {
3413 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003414 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003415 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003416 // Arm encoding have some additional constraints for ldrexd/strexd:
3417 // - registers need to be consecutive
3418 // - the first register should be even but not R14.
3419 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3420 // enable Arm encoding.
3421 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3422
3423 locations->AddTemp(Location::RequiresRegister());
3424 locations->AddTemp(Location::RequiresRegister());
3425 if (field_type == Primitive::kPrimDouble) {
3426 // For doubles we need two more registers to copy the value.
3427 locations->AddTemp(Location::RegisterLocation(R2));
3428 locations->AddTemp(Location::RegisterLocation(R3));
3429 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003430 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003431}
3432
Calin Juravle52c48962014-12-16 17:02:57 +00003433void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003434 const FieldInfo& field_info,
3435 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003436 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3437
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003438 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003439 Register base = locations->InAt(0).AsRegister<Register>();
3440 Location value = locations->InAt(1);
3441
3442 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003443 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003444 Primitive::Type field_type = field_info.GetFieldType();
3445 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003446 bool needs_write_barrier =
3447 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003448
3449 if (is_volatile) {
3450 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3451 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003452
3453 switch (field_type) {
3454 case Primitive::kPrimBoolean:
3455 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003456 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003457 break;
3458 }
3459
3460 case Primitive::kPrimShort:
3461 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003462 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003463 break;
3464 }
3465
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003466 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003467 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003468 if (kPoisonHeapReferences && needs_write_barrier) {
3469 // Note that in the case where `value` is a null reference,
3470 // we do not enter this block, as a null reference does not
3471 // need poisoning.
3472 DCHECK_EQ(field_type, Primitive::kPrimNot);
3473 Register temp = locations->GetTemp(0).AsRegister<Register>();
3474 __ Mov(temp, value.AsRegister<Register>());
3475 __ PoisonHeapReference(temp);
3476 __ StoreToOffset(kStoreWord, temp, base, offset);
3477 } else {
3478 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3479 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003480 break;
3481 }
3482
3483 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003484 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003485 GenerateWideAtomicStore(base, offset,
3486 value.AsRegisterPairLow<Register>(),
3487 value.AsRegisterPairHigh<Register>(),
3488 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003489 locations->GetTemp(1).AsRegister<Register>(),
3490 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003491 } else {
3492 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003493 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003494 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003495 break;
3496 }
3497
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003498 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003499 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003500 break;
3501 }
3502
3503 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003504 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003505 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003506 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3507 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3508
3509 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3510
3511 GenerateWideAtomicStore(base, offset,
3512 value_reg_lo,
3513 value_reg_hi,
3514 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003515 locations->GetTemp(3).AsRegister<Register>(),
3516 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003517 } else {
3518 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003519 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003520 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003521 break;
3522 }
3523
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003524 case Primitive::kPrimVoid:
3525 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003526 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003527 }
Calin Juravle52c48962014-12-16 17:02:57 +00003528
Calin Juravle77520bc2015-01-12 18:45:46 +00003529 // Longs and doubles are handled in the switch.
3530 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3531 codegen_->MaybeRecordImplicitNullCheck(instruction);
3532 }
3533
3534 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3535 Register temp = locations->GetTemp(0).AsRegister<Register>();
3536 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003537 codegen_->MarkGCCard(
3538 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003539 }
3540
Calin Juravle52c48962014-12-16 17:02:57 +00003541 if (is_volatile) {
3542 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3543 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003544}
3545
Calin Juravle52c48962014-12-16 17:02:57 +00003546void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3547 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003548 LocationSummary* locations =
3549 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003550 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003551
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003552 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003553 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003554 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003555 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003556
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003557 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3558 locations->SetOut(Location::RequiresFpuRegister());
3559 } else {
3560 locations->SetOut(Location::RequiresRegister(),
3561 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3562 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003563 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003564 // Arm encoding have some additional constraints for ldrexd/strexd:
3565 // - registers need to be consecutive
3566 // - the first register should be even but not R14.
3567 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3568 // enable Arm encoding.
3569 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3570 locations->AddTemp(Location::RequiresRegister());
3571 locations->AddTemp(Location::RequiresRegister());
3572 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003573}
3574
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003575Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3576 Opcode opcode) {
3577 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3578 if (constant->IsConstant() &&
3579 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3580 return Location::ConstantLocation(constant->AsConstant());
3581 }
3582 return Location::RequiresRegister();
3583}
3584
3585bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3586 Opcode opcode) {
3587 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3588 if (Primitive::Is64BitType(input_cst->GetType())) {
3589 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3590 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3591 } else {
3592 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3593 }
3594}
3595
3596bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3597 ShifterOperand so;
3598 ArmAssembler* assembler = codegen_->GetAssembler();
3599 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3600 return true;
3601 }
3602 Opcode neg_opcode = kNoOperand;
3603 switch (opcode) {
3604 case AND:
3605 neg_opcode = BIC;
3606 break;
3607 case ORR:
3608 neg_opcode = ORN;
3609 break;
3610 default:
3611 return false;
3612 }
3613 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
3614}
3615
Calin Juravle52c48962014-12-16 17:02:57 +00003616void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3617 const FieldInfo& field_info) {
3618 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003619
Calin Juravle52c48962014-12-16 17:02:57 +00003620 LocationSummary* locations = instruction->GetLocations();
3621 Register base = locations->InAt(0).AsRegister<Register>();
3622 Location out = locations->Out();
3623 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003624 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003625 Primitive::Type field_type = field_info.GetFieldType();
3626 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3627
3628 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003629 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003630 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003631 break;
3632 }
3633
3634 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003635 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003636 break;
3637 }
3638
3639 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003640 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003641 break;
3642 }
3643
3644 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003645 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003646 break;
3647 }
3648
3649 case Primitive::kPrimInt:
3650 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003651 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003652 break;
3653 }
3654
3655 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003656 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003657 GenerateWideAtomicLoad(base, offset,
3658 out.AsRegisterPairLow<Register>(),
3659 out.AsRegisterPairHigh<Register>());
3660 } else {
3661 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3662 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003663 break;
3664 }
3665
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003666 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003667 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003668 break;
3669 }
3670
3671 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003672 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003673 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003674 Register lo = locations->GetTemp(0).AsRegister<Register>();
3675 Register hi = locations->GetTemp(1).AsRegister<Register>();
3676 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003678 __ vmovdrr(out_reg, lo, hi);
3679 } else {
3680 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003681 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003682 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003683 break;
3684 }
3685
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003686 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003687 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003688 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003689 }
Calin Juravle52c48962014-12-16 17:02:57 +00003690
Calin Juravle77520bc2015-01-12 18:45:46 +00003691 // Doubles are handled in the switch.
3692 if (field_type != Primitive::kPrimDouble) {
3693 codegen_->MaybeRecordImplicitNullCheck(instruction);
3694 }
3695
Calin Juravle52c48962014-12-16 17:02:57 +00003696 if (is_volatile) {
3697 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3698 }
Roland Levillain4d027112015-07-01 15:41:14 +01003699
3700 if (field_type == Primitive::kPrimNot) {
3701 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3702 }
Calin Juravle52c48962014-12-16 17:02:57 +00003703}
3704
3705void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3706 HandleFieldSet(instruction, instruction->GetFieldInfo());
3707}
3708
3709void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003710 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003711}
3712
3713void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3714 HandleFieldGet(instruction, instruction->GetFieldInfo());
3715}
3716
3717void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3718 HandleFieldGet(instruction, instruction->GetFieldInfo());
3719}
3720
3721void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3722 HandleFieldGet(instruction, instruction->GetFieldInfo());
3723}
3724
3725void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3726 HandleFieldGet(instruction, instruction->GetFieldInfo());
3727}
3728
3729void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3730 HandleFieldSet(instruction, instruction->GetFieldInfo());
3731}
3732
3733void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003734 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003735}
3736
Calin Juravlee460d1d2015-09-29 04:52:17 +01003737void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
3738 HUnresolvedInstanceFieldGet* instruction) {
3739 FieldAccessCallingConventionARM calling_convention;
3740 codegen_->CreateUnresolvedFieldLocationSummary(
3741 instruction, instruction->GetFieldType(), calling_convention);
3742}
3743
3744void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
3745 HUnresolvedInstanceFieldGet* instruction) {
3746 FieldAccessCallingConventionARM calling_convention;
3747 codegen_->GenerateUnresolvedFieldAccess(instruction,
3748 instruction->GetFieldType(),
3749 instruction->GetFieldIndex(),
3750 instruction->GetDexPc(),
3751 calling_convention);
3752}
3753
3754void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
3755 HUnresolvedInstanceFieldSet* instruction) {
3756 FieldAccessCallingConventionARM calling_convention;
3757 codegen_->CreateUnresolvedFieldLocationSummary(
3758 instruction, instruction->GetFieldType(), calling_convention);
3759}
3760
3761void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
3762 HUnresolvedInstanceFieldSet* instruction) {
3763 FieldAccessCallingConventionARM calling_convention;
3764 codegen_->GenerateUnresolvedFieldAccess(instruction,
3765 instruction->GetFieldType(),
3766 instruction->GetFieldIndex(),
3767 instruction->GetDexPc(),
3768 calling_convention);
3769}
3770
3771void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
3772 HUnresolvedStaticFieldGet* instruction) {
3773 FieldAccessCallingConventionARM calling_convention;
3774 codegen_->CreateUnresolvedFieldLocationSummary(
3775 instruction, instruction->GetFieldType(), calling_convention);
3776}
3777
3778void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
3779 HUnresolvedStaticFieldGet* instruction) {
3780 FieldAccessCallingConventionARM calling_convention;
3781 codegen_->GenerateUnresolvedFieldAccess(instruction,
3782 instruction->GetFieldType(),
3783 instruction->GetFieldIndex(),
3784 instruction->GetDexPc(),
3785 calling_convention);
3786}
3787
3788void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
3789 HUnresolvedStaticFieldSet* instruction) {
3790 FieldAccessCallingConventionARM calling_convention;
3791 codegen_->CreateUnresolvedFieldLocationSummary(
3792 instruction, instruction->GetFieldType(), calling_convention);
3793}
3794
3795void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
3796 HUnresolvedStaticFieldSet* instruction) {
3797 FieldAccessCallingConventionARM calling_convention;
3798 codegen_->GenerateUnresolvedFieldAccess(instruction,
3799 instruction->GetFieldType(),
3800 instruction->GetFieldIndex(),
3801 instruction->GetDexPc(),
3802 calling_convention);
3803}
3804
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003805void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003806 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3807 ? LocationSummary::kCallOnSlowPath
3808 : LocationSummary::kNoCall;
3809 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003810 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003811 if (instruction->HasUses()) {
3812 locations->SetOut(Location::SameAsFirstInput());
3813 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003814}
3815
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003816void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003817 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3818 return;
3819 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003820 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003821
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003822 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3823 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3824}
3825
3826void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003827 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003828 codegen_->AddSlowPath(slow_path);
3829
3830 LocationSummary* locations = instruction->GetLocations();
3831 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003832
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003833 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003834}
3835
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003836void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003837 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003838 GenerateImplicitNullCheck(instruction);
3839 } else {
3840 GenerateExplicitNullCheck(instruction);
3841 }
3842}
3843
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003844void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003845 LocationSummary* locations =
3846 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003847 locations->SetInAt(0, Location::RequiresRegister());
3848 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003849 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3850 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3851 } else {
3852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3853 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003854}
3855
3856void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3857 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003858 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003859 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003860 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003861
Roland Levillain4d027112015-07-01 15:41:14 +01003862 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003863 case Primitive::kPrimBoolean: {
3864 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003865 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003866 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003867 size_t offset =
3868 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003869 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3870 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003871 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003872 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3873 }
3874 break;
3875 }
3876
3877 case Primitive::kPrimByte: {
3878 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003879 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003880 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003881 size_t offset =
3882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003883 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3884 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003885 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003886 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3887 }
3888 break;
3889 }
3890
3891 case Primitive::kPrimShort: {
3892 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003893 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003894 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003895 size_t offset =
3896 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003897 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3898 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003899 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003900 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3901 }
3902 break;
3903 }
3904
3905 case Primitive::kPrimChar: {
3906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003907 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003908 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003909 size_t offset =
3910 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3912 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003913 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003914 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3915 }
3916 break;
3917 }
3918
3919 case Primitive::kPrimInt:
3920 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003921 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3922 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003923 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003924 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003925 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003926 size_t offset =
3927 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003928 __ LoadFromOffset(kLoadWord, out, obj, offset);
3929 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003930 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3932 }
3933 break;
3934 }
3935
3936 case Primitive::kPrimLong: {
3937 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003938 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003939 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003940 size_t offset =
3941 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003942 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003943 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003944 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003945 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003946 }
3947 break;
3948 }
3949
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003950 case Primitive::kPrimFloat: {
3951 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3952 Location out = locations->Out();
3953 DCHECK(out.IsFpuRegister());
3954 if (index.IsConstant()) {
3955 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3956 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
3957 } else {
3958 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
3959 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
3960 }
3961 break;
3962 }
3963
3964 case Primitive::kPrimDouble: {
3965 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3966 Location out = locations->Out();
3967 DCHECK(out.IsFpuRegisterPair());
3968 if (index.IsConstant()) {
3969 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3970 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
3971 } else {
3972 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
3973 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
3974 }
3975 break;
3976 }
3977
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003978 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01003979 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003980 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003981 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01003983
3984 if (type == Primitive::kPrimNot) {
3985 Register out = locations->Out().AsRegister<Register>();
3986 __ MaybeUnpoisonHeapReference(out);
3987 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003988}
3989
3990void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003991 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003992
3993 bool needs_write_barrier =
3994 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003995 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003996
Nicolas Geoffray39468442014-09-02 15:17:15 +01003997 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003998 instruction,
3999 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
4000 locations->SetInAt(0, Location::RequiresRegister());
4001 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4002 if (Primitive::IsFloatingPointType(value_type)) {
4003 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004004 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004005 locations->SetInAt(2, Location::RequiresRegister());
4006 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004007
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004008 if (needs_write_barrier) {
4009 // Temporary registers for the write barrier.
4010 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4011 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004012 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004013}
4014
4015void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4016 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004017 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004018 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004019 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004020 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004021 bool needs_write_barrier =
4022 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004023
4024 switch (value_type) {
4025 case Primitive::kPrimBoolean:
4026 case Primitive::kPrimByte: {
4027 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004028 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004029 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004030 size_t offset =
4031 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004032 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004033 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004034 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004035 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4036 }
4037 break;
4038 }
4039
4040 case Primitive::kPrimShort:
4041 case Primitive::kPrimChar: {
4042 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004044 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004045 size_t offset =
4046 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004047 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004048 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004049 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004050 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4051 }
4052 break;
4053 }
4054
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004055 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004056 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4057 Register value = locations->InAt(2).AsRegister<Register>();
4058 Register source = value;
4059
4060 if (instruction->InputAt(2)->IsNullConstant()) {
4061 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004062 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004063 size_t offset =
4064 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004065 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004066 } else {
4067 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004068 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004069 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004070 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004071 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004072 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004073
4074 DCHECK(needs_write_barrier);
4075 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4076 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4077 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4078 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4079 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4080 Label done;
4081 SlowPathCode* slow_path = nullptr;
4082
4083 if (may_need_runtime_call) {
4084 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4085 codegen_->AddSlowPath(slow_path);
4086 if (instruction->GetValueCanBeNull()) {
4087 Label non_zero;
4088 __ CompareAndBranchIfNonZero(value, &non_zero);
4089 if (index.IsConstant()) {
4090 size_t offset =
4091 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4092 __ StoreToOffset(kStoreWord, value, array, offset);
4093 } else {
4094 DCHECK(index.IsRegister()) << index;
4095 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4096 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4097 }
4098 codegen_->MaybeRecordImplicitNullCheck(instruction);
4099 __ b(&done);
4100 __ Bind(&non_zero);
4101 }
4102
4103 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4104 codegen_->MaybeRecordImplicitNullCheck(instruction);
4105 __ MaybeUnpoisonHeapReference(temp1);
4106 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4107 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4108 // No need to poison/unpoison, we're comparing two poisoined references.
4109 __ cmp(temp1, ShifterOperand(temp2));
4110 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4111 Label do_put;
4112 __ b(&do_put, EQ);
4113 __ MaybeUnpoisonHeapReference(temp1);
4114 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4115 // No need to poison/unpoison, we're comparing against null.
4116 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4117 __ Bind(&do_put);
4118 } else {
4119 __ b(slow_path->GetEntryLabel(), NE);
4120 }
4121 }
4122
4123 if (kPoisonHeapReferences) {
4124 // Note that in the case where `value` is a null reference,
4125 // we do not enter this block, as a null reference does not
4126 // need poisoning.
4127 DCHECK_EQ(value_type, Primitive::kPrimNot);
4128 __ Mov(temp1, value);
4129 __ PoisonHeapReference(temp1);
4130 source = temp1;
4131 }
4132
4133 if (index.IsConstant()) {
4134 size_t offset =
4135 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4136 __ StoreToOffset(kStoreWord, source, array, offset);
4137 } else {
4138 DCHECK(index.IsRegister()) << index;
4139 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4140 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4141 }
4142
4143 if (!may_need_runtime_call) {
4144 codegen_->MaybeRecordImplicitNullCheck(instruction);
4145 }
4146
4147 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4148
4149 if (done.IsLinked()) {
4150 __ Bind(&done);
4151 }
4152
4153 if (slow_path != nullptr) {
4154 __ Bind(slow_path->GetExitLabel());
4155 }
4156
4157 break;
4158 }
4159
4160 case Primitive::kPrimInt: {
4161 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4162 Register value = locations->InAt(2).AsRegister<Register>();
4163 if (index.IsConstant()) {
4164 size_t offset =
4165 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4166 __ StoreToOffset(kStoreWord, value, array, offset);
4167 } else {
4168 DCHECK(index.IsRegister()) << index;
4169 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4170 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4171 }
4172
4173 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004174 break;
4175 }
4176
4177 case Primitive::kPrimLong: {
4178 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004179 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004180 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004181 size_t offset =
4182 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004183 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004184 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004185 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004186 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004187 }
4188 break;
4189 }
4190
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004191 case Primitive::kPrimFloat: {
4192 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4193 Location value = locations->InAt(2);
4194 DCHECK(value.IsFpuRegister());
4195 if (index.IsConstant()) {
4196 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004197 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004198 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004199 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004200 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4201 }
4202 break;
4203 }
4204
4205 case Primitive::kPrimDouble: {
4206 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4207 Location value = locations->InAt(2);
4208 DCHECK(value.IsFpuRegisterPair());
4209 if (index.IsConstant()) {
4210 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004211 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004212 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004213 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004214 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4215 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004216
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004217 break;
4218 }
4219
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004220 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004221 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004222 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004223 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004224
4225 // Ints and objects are handled in the switch.
4226 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4227 codegen_->MaybeRecordImplicitNullCheck(instruction);
4228 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004229}
4230
4231void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004232 LocationSummary* locations =
4233 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004234 locations->SetInAt(0, Location::RequiresRegister());
4235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004236}
4237
4238void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4239 LocationSummary* locations = instruction->GetLocations();
4240 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004241 Register obj = locations->InAt(0).AsRegister<Register>();
4242 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004243 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004244 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004245}
4246
4247void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004248 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4249 ? LocationSummary::kCallOnSlowPath
4250 : LocationSummary::kNoCall;
4251 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004252 locations->SetInAt(0, Location::RequiresRegister());
4253 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004254 if (instruction->HasUses()) {
4255 locations->SetOut(Location::SameAsFirstInput());
4256 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004257}
4258
4259void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4260 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004261 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004262 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263 codegen_->AddSlowPath(slow_path);
4264
Roland Levillain271ab9c2014-11-27 15:23:57 +00004265 Register index = locations->InAt(0).AsRegister<Register>();
4266 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004267
4268 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004269 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004270}
4271
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004272void CodeGeneratorARM::MarkGCCard(Register temp,
4273 Register card,
4274 Register object,
4275 Register value,
4276 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004277 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004278 if (can_be_null) {
4279 __ CompareAndBranchIfZero(value, &is_null);
4280 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004281 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4282 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4283 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004284 if (can_be_null) {
4285 __ Bind(&is_null);
4286 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004287}
4288
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4290 temp->SetLocations(nullptr);
4291}
4292
4293void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp) {
4294 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004295 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004296}
4297
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004298void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004299 UNUSED(instruction);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004300 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004301}
4302
4303void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004304 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4305}
4306
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004307void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4308 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4309}
4310
4311void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004312 HBasicBlock* block = instruction->GetBlock();
4313 if (block->GetLoopInformation() != nullptr) {
4314 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4315 // The back edge will generate the suspend check.
4316 return;
4317 }
4318 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4319 // The goto will generate the suspend check.
4320 return;
4321 }
4322 GenerateSuspendCheck(instruction, nullptr);
4323}
4324
4325void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4326 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004327 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004328 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4329 if (slow_path == nullptr) {
4330 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4331 instruction->SetSlowPath(slow_path);
4332 codegen_->AddSlowPath(slow_path);
4333 if (successor != nullptr) {
4334 DCHECK(successor->IsLoopHeader());
4335 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4336 }
4337 } else {
4338 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4339 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004340
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004341 __ LoadFromOffset(
4342 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004343 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004344 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004345 __ Bind(slow_path->GetReturnLabel());
4346 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004347 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004348 __ b(slow_path->GetEntryLabel());
4349 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004350}
4351
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004352ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4353 return codegen_->GetAssembler();
4354}
4355
4356void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004357 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004358 Location source = move->GetSource();
4359 Location destination = move->GetDestination();
4360
4361 if (source.IsRegister()) {
4362 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004363 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004364 } else {
4365 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004366 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004367 SP, destination.GetStackIndex());
4368 }
4369 } else if (source.IsStackSlot()) {
4370 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004371 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004372 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004373 } else if (destination.IsFpuRegister()) {
4374 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004375 } else {
4376 DCHECK(destination.IsStackSlot());
4377 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4378 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4379 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004380 } else if (source.IsFpuRegister()) {
4381 if (destination.IsFpuRegister()) {
4382 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004383 } else {
4384 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004385 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4386 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004387 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004388 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004389 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4390 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004391 } else if (destination.IsRegisterPair()) {
4392 DCHECK(ExpectedPairLayout(destination));
4393 __ LoadFromOffset(
4394 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4395 } else {
4396 DCHECK(destination.IsFpuRegisterPair()) << destination;
4397 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4398 SP,
4399 source.GetStackIndex());
4400 }
4401 } else if (source.IsRegisterPair()) {
4402 if (destination.IsRegisterPair()) {
4403 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4404 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4405 } else {
4406 DCHECK(destination.IsDoubleStackSlot()) << destination;
4407 DCHECK(ExpectedPairLayout(source));
4408 __ StoreToOffset(
4409 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4410 }
4411 } else if (source.IsFpuRegisterPair()) {
4412 if (destination.IsFpuRegisterPair()) {
4413 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4414 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4415 } else {
4416 DCHECK(destination.IsDoubleStackSlot()) << destination;
4417 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4418 SP,
4419 destination.GetStackIndex());
4420 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004421 } else {
4422 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004423 HConstant* constant = source.GetConstant();
4424 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4425 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004426 if (destination.IsRegister()) {
4427 __ LoadImmediate(destination.AsRegister<Register>(), value);
4428 } else {
4429 DCHECK(destination.IsStackSlot());
4430 __ LoadImmediate(IP, value);
4431 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4432 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004433 } else if (constant->IsLongConstant()) {
4434 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004435 if (destination.IsRegisterPair()) {
4436 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4437 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004438 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004439 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004440 __ LoadImmediate(IP, Low32Bits(value));
4441 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4442 __ LoadImmediate(IP, High32Bits(value));
4443 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4444 }
4445 } else if (constant->IsDoubleConstant()) {
4446 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004447 if (destination.IsFpuRegisterPair()) {
4448 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004449 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004450 DCHECK(destination.IsDoubleStackSlot()) << destination;
4451 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004452 __ LoadImmediate(IP, Low32Bits(int_value));
4453 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4454 __ LoadImmediate(IP, High32Bits(int_value));
4455 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4456 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004457 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004458 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004459 float value = constant->AsFloatConstant()->GetValue();
4460 if (destination.IsFpuRegister()) {
4461 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4462 } else {
4463 DCHECK(destination.IsStackSlot());
4464 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4465 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4466 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004467 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004468 }
4469}
4470
4471void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4472 __ Mov(IP, reg);
4473 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4474 __ StoreToOffset(kStoreWord, IP, SP, mem);
4475}
4476
4477void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4478 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4479 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4480 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4481 SP, mem1 + stack_offset);
4482 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4483 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4484 SP, mem2 + stack_offset);
4485 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4486}
4487
4488void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004489 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004490 Location source = move->GetSource();
4491 Location destination = move->GetDestination();
4492
4493 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004494 DCHECK_NE(source.AsRegister<Register>(), IP);
4495 DCHECK_NE(destination.AsRegister<Register>(), IP);
4496 __ Mov(IP, source.AsRegister<Register>());
4497 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4498 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004499 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004500 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004501 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004502 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004503 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4504 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004505 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004506 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004507 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004508 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004509 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004510 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004511 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004512 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004513 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4514 destination.AsRegisterPairHigh<Register>(),
4515 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004516 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004517 Register low_reg = source.IsRegisterPair()
4518 ? source.AsRegisterPairLow<Register>()
4519 : destination.AsRegisterPairLow<Register>();
4520 int mem = source.IsRegisterPair()
4521 ? destination.GetStackIndex()
4522 : source.GetStackIndex();
4523 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004524 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004525 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004526 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004527 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004528 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4529 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004530 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004531 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004532 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004533 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4534 DRegister reg = source.IsFpuRegisterPair()
4535 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4536 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4537 int mem = source.IsFpuRegisterPair()
4538 ? destination.GetStackIndex()
4539 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004540 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004541 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004542 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004543 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4544 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4545 : destination.AsFpuRegister<SRegister>();
4546 int mem = source.IsFpuRegister()
4547 ? destination.GetStackIndex()
4548 : source.GetStackIndex();
4549
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004550 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004551 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004552 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004553 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004554 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4555 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004556 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004557 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004558 }
4559}
4560
4561void ParallelMoveResolverARM::SpillScratch(int reg) {
4562 __ Push(static_cast<Register>(reg));
4563}
4564
4565void ParallelMoveResolverARM::RestoreScratch(int reg) {
4566 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004567}
4568
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004569void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004570 InvokeRuntimeCallingConvention calling_convention;
4571 CodeGenerator::CreateLoadClassLocationSummary(
4572 cls,
4573 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4574 Location::RegisterLocation(R0));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004575}
4576
4577void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004578 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004579 if (cls->NeedsAccessCheck()) {
4580 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4581 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4582 cls,
4583 cls->GetDexPc(),
4584 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004585 return;
4586 }
4587
4588 Register out = locations->Out().AsRegister<Register>();
4589 Register current_method = locations->InAt(0).AsRegister<Register>();
4590 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004591 DCHECK(!cls->CanCallRuntime());
4592 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004593 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004594 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004595 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004596 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004597 __ LoadFromOffset(kLoadWord,
4598 out,
4599 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004600 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004601 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004602 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004603
Andreas Gampe85b62f22015-09-09 13:15:38 -07004604 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004605 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4606 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004607 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004608 if (cls->MustGenerateClinitCheck()) {
4609 GenerateClassInitializationCheck(slow_path, out);
4610 } else {
4611 __ Bind(slow_path->GetExitLabel());
4612 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004613 }
4614}
4615
4616void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4617 LocationSummary* locations =
4618 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4619 locations->SetInAt(0, Location::RequiresRegister());
4620 if (check->HasUses()) {
4621 locations->SetOut(Location::SameAsFirstInput());
4622 }
4623}
4624
4625void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004626 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004627 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004628 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004629 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004630 GenerateClassInitializationCheck(slow_path,
4631 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004632}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004633
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004634void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004635 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004636 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4637 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4638 __ b(slow_path->GetEntryLabel(), LT);
4639 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4640 // properly. Therefore, we do a memory fence.
4641 __ dmb(ISH);
4642 __ Bind(slow_path->GetExitLabel());
4643}
4644
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004645void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4646 LocationSummary* locations =
4647 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004648 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004649 locations->SetOut(Location::RequiresRegister());
4650}
4651
4652void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004653 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004654 codegen_->AddSlowPath(slow_path);
4655
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004656 LocationSummary* locations = load->GetLocations();
4657 Register out = locations->Out().AsRegister<Register>();
4658 Register current_method = locations->InAt(0).AsRegister<Register>();
4659 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004660 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004661 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004662 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004663 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004664 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004665 __ Bind(slow_path->GetExitLabel());
4666}
4667
David Brazdilcb1c0552015-08-04 16:22:25 +01004668static int32_t GetExceptionTlsOffset() {
4669 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4670}
4671
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004672void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4673 LocationSummary* locations =
4674 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4675 locations->SetOut(Location::RequiresRegister());
4676}
4677
4678void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004679 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004680 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4681}
4682
4683void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4684 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4685}
4686
4687void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004688 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004689 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004690}
4691
4692void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4693 LocationSummary* locations =
4694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4695 InvokeRuntimeCallingConvention calling_convention;
4696 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4697}
4698
4699void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4700 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004701 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004702}
4703
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004704void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004705 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4706 switch (instruction->GetTypeCheckKind()) {
4707 case TypeCheckKind::kExactCheck:
4708 case TypeCheckKind::kAbstractClassCheck:
4709 case TypeCheckKind::kClassHierarchyCheck:
4710 case TypeCheckKind::kArrayObjectCheck:
4711 call_kind = LocationSummary::kNoCall;
4712 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004713 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004714 case TypeCheckKind::kInterfaceCheck:
4715 call_kind = LocationSummary::kCall;
4716 break;
4717 case TypeCheckKind::kArrayCheck:
4718 call_kind = LocationSummary::kCallOnSlowPath;
4719 break;
4720 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004721 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004722 if (call_kind != LocationSummary::kCall) {
4723 locations->SetInAt(0, Location::RequiresRegister());
4724 locations->SetInAt(1, Location::RequiresRegister());
4725 // The out register is used as a temporary, so it overlaps with the inputs.
4726 // Note that TypeCheckSlowPathARM uses this register too.
4727 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4728 } else {
4729 InvokeRuntimeCallingConvention calling_convention;
4730 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4731 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4732 locations->SetOut(Location::RegisterLocation(R0));
4733 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004734}
4735
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004736void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004737 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004738 Register obj = locations->InAt(0).AsRegister<Register>();
4739 Register cls = locations->InAt(1).AsRegister<Register>();
4740 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004741 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004742 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4743 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4744 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004745 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004746 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004747
4748 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004749 // avoid null check if we know obj is not null.
4750 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004751 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004752 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004753
Calin Juravle98893e12015-10-02 21:05:03 +01004754 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004755 // This is safe, as the register is caller-save, and the object must be in another
4756 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004757 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4758 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004759 ? obj
4760 : out;
4761 __ LoadFromOffset(kLoadWord, target, obj, class_offset);
4762 __ MaybeUnpoisonHeapReference(target);
4763
4764 switch (instruction->GetTypeCheckKind()) {
4765 case TypeCheckKind::kExactCheck: {
4766 __ cmp(out, ShifterOperand(cls));
4767 // Classes must be equal for the instanceof to succeed.
4768 __ b(&zero, NE);
4769 __ LoadImmediate(out, 1);
4770 __ b(&done);
4771 break;
4772 }
4773 case TypeCheckKind::kAbstractClassCheck: {
4774 // If the class is abstract, we eagerly fetch the super class of the
4775 // object to avoid doing a comparison we know will fail.
4776 Label loop;
4777 __ Bind(&loop);
4778 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4779 __ MaybeUnpoisonHeapReference(out);
4780 // If `out` is null, we use it for the result, and jump to `done`.
4781 __ CompareAndBranchIfZero(out, &done);
4782 __ cmp(out, ShifterOperand(cls));
4783 __ b(&loop, NE);
4784 __ LoadImmediate(out, 1);
4785 if (zero.IsLinked()) {
4786 __ b(&done);
4787 }
4788 break;
4789 }
4790 case TypeCheckKind::kClassHierarchyCheck: {
4791 // Walk over the class hierarchy to find a match.
4792 Label loop, success;
4793 __ Bind(&loop);
4794 __ cmp(out, ShifterOperand(cls));
4795 __ b(&success, EQ);
4796 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4797 __ MaybeUnpoisonHeapReference(out);
4798 __ CompareAndBranchIfNonZero(out, &loop);
4799 // If `out` is null, we use it for the result, and jump to `done`.
4800 __ b(&done);
4801 __ Bind(&success);
4802 __ LoadImmediate(out, 1);
4803 if (zero.IsLinked()) {
4804 __ b(&done);
4805 }
4806 break;
4807 }
4808 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004809 // Do an exact check.
4810 Label exact_check;
4811 __ cmp(out, ShifterOperand(cls));
4812 __ b(&exact_check, EQ);
4813 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004814 __ LoadFromOffset(kLoadWord, out, out, component_offset);
4815 __ MaybeUnpoisonHeapReference(out);
4816 // If `out` is null, we use it for the result, and jump to `done`.
4817 __ CompareAndBranchIfZero(out, &done);
4818 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4819 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4820 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004821 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004822 __ LoadImmediate(out, 1);
4823 __ b(&done);
4824 break;
4825 }
4826 case TypeCheckKind::kArrayCheck: {
4827 __ cmp(out, ShifterOperand(cls));
4828 DCHECK(locations->OnlyCallsOnSlowPath());
4829 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4830 instruction, /* is_fatal */ false);
4831 codegen_->AddSlowPath(slow_path);
4832 __ b(slow_path->GetEntryLabel(), NE);
4833 __ LoadImmediate(out, 1);
4834 if (zero.IsLinked()) {
4835 __ b(&done);
4836 }
4837 break;
4838 }
Calin Juravle98893e12015-10-02 21:05:03 +01004839 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004840 case TypeCheckKind::kInterfaceCheck:
4841 default: {
4842 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4843 instruction,
4844 instruction->GetDexPc(),
4845 nullptr);
4846 if (zero.IsLinked()) {
4847 __ b(&done);
4848 }
4849 break;
4850 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004851 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004852
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004853 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004854 __ Bind(&zero);
4855 __ LoadImmediate(out, 0);
4856 }
4857
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004858 if (done.IsLinked()) {
4859 __ Bind(&done);
4860 }
4861
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004862 if (slow_path != nullptr) {
4863 __ Bind(slow_path->GetExitLabel());
4864 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004865}
4866
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004867void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004868 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4869 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4870
4871 switch (instruction->GetTypeCheckKind()) {
4872 case TypeCheckKind::kExactCheck:
4873 case TypeCheckKind::kAbstractClassCheck:
4874 case TypeCheckKind::kClassHierarchyCheck:
4875 case TypeCheckKind::kArrayObjectCheck:
4876 call_kind = throws_into_catch
4877 ? LocationSummary::kCallOnSlowPath
4878 : LocationSummary::kNoCall;
4879 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004880 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004881 case TypeCheckKind::kInterfaceCheck:
4882 call_kind = LocationSummary::kCall;
4883 break;
4884 case TypeCheckKind::kArrayCheck:
4885 call_kind = LocationSummary::kCallOnSlowPath;
4886 break;
4887 }
4888
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004889 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004890 instruction, call_kind);
4891 if (call_kind != LocationSummary::kCall) {
4892 locations->SetInAt(0, Location::RequiresRegister());
4893 locations->SetInAt(1, Location::RequiresRegister());
4894 // Note that TypeCheckSlowPathARM uses this register too.
4895 locations->AddTemp(Location::RequiresRegister());
4896 } else {
4897 InvokeRuntimeCallingConvention calling_convention;
4898 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4899 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4900 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004901}
4902
4903void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4904 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004905 Register obj = locations->InAt(0).AsRegister<Register>();
4906 Register cls = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004907 Register temp = locations->WillCall()
4908 ? Register(kNoRegister)
4909 : locations->GetTemp(0).AsRegister<Register>();
4910
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004911 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004912 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4913 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4914 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4915 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004916
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004917 if (!locations->WillCall()) {
4918 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4919 instruction, !locations->CanCall());
4920 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004921 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004922
4923 Label done;
4924 // Avoid null check if we know obj is not null.
4925 if (instruction->MustDoNullCheck()) {
4926 __ CompareAndBranchIfZero(obj, &done);
4927 }
4928
4929 if (locations->WillCall()) {
4930 __ LoadFromOffset(kLoadWord, obj, obj, class_offset);
4931 __ MaybeUnpoisonHeapReference(obj);
4932 } else {
4933 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4934 __ MaybeUnpoisonHeapReference(temp);
4935 }
4936
4937 switch (instruction->GetTypeCheckKind()) {
4938 case TypeCheckKind::kExactCheck:
4939 case TypeCheckKind::kArrayCheck: {
4940 __ cmp(temp, ShifterOperand(cls));
4941 // Jump to slow path for throwing the exception or doing a
4942 // more involved array check.
4943 __ b(slow_path->GetEntryLabel(), NE);
4944 break;
4945 }
4946 case TypeCheckKind::kAbstractClassCheck: {
4947 // If the class is abstract, we eagerly fetch the super class of the
4948 // object to avoid doing a comparison we know will fail.
4949 Label loop;
4950 __ Bind(&loop);
4951 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4952 __ MaybeUnpoisonHeapReference(temp);
4953 // Jump to the slow path to throw the exception.
4954 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4955 __ cmp(temp, ShifterOperand(cls));
4956 __ b(&loop, NE);
4957 break;
4958 }
4959 case TypeCheckKind::kClassHierarchyCheck: {
4960 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004961 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004962 __ Bind(&loop);
4963 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004964 __ b(&done, EQ);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004965 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4966 __ MaybeUnpoisonHeapReference(temp);
4967 __ CompareAndBranchIfNonZero(temp, &loop);
4968 // Jump to the slow path to throw the exception.
4969 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004970 break;
4971 }
4972 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004973 // Do an exact check.
4974 __ cmp(temp, ShifterOperand(cls));
4975 __ b(&done, EQ);
4976 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004977 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
4978 __ MaybeUnpoisonHeapReference(temp);
4979 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
4980 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
4981 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4982 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
4983 break;
4984 }
Calin Juravle98893e12015-10-02 21:05:03 +01004985 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004986 case TypeCheckKind::kInterfaceCheck:
4987 default:
4988 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
4989 instruction,
4990 instruction->GetDexPc(),
4991 nullptr);
4992 break;
4993 }
4994 __ Bind(&done);
4995
4996 if (slow_path != nullptr) {
4997 __ Bind(slow_path->GetExitLabel());
4998 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004999}
5000
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005001void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5002 LocationSummary* locations =
5003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5004 InvokeRuntimeCallingConvention calling_convention;
5005 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5006}
5007
5008void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5009 codegen_->InvokeRuntime(instruction->IsEnter()
5010 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5011 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005012 instruction->GetDexPc(),
5013 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005014}
5015
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005016void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5017void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5018void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005019
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005020void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005021 LocationSummary* locations =
5022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5023 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5024 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005025 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005026 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005027 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005029}
5030
5031void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5032 HandleBitwiseOperation(instruction);
5033}
5034
5035void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5036 HandleBitwiseOperation(instruction);
5037}
5038
5039void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5040 HandleBitwiseOperation(instruction);
5041}
5042
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005043void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5044 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5045 if (value == 0xffffffffu) {
5046 if (out != first) {
5047 __ mov(out, ShifterOperand(first));
5048 }
5049 return;
5050 }
5051 if (value == 0u) {
5052 __ mov(out, ShifterOperand(0));
5053 return;
5054 }
5055 ShifterOperand so;
5056 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5057 __ and_(out, first, so);
5058 } else {
5059 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5060 __ bic(out, first, ShifterOperand(~value));
5061 }
5062}
5063
5064void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5065 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5066 if (value == 0u) {
5067 if (out != first) {
5068 __ mov(out, ShifterOperand(first));
5069 }
5070 return;
5071 }
5072 if (value == 0xffffffffu) {
5073 __ mvn(out, ShifterOperand(0));
5074 return;
5075 }
5076 ShifterOperand so;
5077 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5078 __ orr(out, first, so);
5079 } else {
5080 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5081 __ orn(out, first, ShifterOperand(~value));
5082 }
5083}
5084
5085void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5086 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5087 if (value == 0u) {
5088 if (out != first) {
5089 __ mov(out, ShifterOperand(first));
5090 }
5091 return;
5092 }
5093 __ eor(out, first, ShifterOperand(value));
5094}
5095
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005096void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5097 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005098 Location first = locations->InAt(0);
5099 Location second = locations->InAt(1);
5100 Location out = locations->Out();
5101
5102 if (second.IsConstant()) {
5103 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5104 uint32_t value_low = Low32Bits(value);
5105 if (instruction->GetResultType() == Primitive::kPrimInt) {
5106 Register first_reg = first.AsRegister<Register>();
5107 Register out_reg = out.AsRegister<Register>();
5108 if (instruction->IsAnd()) {
5109 GenerateAndConst(out_reg, first_reg, value_low);
5110 } else if (instruction->IsOr()) {
5111 GenerateOrrConst(out_reg, first_reg, value_low);
5112 } else {
5113 DCHECK(instruction->IsXor());
5114 GenerateEorConst(out_reg, first_reg, value_low);
5115 }
5116 } else {
5117 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5118 uint32_t value_high = High32Bits(value);
5119 Register first_low = first.AsRegisterPairLow<Register>();
5120 Register first_high = first.AsRegisterPairHigh<Register>();
5121 Register out_low = out.AsRegisterPairLow<Register>();
5122 Register out_high = out.AsRegisterPairHigh<Register>();
5123 if (instruction->IsAnd()) {
5124 GenerateAndConst(out_low, first_low, value_low);
5125 GenerateAndConst(out_high, first_high, value_high);
5126 } else if (instruction->IsOr()) {
5127 GenerateOrrConst(out_low, first_low, value_low);
5128 GenerateOrrConst(out_high, first_high, value_high);
5129 } else {
5130 DCHECK(instruction->IsXor());
5131 GenerateEorConst(out_low, first_low, value_low);
5132 GenerateEorConst(out_high, first_high, value_high);
5133 }
5134 }
5135 return;
5136 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005137
5138 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005139 Register first_reg = first.AsRegister<Register>();
5140 ShifterOperand second_reg(second.AsRegister<Register>());
5141 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005142 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005143 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005144 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005145 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005146 } else {
5147 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005148 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005149 }
5150 } else {
5151 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005152 Register first_low = first.AsRegisterPairLow<Register>();
5153 Register first_high = first.AsRegisterPairHigh<Register>();
5154 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5155 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5156 Register out_low = out.AsRegisterPairLow<Register>();
5157 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005158 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005159 __ and_(out_low, first_low, second_low);
5160 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005161 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005162 __ orr(out_low, first_low, second_low);
5163 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005164 } else {
5165 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005166 __ eor(out_low, first_low, second_low);
5167 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005168 }
5169 }
5170}
5171
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005172void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00005173 // For better instruction scheduling we load the direct code pointer before the method pointer.
5174 bool direct_code_loaded = false;
5175 switch (invoke->GetCodePtrLocation()) {
5176 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5177 if (IsSameDexFile(*invoke->GetTargetMethod().dex_file, GetGraph()->GetDexFile())) {
5178 break;
5179 }
5180 // Calls across dex files are more likely to exceed the available BL range,
5181 // so use absolute patch by falling through to kDirectCodeFixup.
5182 FALLTHROUGH_INTENDED;
5183 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5184 // LR = code address from literal pool with link-time patch.
5185 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5186 direct_code_loaded = true;
5187 break;
5188 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5189 // LR = invoke->GetDirectCodePtr();
5190 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
5191 direct_code_loaded = true;
5192 break;
5193 default:
5194 break;
5195 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005196
Vladimir Marko58155012015-08-19 12:49:41 +00005197 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5198 switch (invoke->GetMethodLoadKind()) {
5199 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5200 // temp = thread->string_init_entrypoint
5201 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5202 break;
5203 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
5204 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5205 break;
5206 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5207 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5208 break;
5209 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5210 __ LoadLiteral(temp.AsRegister<Register>(),
5211 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5212 break;
5213 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
5214 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5215 FALLTHROUGH_INTENDED;
5216 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5217 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5218 Register method_reg;
5219 Register reg = temp.AsRegister<Register>();
5220 if (current_method.IsRegister()) {
5221 method_reg = current_method.AsRegister<Register>();
5222 } else {
5223 DCHECK(invoke->GetLocations()->Intrinsified());
5224 DCHECK(!current_method.IsValid());
5225 method_reg = reg;
5226 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5227 }
5228 // temp = current_method->dex_cache_resolved_methods_;
5229 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01005230 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
5231 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005232 // temp = temp[index_in_cache]
5233 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5234 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5235 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005236 }
Vladimir Marko58155012015-08-19 12:49:41 +00005237 }
5238
5239 switch (invoke->GetCodePtrLocation()) {
5240 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5241 __ bl(GetFrameEntryLabel());
5242 break;
5243 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
5244 if (!direct_code_loaded) {
5245 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
5246 __ Bind(&relative_call_patches_.back().label);
5247 Label label;
5248 __ bl(&label); // Arbitrarily branch to the instruction after BL, override at link time.
5249 __ Bind(&label);
5250 break;
5251 }
5252 // If we loaded the direct code above, fall through.
5253 FALLTHROUGH_INTENDED;
5254 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5255 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5256 // LR prepared above for better instruction scheduling.
5257 DCHECK(direct_code_loaded);
5258 // LR()
5259 __ blx(LR);
5260 break;
5261 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5262 // LR = callee_method->entry_point_from_quick_compiled_code_
5263 __ LoadFromOffset(
5264 kLoadWord, LR, callee_method.AsRegister<Register>(),
5265 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5266 // LR()
5267 __ blx(LR);
5268 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005269 }
5270
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005271 DCHECK(!IsLeafMethod());
5272}
5273
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005274void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5275 Register temp = temp_location.AsRegister<Register>();
5276 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5277 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5278 LocationSummary* locations = invoke->GetLocations();
5279 Location receiver = locations->InAt(0);
5280 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5281 // temp = object->GetClass();
5282 DCHECK(receiver.IsRegister());
5283 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5284 MaybeRecordImplicitNullCheck(invoke);
5285 __ MaybeUnpoisonHeapReference(temp);
5286 // temp = temp->GetMethodAt(method_offset);
5287 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5288 kArmWordSize).Int32Value();
5289 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5290 // LR = temp->GetEntryPoint();
5291 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5292 // LR();
5293 __ blx(LR);
5294}
5295
Vladimir Marko58155012015-08-19 12:49:41 +00005296void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5297 DCHECK(linker_patches->empty());
5298 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5299 linker_patches->reserve(size);
5300 for (const auto& entry : method_patches_) {
5301 const MethodReference& target_method = entry.first;
5302 Literal* literal = entry.second;
5303 DCHECK(literal->GetLabel()->IsBound());
5304 uint32_t literal_offset = literal->GetLabel()->Position();
5305 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5306 target_method.dex_file,
5307 target_method.dex_method_index));
5308 }
5309 for (const auto& entry : call_patches_) {
5310 const MethodReference& target_method = entry.first;
5311 Literal* literal = entry.second;
5312 DCHECK(literal->GetLabel()->IsBound());
5313 uint32_t literal_offset = literal->GetLabel()->Position();
5314 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5315 target_method.dex_file,
5316 target_method.dex_method_index));
5317 }
5318 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5319 uint32_t literal_offset = info.label.Position();
5320 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5321 info.target_method.dex_file,
5322 info.target_method.dex_method_index));
5323 }
5324}
5325
5326Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5327 MethodToLiteralMap* map) {
5328 // Look up the literal for target_method.
5329 auto lb = map->lower_bound(target_method);
5330 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5331 return lb->second;
5332 }
5333 // We don't have a literal for this method yet, insert a new one.
5334 Literal* literal = __ NewLiteral<uint32_t>(0u);
5335 map->PutBefore(lb, target_method, literal);
5336 return literal;
5337}
5338
5339Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5340 return DeduplicateMethodLiteral(target_method, &method_patches_);
5341}
5342
5343Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5344 return DeduplicateMethodLiteral(target_method, &call_patches_);
5345}
5346
Calin Juravleb1498f62015-02-16 13:13:29 +00005347void LocationsBuilderARM::VisitBoundType(HBoundType* instruction) {
5348 // Nothing to do, this should be removed during prepare for register allocator.
5349 UNUSED(instruction);
5350 LOG(FATAL) << "Unreachable";
5351}
5352
5353void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction) {
5354 // Nothing to do, this should be removed during prepare for register allocator.
5355 UNUSED(instruction);
5356 LOG(FATAL) << "Unreachable";
5357}
5358
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005359void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5360 DCHECK(codegen_->IsBaseline());
5361 LocationSummary* locations =
5362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5363 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5364}
5365
5366void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5367 DCHECK(codegen_->IsBaseline());
5368 // Will be generated at use site.
5369}
5370
Mark Mendellfe57faa2015-09-18 09:26:15 -04005371// Simple implementation of packed switch - generate cascaded compare/jumps.
5372void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5373 LocationSummary* locations =
5374 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5375 locations->SetInAt(0, Location::RequiresRegister());
5376}
5377
5378void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5379 int32_t lower_bound = switch_instr->GetStartValue();
5380 int32_t num_entries = switch_instr->GetNumEntries();
5381 LocationSummary* locations = switch_instr->GetLocations();
5382 Register value_reg = locations->InAt(0).AsRegister<Register>();
5383 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5384
5385 // Create a series of compare/jumps.
5386 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5387 for (int32_t i = 0; i < num_entries; i++) {
5388 GenerateCompareWithImmediate(value_reg, lower_bound + i);
Vladimir Markoec7802a2015-10-01 20:57:57 +01005389 __ b(codegen_->GetLabelOf(successors[i]), EQ);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005390 }
5391
5392 // And the default for any other value.
5393 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5394 __ b(codegen_->GetLabelOf(default_block));
5395 }
5396}
5397
Andreas Gampe85b62f22015-09-09 13:15:38 -07005398void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5399 if (!trg.IsValid()) {
5400 DCHECK(type == Primitive::kPrimVoid);
5401 return;
5402 }
5403
5404 DCHECK_NE(type, Primitive::kPrimVoid);
5405
5406 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5407 if (return_loc.Equals(trg)) {
5408 return;
5409 }
5410
5411 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5412 // with the last branch.
5413 if (type == Primitive::kPrimLong) {
5414 HParallelMove parallel_move(GetGraph()->GetArena());
5415 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
5416 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
5417 GetMoveResolver()->EmitNativeCode(&parallel_move);
5418 } else if (type == Primitive::kPrimDouble) {
5419 HParallelMove parallel_move(GetGraph()->GetArena());
5420 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
5421 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
5422 GetMoveResolver()->EmitNativeCode(&parallel_move);
5423 } else {
5424 // Let the parallel move resolver take care of all of this.
5425 HParallelMove parallel_move(GetGraph()->GetArena());
5426 parallel_move.AddMove(return_loc, trg, type, nullptr);
5427 GetMoveResolver()->EmitNativeCode(&parallel_move);
5428 }
5429}
5430
Roland Levillain4d027112015-07-01 15:41:14 +01005431#undef __
5432#undef QUICK_ENTRY_POINT
5433
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005434} // namespace arm
5435} // namespace art