blob: 272579219f6d1f6448f928ffb782a7f904d2cbba [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
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain62a46b22015-06-01 18:24:13 +010062#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010063#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064
Andreas Gampe85b62f22015-09-09 13:15:38 -070065class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010067 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068
Alexandre Rames67555f72014-11-18 10:55:16 +000069 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010070 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000072 if (instruction_->CanThrowIntoCatchBlock()) {
73 // Live registers will be restored in the catch block if caught.
74 SaveLiveRegisters(codegen, instruction_->GetLocations());
75 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010076 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000077 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000078 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 }
80
Alexandre Rames8158f282015-08-07 10:26:17 +010081 bool IsFatal() const OVERRIDE { return true; }
82
Alexandre Rames9931f312015-06-19 14:47:01 +010083 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
84
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010086 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
88};
89
Andreas Gampe85b62f22015-09-09 13:15:38 -070090class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000091 public:
92 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
93
Alexandre Rames67555f72014-11-18 10:55:16 +000094 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000095 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
96 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000097 if (instruction_->CanThrowIntoCatchBlock()) {
98 // Live registers will be restored in the catch block if caught.
99 SaveLiveRegisters(codegen, instruction_->GetLocations());
100 }
Calin Juravled0d48522014-11-04 16:40:20 +0000101 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000102 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000103 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000104 }
105
Alexandre Rames8158f282015-08-07 10:26:17 +0100106 bool IsFatal() const OVERRIDE { return true; }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
111 HDivZeroCheck* const instruction_;
112 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
113};
114
Andreas Gampe85b62f22015-09-09 13:15:38 -0700115class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000117 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100118 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames67555f72014-11-18 10:55:16 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100124 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000125 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000126 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000127 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 if (successor_ == nullptr) {
129 __ b(GetReturnLabel());
130 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 }
134
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 Label* GetReturnLabel() {
136 DCHECK(successor_ == nullptr);
137 return &return_label_;
138 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100140 HBasicBlock* GetSuccessor() const {
141 return successor_;
142 }
143
Alexandre Rames9931f312015-06-19 14:47:01 +0100144 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
145
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 private:
147 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 // If not null, the block to branch to after the suspend check.
149 HBasicBlock* const successor_;
150
151 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152 Label return_label_;
153
154 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
155};
156
Andreas Gampe85b62f22015-09-09 13:15:38 -0700157class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100159 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
160 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161
Alexandre Rames67555f72014-11-18 10:55:16 +0000162 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100164 LocationSummary* locations = instruction_->GetLocations();
165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 if (instruction_->CanThrowIntoCatchBlock()) {
168 // Live registers will be restored in the catch block if caught.
169 SaveLiveRegisters(codegen, instruction_->GetLocations());
170 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000171 // We're moving two locations to locations that could overlap, so we need a parallel
172 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100175 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000176 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100177 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100179 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
180 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100181 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000182 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000183 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 }
185
Alexandre Rames8158f282015-08-07 10:26:17 +0100186 bool IsFatal() const OVERRIDE { return true; }
187
Alexandre Rames9931f312015-06-19 14:47:01 +0100188 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
189
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100191 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192
193 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
194};
195
Andreas Gampe85b62f22015-09-09 13:15:38 -0700196class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100197 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000198 LoadClassSlowPathARM(HLoadClass* cls,
199 HInstruction* at,
200 uint32_t dex_pc,
201 bool do_clinit)
202 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
203 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
204 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205
Alexandre Rames67555f72014-11-18 10:55:16 +0000206 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 LocationSummary* locations = at_->GetLocations();
208
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
210 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 int32_t entry_point_offset = do_clinit_
216 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
217 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000218 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000219 if (do_clinit_) {
220 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
221 } else {
222 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
223 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224
225 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000226 Location out = locations->Out();
227 if (out.IsValid()) {
228 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
230 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232 __ b(GetExitLabel());
233 }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
236
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100237 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 // The class this slow path will load.
239 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 // The instruction where this slow path is happening.
242 // (Might be the load class or an initialization check).
243 HInstruction* const at_;
244
245 // The dex PC of `at_`.
246 const uint32_t dex_pc_;
247
248 // Whether to initialize the class.
249 const bool do_clinit_;
250
251 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252};
253
Andreas Gampe85b62f22015-09-09 13:15:38 -0700254class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255 public:
256 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
257
Alexandre Rames67555f72014-11-18 10:55:16 +0000258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000259 LocationSummary* locations = instruction_->GetLocations();
260 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
261
262 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
263 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000265
266 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000268 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000270 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
272
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 __ b(GetExitLabel());
275 }
276
Alexandre Rames9931f312015-06-19 14:47:01 +0100277 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
278
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000279 private:
280 HLoadString* const instruction_;
281
282 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
283};
284
Andreas Gampe85b62f22015-09-09 13:15:38 -0700285class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000287 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
288 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100292 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
293 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 DCHECK(instruction_->IsCheckCast()
295 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
298 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000300 if (!is_fatal_) {
301 SaveLiveRegisters(codegen, locations);
302 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
304 // We're moving two locations to locations that could overlap, so we need a parallel
305 // move resolver.
306 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100308 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100310 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100311 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100312 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
313 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100316 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
317 instruction_,
318 instruction_->GetDexPc(),
319 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000320 CheckEntrypointTypes<
321 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
323 } else {
324 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
326 instruction_,
327 instruction_->GetDexPc(),
328 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000329 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 if (!is_fatal_) {
333 RestoreLiveRegisters(codegen, locations);
334 __ b(GetExitLabel());
335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 }
337
Alexandre Rames9931f312015-06-19 14:47:01 +0100338 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340 bool IsFatal() const OVERRIDE { return is_fatal_; }
341
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
347};
348
Andreas Gampe85b62f22015-09-09 13:15:38 -0700349class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700350 public:
Aart Bik42249c32016-01-07 15:33:50 -0800351 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700352 : instruction_(instruction) {}
353
354 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800355 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700356 __ Bind(GetEntryLabel());
357 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800358 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
359 instruction_,
360 instruction_->GetDexPc(),
361 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000362 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700363 }
364
Alexandre Rames9931f312015-06-19 14:47:01 +0100365 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
366
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 private:
Aart Bik42249c32016-01-07 15:33:50 -0800368 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
370};
371
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100372class ArraySetSlowPathARM : public SlowPathCode {
373 public:
374 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
375
376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
377 LocationSummary* locations = instruction_->GetLocations();
378 __ Bind(GetEntryLabel());
379 SaveLiveRegisters(codegen, locations);
380
381 InvokeRuntimeCallingConvention calling_convention;
382 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
383 parallel_move.AddMove(
384 locations->InAt(0),
385 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
386 Primitive::kPrimNot,
387 nullptr);
388 parallel_move.AddMove(
389 locations->InAt(1),
390 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
391 Primitive::kPrimInt,
392 nullptr);
393 parallel_move.AddMove(
394 locations->InAt(2),
395 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
396 Primitive::kPrimNot,
397 nullptr);
398 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
399
400 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
401 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
402 instruction_,
403 instruction_->GetDexPc(),
404 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000405 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406 RestoreLiveRegisters(codegen, locations);
407 __ b(GetExitLabel());
408 }
409
410 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
411
412 private:
413 HInstruction* const instruction_;
414
415 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
416};
417
Roland Levillainc9285912015-12-18 10:38:42 +0000418// Slow path marking an object during a read barrier.
419class ReadBarrierMarkSlowPathARM : public SlowPathCode {
420 public:
421 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location out, Location obj)
422 : instruction_(instruction), out_(out), obj_(obj) {
423 DCHECK(kEmitCompilerReadBarrier);
424 }
425
426 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
427
428 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
429 LocationSummary* locations = instruction_->GetLocations();
430 Register reg_out = out_.AsRegister<Register>();
431 DCHECK(locations->CanCall());
432 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
433 DCHECK(instruction_->IsInstanceFieldGet() ||
434 instruction_->IsStaticFieldGet() ||
435 instruction_->IsArrayGet() ||
436 instruction_->IsLoadClass() ||
437 instruction_->IsLoadString() ||
438 instruction_->IsInstanceOf() ||
439 instruction_->IsCheckCast())
440 << "Unexpected instruction in read barrier marking slow path: "
441 << instruction_->DebugName();
442
443 __ Bind(GetEntryLabel());
444 SaveLiveRegisters(codegen, locations);
445
446 InvokeRuntimeCallingConvention calling_convention;
447 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
448 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
449 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
450 instruction_,
451 instruction_->GetDexPc(),
452 this);
453 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
454 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
455
456 RestoreLiveRegisters(codegen, locations);
457 __ b(GetExitLabel());
458 }
459
460 private:
461 HInstruction* const instruction_;
462 const Location out_;
463 const Location obj_;
464
465 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
466};
467
Roland Levillain3b359c72015-11-17 19:35:12 +0000468// Slow path generating a read barrier for a heap reference.
469class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
470 public:
471 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
472 Location out,
473 Location ref,
474 Location obj,
475 uint32_t offset,
476 Location index)
477 : instruction_(instruction),
478 out_(out),
479 ref_(ref),
480 obj_(obj),
481 offset_(offset),
482 index_(index) {
483 DCHECK(kEmitCompilerReadBarrier);
484 // If `obj` is equal to `out` or `ref`, it means the initial object
485 // has been overwritten by (or after) the heap object reference load
486 // to be instrumented, e.g.:
487 //
488 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000489 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000490 //
491 // In that case, we have lost the information about the original
492 // object, and the emitted read barrier cannot work properly.
493 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
494 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
495 }
496
497 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
498 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
499 LocationSummary* locations = instruction_->GetLocations();
500 Register reg_out = out_.AsRegister<Register>();
501 DCHECK(locations->CanCall());
502 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
503 DCHECK(!instruction_->IsInvoke() ||
504 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillainc9285912015-12-18 10:38:42 +0000505 instruction_->GetLocations()->Intrinsified()))
506 << "Unexpected instruction in read barrier for heap reference slow path: "
507 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000508
509 __ Bind(GetEntryLabel());
510 SaveLiveRegisters(codegen, locations);
511
512 // We may have to change the index's value, but as `index_` is a
513 // constant member (like other "inputs" of this slow path),
514 // introduce a copy of it, `index`.
515 Location index = index_;
516 if (index_.IsValid()) {
517 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
518 if (instruction_->IsArrayGet()) {
519 // Compute the actual memory offset and store it in `index`.
520 Register index_reg = index_.AsRegister<Register>();
521 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
522 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
523 // We are about to change the value of `index_reg` (see the
524 // calls to art::arm::Thumb2Assembler::Lsl and
525 // art::arm::Thumb2Assembler::AddConstant below), but it has
526 // not been saved by the previous call to
527 // art::SlowPathCode::SaveLiveRegisters, as it is a
528 // callee-save register --
529 // art::SlowPathCode::SaveLiveRegisters does not consider
530 // callee-save registers, as it has been designed with the
531 // assumption that callee-save registers are supposed to be
532 // handled by the called function. So, as a callee-save
533 // register, `index_reg` _would_ eventually be saved onto
534 // the stack, but it would be too late: we would have
535 // changed its value earlier. Therefore, we manually save
536 // it here into another freely available register,
537 // `free_reg`, chosen of course among the caller-save
538 // registers (as a callee-save `free_reg` register would
539 // exhibit the same problem).
540 //
541 // Note we could have requested a temporary register from
542 // the register allocator instead; but we prefer not to, as
543 // this is a slow path, and we know we can find a
544 // caller-save register that is available.
545 Register free_reg = FindAvailableCallerSaveRegister(codegen);
546 __ Mov(free_reg, index_reg);
547 index_reg = free_reg;
548 index = Location::RegisterLocation(index_reg);
549 } else {
550 // The initial register stored in `index_` has already been
551 // saved in the call to art::SlowPathCode::SaveLiveRegisters
552 // (as it is not a callee-save register), so we can freely
553 // use it.
554 }
555 // Shifting the index value contained in `index_reg` by the scale
556 // factor (2) cannot overflow in practice, as the runtime is
557 // unable to allocate object arrays with a size larger than
558 // 2^26 - 1 (that is, 2^28 - 4 bytes).
559 __ Lsl(index_reg, index_reg, TIMES_4);
560 static_assert(
561 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
562 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
563 __ AddConstant(index_reg, index_reg, offset_);
564 } else {
565 DCHECK(instruction_->IsInvoke());
566 DCHECK(instruction_->GetLocations()->Intrinsified());
567 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
568 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
569 << instruction_->AsInvoke()->GetIntrinsic();
570 DCHECK_EQ(offset_, 0U);
571 DCHECK(index_.IsRegisterPair());
572 // UnsafeGet's offset location is a register pair, the low
573 // part contains the correct offset.
574 index = index_.ToLow();
575 }
576 }
577
578 // We're moving two or three locations to locations that could
579 // overlap, so we need a parallel move resolver.
580 InvokeRuntimeCallingConvention calling_convention;
581 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
582 parallel_move.AddMove(ref_,
583 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
584 Primitive::kPrimNot,
585 nullptr);
586 parallel_move.AddMove(obj_,
587 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
588 Primitive::kPrimNot,
589 nullptr);
590 if (index.IsValid()) {
591 parallel_move.AddMove(index,
592 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
593 Primitive::kPrimInt,
594 nullptr);
595 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
596 } else {
597 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
598 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
599 }
600 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
601 instruction_,
602 instruction_->GetDexPc(),
603 this);
604 CheckEntrypointTypes<
605 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
606 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
607
608 RestoreLiveRegisters(codegen, locations);
609 __ b(GetExitLabel());
610 }
611
612 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
613
614 private:
615 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
616 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
617 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
618 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
619 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
620 return static_cast<Register>(i);
621 }
622 }
623 // We shall never fail to find a free caller-save register, as
624 // there are more than two core caller-save registers on ARM
625 // (meaning it is possible to find one which is different from
626 // `ref` and `obj`).
627 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
628 LOG(FATAL) << "Could not find a free caller-save register";
629 UNREACHABLE();
630 }
631
632 HInstruction* const instruction_;
633 const Location out_;
634 const Location ref_;
635 const Location obj_;
636 const uint32_t offset_;
637 // An additional location containing an index to an array.
638 // Only used for HArrayGet and the UnsafeGetObject &
639 // UnsafeGetObjectVolatile intrinsics.
640 const Location index_;
641
642 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
643};
644
645// Slow path generating a read barrier for a GC root.
646class ReadBarrierForRootSlowPathARM : public SlowPathCode {
647 public:
648 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Roland Levillainc9285912015-12-18 10:38:42 +0000649 : instruction_(instruction), out_(out), root_(root) {
650 DCHECK(kEmitCompilerReadBarrier);
651 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000652
653 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
654 LocationSummary* locations = instruction_->GetLocations();
655 Register reg_out = out_.AsRegister<Register>();
656 DCHECK(locations->CanCall());
657 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000658 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
659 << "Unexpected instruction in read barrier for GC root slow path: "
660 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000661
662 __ Bind(GetEntryLabel());
663 SaveLiveRegisters(codegen, locations);
664
665 InvokeRuntimeCallingConvention calling_convention;
666 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
667 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
668 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
669 instruction_,
670 instruction_->GetDexPc(),
671 this);
672 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
673 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
674
675 RestoreLiveRegisters(codegen, locations);
676 __ b(GetExitLabel());
677 }
678
679 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
680
681 private:
682 HInstruction* const instruction_;
683 const Location out_;
684 const Location root_;
685
686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
687};
688
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000689#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100690#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700691
Aart Bike9f37602015-10-09 11:15:55 -0700692inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700693 switch (cond) {
694 case kCondEQ: return EQ;
695 case kCondNE: return NE;
696 case kCondLT: return LT;
697 case kCondLE: return LE;
698 case kCondGT: return GT;
699 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700700 case kCondB: return LO;
701 case kCondBE: return LS;
702 case kCondA: return HI;
703 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700704 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100705 LOG(FATAL) << "Unreachable";
706 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700707}
708
Aart Bike9f37602015-10-09 11:15:55 -0700709// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100710inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700711 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100712 case kCondEQ: return EQ;
713 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700714 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100715 case kCondLT: return LO;
716 case kCondLE: return LS;
717 case kCondGT: return HI;
718 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700719 // Unsigned remain unchanged.
720 case kCondB: return LO;
721 case kCondBE: return LS;
722 case kCondA: return HI;
723 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700724 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100725 LOG(FATAL) << "Unreachable";
726 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700727}
728
Vladimir Markod6e069b2016-01-18 11:11:01 +0000729inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
730 // The ARM condition codes can express all the necessary branches, see the
731 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
732 // There is no dex instruction or HIR that would need the missing conditions
733 // "equal or unordered" or "not equal".
734 switch (cond) {
735 case kCondEQ: return EQ;
736 case kCondNE: return NE /* unordered */;
737 case kCondLT: return gt_bias ? CC : LT /* unordered */;
738 case kCondLE: return gt_bias ? LS : LE /* unordered */;
739 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
740 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
741 default:
742 LOG(FATAL) << "UNREACHABLE";
743 UNREACHABLE();
744 }
745}
746
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100747void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100748 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749}
750
751void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100752 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100753}
754
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100755size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
756 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
757 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100758}
759
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100760size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
761 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
762 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100763}
764
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000765size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
766 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
767 return kArmWordSize;
768}
769
770size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
771 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
772 return kArmWordSize;
773}
774
Calin Juravle34166012014-12-19 17:22:29 +0000775CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000776 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100777 const CompilerOptions& compiler_options,
778 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000779 : CodeGenerator(graph,
780 kNumberOfCoreRegisters,
781 kNumberOfSRegisters,
782 kNumberOfRegisterPairs,
783 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
784 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000785 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
786 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100787 compiler_options,
788 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100789 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100790 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100791 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100792 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000793 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000794 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100795 method_patches_(MethodReferenceComparator(),
796 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
797 call_patches_(MethodReferenceComparator(),
798 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000799 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
800 dex_cache_arrays_base_labels_(std::less<HArmDexCacheArraysBase*>(),
801 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700802 // Always save the LR register to mimic Quick.
803 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100804}
805
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000806void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
807 // Ensure that we fix up branches and literal loads and emit the literal pool.
808 __ FinalizeCode();
809
810 // Adjust native pc offsets in stack maps.
811 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
812 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
813 uint32_t new_position = __ GetAdjustedPosition(old_position);
814 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
815 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100816 // Adjust pc offsets for the disassembly information.
817 if (disasm_info_ != nullptr) {
818 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
819 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
820 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
821 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
822 it.second.start = __ GetAdjustedPosition(it.second.start);
823 it.second.end = __ GetAdjustedPosition(it.second.end);
824 }
825 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
826 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
827 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
828 }
829 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000830
831 CodeGenerator::Finalize(allocator);
832}
833
David Brazdil58282f42016-01-14 12:45:10 +0000834void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100835 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100836 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100837
838 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100839 blocked_core_registers_[SP] = true;
840 blocked_core_registers_[LR] = true;
841 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100842
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100844 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100845
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100846 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100847 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100848
David Brazdil58282f42016-01-14 12:45:10 +0000849 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100850 // Stubs do not save callee-save floating point registers. If the graph
851 // is debuggable, we need to deal with these registers differently. For
852 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000853 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
854 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
855 }
856 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100857
858 UpdateBlockedPairRegisters();
859}
860
861void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
862 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
863 ArmManagedRegister current =
864 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
865 if (blocked_core_registers_[current.AsRegisterPairLow()]
866 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
867 blocked_register_pairs_[i] = true;
868 }
869 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870}
871
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100872InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800873 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100874 assembler_(codegen->GetAssembler()),
875 codegen_(codegen) {}
876
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000877void CodeGeneratorARM::ComputeSpillMask() {
878 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
879 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000880 // There is no easy instruction to restore just the PC on thumb2. We spill and
881 // restore another arbitrary register.
882 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000883 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
884 // We use vpush and vpop for saving and restoring floating point registers, which take
885 // a SRegister and the number of registers to save/restore after that SRegister. We
886 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
887 // but in the range.
888 if (fpu_spill_mask_ != 0) {
889 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
890 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
891 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
892 fpu_spill_mask_ |= (1 << i);
893 }
894 }
895}
896
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100897static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100898 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100899}
900
901static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100902 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100903}
904
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000905void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000906 bool skip_overflow_check =
907 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000908 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000909 __ Bind(&frame_entry_label_);
910
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000911 if (HasEmptyFrame()) {
912 return;
913 }
914
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100915 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000916 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
917 __ LoadFromOffset(kLoadWord, IP, IP, 0);
918 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100919 }
920
Andreas Gampe501fd632015-09-10 16:11:06 -0700921 __ PushList(core_spill_mask_);
922 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
923 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000924 if (fpu_spill_mask_ != 0) {
925 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
926 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100927 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100928 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000929 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930 int adjust = GetFrameSize() - FrameEntrySpillSize();
931 __ AddConstant(SP, -adjust);
932 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100933 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000934}
935
936void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000937 if (HasEmptyFrame()) {
938 __ bx(LR);
939 return;
940 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100941 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100942 int adjust = GetFrameSize() - FrameEntrySpillSize();
943 __ AddConstant(SP, adjust);
944 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000945 if (fpu_spill_mask_ != 0) {
946 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
947 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100948 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
949 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000950 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700951 // Pop LR into PC to return.
952 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
953 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
954 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100955 __ cfi().RestoreState();
956 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957}
958
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100959void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700960 Label* label = GetLabelOf(block);
961 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000962}
963
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100964Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
965 switch (load->GetType()) {
966 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100968 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100969
970 case Primitive::kPrimInt:
971 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100972 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100974
975 case Primitive::kPrimBoolean:
976 case Primitive::kPrimByte:
977 case Primitive::kPrimChar:
978 case Primitive::kPrimShort:
979 case Primitive::kPrimVoid:
980 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700981 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100982 }
983
984 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700985 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100986}
987
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100988Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100989 switch (type) {
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimInt:
995 case Primitive::kPrimNot: {
996 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000997 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100999 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001000 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001001 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001002 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001003 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001005 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001006 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001007 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001009 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001011 if (calling_convention.GetRegisterAt(index) == R1) {
1012 // Skip R1, and use R2_R3 instead.
1013 gp_index_++;
1014 index++;
1015 }
1016 }
1017 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1018 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001019 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001020
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001021 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001022 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001023 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001024 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1025 }
1026 }
1027
1028 case Primitive::kPrimFloat: {
1029 uint32_t stack_index = stack_index_++;
1030 if (float_index_ % 2 == 0) {
1031 float_index_ = std::max(double_index_, float_index_);
1032 }
1033 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1034 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1035 } else {
1036 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1037 }
1038 }
1039
1040 case Primitive::kPrimDouble: {
1041 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1042 uint32_t stack_index = stack_index_;
1043 stack_index_ += 2;
1044 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1045 uint32_t index = double_index_;
1046 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001047 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001048 calling_convention.GetFpuRegisterAt(index),
1049 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001050 DCHECK(ExpectedPairLayout(result));
1051 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001052 } else {
1053 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001054 }
1055 }
1056
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001057 case Primitive::kPrimVoid:
1058 LOG(FATAL) << "Unexpected parameter type " << type;
1059 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001060 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001061 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001062}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001063
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001064Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001065 switch (type) {
1066 case Primitive::kPrimBoolean:
1067 case Primitive::kPrimByte:
1068 case Primitive::kPrimChar:
1069 case Primitive::kPrimShort:
1070 case Primitive::kPrimInt:
1071 case Primitive::kPrimNot: {
1072 return Location::RegisterLocation(R0);
1073 }
1074
1075 case Primitive::kPrimFloat: {
1076 return Location::FpuRegisterLocation(S0);
1077 }
1078
1079 case Primitive::kPrimLong: {
1080 return Location::RegisterPairLocation(R0, R1);
1081 }
1082
1083 case Primitive::kPrimDouble: {
1084 return Location::FpuRegisterPairLocation(S0, S1);
1085 }
1086
1087 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001088 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001089 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001090
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001091 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001092}
1093
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001094Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1095 return Location::RegisterLocation(kMethodRegisterArgument);
1096}
1097
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098void CodeGeneratorARM::Move32(Location destination, Location source) {
1099 if (source.Equals(destination)) {
1100 return;
1101 }
1102 if (destination.IsRegister()) {
1103 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001106 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001108 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 } else if (destination.IsFpuRegister()) {
1111 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001112 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001114 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001115 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001116 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001119 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001121 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001123 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001125 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001126 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1127 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 }
1129 }
1130}
1131
1132void CodeGeneratorARM::Move64(Location destination, Location source) {
1133 if (source.Equals(destination)) {
1134 return;
1135 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001136 if (destination.IsRegisterPair()) {
1137 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001138 EmitParallelMoves(
1139 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1140 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001141 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001142 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001143 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1144 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001145 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001146 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001147 } else if (source.IsFpuRegisterPair()) {
1148 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1149 destination.AsRegisterPairHigh<Register>(),
1150 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
1152 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001153 DCHECK(ExpectedPairLayout(destination));
1154 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1155 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001157 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001159 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1160 SP,
1161 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001162 } else if (source.IsRegisterPair()) {
1163 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1164 source.AsRegisterPairLow<Register>(),
1165 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001167 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 } else {
1170 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001172 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 if (source.AsRegisterPairLow<Register>() == R1) {
1174 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001175 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1176 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 SP, destination.GetStackIndex());
1180 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001181 } else if (source.IsFpuRegisterPair()) {
1182 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1183 SP,
1184 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 } else {
1186 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001187 EmitParallelMoves(
1188 Location::StackSlot(source.GetStackIndex()),
1189 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001190 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001191 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001192 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1193 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 }
1195 }
1196}
1197
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001198void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001199 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001200 if (instruction->IsCurrentMethod()) {
1201 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1202 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001203 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001204 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001205 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001206 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1207 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +00001208 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001210 } else {
1211 DCHECK(location.IsStackSlot());
1212 __ LoadImmediate(IP, value);
1213 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1214 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001215 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +00001216 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +00001217 int64_t value = const_to_move->AsLongConstant()->GetValue();
1218 if (location.IsRegisterPair()) {
1219 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
1220 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
1221 } else {
1222 DCHECK(location.IsDoubleStackSlot());
1223 __ LoadImmediate(IP, Low32Bits(value));
1224 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1225 __ LoadImmediate(IP, High32Bits(value));
1226 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
1227 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 }
Roland Levillain476df552014-10-09 17:51:36 +01001229 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1231 switch (instruction->GetType()) {
1232 case Primitive::kPrimBoolean:
1233 case Primitive::kPrimByte:
1234 case Primitive::kPrimChar:
1235 case Primitive::kPrimShort:
1236 case Primitive::kPrimInt:
1237 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 Move32(location, Location::StackSlot(stack_slot));
1240 break;
1241
1242 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 Move64(location, Location::DoubleStackSlot(stack_slot));
1245 break;
1246
1247 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001250 } else if (instruction->IsTemporary()) {
1251 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001252 if (temp_location.IsStackSlot()) {
1253 Move32(location, temp_location);
1254 } else {
1255 DCHECK(temp_location.IsDoubleStackSlot());
1256 Move64(location, temp_location);
1257 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001258 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001259 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 switch (instruction->GetType()) {
1261 case Primitive::kPrimBoolean:
1262 case Primitive::kPrimByte:
1263 case Primitive::kPrimChar:
1264 case Primitive::kPrimShort:
1265 case Primitive::kPrimNot:
1266 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001267 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001268 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269 break;
1270
1271 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001272 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001273 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 break;
1275
1276 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001279 }
1280}
1281
Calin Juravle175dc732015-08-25 15:42:32 +01001282void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1283 DCHECK(location.IsRegister());
1284 __ LoadImmediate(location.AsRegister<Register>(), value);
1285}
1286
Calin Juravlee460d1d2015-09-29 04:52:17 +01001287void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1288 if (Primitive::Is64BitType(dst_type)) {
1289 Move64(dst, src);
1290 } else {
1291 Move32(dst, src);
1292 }
1293}
1294
1295void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1296 if (location.IsRegister()) {
1297 locations->AddTemp(location);
1298 } else if (location.IsRegisterPair()) {
1299 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1300 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1301 } else {
1302 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1303 }
1304}
1305
Calin Juravle175dc732015-08-25 15:42:32 +01001306void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1307 HInstruction* instruction,
1308 uint32_t dex_pc,
1309 SlowPathCode* slow_path) {
1310 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1311 instruction,
1312 dex_pc,
1313 slow_path);
1314}
1315
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001316void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1317 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001318 uint32_t dex_pc,
1319 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001320 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001321 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1322 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001323 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001324}
1325
David Brazdilfc6a86a2015-06-26 10:33:45 +00001326void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001327 DCHECK(!successor->IsExitBlock());
1328
1329 HBasicBlock* block = got->GetBlock();
1330 HInstruction* previous = got->GetPrevious();
1331
1332 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001333 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001334 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1335 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1336 return;
1337 }
1338
1339 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1340 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1341 }
1342 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001343 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001344 }
1345}
1346
David Brazdilfc6a86a2015-06-26 10:33:45 +00001347void LocationsBuilderARM::VisitGoto(HGoto* got) {
1348 got->SetLocations(nullptr);
1349}
1350
1351void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1352 HandleGoto(got, got->GetSuccessor());
1353}
1354
1355void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1356 try_boundary->SetLocations(nullptr);
1357}
1358
1359void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1360 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1361 if (!successor->IsExitBlock()) {
1362 HandleGoto(try_boundary, successor);
1363 }
1364}
1365
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001366void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001367 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001368}
1369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001370void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001371}
1372
Roland Levillain4fa13f62015-07-06 18:11:54 +01001373void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1374 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001375 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001376 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001377 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001378}
1379
1380void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1381 Label* true_label,
1382 Label* false_label) {
1383 LocationSummary* locations = cond->GetLocations();
1384 Location left = locations->InAt(0);
1385 Location right = locations->InAt(1);
1386 IfCondition if_cond = cond->GetCondition();
1387
1388 Register left_high = left.AsRegisterPairHigh<Register>();
1389 Register left_low = left.AsRegisterPairLow<Register>();
1390 IfCondition true_high_cond = if_cond;
1391 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001392 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001393
1394 // Set the conditions for the test, remembering that == needs to be
1395 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001396 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397 switch (if_cond) {
1398 case kCondEQ:
1399 case kCondNE:
1400 // Nothing to do.
1401 break;
1402 case kCondLT:
1403 false_high_cond = kCondGT;
1404 break;
1405 case kCondLE:
1406 true_high_cond = kCondLT;
1407 break;
1408 case kCondGT:
1409 false_high_cond = kCondLT;
1410 break;
1411 case kCondGE:
1412 true_high_cond = kCondGT;
1413 break;
Aart Bike9f37602015-10-09 11:15:55 -07001414 case kCondB:
1415 false_high_cond = kCondA;
1416 break;
1417 case kCondBE:
1418 true_high_cond = kCondB;
1419 break;
1420 case kCondA:
1421 false_high_cond = kCondB;
1422 break;
1423 case kCondAE:
1424 true_high_cond = kCondA;
1425 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001426 }
1427 if (right.IsConstant()) {
1428 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1429 int32_t val_low = Low32Bits(value);
1430 int32_t val_high = High32Bits(value);
1431
Vladimir Markoac6ac102015-12-17 12:14:00 +00001432 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001433 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001434 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001435 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001436 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001437 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001438 __ b(true_label, ARMCondition(true_high_cond));
1439 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001440 }
1441 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001442 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001443 } else {
1444 Register right_high = right.AsRegisterPairHigh<Register>();
1445 Register right_low = right.AsRegisterPairLow<Register>();
1446
1447 __ cmp(left_high, ShifterOperand(right_high));
1448 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001449 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001450 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001451 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001452 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001453 __ b(true_label, ARMCondition(true_high_cond));
1454 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001455 }
1456 // Must be equal high, so compare the lows.
1457 __ cmp(left_low, ShifterOperand(right_low));
1458 }
1459 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001460 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001461 __ b(true_label, final_condition);
1462}
1463
David Brazdil0debae72015-11-12 18:37:00 +00001464void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1465 Label* true_target_in,
1466 Label* false_target_in) {
1467 // Generated branching requires both targets to be explicit. If either of the
1468 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1469 Label fallthrough_target;
1470 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1471 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1472
Roland Levillain4fa13f62015-07-06 18:11:54 +01001473 LocationSummary* locations = condition->GetLocations();
1474 Location left = locations->InAt(0);
1475 Location right = locations->InAt(1);
1476
Roland Levillain4fa13f62015-07-06 18:11:54 +01001477 Primitive::Type type = condition->InputAt(0)->GetType();
1478 switch (type) {
1479 case Primitive::kPrimLong:
1480 GenerateLongComparesAndJumps(condition, true_target, false_target);
1481 break;
1482 case Primitive::kPrimFloat:
1483 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1484 GenerateFPJumps(condition, true_target, false_target);
1485 break;
1486 case Primitive::kPrimDouble:
1487 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1488 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1489 GenerateFPJumps(condition, true_target, false_target);
1490 break;
1491 default:
1492 LOG(FATAL) << "Unexpected compare type " << type;
1493 }
1494
David Brazdil0debae72015-11-12 18:37:00 +00001495 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001496 __ b(false_target);
1497 }
David Brazdil0debae72015-11-12 18:37:00 +00001498
1499 if (fallthrough_target.IsLinked()) {
1500 __ Bind(&fallthrough_target);
1501 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001502}
1503
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001504void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001505 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001506 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001507 Label* false_target) {
1508 HInstruction* cond = instruction->InputAt(condition_input_index);
1509
1510 if (true_target == nullptr && false_target == nullptr) {
1511 // Nothing to do. The code always falls through.
1512 return;
1513 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001514 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001515 if (cond->AsIntConstant()->IsOne()) {
1516 if (true_target != nullptr) {
1517 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001518 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001519 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001520 DCHECK(cond->AsIntConstant()->IsZero());
1521 if (false_target != nullptr) {
1522 __ b(false_target);
1523 }
1524 }
1525 return;
1526 }
1527
1528 // The following code generates these patterns:
1529 // (1) true_target == nullptr && false_target != nullptr
1530 // - opposite condition true => branch to false_target
1531 // (2) true_target != nullptr && false_target == nullptr
1532 // - condition true => branch to true_target
1533 // (3) true_target != nullptr && false_target != nullptr
1534 // - condition true => branch to true_target
1535 // - branch to false_target
1536 if (IsBooleanValueOrMaterializedCondition(cond)) {
1537 // Condition has been materialized, compare the output to 0.
1538 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1539 DCHECK(cond_val.IsRegister());
1540 if (true_target == nullptr) {
1541 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1542 } else {
1543 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001544 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001545 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001546 // Condition has not been materialized. Use its inputs as the comparison and
1547 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001548 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001549
1550 // If this is a long or FP comparison that has been folded into
1551 // the HCondition, generate the comparison directly.
1552 Primitive::Type type = condition->InputAt(0)->GetType();
1553 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1554 GenerateCompareTestAndBranch(condition, true_target, false_target);
1555 return;
1556 }
1557
1558 LocationSummary* locations = cond->GetLocations();
1559 DCHECK(locations->InAt(0).IsRegister());
1560 Register left = locations->InAt(0).AsRegister<Register>();
1561 Location right = locations->InAt(1);
1562 if (right.IsRegister()) {
1563 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001564 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001565 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001566 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001567 }
1568 if (true_target == nullptr) {
1569 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1570 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001571 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001572 }
Dave Allison20dfc792014-06-16 20:44:29 -07001573 }
David Brazdil0debae72015-11-12 18:37:00 +00001574
1575 // If neither branch falls through (case 3), the conditional branch to `true_target`
1576 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1577 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001578 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001579 }
1580}
1581
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001582void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1584 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001585 locations->SetInAt(0, Location::RequiresRegister());
1586 }
1587}
1588
1589void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001590 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1591 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1592 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1593 nullptr : codegen_->GetLabelOf(true_successor);
1594 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1595 nullptr : codegen_->GetLabelOf(false_successor);
1596 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001597}
1598
1599void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1600 LocationSummary* locations = new (GetGraph()->GetArena())
1601 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001602 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001603 locations->SetInAt(0, Location::RequiresRegister());
1604 }
1605}
1606
1607void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001608 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001609 GenerateTestAndBranch(deoptimize,
1610 /* condition_input_index */ 0,
1611 slow_path->GetEntryLabel(),
1612 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001613}
Dave Allison20dfc792014-06-16 20:44:29 -07001614
David Srbecky0cf44932015-12-09 14:09:59 +00001615void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1616 new (GetGraph()->GetArena()) LocationSummary(info);
1617}
1618
1619void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001620 if (codegen_->HasStackMapAtCurrentPc()) {
1621 // Ensure that we do not collide with the stack map of the previous instruction.
1622 __ nop();
1623 }
David Srbecky0cf44932015-12-09 14:09:59 +00001624 codegen_->RecordPcInfo(info, info->GetDexPc());
1625}
1626
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001627void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001628 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001629 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001630 // Handle the long/FP comparisons made in instruction simplification.
1631 switch (cond->InputAt(0)->GetType()) {
1632 case Primitive::kPrimLong:
1633 locations->SetInAt(0, Location::RequiresRegister());
1634 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1635 if (cond->NeedsMaterialization()) {
1636 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1637 }
1638 break;
1639
1640 case Primitive::kPrimFloat:
1641 case Primitive::kPrimDouble:
1642 locations->SetInAt(0, Location::RequiresFpuRegister());
1643 locations->SetInAt(1, Location::RequiresFpuRegister());
1644 if (cond->NeedsMaterialization()) {
1645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1646 }
1647 break;
1648
1649 default:
1650 locations->SetInAt(0, Location::RequiresRegister());
1651 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1652 if (cond->NeedsMaterialization()) {
1653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1654 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001655 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001656}
1657
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001658void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001659 if (!cond->NeedsMaterialization()) {
1660 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001661 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001662
1663 LocationSummary* locations = cond->GetLocations();
1664 Location left = locations->InAt(0);
1665 Location right = locations->InAt(1);
1666 Register out = locations->Out().AsRegister<Register>();
1667 Label true_label, false_label;
1668
1669 switch (cond->InputAt(0)->GetType()) {
1670 default: {
1671 // Integer case.
1672 if (right.IsRegister()) {
1673 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1674 } else {
1675 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001676 __ CmpConstant(left.AsRegister<Register>(),
1677 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001678 }
Aart Bike9f37602015-10-09 11:15:55 -07001679 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001680 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001681 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001682 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001683 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001684 return;
1685 }
1686 case Primitive::kPrimLong:
1687 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1688 break;
1689 case Primitive::kPrimFloat:
1690 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1691 GenerateFPJumps(cond, &true_label, &false_label);
1692 break;
1693 case Primitive::kPrimDouble:
1694 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1695 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1696 GenerateFPJumps(cond, &true_label, &false_label);
1697 break;
1698 }
1699
1700 // Convert the jumps into the result.
1701 Label done_label;
1702
1703 // False case: result = 0.
1704 __ Bind(&false_label);
1705 __ LoadImmediate(out, 0);
1706 __ b(&done_label);
1707
1708 // True case: result = 1.
1709 __ Bind(&true_label);
1710 __ LoadImmediate(out, 1);
1711 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001712}
1713
1714void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001715 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001716}
1717
1718void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001719 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001720}
1721
1722void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001723 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001724}
1725
1726void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001727 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001728}
1729
1730void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001731 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001732}
1733
1734void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001735 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001736}
1737
1738void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001739 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001740}
1741
1742void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001743 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001744}
1745
1746void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001747 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001748}
1749
1750void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001751 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001752}
1753
1754void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001755 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001756}
1757
1758void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001759 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001760}
1761
Aart Bike9f37602015-10-09 11:15:55 -07001762void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001764}
1765
1766void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001768}
1769
1770void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001771 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001772}
1773
1774void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001776}
1777
1778void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001780}
1781
1782void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001784}
1785
1786void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001788}
1789
1790void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001792}
1793
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001794void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001795 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001796}
1797
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001798void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1799 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001800}
1801
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001803 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804}
1805
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001806void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001807 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001808}
1809
1810void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001811 LocationSummary* locations =
1812 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001813 switch (store->InputAt(1)->GetType()) {
1814 case Primitive::kPrimBoolean:
1815 case Primitive::kPrimByte:
1816 case Primitive::kPrimChar:
1817 case Primitive::kPrimShort:
1818 case Primitive::kPrimInt:
1819 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001820 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001821 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1822 break;
1823
1824 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001825 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001826 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1827 break;
1828
1829 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001830 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001831 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001832}
1833
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001834void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835}
1836
1837void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001838 LocationSummary* locations =
1839 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001840 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001841}
1842
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001843void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001844 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001845}
1846
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001847void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1848 LocationSummary* locations =
1849 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1850 locations->SetOut(Location::ConstantLocation(constant));
1851}
1852
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001853void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001854 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001855}
1856
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001858 LocationSummary* locations =
1859 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001860 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001861}
1862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001863void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001864 // Will be generated at use site.
1865}
1866
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001867void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1868 LocationSummary* locations =
1869 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1870 locations->SetOut(Location::ConstantLocation(constant));
1871}
1872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001873void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001874 // Will be generated at use site.
1875}
1876
1877void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1878 LocationSummary* locations =
1879 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1880 locations->SetOut(Location::ConstantLocation(constant));
1881}
1882
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001883void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001884 // Will be generated at use site.
1885}
1886
Calin Juravle27df7582015-04-17 19:12:31 +01001887void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1888 memory_barrier->SetLocations(nullptr);
1889}
1890
1891void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001892 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001893}
1894
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001895void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001896 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001897}
1898
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001899void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001900 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001901}
1902
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001903void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001904 LocationSummary* locations =
1905 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001906 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001907}
1908
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001909void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001911}
1912
Calin Juravle175dc732015-08-25 15:42:32 +01001913void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1914 // The trampoline uses the same calling convention as dex calling conventions,
1915 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1916 // the method_idx.
1917 HandleInvoke(invoke);
1918}
1919
1920void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1921 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1922}
1923
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001924void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001925 // Explicit clinit checks triggered by static invokes must have been pruned by
1926 // art::PrepareForRegisterAllocation.
1927 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001928
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001929 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001930 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001931 codegen_->GetInstructionSetFeatures());
1932 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001933 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1934 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1935 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001936 return;
1937 }
1938
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001939 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001940
1941 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1942 if (invoke->HasPcRelativeDexCache()) {
1943 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1944 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001945}
1946
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001947static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1948 if (invoke->GetLocations()->Intrinsified()) {
1949 IntrinsicCodeGeneratorARM intrinsic(codegen);
1950 intrinsic.Dispatch(invoke);
1951 return true;
1952 }
1953 return false;
1954}
1955
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001956void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001957 // Explicit clinit checks triggered by static invokes must have been pruned by
1958 // art::PrepareForRegisterAllocation.
1959 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001960
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001961 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1962 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001963 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001964
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001965 LocationSummary* locations = invoke->GetLocations();
1966 codegen_->GenerateStaticOrDirectCall(
1967 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001968 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001969}
1970
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001971void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001972 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001973 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001974}
1975
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001976void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001977 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001978 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001979 codegen_->GetInstructionSetFeatures());
1980 if (intrinsic.TryDispatch(invoke)) {
1981 return;
1982 }
1983
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001984 HandleInvoke(invoke);
1985}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001986
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001987void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001988 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1989 return;
1990 }
1991
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001992 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001993 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001994 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001995}
1996
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001997void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1998 HandleInvoke(invoke);
1999 // Add the hidden argument.
2000 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2001}
2002
2003void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2004 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002005 LocationSummary* locations = invoke->GetLocations();
2006 Register temp = locations->GetTemp(0).AsRegister<Register>();
2007 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2009 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002010 Location receiver = locations->InAt(0);
2011 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2012
Roland Levillain3b359c72015-11-17 19:35:12 +00002013 // Set the hidden argument. This is safe to do this here, as R12
2014 // won't be modified thereafter, before the `blx` (call) instruction.
2015 DCHECK_EQ(R12, hidden_reg);
2016 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002017
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002018 if (receiver.IsStackSlot()) {
2019 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002020 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002021 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2022 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002023 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002024 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002025 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002026 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002027 // Instead of simply (possibly) unpoisoning `temp` here, we should
2028 // emit a read barrier for the previous class reference load.
2029 // However this is not required in practice, as this is an
2030 // intermediate/temporary reference and because the current
2031 // concurrent copying collector keeps the from-space memory
2032 // intact/accessible until the end of the marking phase (the
2033 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002034 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002035 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002036 uint32_t entry_point =
2037 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002038 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2039 // LR = temp->GetEntryPoint();
2040 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2041 // LR();
2042 __ blx(LR);
2043 DCHECK(!codegen_->IsLeafMethod());
2044 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2045}
2046
Roland Levillain88cb1752014-10-20 16:36:47 +01002047void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2048 LocationSummary* locations =
2049 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2050 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002051 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002052 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2054 break;
2055 }
2056 case Primitive::kPrimLong: {
2057 locations->SetInAt(0, Location::RequiresRegister());
2058 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002059 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002060 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002061
Roland Levillain88cb1752014-10-20 16:36:47 +01002062 case Primitive::kPrimFloat:
2063 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002064 locations->SetInAt(0, Location::RequiresFpuRegister());
2065 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002066 break;
2067
2068 default:
2069 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2070 }
2071}
2072
2073void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2074 LocationSummary* locations = neg->GetLocations();
2075 Location out = locations->Out();
2076 Location in = locations->InAt(0);
2077 switch (neg->GetResultType()) {
2078 case Primitive::kPrimInt:
2079 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002080 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002081 break;
2082
2083 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002084 DCHECK(in.IsRegisterPair());
2085 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2086 __ rsbs(out.AsRegisterPairLow<Register>(),
2087 in.AsRegisterPairLow<Register>(),
2088 ShifterOperand(0));
2089 // We cannot emit an RSC (Reverse Subtract with Carry)
2090 // instruction here, as it does not exist in the Thumb-2
2091 // instruction set. We use the following approach
2092 // using SBC and SUB instead.
2093 //
2094 // out.hi = -C
2095 __ sbc(out.AsRegisterPairHigh<Register>(),
2096 out.AsRegisterPairHigh<Register>(),
2097 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2098 // out.hi = out.hi - in.hi
2099 __ sub(out.AsRegisterPairHigh<Register>(),
2100 out.AsRegisterPairHigh<Register>(),
2101 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2102 break;
2103
Roland Levillain88cb1752014-10-20 16:36:47 +01002104 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002105 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002106 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002107 break;
2108
Roland Levillain88cb1752014-10-20 16:36:47 +01002109 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002110 DCHECK(in.IsFpuRegisterPair());
2111 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2112 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002113 break;
2114
2115 default:
2116 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2117 }
2118}
2119
Roland Levillaindff1f282014-11-05 14:15:05 +00002120void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002121 Primitive::Type result_type = conversion->GetResultType();
2122 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002123 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002124
Roland Levillain5b3ee562015-04-14 16:02:41 +01002125 // The float-to-long, double-to-long and long-to-float type conversions
2126 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002127 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002128 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2129 && result_type == Primitive::kPrimLong)
2130 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002131 ? LocationSummary::kCall
2132 : LocationSummary::kNoCall;
2133 LocationSummary* locations =
2134 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2135
David Brazdilb2bd1c52015-03-25 11:17:37 +00002136 // The Java language does not allow treating boolean as an integral type but
2137 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002138
Roland Levillaindff1f282014-11-05 14:15:05 +00002139 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002140 case Primitive::kPrimByte:
2141 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002142 case Primitive::kPrimBoolean:
2143 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002144 case Primitive::kPrimShort:
2145 case Primitive::kPrimInt:
2146 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002147 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002148 locations->SetInAt(0, Location::RequiresRegister());
2149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2150 break;
2151
2152 default:
2153 LOG(FATAL) << "Unexpected type conversion from " << input_type
2154 << " to " << result_type;
2155 }
2156 break;
2157
Roland Levillain01a8d712014-11-14 16:27:39 +00002158 case Primitive::kPrimShort:
2159 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002160 case Primitive::kPrimBoolean:
2161 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002162 case Primitive::kPrimByte:
2163 case Primitive::kPrimInt:
2164 case Primitive::kPrimChar:
2165 // Processing a Dex `int-to-short' instruction.
2166 locations->SetInAt(0, Location::RequiresRegister());
2167 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2168 break;
2169
2170 default:
2171 LOG(FATAL) << "Unexpected type conversion from " << input_type
2172 << " to " << result_type;
2173 }
2174 break;
2175
Roland Levillain946e1432014-11-11 17:35:19 +00002176 case Primitive::kPrimInt:
2177 switch (input_type) {
2178 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002179 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002180 locations->SetInAt(0, Location::Any());
2181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2182 break;
2183
2184 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002185 // Processing a Dex `float-to-int' instruction.
2186 locations->SetInAt(0, Location::RequiresFpuRegister());
2187 locations->SetOut(Location::RequiresRegister());
2188 locations->AddTemp(Location::RequiresFpuRegister());
2189 break;
2190
Roland Levillain946e1432014-11-11 17:35:19 +00002191 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002192 // Processing a Dex `double-to-int' instruction.
2193 locations->SetInAt(0, Location::RequiresFpuRegister());
2194 locations->SetOut(Location::RequiresRegister());
2195 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002196 break;
2197
2198 default:
2199 LOG(FATAL) << "Unexpected type conversion from " << input_type
2200 << " to " << result_type;
2201 }
2202 break;
2203
Roland Levillaindff1f282014-11-05 14:15:05 +00002204 case Primitive::kPrimLong:
2205 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002206 case Primitive::kPrimBoolean:
2207 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002208 case Primitive::kPrimByte:
2209 case Primitive::kPrimShort:
2210 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002211 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002212 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002213 locations->SetInAt(0, Location::RequiresRegister());
2214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2215 break;
2216
Roland Levillain624279f2014-12-04 11:54:28 +00002217 case Primitive::kPrimFloat: {
2218 // Processing a Dex `float-to-long' instruction.
2219 InvokeRuntimeCallingConvention calling_convention;
2220 locations->SetInAt(0, Location::FpuRegisterLocation(
2221 calling_convention.GetFpuRegisterAt(0)));
2222 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2223 break;
2224 }
2225
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002226 case Primitive::kPrimDouble: {
2227 // Processing a Dex `double-to-long' instruction.
2228 InvokeRuntimeCallingConvention calling_convention;
2229 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2230 calling_convention.GetFpuRegisterAt(0),
2231 calling_convention.GetFpuRegisterAt(1)));
2232 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002233 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002234 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002235
2236 default:
2237 LOG(FATAL) << "Unexpected type conversion from " << input_type
2238 << " to " << result_type;
2239 }
2240 break;
2241
Roland Levillain981e4542014-11-14 11:47:14 +00002242 case Primitive::kPrimChar:
2243 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002244 case Primitive::kPrimBoolean:
2245 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002246 case Primitive::kPrimByte:
2247 case Primitive::kPrimShort:
2248 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002249 // Processing a Dex `int-to-char' instruction.
2250 locations->SetInAt(0, Location::RequiresRegister());
2251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2252 break;
2253
2254 default:
2255 LOG(FATAL) << "Unexpected type conversion from " << input_type
2256 << " to " << result_type;
2257 }
2258 break;
2259
Roland Levillaindff1f282014-11-05 14:15:05 +00002260 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002261 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002262 case Primitive::kPrimBoolean:
2263 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002264 case Primitive::kPrimByte:
2265 case Primitive::kPrimShort:
2266 case Primitive::kPrimInt:
2267 case Primitive::kPrimChar:
2268 // Processing a Dex `int-to-float' instruction.
2269 locations->SetInAt(0, Location::RequiresRegister());
2270 locations->SetOut(Location::RequiresFpuRegister());
2271 break;
2272
Roland Levillain5b3ee562015-04-14 16:02:41 +01002273 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002274 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002275 InvokeRuntimeCallingConvention calling_convention;
2276 locations->SetInAt(0, Location::RegisterPairLocation(
2277 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2278 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002279 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002280 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002281
Roland Levillaincff13742014-11-17 14:32:17 +00002282 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002283 // Processing a Dex `double-to-float' instruction.
2284 locations->SetInAt(0, Location::RequiresFpuRegister());
2285 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002286 break;
2287
2288 default:
2289 LOG(FATAL) << "Unexpected type conversion from " << input_type
2290 << " to " << result_type;
2291 };
2292 break;
2293
Roland Levillaindff1f282014-11-05 14:15:05 +00002294 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002295 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002296 case Primitive::kPrimBoolean:
2297 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002298 case Primitive::kPrimByte:
2299 case Primitive::kPrimShort:
2300 case Primitive::kPrimInt:
2301 case Primitive::kPrimChar:
2302 // Processing a Dex `int-to-double' instruction.
2303 locations->SetInAt(0, Location::RequiresRegister());
2304 locations->SetOut(Location::RequiresFpuRegister());
2305 break;
2306
2307 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002308 // Processing a Dex `long-to-double' instruction.
2309 locations->SetInAt(0, Location::RequiresRegister());
2310 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002311 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002312 locations->AddTemp(Location::RequiresFpuRegister());
2313 break;
2314
Roland Levillaincff13742014-11-17 14:32:17 +00002315 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002316 // Processing a Dex `float-to-double' instruction.
2317 locations->SetInAt(0, Location::RequiresFpuRegister());
2318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002319 break;
2320
2321 default:
2322 LOG(FATAL) << "Unexpected type conversion from " << input_type
2323 << " to " << result_type;
2324 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002325 break;
2326
2327 default:
2328 LOG(FATAL) << "Unexpected type conversion from " << input_type
2329 << " to " << result_type;
2330 }
2331}
2332
2333void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2334 LocationSummary* locations = conversion->GetLocations();
2335 Location out = locations->Out();
2336 Location in = locations->InAt(0);
2337 Primitive::Type result_type = conversion->GetResultType();
2338 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002339 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002340 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002341 case Primitive::kPrimByte:
2342 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002343 case Primitive::kPrimBoolean:
2344 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002345 case Primitive::kPrimShort:
2346 case Primitive::kPrimInt:
2347 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002348 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002349 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002350 break;
2351
2352 default:
2353 LOG(FATAL) << "Unexpected type conversion from " << input_type
2354 << " to " << result_type;
2355 }
2356 break;
2357
Roland Levillain01a8d712014-11-14 16:27:39 +00002358 case Primitive::kPrimShort:
2359 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002360 case Primitive::kPrimBoolean:
2361 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002362 case Primitive::kPrimByte:
2363 case Primitive::kPrimInt:
2364 case Primitive::kPrimChar:
2365 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002366 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002367 break;
2368
2369 default:
2370 LOG(FATAL) << "Unexpected type conversion from " << input_type
2371 << " to " << result_type;
2372 }
2373 break;
2374
Roland Levillain946e1432014-11-11 17:35:19 +00002375 case Primitive::kPrimInt:
2376 switch (input_type) {
2377 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002378 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002379 DCHECK(out.IsRegister());
2380 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002381 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002382 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002383 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002384 } else {
2385 DCHECK(in.IsConstant());
2386 DCHECK(in.GetConstant()->IsLongConstant());
2387 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002388 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002389 }
2390 break;
2391
Roland Levillain3f8f9362014-12-02 17:45:01 +00002392 case Primitive::kPrimFloat: {
2393 // Processing a Dex `float-to-int' instruction.
2394 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2395 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2396 __ vcvtis(temp, temp);
2397 __ vmovrs(out.AsRegister<Register>(), temp);
2398 break;
2399 }
2400
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002401 case Primitive::kPrimDouble: {
2402 // Processing a Dex `double-to-int' instruction.
2403 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2404 DRegister temp_d = FromLowSToD(temp_s);
2405 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2406 __ vcvtid(temp_s, temp_d);
2407 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002408 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002409 }
Roland Levillain946e1432014-11-11 17:35:19 +00002410
2411 default:
2412 LOG(FATAL) << "Unexpected type conversion from " << input_type
2413 << " to " << result_type;
2414 }
2415 break;
2416
Roland Levillaindff1f282014-11-05 14:15:05 +00002417 case Primitive::kPrimLong:
2418 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002419 case Primitive::kPrimBoolean:
2420 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002421 case Primitive::kPrimByte:
2422 case Primitive::kPrimShort:
2423 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002424 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002425 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002426 DCHECK(out.IsRegisterPair());
2427 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002428 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002429 // Sign extension.
2430 __ Asr(out.AsRegisterPairHigh<Register>(),
2431 out.AsRegisterPairLow<Register>(),
2432 31);
2433 break;
2434
2435 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002436 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002437 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2438 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002439 conversion->GetDexPc(),
2440 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002441 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002442 break;
2443
Roland Levillaindff1f282014-11-05 14:15:05 +00002444 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002445 // Processing a Dex `double-to-long' instruction.
2446 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2447 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002448 conversion->GetDexPc(),
2449 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002450 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 }
2457 break;
2458
Roland Levillain981e4542014-11-14 11:47:14 +00002459 case Primitive::kPrimChar:
2460 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002461 case Primitive::kPrimBoolean:
2462 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002463 case Primitive::kPrimByte:
2464 case Primitive::kPrimShort:
2465 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002466 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002467 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
2474 break;
2475
Roland Levillaindff1f282014-11-05 14:15:05 +00002476 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002477 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002478 case Primitive::kPrimBoolean:
2479 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002480 case Primitive::kPrimByte:
2481 case Primitive::kPrimShort:
2482 case Primitive::kPrimInt:
2483 case Primitive::kPrimChar: {
2484 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002485 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2486 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002487 break;
2488 }
2489
Roland Levillain5b3ee562015-04-14 16:02:41 +01002490 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002491 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002492 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2493 conversion,
2494 conversion->GetDexPc(),
2495 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002496 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002497 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002498
Roland Levillaincff13742014-11-17 14:32:17 +00002499 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002500 // Processing a Dex `double-to-float' instruction.
2501 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2502 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002503 break;
2504
2505 default:
2506 LOG(FATAL) << "Unexpected type conversion from " << input_type
2507 << " to " << result_type;
2508 };
2509 break;
2510
Roland Levillaindff1f282014-11-05 14:15:05 +00002511 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002512 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002513 case Primitive::kPrimBoolean:
2514 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002515 case Primitive::kPrimByte:
2516 case Primitive::kPrimShort:
2517 case Primitive::kPrimInt:
2518 case Primitive::kPrimChar: {
2519 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002520 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002521 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2522 out.AsFpuRegisterPairLow<SRegister>());
2523 break;
2524 }
2525
Roland Levillain647b9ed2014-11-27 12:06:00 +00002526 case Primitive::kPrimLong: {
2527 // Processing a Dex `long-to-double' instruction.
2528 Register low = in.AsRegisterPairLow<Register>();
2529 Register high = in.AsRegisterPairHigh<Register>();
2530 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2531 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002532 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002533 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002534 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2535 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002536
Roland Levillain682393c2015-04-14 15:57:52 +01002537 // temp_d = int-to-double(high)
2538 __ vmovsr(temp_s, high);
2539 __ vcvtdi(temp_d, temp_s);
2540 // constant_d = k2Pow32EncodingForDouble
2541 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2542 // out_d = unsigned-to-double(low)
2543 __ vmovsr(out_s, low);
2544 __ vcvtdu(out_d, out_s);
2545 // out_d += temp_d * constant_d
2546 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002547 break;
2548 }
2549
Roland Levillaincff13742014-11-17 14:32:17 +00002550 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002551 // Processing a Dex `float-to-double' instruction.
2552 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2553 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002554 break;
2555
2556 default:
2557 LOG(FATAL) << "Unexpected type conversion from " << input_type
2558 << " to " << result_type;
2559 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002560 break;
2561
2562 default:
2563 LOG(FATAL) << "Unexpected type conversion from " << input_type
2564 << " to " << result_type;
2565 }
2566}
2567
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002568void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002569 LocationSummary* locations =
2570 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002571 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002572 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002573 locations->SetInAt(0, Location::RequiresRegister());
2574 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002575 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2576 break;
2577 }
2578
2579 case Primitive::kPrimLong: {
2580 locations->SetInAt(0, Location::RequiresRegister());
2581 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002583 break;
2584 }
2585
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002586 case Primitive::kPrimFloat:
2587 case Primitive::kPrimDouble: {
2588 locations->SetInAt(0, Location::RequiresFpuRegister());
2589 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002591 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002592 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002593
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002594 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002595 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002596 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002597}
2598
2599void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2600 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002601 Location out = locations->Out();
2602 Location first = locations->InAt(0);
2603 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002604 switch (add->GetResultType()) {
2605 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002606 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002607 __ add(out.AsRegister<Register>(),
2608 first.AsRegister<Register>(),
2609 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002610 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 __ AddConstant(out.AsRegister<Register>(),
2612 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002613 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002614 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002615 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002616
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002617 case Primitive::kPrimLong: {
2618 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002619 __ adds(out.AsRegisterPairLow<Register>(),
2620 first.AsRegisterPairLow<Register>(),
2621 ShifterOperand(second.AsRegisterPairLow<Register>()));
2622 __ adc(out.AsRegisterPairHigh<Register>(),
2623 first.AsRegisterPairHigh<Register>(),
2624 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002625 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002626 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002627
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002628 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002629 __ vadds(out.AsFpuRegister<SRegister>(),
2630 first.AsFpuRegister<SRegister>(),
2631 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002632 break;
2633
2634 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002635 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2636 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2637 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002638 break;
2639
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002640 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002641 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002642 }
2643}
2644
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002645void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002646 LocationSummary* locations =
2647 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002648 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002649 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002650 locations->SetInAt(0, Location::RequiresRegister());
2651 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002652 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2653 break;
2654 }
2655
2656 case Primitive::kPrimLong: {
2657 locations->SetInAt(0, Location::RequiresRegister());
2658 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002659 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002660 break;
2661 }
Calin Juravle11351682014-10-23 15:38:15 +01002662 case Primitive::kPrimFloat:
2663 case Primitive::kPrimDouble: {
2664 locations->SetInAt(0, Location::RequiresFpuRegister());
2665 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002666 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002667 break;
Calin Juravle11351682014-10-23 15:38:15 +01002668 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002669 default:
Calin Juravle11351682014-10-23 15:38:15 +01002670 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002671 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002672}
2673
2674void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2675 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002676 Location out = locations->Out();
2677 Location first = locations->InAt(0);
2678 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002679 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002680 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002681 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002682 __ sub(out.AsRegister<Register>(),
2683 first.AsRegister<Register>(),
2684 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002685 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ AddConstant(out.AsRegister<Register>(),
2687 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002688 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002689 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002690 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002691 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002692
Calin Juravle11351682014-10-23 15:38:15 +01002693 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002694 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002695 __ subs(out.AsRegisterPairLow<Register>(),
2696 first.AsRegisterPairLow<Register>(),
2697 ShifterOperand(second.AsRegisterPairLow<Register>()));
2698 __ sbc(out.AsRegisterPairHigh<Register>(),
2699 first.AsRegisterPairHigh<Register>(),
2700 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002701 break;
Calin Juravle11351682014-10-23 15:38:15 +01002702 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002703
Calin Juravle11351682014-10-23 15:38:15 +01002704 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002705 __ vsubs(out.AsFpuRegister<SRegister>(),
2706 first.AsFpuRegister<SRegister>(),
2707 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002708 break;
Calin Juravle11351682014-10-23 15:38:15 +01002709 }
2710
2711 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002712 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2713 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2714 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002715 break;
2716 }
2717
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002718
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002719 default:
Calin Juravle11351682014-10-23 15:38:15 +01002720 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002721 }
2722}
2723
Calin Juravle34bacdf2014-10-07 20:23:36 +01002724void LocationsBuilderARM::VisitMul(HMul* mul) {
2725 LocationSummary* locations =
2726 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2727 switch (mul->GetResultType()) {
2728 case Primitive::kPrimInt:
2729 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002730 locations->SetInAt(0, Location::RequiresRegister());
2731 locations->SetInAt(1, Location::RequiresRegister());
2732 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002733 break;
2734 }
2735
Calin Juravleb5bfa962014-10-21 18:02:24 +01002736 case Primitive::kPrimFloat:
2737 case Primitive::kPrimDouble: {
2738 locations->SetInAt(0, Location::RequiresFpuRegister());
2739 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002740 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002741 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002742 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002743
2744 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002745 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002746 }
2747}
2748
2749void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2750 LocationSummary* locations = mul->GetLocations();
2751 Location out = locations->Out();
2752 Location first = locations->InAt(0);
2753 Location second = locations->InAt(1);
2754 switch (mul->GetResultType()) {
2755 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002756 __ mul(out.AsRegister<Register>(),
2757 first.AsRegister<Register>(),
2758 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002759 break;
2760 }
2761 case Primitive::kPrimLong: {
2762 Register out_hi = out.AsRegisterPairHigh<Register>();
2763 Register out_lo = out.AsRegisterPairLow<Register>();
2764 Register in1_hi = first.AsRegisterPairHigh<Register>();
2765 Register in1_lo = first.AsRegisterPairLow<Register>();
2766 Register in2_hi = second.AsRegisterPairHigh<Register>();
2767 Register in2_lo = second.AsRegisterPairLow<Register>();
2768
2769 // Extra checks to protect caused by the existence of R1_R2.
2770 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2771 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2772 DCHECK_NE(out_hi, in1_lo);
2773 DCHECK_NE(out_hi, in2_lo);
2774
2775 // input: in1 - 64 bits, in2 - 64 bits
2776 // output: out
2777 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2778 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2779 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2780
2781 // IP <- in1.lo * in2.hi
2782 __ mul(IP, in1_lo, in2_hi);
2783 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2784 __ mla(out_hi, in1_hi, in2_lo, IP);
2785 // out.lo <- (in1.lo * in2.lo)[31:0];
2786 __ umull(out_lo, IP, in1_lo, in2_lo);
2787 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2788 __ add(out_hi, out_hi, ShifterOperand(IP));
2789 break;
2790 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002791
2792 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002793 __ vmuls(out.AsFpuRegister<SRegister>(),
2794 first.AsFpuRegister<SRegister>(),
2795 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002796 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002797 }
2798
2799 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002800 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2801 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2802 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002803 break;
2804 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002805
2806 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002807 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002808 }
2809}
2810
Zheng Xuc6667102015-05-15 16:08:45 +08002811void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2812 DCHECK(instruction->IsDiv() || instruction->IsRem());
2813 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2814
2815 LocationSummary* locations = instruction->GetLocations();
2816 Location second = locations->InAt(1);
2817 DCHECK(second.IsConstant());
2818
2819 Register out = locations->Out().AsRegister<Register>();
2820 Register dividend = locations->InAt(0).AsRegister<Register>();
2821 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2822 DCHECK(imm == 1 || imm == -1);
2823
2824 if (instruction->IsRem()) {
2825 __ LoadImmediate(out, 0);
2826 } else {
2827 if (imm == 1) {
2828 __ Mov(out, dividend);
2829 } else {
2830 __ rsb(out, dividend, ShifterOperand(0));
2831 }
2832 }
2833}
2834
2835void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2836 DCHECK(instruction->IsDiv() || instruction->IsRem());
2837 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2838
2839 LocationSummary* locations = instruction->GetLocations();
2840 Location second = locations->InAt(1);
2841 DCHECK(second.IsConstant());
2842
2843 Register out = locations->Out().AsRegister<Register>();
2844 Register dividend = locations->InAt(0).AsRegister<Register>();
2845 Register temp = locations->GetTemp(0).AsRegister<Register>();
2846 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002847 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002848 int ctz_imm = CTZ(abs_imm);
2849
2850 if (ctz_imm == 1) {
2851 __ Lsr(temp, dividend, 32 - ctz_imm);
2852 } else {
2853 __ Asr(temp, dividend, 31);
2854 __ Lsr(temp, temp, 32 - ctz_imm);
2855 }
2856 __ add(out, temp, ShifterOperand(dividend));
2857
2858 if (instruction->IsDiv()) {
2859 __ Asr(out, out, ctz_imm);
2860 if (imm < 0) {
2861 __ rsb(out, out, ShifterOperand(0));
2862 }
2863 } else {
2864 __ ubfx(out, out, 0, ctz_imm);
2865 __ sub(out, out, ShifterOperand(temp));
2866 }
2867}
2868
2869void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2870 DCHECK(instruction->IsDiv() || instruction->IsRem());
2871 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2872
2873 LocationSummary* locations = instruction->GetLocations();
2874 Location second = locations->InAt(1);
2875 DCHECK(second.IsConstant());
2876
2877 Register out = locations->Out().AsRegister<Register>();
2878 Register dividend = locations->InAt(0).AsRegister<Register>();
2879 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2880 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2881 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2882
2883 int64_t magic;
2884 int shift;
2885 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2886
2887 __ LoadImmediate(temp1, magic);
2888 __ smull(temp2, temp1, dividend, temp1);
2889
2890 if (imm > 0 && magic < 0) {
2891 __ add(temp1, temp1, ShifterOperand(dividend));
2892 } else if (imm < 0 && magic > 0) {
2893 __ sub(temp1, temp1, ShifterOperand(dividend));
2894 }
2895
2896 if (shift != 0) {
2897 __ Asr(temp1, temp1, shift);
2898 }
2899
2900 if (instruction->IsDiv()) {
2901 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2902 } else {
2903 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2904 // TODO: Strength reduction for mls.
2905 __ LoadImmediate(temp2, imm);
2906 __ mls(out, temp1, temp2, dividend);
2907 }
2908}
2909
2910void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2911 DCHECK(instruction->IsDiv() || instruction->IsRem());
2912 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2913
2914 LocationSummary* locations = instruction->GetLocations();
2915 Location second = locations->InAt(1);
2916 DCHECK(second.IsConstant());
2917
2918 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2919 if (imm == 0) {
2920 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2921 } else if (imm == 1 || imm == -1) {
2922 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002923 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002924 DivRemByPowerOfTwo(instruction);
2925 } else {
2926 DCHECK(imm <= -2 || imm >= 2);
2927 GenerateDivRemWithAnyConstant(instruction);
2928 }
2929}
2930
Calin Juravle7c4954d2014-10-28 16:57:40 +00002931void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002932 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2933 if (div->GetResultType() == Primitive::kPrimLong) {
2934 // pLdiv runtime call.
2935 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002936 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2937 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002938 } else if (div->GetResultType() == Primitive::kPrimInt &&
2939 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2940 // pIdivmod runtime call.
2941 call_kind = LocationSummary::kCall;
2942 }
2943
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2945
Calin Juravle7c4954d2014-10-28 16:57:40 +00002946 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002947 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002948 if (div->InputAt(1)->IsConstant()) {
2949 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002950 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002951 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002952 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2953 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002954 // No temp register required.
2955 } else {
2956 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002957 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002958 locations->AddTemp(Location::RequiresRegister());
2959 }
2960 }
2961 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002962 locations->SetInAt(0, Location::RequiresRegister());
2963 locations->SetInAt(1, Location::RequiresRegister());
2964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2965 } else {
2966 InvokeRuntimeCallingConvention calling_convention;
2967 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2968 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2969 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2970 // we only need the former.
2971 locations->SetOut(Location::RegisterLocation(R0));
2972 }
Calin Juravled0d48522014-11-04 16:40:20 +00002973 break;
2974 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002975 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002976 InvokeRuntimeCallingConvention calling_convention;
2977 locations->SetInAt(0, Location::RegisterPairLocation(
2978 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2979 locations->SetInAt(1, Location::RegisterPairLocation(
2980 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002981 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002982 break;
2983 }
2984 case Primitive::kPrimFloat:
2985 case Primitive::kPrimDouble: {
2986 locations->SetInAt(0, Location::RequiresFpuRegister());
2987 locations->SetInAt(1, Location::RequiresFpuRegister());
2988 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2989 break;
2990 }
2991
2992 default:
2993 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2994 }
2995}
2996
2997void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2998 LocationSummary* locations = div->GetLocations();
2999 Location out = locations->Out();
3000 Location first = locations->InAt(0);
3001 Location second = locations->InAt(1);
3002
3003 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003004 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003005 if (second.IsConstant()) {
3006 GenerateDivRemConstantIntegral(div);
3007 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003008 __ sdiv(out.AsRegister<Register>(),
3009 first.AsRegister<Register>(),
3010 second.AsRegister<Register>());
3011 } else {
3012 InvokeRuntimeCallingConvention calling_convention;
3013 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3014 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3015 DCHECK_EQ(R0, out.AsRegister<Register>());
3016
3017 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003018 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003019 }
Calin Juravled0d48522014-11-04 16:40:20 +00003020 break;
3021 }
3022
Calin Juravle7c4954d2014-10-28 16:57:40 +00003023 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003024 InvokeRuntimeCallingConvention calling_convention;
3025 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3026 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3027 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3028 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3029 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003030 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003031
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003032 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003033 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003034 break;
3035 }
3036
3037 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003038 __ vdivs(out.AsFpuRegister<SRegister>(),
3039 first.AsFpuRegister<SRegister>(),
3040 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003041 break;
3042 }
3043
3044 case Primitive::kPrimDouble: {
3045 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3046 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3047 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3048 break;
3049 }
3050
3051 default:
3052 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3053 }
3054}
3055
Calin Juravlebacfec32014-11-14 15:54:36 +00003056void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003057 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003058
3059 // Most remainders are implemented in the runtime.
3060 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003061 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3062 // sdiv will be replaced by other instruction sequence.
3063 call_kind = LocationSummary::kNoCall;
3064 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3065 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003066 // Have hardware divide instruction for int, do it with three instructions.
3067 call_kind = LocationSummary::kNoCall;
3068 }
3069
Calin Juravlebacfec32014-11-14 15:54:36 +00003070 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3071
Calin Juravled2ec87d2014-12-08 14:24:46 +00003072 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003073 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003074 if (rem->InputAt(1)->IsConstant()) {
3075 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003076 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003078 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3079 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003080 // No temp register required.
3081 } else {
3082 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003083 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003084 locations->AddTemp(Location::RequiresRegister());
3085 }
3086 }
3087 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003088 locations->SetInAt(0, Location::RequiresRegister());
3089 locations->SetInAt(1, Location::RequiresRegister());
3090 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3091 locations->AddTemp(Location::RequiresRegister());
3092 } else {
3093 InvokeRuntimeCallingConvention calling_convention;
3094 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3095 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3096 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3097 // we only need the latter.
3098 locations->SetOut(Location::RegisterLocation(R1));
3099 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003100 break;
3101 }
3102 case Primitive::kPrimLong: {
3103 InvokeRuntimeCallingConvention calling_convention;
3104 locations->SetInAt(0, Location::RegisterPairLocation(
3105 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3106 locations->SetInAt(1, Location::RegisterPairLocation(
3107 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3108 // The runtime helper puts the output in R2,R3.
3109 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3110 break;
3111 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003112 case Primitive::kPrimFloat: {
3113 InvokeRuntimeCallingConvention calling_convention;
3114 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3115 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3116 locations->SetOut(Location::FpuRegisterLocation(S0));
3117 break;
3118 }
3119
Calin Juravlebacfec32014-11-14 15:54:36 +00003120 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003121 InvokeRuntimeCallingConvention calling_convention;
3122 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3123 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3124 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3125 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3126 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003127 break;
3128 }
3129
3130 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003131 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003132 }
3133}
3134
3135void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3136 LocationSummary* locations = rem->GetLocations();
3137 Location out = locations->Out();
3138 Location first = locations->InAt(0);
3139 Location second = locations->InAt(1);
3140
Calin Juravled2ec87d2014-12-08 14:24:46 +00003141 Primitive::Type type = rem->GetResultType();
3142 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003143 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003144 if (second.IsConstant()) {
3145 GenerateDivRemConstantIntegral(rem);
3146 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003147 Register reg1 = first.AsRegister<Register>();
3148 Register reg2 = second.AsRegister<Register>();
3149 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003150
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003151 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003152 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003153 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003154 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003155 } else {
3156 InvokeRuntimeCallingConvention calling_convention;
3157 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3158 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3159 DCHECK_EQ(R1, out.AsRegister<Register>());
3160
3161 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003162 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003163 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003164 break;
3165 }
3166
3167 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003168 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003169 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003170 break;
3171 }
3172
Calin Juravled2ec87d2014-12-08 14:24:46 +00003173 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003174 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003175 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003176 break;
3177 }
3178
Calin Juravlebacfec32014-11-14 15:54:36 +00003179 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003180 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003181 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003182 break;
3183 }
3184
3185 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003186 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003187 }
3188}
3189
Calin Juravled0d48522014-11-04 16:40:20 +00003190void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003191 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3192 ? LocationSummary::kCallOnSlowPath
3193 : LocationSummary::kNoCall;
3194 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003195 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003196 if (instruction->HasUses()) {
3197 locations->SetOut(Location::SameAsFirstInput());
3198 }
3199}
3200
3201void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003202 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003203 codegen_->AddSlowPath(slow_path);
3204
3205 LocationSummary* locations = instruction->GetLocations();
3206 Location value = locations->InAt(0);
3207
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003208 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003209 case Primitive::kPrimByte:
3210 case Primitive::kPrimChar:
3211 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003212 case Primitive::kPrimInt: {
3213 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003214 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003215 } else {
3216 DCHECK(value.IsConstant()) << value;
3217 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3218 __ b(slow_path->GetEntryLabel());
3219 }
3220 }
3221 break;
3222 }
3223 case Primitive::kPrimLong: {
3224 if (value.IsRegisterPair()) {
3225 __ orrs(IP,
3226 value.AsRegisterPairLow<Register>(),
3227 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3228 __ b(slow_path->GetEntryLabel(), EQ);
3229 } else {
3230 DCHECK(value.IsConstant()) << value;
3231 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3232 __ b(slow_path->GetEntryLabel());
3233 }
3234 }
3235 break;
3236 default:
3237 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3238 }
3239 }
Calin Juravled0d48522014-11-04 16:40:20 +00003240}
3241
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003242void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3243 Register in = locations->InAt(0).AsRegister<Register>();
3244 Location rhs = locations->InAt(1);
3245 Register out = locations->Out().AsRegister<Register>();
3246
3247 if (rhs.IsConstant()) {
3248 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3249 // so map all rotations to a +ve. equivalent in that range.
3250 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3251 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3252 if (rot) {
3253 // Rotate, mapping left rotations to right equivalents if necessary.
3254 // (e.g. left by 2 bits == right by 30.)
3255 __ Ror(out, in, rot);
3256 } else if (out != in) {
3257 __ Mov(out, in);
3258 }
3259 } else {
3260 __ Ror(out, in, rhs.AsRegister<Register>());
3261 }
3262}
3263
3264// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3265// rotates by swapping input regs (effectively rotating by the first 32-bits of
3266// a larger rotation) or flipping direction (thus treating larger right/left
3267// rotations as sub-word sized rotations in the other direction) as appropriate.
3268void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3269 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3270 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3271 Location rhs = locations->InAt(1);
3272 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3273 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3274
3275 if (rhs.IsConstant()) {
3276 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3277 // Map all rotations to +ve. equivalents on the interval [0,63].
3278 rot &= kMaxLongShiftValue;
3279 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3280 // logic below to a simple pair of binary orr.
3281 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3282 if (rot >= kArmBitsPerWord) {
3283 rot -= kArmBitsPerWord;
3284 std::swap(in_reg_hi, in_reg_lo);
3285 }
3286 // Rotate, or mov to out for zero or word size rotations.
3287 if (rot != 0u) {
3288 __ Lsr(out_reg_hi, in_reg_hi, rot);
3289 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3290 __ Lsr(out_reg_lo, in_reg_lo, rot);
3291 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3292 } else {
3293 __ Mov(out_reg_lo, in_reg_lo);
3294 __ Mov(out_reg_hi, in_reg_hi);
3295 }
3296 } else {
3297 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3298 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3299 Label end;
3300 Label shift_by_32_plus_shift_right;
3301
3302 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3303 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3304 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3305 __ b(&shift_by_32_plus_shift_right, CC);
3306
3307 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3308 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3309 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3310 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3311 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3312 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3313 __ Lsr(shift_left, in_reg_hi, shift_right);
3314 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3315 __ b(&end);
3316
3317 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3318 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3319 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3320 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3321 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3322 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3323 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3324 __ Lsl(shift_right, in_reg_hi, shift_left);
3325 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3326
3327 __ Bind(&end);
3328 }
3329}
3330void LocationsBuilderARM::HandleRotate(HRor* ror) {
3331 LocationSummary* locations =
3332 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3333 switch (ror->GetResultType()) {
3334 case Primitive::kPrimInt: {
3335 locations->SetInAt(0, Location::RequiresRegister());
3336 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3338 break;
3339 }
3340 case Primitive::kPrimLong: {
3341 locations->SetInAt(0, Location::RequiresRegister());
3342 if (ror->InputAt(1)->IsConstant()) {
3343 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3344 } else {
3345 locations->SetInAt(1, Location::RequiresRegister());
3346 locations->AddTemp(Location::RequiresRegister());
3347 locations->AddTemp(Location::RequiresRegister());
3348 }
3349 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3350 break;
3351 }
3352 default:
3353 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3354 }
3355}
3356
3357void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3358 LocationSummary* locations = ror->GetLocations();
3359 Primitive::Type type = ror->GetResultType();
3360 switch (type) {
3361 case Primitive::kPrimInt: {
3362 HandleIntegerRotate(locations);
3363 break;
3364 }
3365 case Primitive::kPrimLong: {
3366 HandleLongRotate(locations);
3367 break;
3368 }
3369 default:
3370 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003371 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003372 }
3373}
3374
3375void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003376 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003377}
3378
3379void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003380 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003381}
3382
Calin Juravle9aec02f2014-11-18 23:06:35 +00003383void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3384 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3385
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003386 LocationSummary* locations =
3387 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003388
3389 switch (op->GetResultType()) {
3390 case Primitive::kPrimInt: {
3391 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003392 if (op->InputAt(1)->IsConstant()) {
3393 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3395 } else {
3396 locations->SetInAt(1, Location::RequiresRegister());
3397 // Make the output overlap, as it will be used to hold the masked
3398 // second input.
3399 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3400 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003401 break;
3402 }
3403 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003404 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003405 if (op->InputAt(1)->IsConstant()) {
3406 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3407 // For simplicity, use kOutputOverlap even though we only require that low registers
3408 // don't clash with high registers which the register allocator currently guarantees.
3409 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3410 } else {
3411 locations->SetInAt(1, Location::RequiresRegister());
3412 locations->AddTemp(Location::RequiresRegister());
3413 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3414 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003415 break;
3416 }
3417 default:
3418 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3419 }
3420}
3421
3422void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3423 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3424
3425 LocationSummary* locations = op->GetLocations();
3426 Location out = locations->Out();
3427 Location first = locations->InAt(0);
3428 Location second = locations->InAt(1);
3429
3430 Primitive::Type type = op->GetResultType();
3431 switch (type) {
3432 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003433 Register out_reg = out.AsRegister<Register>();
3434 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003435 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003436 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003437 // ARM doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003438 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003439 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003440 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003441 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003442 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003443 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003444 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003445 }
3446 } else {
3447 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3448 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
Roland Levillainc9285912015-12-18 10:38:42 +00003449 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003450 __ Mov(out_reg, first_reg);
3451 } else if (op->IsShl()) {
3452 __ Lsl(out_reg, first_reg, shift_value);
3453 } else if (op->IsShr()) {
3454 __ Asr(out_reg, first_reg, shift_value);
3455 } else {
3456 __ Lsr(out_reg, first_reg, shift_value);
3457 }
3458 }
3459 break;
3460 }
3461 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003462 Register o_h = out.AsRegisterPairHigh<Register>();
3463 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003464
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003465 Register high = first.AsRegisterPairHigh<Register>();
3466 Register low = first.AsRegisterPairLow<Register>();
3467
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003468 if (second.IsRegister()) {
3469 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003470
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003471 Register second_reg = second.AsRegister<Register>();
3472
3473 if (op->IsShl()) {
3474 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3475 // Shift the high part
3476 __ Lsl(o_h, high, o_l);
3477 // Shift the low part and `or` what overflew on the high part
3478 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3479 __ Lsr(temp, low, temp);
3480 __ orr(o_h, o_h, ShifterOperand(temp));
3481 // If the shift is > 32 bits, override the high part
3482 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3483 __ it(PL);
3484 __ Lsl(o_h, low, temp, PL);
3485 // Shift the low part
3486 __ Lsl(o_l, low, o_l);
3487 } else if (op->IsShr()) {
3488 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3489 // Shift the low part
3490 __ Lsr(o_l, low, o_h);
3491 // Shift the high part and `or` what underflew on the low part
3492 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3493 __ Lsl(temp, high, temp);
3494 __ orr(o_l, o_l, ShifterOperand(temp));
3495 // If the shift is > 32 bits, override the low part
3496 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3497 __ it(PL);
3498 __ Asr(o_l, high, temp, PL);
3499 // Shift the high part
3500 __ Asr(o_h, high, o_h);
3501 } else {
3502 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3503 // same as Shr except we use `Lsr`s and not `Asr`s
3504 __ Lsr(o_l, low, o_h);
3505 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3506 __ Lsl(temp, high, temp);
3507 __ orr(o_l, o_l, ShifterOperand(temp));
3508 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3509 __ it(PL);
3510 __ Lsr(o_l, high, temp, PL);
3511 __ Lsr(o_h, high, o_h);
3512 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003513 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003514 // Register allocator doesn't create partial overlap.
3515 DCHECK_NE(o_l, high);
3516 DCHECK_NE(o_h, low);
3517 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3518 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3519 if (shift_value > 32) {
3520 if (op->IsShl()) {
3521 __ Lsl(o_h, low, shift_value - 32);
3522 __ LoadImmediate(o_l, 0);
3523 } else if (op->IsShr()) {
3524 __ Asr(o_l, high, shift_value - 32);
3525 __ Asr(o_h, high, 31);
3526 } else {
3527 __ Lsr(o_l, high, shift_value - 32);
3528 __ LoadImmediate(o_h, 0);
3529 }
3530 } else if (shift_value == 32) {
3531 if (op->IsShl()) {
3532 __ mov(o_h, ShifterOperand(low));
3533 __ LoadImmediate(o_l, 0);
3534 } else if (op->IsShr()) {
3535 __ mov(o_l, ShifterOperand(high));
3536 __ Asr(o_h, high, 31);
3537 } else {
3538 __ mov(o_l, ShifterOperand(high));
3539 __ LoadImmediate(o_h, 0);
3540 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003541 } else if (shift_value == 1) {
3542 if (op->IsShl()) {
3543 __ Lsls(o_l, low, 1);
3544 __ adc(o_h, high, ShifterOperand(high));
3545 } else if (op->IsShr()) {
3546 __ Asrs(o_h, high, 1);
3547 __ Rrx(o_l, low);
3548 } else {
3549 __ Lsrs(o_h, high, 1);
3550 __ Rrx(o_l, low);
3551 }
3552 } else {
3553 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003554 if (op->IsShl()) {
3555 __ Lsl(o_h, high, shift_value);
3556 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3557 __ Lsl(o_l, low, shift_value);
3558 } else if (op->IsShr()) {
3559 __ Lsr(o_l, low, shift_value);
3560 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3561 __ Asr(o_h, high, shift_value);
3562 } else {
3563 __ Lsr(o_l, low, shift_value);
3564 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3565 __ Lsr(o_h, high, shift_value);
3566 }
3567 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003568 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003569 break;
3570 }
3571 default:
3572 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003573 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003574 }
3575}
3576
3577void LocationsBuilderARM::VisitShl(HShl* shl) {
3578 HandleShift(shl);
3579}
3580
3581void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3582 HandleShift(shl);
3583}
3584
3585void LocationsBuilderARM::VisitShr(HShr* shr) {
3586 HandleShift(shr);
3587}
3588
3589void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3590 HandleShift(shr);
3591}
3592
3593void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3594 HandleShift(ushr);
3595}
3596
3597void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3598 HandleShift(ushr);
3599}
3600
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003601void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003602 LocationSummary* locations =
3603 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
David Brazdil6de19382016-01-08 17:37:10 +00003604 if (instruction->IsStringAlloc()) {
3605 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3606 } else {
3607 InvokeRuntimeCallingConvention calling_convention;
3608 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3609 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3610 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003611 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003612}
3613
3614void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003615 // Note: if heap poisoning is enabled, the entry point takes cares
3616 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003617 if (instruction->IsStringAlloc()) {
3618 // String is allocated through StringFactory. Call NewEmptyString entry point.
3619 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3620 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3621 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3622 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3623 __ blx(LR);
3624 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3625 } else {
3626 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3627 instruction,
3628 instruction->GetDexPc(),
3629 nullptr);
3630 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3631 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003632}
3633
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003634void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3637 InvokeRuntimeCallingConvention calling_convention;
3638 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003639 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003640 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003641 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003642}
3643
3644void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3645 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003646 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003647 // Note: if heap poisoning is enabled, the entry point takes cares
3648 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003649 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003650 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003651 instruction->GetDexPc(),
3652 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003653 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003654}
3655
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003656void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003657 LocationSummary* locations =
3658 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003659 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3660 if (location.IsStackSlot()) {
3661 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3662 } else if (location.IsDoubleStackSlot()) {
3663 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003664 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003665 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003666}
3667
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003668void InstructionCodeGeneratorARM::VisitParameterValue(
3669 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003670 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003671}
3672
3673void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3674 LocationSummary* locations =
3675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3676 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3677}
3678
3679void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3680 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003681}
3682
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003683void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003684 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003685 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003686 locations->SetInAt(0, Location::RequiresRegister());
3687 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003688}
3689
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003690void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3691 LocationSummary* locations = not_->GetLocations();
3692 Location out = locations->Out();
3693 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003694 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003695 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003696 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003697 break;
3698
3699 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003700 __ mvn(out.AsRegisterPairLow<Register>(),
3701 ShifterOperand(in.AsRegisterPairLow<Register>()));
3702 __ mvn(out.AsRegisterPairHigh<Register>(),
3703 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003704 break;
3705
3706 default:
3707 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3708 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003709}
3710
David Brazdil66d126e2015-04-03 16:02:44 +01003711void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3712 LocationSummary* locations =
3713 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3714 locations->SetInAt(0, Location::RequiresRegister());
3715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3716}
3717
3718void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003719 LocationSummary* locations = bool_not->GetLocations();
3720 Location out = locations->Out();
3721 Location in = locations->InAt(0);
3722 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3723}
3724
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003725void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003726 LocationSummary* locations =
3727 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003728 switch (compare->InputAt(0)->GetType()) {
3729 case Primitive::kPrimLong: {
3730 locations->SetInAt(0, Location::RequiresRegister());
3731 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003732 // Output overlaps because it is written before doing the low comparison.
3733 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003734 break;
3735 }
3736 case Primitive::kPrimFloat:
3737 case Primitive::kPrimDouble: {
3738 locations->SetInAt(0, Location::RequiresFpuRegister());
3739 locations->SetInAt(1, Location::RequiresFpuRegister());
3740 locations->SetOut(Location::RequiresRegister());
3741 break;
3742 }
3743 default:
3744 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3745 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003746}
3747
3748void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003749 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003751 Location left = locations->InAt(0);
3752 Location right = locations->InAt(1);
3753
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003754 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003755 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003756 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003757 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003758 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003759 __ cmp(left.AsRegisterPairHigh<Register>(),
3760 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003761 __ b(&less, LT);
3762 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003763 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003764 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003765 __ cmp(left.AsRegisterPairLow<Register>(),
3766 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003767 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003768 break;
3769 }
3770 case Primitive::kPrimFloat:
3771 case Primitive::kPrimDouble: {
3772 __ LoadImmediate(out, 0);
3773 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003774 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003775 } else {
3776 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3777 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3778 }
3779 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003780 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003781 break;
3782 }
3783 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003784 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003785 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003786 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003787 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003788 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003789
3790 __ Bind(&greater);
3791 __ LoadImmediate(out, 1);
3792 __ b(&done);
3793
3794 __ Bind(&less);
3795 __ LoadImmediate(out, -1);
3796
3797 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003798}
3799
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003800void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003801 LocationSummary* locations =
3802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003803 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3804 locations->SetInAt(i, Location::Any());
3805 }
3806 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003807}
3808
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003809void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003810 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003811}
3812
Roland Levillainc9285912015-12-18 10:38:42 +00003813void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3814 // TODO (ported from quick): revisit ARM barrier kinds.
3815 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003816 switch (kind) {
3817 case MemBarrierKind::kAnyStore:
3818 case MemBarrierKind::kLoadAny:
3819 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003820 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003821 break;
3822 }
3823 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003824 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003825 break;
3826 }
3827 default:
3828 LOG(FATAL) << "Unexpected memory barrier " << kind;
3829 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003830 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003831}
3832
3833void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3834 uint32_t offset,
3835 Register out_lo,
3836 Register out_hi) {
3837 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003838 // Ensure `out_lo` is different from `addr`, so that loading
3839 // `offset` into `out_lo` does not clutter `addr`.
3840 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003841 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003842 __ add(IP, addr, ShifterOperand(out_lo));
3843 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003844 }
3845 __ ldrexd(out_lo, out_hi, addr);
3846}
3847
3848void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3849 uint32_t offset,
3850 Register value_lo,
3851 Register value_hi,
3852 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003853 Register temp2,
3854 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003855 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003856 if (offset != 0) {
3857 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003858 __ add(IP, addr, ShifterOperand(temp1));
3859 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003860 }
3861 __ Bind(&fail);
3862 // We need a load followed by store. (The address used in a STREX instruction must
3863 // be the same as the address in the most recently executed LDREX instruction.)
3864 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003865 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003866 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003867 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003868}
3869
3870void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3871 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3872
Nicolas Geoffray39468442014-09-02 15:17:15 +01003873 LocationSummary* locations =
3874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003875 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003876
Calin Juravle52c48962014-12-16 17:02:57 +00003877 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003878 if (Primitive::IsFloatingPointType(field_type)) {
3879 locations->SetInAt(1, Location::RequiresFpuRegister());
3880 } else {
3881 locations->SetInAt(1, Location::RequiresRegister());
3882 }
3883
Calin Juravle52c48962014-12-16 17:02:57 +00003884 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003885 bool generate_volatile = field_info.IsVolatile()
3886 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003887 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003888 bool needs_write_barrier =
3889 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003890 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003891 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003892 if (needs_write_barrier) {
3893 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003894 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003895 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003896 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003897 // - registers need to be consecutive
3898 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003899 // We don't test for ARM yet, and the assertion makes sure that we
3900 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003901 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3902
3903 locations->AddTemp(Location::RequiresRegister());
3904 locations->AddTemp(Location::RequiresRegister());
3905 if (field_type == Primitive::kPrimDouble) {
3906 // For doubles we need two more registers to copy the value.
3907 locations->AddTemp(Location::RegisterLocation(R2));
3908 locations->AddTemp(Location::RegisterLocation(R3));
3909 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003910 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003911}
3912
Calin Juravle52c48962014-12-16 17:02:57 +00003913void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003914 const FieldInfo& field_info,
3915 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003916 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3917
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003918 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003919 Register base = locations->InAt(0).AsRegister<Register>();
3920 Location value = locations->InAt(1);
3921
3922 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003923 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003924 Primitive::Type field_type = field_info.GetFieldType();
3925 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003926 bool needs_write_barrier =
3927 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003928
3929 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003930 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003931 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003932
3933 switch (field_type) {
3934 case Primitive::kPrimBoolean:
3935 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003936 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003937 break;
3938 }
3939
3940 case Primitive::kPrimShort:
3941 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003942 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003943 break;
3944 }
3945
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003946 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003947 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003948 if (kPoisonHeapReferences && needs_write_barrier) {
3949 // Note that in the case where `value` is a null reference,
3950 // we do not enter this block, as a null reference does not
3951 // need poisoning.
3952 DCHECK_EQ(field_type, Primitive::kPrimNot);
3953 Register temp = locations->GetTemp(0).AsRegister<Register>();
3954 __ Mov(temp, value.AsRegister<Register>());
3955 __ PoisonHeapReference(temp);
3956 __ StoreToOffset(kStoreWord, temp, base, offset);
3957 } else {
3958 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3959 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003960 break;
3961 }
3962
3963 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003964 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003965 GenerateWideAtomicStore(base, offset,
3966 value.AsRegisterPairLow<Register>(),
3967 value.AsRegisterPairHigh<Register>(),
3968 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003969 locations->GetTemp(1).AsRegister<Register>(),
3970 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003971 } else {
3972 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003973 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003974 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003975 break;
3976 }
3977
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003978 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003979 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003980 break;
3981 }
3982
3983 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003984 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003985 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003986 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3987 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3988
3989 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3990
3991 GenerateWideAtomicStore(base, offset,
3992 value_reg_lo,
3993 value_reg_hi,
3994 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003995 locations->GetTemp(3).AsRegister<Register>(),
3996 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003997 } else {
3998 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003999 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004000 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004001 break;
4002 }
4003
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004004 case Primitive::kPrimVoid:
4005 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004006 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004007 }
Calin Juravle52c48962014-12-16 17:02:57 +00004008
Calin Juravle77520bc2015-01-12 18:45:46 +00004009 // Longs and doubles are handled in the switch.
4010 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4011 codegen_->MaybeRecordImplicitNullCheck(instruction);
4012 }
4013
4014 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4015 Register temp = locations->GetTemp(0).AsRegister<Register>();
4016 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004017 codegen_->MarkGCCard(
4018 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004019 }
4020
Calin Juravle52c48962014-12-16 17:02:57 +00004021 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004022 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004023 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004024}
4025
Calin Juravle52c48962014-12-16 17:02:57 +00004026void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4027 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004028
4029 bool object_field_get_with_read_barrier =
4030 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004031 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004032 new (GetGraph()->GetArena()) LocationSummary(instruction,
4033 object_field_get_with_read_barrier ?
4034 LocationSummary::kCallOnSlowPath :
4035 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004036 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004037
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004038 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004039 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004040 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004041 // The output overlaps in case of volatile long: we don't want the
4042 // code generated by GenerateWideAtomicLoad to overwrite the
4043 // object's location. Likewise, in the case of an object field get
4044 // with read barriers enabled, we do not want the load to overwrite
4045 // the object's location, as we need it to emit the read barrier.
4046 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4047 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004048
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004049 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4050 locations->SetOut(Location::RequiresFpuRegister());
4051 } else {
4052 locations->SetOut(Location::RequiresRegister(),
4053 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4054 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004055 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004056 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004057 // - registers need to be consecutive
4058 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004059 // We don't test for ARM yet, and the assertion makes sure that we
4060 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004061 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4062 locations->AddTemp(Location::RequiresRegister());
4063 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004064 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4065 // We need a temporary register for the read barrier marking slow
4066 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4067 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004068 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004069}
4070
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004071Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4072 Opcode opcode) {
4073 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4074 if (constant->IsConstant() &&
4075 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4076 return Location::ConstantLocation(constant->AsConstant());
4077 }
4078 return Location::RequiresRegister();
4079}
4080
4081bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4082 Opcode opcode) {
4083 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4084 if (Primitive::Is64BitType(input_cst->GetType())) {
4085 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4086 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4087 } else {
4088 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4089 }
4090}
4091
4092bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4093 ShifterOperand so;
4094 ArmAssembler* assembler = codegen_->GetAssembler();
4095 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4096 return true;
4097 }
4098 Opcode neg_opcode = kNoOperand;
4099 switch (opcode) {
4100 case AND:
4101 neg_opcode = BIC;
4102 break;
4103 case ORR:
4104 neg_opcode = ORN;
4105 break;
4106 default:
4107 return false;
4108 }
4109 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4110}
4111
Calin Juravle52c48962014-12-16 17:02:57 +00004112void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4113 const FieldInfo& field_info) {
4114 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004115
Calin Juravle52c48962014-12-16 17:02:57 +00004116 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004117 Location base_loc = locations->InAt(0);
4118 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004119 Location out = locations->Out();
4120 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004121 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004122 Primitive::Type field_type = field_info.GetFieldType();
4123 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4124
4125 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004126 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004127 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004128 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004129
Roland Levillainc9285912015-12-18 10:38:42 +00004130 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004131 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004132 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004133
Roland Levillainc9285912015-12-18 10:38:42 +00004134 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004135 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004136 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004137
Roland Levillainc9285912015-12-18 10:38:42 +00004138 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004139 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004140 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004141
4142 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004143 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004144 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004145
4146 case Primitive::kPrimNot: {
4147 // /* HeapReference<Object> */ out = *(base + offset)
4148 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4149 Location temp_loc = locations->GetTemp(0);
4150 // Note that a potential implicit null check is handled in this
4151 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4152 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4153 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4154 if (is_volatile) {
4155 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4156 }
4157 } else {
4158 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4159 codegen_->MaybeRecordImplicitNullCheck(instruction);
4160 if (is_volatile) {
4161 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4162 }
4163 // If read barriers are enabled, emit read barriers other than
4164 // Baker's using a slow path (and also unpoison the loaded
4165 // reference, if heap poisoning is enabled).
4166 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4167 }
4168 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004169 }
4170
Roland Levillainc9285912015-12-18 10:38:42 +00004171 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004172 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004173 GenerateWideAtomicLoad(base, offset,
4174 out.AsRegisterPairLow<Register>(),
4175 out.AsRegisterPairHigh<Register>());
4176 } else {
4177 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4178 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004179 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004180
Roland Levillainc9285912015-12-18 10:38:42 +00004181 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004182 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004183 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004184
4185 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004186 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004187 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004188 Register lo = locations->GetTemp(0).AsRegister<Register>();
4189 Register hi = locations->GetTemp(1).AsRegister<Register>();
4190 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004191 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004192 __ vmovdrr(out_reg, lo, hi);
4193 } else {
4194 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004195 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004196 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004197 break;
4198 }
4199
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004200 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004201 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004202 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004203 }
Calin Juravle52c48962014-12-16 17:02:57 +00004204
Roland Levillainc9285912015-12-18 10:38:42 +00004205 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4206 // Potential implicit null checks, in the case of reference or
4207 // double fields, are handled in the previous switch statement.
4208 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004209 codegen_->MaybeRecordImplicitNullCheck(instruction);
4210 }
4211
Calin Juravle52c48962014-12-16 17:02:57 +00004212 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004213 if (field_type == Primitive::kPrimNot) {
4214 // Memory barriers, in the case of references, are also handled
4215 // in the previous switch statement.
4216 } else {
4217 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4218 }
Roland Levillain4d027112015-07-01 15:41:14 +01004219 }
Calin Juravle52c48962014-12-16 17:02:57 +00004220}
4221
4222void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4223 HandleFieldSet(instruction, instruction->GetFieldInfo());
4224}
4225
4226void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004227 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004228}
4229
4230void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4231 HandleFieldGet(instruction, instruction->GetFieldInfo());
4232}
4233
4234void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4235 HandleFieldGet(instruction, instruction->GetFieldInfo());
4236}
4237
4238void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4239 HandleFieldGet(instruction, instruction->GetFieldInfo());
4240}
4241
4242void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4243 HandleFieldGet(instruction, instruction->GetFieldInfo());
4244}
4245
4246void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4247 HandleFieldSet(instruction, instruction->GetFieldInfo());
4248}
4249
4250void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004251 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004252}
4253
Calin Juravlee460d1d2015-09-29 04:52:17 +01004254void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4255 HUnresolvedInstanceFieldGet* instruction) {
4256 FieldAccessCallingConventionARM calling_convention;
4257 codegen_->CreateUnresolvedFieldLocationSummary(
4258 instruction, instruction->GetFieldType(), calling_convention);
4259}
4260
4261void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4262 HUnresolvedInstanceFieldGet* instruction) {
4263 FieldAccessCallingConventionARM calling_convention;
4264 codegen_->GenerateUnresolvedFieldAccess(instruction,
4265 instruction->GetFieldType(),
4266 instruction->GetFieldIndex(),
4267 instruction->GetDexPc(),
4268 calling_convention);
4269}
4270
4271void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4272 HUnresolvedInstanceFieldSet* instruction) {
4273 FieldAccessCallingConventionARM calling_convention;
4274 codegen_->CreateUnresolvedFieldLocationSummary(
4275 instruction, instruction->GetFieldType(), calling_convention);
4276}
4277
4278void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4279 HUnresolvedInstanceFieldSet* instruction) {
4280 FieldAccessCallingConventionARM calling_convention;
4281 codegen_->GenerateUnresolvedFieldAccess(instruction,
4282 instruction->GetFieldType(),
4283 instruction->GetFieldIndex(),
4284 instruction->GetDexPc(),
4285 calling_convention);
4286}
4287
4288void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4289 HUnresolvedStaticFieldGet* instruction) {
4290 FieldAccessCallingConventionARM calling_convention;
4291 codegen_->CreateUnresolvedFieldLocationSummary(
4292 instruction, instruction->GetFieldType(), calling_convention);
4293}
4294
4295void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4296 HUnresolvedStaticFieldGet* instruction) {
4297 FieldAccessCallingConventionARM calling_convention;
4298 codegen_->GenerateUnresolvedFieldAccess(instruction,
4299 instruction->GetFieldType(),
4300 instruction->GetFieldIndex(),
4301 instruction->GetDexPc(),
4302 calling_convention);
4303}
4304
4305void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4306 HUnresolvedStaticFieldSet* instruction) {
4307 FieldAccessCallingConventionARM calling_convention;
4308 codegen_->CreateUnresolvedFieldLocationSummary(
4309 instruction, instruction->GetFieldType(), calling_convention);
4310}
4311
4312void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4313 HUnresolvedStaticFieldSet* instruction) {
4314 FieldAccessCallingConventionARM calling_convention;
4315 codegen_->GenerateUnresolvedFieldAccess(instruction,
4316 instruction->GetFieldType(),
4317 instruction->GetFieldIndex(),
4318 instruction->GetDexPc(),
4319 calling_convention);
4320}
4321
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004323 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4324 ? LocationSummary::kCallOnSlowPath
4325 : LocationSummary::kNoCall;
4326 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004327 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004328 if (instruction->HasUses()) {
4329 locations->SetOut(Location::SameAsFirstInput());
4330 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004331}
4332
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004333void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004334 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4335 return;
4336 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004337 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004338
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004339 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4340 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4341}
4342
4343void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004344 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004345 codegen_->AddSlowPath(slow_path);
4346
4347 LocationSummary* locations = instruction->GetLocations();
4348 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004349
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004350 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004351}
4352
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004353void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004354 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004355 GenerateImplicitNullCheck(instruction);
4356 } else {
4357 GenerateExplicitNullCheck(instruction);
4358 }
4359}
4360
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004362 bool object_array_get_with_read_barrier =
4363 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004364 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004365 new (GetGraph()->GetArena()) LocationSummary(instruction,
4366 object_array_get_with_read_barrier ?
4367 LocationSummary::kCallOnSlowPath :
4368 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004369 locations->SetInAt(0, Location::RequiresRegister());
4370 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004371 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4372 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4373 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004374 // The output overlaps in the case of an object array get with
4375 // read barriers enabled: we do not want the move to overwrite the
4376 // array's location, as we need it to emit the read barrier.
4377 locations->SetOut(
4378 Location::RequiresRegister(),
4379 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004380 }
Roland Levillainc9285912015-12-18 10:38:42 +00004381 // We need a temporary register for the read barrier marking slow
4382 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4383 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4384 locations->AddTemp(Location::RequiresRegister());
4385 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004386}
4387
4388void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4389 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004390 Location obj_loc = locations->InAt(0);
4391 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004392 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004393 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394
Roland Levillainc9285912015-12-18 10:38:42 +00004395 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004396 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004397 case Primitive::kPrimBoolean: {
4398 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004399 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004400 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004401 size_t offset =
4402 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4404 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004405 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004406 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4407 }
4408 break;
4409 }
4410
4411 case Primitive::kPrimByte: {
4412 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004413 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004414 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004415 size_t offset =
4416 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004417 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4418 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004419 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004420 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4421 }
4422 break;
4423 }
4424
4425 case Primitive::kPrimShort: {
4426 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004427 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004428 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004429 size_t offset =
4430 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4432 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004433 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004434 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4435 }
4436 break;
4437 }
4438
4439 case Primitive::kPrimChar: {
4440 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004441 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004442 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004443 size_t offset =
4444 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004445 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4446 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004447 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004448 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4449 }
4450 break;
4451 }
4452
Roland Levillainc9285912015-12-18 10:38:42 +00004453 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004454 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004455 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004456 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004457 size_t offset =
4458 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004459 __ LoadFromOffset(kLoadWord, out, obj, offset);
4460 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004462 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4463 }
4464 break;
4465 }
4466
Roland Levillainc9285912015-12-18 10:38:42 +00004467 case Primitive::kPrimNot: {
4468 static_assert(
4469 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4470 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4471 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4472 // /* HeapReference<Object> */ out =
4473 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4474 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4475 Location temp = locations->GetTemp(0);
4476 // Note that a potential implicit null check is handled in this
4477 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4478 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4479 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4480 } else {
4481 Register out = out_loc.AsRegister<Register>();
4482 if (index.IsConstant()) {
4483 size_t offset =
4484 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4485 __ LoadFromOffset(kLoadWord, out, obj, offset);
4486 codegen_->MaybeRecordImplicitNullCheck(instruction);
4487 // If read barriers are enabled, emit read barriers other than
4488 // Baker's using a slow path (and also unpoison the loaded
4489 // reference, if heap poisoning is enabled).
4490 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4491 } else {
4492 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4493 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4494 codegen_->MaybeRecordImplicitNullCheck(instruction);
4495 // If read barriers are enabled, emit read barriers other than
4496 // Baker's using a slow path (and also unpoison the loaded
4497 // reference, if heap poisoning is enabled).
4498 codegen_->MaybeGenerateReadBarrierSlow(
4499 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4500 }
4501 }
4502 break;
4503 }
4504
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004505 case Primitive::kPrimLong: {
4506 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004507 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004508 size_t offset =
4509 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004510 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004511 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004512 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004513 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004514 }
4515 break;
4516 }
4517
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004518 case Primitive::kPrimFloat: {
4519 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004520 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004521 if (index.IsConstant()) {
4522 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004523 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004524 } else {
4525 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004526 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004527 }
4528 break;
4529 }
4530
4531 case Primitive::kPrimDouble: {
4532 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004533 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004534 if (index.IsConstant()) {
4535 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004536 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004537 } else {
4538 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004539 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004540 }
4541 break;
4542 }
4543
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004544 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004545 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004546 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004547 }
Roland Levillain4d027112015-07-01 15:41:14 +01004548
4549 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004550 // Potential implicit null checks, in the case of reference
4551 // arrays, are handled in the previous switch statement.
4552 } else {
4553 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004554 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004555}
4556
4557void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004558 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004559
4560 bool needs_write_barrier =
4561 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004562 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4563 bool object_array_set_with_read_barrier =
4564 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004565
Nicolas Geoffray39468442014-09-02 15:17:15 +01004566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004567 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004568 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4569 LocationSummary::kCallOnSlowPath :
4570 LocationSummary::kNoCall);
4571
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004572 locations->SetInAt(0, Location::RequiresRegister());
4573 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4574 if (Primitive::IsFloatingPointType(value_type)) {
4575 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004577 locations->SetInAt(2, Location::RequiresRegister());
4578 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004579 if (needs_write_barrier) {
4580 // Temporary registers for the write barrier.
4581 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004582 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004584}
4585
4586void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4587 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004588 Location array_loc = locations->InAt(0);
4589 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004591 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004592 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004593 bool needs_write_barrier =
4594 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004595
4596 switch (value_type) {
4597 case Primitive::kPrimBoolean:
4598 case Primitive::kPrimByte: {
4599 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004600 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004602 size_t offset =
4603 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004604 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004606 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4608 }
4609 break;
4610 }
4611
4612 case Primitive::kPrimShort:
4613 case Primitive::kPrimChar: {
4614 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004615 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004617 size_t offset =
4618 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004619 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004620 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004621 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004622 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4623 }
4624 break;
4625 }
4626
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004628 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004629 Location value_loc = locations->InAt(2);
4630 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004631 Register source = value;
4632
4633 if (instruction->InputAt(2)->IsNullConstant()) {
4634 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004635 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004636 size_t offset =
4637 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004638 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004639 } else {
4640 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004641 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004642 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004643 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004644 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004645 DCHECK(!needs_write_barrier);
4646 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004647 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004648 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004649
4650 DCHECK(needs_write_barrier);
4651 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4652 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4653 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4654 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4655 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4656 Label done;
4657 SlowPathCode* slow_path = nullptr;
4658
Roland Levillain3b359c72015-11-17 19:35:12 +00004659 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004660 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4661 codegen_->AddSlowPath(slow_path);
4662 if (instruction->GetValueCanBeNull()) {
4663 Label non_zero;
4664 __ CompareAndBranchIfNonZero(value, &non_zero);
4665 if (index.IsConstant()) {
4666 size_t offset =
4667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4668 __ StoreToOffset(kStoreWord, value, array, offset);
4669 } else {
4670 DCHECK(index.IsRegister()) << index;
4671 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4672 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4673 }
4674 codegen_->MaybeRecordImplicitNullCheck(instruction);
4675 __ b(&done);
4676 __ Bind(&non_zero);
4677 }
4678
Roland Levillain3b359c72015-11-17 19:35:12 +00004679 if (kEmitCompilerReadBarrier) {
4680 // When read barriers are enabled, the type checking
4681 // instrumentation requires two read barriers:
4682 //
4683 // __ Mov(temp2, temp1);
4684 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4685 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004686 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004687 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4688 //
4689 // // /* HeapReference<Class> */ temp2 = value->klass_
4690 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004691 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004692 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4693 //
4694 // __ cmp(temp1, ShifterOperand(temp2));
4695 //
4696 // However, the second read barrier may trash `temp`, as it
4697 // is a temporary register, and as such would not be saved
4698 // along with live registers before calling the runtime (nor
4699 // restored afterwards). So in this case, we bail out and
4700 // delegate the work to the array set slow path.
4701 //
4702 // TODO: Extend the register allocator to support a new
4703 // "(locally) live temp" location so as to avoid always
4704 // going into the slow path when read barriers are enabled.
4705 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004706 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004707 // /* HeapReference<Class> */ temp1 = array->klass_
4708 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4709 codegen_->MaybeRecordImplicitNullCheck(instruction);
4710 __ MaybeUnpoisonHeapReference(temp1);
4711
4712 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4713 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4714 // /* HeapReference<Class> */ temp2 = value->klass_
4715 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4716 // If heap poisoning is enabled, no need to unpoison `temp1`
4717 // nor `temp2`, as we are comparing two poisoned references.
4718 __ cmp(temp1, ShifterOperand(temp2));
4719
4720 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4721 Label do_put;
4722 __ b(&do_put, EQ);
4723 // If heap poisoning is enabled, the `temp1` reference has
4724 // not been unpoisoned yet; unpoison it now.
4725 __ MaybeUnpoisonHeapReference(temp1);
4726
4727 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4728 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4729 // If heap poisoning is enabled, no need to unpoison
4730 // `temp1`, as we are comparing against null below.
4731 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4732 __ Bind(&do_put);
4733 } else {
4734 __ b(slow_path->GetEntryLabel(), NE);
4735 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004736 }
4737 }
4738
4739 if (kPoisonHeapReferences) {
4740 // Note that in the case where `value` is a null reference,
4741 // we do not enter this block, as a null reference does not
4742 // need poisoning.
4743 DCHECK_EQ(value_type, Primitive::kPrimNot);
4744 __ Mov(temp1, value);
4745 __ PoisonHeapReference(temp1);
4746 source = temp1;
4747 }
4748
4749 if (index.IsConstant()) {
4750 size_t offset =
4751 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4752 __ StoreToOffset(kStoreWord, source, array, offset);
4753 } else {
4754 DCHECK(index.IsRegister()) << index;
4755 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4756 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4757 }
4758
Roland Levillain3b359c72015-11-17 19:35:12 +00004759 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004760 codegen_->MaybeRecordImplicitNullCheck(instruction);
4761 }
4762
4763 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4764
4765 if (done.IsLinked()) {
4766 __ Bind(&done);
4767 }
4768
4769 if (slow_path != nullptr) {
4770 __ Bind(slow_path->GetExitLabel());
4771 }
4772
4773 break;
4774 }
4775
4776 case Primitive::kPrimInt: {
4777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4778 Register value = locations->InAt(2).AsRegister<Register>();
4779 if (index.IsConstant()) {
4780 size_t offset =
4781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4782 __ StoreToOffset(kStoreWord, value, array, offset);
4783 } else {
4784 DCHECK(index.IsRegister()) << index;
4785 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4786 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4787 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004788 break;
4789 }
4790
4791 case Primitive::kPrimLong: {
4792 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004793 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004794 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004795 size_t offset =
4796 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004797 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004800 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 }
4802 break;
4803 }
4804
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004805 case Primitive::kPrimFloat: {
4806 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4807 Location value = locations->InAt(2);
4808 DCHECK(value.IsFpuRegister());
4809 if (index.IsConstant()) {
4810 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004811 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004812 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004813 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004814 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4815 }
4816 break;
4817 }
4818
4819 case Primitive::kPrimDouble: {
4820 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4821 Location value = locations->InAt(2);
4822 DCHECK(value.IsFpuRegisterPair());
4823 if (index.IsConstant()) {
4824 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004825 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004826 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004828 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4829 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004830
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004831 break;
4832 }
4833
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004834 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004835 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004836 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004837 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004838
Roland Levillain80e67092016-01-08 16:04:55 +00004839 // Objects are handled in the switch.
4840 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 codegen_->MaybeRecordImplicitNullCheck(instruction);
4842 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004843}
4844
4845void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004846 LocationSummary* locations =
4847 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004848 locations->SetInAt(0, Location::RequiresRegister());
4849 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004850}
4851
4852void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4853 LocationSummary* locations = instruction->GetLocations();
4854 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004855 Register obj = locations->InAt(0).AsRegister<Register>();
4856 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004858 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004859}
4860
4861void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004862 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4863 ? LocationSummary::kCallOnSlowPath
4864 : LocationSummary::kNoCall;
4865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004866 locations->SetInAt(0, Location::RequiresRegister());
4867 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004868 if (instruction->HasUses()) {
4869 locations->SetOut(Location::SameAsFirstInput());
4870 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004871}
4872
4873void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4874 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004875 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004876 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 codegen_->AddSlowPath(slow_path);
4878
Roland Levillain271ab9c2014-11-27 15:23:57 +00004879 Register index = locations->InAt(0).AsRegister<Register>();
4880 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881
4882 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004883 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004884}
4885
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004886void CodeGeneratorARM::MarkGCCard(Register temp,
4887 Register card,
4888 Register object,
4889 Register value,
4890 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004891 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004892 if (can_be_null) {
4893 __ CompareAndBranchIfZero(value, &is_null);
4894 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004895 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4896 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4897 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004898 if (can_be_null) {
4899 __ Bind(&is_null);
4900 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004901}
4902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4904 temp->SetLocations(nullptr);
4905}
4906
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004907void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004908 // Nothing to do, this is driven by the code generator.
4909}
4910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004911void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004912 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004913}
4914
4915void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004916 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4917}
4918
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004919void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4920 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4921}
4922
4923void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004924 HBasicBlock* block = instruction->GetBlock();
4925 if (block->GetLoopInformation() != nullptr) {
4926 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4927 // The back edge will generate the suspend check.
4928 return;
4929 }
4930 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4931 // The goto will generate the suspend check.
4932 return;
4933 }
4934 GenerateSuspendCheck(instruction, nullptr);
4935}
4936
4937void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4938 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004939 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004940 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4941 if (slow_path == nullptr) {
4942 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4943 instruction->SetSlowPath(slow_path);
4944 codegen_->AddSlowPath(slow_path);
4945 if (successor != nullptr) {
4946 DCHECK(successor->IsLoopHeader());
4947 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4948 }
4949 } else {
4950 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4951 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004952
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004953 __ LoadFromOffset(
4954 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004955 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004956 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004957 __ Bind(slow_path->GetReturnLabel());
4958 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004959 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004960 __ b(slow_path->GetEntryLabel());
4961 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004962}
4963
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004964ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4965 return codegen_->GetAssembler();
4966}
4967
4968void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004969 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004970 Location source = move->GetSource();
4971 Location destination = move->GetDestination();
4972
4973 if (source.IsRegister()) {
4974 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004975 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004976 } else {
4977 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004978 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004979 SP, destination.GetStackIndex());
4980 }
4981 } else if (source.IsStackSlot()) {
4982 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004983 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004984 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004985 } else if (destination.IsFpuRegister()) {
4986 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004987 } else {
4988 DCHECK(destination.IsStackSlot());
4989 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4990 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4991 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004992 } else if (source.IsFpuRegister()) {
4993 if (destination.IsFpuRegister()) {
4994 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004995 } else {
4996 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004997 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4998 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004999 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005000 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005001 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5002 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005003 } else if (destination.IsRegisterPair()) {
5004 DCHECK(ExpectedPairLayout(destination));
5005 __ LoadFromOffset(
5006 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5007 } else {
5008 DCHECK(destination.IsFpuRegisterPair()) << destination;
5009 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5010 SP,
5011 source.GetStackIndex());
5012 }
5013 } else if (source.IsRegisterPair()) {
5014 if (destination.IsRegisterPair()) {
5015 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5016 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
5017 } else {
5018 DCHECK(destination.IsDoubleStackSlot()) << destination;
5019 DCHECK(ExpectedPairLayout(source));
5020 __ StoreToOffset(
5021 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5022 }
5023 } else if (source.IsFpuRegisterPair()) {
5024 if (destination.IsFpuRegisterPair()) {
5025 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5026 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5027 } else {
5028 DCHECK(destination.IsDoubleStackSlot()) << destination;
5029 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5030 SP,
5031 destination.GetStackIndex());
5032 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005033 } else {
5034 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005035 HConstant* constant = source.GetConstant();
5036 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5037 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005038 if (destination.IsRegister()) {
5039 __ LoadImmediate(destination.AsRegister<Register>(), value);
5040 } else {
5041 DCHECK(destination.IsStackSlot());
5042 __ LoadImmediate(IP, value);
5043 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5044 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005045 } else if (constant->IsLongConstant()) {
5046 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005047 if (destination.IsRegisterPair()) {
5048 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5049 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005050 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005051 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005052 __ LoadImmediate(IP, Low32Bits(value));
5053 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5054 __ LoadImmediate(IP, High32Bits(value));
5055 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5056 }
5057 } else if (constant->IsDoubleConstant()) {
5058 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005059 if (destination.IsFpuRegisterPair()) {
5060 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005061 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005062 DCHECK(destination.IsDoubleStackSlot()) << destination;
5063 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005064 __ LoadImmediate(IP, Low32Bits(int_value));
5065 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5066 __ LoadImmediate(IP, High32Bits(int_value));
5067 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5068 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005069 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005070 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005071 float value = constant->AsFloatConstant()->GetValue();
5072 if (destination.IsFpuRegister()) {
5073 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5074 } else {
5075 DCHECK(destination.IsStackSlot());
5076 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5077 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5078 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005079 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005080 }
5081}
5082
5083void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5084 __ Mov(IP, reg);
5085 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5086 __ StoreToOffset(kStoreWord, IP, SP, mem);
5087}
5088
5089void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5090 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5091 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5092 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5093 SP, mem1 + stack_offset);
5094 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5095 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5096 SP, mem2 + stack_offset);
5097 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5098}
5099
5100void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005101 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005102 Location source = move->GetSource();
5103 Location destination = move->GetDestination();
5104
5105 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005106 DCHECK_NE(source.AsRegister<Register>(), IP);
5107 DCHECK_NE(destination.AsRegister<Register>(), IP);
5108 __ Mov(IP, source.AsRegister<Register>());
5109 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5110 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005111 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005112 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005113 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005114 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005115 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5116 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005117 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005118 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005119 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005120 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005121 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005122 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005123 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005124 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005125 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5126 destination.AsRegisterPairHigh<Register>(),
5127 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005128 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005129 Register low_reg = source.IsRegisterPair()
5130 ? source.AsRegisterPairLow<Register>()
5131 : destination.AsRegisterPairLow<Register>();
5132 int mem = source.IsRegisterPair()
5133 ? destination.GetStackIndex()
5134 : source.GetStackIndex();
5135 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005136 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005137 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005138 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005139 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005140 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5141 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005142 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005143 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005144 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005145 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5146 DRegister reg = source.IsFpuRegisterPair()
5147 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5148 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5149 int mem = source.IsFpuRegisterPair()
5150 ? destination.GetStackIndex()
5151 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005152 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005153 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005154 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005155 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5156 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5157 : destination.AsFpuRegister<SRegister>();
5158 int mem = source.IsFpuRegister()
5159 ? destination.GetStackIndex()
5160 : source.GetStackIndex();
5161
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005162 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005163 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005164 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005165 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005166 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5167 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005168 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005169 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005170 }
5171}
5172
5173void ParallelMoveResolverARM::SpillScratch(int reg) {
5174 __ Push(static_cast<Register>(reg));
5175}
5176
5177void ParallelMoveResolverARM::RestoreScratch(int reg) {
5178 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005179}
5180
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005181void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005182 InvokeRuntimeCallingConvention calling_convention;
5183 CodeGenerator::CreateLoadClassLocationSummary(
5184 cls,
5185 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005186 Location::RegisterLocation(R0),
5187 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005188}
5189
5190void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005191 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005192 if (cls->NeedsAccessCheck()) {
5193 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5194 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5195 cls,
5196 cls->GetDexPc(),
5197 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005198 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005199 return;
5200 }
5201
Roland Levillain3b359c72015-11-17 19:35:12 +00005202 Location out_loc = locations->Out();
5203 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005204 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005205
Calin Juravle580b6092015-10-06 17:35:58 +01005206 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005207 DCHECK(!cls->CanCallRuntime());
5208 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005209 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5210 GenerateGcRootFieldLoad(
5211 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005212 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005213 // /* GcRoot<mirror::Class>[] */ out =
5214 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005215 __ LoadFromOffset(kLoadWord,
5216 out,
5217 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005218 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005219 // /* GcRoot<mirror::Class> */ out = out[type_index]
5220 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005221
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005222 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5223 DCHECK(cls->CanCallRuntime());
5224 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5225 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5226 codegen_->AddSlowPath(slow_path);
5227 if (!cls->IsInDexCache()) {
5228 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5229 }
5230 if (cls->MustGenerateClinitCheck()) {
5231 GenerateClassInitializationCheck(slow_path, out);
5232 } else {
5233 __ Bind(slow_path->GetExitLabel());
5234 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005235 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005236 }
5237}
5238
5239void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5240 LocationSummary* locations =
5241 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5242 locations->SetInAt(0, Location::RequiresRegister());
5243 if (check->HasUses()) {
5244 locations->SetOut(Location::SameAsFirstInput());
5245 }
5246}
5247
5248void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005249 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005250 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005251 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005252 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005253 GenerateClassInitializationCheck(slow_path,
5254 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005255}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005256
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005257void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005258 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005259 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5260 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5261 __ b(slow_path->GetEntryLabel(), LT);
5262 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5263 // properly. Therefore, we do a memory fence.
5264 __ dmb(ISH);
5265 __ Bind(slow_path->GetExitLabel());
5266}
5267
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005268void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005269 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5270 ? LocationSummary::kCallOnSlowPath
5271 : LocationSummary::kNoCall;
5272 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005273 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005274 locations->SetOut(Location::RequiresRegister());
5275}
5276
5277void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005278 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005279 Location out_loc = locations->Out();
5280 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005281 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005282
Roland Levillainc9285912015-12-18 10:38:42 +00005283 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5284 GenerateGcRootFieldLoad(
5285 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005286 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005287 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005288 // /* GcRoot<mirror::String> */ out = out[string_index]
5289 GenerateGcRootFieldLoad(
5290 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005291
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005292 if (!load->IsInDexCache()) {
5293 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5294 codegen_->AddSlowPath(slow_path);
5295 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5296 __ Bind(slow_path->GetExitLabel());
5297 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005298}
5299
David Brazdilcb1c0552015-08-04 16:22:25 +01005300static int32_t GetExceptionTlsOffset() {
5301 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5302}
5303
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005304void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5305 LocationSummary* locations =
5306 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5307 locations->SetOut(Location::RequiresRegister());
5308}
5309
5310void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005311 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005312 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5313}
5314
5315void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5316 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5317}
5318
5319void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005320 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005321 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005322}
5323
5324void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5325 LocationSummary* locations =
5326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5327 InvokeRuntimeCallingConvention calling_convention;
5328 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5329}
5330
5331void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5332 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005333 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005334 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005335}
5336
Roland Levillainc9285912015-12-18 10:38:42 +00005337static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5338 return kEmitCompilerReadBarrier &&
5339 (kUseBakerReadBarrier ||
5340 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5341 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5342 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5343}
5344
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005345void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005346 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005347 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5348 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005349 case TypeCheckKind::kExactCheck:
5350 case TypeCheckKind::kAbstractClassCheck:
5351 case TypeCheckKind::kClassHierarchyCheck:
5352 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005353 call_kind =
5354 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005355 break;
5356 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005357 case TypeCheckKind::kUnresolvedCheck:
5358 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005359 call_kind = LocationSummary::kCallOnSlowPath;
5360 break;
5361 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005362
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005364 locations->SetInAt(0, Location::RequiresRegister());
5365 locations->SetInAt(1, Location::RequiresRegister());
5366 // The "out" register is used as a temporary, so it overlaps with the inputs.
5367 // Note that TypeCheckSlowPathARM uses this register too.
5368 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5369 // When read barriers are enabled, we need a temporary register for
5370 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005371 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005372 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005373 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005374}
5375
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005376void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005377 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005378 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005379 Location obj_loc = locations->InAt(0);
5380 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005381 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005382 Location out_loc = locations->Out();
5383 Register out = out_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005384 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5385 locations->GetTemp(0) :
5386 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005387 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005388 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5389 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5390 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005391 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005392 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005393
5394 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005395 // avoid null check if we know obj is not null.
5396 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005397 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005398 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005399
Roland Levillain3b359c72015-11-17 19:35:12 +00005400 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005401 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005402
Roland Levillainc9285912015-12-18 10:38:42 +00005403 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005404 case TypeCheckKind::kExactCheck: {
5405 __ cmp(out, ShifterOperand(cls));
5406 // Classes must be equal for the instanceof to succeed.
5407 __ b(&zero, NE);
5408 __ LoadImmediate(out, 1);
5409 __ b(&done);
5410 break;
5411 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005412
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005413 case TypeCheckKind::kAbstractClassCheck: {
5414 // If the class is abstract, we eagerly fetch the super class of the
5415 // object to avoid doing a comparison we know will fail.
5416 Label loop;
5417 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005418 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005419 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005420 // If `out` is null, we use it for the result, and jump to `done`.
5421 __ CompareAndBranchIfZero(out, &done);
5422 __ cmp(out, ShifterOperand(cls));
5423 __ b(&loop, NE);
5424 __ LoadImmediate(out, 1);
5425 if (zero.IsLinked()) {
5426 __ b(&done);
5427 }
5428 break;
5429 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005430
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005431 case TypeCheckKind::kClassHierarchyCheck: {
5432 // Walk over the class hierarchy to find a match.
5433 Label loop, success;
5434 __ Bind(&loop);
5435 __ cmp(out, ShifterOperand(cls));
5436 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005437 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005438 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005439 __ CompareAndBranchIfNonZero(out, &loop);
5440 // If `out` is null, we use it for the result, and jump to `done`.
5441 __ b(&done);
5442 __ Bind(&success);
5443 __ LoadImmediate(out, 1);
5444 if (zero.IsLinked()) {
5445 __ b(&done);
5446 }
5447 break;
5448 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005449
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005450 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005451 // Do an exact check.
5452 Label exact_check;
5453 __ cmp(out, ShifterOperand(cls));
5454 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005455 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005456 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005457 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005458 // If `out` is null, we use it for the result, and jump to `done`.
5459 __ CompareAndBranchIfZero(out, &done);
5460 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5461 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5462 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005463 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005464 __ LoadImmediate(out, 1);
5465 __ b(&done);
5466 break;
5467 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005469 case TypeCheckKind::kArrayCheck: {
5470 __ cmp(out, ShifterOperand(cls));
5471 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005472 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5473 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005474 codegen_->AddSlowPath(slow_path);
5475 __ b(slow_path->GetEntryLabel(), NE);
5476 __ LoadImmediate(out, 1);
5477 if (zero.IsLinked()) {
5478 __ b(&done);
5479 }
5480 break;
5481 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005482
Calin Juravle98893e12015-10-02 21:05:03 +01005483 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005484 case TypeCheckKind::kInterfaceCheck: {
5485 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005486 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005487 // cases.
5488 //
5489 // We cannot directly call the InstanceofNonTrivial runtime
5490 // entry point without resorting to a type checking slow path
5491 // here (i.e. by calling InvokeRuntime directly), as it would
5492 // require to assign fixed registers for the inputs of this
5493 // HInstanceOf instruction (following the runtime calling
5494 // convention), which might be cluttered by the potential first
5495 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005496 //
5497 // TODO: Introduce a new runtime entry point taking the object
5498 // to test (instead of its class) as argument, and let it deal
5499 // with the read barrier issues. This will let us refactor this
5500 // case of the `switch` code as it was previously (with a direct
5501 // call to the runtime not using a type checking slow path).
5502 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005503 DCHECK(locations->OnlyCallsOnSlowPath());
5504 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5505 /* is_fatal */ false);
5506 codegen_->AddSlowPath(slow_path);
5507 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005508 if (zero.IsLinked()) {
5509 __ b(&done);
5510 }
5511 break;
5512 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005513 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005514
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005515 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005516 __ Bind(&zero);
5517 __ LoadImmediate(out, 0);
5518 }
5519
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005520 if (done.IsLinked()) {
5521 __ Bind(&done);
5522 }
5523
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005524 if (slow_path != nullptr) {
5525 __ Bind(slow_path->GetExitLabel());
5526 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005527}
5528
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005529void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005530 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5531 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5532
Roland Levillain3b359c72015-11-17 19:35:12 +00005533 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5534 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005535 case TypeCheckKind::kExactCheck:
5536 case TypeCheckKind::kAbstractClassCheck:
5537 case TypeCheckKind::kClassHierarchyCheck:
5538 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005539 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5540 LocationSummary::kCallOnSlowPath :
5541 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005542 break;
5543 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005544 case TypeCheckKind::kUnresolvedCheck:
5545 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005546 call_kind = LocationSummary::kCallOnSlowPath;
5547 break;
5548 }
5549
Roland Levillain3b359c72015-11-17 19:35:12 +00005550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5551 locations->SetInAt(0, Location::RequiresRegister());
5552 locations->SetInAt(1, Location::RequiresRegister());
5553 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5554 locations->AddTemp(Location::RequiresRegister());
5555 // When read barriers are enabled, we need an additional temporary
5556 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005557 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005558 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005559 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005560}
5561
5562void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005563 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005564 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005565 Location obj_loc = locations->InAt(0);
5566 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005567 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005568 Location temp_loc = locations->GetTemp(0);
5569 Register temp = temp_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005570 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5571 locations->GetTemp(1) :
5572 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005573 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005574 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5575 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5576 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005577
Roland Levillain3b359c72015-11-17 19:35:12 +00005578 bool is_type_check_slow_path_fatal =
5579 (type_check_kind == TypeCheckKind::kExactCheck ||
5580 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5581 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5582 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5583 !instruction->CanThrowIntoCatchBlock();
5584 SlowPathCode* type_check_slow_path =
5585 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5586 is_type_check_slow_path_fatal);
5587 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005588
5589 Label done;
5590 // Avoid null check if we know obj is not null.
5591 if (instruction->MustDoNullCheck()) {
5592 __ CompareAndBranchIfZero(obj, &done);
5593 }
5594
Roland Levillain3b359c72015-11-17 19:35:12 +00005595 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005596 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005597
Roland Levillain3b359c72015-11-17 19:35:12 +00005598 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005599 case TypeCheckKind::kExactCheck:
5600 case TypeCheckKind::kArrayCheck: {
5601 __ cmp(temp, ShifterOperand(cls));
5602 // Jump to slow path for throwing the exception or doing a
5603 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005604 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005605 break;
5606 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005607
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005608 case TypeCheckKind::kAbstractClassCheck: {
5609 // If the class is abstract, we eagerly fetch the super class of the
5610 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005611 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005612 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005613 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005614 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005615
5616 // If the class reference currently in `temp` is not null, jump
5617 // to the `compare_classes` label to compare it with the checked
5618 // class.
5619 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5620 // Otherwise, jump to the slow path to throw the exception.
5621 //
5622 // But before, move back the object's class into `temp` before
5623 // going into the slow path, as it has been overwritten in the
5624 // meantime.
5625 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005626 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005627 __ b(type_check_slow_path->GetEntryLabel());
5628
5629 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005630 __ cmp(temp, ShifterOperand(cls));
5631 __ b(&loop, NE);
5632 break;
5633 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005634
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005635 case TypeCheckKind::kClassHierarchyCheck: {
5636 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005637 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005638 __ Bind(&loop);
5639 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005640 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005641
Roland Levillain3b359c72015-11-17 19:35:12 +00005642 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005643 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005644
5645 // If the class reference currently in `temp` is not null, jump
5646 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005647 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005648 // Otherwise, jump to the slow path to throw the exception.
5649 //
5650 // But before, move back the object's class into `temp` before
5651 // going into the slow path, as it has been overwritten in the
5652 // meantime.
5653 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005654 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005655 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005656 break;
5657 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005658
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005659 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005660 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005661 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005662 __ cmp(temp, ShifterOperand(cls));
5663 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005664
5665 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005666 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005667 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005668
5669 // If the component type is not null (i.e. the object is indeed
5670 // an array), jump to label `check_non_primitive_component_type`
5671 // to further check that this component type is not a primitive
5672 // type.
5673 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5674 // Otherwise, jump to the slow path to throw the exception.
5675 //
5676 // But before, move back the object's class into `temp` before
5677 // going into the slow path, as it has been overwritten in the
5678 // meantime.
5679 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005680 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005681 __ b(type_check_slow_path->GetEntryLabel());
5682
5683 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005685 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5686 __ CompareAndBranchIfZero(temp, &done);
5687 // Same comment as above regarding `temp` and the slow path.
5688 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005689 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005690 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005691 break;
5692 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005693
Calin Juravle98893e12015-10-02 21:05:03 +01005694 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005695 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005696 // We always go into the type check slow path for the unresolved
5697 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005698 //
5699 // We cannot directly call the CheckCast runtime entry point
5700 // without resorting to a type checking slow path here (i.e. by
5701 // calling InvokeRuntime directly), as it would require to
5702 // assign fixed registers for the inputs of this HInstanceOf
5703 // instruction (following the runtime calling convention), which
5704 // might be cluttered by the potential first read barrier
5705 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005706 //
5707 // TODO: Introduce a new runtime entry point taking the object
5708 // to test (instead of its class) as argument, and let it deal
5709 // with the read barrier issues. This will let us refactor this
5710 // case of the `switch` code as it was previously (with a direct
5711 // call to the runtime not using a type checking slow path).
5712 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005713 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005714 break;
5715 }
5716 __ Bind(&done);
5717
Roland Levillain3b359c72015-11-17 19:35:12 +00005718 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005719}
5720
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005721void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5722 LocationSummary* locations =
5723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5724 InvokeRuntimeCallingConvention calling_convention;
5725 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5726}
5727
5728void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5729 codegen_->InvokeRuntime(instruction->IsEnter()
5730 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5731 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005732 instruction->GetDexPc(),
5733 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005734 if (instruction->IsEnter()) {
5735 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5736 } else {
5737 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5738 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005739}
5740
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005741void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5742void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5743void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005744
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005745void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005746 LocationSummary* locations =
5747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5748 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5749 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005750 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005751 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005752 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005754}
5755
5756void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5757 HandleBitwiseOperation(instruction);
5758}
5759
5760void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5761 HandleBitwiseOperation(instruction);
5762}
5763
5764void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5765 HandleBitwiseOperation(instruction);
5766}
5767
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005768void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5769 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5770 if (value == 0xffffffffu) {
5771 if (out != first) {
5772 __ mov(out, ShifterOperand(first));
5773 }
5774 return;
5775 }
5776 if (value == 0u) {
5777 __ mov(out, ShifterOperand(0));
5778 return;
5779 }
5780 ShifterOperand so;
5781 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5782 __ and_(out, first, so);
5783 } else {
5784 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5785 __ bic(out, first, ShifterOperand(~value));
5786 }
5787}
5788
5789void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5790 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5791 if (value == 0u) {
5792 if (out != first) {
5793 __ mov(out, ShifterOperand(first));
5794 }
5795 return;
5796 }
5797 if (value == 0xffffffffu) {
5798 __ mvn(out, ShifterOperand(0));
5799 return;
5800 }
5801 ShifterOperand so;
5802 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5803 __ orr(out, first, so);
5804 } else {
5805 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5806 __ orn(out, first, ShifterOperand(~value));
5807 }
5808}
5809
5810void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5811 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5812 if (value == 0u) {
5813 if (out != first) {
5814 __ mov(out, ShifterOperand(first));
5815 }
5816 return;
5817 }
5818 __ eor(out, first, ShifterOperand(value));
5819}
5820
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005821void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5822 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005823 Location first = locations->InAt(0);
5824 Location second = locations->InAt(1);
5825 Location out = locations->Out();
5826
5827 if (second.IsConstant()) {
5828 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5829 uint32_t value_low = Low32Bits(value);
5830 if (instruction->GetResultType() == Primitive::kPrimInt) {
5831 Register first_reg = first.AsRegister<Register>();
5832 Register out_reg = out.AsRegister<Register>();
5833 if (instruction->IsAnd()) {
5834 GenerateAndConst(out_reg, first_reg, value_low);
5835 } else if (instruction->IsOr()) {
5836 GenerateOrrConst(out_reg, first_reg, value_low);
5837 } else {
5838 DCHECK(instruction->IsXor());
5839 GenerateEorConst(out_reg, first_reg, value_low);
5840 }
5841 } else {
5842 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5843 uint32_t value_high = High32Bits(value);
5844 Register first_low = first.AsRegisterPairLow<Register>();
5845 Register first_high = first.AsRegisterPairHigh<Register>();
5846 Register out_low = out.AsRegisterPairLow<Register>();
5847 Register out_high = out.AsRegisterPairHigh<Register>();
5848 if (instruction->IsAnd()) {
5849 GenerateAndConst(out_low, first_low, value_low);
5850 GenerateAndConst(out_high, first_high, value_high);
5851 } else if (instruction->IsOr()) {
5852 GenerateOrrConst(out_low, first_low, value_low);
5853 GenerateOrrConst(out_high, first_high, value_high);
5854 } else {
5855 DCHECK(instruction->IsXor());
5856 GenerateEorConst(out_low, first_low, value_low);
5857 GenerateEorConst(out_high, first_high, value_high);
5858 }
5859 }
5860 return;
5861 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005862
5863 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005864 Register first_reg = first.AsRegister<Register>();
5865 ShifterOperand second_reg(second.AsRegister<Register>());
5866 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005867 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005868 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005869 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005870 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005871 } else {
5872 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005873 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005874 }
5875 } else {
5876 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005877 Register first_low = first.AsRegisterPairLow<Register>();
5878 Register first_high = first.AsRegisterPairHigh<Register>();
5879 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5880 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5881 Register out_low = out.AsRegisterPairLow<Register>();
5882 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005883 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005884 __ and_(out_low, first_low, second_low);
5885 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005886 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005887 __ orr(out_low, first_low, second_low);
5888 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005889 } else {
5890 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005891 __ eor(out_low, first_low, second_low);
5892 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005893 }
5894 }
5895}
5896
Roland Levillainc9285912015-12-18 10:38:42 +00005897void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5898 Location out,
5899 uint32_t offset,
5900 Location temp) {
5901 Register out_reg = out.AsRegister<Register>();
5902 if (kEmitCompilerReadBarrier) {
5903 if (kUseBakerReadBarrier) {
5904 // Load with fast path based Baker's read barrier.
5905 // /* HeapReference<Object> */ out = *(out + offset)
5906 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5907 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
5908 } else {
5909 // Load with slow path based read barrier.
5910 // Save the value of `out` into `temp` before overwriting it
5911 // in the following move operation, as we will need it for the
5912 // read barrier below.
5913 __ Mov(temp.AsRegister<Register>(), out_reg);
5914 // /* HeapReference<Object> */ out = *(out + offset)
5915 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5916 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
5917 }
5918 } else {
5919 // Plain load with no read barrier.
5920 // /* HeapReference<Object> */ out = *(out + offset)
5921 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5922 __ MaybeUnpoisonHeapReference(out_reg);
5923 }
5924}
5925
5926void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5927 Location out,
5928 Location obj,
5929 uint32_t offset,
5930 Location temp) {
5931 Register out_reg = out.AsRegister<Register>();
5932 Register obj_reg = obj.AsRegister<Register>();
5933 if (kEmitCompilerReadBarrier) {
5934 if (kUseBakerReadBarrier) {
5935 // Load with fast path based Baker's read barrier.
5936 // /* HeapReference<Object> */ out = *(obj + offset)
5937 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5938 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
5939 } else {
5940 // Load with slow path based read barrier.
5941 // /* HeapReference<Object> */ out = *(obj + offset)
5942 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5943 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5944 }
5945 } else {
5946 // Plain load with no read barrier.
5947 // /* HeapReference<Object> */ out = *(obj + offset)
5948 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5949 __ MaybeUnpoisonHeapReference(out_reg);
5950 }
5951}
5952
5953void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
5954 Location root,
5955 Register obj,
5956 uint32_t offset) {
5957 Register root_reg = root.AsRegister<Register>();
5958 if (kEmitCompilerReadBarrier) {
5959 if (kUseBakerReadBarrier) {
5960 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5961 // Baker's read barrier are used:
5962 //
5963 // root = obj.field;
5964 // if (Thread::Current()->GetIsGcMarking()) {
5965 // root = ReadBarrier::Mark(root)
5966 // }
5967
5968 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5969 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5970 static_assert(
5971 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5972 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5973 "have different sizes.");
5974 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5975 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5976 "have different sizes.");
5977
5978 // Slow path used to mark the GC root `root`.
5979 SlowPathCode* slow_path =
5980 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
5981 codegen_->AddSlowPath(slow_path);
5982
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005983 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00005984 __ LoadFromOffset(
5985 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
5986 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
5987 __ Bind(slow_path->GetExitLabel());
5988 } else {
5989 // GC root loaded through a slow path for read barriers other
5990 // than Baker's.
5991 // /* GcRoot<mirror::Object>* */ root = obj + offset
5992 __ AddConstant(root_reg, obj, offset);
5993 // /* mirror::Object* */ root = root->Read()
5994 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5995 }
5996 } else {
5997 // Plain GC root load with no read barrier.
5998 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5999 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6000 // Note that GC roots are not affected by heap poisoning, thus we
6001 // do not have to unpoison `root_reg` here.
6002 }
6003}
6004
6005void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6006 Location ref,
6007 Register obj,
6008 uint32_t offset,
6009 Location temp,
6010 bool needs_null_check) {
6011 DCHECK(kEmitCompilerReadBarrier);
6012 DCHECK(kUseBakerReadBarrier);
6013
6014 // /* HeapReference<Object> */ ref = *(obj + offset)
6015 Location no_index = Location::NoLocation();
6016 GenerateReferenceLoadWithBakerReadBarrier(
6017 instruction, ref, obj, offset, no_index, temp, needs_null_check);
6018}
6019
6020void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6021 Location ref,
6022 Register obj,
6023 uint32_t data_offset,
6024 Location index,
6025 Location temp,
6026 bool needs_null_check) {
6027 DCHECK(kEmitCompilerReadBarrier);
6028 DCHECK(kUseBakerReadBarrier);
6029
6030 // /* HeapReference<Object> */ ref =
6031 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6032 GenerateReferenceLoadWithBakerReadBarrier(
6033 instruction, ref, obj, data_offset, index, temp, needs_null_check);
6034}
6035
6036void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6037 Location ref,
6038 Register obj,
6039 uint32_t offset,
6040 Location index,
6041 Location temp,
6042 bool needs_null_check) {
6043 DCHECK(kEmitCompilerReadBarrier);
6044 DCHECK(kUseBakerReadBarrier);
6045
6046 // In slow path based read barriers, the read barrier call is
6047 // inserted after the original load. However, in fast path based
6048 // Baker's read barriers, we need to perform the load of
6049 // mirror::Object::monitor_ *before* the original reference load.
6050 // This load-load ordering is required by the read barrier.
6051 // The fast path/slow path (for Baker's algorithm) should look like:
6052 //
6053 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6054 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6055 // HeapReference<Object> ref = *src; // Original reference load.
6056 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6057 // if (is_gray) {
6058 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6059 // }
6060 //
6061 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006062 // slightly more complex as it performs additional checks that we do
6063 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006064
6065 Register ref_reg = ref.AsRegister<Register>();
6066 Register temp_reg = temp.AsRegister<Register>();
6067 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6068
6069 // /* int32_t */ monitor = obj->monitor_
6070 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6071 if (needs_null_check) {
6072 MaybeRecordImplicitNullCheck(instruction);
6073 }
6074 // /* LockWord */ lock_word = LockWord(monitor)
6075 static_assert(sizeof(LockWord) == sizeof(int32_t),
6076 "art::LockWord and int32_t have different sizes.");
6077 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6078 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6079 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6080 static_assert(
6081 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6082 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6083
6084 // Introduce a dependency on the high bits of rb_state, which shall
6085 // be all zeroes, to prevent load-load reordering, and without using
6086 // a memory barrier (which would be more expensive).
6087 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6088 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6089 // obj is unchanged by this operation, but its value now depends on
6090 // IP, which depends on temp_reg.
6091 __ add(obj, obj, ShifterOperand(IP));
6092
6093 // The actual reference load.
6094 if (index.IsValid()) {
6095 static_assert(
6096 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6097 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6098 // /* HeapReference<Object> */ ref =
6099 // *(obj + offset + index * sizeof(HeapReference<Object>))
6100 if (index.IsConstant()) {
6101 size_t computed_offset =
6102 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6103 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6104 } else {
6105 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6106 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6107 }
6108 } else {
6109 // /* HeapReference<Object> */ ref = *(obj + offset)
6110 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6111 }
6112
6113 // Object* ref = ref_addr->AsMirrorPtr()
6114 __ MaybeUnpoisonHeapReference(ref_reg);
6115
6116 // Slow path used to mark the object `ref` when it is gray.
6117 SlowPathCode* slow_path =
6118 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6119 AddSlowPath(slow_path);
6120
6121 // if (rb_state == ReadBarrier::gray_ptr_)
6122 // ref = ReadBarrier::Mark(ref);
6123 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6124 __ b(slow_path->GetEntryLabel(), EQ);
6125 __ Bind(slow_path->GetExitLabel());
6126}
6127
6128void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6129 Location out,
6130 Location ref,
6131 Location obj,
6132 uint32_t offset,
6133 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006134 DCHECK(kEmitCompilerReadBarrier);
6135
Roland Levillainc9285912015-12-18 10:38:42 +00006136 // Insert a slow path based read barrier *after* the reference load.
6137 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006138 // If heap poisoning is enabled, the unpoisoning of the loaded
6139 // reference will be carried out by the runtime within the slow
6140 // path.
6141 //
6142 // Note that `ref` currently does not get unpoisoned (when heap
6143 // poisoning is enabled), which is alright as the `ref` argument is
6144 // not used by the artReadBarrierSlow entry point.
6145 //
6146 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6147 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6148 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6149 AddSlowPath(slow_path);
6150
Roland Levillain3b359c72015-11-17 19:35:12 +00006151 __ b(slow_path->GetEntryLabel());
6152 __ Bind(slow_path->GetExitLabel());
6153}
6154
Roland Levillainc9285912015-12-18 10:38:42 +00006155void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6156 Location out,
6157 Location ref,
6158 Location obj,
6159 uint32_t offset,
6160 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006161 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006162 // Baker's read barriers shall be handled by the fast path
6163 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6164 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006165 // If heap poisoning is enabled, unpoisoning will be taken care of
6166 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006167 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006168 } else if (kPoisonHeapReferences) {
6169 __ UnpoisonHeapReference(out.AsRegister<Register>());
6170 }
6171}
6172
Roland Levillainc9285912015-12-18 10:38:42 +00006173void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6174 Location out,
6175 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006176 DCHECK(kEmitCompilerReadBarrier);
6177
Roland Levillainc9285912015-12-18 10:38:42 +00006178 // Insert a slow path based read barrier *after* the GC root load.
6179 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006180 // Note that GC roots are not affected by heap poisoning, so we do
6181 // not need to do anything special for this here.
6182 SlowPathCode* slow_path =
6183 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6184 AddSlowPath(slow_path);
6185
Roland Levillain3b359c72015-11-17 19:35:12 +00006186 __ b(slow_path->GetEntryLabel());
6187 __ Bind(slow_path->GetExitLabel());
6188}
6189
Vladimir Markodc151b22015-10-15 18:02:30 +01006190HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6191 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6192 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006193 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6194 // We disable pc-relative load when there is an irreducible loop, as the optimization
6195 // is incompatible with it.
6196 if (GetGraph()->HasIrreducibleLoops() &&
6197 (dispatch_info.method_load_kind ==
6198 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6199 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6200 }
6201
6202 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006203 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6204 if (&outer_dex_file != target_method.dex_file) {
6205 // Calls across dex files are more likely to exceed the available BL range,
6206 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6207 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6208 (desired_dispatch_info.method_load_kind ==
6209 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6210 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6211 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6212 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006213 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006214 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006215 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006216 0u
6217 };
6218 }
6219 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006220 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006221}
6222
Vladimir Markob4536b72015-11-24 13:45:23 +00006223Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6224 Register temp) {
6225 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6226 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6227 if (!invoke->GetLocations()->Intrinsified()) {
6228 return location.AsRegister<Register>();
6229 }
6230 // For intrinsics we allow any location, so it may be on the stack.
6231 if (!location.IsRegister()) {
6232 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6233 return temp;
6234 }
6235 // For register locations, check if the register was saved. If so, get it from the stack.
6236 // Note: There is a chance that the register was saved but not overwritten, so we could
6237 // save one load. However, since this is just an intrinsic slow path we prefer this
6238 // simple and more robust approach rather that trying to determine if that's the case.
6239 SlowPathCode* slow_path = GetCurrentSlowPath();
6240 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6241 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6242 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6243 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6244 return temp;
6245 }
6246 return location.AsRegister<Register>();
6247}
6248
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006249void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006250 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006251 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006252 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6253 // LR = code address from literal pool with link-time patch.
6254 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006255 break;
6256 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6257 // LR = invoke->GetDirectCodePtr();
6258 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006259 break;
6260 default:
6261 break;
6262 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006263
Vladimir Marko58155012015-08-19 12:49:41 +00006264 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6265 switch (invoke->GetMethodLoadKind()) {
6266 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6267 // temp = thread->string_init_entrypoint
6268 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6269 break;
6270 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006271 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006272 break;
6273 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6274 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6275 break;
6276 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6277 __ LoadLiteral(temp.AsRegister<Register>(),
6278 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6279 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006280 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6281 HArmDexCacheArraysBase* base =
6282 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6283 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6284 temp.AsRegister<Register>());
6285 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6286 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6287 break;
6288 }
Vladimir Marko58155012015-08-19 12:49:41 +00006289 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006290 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006291 Register method_reg;
6292 Register reg = temp.AsRegister<Register>();
6293 if (current_method.IsRegister()) {
6294 method_reg = current_method.AsRegister<Register>();
6295 } else {
6296 DCHECK(invoke->GetLocations()->Intrinsified());
6297 DCHECK(!current_method.IsValid());
6298 method_reg = reg;
6299 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6300 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006301 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6302 __ LoadFromOffset(kLoadWord,
6303 reg,
6304 method_reg,
6305 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006306 // temp = temp[index_in_cache]
6307 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6308 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6309 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006310 }
Vladimir Marko58155012015-08-19 12:49:41 +00006311 }
6312
6313 switch (invoke->GetCodePtrLocation()) {
6314 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6315 __ bl(GetFrameEntryLabel());
6316 break;
6317 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006318 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006319 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006320 // Arbitrarily branch to the BL itself, override at link time.
6321 __ bl(&relative_call_patches_.back().label);
6322 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006323 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6324 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6325 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006326 // LR()
6327 __ blx(LR);
6328 break;
6329 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6330 // LR = callee_method->entry_point_from_quick_compiled_code_
6331 __ LoadFromOffset(
6332 kLoadWord, LR, callee_method.AsRegister<Register>(),
6333 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6334 // LR()
6335 __ blx(LR);
6336 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006337 }
6338
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006339 DCHECK(!IsLeafMethod());
6340}
6341
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006342void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6343 Register temp = temp_location.AsRegister<Register>();
6344 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6345 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006346
6347 // Use the calling convention instead of the location of the receiver, as
6348 // intrinsics may have put the receiver in a different register. In the intrinsics
6349 // slow path, the arguments have been moved to the right place, so here we are
6350 // guaranteed that the receiver is the first register of the calling convention.
6351 InvokeDexCallingConvention calling_convention;
6352 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006353 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006354 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006355 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006356 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006357 // Instead of simply (possibly) unpoisoning `temp` here, we should
6358 // emit a read barrier for the previous class reference load.
6359 // However this is not required in practice, as this is an
6360 // intermediate/temporary reference and because the current
6361 // concurrent copying collector keeps the from-space memory
6362 // intact/accessible until the end of the marking phase (the
6363 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006364 __ MaybeUnpoisonHeapReference(temp);
6365 // temp = temp->GetMethodAt(method_offset);
6366 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6367 kArmWordSize).Int32Value();
6368 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6369 // LR = temp->GetEntryPoint();
6370 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6371 // LR();
6372 __ blx(LR);
6373}
6374
Vladimir Marko58155012015-08-19 12:49:41 +00006375void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6376 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006377 size_t size =
6378 method_patches_.size() +
6379 call_patches_.size() +
6380 relative_call_patches_.size() +
6381 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006382 linker_patches->reserve(size);
6383 for (const auto& entry : method_patches_) {
6384 const MethodReference& target_method = entry.first;
6385 Literal* literal = entry.second;
6386 DCHECK(literal->GetLabel()->IsBound());
6387 uint32_t literal_offset = literal->GetLabel()->Position();
6388 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6389 target_method.dex_file,
6390 target_method.dex_method_index));
6391 }
6392 for (const auto& entry : call_patches_) {
6393 const MethodReference& target_method = entry.first;
6394 Literal* literal = entry.second;
6395 DCHECK(literal->GetLabel()->IsBound());
6396 uint32_t literal_offset = literal->GetLabel()->Position();
6397 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6398 target_method.dex_file,
6399 target_method.dex_method_index));
6400 }
6401 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6402 uint32_t literal_offset = info.label.Position();
6403 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6404 info.target_method.dex_file,
6405 info.target_method.dex_method_index));
6406 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006407 for (const auto& pair : dex_cache_arrays_base_labels_) {
6408 HArmDexCacheArraysBase* base = pair.first;
6409 const DexCacheArraysBaseLabels* labels = &pair.second;
6410 const DexFile& dex_file = base->GetDexFile();
6411 size_t base_element_offset = base->GetElementOffset();
6412 DCHECK(labels->add_pc_label.IsBound());
6413 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6414 // Add MOVW patch.
6415 DCHECK(labels->movw_label.IsBound());
6416 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6417 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6418 &dex_file,
6419 add_pc_offset,
6420 base_element_offset));
6421 // Add MOVT patch.
6422 DCHECK(labels->movt_label.IsBound());
6423 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6424 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6425 &dex_file,
6426 add_pc_offset,
6427 base_element_offset));
6428 }
Vladimir Marko58155012015-08-19 12:49:41 +00006429}
6430
6431Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6432 MethodToLiteralMap* map) {
6433 // Look up the literal for target_method.
6434 auto lb = map->lower_bound(target_method);
6435 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6436 return lb->second;
6437 }
6438 // We don't have a literal for this method yet, insert a new one.
6439 Literal* literal = __ NewLiteral<uint32_t>(0u);
6440 map->PutBefore(lb, target_method, literal);
6441 return literal;
6442}
6443
6444Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6445 return DeduplicateMethodLiteral(target_method, &method_patches_);
6446}
6447
6448Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6449 return DeduplicateMethodLiteral(target_method, &call_patches_);
6450}
6451
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006452void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006453 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006454 LOG(FATAL) << "Unreachable";
6455}
6456
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006457void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006458 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006459 LOG(FATAL) << "Unreachable";
6460}
6461
Mark Mendellfe57faa2015-09-18 09:26:15 -04006462// Simple implementation of packed switch - generate cascaded compare/jumps.
6463void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6464 LocationSummary* locations =
6465 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6466 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006467 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006468 codegen_->GetAssembler()->IsThumb()) {
6469 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6470 if (switch_instr->GetStartValue() != 0) {
6471 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6472 }
6473 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006474}
6475
6476void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6477 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006478 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006479 LocationSummary* locations = switch_instr->GetLocations();
6480 Register value_reg = locations->InAt(0).AsRegister<Register>();
6481 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6482
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006483 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006484 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006485 Register temp_reg = IP;
6486 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6487 // the immediate, because IP is used as the destination register. For the other
6488 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6489 // and they can be encoded in the instruction without making use of IP register.
6490 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6491
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006492 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006493 // Jump to successors[0] if value == lower_bound.
6494 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6495 int32_t last_index = 0;
6496 for (; num_entries - last_index > 2; last_index += 2) {
6497 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6498 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6499 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6500 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6501 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6502 }
6503 if (num_entries - last_index == 2) {
6504 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006505 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006506 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006507 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006508
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006509 // And the default for any other value.
6510 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6511 __ b(codegen_->GetLabelOf(default_block));
6512 }
6513 } else {
6514 // Create a table lookup.
6515 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6516
6517 // Materialize a pointer to the switch table
6518 std::vector<Label*> labels(num_entries);
6519 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6520 for (uint32_t i = 0; i < num_entries; i++) {
6521 labels[i] = codegen_->GetLabelOf(successors[i]);
6522 }
6523 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6524
6525 // Remove the bias.
6526 Register key_reg;
6527 if (lower_bound != 0) {
6528 key_reg = locations->GetTemp(1).AsRegister<Register>();
6529 __ AddConstant(key_reg, value_reg, -lower_bound);
6530 } else {
6531 key_reg = value_reg;
6532 }
6533
6534 // Check whether the value is in the table, jump to default block if not.
6535 __ CmpConstant(key_reg, num_entries - 1);
6536 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6537
6538 // Load the displacement from the table.
6539 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6540
6541 // Dispatch is a direct add to the PC (for Thumb2).
6542 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006543 }
6544}
6545
Vladimir Markob4536b72015-11-24 13:45:23 +00006546void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6548 locations->SetOut(Location::RequiresRegister());
6549 codegen_->AddDexCacheArraysBase(base);
6550}
6551
6552void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6553 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6554 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6555 __ BindTrackedLabel(&labels->movw_label);
6556 __ movw(base_reg, 0u);
6557 __ BindTrackedLabel(&labels->movt_label);
6558 __ movt(base_reg, 0u);
6559 __ BindTrackedLabel(&labels->add_pc_label);
6560 __ add(base_reg, base_reg, ShifterOperand(PC));
6561}
6562
Andreas Gampe85b62f22015-09-09 13:15:38 -07006563void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6564 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006565 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006566 return;
6567 }
6568
6569 DCHECK_NE(type, Primitive::kPrimVoid);
6570
6571 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6572 if (return_loc.Equals(trg)) {
6573 return;
6574 }
6575
6576 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6577 // with the last branch.
6578 if (type == Primitive::kPrimLong) {
6579 HParallelMove parallel_move(GetGraph()->GetArena());
6580 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6581 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6582 GetMoveResolver()->EmitNativeCode(&parallel_move);
6583 } else if (type == Primitive::kPrimDouble) {
6584 HParallelMove parallel_move(GetGraph()->GetArena());
6585 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6586 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6587 GetMoveResolver()->EmitNativeCode(&parallel_move);
6588 } else {
6589 // Let the parallel move resolver take care of all of this.
6590 HParallelMove parallel_move(GetGraph()->GetArena());
6591 parallel_move.AddMove(return_loc, trg, type, nullptr);
6592 GetMoveResolver()->EmitNativeCode(&parallel_move);
6593 }
6594}
6595
Roland Levillain4d027112015-07-01 15:41:14 +01006596#undef __
6597#undef QUICK_ENTRY_POINT
6598
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006599} // namespace arm
6600} // namespace art