blob: e30aa6e51c73935524a67fa9eb6c438e25ad6951 [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
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070059static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6;
60
Roland Levillain62a46b22015-06-01 18:24:13 +010061#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010062#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063
Andreas Gampe85b62f22015-09-09 13:15:38 -070064class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010066 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067
Alexandre Rames67555f72014-11-18 10:55:16 +000068 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010069 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000071 if (instruction_->CanThrowIntoCatchBlock()) {
72 // Live registers will be restored in the catch block if caught.
73 SaveLiveRegisters(codegen, instruction_->GetLocations());
74 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010075 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000076 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 }
78
Alexandre Rames8158f282015-08-07 10:26:17 +010079 bool IsFatal() const OVERRIDE { return true; }
80
Alexandre Rames9931f312015-06-19 14:47:01 +010081 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
82
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
86};
87
Andreas Gampe85b62f22015-09-09 13:15:38 -070088class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000089 public:
90 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
91
Alexandre Rames67555f72014-11-18 10:55:16 +000092 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000093 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
94 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000095 if (instruction_->CanThrowIntoCatchBlock()) {
96 // Live registers will be restored in the catch block if caught.
97 SaveLiveRegisters(codegen, instruction_->GetLocations());
98 }
Calin Juravled0d48522014-11-04 16:40:20 +000099 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000100 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Calin Juravled0d48522014-11-04 16:40:20 +0000101 }
102
Alexandre Rames8158f282015-08-07 10:26:17 +0100103 bool IsFatal() const OVERRIDE { return true; }
104
Alexandre Rames9931f312015-06-19 14:47:01 +0100105 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
106
Calin Juravled0d48522014-11-04 16:40:20 +0000107 private:
108 HDivZeroCheck* const instruction_;
109 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
110};
111
Andreas Gampe85b62f22015-09-09 13:15:38 -0700112class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000113 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000114 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100115 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116
Alexandre Rames67555f72014-11-18 10:55:16 +0000117 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100118 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000120 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100121 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000122 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100124 if (successor_ == nullptr) {
125 __ b(GetReturnLabel());
126 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100127 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000129 }
130
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100131 Label* GetReturnLabel() {
132 DCHECK(successor_ == nullptr);
133 return &return_label_;
134 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000135
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100136 HBasicBlock* GetSuccessor() const {
137 return successor_;
138 }
139
Alexandre Rames9931f312015-06-19 14:47:01 +0100140 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
141
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000142 private:
143 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100144 // If not null, the block to branch to after the suspend check.
145 HBasicBlock* const successor_;
146
147 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 Label return_label_;
149
150 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
151};
152
Andreas Gampe85b62f22015-09-09 13:15:38 -0700153class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100155 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
156 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157
Alexandre Rames67555f72014-11-18 10:55:16 +0000158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100159 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100160 LocationSummary* locations = instruction_->GetLocations();
161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000163 if (instruction_->CanThrowIntoCatchBlock()) {
164 // Live registers will be restored in the catch block if caught.
165 SaveLiveRegisters(codegen, instruction_->GetLocations());
166 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000167 // We're moving two locations to locations that could overlap, so we need a parallel
168 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100169 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000170 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100171 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000172 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100173 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100174 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100175 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
176 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100177 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000178 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 }
180
Alexandre Rames8158f282015-08-07 10:26:17 +0100181 bool IsFatal() const OVERRIDE { return true; }
182
Alexandre Rames9931f312015-06-19 14:47:01 +0100183 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
184
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100186 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187
188 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
189};
190
Andreas Gampe85b62f22015-09-09 13:15:38 -0700191class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100192 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000193 LoadClassSlowPathARM(HLoadClass* cls,
194 HInstruction* at,
195 uint32_t dex_pc,
196 bool do_clinit)
197 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
198 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
199 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100200
Alexandre Rames67555f72014-11-18 10:55:16 +0000201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202 LocationSummary* locations = at_->GetLocations();
203
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100204 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
205 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000206 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100207
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100208 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 int32_t entry_point_offset = do_clinit_
211 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
212 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000213 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214
215 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000216 Location out = locations->Out();
217 if (out.IsValid()) {
218 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
220 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000221 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222 __ b(GetExitLabel());
223 }
224
Alexandre Rames9931f312015-06-19 14:47:01 +0100225 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
226
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 // The class this slow path will load.
229 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 // The instruction where this slow path is happening.
232 // (Might be the load class or an initialization check).
233 HInstruction* const at_;
234
235 // The dex PC of `at_`.
236 const uint32_t dex_pc_;
237
238 // Whether to initialize the class.
239 const bool do_clinit_;
240
241 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100242};
243
Andreas Gampe85b62f22015-09-09 13:15:38 -0700244class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000245 public:
246 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
247
Alexandre Rames67555f72014-11-18 10:55:16 +0000248 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000249 LocationSummary* locations = instruction_->GetLocations();
250 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
251
252 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
253 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000254 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255
256 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800257 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000258 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000259 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000260 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
261
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000262 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000263 __ b(GetExitLabel());
264 }
265
Alexandre Rames9931f312015-06-19 14:47:01 +0100266 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
267
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000268 private:
269 HLoadString* const instruction_;
270
271 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
272};
273
Andreas Gampe85b62f22015-09-09 13:15:38 -0700274class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000276 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
277 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278
Alexandre Rames67555f72014-11-18 10:55:16 +0000279 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000280 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100281 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
282 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000283 DCHECK(instruction_->IsCheckCast()
284 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285
286 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
287 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000288
289 if (instruction_->IsCheckCast()) {
290 // The codegen for the instruction overwrites `temp`, so put it back in place.
291 Register obj = locations->InAt(0).AsRegister<Register>();
292 Register temp = locations->GetTemp(0).AsRegister<Register>();
293 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
294 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
295 __ MaybeUnpoisonHeapReference(temp);
296 }
297
298 if (!is_fatal_) {
299 SaveLiveRegisters(codegen, locations);
300 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 // We're moving two locations to locations that could overlap, so we need a parallel
303 // move resolver.
304 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000305 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100308 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100309 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100310 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
311 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100314 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
315 instruction_,
316 instruction_->GetDexPc(),
317 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
319 } else {
320 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100321 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
322 instruction_,
323 instruction_->GetDexPc(),
324 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000327 if (!is_fatal_) {
328 RestoreLiveRegisters(codegen, locations);
329 __ b(GetExitLabel());
330 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331 }
332
Alexandre Rames9931f312015-06-19 14:47:01 +0100333 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
334
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335 bool IsFatal() const OVERRIDE { return is_fatal_; }
336
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000339 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
341 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
342};
343
Andreas Gampe85b62f22015-09-09 13:15:38 -0700344class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700345 public:
346 explicit DeoptimizationSlowPathARM(HInstruction* instruction)
347 : instruction_(instruction) {}
348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
350 __ Bind(GetEntryLabel());
351 SaveLiveRegisters(codegen, instruction_->GetLocations());
352 DCHECK(instruction_->IsDeoptimize());
353 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
354 uint32_t dex_pc = deoptimize->GetDexPc();
355 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
356 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
357 }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
360
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 private:
362 HInstruction* const instruction_;
363 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
364};
365
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100366class ArraySetSlowPathARM : public SlowPathCode {
367 public:
368 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
369
370 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
371 LocationSummary* locations = instruction_->GetLocations();
372 __ Bind(GetEntryLabel());
373 SaveLiveRegisters(codegen, locations);
374
375 InvokeRuntimeCallingConvention calling_convention;
376 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
377 parallel_move.AddMove(
378 locations->InAt(0),
379 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
380 Primitive::kPrimNot,
381 nullptr);
382 parallel_move.AddMove(
383 locations->InAt(1),
384 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
385 Primitive::kPrimInt,
386 nullptr);
387 parallel_move.AddMove(
388 locations->InAt(2),
389 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
390 Primitive::kPrimNot,
391 nullptr);
392 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
393
394 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
395 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
396 instruction_,
397 instruction_->GetDexPc(),
398 this);
399 RestoreLiveRegisters(codegen, locations);
400 __ b(GetExitLabel());
401 }
402
403 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
404
405 private:
406 HInstruction* const instruction_;
407
408 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
409};
410
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000411#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100412#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700413
Aart Bike9f37602015-10-09 11:15:55 -0700414inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700415 switch (cond) {
416 case kCondEQ: return EQ;
417 case kCondNE: return NE;
418 case kCondLT: return LT;
419 case kCondLE: return LE;
420 case kCondGT: return GT;
421 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700422 case kCondB: return LO;
423 case kCondBE: return LS;
424 case kCondA: return HI;
425 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700426 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100427 LOG(FATAL) << "Unreachable";
428 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700429}
430
Aart Bike9f37602015-10-09 11:15:55 -0700431// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100432inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700433 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100434 case kCondEQ: return EQ;
435 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700436 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100437 case kCondLT: return LO;
438 case kCondLE: return LS;
439 case kCondGT: return HI;
440 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700441 // Unsigned remain unchanged.
442 case kCondB: return LO;
443 case kCondBE: return LS;
444 case kCondA: return HI;
445 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700446 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100447 LOG(FATAL) << "Unreachable";
448 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700449}
450
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100451void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100452 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100453}
454
455void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100456 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100457}
458
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100459size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
460 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
461 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100462}
463
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100464size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
465 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
466 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100467}
468
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000469size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
470 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
471 return kArmWordSize;
472}
473
474size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
475 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
476 return kArmWordSize;
477}
478
Calin Juravle34166012014-12-19 17:22:29 +0000479CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000480 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100481 const CompilerOptions& compiler_options,
482 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000483 : CodeGenerator(graph,
484 kNumberOfCoreRegisters,
485 kNumberOfSRegisters,
486 kNumberOfRegisterPairs,
487 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
488 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000489 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
490 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100491 compiler_options,
492 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100493 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100495 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100496 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000497 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000498 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100499 method_patches_(MethodReferenceComparator(),
500 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
501 call_patches_(MethodReferenceComparator(),
502 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
503 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700504 // Always save the LR register to mimic Quick.
505 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100506}
507
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000508void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
509 // Ensure that we fix up branches and literal loads and emit the literal pool.
510 __ FinalizeCode();
511
512 // Adjust native pc offsets in stack maps.
513 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
514 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
515 uint32_t new_position = __ GetAdjustedPosition(old_position);
516 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
517 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100518 // Adjust pc offsets for the disassembly information.
519 if (disasm_info_ != nullptr) {
520 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
521 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
522 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
523 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
524 it.second.start = __ GetAdjustedPosition(it.second.start);
525 it.second.end = __ GetAdjustedPosition(it.second.end);
526 }
527 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
528 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
529 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
530 }
531 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000532
533 CodeGenerator::Finalize(allocator);
534}
535
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100536Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100537 switch (type) {
538 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100539 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100540 ArmManagedRegister pair =
541 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100542 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
543 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
544
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100545 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
546 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100547 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100548 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100549 }
550
551 case Primitive::kPrimByte:
552 case Primitive::kPrimBoolean:
553 case Primitive::kPrimChar:
554 case Primitive::kPrimShort:
555 case Primitive::kPrimInt:
556 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100557 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100558 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100559 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
560 ArmManagedRegister current =
561 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
562 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100563 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100564 }
565 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100566 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100567 }
568
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000569 case Primitive::kPrimFloat: {
570 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100571 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100572 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100573
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000574 case Primitive::kPrimDouble: {
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000575 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters);
576 DCHECK_EQ(reg % 2, 0);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000577 return Location::FpuRegisterPairLocation(reg, reg + 1);
578 }
579
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100580 case Primitive::kPrimVoid:
581 LOG(FATAL) << "Unreachable type " << type;
582 }
583
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100585}
586
Nicolas Geoffraya0bb2bd2015-01-26 12:49:35 +0000587void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100588 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100589 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100590
591 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100592 blocked_core_registers_[SP] = true;
593 blocked_core_registers_[LR] = true;
594 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100595
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100596 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100597 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100599 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100600 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100601
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000602 if (is_baseline) {
603 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
604 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
605 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000606
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000607 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false;
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100608 }
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000609
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100610 if (is_baseline || GetGraph()->IsDebuggable()) {
611 // Stubs do not save callee-save floating point registers. If the graph
612 // is debuggable, we need to deal with these registers differently. For
613 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000614 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
615 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
616 }
617 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100618
619 UpdateBlockedPairRegisters();
620}
621
622void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
623 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
624 ArmManagedRegister current =
625 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
626 if (blocked_core_registers_[current.AsRegisterPairLow()]
627 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
628 blocked_register_pairs_[i] = true;
629 }
630 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100631}
632
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100633InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
634 : HGraphVisitor(graph),
635 assembler_(codegen->GetAssembler()),
636 codegen_(codegen) {}
637
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000638void CodeGeneratorARM::ComputeSpillMask() {
639 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000640 // Save one extra register for baseline. Note that on thumb2, there is no easy
641 // instruction to restore just the PC, so this actually helps both baseline
642 // and non-baseline to save and restore at least two registers at entry and exit.
643 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000644 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
645 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
646 // We use vpush and vpop for saving and restoring floating point registers, which take
647 // a SRegister and the number of registers to save/restore after that SRegister. We
648 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
649 // but in the range.
650 if (fpu_spill_mask_ != 0) {
651 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
652 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
653 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
654 fpu_spill_mask_ |= (1 << i);
655 }
656 }
657}
658
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100659static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100660 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100661}
662
663static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100664 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100665}
666
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000667void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000668 bool skip_overflow_check =
669 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000670 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000671 __ Bind(&frame_entry_label_);
672
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000673 if (HasEmptyFrame()) {
674 return;
675 }
676
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100677 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000678 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
679 __ LoadFromOffset(kLoadWord, IP, IP, 0);
680 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100681 }
682
Andreas Gampe501fd632015-09-10 16:11:06 -0700683 __ PushList(core_spill_mask_);
684 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
685 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000686 if (fpu_spill_mask_ != 0) {
687 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
688 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100689 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100690 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000691 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100692 int adjust = GetFrameSize() - FrameEntrySpillSize();
693 __ AddConstant(SP, -adjust);
694 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100695 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000696}
697
698void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000699 if (HasEmptyFrame()) {
700 __ bx(LR);
701 return;
702 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100703 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100704 int adjust = GetFrameSize() - FrameEntrySpillSize();
705 __ AddConstant(SP, adjust);
706 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000707 if (fpu_spill_mask_ != 0) {
708 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
709 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100710 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
711 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000712 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700713 // Pop LR into PC to return.
714 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
715 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
716 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100717 __ cfi().RestoreState();
718 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000719}
720
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100721void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700722 Label* label = GetLabelOf(block);
723 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000724}
725
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100726Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
727 switch (load->GetType()) {
728 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100730 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100731
732 case Primitive::kPrimInt:
733 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100734 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100736
737 case Primitive::kPrimBoolean:
738 case Primitive::kPrimByte:
739 case Primitive::kPrimChar:
740 case Primitive::kPrimShort:
741 case Primitive::kPrimVoid:
742 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700743 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100744 }
745
746 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700747 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100748}
749
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100750Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100751 switch (type) {
752 case Primitive::kPrimBoolean:
753 case Primitive::kPrimByte:
754 case Primitive::kPrimChar:
755 case Primitive::kPrimShort:
756 case Primitive::kPrimInt:
757 case Primitive::kPrimNot: {
758 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000759 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100760 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100761 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100762 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000763 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100764 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100765 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100766
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000767 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100768 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000769 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100770 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000771 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100772 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000773 if (calling_convention.GetRegisterAt(index) == R1) {
774 // Skip R1, and use R2_R3 instead.
775 gp_index_++;
776 index++;
777 }
778 }
779 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
780 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000781 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +0100782
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000783 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +0000784 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100785 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000786 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
787 }
788 }
789
790 case Primitive::kPrimFloat: {
791 uint32_t stack_index = stack_index_++;
792 if (float_index_ % 2 == 0) {
793 float_index_ = std::max(double_index_, float_index_);
794 }
795 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
796 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
797 } else {
798 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
799 }
800 }
801
802 case Primitive::kPrimDouble: {
803 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
804 uint32_t stack_index = stack_index_;
805 stack_index_ += 2;
806 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
807 uint32_t index = double_index_;
808 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000809 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000810 calling_convention.GetFpuRegisterAt(index),
811 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000812 DCHECK(ExpectedPairLayout(result));
813 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000814 } else {
815 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100816 }
817 }
818
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100819 case Primitive::kPrimVoid:
820 LOG(FATAL) << "Unexpected parameter type " << type;
821 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100822 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100823 return Location();
824}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100825
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100826Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000827 switch (type) {
828 case Primitive::kPrimBoolean:
829 case Primitive::kPrimByte:
830 case Primitive::kPrimChar:
831 case Primitive::kPrimShort:
832 case Primitive::kPrimInt:
833 case Primitive::kPrimNot: {
834 return Location::RegisterLocation(R0);
835 }
836
837 case Primitive::kPrimFloat: {
838 return Location::FpuRegisterLocation(S0);
839 }
840
841 case Primitive::kPrimLong: {
842 return Location::RegisterPairLocation(R0, R1);
843 }
844
845 case Primitive::kPrimDouble: {
846 return Location::FpuRegisterPairLocation(S0, S1);
847 }
848
849 case Primitive::kPrimVoid:
850 return Location();
851 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100852
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000853 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000854}
855
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100856Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
857 return Location::RegisterLocation(kMethodRegisterArgument);
858}
859
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100860void CodeGeneratorARM::Move32(Location destination, Location source) {
861 if (source.Equals(destination)) {
862 return;
863 }
864 if (destination.IsRegister()) {
865 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000866 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100867 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100869 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000870 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100871 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100872 } else if (destination.IsFpuRegister()) {
873 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000874 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100875 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000876 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100877 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000878 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100879 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000881 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000883 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100884 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000885 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +0000887 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100888 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
889 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890 }
891 }
892}
893
894void CodeGeneratorARM::Move64(Location destination, Location source) {
895 if (source.Equals(destination)) {
896 return;
897 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100898 if (destination.IsRegisterPair()) {
899 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000900 EmitParallelMoves(
901 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
902 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100903 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000904 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100905 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
906 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000908 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +0100909 } else if (source.IsFpuRegisterPair()) {
910 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
911 destination.AsRegisterPairHigh<Register>(),
912 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 } else {
914 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +0000915 DCHECK(ExpectedPairLayout(destination));
916 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
917 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100918 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000919 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000921 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
922 SP,
923 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100924 } else if (source.IsRegisterPair()) {
925 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
926 source.AsRegisterPairLow<Register>(),
927 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100928 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000929 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100930 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100931 } else {
932 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100933 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000934 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100935 if (source.AsRegisterPairLow<Register>() == R1) {
936 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100937 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
938 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100939 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100941 SP, destination.GetStackIndex());
942 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000943 } else if (source.IsFpuRegisterPair()) {
944 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
945 SP,
946 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100947 } else {
948 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000949 EmitParallelMoves(
950 Location::StackSlot(source.GetStackIndex()),
951 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100952 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000953 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100954 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
955 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 }
957 }
958}
959
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100960void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100961 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100962 if (instruction->IsCurrentMethod()) {
963 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
964 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100965 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100966 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000967 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000968 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
969 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +0000970 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000971 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +0000972 } else {
973 DCHECK(location.IsStackSlot());
974 __ LoadImmediate(IP, value);
975 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
976 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000977 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000978 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +0000979 int64_t value = const_to_move->AsLongConstant()->GetValue();
980 if (location.IsRegisterPair()) {
981 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
982 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
983 } else {
984 DCHECK(location.IsDoubleStackSlot());
985 __ LoadImmediate(IP, Low32Bits(value));
986 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
987 __ LoadImmediate(IP, High32Bits(value));
988 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
989 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100990 }
Roland Levillain476df552014-10-09 17:51:36 +0100991 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
993 switch (instruction->GetType()) {
994 case Primitive::kPrimBoolean:
995 case Primitive::kPrimByte:
996 case Primitive::kPrimChar:
997 case Primitive::kPrimShort:
998 case Primitive::kPrimInt:
999 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001000 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001 Move32(location, Location::StackSlot(stack_slot));
1002 break;
1003
1004 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001006 Move64(location, Location::DoubleStackSlot(stack_slot));
1007 break;
1008
1009 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001012 } else if (instruction->IsTemporary()) {
1013 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001014 if (temp_location.IsStackSlot()) {
1015 Move32(location, temp_location);
1016 } else {
1017 DCHECK(temp_location.IsDoubleStackSlot());
1018 Move64(location, temp_location);
1019 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001020 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001021 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022 switch (instruction->GetType()) {
1023 case Primitive::kPrimBoolean:
1024 case Primitive::kPrimByte:
1025 case Primitive::kPrimChar:
1026 case Primitive::kPrimShort:
1027 case Primitive::kPrimNot:
1028 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001030 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001031 break;
1032
1033 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001034 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001035 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 break;
1037
1038 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001039 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001041 }
1042}
1043
Calin Juravle175dc732015-08-25 15:42:32 +01001044void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1045 DCHECK(location.IsRegister());
1046 __ LoadImmediate(location.AsRegister<Register>(), value);
1047}
1048
Calin Juravlee460d1d2015-09-29 04:52:17 +01001049void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1050 if (Primitive::Is64BitType(dst_type)) {
1051 Move64(dst, src);
1052 } else {
1053 Move32(dst, src);
1054 }
1055}
1056
1057void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1058 if (location.IsRegister()) {
1059 locations->AddTemp(location);
1060 } else if (location.IsRegisterPair()) {
1061 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1062 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1063 } else {
1064 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1065 }
1066}
1067
Calin Juravle175dc732015-08-25 15:42:32 +01001068void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1069 HInstruction* instruction,
1070 uint32_t dex_pc,
1071 SlowPathCode* slow_path) {
1072 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1073 instruction,
1074 dex_pc,
1075 slow_path);
1076}
1077
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001078void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1079 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001080 uint32_t dex_pc,
1081 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001082 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001083 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1084 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001085 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001086}
1087
David Brazdilfc6a86a2015-06-26 10:33:45 +00001088void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001089 DCHECK(!successor->IsExitBlock());
1090
1091 HBasicBlock* block = got->GetBlock();
1092 HInstruction* previous = got->GetPrevious();
1093
1094 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001095 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001096 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1097 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1098 return;
1099 }
1100
1101 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1102 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1103 }
1104 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001105 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001106 }
1107}
1108
David Brazdilfc6a86a2015-06-26 10:33:45 +00001109void LocationsBuilderARM::VisitGoto(HGoto* got) {
1110 got->SetLocations(nullptr);
1111}
1112
1113void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1114 HandleGoto(got, got->GetSuccessor());
1115}
1116
1117void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1118 try_boundary->SetLocations(nullptr);
1119}
1120
1121void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1122 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1123 if (!successor->IsExitBlock()) {
1124 HandleGoto(try_boundary, successor);
1125 }
1126}
1127
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001128void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001129 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001130}
1131
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001132void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001133}
1134
Roland Levillain4fa13f62015-07-06 18:11:54 +01001135void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) {
1136 ShifterOperand operand;
1137 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) {
1138 __ cmp(left, operand);
1139 } else {
1140 Register temp = IP;
1141 __ LoadImmediate(temp, right);
1142 __ cmp(left, ShifterOperand(temp));
1143 }
1144}
1145
1146void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1147 Label* true_label,
1148 Label* false_label) {
1149 __ vmstat(); // transfer FP status register to ARM APSR.
Aart Bike9f37602015-10-09 11:15:55 -07001150 // TODO: merge into a single branch (except "equal or unordered" and "not equal")
Roland Levillain4fa13f62015-07-06 18:11:54 +01001151 if (cond->IsFPConditionTrueIfNaN()) {
1152 __ b(true_label, VS); // VS for unordered.
1153 } else if (cond->IsFPConditionFalseIfNaN()) {
1154 __ b(false_label, VS); // VS for unordered.
1155 }
Aart Bike9f37602015-10-09 11:15:55 -07001156 __ b(true_label, ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001157}
1158
1159void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1160 Label* true_label,
1161 Label* false_label) {
1162 LocationSummary* locations = cond->GetLocations();
1163 Location left = locations->InAt(0);
1164 Location right = locations->InAt(1);
1165 IfCondition if_cond = cond->GetCondition();
1166
1167 Register left_high = left.AsRegisterPairHigh<Register>();
1168 Register left_low = left.AsRegisterPairLow<Register>();
1169 IfCondition true_high_cond = if_cond;
1170 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001171 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001172
1173 // Set the conditions for the test, remembering that == needs to be
1174 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001175 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001176 switch (if_cond) {
1177 case kCondEQ:
1178 case kCondNE:
1179 // Nothing to do.
1180 break;
1181 case kCondLT:
1182 false_high_cond = kCondGT;
1183 break;
1184 case kCondLE:
1185 true_high_cond = kCondLT;
1186 break;
1187 case kCondGT:
1188 false_high_cond = kCondLT;
1189 break;
1190 case kCondGE:
1191 true_high_cond = kCondGT;
1192 break;
Aart Bike9f37602015-10-09 11:15:55 -07001193 case kCondB:
1194 false_high_cond = kCondA;
1195 break;
1196 case kCondBE:
1197 true_high_cond = kCondB;
1198 break;
1199 case kCondA:
1200 false_high_cond = kCondB;
1201 break;
1202 case kCondAE:
1203 true_high_cond = kCondA;
1204 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001205 }
1206 if (right.IsConstant()) {
1207 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1208 int32_t val_low = Low32Bits(value);
1209 int32_t val_high = High32Bits(value);
1210
1211 GenerateCompareWithImmediate(left_high, val_high);
1212 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001213 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001214 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001215 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001216 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001217 __ b(true_label, ARMCondition(true_high_cond));
1218 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001219 }
1220 // Must be equal high, so compare the lows.
1221 GenerateCompareWithImmediate(left_low, val_low);
1222 } else {
1223 Register right_high = right.AsRegisterPairHigh<Register>();
1224 Register right_low = right.AsRegisterPairLow<Register>();
1225
1226 __ cmp(left_high, ShifterOperand(right_high));
1227 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001228 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001229 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001230 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001231 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001232 __ b(true_label, ARMCondition(true_high_cond));
1233 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001234 }
1235 // Must be equal high, so compare the lows.
1236 __ cmp(left_low, ShifterOperand(right_low));
1237 }
1238 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001239 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001240 __ b(true_label, final_condition);
1241}
1242
1243void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr,
1244 HCondition* condition,
1245 Label* true_target,
1246 Label* false_target,
1247 Label* always_true_target) {
1248 LocationSummary* locations = condition->GetLocations();
1249 Location left = locations->InAt(0);
1250 Location right = locations->InAt(1);
1251
1252 // We don't want true_target as a nullptr.
1253 if (true_target == nullptr) {
1254 true_target = always_true_target;
1255 }
1256 bool falls_through = (false_target == nullptr);
1257
1258 // FP compares don't like null false_targets.
1259 if (false_target == nullptr) {
1260 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1261 }
1262
1263 Primitive::Type type = condition->InputAt(0)->GetType();
1264 switch (type) {
1265 case Primitive::kPrimLong:
1266 GenerateLongComparesAndJumps(condition, true_target, false_target);
1267 break;
1268 case Primitive::kPrimFloat:
1269 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1270 GenerateFPJumps(condition, true_target, false_target);
1271 break;
1272 case Primitive::kPrimDouble:
1273 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1274 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1275 GenerateFPJumps(condition, true_target, false_target);
1276 break;
1277 default:
1278 LOG(FATAL) << "Unexpected compare type " << type;
1279 }
1280
1281 if (!falls_through) {
1282 __ b(false_target);
1283 }
1284}
1285
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001286void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
1287 Label* true_target,
1288 Label* false_target,
1289 Label* always_true_target) {
1290 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001291 if (cond->IsIntConstant()) {
1292 // Constant condition, statically compared against 1.
1293 int32_t cond_value = cond->AsIntConstant()->GetValue();
1294 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001295 if (always_true_target != nullptr) {
1296 __ b(always_true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001297 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001298 return;
1299 } else {
1300 DCHECK_EQ(cond_value, 0);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001301 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001302 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001303 // Can we optimize the jump if we know that the next block is the true case?
1304 HCondition* condition = cond->AsCondition();
1305 bool can_jump_to_false = CanReverseCondition(always_true_target, false_target, condition);
1306 if (condition == nullptr || condition->NeedsMaterialization()) {
1307 // Condition has been materialized, compare the output to 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001308 DCHECK(instruction->GetLocations()->InAt(0).IsRegister());
Mark Mendellb8b97692015-05-22 16:58:19 -04001309 if (can_jump_to_false) {
1310 __ CompareAndBranchIfZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1311 false_target);
1312 return;
1313 }
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01001314 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(),
1315 true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001316 } else {
1317 // Condition has not been materialized, use its inputs as the
1318 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001319 Primitive::Type type = (condition != nullptr)
1320 ? cond->InputAt(0)->GetType()
1321 : Primitive::kPrimInt;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001322 // Is this a long or FP comparison that has been folded into the HCondition?
1323 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1324 // Generate the comparison directly.
Mark Mendellb8b97692015-05-22 16:58:19 -04001325 GenerateCompareTestAndBranch(instruction->AsIf(), condition,
Roland Levillain4fa13f62015-07-06 18:11:54 +01001326 true_target, false_target, always_true_target);
1327 return;
1328 }
1329
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001330 LocationSummary* locations = cond->GetLocations();
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001331 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0);
Nicolas Geoffray3bcc8ea2014-11-28 15:00:02 +00001332 Register left = locations->InAt(0).AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001333 Location right = locations->InAt(1);
1334 if (right.IsRegister()) {
1335 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001336 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001337 DCHECK(right.IsConstant());
1338 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001339 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001340 if (can_jump_to_false) {
1341 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1342 return;
1343 }
1344
1345 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001346 }
Dave Allison20dfc792014-06-16 20:44:29 -07001347 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001348 if (false_target != nullptr) {
1349 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001350 }
1351}
1352
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001353void LocationsBuilderARM::VisitIf(HIf* if_instr) {
1354 LocationSummary* locations =
1355 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1356 HInstruction* cond = if_instr->InputAt(0);
1357 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1358 locations->SetInAt(0, Location::RequiresRegister());
1359 }
1360}
1361
1362void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
1363 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1364 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1365 Label* always_true_target = true_target;
1366 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1367 if_instr->IfTrueSuccessor())) {
1368 always_true_target = nullptr;
1369 }
1370 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1371 if_instr->IfFalseSuccessor())) {
1372 false_target = nullptr;
1373 }
1374 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1375}
1376
1377void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1378 LocationSummary* locations = new (GetGraph()->GetArena())
1379 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1380 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001381 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001382 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
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001573void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001574 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001575}
1576
1577void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001578 LocationSummary* locations =
1579 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001580 switch (store->InputAt(1)->GetType()) {
1581 case Primitive::kPrimBoolean:
1582 case Primitive::kPrimByte:
1583 case Primitive::kPrimChar:
1584 case Primitive::kPrimShort:
1585 case Primitive::kPrimInt:
1586 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001587 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001588 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1589 break;
1590
1591 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001592 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001593 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1594 break;
1595
1596 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001597 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001598 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001599}
1600
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001601void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001602}
1603
1604void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001605 LocationSummary* locations =
1606 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001607 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001608}
1609
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001610void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001611 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001612}
1613
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001614void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1615 LocationSummary* locations =
1616 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1617 locations->SetOut(Location::ConstantLocation(constant));
1618}
1619
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001620void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001621 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001622}
1623
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001624void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001625 LocationSummary* locations =
1626 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001627 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001628}
1629
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001630void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001631 // Will be generated at use site.
1632}
1633
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001634void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1635 LocationSummary* locations =
1636 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1637 locations->SetOut(Location::ConstantLocation(constant));
1638}
1639
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001640void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001641 // Will be generated at use site.
1642}
1643
1644void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1645 LocationSummary* locations =
1646 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1647 locations->SetOut(Location::ConstantLocation(constant));
1648}
1649
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001650void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001651 // Will be generated at use site.
1652}
1653
Calin Juravle27df7582015-04-17 19:12:31 +01001654void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1655 memory_barrier->SetLocations(nullptr);
1656}
1657
1658void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1659 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1660}
1661
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001662void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001663 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001664}
1665
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001666void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001667 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001668}
1669
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001670void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001671 LocationSummary* locations =
1672 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001673 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001674}
1675
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001676void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001677 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001678}
1679
Calin Juravle175dc732015-08-25 15:42:32 +01001680void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1681 // The trampoline uses the same calling convention as dex calling conventions,
1682 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1683 // the method_idx.
1684 HandleInvoke(invoke);
1685}
1686
1687void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1688 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1689}
1690
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001691void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001692 // When we do not run baseline, explicit clinit checks triggered by static
1693 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1694 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001695
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001696 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001697 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001698 codegen_->GetInstructionSetFeatures());
1699 if (intrinsic.TryDispatch(invoke)) {
1700 return;
1701 }
1702
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001703 HandleInvoke(invoke);
1704}
1705
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001706static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1707 if (invoke->GetLocations()->Intrinsified()) {
1708 IntrinsicCodeGeneratorARM intrinsic(codegen);
1709 intrinsic.Dispatch(invoke);
1710 return true;
1711 }
1712 return false;
1713}
1714
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001715void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001716 // When we do not run baseline, explicit clinit checks triggered by static
1717 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1718 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001719
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001720 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1721 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001722 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001723
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001724 LocationSummary* locations = invoke->GetLocations();
1725 codegen_->GenerateStaticOrDirectCall(
1726 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001727 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001728}
1729
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001730void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001731 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001732 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001733}
1734
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001735void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001736 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001737 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001738 codegen_->GetInstructionSetFeatures());
1739 if (intrinsic.TryDispatch(invoke)) {
1740 return;
1741 }
1742
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001743 HandleInvoke(invoke);
1744}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001745
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001746void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001747 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1748 return;
1749 }
1750
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001751 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001752 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001753 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001754}
1755
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001756void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1757 HandleInvoke(invoke);
1758 // Add the hidden argument.
1759 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1760}
1761
1762void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1763 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001764 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001765 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1766 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001767 LocationSummary* locations = invoke->GetLocations();
1768 Location receiver = locations->InAt(0);
1769 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1770
1771 // Set the hidden argument.
Roland Levillain199f3362014-11-27 17:15:16 +00001772 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
1773 invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001774
1775 // temp = object->GetClass();
1776 if (receiver.IsStackSlot()) {
1777 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
1778 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1779 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001780 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001781 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001782 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain4d027112015-07-01 15:41:14 +01001783 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001784 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001785 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001786 kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001787 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1788 // LR = temp->GetEntryPoint();
1789 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1790 // LR();
1791 __ blx(LR);
1792 DCHECK(!codegen_->IsLeafMethod());
1793 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1794}
1795
Roland Levillain88cb1752014-10-20 16:36:47 +01001796void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1797 LocationSummary* locations =
1798 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1799 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001800 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001801 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1803 break;
1804 }
1805 case Primitive::kPrimLong: {
1806 locations->SetInAt(0, Location::RequiresRegister());
1807 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001808 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001809 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001810
Roland Levillain88cb1752014-10-20 16:36:47 +01001811 case Primitive::kPrimFloat:
1812 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001813 locations->SetInAt(0, Location::RequiresFpuRegister());
1814 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001815 break;
1816
1817 default:
1818 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1819 }
1820}
1821
1822void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
1823 LocationSummary* locations = neg->GetLocations();
1824 Location out = locations->Out();
1825 Location in = locations->InAt(0);
1826 switch (neg->GetResultType()) {
1827 case Primitive::kPrimInt:
1828 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001829 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01001830 break;
1831
1832 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001833 DCHECK(in.IsRegisterPair());
1834 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
1835 __ rsbs(out.AsRegisterPairLow<Register>(),
1836 in.AsRegisterPairLow<Register>(),
1837 ShifterOperand(0));
1838 // We cannot emit an RSC (Reverse Subtract with Carry)
1839 // instruction here, as it does not exist in the Thumb-2
1840 // instruction set. We use the following approach
1841 // using SBC and SUB instead.
1842 //
1843 // out.hi = -C
1844 __ sbc(out.AsRegisterPairHigh<Register>(),
1845 out.AsRegisterPairHigh<Register>(),
1846 ShifterOperand(out.AsRegisterPairHigh<Register>()));
1847 // out.hi = out.hi - in.hi
1848 __ sub(out.AsRegisterPairHigh<Register>(),
1849 out.AsRegisterPairHigh<Register>(),
1850 ShifterOperand(in.AsRegisterPairHigh<Register>()));
1851 break;
1852
Roland Levillain88cb1752014-10-20 16:36:47 +01001853 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001854 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001855 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001856 break;
1857
Roland Levillain88cb1752014-10-20 16:36:47 +01001858 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001859 DCHECK(in.IsFpuRegisterPair());
1860 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
1861 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01001862 break;
1863
1864 default:
1865 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1866 }
1867}
1868
Roland Levillaindff1f282014-11-05 14:15:05 +00001869void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001870 Primitive::Type result_type = conversion->GetResultType();
1871 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001872 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001873
Roland Levillain5b3ee562015-04-14 16:02:41 +01001874 // The float-to-long, double-to-long and long-to-float type conversions
1875 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001876 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01001877 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1878 && result_type == Primitive::kPrimLong)
1879 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00001880 ? LocationSummary::kCall
1881 : LocationSummary::kNoCall;
1882 LocationSummary* locations =
1883 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1884
David Brazdilb2bd1c52015-03-25 11:17:37 +00001885 // The Java language does not allow treating boolean as an integral type but
1886 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001887
Roland Levillaindff1f282014-11-05 14:15:05 +00001888 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001889 case Primitive::kPrimByte:
1890 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001891 case Primitive::kPrimBoolean:
1892 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001893 case Primitive::kPrimShort:
1894 case Primitive::kPrimInt:
1895 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001896 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001897 locations->SetInAt(0, Location::RequiresRegister());
1898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1899 break;
1900
1901 default:
1902 LOG(FATAL) << "Unexpected type conversion from " << input_type
1903 << " to " << result_type;
1904 }
1905 break;
1906
Roland Levillain01a8d712014-11-14 16:27:39 +00001907 case Primitive::kPrimShort:
1908 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001909 case Primitive::kPrimBoolean:
1910 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001911 case Primitive::kPrimByte:
1912 case Primitive::kPrimInt:
1913 case Primitive::kPrimChar:
1914 // Processing a Dex `int-to-short' instruction.
1915 locations->SetInAt(0, Location::RequiresRegister());
1916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1917 break;
1918
1919 default:
1920 LOG(FATAL) << "Unexpected type conversion from " << input_type
1921 << " to " << result_type;
1922 }
1923 break;
1924
Roland Levillain946e1432014-11-11 17:35:19 +00001925 case Primitive::kPrimInt:
1926 switch (input_type) {
1927 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001928 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001929 locations->SetInAt(0, Location::Any());
1930 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1931 break;
1932
1933 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001934 // Processing a Dex `float-to-int' instruction.
1935 locations->SetInAt(0, Location::RequiresFpuRegister());
1936 locations->SetOut(Location::RequiresRegister());
1937 locations->AddTemp(Location::RequiresFpuRegister());
1938 break;
1939
Roland Levillain946e1432014-11-11 17:35:19 +00001940 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001941 // Processing a Dex `double-to-int' instruction.
1942 locations->SetInAt(0, Location::RequiresFpuRegister());
1943 locations->SetOut(Location::RequiresRegister());
1944 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001945 break;
1946
1947 default:
1948 LOG(FATAL) << "Unexpected type conversion from " << input_type
1949 << " to " << result_type;
1950 }
1951 break;
1952
Roland Levillaindff1f282014-11-05 14:15:05 +00001953 case Primitive::kPrimLong:
1954 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001955 case Primitive::kPrimBoolean:
1956 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001957 case Primitive::kPrimByte:
1958 case Primitive::kPrimShort:
1959 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001960 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001961 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001962 locations->SetInAt(0, Location::RequiresRegister());
1963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1964 break;
1965
Roland Levillain624279f2014-12-04 11:54:28 +00001966 case Primitive::kPrimFloat: {
1967 // Processing a Dex `float-to-long' instruction.
1968 InvokeRuntimeCallingConvention calling_convention;
1969 locations->SetInAt(0, Location::FpuRegisterLocation(
1970 calling_convention.GetFpuRegisterAt(0)));
1971 locations->SetOut(Location::RegisterPairLocation(R0, R1));
1972 break;
1973 }
1974
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001975 case Primitive::kPrimDouble: {
1976 // Processing a Dex `double-to-long' instruction.
1977 InvokeRuntimeCallingConvention calling_convention;
1978 locations->SetInAt(0, Location::FpuRegisterPairLocation(
1979 calling_convention.GetFpuRegisterAt(0),
1980 calling_convention.GetFpuRegisterAt(1)));
1981 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00001982 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001983 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001984
1985 default:
1986 LOG(FATAL) << "Unexpected type conversion from " << input_type
1987 << " to " << result_type;
1988 }
1989 break;
1990
Roland Levillain981e4542014-11-14 11:47:14 +00001991 case Primitive::kPrimChar:
1992 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001993 case Primitive::kPrimBoolean:
1994 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001995 case Primitive::kPrimByte:
1996 case Primitive::kPrimShort:
1997 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001998 // Processing a Dex `int-to-char' instruction.
1999 locations->SetInAt(0, Location::RequiresRegister());
2000 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2001 break;
2002
2003 default:
2004 LOG(FATAL) << "Unexpected type conversion from " << input_type
2005 << " to " << result_type;
2006 }
2007 break;
2008
Roland Levillaindff1f282014-11-05 14:15:05 +00002009 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002010 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002011 case Primitive::kPrimBoolean:
2012 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002013 case Primitive::kPrimByte:
2014 case Primitive::kPrimShort:
2015 case Primitive::kPrimInt:
2016 case Primitive::kPrimChar:
2017 // Processing a Dex `int-to-float' instruction.
2018 locations->SetInAt(0, Location::RequiresRegister());
2019 locations->SetOut(Location::RequiresFpuRegister());
2020 break;
2021
Roland Levillain5b3ee562015-04-14 16:02:41 +01002022 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002023 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002024 InvokeRuntimeCallingConvention calling_convention;
2025 locations->SetInAt(0, Location::RegisterPairLocation(
2026 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2027 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002028 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002029 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002030
Roland Levillaincff13742014-11-17 14:32:17 +00002031 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002032 // Processing a Dex `double-to-float' instruction.
2033 locations->SetInAt(0, Location::RequiresFpuRegister());
2034 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002035 break;
2036
2037 default:
2038 LOG(FATAL) << "Unexpected type conversion from " << input_type
2039 << " to " << result_type;
2040 };
2041 break;
2042
Roland Levillaindff1f282014-11-05 14:15:05 +00002043 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002044 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002045 case Primitive::kPrimBoolean:
2046 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002047 case Primitive::kPrimByte:
2048 case Primitive::kPrimShort:
2049 case Primitive::kPrimInt:
2050 case Primitive::kPrimChar:
2051 // Processing a Dex `int-to-double' instruction.
2052 locations->SetInAt(0, Location::RequiresRegister());
2053 locations->SetOut(Location::RequiresFpuRegister());
2054 break;
2055
2056 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002057 // Processing a Dex `long-to-double' instruction.
2058 locations->SetInAt(0, Location::RequiresRegister());
2059 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002060 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002061 locations->AddTemp(Location::RequiresFpuRegister());
2062 break;
2063
Roland Levillaincff13742014-11-17 14:32:17 +00002064 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002065 // Processing a Dex `float-to-double' instruction.
2066 locations->SetInAt(0, Location::RequiresFpuRegister());
2067 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002068 break;
2069
2070 default:
2071 LOG(FATAL) << "Unexpected type conversion from " << input_type
2072 << " to " << result_type;
2073 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002074 break;
2075
2076 default:
2077 LOG(FATAL) << "Unexpected type conversion from " << input_type
2078 << " to " << result_type;
2079 }
2080}
2081
2082void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2083 LocationSummary* locations = conversion->GetLocations();
2084 Location out = locations->Out();
2085 Location in = locations->InAt(0);
2086 Primitive::Type result_type = conversion->GetResultType();
2087 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002088 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002089 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002090 case Primitive::kPrimByte:
2091 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002092 case Primitive::kPrimBoolean:
2093 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002094 case Primitive::kPrimShort:
2095 case Primitive::kPrimInt:
2096 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002097 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002098 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002099 break;
2100
2101 default:
2102 LOG(FATAL) << "Unexpected type conversion from " << input_type
2103 << " to " << result_type;
2104 }
2105 break;
2106
Roland Levillain01a8d712014-11-14 16:27:39 +00002107 case Primitive::kPrimShort:
2108 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002109 case Primitive::kPrimBoolean:
2110 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002111 case Primitive::kPrimByte:
2112 case Primitive::kPrimInt:
2113 case Primitive::kPrimChar:
2114 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002115 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002116 break;
2117
2118 default:
2119 LOG(FATAL) << "Unexpected type conversion from " << input_type
2120 << " to " << result_type;
2121 }
2122 break;
2123
Roland Levillain946e1432014-11-11 17:35:19 +00002124 case Primitive::kPrimInt:
2125 switch (input_type) {
2126 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002127 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002128 DCHECK(out.IsRegister());
2129 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002131 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002133 } else {
2134 DCHECK(in.IsConstant());
2135 DCHECK(in.GetConstant()->IsLongConstant());
2136 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002138 }
2139 break;
2140
Roland Levillain3f8f9362014-12-02 17:45:01 +00002141 case Primitive::kPrimFloat: {
2142 // Processing a Dex `float-to-int' instruction.
2143 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2144 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2145 __ vcvtis(temp, temp);
2146 __ vmovrs(out.AsRegister<Register>(), temp);
2147 break;
2148 }
2149
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002150 case Primitive::kPrimDouble: {
2151 // Processing a Dex `double-to-int' instruction.
2152 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2153 DRegister temp_d = FromLowSToD(temp_s);
2154 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2155 __ vcvtid(temp_s, temp_d);
2156 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002157 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002158 }
Roland Levillain946e1432014-11-11 17:35:19 +00002159
2160 default:
2161 LOG(FATAL) << "Unexpected type conversion from " << input_type
2162 << " to " << result_type;
2163 }
2164 break;
2165
Roland Levillaindff1f282014-11-05 14:15:05 +00002166 case Primitive::kPrimLong:
2167 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002168 case Primitive::kPrimBoolean:
2169 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002170 case Primitive::kPrimByte:
2171 case Primitive::kPrimShort:
2172 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002173 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002174 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002175 DCHECK(out.IsRegisterPair());
2176 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002177 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002178 // Sign extension.
2179 __ Asr(out.AsRegisterPairHigh<Register>(),
2180 out.AsRegisterPairLow<Register>(),
2181 31);
2182 break;
2183
2184 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002185 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002186 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2187 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002188 conversion->GetDexPc(),
2189 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002190 break;
2191
Roland Levillaindff1f282014-11-05 14:15:05 +00002192 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002193 // Processing a Dex `double-to-long' instruction.
2194 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2195 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002196 conversion->GetDexPc(),
2197 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 }
2204 break;
2205
Roland Levillain981e4542014-11-14 11:47:14 +00002206 case Primitive::kPrimChar:
2207 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002208 case Primitive::kPrimBoolean:
2209 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002210 case Primitive::kPrimByte:
2211 case Primitive::kPrimShort:
2212 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002213 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002214 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002215 break;
2216
2217 default:
2218 LOG(FATAL) << "Unexpected type conversion from " << input_type
2219 << " to " << result_type;
2220 }
2221 break;
2222
Roland Levillaindff1f282014-11-05 14:15:05 +00002223 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002224 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002225 case Primitive::kPrimBoolean:
2226 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002227 case Primitive::kPrimByte:
2228 case Primitive::kPrimShort:
2229 case Primitive::kPrimInt:
2230 case Primitive::kPrimChar: {
2231 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002232 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2233 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002234 break;
2235 }
2236
Roland Levillain5b3ee562015-04-14 16:02:41 +01002237 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002238 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002239 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2240 conversion,
2241 conversion->GetDexPc(),
2242 nullptr);
Roland Levillain6d0e4832014-11-27 18:31:21 +00002243 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002244
Roland Levillaincff13742014-11-17 14:32:17 +00002245 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002246 // Processing a Dex `double-to-float' instruction.
2247 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2248 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002249 break;
2250
2251 default:
2252 LOG(FATAL) << "Unexpected type conversion from " << input_type
2253 << " to " << result_type;
2254 };
2255 break;
2256
Roland Levillaindff1f282014-11-05 14:15:05 +00002257 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002258 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002259 case Primitive::kPrimBoolean:
2260 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002261 case Primitive::kPrimByte:
2262 case Primitive::kPrimShort:
2263 case Primitive::kPrimInt:
2264 case Primitive::kPrimChar: {
2265 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002266 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002267 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2268 out.AsFpuRegisterPairLow<SRegister>());
2269 break;
2270 }
2271
Roland Levillain647b9ed2014-11-27 12:06:00 +00002272 case Primitive::kPrimLong: {
2273 // Processing a Dex `long-to-double' instruction.
2274 Register low = in.AsRegisterPairLow<Register>();
2275 Register high = in.AsRegisterPairHigh<Register>();
2276 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2277 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002278 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002279 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002280 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2281 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002282
Roland Levillain682393c2015-04-14 15:57:52 +01002283 // temp_d = int-to-double(high)
2284 __ vmovsr(temp_s, high);
2285 __ vcvtdi(temp_d, temp_s);
2286 // constant_d = k2Pow32EncodingForDouble
2287 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2288 // out_d = unsigned-to-double(low)
2289 __ vmovsr(out_s, low);
2290 __ vcvtdu(out_d, out_s);
2291 // out_d += temp_d * constant_d
2292 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002293 break;
2294 }
2295
Roland Levillaincff13742014-11-17 14:32:17 +00002296 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002297 // Processing a Dex `float-to-double' instruction.
2298 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2299 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002300 break;
2301
2302 default:
2303 LOG(FATAL) << "Unexpected type conversion from " << input_type
2304 << " to " << result_type;
2305 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002306 break;
2307
2308 default:
2309 LOG(FATAL) << "Unexpected type conversion from " << input_type
2310 << " to " << result_type;
2311 }
2312}
2313
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002314void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002315 LocationSummary* locations =
2316 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002317 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002318 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002319 locations->SetInAt(0, Location::RequiresRegister());
2320 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2322 break;
2323 }
2324
2325 case Primitive::kPrimLong: {
2326 locations->SetInAt(0, Location::RequiresRegister());
2327 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002329 break;
2330 }
2331
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002332 case Primitive::kPrimFloat:
2333 case Primitive::kPrimDouble: {
2334 locations->SetInAt(0, Location::RequiresFpuRegister());
2335 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002337 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002338 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002339
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002340 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002341 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002342 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002343}
2344
2345void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2346 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002347 Location out = locations->Out();
2348 Location first = locations->InAt(0);
2349 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002350 switch (add->GetResultType()) {
2351 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002352 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002353 __ add(out.AsRegister<Register>(),
2354 first.AsRegister<Register>(),
2355 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002356 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 __ AddConstant(out.AsRegister<Register>(),
2358 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002359 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002360 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002361 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002362
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002363 case Primitive::kPrimLong: {
2364 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002365 __ adds(out.AsRegisterPairLow<Register>(),
2366 first.AsRegisterPairLow<Register>(),
2367 ShifterOperand(second.AsRegisterPairLow<Register>()));
2368 __ adc(out.AsRegisterPairHigh<Register>(),
2369 first.AsRegisterPairHigh<Register>(),
2370 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002371 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002372 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002373
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002374 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002375 __ vadds(out.AsFpuRegister<SRegister>(),
2376 first.AsFpuRegister<SRegister>(),
2377 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002378 break;
2379
2380 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002381 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2382 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2383 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002384 break;
2385
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002386 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002387 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002388 }
2389}
2390
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002391void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002392 LocationSummary* locations =
2393 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002394 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002395 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002396 locations->SetInAt(0, Location::RequiresRegister());
2397 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2399 break;
2400 }
2401
2402 case Primitive::kPrimLong: {
2403 locations->SetInAt(0, Location::RequiresRegister());
2404 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002406 break;
2407 }
Calin Juravle11351682014-10-23 15:38:15 +01002408 case Primitive::kPrimFloat:
2409 case Primitive::kPrimDouble: {
2410 locations->SetInAt(0, Location::RequiresFpuRegister());
2411 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002412 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002413 break;
Calin Juravle11351682014-10-23 15:38:15 +01002414 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002415 default:
Calin Juravle11351682014-10-23 15:38:15 +01002416 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002417 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002418}
2419
2420void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2421 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002422 Location out = locations->Out();
2423 Location first = locations->InAt(0);
2424 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002425 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002426 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002427 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002428 __ sub(out.AsRegister<Register>(),
2429 first.AsRegister<Register>(),
2430 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002431 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002432 __ AddConstant(out.AsRegister<Register>(),
2433 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002434 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002435 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002436 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002437 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002438
Calin Juravle11351682014-10-23 15:38:15 +01002439 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002440 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002441 __ subs(out.AsRegisterPairLow<Register>(),
2442 first.AsRegisterPairLow<Register>(),
2443 ShifterOperand(second.AsRegisterPairLow<Register>()));
2444 __ sbc(out.AsRegisterPairHigh<Register>(),
2445 first.AsRegisterPairHigh<Register>(),
2446 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002447 break;
Calin Juravle11351682014-10-23 15:38:15 +01002448 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002449
Calin Juravle11351682014-10-23 15:38:15 +01002450 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002451 __ vsubs(out.AsFpuRegister<SRegister>(),
2452 first.AsFpuRegister<SRegister>(),
2453 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002454 break;
Calin Juravle11351682014-10-23 15:38:15 +01002455 }
2456
2457 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002458 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2459 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2460 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002461 break;
2462 }
2463
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002464
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002465 default:
Calin Juravle11351682014-10-23 15:38:15 +01002466 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002467 }
2468}
2469
Calin Juravle34bacdf2014-10-07 20:23:36 +01002470void LocationsBuilderARM::VisitMul(HMul* mul) {
2471 LocationSummary* locations =
2472 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2473 switch (mul->GetResultType()) {
2474 case Primitive::kPrimInt:
2475 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002476 locations->SetInAt(0, Location::RequiresRegister());
2477 locations->SetInAt(1, Location::RequiresRegister());
2478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002479 break;
2480 }
2481
Calin Juravleb5bfa962014-10-21 18:02:24 +01002482 case Primitive::kPrimFloat:
2483 case Primitive::kPrimDouble: {
2484 locations->SetInAt(0, Location::RequiresFpuRegister());
2485 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002487 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002488 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002489
2490 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002491 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002492 }
2493}
2494
2495void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2496 LocationSummary* locations = mul->GetLocations();
2497 Location out = locations->Out();
2498 Location first = locations->InAt(0);
2499 Location second = locations->InAt(1);
2500 switch (mul->GetResultType()) {
2501 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002502 __ mul(out.AsRegister<Register>(),
2503 first.AsRegister<Register>(),
2504 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002505 break;
2506 }
2507 case Primitive::kPrimLong: {
2508 Register out_hi = out.AsRegisterPairHigh<Register>();
2509 Register out_lo = out.AsRegisterPairLow<Register>();
2510 Register in1_hi = first.AsRegisterPairHigh<Register>();
2511 Register in1_lo = first.AsRegisterPairLow<Register>();
2512 Register in2_hi = second.AsRegisterPairHigh<Register>();
2513 Register in2_lo = second.AsRegisterPairLow<Register>();
2514
2515 // Extra checks to protect caused by the existence of R1_R2.
2516 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2517 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2518 DCHECK_NE(out_hi, in1_lo);
2519 DCHECK_NE(out_hi, in2_lo);
2520
2521 // input: in1 - 64 bits, in2 - 64 bits
2522 // output: out
2523 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2524 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2525 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2526
2527 // IP <- in1.lo * in2.hi
2528 __ mul(IP, in1_lo, in2_hi);
2529 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2530 __ mla(out_hi, in1_hi, in2_lo, IP);
2531 // out.lo <- (in1.lo * in2.lo)[31:0];
2532 __ umull(out_lo, IP, in1_lo, in2_lo);
2533 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2534 __ add(out_hi, out_hi, ShifterOperand(IP));
2535 break;
2536 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002537
2538 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002539 __ vmuls(out.AsFpuRegister<SRegister>(),
2540 first.AsFpuRegister<SRegister>(),
2541 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002542 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002543 }
2544
2545 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002546 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2547 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2548 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002549 break;
2550 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002551
2552 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002553 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002554 }
2555}
2556
Zheng Xuc6667102015-05-15 16:08:45 +08002557void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2558 DCHECK(instruction->IsDiv() || instruction->IsRem());
2559 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2560
2561 LocationSummary* locations = instruction->GetLocations();
2562 Location second = locations->InAt(1);
2563 DCHECK(second.IsConstant());
2564
2565 Register out = locations->Out().AsRegister<Register>();
2566 Register dividend = locations->InAt(0).AsRegister<Register>();
2567 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2568 DCHECK(imm == 1 || imm == -1);
2569
2570 if (instruction->IsRem()) {
2571 __ LoadImmediate(out, 0);
2572 } else {
2573 if (imm == 1) {
2574 __ Mov(out, dividend);
2575 } else {
2576 __ rsb(out, dividend, ShifterOperand(0));
2577 }
2578 }
2579}
2580
2581void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2582 DCHECK(instruction->IsDiv() || instruction->IsRem());
2583 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2584
2585 LocationSummary* locations = instruction->GetLocations();
2586 Location second = locations->InAt(1);
2587 DCHECK(second.IsConstant());
2588
2589 Register out = locations->Out().AsRegister<Register>();
2590 Register dividend = locations->InAt(0).AsRegister<Register>();
2591 Register temp = locations->GetTemp(0).AsRegister<Register>();
2592 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Vladimir Marko80afd022015-05-19 18:08:00 +01002593 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002594 DCHECK(IsPowerOfTwo(abs_imm));
2595 int ctz_imm = CTZ(abs_imm);
2596
2597 if (ctz_imm == 1) {
2598 __ Lsr(temp, dividend, 32 - ctz_imm);
2599 } else {
2600 __ Asr(temp, dividend, 31);
2601 __ Lsr(temp, temp, 32 - ctz_imm);
2602 }
2603 __ add(out, temp, ShifterOperand(dividend));
2604
2605 if (instruction->IsDiv()) {
2606 __ Asr(out, out, ctz_imm);
2607 if (imm < 0) {
2608 __ rsb(out, out, ShifterOperand(0));
2609 }
2610 } else {
2611 __ ubfx(out, out, 0, ctz_imm);
2612 __ sub(out, out, ShifterOperand(temp));
2613 }
2614}
2615
2616void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2617 DCHECK(instruction->IsDiv() || instruction->IsRem());
2618 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2619
2620 LocationSummary* locations = instruction->GetLocations();
2621 Location second = locations->InAt(1);
2622 DCHECK(second.IsConstant());
2623
2624 Register out = locations->Out().AsRegister<Register>();
2625 Register dividend = locations->InAt(0).AsRegister<Register>();
2626 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2627 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2628 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2629
2630 int64_t magic;
2631 int shift;
2632 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2633
2634 __ LoadImmediate(temp1, magic);
2635 __ smull(temp2, temp1, dividend, temp1);
2636
2637 if (imm > 0 && magic < 0) {
2638 __ add(temp1, temp1, ShifterOperand(dividend));
2639 } else if (imm < 0 && magic > 0) {
2640 __ sub(temp1, temp1, ShifterOperand(dividend));
2641 }
2642
2643 if (shift != 0) {
2644 __ Asr(temp1, temp1, shift);
2645 }
2646
2647 if (instruction->IsDiv()) {
2648 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2649 } else {
2650 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2651 // TODO: Strength reduction for mls.
2652 __ LoadImmediate(temp2, imm);
2653 __ mls(out, temp1, temp2, dividend);
2654 }
2655}
2656
2657void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2658 DCHECK(instruction->IsDiv() || instruction->IsRem());
2659 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2660
2661 LocationSummary* locations = instruction->GetLocations();
2662 Location second = locations->InAt(1);
2663 DCHECK(second.IsConstant());
2664
2665 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2666 if (imm == 0) {
2667 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2668 } else if (imm == 1 || imm == -1) {
2669 DivRemOneOrMinusOne(instruction);
2670 } else if (IsPowerOfTwo(std::abs(imm))) {
2671 DivRemByPowerOfTwo(instruction);
2672 } else {
2673 DCHECK(imm <= -2 || imm >= 2);
2674 GenerateDivRemWithAnyConstant(instruction);
2675 }
2676}
2677
Calin Juravle7c4954d2014-10-28 16:57:40 +00002678void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002679 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2680 if (div->GetResultType() == Primitive::kPrimLong) {
2681 // pLdiv runtime call.
2682 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002683 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2684 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002685 } else if (div->GetResultType() == Primitive::kPrimInt &&
2686 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2687 // pIdivmod runtime call.
2688 call_kind = LocationSummary::kCall;
2689 }
2690
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2692
Calin Juravle7c4954d2014-10-28 16:57:40 +00002693 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002694 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002695 if (div->InputAt(1)->IsConstant()) {
2696 locations->SetInAt(0, Location::RequiresRegister());
2697 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
2698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2699 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue());
2700 if (abs_imm <= 1) {
2701 // No temp register required.
2702 } else {
2703 locations->AddTemp(Location::RequiresRegister());
2704 if (!IsPowerOfTwo(abs_imm)) {
2705 locations->AddTemp(Location::RequiresRegister());
2706 }
2707 }
2708 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002709 locations->SetInAt(0, Location::RequiresRegister());
2710 locations->SetInAt(1, Location::RequiresRegister());
2711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2712 } else {
2713 InvokeRuntimeCallingConvention calling_convention;
2714 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2715 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2716 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2717 // we only need the former.
2718 locations->SetOut(Location::RegisterLocation(R0));
2719 }
Calin Juravled0d48522014-11-04 16:40:20 +00002720 break;
2721 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002722 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002723 InvokeRuntimeCallingConvention calling_convention;
2724 locations->SetInAt(0, Location::RegisterPairLocation(
2725 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2726 locations->SetInAt(1, Location::RegisterPairLocation(
2727 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002728 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002729 break;
2730 }
2731 case Primitive::kPrimFloat:
2732 case Primitive::kPrimDouble: {
2733 locations->SetInAt(0, Location::RequiresFpuRegister());
2734 locations->SetInAt(1, Location::RequiresFpuRegister());
2735 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2736 break;
2737 }
2738
2739 default:
2740 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2741 }
2742}
2743
2744void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2745 LocationSummary* locations = div->GetLocations();
2746 Location out = locations->Out();
2747 Location first = locations->InAt(0);
2748 Location second = locations->InAt(1);
2749
2750 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002751 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002752 if (second.IsConstant()) {
2753 GenerateDivRemConstantIntegral(div);
2754 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002755 __ sdiv(out.AsRegister<Register>(),
2756 first.AsRegister<Register>(),
2757 second.AsRegister<Register>());
2758 } else {
2759 InvokeRuntimeCallingConvention calling_convention;
2760 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2761 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2762 DCHECK_EQ(R0, out.AsRegister<Register>());
2763
2764 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
2765 }
Calin Juravled0d48522014-11-04 16:40:20 +00002766 break;
2767 }
2768
Calin Juravle7c4954d2014-10-28 16:57:40 +00002769 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002770 InvokeRuntimeCallingConvention calling_convention;
2771 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2772 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2773 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2774 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2775 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002776 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002777
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002778 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002779 break;
2780 }
2781
2782 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002783 __ vdivs(out.AsFpuRegister<SRegister>(),
2784 first.AsFpuRegister<SRegister>(),
2785 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002786 break;
2787 }
2788
2789 case Primitive::kPrimDouble: {
2790 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2791 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2792 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2793 break;
2794 }
2795
2796 default:
2797 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2798 }
2799}
2800
Calin Juravlebacfec32014-11-14 15:54:36 +00002801void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002802 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002803
2804 // Most remainders are implemented in the runtime.
2805 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002806 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
2807 // sdiv will be replaced by other instruction sequence.
2808 call_kind = LocationSummary::kNoCall;
2809 } else if ((rem->GetResultType() == Primitive::kPrimInt)
2810 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002811 // Have hardware divide instruction for int, do it with three instructions.
2812 call_kind = LocationSummary::kNoCall;
2813 }
2814
Calin Juravlebacfec32014-11-14 15:54:36 +00002815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2816
Calin Juravled2ec87d2014-12-08 14:24:46 +00002817 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002818 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002819 if (rem->InputAt(1)->IsConstant()) {
2820 locations->SetInAt(0, Location::RequiresRegister());
2821 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
2822 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2823 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue());
2824 if (abs_imm <= 1) {
2825 // No temp register required.
2826 } else {
2827 locations->AddTemp(Location::RequiresRegister());
2828 if (!IsPowerOfTwo(abs_imm)) {
2829 locations->AddTemp(Location::RequiresRegister());
2830 }
2831 }
2832 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002833 locations->SetInAt(0, Location::RequiresRegister());
2834 locations->SetInAt(1, Location::RequiresRegister());
2835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2836 locations->AddTemp(Location::RequiresRegister());
2837 } else {
2838 InvokeRuntimeCallingConvention calling_convention;
2839 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2840 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2841 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2842 // we only need the latter.
2843 locations->SetOut(Location::RegisterLocation(R1));
2844 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002845 break;
2846 }
2847 case Primitive::kPrimLong: {
2848 InvokeRuntimeCallingConvention calling_convention;
2849 locations->SetInAt(0, Location::RegisterPairLocation(
2850 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2851 locations->SetInAt(1, Location::RegisterPairLocation(
2852 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2853 // The runtime helper puts the output in R2,R3.
2854 locations->SetOut(Location::RegisterPairLocation(R2, R3));
2855 break;
2856 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002857 case Primitive::kPrimFloat: {
2858 InvokeRuntimeCallingConvention calling_convention;
2859 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
2860 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
2861 locations->SetOut(Location::FpuRegisterLocation(S0));
2862 break;
2863 }
2864
Calin Juravlebacfec32014-11-14 15:54:36 +00002865 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002866 InvokeRuntimeCallingConvention calling_convention;
2867 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2868 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
2869 locations->SetInAt(1, Location::FpuRegisterPairLocation(
2870 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
2871 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00002872 break;
2873 }
2874
2875 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002876 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002877 }
2878}
2879
2880void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
2881 LocationSummary* locations = rem->GetLocations();
2882 Location out = locations->Out();
2883 Location first = locations->InAt(0);
2884 Location second = locations->InAt(1);
2885
Calin Juravled2ec87d2014-12-08 14:24:46 +00002886 Primitive::Type type = rem->GetResultType();
2887 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002888 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002889 if (second.IsConstant()) {
2890 GenerateDivRemConstantIntegral(rem);
2891 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002892 Register reg1 = first.AsRegister<Register>();
2893 Register reg2 = second.AsRegister<Register>();
2894 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00002895
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002896 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002897 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002898 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01002899 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002900 } else {
2901 InvokeRuntimeCallingConvention calling_convention;
2902 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2903 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2904 DCHECK_EQ(R1, out.AsRegister<Register>());
2905
2906 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
2907 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002908 break;
2909 }
2910
2911 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002912 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002913 break;
2914 }
2915
Calin Juravled2ec87d2014-12-08 14:24:46 +00002916 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002917 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Calin Juravled2ec87d2014-12-08 14:24:46 +00002918 break;
2919 }
2920
Calin Juravlebacfec32014-11-14 15:54:36 +00002921 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002922 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002923 break;
2924 }
2925
2926 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002927 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002928 }
2929}
2930
Calin Juravled0d48522014-11-04 16:40:20 +00002931void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002932 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2933 ? LocationSummary::kCallOnSlowPath
2934 : LocationSummary::kNoCall;
2935 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002936 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00002937 if (instruction->HasUses()) {
2938 locations->SetOut(Location::SameAsFirstInput());
2939 }
2940}
2941
2942void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07002943 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00002944 codegen_->AddSlowPath(slow_path);
2945
2946 LocationSummary* locations = instruction->GetLocations();
2947 Location value = locations->InAt(0);
2948
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002949 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002950 case Primitive::kPrimByte:
2951 case Primitive::kPrimChar:
2952 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002953 case Primitive::kPrimInt: {
2954 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01002955 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002956 } else {
2957 DCHECK(value.IsConstant()) << value;
2958 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2959 __ b(slow_path->GetEntryLabel());
2960 }
2961 }
2962 break;
2963 }
2964 case Primitive::kPrimLong: {
2965 if (value.IsRegisterPair()) {
2966 __ orrs(IP,
2967 value.AsRegisterPairLow<Register>(),
2968 ShifterOperand(value.AsRegisterPairHigh<Register>()));
2969 __ b(slow_path->GetEntryLabel(), EQ);
2970 } else {
2971 DCHECK(value.IsConstant()) << value;
2972 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2973 __ b(slow_path->GetEntryLabel());
2974 }
2975 }
2976 break;
2977 default:
2978 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2979 }
2980 }
Calin Juravled0d48522014-11-04 16:40:20 +00002981}
2982
Calin Juravle9aec02f2014-11-18 23:06:35 +00002983void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
2984 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2985
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00002986 LocationSummary* locations =
2987 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002988
2989 switch (op->GetResultType()) {
2990 case Primitive::kPrimInt: {
2991 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00002992 if (op->InputAt(1)->IsConstant()) {
2993 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
2994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2995 } else {
2996 locations->SetInAt(1, Location::RequiresRegister());
2997 // Make the output overlap, as it will be used to hold the masked
2998 // second input.
2999 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3000 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003001 break;
3002 }
3003 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003004 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003005 if (op->InputAt(1)->IsConstant()) {
3006 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3007 // For simplicity, use kOutputOverlap even though we only require that low registers
3008 // don't clash with high registers which the register allocator currently guarantees.
3009 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3010 } else {
3011 locations->SetInAt(1, Location::RequiresRegister());
3012 locations->AddTemp(Location::RequiresRegister());
3013 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3014 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003015 break;
3016 }
3017 default:
3018 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3019 }
3020}
3021
3022void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3023 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3024
3025 LocationSummary* locations = op->GetLocations();
3026 Location out = locations->Out();
3027 Location first = locations->InAt(0);
3028 Location second = locations->InAt(1);
3029
3030 Primitive::Type type = op->GetResultType();
3031 switch (type) {
3032 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 Register out_reg = out.AsRegister<Register>();
3034 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003035 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 Register second_reg = second.AsRegister<Register>();
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003037 // Arm doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003038 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003039 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003040 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003041 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003042 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003043 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003044 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003045 }
3046 } else {
3047 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3048 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
3049 if (shift_value == 0) { // arm does not support shifting with 0 immediate.
3050 __ Mov(out_reg, first_reg);
3051 } else if (op->IsShl()) {
3052 __ Lsl(out_reg, first_reg, shift_value);
3053 } else if (op->IsShr()) {
3054 __ Asr(out_reg, first_reg, shift_value);
3055 } else {
3056 __ Lsr(out_reg, first_reg, shift_value);
3057 }
3058 }
3059 break;
3060 }
3061 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003062 Register o_h = out.AsRegisterPairHigh<Register>();
3063 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003064
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003065 Register high = first.AsRegisterPairHigh<Register>();
3066 Register low = first.AsRegisterPairLow<Register>();
3067
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003068 if (second.IsRegister()) {
3069 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003070
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003071 Register second_reg = second.AsRegister<Register>();
3072
3073 if (op->IsShl()) {
3074 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3075 // Shift the high part
3076 __ Lsl(o_h, high, o_l);
3077 // Shift the low part and `or` what overflew on the high part
3078 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3079 __ Lsr(temp, low, temp);
3080 __ orr(o_h, o_h, ShifterOperand(temp));
3081 // If the shift is > 32 bits, override the high part
3082 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3083 __ it(PL);
3084 __ Lsl(o_h, low, temp, PL);
3085 // Shift the low part
3086 __ Lsl(o_l, low, o_l);
3087 } else if (op->IsShr()) {
3088 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3089 // Shift the low part
3090 __ Lsr(o_l, low, o_h);
3091 // Shift the high part and `or` what underflew on the low part
3092 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3093 __ Lsl(temp, high, temp);
3094 __ orr(o_l, o_l, ShifterOperand(temp));
3095 // If the shift is > 32 bits, override the low part
3096 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3097 __ it(PL);
3098 __ Asr(o_l, high, temp, PL);
3099 // Shift the high part
3100 __ Asr(o_h, high, o_h);
3101 } else {
3102 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3103 // same as Shr except we use `Lsr`s and not `Asr`s
3104 __ Lsr(o_l, low, o_h);
3105 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3106 __ Lsl(temp, high, temp);
3107 __ orr(o_l, o_l, ShifterOperand(temp));
3108 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3109 __ it(PL);
3110 __ Lsr(o_l, high, temp, PL);
3111 __ Lsr(o_h, high, o_h);
3112 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003113 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003114 // Register allocator doesn't create partial overlap.
3115 DCHECK_NE(o_l, high);
3116 DCHECK_NE(o_h, low);
3117 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3118 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3119 if (shift_value > 32) {
3120 if (op->IsShl()) {
3121 __ Lsl(o_h, low, shift_value - 32);
3122 __ LoadImmediate(o_l, 0);
3123 } else if (op->IsShr()) {
3124 __ Asr(o_l, high, shift_value - 32);
3125 __ Asr(o_h, high, 31);
3126 } else {
3127 __ Lsr(o_l, high, shift_value - 32);
3128 __ LoadImmediate(o_h, 0);
3129 }
3130 } else if (shift_value == 32) {
3131 if (op->IsShl()) {
3132 __ mov(o_h, ShifterOperand(low));
3133 __ LoadImmediate(o_l, 0);
3134 } else if (op->IsShr()) {
3135 __ mov(o_l, ShifterOperand(high));
3136 __ Asr(o_h, high, 31);
3137 } else {
3138 __ mov(o_l, ShifterOperand(high));
3139 __ LoadImmediate(o_h, 0);
3140 }
3141 } else { // shift_value < 32
3142 if (op->IsShl()) {
3143 __ Lsl(o_h, high, shift_value);
3144 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3145 __ Lsl(o_l, low, shift_value);
3146 } else if (op->IsShr()) {
3147 __ Lsr(o_l, low, shift_value);
3148 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3149 __ Asr(o_h, high, shift_value);
3150 } else {
3151 __ Lsr(o_l, low, shift_value);
3152 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3153 __ Lsr(o_h, high, shift_value);
3154 }
3155 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003156 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003157 break;
3158 }
3159 default:
3160 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003161 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003162 }
3163}
3164
3165void LocationsBuilderARM::VisitShl(HShl* shl) {
3166 HandleShift(shl);
3167}
3168
3169void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3170 HandleShift(shl);
3171}
3172
3173void LocationsBuilderARM::VisitShr(HShr* shr) {
3174 HandleShift(shr);
3175}
3176
3177void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3178 HandleShift(shr);
3179}
3180
3181void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3182 HandleShift(ushr);
3183}
3184
3185void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3186 HandleShift(ushr);
3187}
3188
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003189void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003190 LocationSummary* locations =
3191 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003192 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003193 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003194 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003195 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003196}
3197
3198void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
3199 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003200 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003201 // Note: if heap poisoning is enabled, the entry point takes cares
3202 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003203 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003204 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003205 instruction->GetDexPc(),
3206 nullptr);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003207}
3208
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003209void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3210 LocationSummary* locations =
3211 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3212 InvokeRuntimeCallingConvention calling_convention;
3213 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003214 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003215 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003216 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003217}
3218
3219void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3220 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003221 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003222 // Note: if heap poisoning is enabled, the entry point takes cares
3223 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003224 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003225 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003226 instruction->GetDexPc(),
3227 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003228}
3229
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003230void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003231 LocationSummary* locations =
3232 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003233 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3234 if (location.IsStackSlot()) {
3235 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3236 } else if (location.IsDoubleStackSlot()) {
3237 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003238 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003239 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003240}
3241
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003242void InstructionCodeGeneratorARM::VisitParameterValue(
3243 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003244 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003245}
3246
3247void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3248 LocationSummary* locations =
3249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3250 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3251}
3252
3253void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3254 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003255}
3256
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003257void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003258 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003259 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003260 locations->SetInAt(0, Location::RequiresRegister());
3261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003262}
3263
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003264void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3265 LocationSummary* locations = not_->GetLocations();
3266 Location out = locations->Out();
3267 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003268 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003269 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003271 break;
3272
3273 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003274 __ mvn(out.AsRegisterPairLow<Register>(),
3275 ShifterOperand(in.AsRegisterPairLow<Register>()));
3276 __ mvn(out.AsRegisterPairHigh<Register>(),
3277 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003278 break;
3279
3280 default:
3281 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3282 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003283}
3284
David Brazdil66d126e2015-04-03 16:02:44 +01003285void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3286 LocationSummary* locations =
3287 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3288 locations->SetInAt(0, Location::RequiresRegister());
3289 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3290}
3291
3292void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003293 LocationSummary* locations = bool_not->GetLocations();
3294 Location out = locations->Out();
3295 Location in = locations->InAt(0);
3296 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3297}
3298
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003299void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003300 LocationSummary* locations =
3301 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003302 switch (compare->InputAt(0)->GetType()) {
3303 case Primitive::kPrimLong: {
3304 locations->SetInAt(0, Location::RequiresRegister());
3305 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003306 // Output overlaps because it is written before doing the low comparison.
3307 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003308 break;
3309 }
3310 case Primitive::kPrimFloat:
3311 case Primitive::kPrimDouble: {
3312 locations->SetInAt(0, Location::RequiresFpuRegister());
3313 locations->SetInAt(1, Location::RequiresFpuRegister());
3314 locations->SetOut(Location::RequiresRegister());
3315 break;
3316 }
3317 default:
3318 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3319 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003320}
3321
3322void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003323 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003324 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003325 Location left = locations->InAt(0);
3326 Location right = locations->InAt(1);
3327
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003328 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003329 Primitive::Type type = compare->InputAt(0)->GetType();
3330 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003331 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003332 __ cmp(left.AsRegisterPairHigh<Register>(),
3333 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003334 __ b(&less, LT);
3335 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003336 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003337 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003338 __ cmp(left.AsRegisterPairLow<Register>(),
3339 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003340 break;
3341 }
3342 case Primitive::kPrimFloat:
3343 case Primitive::kPrimDouble: {
3344 __ LoadImmediate(out, 0);
3345 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003346 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003347 } else {
3348 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3349 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3350 }
3351 __ vmstat(); // transfer FP status register to ARM APSR.
3352 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003353 break;
3354 }
3355 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003356 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003357 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003358 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003359 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003360
3361 __ Bind(&greater);
3362 __ LoadImmediate(out, 1);
3363 __ b(&done);
3364
3365 __ Bind(&less);
3366 __ LoadImmediate(out, -1);
3367
3368 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003369}
3370
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003371void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003372 LocationSummary* locations =
3373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003374 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3375 locations->SetInAt(i, Location::Any());
3376 }
3377 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003378}
3379
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003380void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003381 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003382}
3383
Calin Juravle52c48962014-12-16 17:02:57 +00003384void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3385 // TODO (ported from quick): revisit Arm barrier kinds
Kenny Root1d8199d2015-06-02 11:01:10 -07003386 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings
Calin Juravle52c48962014-12-16 17:02:57 +00003387 switch (kind) {
3388 case MemBarrierKind::kAnyStore:
3389 case MemBarrierKind::kLoadAny:
3390 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003391 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003392 break;
3393 }
3394 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003395 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003396 break;
3397 }
3398 default:
3399 LOG(FATAL) << "Unexpected memory barrier " << kind;
3400 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003401 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003402}
3403
3404void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3405 uint32_t offset,
3406 Register out_lo,
3407 Register out_hi) {
3408 if (offset != 0) {
3409 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003410 __ add(IP, addr, ShifterOperand(out_lo));
3411 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003412 }
3413 __ ldrexd(out_lo, out_hi, addr);
3414}
3415
3416void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3417 uint32_t offset,
3418 Register value_lo,
3419 Register value_hi,
3420 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003421 Register temp2,
3422 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003423 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003424 if (offset != 0) {
3425 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003426 __ add(IP, addr, ShifterOperand(temp1));
3427 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003428 }
3429 __ Bind(&fail);
3430 // We need a load followed by store. (The address used in a STREX instruction must
3431 // be the same as the address in the most recently executed LDREX instruction.)
3432 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003433 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003434 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003435 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003436}
3437
3438void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3439 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3440
Nicolas Geoffray39468442014-09-02 15:17:15 +01003441 LocationSummary* locations =
3442 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003443 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003444
Calin Juravle52c48962014-12-16 17:02:57 +00003445 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003446 if (Primitive::IsFloatingPointType(field_type)) {
3447 locations->SetInAt(1, Location::RequiresFpuRegister());
3448 } else {
3449 locations->SetInAt(1, Location::RequiresRegister());
3450 }
3451
Calin Juravle52c48962014-12-16 17:02:57 +00003452 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003453 bool generate_volatile = field_info.IsVolatile()
3454 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003455 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003456 bool needs_write_barrier =
3457 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003458 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003459 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003460 if (needs_write_barrier) {
3461 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003462 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003463 } else if (generate_volatile) {
Calin Juravle52c48962014-12-16 17:02:57 +00003464 // Arm encoding have some additional constraints for ldrexd/strexd:
3465 // - registers need to be consecutive
3466 // - the first register should be even but not R14.
3467 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3468 // enable Arm encoding.
3469 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3470
3471 locations->AddTemp(Location::RequiresRegister());
3472 locations->AddTemp(Location::RequiresRegister());
3473 if (field_type == Primitive::kPrimDouble) {
3474 // For doubles we need two more registers to copy the value.
3475 locations->AddTemp(Location::RegisterLocation(R2));
3476 locations->AddTemp(Location::RegisterLocation(R3));
3477 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003478 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003479}
3480
Calin Juravle52c48962014-12-16 17:02:57 +00003481void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003482 const FieldInfo& field_info,
3483 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003484 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3485
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003486 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003487 Register base = locations->InAt(0).AsRegister<Register>();
3488 Location value = locations->InAt(1);
3489
3490 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003491 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003492 Primitive::Type field_type = field_info.GetFieldType();
3493 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003494 bool needs_write_barrier =
3495 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003496
3497 if (is_volatile) {
3498 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3499 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003500
3501 switch (field_type) {
3502 case Primitive::kPrimBoolean:
3503 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003504 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003505 break;
3506 }
3507
3508 case Primitive::kPrimShort:
3509 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003510 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003511 break;
3512 }
3513
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003514 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003515 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003516 if (kPoisonHeapReferences && needs_write_barrier) {
3517 // Note that in the case where `value` is a null reference,
3518 // we do not enter this block, as a null reference does not
3519 // need poisoning.
3520 DCHECK_EQ(field_type, Primitive::kPrimNot);
3521 Register temp = locations->GetTemp(0).AsRegister<Register>();
3522 __ Mov(temp, value.AsRegister<Register>());
3523 __ PoisonHeapReference(temp);
3524 __ StoreToOffset(kStoreWord, temp, base, offset);
3525 } else {
3526 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3527 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003528 break;
3529 }
3530
3531 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003532 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003533 GenerateWideAtomicStore(base, offset,
3534 value.AsRegisterPairLow<Register>(),
3535 value.AsRegisterPairHigh<Register>(),
3536 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003537 locations->GetTemp(1).AsRegister<Register>(),
3538 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003539 } else {
3540 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003541 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003542 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003543 break;
3544 }
3545
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003546 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003547 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003548 break;
3549 }
3550
3551 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003552 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003553 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003554 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3555 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3556
3557 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3558
3559 GenerateWideAtomicStore(base, offset,
3560 value_reg_lo,
3561 value_reg_hi,
3562 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003563 locations->GetTemp(3).AsRegister<Register>(),
3564 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003565 } else {
3566 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003567 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003568 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003569 break;
3570 }
3571
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003572 case Primitive::kPrimVoid:
3573 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003574 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003575 }
Calin Juravle52c48962014-12-16 17:02:57 +00003576
Calin Juravle77520bc2015-01-12 18:45:46 +00003577 // Longs and doubles are handled in the switch.
3578 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3579 codegen_->MaybeRecordImplicitNullCheck(instruction);
3580 }
3581
3582 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3583 Register temp = locations->GetTemp(0).AsRegister<Register>();
3584 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003585 codegen_->MarkGCCard(
3586 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003587 }
3588
Calin Juravle52c48962014-12-16 17:02:57 +00003589 if (is_volatile) {
3590 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3591 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003592}
3593
Calin Juravle52c48962014-12-16 17:02:57 +00003594void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3595 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003596 LocationSummary* locations =
3597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003598 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003599
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003600 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003601 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003602 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003603 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong);
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003604
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003605 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3606 locations->SetOut(Location::RequiresFpuRegister());
3607 } else {
3608 locations->SetOut(Location::RequiresRegister(),
3609 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
3610 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003611 if (volatile_for_double) {
Calin Juravle52c48962014-12-16 17:02:57 +00003612 // Arm encoding have some additional constraints for ldrexd/strexd:
3613 // - registers need to be consecutive
3614 // - the first register should be even but not R14.
3615 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever
3616 // enable Arm encoding.
3617 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3618 locations->AddTemp(Location::RequiresRegister());
3619 locations->AddTemp(Location::RequiresRegister());
3620 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003621}
3622
Vladimir Markod2b4ca22015-09-14 15:13:26 +01003623Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
3624 Opcode opcode) {
3625 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
3626 if (constant->IsConstant() &&
3627 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
3628 return Location::ConstantLocation(constant->AsConstant());
3629 }
3630 return Location::RequiresRegister();
3631}
3632
3633bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
3634 Opcode opcode) {
3635 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
3636 if (Primitive::Is64BitType(input_cst->GetType())) {
3637 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
3638 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
3639 } else {
3640 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
3641 }
3642}
3643
3644bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
3645 ShifterOperand so;
3646 ArmAssembler* assembler = codegen_->GetAssembler();
3647 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
3648 return true;
3649 }
3650 Opcode neg_opcode = kNoOperand;
3651 switch (opcode) {
3652 case AND:
3653 neg_opcode = BIC;
3654 break;
3655 case ORR:
3656 neg_opcode = ORN;
3657 break;
3658 default:
3659 return false;
3660 }
3661 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
3662}
3663
Calin Juravle52c48962014-12-16 17:02:57 +00003664void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
3665 const FieldInfo& field_info) {
3666 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003667
Calin Juravle52c48962014-12-16 17:02:57 +00003668 LocationSummary* locations = instruction->GetLocations();
3669 Register base = locations->InAt(0).AsRegister<Register>();
3670 Location out = locations->Out();
3671 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003672 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003673 Primitive::Type field_type = field_info.GetFieldType();
3674 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3675
3676 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003677 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003678 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003679 break;
3680 }
3681
3682 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003683 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003684 break;
3685 }
3686
3687 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003688 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003689 break;
3690 }
3691
3692 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003693 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003694 break;
3695 }
3696
3697 case Primitive::kPrimInt:
3698 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003699 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003700 break;
3701 }
3702
3703 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003704 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003705 GenerateWideAtomicLoad(base, offset,
3706 out.AsRegisterPairLow<Register>(),
3707 out.AsRegisterPairHigh<Register>());
3708 } else {
3709 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
3710 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003711 break;
3712 }
3713
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003714 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003715 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003716 break;
3717 }
3718
3719 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003720 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003721 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003722 Register lo = locations->GetTemp(0).AsRegister<Register>();
3723 Register hi = locations->GetTemp(1).AsRegister<Register>();
3724 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00003725 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003726 __ vmovdrr(out_reg, lo, hi);
3727 } else {
3728 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003729 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003730 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003731 break;
3732 }
3733
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003734 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003735 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003736 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003737 }
Calin Juravle52c48962014-12-16 17:02:57 +00003738
Calin Juravle77520bc2015-01-12 18:45:46 +00003739 // Doubles are handled in the switch.
3740 if (field_type != Primitive::kPrimDouble) {
3741 codegen_->MaybeRecordImplicitNullCheck(instruction);
3742 }
3743
Calin Juravle52c48962014-12-16 17:02:57 +00003744 if (is_volatile) {
3745 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3746 }
Roland Levillain4d027112015-07-01 15:41:14 +01003747
3748 if (field_type == Primitive::kPrimNot) {
3749 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3750 }
Calin Juravle52c48962014-12-16 17:02:57 +00003751}
3752
3753void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3754 HandleFieldSet(instruction, instruction->GetFieldInfo());
3755}
3756
3757void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003758 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00003759}
3760
3761void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3762 HandleFieldGet(instruction, instruction->GetFieldInfo());
3763}
3764
3765void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3766 HandleFieldGet(instruction, instruction->GetFieldInfo());
3767}
3768
3769void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3770 HandleFieldGet(instruction, instruction->GetFieldInfo());
3771}
3772
3773void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3774 HandleFieldGet(instruction, instruction->GetFieldInfo());
3775}
3776
3777void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3778 HandleFieldSet(instruction, instruction->GetFieldInfo());
3779}
3780
3781void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003782 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003783}
3784
Calin Juravlee460d1d2015-09-29 04:52:17 +01003785void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
3786 HUnresolvedInstanceFieldGet* instruction) {
3787 FieldAccessCallingConventionARM calling_convention;
3788 codegen_->CreateUnresolvedFieldLocationSummary(
3789 instruction, instruction->GetFieldType(), calling_convention);
3790}
3791
3792void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
3793 HUnresolvedInstanceFieldGet* instruction) {
3794 FieldAccessCallingConventionARM calling_convention;
3795 codegen_->GenerateUnresolvedFieldAccess(instruction,
3796 instruction->GetFieldType(),
3797 instruction->GetFieldIndex(),
3798 instruction->GetDexPc(),
3799 calling_convention);
3800}
3801
3802void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
3803 HUnresolvedInstanceFieldSet* instruction) {
3804 FieldAccessCallingConventionARM calling_convention;
3805 codegen_->CreateUnresolvedFieldLocationSummary(
3806 instruction, instruction->GetFieldType(), calling_convention);
3807}
3808
3809void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
3810 HUnresolvedInstanceFieldSet* instruction) {
3811 FieldAccessCallingConventionARM calling_convention;
3812 codegen_->GenerateUnresolvedFieldAccess(instruction,
3813 instruction->GetFieldType(),
3814 instruction->GetFieldIndex(),
3815 instruction->GetDexPc(),
3816 calling_convention);
3817}
3818
3819void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
3820 HUnresolvedStaticFieldGet* instruction) {
3821 FieldAccessCallingConventionARM calling_convention;
3822 codegen_->CreateUnresolvedFieldLocationSummary(
3823 instruction, instruction->GetFieldType(), calling_convention);
3824}
3825
3826void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
3827 HUnresolvedStaticFieldGet* instruction) {
3828 FieldAccessCallingConventionARM calling_convention;
3829 codegen_->GenerateUnresolvedFieldAccess(instruction,
3830 instruction->GetFieldType(),
3831 instruction->GetFieldIndex(),
3832 instruction->GetDexPc(),
3833 calling_convention);
3834}
3835
3836void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
3837 HUnresolvedStaticFieldSet* instruction) {
3838 FieldAccessCallingConventionARM calling_convention;
3839 codegen_->CreateUnresolvedFieldLocationSummary(
3840 instruction, instruction->GetFieldType(), calling_convention);
3841}
3842
3843void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
3844 HUnresolvedStaticFieldSet* instruction) {
3845 FieldAccessCallingConventionARM calling_convention;
3846 codegen_->GenerateUnresolvedFieldAccess(instruction,
3847 instruction->GetFieldType(),
3848 instruction->GetFieldIndex(),
3849 instruction->GetDexPc(),
3850 calling_convention);
3851}
3852
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003853void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003854 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3855 ? LocationSummary::kCallOnSlowPath
3856 : LocationSummary::kNoCall;
3857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00003858 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003859 if (instruction->HasUses()) {
3860 locations->SetOut(Location::SameAsFirstInput());
3861 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862}
3863
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003864void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003865 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3866 return;
3867 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003868 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003869
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003870 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
3871 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3872}
3873
3874void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003875 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003876 codegen_->AddSlowPath(slow_path);
3877
3878 LocationSummary* locations = instruction->GetLocations();
3879 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003880
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003881 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003882}
3883
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003884void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003885 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003886 GenerateImplicitNullCheck(instruction);
3887 } else {
3888 GenerateExplicitNullCheck(instruction);
3889 }
3890}
3891
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003892void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003893 LocationSummary* locations =
3894 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003895 locations->SetInAt(0, Location::RequiresRegister());
3896 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003897 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3898 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3899 } else {
3900 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3901 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003902}
3903
3904void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
3905 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003907 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01003908 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003909
Roland Levillain4d027112015-07-01 15:41:14 +01003910 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003911 case Primitive::kPrimBoolean: {
3912 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003913 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003914 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003915 size_t offset =
3916 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003917 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
3918 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003919 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003920 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
3921 }
3922 break;
3923 }
3924
3925 case Primitive::kPrimByte: {
3926 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003927 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003928 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003929 size_t offset =
3930 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
3932 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003934 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
3935 }
3936 break;
3937 }
3938
3939 case Primitive::kPrimShort: {
3940 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003941 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003942 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003943 size_t offset =
3944 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003945 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
3946 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003947 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003948 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
3949 }
3950 break;
3951 }
3952
3953 case Primitive::kPrimChar: {
3954 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003955 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003956 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003957 size_t offset =
3958 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003959 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
3960 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003961 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003962 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
3963 }
3964 break;
3965 }
3966
3967 case Primitive::kPrimInt:
3968 case Primitive::kPrimNot: {
Roland Levillain33d69032015-06-18 18:20:59 +01003969 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
3970 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003971 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003972 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003973 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003974 size_t offset =
3975 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003976 __ LoadFromOffset(kLoadWord, out, obj, offset);
3977 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003978 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
3980 }
3981 break;
3982 }
3983
3984 case Primitive::kPrimLong: {
3985 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003986 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003987 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003988 size_t offset =
3989 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003990 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003991 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003992 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003993 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003994 }
3995 break;
3996 }
3997
Nicolas Geoffray840e5462015-01-07 16:01:24 +00003998 case Primitive::kPrimFloat: {
3999 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4000 Location out = locations->Out();
4001 DCHECK(out.IsFpuRegister());
4002 if (index.IsConstant()) {
4003 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4004 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset);
4005 } else {
4006 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4007 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset);
4008 }
4009 break;
4010 }
4011
4012 case Primitive::kPrimDouble: {
4013 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4014 Location out = locations->Out();
4015 DCHECK(out.IsFpuRegisterPair());
4016 if (index.IsConstant()) {
4017 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4018 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset);
4019 } else {
4020 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
4021 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4022 }
4023 break;
4024 }
4025
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004026 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004027 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004028 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004029 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004030 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004031
4032 if (type == Primitive::kPrimNot) {
4033 Register out = locations->Out().AsRegister<Register>();
4034 __ MaybeUnpoisonHeapReference(out);
4035 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004036}
4037
4038void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004039 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004040
4041 bool needs_write_barrier =
4042 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004043 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004044
Nicolas Geoffray39468442014-09-02 15:17:15 +01004045 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004046 instruction,
4047 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
4048 locations->SetInAt(0, Location::RequiresRegister());
4049 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4050 if (Primitive::IsFloatingPointType(value_type)) {
4051 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004052 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004053 locations->SetInAt(2, Location::RequiresRegister());
4054 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004055
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004056 if (needs_write_barrier) {
4057 // Temporary registers for the write barrier.
4058 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4059 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004060 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004061}
4062
4063void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4064 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004065 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004066 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004067 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004068 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004069 bool needs_write_barrier =
4070 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004071
4072 switch (value_type) {
4073 case Primitive::kPrimBoolean:
4074 case Primitive::kPrimByte: {
4075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004076 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004077 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004078 size_t offset =
4079 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004080 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004081 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004082 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004083 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4084 }
4085 break;
4086 }
4087
4088 case Primitive::kPrimShort:
4089 case Primitive::kPrimChar: {
4090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004091 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004092 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004093 size_t offset =
4094 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004095 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004096 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004097 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004098 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4099 }
4100 break;
4101 }
4102
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004103 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4105 Register value = locations->InAt(2).AsRegister<Register>();
4106 Register source = value;
4107
4108 if (instruction->InputAt(2)->IsNullConstant()) {
4109 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004110 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004111 size_t offset =
4112 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004113 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004114 } else {
4115 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004116 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004117 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004118 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004119 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004120 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004121
4122 DCHECK(needs_write_barrier);
4123 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4124 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4125 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4126 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4127 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4128 Label done;
4129 SlowPathCode* slow_path = nullptr;
4130
4131 if (may_need_runtime_call) {
4132 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4133 codegen_->AddSlowPath(slow_path);
4134 if (instruction->GetValueCanBeNull()) {
4135 Label non_zero;
4136 __ CompareAndBranchIfNonZero(value, &non_zero);
4137 if (index.IsConstant()) {
4138 size_t offset =
4139 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4140 __ StoreToOffset(kStoreWord, value, array, offset);
4141 } else {
4142 DCHECK(index.IsRegister()) << index;
4143 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4144 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4145 }
4146 codegen_->MaybeRecordImplicitNullCheck(instruction);
4147 __ b(&done);
4148 __ Bind(&non_zero);
4149 }
4150
4151 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4152 codegen_->MaybeRecordImplicitNullCheck(instruction);
4153 __ MaybeUnpoisonHeapReference(temp1);
4154 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4155 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4156 // No need to poison/unpoison, we're comparing two poisoined references.
4157 __ cmp(temp1, ShifterOperand(temp2));
4158 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4159 Label do_put;
4160 __ b(&do_put, EQ);
4161 __ MaybeUnpoisonHeapReference(temp1);
4162 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4163 // No need to poison/unpoison, we're comparing against null.
4164 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4165 __ Bind(&do_put);
4166 } else {
4167 __ b(slow_path->GetEntryLabel(), NE);
4168 }
4169 }
4170
4171 if (kPoisonHeapReferences) {
4172 // Note that in the case where `value` is a null reference,
4173 // we do not enter this block, as a null reference does not
4174 // need poisoning.
4175 DCHECK_EQ(value_type, Primitive::kPrimNot);
4176 __ Mov(temp1, value);
4177 __ PoisonHeapReference(temp1);
4178 source = temp1;
4179 }
4180
4181 if (index.IsConstant()) {
4182 size_t offset =
4183 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4184 __ StoreToOffset(kStoreWord, source, array, offset);
4185 } else {
4186 DCHECK(index.IsRegister()) << index;
4187 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4188 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4189 }
4190
4191 if (!may_need_runtime_call) {
4192 codegen_->MaybeRecordImplicitNullCheck(instruction);
4193 }
4194
4195 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4196
4197 if (done.IsLinked()) {
4198 __ Bind(&done);
4199 }
4200
4201 if (slow_path != nullptr) {
4202 __ Bind(slow_path->GetExitLabel());
4203 }
4204
4205 break;
4206 }
4207
4208 case Primitive::kPrimInt: {
4209 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4210 Register value = locations->InAt(2).AsRegister<Register>();
4211 if (index.IsConstant()) {
4212 size_t offset =
4213 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4214 __ StoreToOffset(kStoreWord, value, array, offset);
4215 } else {
4216 DCHECK(index.IsRegister()) << index;
4217 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4218 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4219 }
4220
4221 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004222 break;
4223 }
4224
4225 case Primitive::kPrimLong: {
4226 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004227 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004228 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004229 size_t offset =
4230 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004231 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004233 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004234 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004235 }
4236 break;
4237 }
4238
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004239 case Primitive::kPrimFloat: {
4240 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4241 Location value = locations->InAt(2);
4242 DCHECK(value.IsFpuRegister());
4243 if (index.IsConstant()) {
4244 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004245 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004246 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004247 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004248 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4249 }
4250 break;
4251 }
4252
4253 case Primitive::kPrimDouble: {
4254 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4255 Location value = locations->InAt(2);
4256 DCHECK(value.IsFpuRegisterPair());
4257 if (index.IsConstant()) {
4258 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004259 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004260 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004261 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004262 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4263 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004264
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004265 break;
4266 }
4267
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004268 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004269 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004270 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004271 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004272
4273 // Ints and objects are handled in the switch.
4274 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
4275 codegen_->MaybeRecordImplicitNullCheck(instruction);
4276 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004277}
4278
4279void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004280 LocationSummary* locations =
4281 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004282 locations->SetInAt(0, Location::RequiresRegister());
4283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004284}
4285
4286void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4287 LocationSummary* locations = instruction->GetLocations();
4288 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004289 Register obj = locations->InAt(0).AsRegister<Register>();
4290 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004291 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004292 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004293}
4294
4295void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004296 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4297 ? LocationSummary::kCallOnSlowPath
4298 : LocationSummary::kNoCall;
4299 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004300 locations->SetInAt(0, Location::RequiresRegister());
4301 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004302 if (instruction->HasUses()) {
4303 locations->SetOut(Location::SameAsFirstInput());
4304 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004305}
4306
4307void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4308 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004309 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004310 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 codegen_->AddSlowPath(slow_path);
4312
Roland Levillain271ab9c2014-11-27 15:23:57 +00004313 Register index = locations->InAt(0).AsRegister<Register>();
4314 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004315
4316 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004317 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004318}
4319
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004320void CodeGeneratorARM::MarkGCCard(Register temp,
4321 Register card,
4322 Register object,
4323 Register value,
4324 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004325 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004326 if (can_be_null) {
4327 __ CompareAndBranchIfZero(value, &is_null);
4328 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004329 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4330 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4331 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004332 if (can_be_null) {
4333 __ Bind(&is_null);
4334 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004335}
4336
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004337void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4338 temp->SetLocations(nullptr);
4339}
4340
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004341void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004342 // Nothing to do, this is driven by the code generator.
4343}
4344
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004345void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004346 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004347}
4348
4349void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004350 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4351}
4352
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004353void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4354 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4355}
4356
4357void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004358 HBasicBlock* block = instruction->GetBlock();
4359 if (block->GetLoopInformation() != nullptr) {
4360 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4361 // The back edge will generate the suspend check.
4362 return;
4363 }
4364 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4365 // The goto will generate the suspend check.
4366 return;
4367 }
4368 GenerateSuspendCheck(instruction, nullptr);
4369}
4370
4371void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4372 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004373 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004374 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4375 if (slow_path == nullptr) {
4376 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4377 instruction->SetSlowPath(slow_path);
4378 codegen_->AddSlowPath(slow_path);
4379 if (successor != nullptr) {
4380 DCHECK(successor->IsLoopHeader());
4381 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4382 }
4383 } else {
4384 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4385 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004386
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004387 __ LoadFromOffset(
4388 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004389 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004390 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004391 __ Bind(slow_path->GetReturnLabel());
4392 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004393 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004394 __ b(slow_path->GetEntryLabel());
4395 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004396}
4397
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004398ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4399 return codegen_->GetAssembler();
4400}
4401
4402void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004403 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004404 Location source = move->GetSource();
4405 Location destination = move->GetDestination();
4406
4407 if (source.IsRegister()) {
4408 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004409 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004410 } else {
4411 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004412 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004413 SP, destination.GetStackIndex());
4414 }
4415 } else if (source.IsStackSlot()) {
4416 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004417 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004418 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004419 } else if (destination.IsFpuRegister()) {
4420 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004421 } else {
4422 DCHECK(destination.IsStackSlot());
4423 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4424 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4425 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004426 } else if (source.IsFpuRegister()) {
4427 if (destination.IsFpuRegister()) {
4428 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004429 } else {
4430 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004431 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4432 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004433 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004434 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004435 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4436 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004437 } else if (destination.IsRegisterPair()) {
4438 DCHECK(ExpectedPairLayout(destination));
4439 __ LoadFromOffset(
4440 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4441 } else {
4442 DCHECK(destination.IsFpuRegisterPair()) << destination;
4443 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4444 SP,
4445 source.GetStackIndex());
4446 }
4447 } else if (source.IsRegisterPair()) {
4448 if (destination.IsRegisterPair()) {
4449 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4450 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
4451 } else {
4452 DCHECK(destination.IsDoubleStackSlot()) << destination;
4453 DCHECK(ExpectedPairLayout(source));
4454 __ StoreToOffset(
4455 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4456 }
4457 } else if (source.IsFpuRegisterPair()) {
4458 if (destination.IsFpuRegisterPair()) {
4459 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4460 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4461 } else {
4462 DCHECK(destination.IsDoubleStackSlot()) << destination;
4463 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4464 SP,
4465 destination.GetStackIndex());
4466 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004467 } else {
4468 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004469 HConstant* constant = source.GetConstant();
4470 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4471 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004472 if (destination.IsRegister()) {
4473 __ LoadImmediate(destination.AsRegister<Register>(), value);
4474 } else {
4475 DCHECK(destination.IsStackSlot());
4476 __ LoadImmediate(IP, value);
4477 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4478 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004479 } else if (constant->IsLongConstant()) {
4480 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004481 if (destination.IsRegisterPair()) {
4482 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
4483 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004484 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004485 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004486 __ LoadImmediate(IP, Low32Bits(value));
4487 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4488 __ LoadImmediate(IP, High32Bits(value));
4489 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4490 }
4491 } else if (constant->IsDoubleConstant()) {
4492 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004493 if (destination.IsFpuRegisterPair()) {
4494 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004495 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004496 DCHECK(destination.IsDoubleStackSlot()) << destination;
4497 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004498 __ LoadImmediate(IP, Low32Bits(int_value));
4499 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4500 __ LoadImmediate(IP, High32Bits(int_value));
4501 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
4502 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004503 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004504 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004505 float value = constant->AsFloatConstant()->GetValue();
4506 if (destination.IsFpuRegister()) {
4507 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
4508 } else {
4509 DCHECK(destination.IsStackSlot());
4510 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
4511 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4512 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004513 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004514 }
4515}
4516
4517void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
4518 __ Mov(IP, reg);
4519 __ LoadFromOffset(kLoadWord, reg, SP, mem);
4520 __ StoreToOffset(kStoreWord, IP, SP, mem);
4521}
4522
4523void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
4524 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
4525 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
4526 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
4527 SP, mem1 + stack_offset);
4528 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
4529 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
4530 SP, mem2 + stack_offset);
4531 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
4532}
4533
4534void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004535 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004536 Location source = move->GetSource();
4537 Location destination = move->GetDestination();
4538
4539 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004540 DCHECK_NE(source.AsRegister<Register>(), IP);
4541 DCHECK_NE(destination.AsRegister<Register>(), IP);
4542 __ Mov(IP, source.AsRegister<Register>());
4543 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
4544 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004545 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004546 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004547 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004548 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004549 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4550 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004551 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004552 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004553 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004554 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004555 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004556 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004557 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004558 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004559 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4560 destination.AsRegisterPairHigh<Register>(),
4561 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004562 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004563 Register low_reg = source.IsRegisterPair()
4564 ? source.AsRegisterPairLow<Register>()
4565 : destination.AsRegisterPairLow<Register>();
4566 int mem = source.IsRegisterPair()
4567 ? destination.GetStackIndex()
4568 : source.GetStackIndex();
4569 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004570 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004571 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004572 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004573 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004574 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
4575 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004576 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004577 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004578 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004579 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
4580 DRegister reg = source.IsFpuRegisterPair()
4581 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
4582 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
4583 int mem = source.IsFpuRegisterPair()
4584 ? destination.GetStackIndex()
4585 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004586 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004587 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004588 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004589 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
4590 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
4591 : destination.AsFpuRegister<SRegister>();
4592 int mem = source.IsFpuRegister()
4593 ? destination.GetStackIndex()
4594 : source.GetStackIndex();
4595
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004596 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004597 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00004598 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004599 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004600 Exchange(source.GetStackIndex(), destination.GetStackIndex());
4601 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004602 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00004603 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004604 }
4605}
4606
4607void ParallelMoveResolverARM::SpillScratch(int reg) {
4608 __ Push(static_cast<Register>(reg));
4609}
4610
4611void ParallelMoveResolverARM::RestoreScratch(int reg) {
4612 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004613}
4614
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004615void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004616 InvokeRuntimeCallingConvention calling_convention;
4617 CodeGenerator::CreateLoadClassLocationSummary(
4618 cls,
4619 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4620 Location::RegisterLocation(R0));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004621}
4622
4623void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004624 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01004625 if (cls->NeedsAccessCheck()) {
4626 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4627 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4628 cls,
4629 cls->GetDexPc(),
4630 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01004631 return;
4632 }
4633
4634 Register out = locations->Out().AsRegister<Register>();
4635 Register current_method = locations->InAt(0).AsRegister<Register>();
4636 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004637 DCHECK(!cls->CanCallRuntime());
4638 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004639 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004640 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004641 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004642 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004643 __ LoadFromOffset(kLoadWord,
4644 out,
4645 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01004646 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004647 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004648 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004649
Andreas Gampe85b62f22015-09-09 13:15:38 -07004650 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004651 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4652 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004653 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004654 if (cls->MustGenerateClinitCheck()) {
4655 GenerateClassInitializationCheck(slow_path, out);
4656 } else {
4657 __ Bind(slow_path->GetExitLabel());
4658 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004659 }
4660}
4661
4662void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
4663 LocationSummary* locations =
4664 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4665 locations->SetInAt(0, Location::RequiresRegister());
4666 if (check->HasUses()) {
4667 locations->SetOut(Location::SameAsFirstInput());
4668 }
4669}
4670
4671void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004672 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07004673 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004674 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004675 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00004676 GenerateClassInitializationCheck(slow_path,
4677 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004678}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004679
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004680void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07004681 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004682 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
4683 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
4684 __ b(slow_path->GetEntryLabel(), LT);
4685 // Even if the initialized flag is set, we may be in a situation where caches are not synced
4686 // properly. Therefore, we do a memory fence.
4687 __ dmb(ISH);
4688 __ Bind(slow_path->GetExitLabel());
4689}
4690
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004691void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
4692 LocationSummary* locations =
4693 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004694 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004695 locations->SetOut(Location::RequiresRegister());
4696}
4697
4698void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004699 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004700 codegen_->AddSlowPath(slow_path);
4701
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01004702 LocationSummary* locations = load->GetLocations();
4703 Register out = locations->Out().AsRegister<Register>();
4704 Register current_method = locations->InAt(0).AsRegister<Register>();
4705 __ LoadFromOffset(
Mathieu Chartiere401d142015-04-22 13:56:20 -07004706 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Mathieu Chartiereace4582014-11-24 18:29:54 -08004707 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004708 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01004709 // TODO: We will need a read barrier here.
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004710 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00004711 __ Bind(slow_path->GetExitLabel());
4712}
4713
David Brazdilcb1c0552015-08-04 16:22:25 +01004714static int32_t GetExceptionTlsOffset() {
4715 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
4716}
4717
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004718void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
4719 LocationSummary* locations =
4720 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4721 locations->SetOut(Location::RequiresRegister());
4722}
4723
4724void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004725 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004726 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4727}
4728
4729void LocationsBuilderARM::VisitClearException(HClearException* clear) {
4730 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4731}
4732
4733void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004734 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01004735 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004736}
4737
4738void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
4739 LocationSummary* locations =
4740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4741 InvokeRuntimeCallingConvention calling_convention;
4742 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4743}
4744
4745void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
4746 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004747 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00004748}
4749
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004750void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004751 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4752 switch (instruction->GetTypeCheckKind()) {
4753 case TypeCheckKind::kExactCheck:
4754 case TypeCheckKind::kAbstractClassCheck:
4755 case TypeCheckKind::kClassHierarchyCheck:
4756 case TypeCheckKind::kArrayObjectCheck:
4757 call_kind = LocationSummary::kNoCall;
4758 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004759 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004760 case TypeCheckKind::kInterfaceCheck:
4761 call_kind = LocationSummary::kCall;
4762 break;
4763 case TypeCheckKind::kArrayCheck:
4764 call_kind = LocationSummary::kCallOnSlowPath;
4765 break;
4766 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004768 if (call_kind != LocationSummary::kCall) {
4769 locations->SetInAt(0, Location::RequiresRegister());
4770 locations->SetInAt(1, Location::RequiresRegister());
4771 // The out register is used as a temporary, so it overlaps with the inputs.
4772 // Note that TypeCheckSlowPathARM uses this register too.
4773 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4774 } else {
4775 InvokeRuntimeCallingConvention calling_convention;
4776 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4777 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4778 locations->SetOut(Location::RegisterLocation(R0));
4779 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004780}
4781
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004782void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004783 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004784 Register obj = locations->InAt(0).AsRegister<Register>();
4785 Register cls = locations->InAt(1).AsRegister<Register>();
4786 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004787 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004788 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4789 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4790 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004791 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07004792 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004793
4794 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004795 // avoid null check if we know obj is not null.
4796 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00004797 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004798 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004799
Calin Juravle98893e12015-10-02 21:05:03 +01004800 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004801 // This is safe, as the register is caller-save, and the object must be in another
4802 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01004803 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
4804 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004805 ? obj
4806 : out;
4807 __ LoadFromOffset(kLoadWord, target, obj, class_offset);
4808 __ MaybeUnpoisonHeapReference(target);
4809
4810 switch (instruction->GetTypeCheckKind()) {
4811 case TypeCheckKind::kExactCheck: {
4812 __ cmp(out, ShifterOperand(cls));
4813 // Classes must be equal for the instanceof to succeed.
4814 __ b(&zero, NE);
4815 __ LoadImmediate(out, 1);
4816 __ b(&done);
4817 break;
4818 }
4819 case TypeCheckKind::kAbstractClassCheck: {
4820 // If the class is abstract, we eagerly fetch the super class of the
4821 // object to avoid doing a comparison we know will fail.
4822 Label loop;
4823 __ Bind(&loop);
4824 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4825 __ MaybeUnpoisonHeapReference(out);
4826 // If `out` is null, we use it for the result, and jump to `done`.
4827 __ CompareAndBranchIfZero(out, &done);
4828 __ cmp(out, ShifterOperand(cls));
4829 __ b(&loop, NE);
4830 __ LoadImmediate(out, 1);
4831 if (zero.IsLinked()) {
4832 __ b(&done);
4833 }
4834 break;
4835 }
4836 case TypeCheckKind::kClassHierarchyCheck: {
4837 // Walk over the class hierarchy to find a match.
4838 Label loop, success;
4839 __ Bind(&loop);
4840 __ cmp(out, ShifterOperand(cls));
4841 __ b(&success, EQ);
4842 __ LoadFromOffset(kLoadWord, out, out, super_offset);
4843 __ MaybeUnpoisonHeapReference(out);
4844 __ CompareAndBranchIfNonZero(out, &loop);
4845 // If `out` is null, we use it for the result, and jump to `done`.
4846 __ b(&done);
4847 __ Bind(&success);
4848 __ LoadImmediate(out, 1);
4849 if (zero.IsLinked()) {
4850 __ b(&done);
4851 }
4852 break;
4853 }
4854 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004855 // Do an exact check.
4856 Label exact_check;
4857 __ cmp(out, ShifterOperand(cls));
4858 __ b(&exact_check, EQ);
4859 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004860 __ LoadFromOffset(kLoadWord, out, out, component_offset);
4861 __ MaybeUnpoisonHeapReference(out);
4862 // If `out` is null, we use it for the result, and jump to `done`.
4863 __ CompareAndBranchIfZero(out, &done);
4864 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4865 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4866 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004867 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004868 __ LoadImmediate(out, 1);
4869 __ b(&done);
4870 break;
4871 }
4872 case TypeCheckKind::kArrayCheck: {
4873 __ cmp(out, ShifterOperand(cls));
4874 DCHECK(locations->OnlyCallsOnSlowPath());
4875 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4876 instruction, /* is_fatal */ false);
4877 codegen_->AddSlowPath(slow_path);
4878 __ b(slow_path->GetEntryLabel(), NE);
4879 __ LoadImmediate(out, 1);
4880 if (zero.IsLinked()) {
4881 __ b(&done);
4882 }
4883 break;
4884 }
Calin Juravle98893e12015-10-02 21:05:03 +01004885 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004886 case TypeCheckKind::kInterfaceCheck:
4887 default: {
4888 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
4889 instruction,
4890 instruction->GetDexPc(),
4891 nullptr);
4892 if (zero.IsLinked()) {
4893 __ b(&done);
4894 }
4895 break;
4896 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004897 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004898
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004899 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004900 __ Bind(&zero);
4901 __ LoadImmediate(out, 0);
4902 }
4903
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004904 if (done.IsLinked()) {
4905 __ Bind(&done);
4906 }
4907
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004908 if (slow_path != nullptr) {
4909 __ Bind(slow_path->GetExitLabel());
4910 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00004911}
4912
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004913void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004914 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4915 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4916
4917 switch (instruction->GetTypeCheckKind()) {
4918 case TypeCheckKind::kExactCheck:
4919 case TypeCheckKind::kAbstractClassCheck:
4920 case TypeCheckKind::kClassHierarchyCheck:
4921 case TypeCheckKind::kArrayObjectCheck:
4922 call_kind = throws_into_catch
4923 ? LocationSummary::kCallOnSlowPath
4924 : LocationSummary::kNoCall;
4925 break;
Calin Juravle98893e12015-10-02 21:05:03 +01004926 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004927 case TypeCheckKind::kInterfaceCheck:
4928 call_kind = LocationSummary::kCall;
4929 break;
4930 case TypeCheckKind::kArrayCheck:
4931 call_kind = LocationSummary::kCallOnSlowPath;
4932 break;
4933 }
4934
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004935 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004936 instruction, call_kind);
4937 if (call_kind != LocationSummary::kCall) {
4938 locations->SetInAt(0, Location::RequiresRegister());
4939 locations->SetInAt(1, Location::RequiresRegister());
4940 // Note that TypeCheckSlowPathARM uses this register too.
4941 locations->AddTemp(Location::RequiresRegister());
4942 } else {
4943 InvokeRuntimeCallingConvention calling_convention;
4944 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4945 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4946 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004947}
4948
4949void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
4950 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004951 Register obj = locations->InAt(0).AsRegister<Register>();
4952 Register cls = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004953 Register temp = locations->WillCall()
4954 ? Register(kNoRegister)
4955 : locations->GetTemp(0).AsRegister<Register>();
4956
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004957 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004958 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4959 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4960 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4961 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00004962
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004963 if (!locations->WillCall()) {
4964 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(
4965 instruction, !locations->CanCall());
4966 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01004967 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004968
4969 Label done;
4970 // Avoid null check if we know obj is not null.
4971 if (instruction->MustDoNullCheck()) {
4972 __ CompareAndBranchIfZero(obj, &done);
4973 }
4974
4975 if (locations->WillCall()) {
4976 __ LoadFromOffset(kLoadWord, obj, obj, class_offset);
4977 __ MaybeUnpoisonHeapReference(obj);
4978 } else {
4979 __ LoadFromOffset(kLoadWord, temp, obj, class_offset);
4980 __ MaybeUnpoisonHeapReference(temp);
4981 }
4982
4983 switch (instruction->GetTypeCheckKind()) {
4984 case TypeCheckKind::kExactCheck:
4985 case TypeCheckKind::kArrayCheck: {
4986 __ cmp(temp, ShifterOperand(cls));
4987 // Jump to slow path for throwing the exception or doing a
4988 // more involved array check.
4989 __ b(slow_path->GetEntryLabel(), NE);
4990 break;
4991 }
4992 case TypeCheckKind::kAbstractClassCheck: {
4993 // If the class is abstract, we eagerly fetch the super class of the
4994 // object to avoid doing a comparison we know will fail.
4995 Label loop;
4996 __ Bind(&loop);
4997 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
4998 __ MaybeUnpoisonHeapReference(temp);
4999 // Jump to the slow path to throw the exception.
5000 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
5001 __ cmp(temp, ShifterOperand(cls));
5002 __ b(&loop, NE);
5003 break;
5004 }
5005 case TypeCheckKind::kClassHierarchyCheck: {
5006 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005007 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005008 __ Bind(&loop);
5009 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005010 __ b(&done, EQ);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005011 __ LoadFromOffset(kLoadWord, temp, temp, super_offset);
5012 __ MaybeUnpoisonHeapReference(temp);
5013 __ CompareAndBranchIfNonZero(temp, &loop);
5014 // Jump to the slow path to throw the exception.
5015 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005016 break;
5017 }
5018 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005019 // Do an exact check.
5020 __ cmp(temp, ShifterOperand(cls));
5021 __ b(&done, EQ);
5022 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005023 __ LoadFromOffset(kLoadWord, temp, temp, component_offset);
5024 __ MaybeUnpoisonHeapReference(temp);
5025 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel());
5026 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
5027 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5028 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel());
5029 break;
5030 }
Calin Juravle98893e12015-10-02 21:05:03 +01005031 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005032 case TypeCheckKind::kInterfaceCheck:
5033 default:
5034 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5035 instruction,
5036 instruction->GetDexPc(),
5037 nullptr);
5038 break;
5039 }
5040 __ Bind(&done);
5041
5042 if (slow_path != nullptr) {
5043 __ Bind(slow_path->GetExitLabel());
5044 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005045}
5046
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005047void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5048 LocationSummary* locations =
5049 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5050 InvokeRuntimeCallingConvention calling_convention;
5051 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5052}
5053
5054void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5055 codegen_->InvokeRuntime(instruction->IsEnter()
5056 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5057 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005058 instruction->GetDexPc(),
5059 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005060}
5061
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005062void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5063void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5064void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005065
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005066void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005067 LocationSummary* locations =
5068 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5069 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5070 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005071 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005072 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005073 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005075}
5076
5077void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5078 HandleBitwiseOperation(instruction);
5079}
5080
5081void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5082 HandleBitwiseOperation(instruction);
5083}
5084
5085void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5086 HandleBitwiseOperation(instruction);
5087}
5088
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005089void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5090 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5091 if (value == 0xffffffffu) {
5092 if (out != first) {
5093 __ mov(out, ShifterOperand(first));
5094 }
5095 return;
5096 }
5097 if (value == 0u) {
5098 __ mov(out, ShifterOperand(0));
5099 return;
5100 }
5101 ShifterOperand so;
5102 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5103 __ and_(out, first, so);
5104 } else {
5105 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5106 __ bic(out, first, ShifterOperand(~value));
5107 }
5108}
5109
5110void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5111 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5112 if (value == 0u) {
5113 if (out != first) {
5114 __ mov(out, ShifterOperand(first));
5115 }
5116 return;
5117 }
5118 if (value == 0xffffffffu) {
5119 __ mvn(out, ShifterOperand(0));
5120 return;
5121 }
5122 ShifterOperand so;
5123 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5124 __ orr(out, first, so);
5125 } else {
5126 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5127 __ orn(out, first, ShifterOperand(~value));
5128 }
5129}
5130
5131void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5132 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5133 if (value == 0u) {
5134 if (out != first) {
5135 __ mov(out, ShifterOperand(first));
5136 }
5137 return;
5138 }
5139 __ eor(out, first, ShifterOperand(value));
5140}
5141
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005142void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5143 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005144 Location first = locations->InAt(0);
5145 Location second = locations->InAt(1);
5146 Location out = locations->Out();
5147
5148 if (second.IsConstant()) {
5149 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5150 uint32_t value_low = Low32Bits(value);
5151 if (instruction->GetResultType() == Primitive::kPrimInt) {
5152 Register first_reg = first.AsRegister<Register>();
5153 Register out_reg = out.AsRegister<Register>();
5154 if (instruction->IsAnd()) {
5155 GenerateAndConst(out_reg, first_reg, value_low);
5156 } else if (instruction->IsOr()) {
5157 GenerateOrrConst(out_reg, first_reg, value_low);
5158 } else {
5159 DCHECK(instruction->IsXor());
5160 GenerateEorConst(out_reg, first_reg, value_low);
5161 }
5162 } else {
5163 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5164 uint32_t value_high = High32Bits(value);
5165 Register first_low = first.AsRegisterPairLow<Register>();
5166 Register first_high = first.AsRegisterPairHigh<Register>();
5167 Register out_low = out.AsRegisterPairLow<Register>();
5168 Register out_high = out.AsRegisterPairHigh<Register>();
5169 if (instruction->IsAnd()) {
5170 GenerateAndConst(out_low, first_low, value_low);
5171 GenerateAndConst(out_high, first_high, value_high);
5172 } else if (instruction->IsOr()) {
5173 GenerateOrrConst(out_low, first_low, value_low);
5174 GenerateOrrConst(out_high, first_high, value_high);
5175 } else {
5176 DCHECK(instruction->IsXor());
5177 GenerateEorConst(out_low, first_low, value_low);
5178 GenerateEorConst(out_high, first_high, value_high);
5179 }
5180 }
5181 return;
5182 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005183
5184 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005185 Register first_reg = first.AsRegister<Register>();
5186 ShifterOperand second_reg(second.AsRegister<Register>());
5187 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005188 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005189 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005190 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005191 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005192 } else {
5193 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005194 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005195 }
5196 } else {
5197 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005198 Register first_low = first.AsRegisterPairLow<Register>();
5199 Register first_high = first.AsRegisterPairHigh<Register>();
5200 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5201 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5202 Register out_low = out.AsRegisterPairLow<Register>();
5203 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005204 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005205 __ and_(out_low, first_low, second_low);
5206 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005207 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005208 __ orr(out_low, first_low, second_low);
5209 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005210 } else {
5211 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005212 __ eor(out_low, first_low, second_low);
5213 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005214 }
5215 }
5216}
5217
Vladimir Markodc151b22015-10-15 18:02:30 +01005218HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
5219 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
5220 MethodReference target_method) {
5221 if (desired_dispatch_info.method_load_kind ==
5222 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative) {
5223 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
5224 return HInvokeStaticOrDirect::DispatchInfo {
5225 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
5226 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
5227 0u,
5228 0u
5229 };
5230 }
5231 if (desired_dispatch_info.code_ptr_location ==
5232 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
5233 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
5234 if (&outer_dex_file != target_method.dex_file) {
5235 // Calls across dex files are more likely to exceed the available BL range,
5236 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
5237 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
5238 (desired_dispatch_info.method_load_kind ==
5239 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
5240 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
5241 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5242 return HInvokeStaticOrDirect::DispatchInfo {
5243 desired_dispatch_info.method_load_kind,
5244 code_ptr_location,
5245 desired_dispatch_info.method_load_data,
5246 0u
5247 };
5248 }
5249 }
5250 return desired_dispatch_info;
5251}
5252
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005253void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00005254 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00005255 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00005256 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5257 // LR = code address from literal pool with link-time patch.
5258 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00005259 break;
5260 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5261 // LR = invoke->GetDirectCodePtr();
5262 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00005263 break;
5264 default:
5265 break;
5266 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005267
Vladimir Marko58155012015-08-19 12:49:41 +00005268 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5269 switch (invoke->GetMethodLoadKind()) {
5270 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
5271 // temp = thread->string_init_entrypoint
5272 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
5273 break;
5274 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
5275 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5276 break;
5277 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5278 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5279 break;
5280 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5281 __ LoadLiteral(temp.AsRegister<Register>(),
5282 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5283 break;
5284 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01005285 // TODO: Implement this type.
5286 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5287 LOG(FATAL) << "Unsupported";
5288 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00005289 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
5290 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
5291 Register method_reg;
5292 Register reg = temp.AsRegister<Register>();
5293 if (current_method.IsRegister()) {
5294 method_reg = current_method.AsRegister<Register>();
5295 } else {
5296 DCHECK(invoke->GetLocations()->Intrinsified());
5297 DCHECK(!current_method.IsValid());
5298 method_reg = reg;
5299 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
5300 }
5301 // temp = current_method->dex_cache_resolved_methods_;
5302 __ LoadFromOffset(
Vladimir Marko05792b92015-08-03 11:56:49 +01005303 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset(
5304 kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005305 // temp = temp[index_in_cache]
5306 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
5307 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
5308 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01005309 }
Vladimir Marko58155012015-08-19 12:49:41 +00005310 }
5311
5312 switch (invoke->GetCodePtrLocation()) {
5313 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5314 __ bl(GetFrameEntryLabel());
5315 break;
5316 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01005317 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07005318 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01005319 // Arbitrarily branch to the BL itself, override at link time.
5320 __ bl(&relative_call_patches_.back().label);
5321 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005322 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5323 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5324 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00005325 // LR()
5326 __ blx(LR);
5327 break;
5328 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5329 // LR = callee_method->entry_point_from_quick_compiled_code_
5330 __ LoadFromOffset(
5331 kLoadWord, LR, callee_method.AsRegister<Register>(),
5332 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
5333 // LR()
5334 __ blx(LR);
5335 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005336 }
5337
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08005338 DCHECK(!IsLeafMethod());
5339}
5340
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005341void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
5342 Register temp = temp_location.AsRegister<Register>();
5343 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5344 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
5345 LocationSummary* locations = invoke->GetLocations();
5346 Location receiver = locations->InAt(0);
5347 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5348 // temp = object->GetClass();
5349 DCHECK(receiver.IsRegister());
5350 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5351 MaybeRecordImplicitNullCheck(invoke);
5352 __ MaybeUnpoisonHeapReference(temp);
5353 // temp = temp->GetMethodAt(method_offset);
5354 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
5355 kArmWordSize).Int32Value();
5356 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5357 // LR = temp->GetEntryPoint();
5358 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
5359 // LR();
5360 __ blx(LR);
5361}
5362
Vladimir Marko58155012015-08-19 12:49:41 +00005363void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
5364 DCHECK(linker_patches->empty());
5365 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size();
5366 linker_patches->reserve(size);
5367 for (const auto& entry : method_patches_) {
5368 const MethodReference& target_method = entry.first;
5369 Literal* literal = entry.second;
5370 DCHECK(literal->GetLabel()->IsBound());
5371 uint32_t literal_offset = literal->GetLabel()->Position();
5372 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
5373 target_method.dex_file,
5374 target_method.dex_method_index));
5375 }
5376 for (const auto& entry : call_patches_) {
5377 const MethodReference& target_method = entry.first;
5378 Literal* literal = entry.second;
5379 DCHECK(literal->GetLabel()->IsBound());
5380 uint32_t literal_offset = literal->GetLabel()->Position();
5381 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
5382 target_method.dex_file,
5383 target_method.dex_method_index));
5384 }
5385 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
5386 uint32_t literal_offset = info.label.Position();
5387 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
5388 info.target_method.dex_file,
5389 info.target_method.dex_method_index));
5390 }
5391}
5392
5393Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
5394 MethodToLiteralMap* map) {
5395 // Look up the literal for target_method.
5396 auto lb = map->lower_bound(target_method);
5397 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
5398 return lb->second;
5399 }
5400 // We don't have a literal for this method yet, insert a new one.
5401 Literal* literal = __ NewLiteral<uint32_t>(0u);
5402 map->PutBefore(lb, target_method, literal);
5403 return literal;
5404}
5405
5406Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
5407 return DeduplicateMethodLiteral(target_method, &method_patches_);
5408}
5409
5410Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
5411 return DeduplicateMethodLiteral(target_method, &call_patches_);
5412}
5413
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005414void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005415 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005416 LOG(FATAL) << "Unreachable";
5417}
5418
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005419void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005420 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005421 LOG(FATAL) << "Unreachable";
5422}
5423
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005424void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) {
5425 DCHECK(codegen_->IsBaseline());
5426 LocationSummary* locations =
5427 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5428 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5429}
5430
5431void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5432 DCHECK(codegen_->IsBaseline());
5433 // Will be generated at use site.
5434}
5435
Mark Mendellfe57faa2015-09-18 09:26:15 -04005436// Simple implementation of packed switch - generate cascaded compare/jumps.
5437void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5438 LocationSummary* locations =
5439 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5440 locations->SetInAt(0, Location::RequiresRegister());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07005441 if (switch_instr->GetNumEntries() >= kPackedSwitchJumpTableThreshold &&
5442 codegen_->GetAssembler()->IsThumb()) {
5443 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
5444 if (switch_instr->GetStartValue() != 0) {
5445 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
5446 }
5447 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04005448}
5449
5450void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5451 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07005452 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005453 LocationSummary* locations = switch_instr->GetLocations();
5454 Register value_reg = locations->InAt(0).AsRegister<Register>();
5455 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5456
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07005457 if (num_entries < kPackedSwitchJumpTableThreshold || !codegen_->GetAssembler()->IsThumb()) {
5458 // Create a series of compare/jumps.
5459 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5460 for (uint32_t i = 0; i < num_entries; i++) {
5461 GenerateCompareWithImmediate(value_reg, lower_bound + i);
5462 __ b(codegen_->GetLabelOf(successors[i]), EQ);
5463 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04005464
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07005465 // And the default for any other value.
5466 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5467 __ b(codegen_->GetLabelOf(default_block));
5468 }
5469 } else {
5470 // Create a table lookup.
5471 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
5472
5473 // Materialize a pointer to the switch table
5474 std::vector<Label*> labels(num_entries);
5475 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5476 for (uint32_t i = 0; i < num_entries; i++) {
5477 labels[i] = codegen_->GetLabelOf(successors[i]);
5478 }
5479 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
5480
5481 // Remove the bias.
5482 Register key_reg;
5483 if (lower_bound != 0) {
5484 key_reg = locations->GetTemp(1).AsRegister<Register>();
5485 __ AddConstant(key_reg, value_reg, -lower_bound);
5486 } else {
5487 key_reg = value_reg;
5488 }
5489
5490 // Check whether the value is in the table, jump to default block if not.
5491 __ CmpConstant(key_reg, num_entries - 1);
5492 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
5493
5494 // Load the displacement from the table.
5495 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
5496
5497 // Dispatch is a direct add to the PC (for Thumb2).
5498 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005499 }
5500}
5501
Andreas Gampe85b62f22015-09-09 13:15:38 -07005502void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
5503 if (!trg.IsValid()) {
5504 DCHECK(type == Primitive::kPrimVoid);
5505 return;
5506 }
5507
5508 DCHECK_NE(type, Primitive::kPrimVoid);
5509
5510 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
5511 if (return_loc.Equals(trg)) {
5512 return;
5513 }
5514
5515 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
5516 // with the last branch.
5517 if (type == Primitive::kPrimLong) {
5518 HParallelMove parallel_move(GetGraph()->GetArena());
5519 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
5520 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
5521 GetMoveResolver()->EmitNativeCode(&parallel_move);
5522 } else if (type == Primitive::kPrimDouble) {
5523 HParallelMove parallel_move(GetGraph()->GetArena());
5524 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
5525 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
5526 GetMoveResolver()->EmitNativeCode(&parallel_move);
5527 } else {
5528 // Let the parallel move resolver take care of all of this.
5529 HParallelMove parallel_move(GetGraph()->GetArena());
5530 parallel_move.AddMove(return_loc, trg, type, nullptr);
5531 GetMoveResolver()->EmitNativeCode(&parallel_move);
5532 }
5533}
5534
Roland Levillain4d027112015-07-01 15:41:14 +01005535#undef __
5536#undef QUICK_ENTRY_POINT
5537
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005538} // namespace arm
5539} // namespace art