blob: 005b6c189eb05fcf3be93934b4146284c0cbf338 [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
Calin Juravle175dc732015-08-25 15:42:32 +01001198void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1199 DCHECK(location.IsRegister());
1200 __ LoadImmediate(location.AsRegister<Register>(), value);
1201}
1202
Calin Juravlee460d1d2015-09-29 04:52:17 +01001203void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001204 HParallelMove move(GetGraph()->GetArena());
1205 move.AddMove(src, dst, dst_type, nullptr);
1206 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207}
1208
1209void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1210 if (location.IsRegister()) {
1211 locations->AddTemp(location);
1212 } else if (location.IsRegisterPair()) {
1213 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1214 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1215 } else {
1216 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1217 }
1218}
1219
Calin Juravle175dc732015-08-25 15:42:32 +01001220void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1221 HInstruction* instruction,
1222 uint32_t dex_pc,
1223 SlowPathCode* slow_path) {
1224 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1225 instruction,
1226 dex_pc,
1227 slow_path);
1228}
1229
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001230void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1231 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001232 uint32_t dex_pc,
1233 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001234 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001235 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1236 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001237 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001238}
1239
David Brazdilfc6a86a2015-06-26 10:33:45 +00001240void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001241 DCHECK(!successor->IsExitBlock());
1242
1243 HBasicBlock* block = got->GetBlock();
1244 HInstruction* previous = got->GetPrevious();
1245
1246 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001247 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001248 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1249 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1250 return;
1251 }
1252
1253 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1254 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1255 }
1256 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001257 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001258 }
1259}
1260
David Brazdilfc6a86a2015-06-26 10:33:45 +00001261void LocationsBuilderARM::VisitGoto(HGoto* got) {
1262 got->SetLocations(nullptr);
1263}
1264
1265void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1266 HandleGoto(got, got->GetSuccessor());
1267}
1268
1269void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1270 try_boundary->SetLocations(nullptr);
1271}
1272
1273void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1274 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1275 if (!successor->IsExitBlock()) {
1276 HandleGoto(try_boundary, successor);
1277 }
1278}
1279
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001280void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001281 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001282}
1283
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001284void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001285}
1286
Roland Levillain4fa13f62015-07-06 18:11:54 +01001287void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1288 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001289 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001290 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001291 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001292}
1293
1294void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1295 Label* true_label,
1296 Label* false_label) {
1297 LocationSummary* locations = cond->GetLocations();
1298 Location left = locations->InAt(0);
1299 Location right = locations->InAt(1);
1300 IfCondition if_cond = cond->GetCondition();
1301
1302 Register left_high = left.AsRegisterPairHigh<Register>();
1303 Register left_low = left.AsRegisterPairLow<Register>();
1304 IfCondition true_high_cond = if_cond;
1305 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001306 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001307
1308 // Set the conditions for the test, remembering that == needs to be
1309 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001310 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001311 switch (if_cond) {
1312 case kCondEQ:
1313 case kCondNE:
1314 // Nothing to do.
1315 break;
1316 case kCondLT:
1317 false_high_cond = kCondGT;
1318 break;
1319 case kCondLE:
1320 true_high_cond = kCondLT;
1321 break;
1322 case kCondGT:
1323 false_high_cond = kCondLT;
1324 break;
1325 case kCondGE:
1326 true_high_cond = kCondGT;
1327 break;
Aart Bike9f37602015-10-09 11:15:55 -07001328 case kCondB:
1329 false_high_cond = kCondA;
1330 break;
1331 case kCondBE:
1332 true_high_cond = kCondB;
1333 break;
1334 case kCondA:
1335 false_high_cond = kCondB;
1336 break;
1337 case kCondAE:
1338 true_high_cond = kCondA;
1339 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001340 }
1341 if (right.IsConstant()) {
1342 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1343 int32_t val_low = Low32Bits(value);
1344 int32_t val_high = High32Bits(value);
1345
Vladimir Markoac6ac102015-12-17 12:14:00 +00001346 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001347 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001348 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001349 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001350 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001351 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001352 __ b(true_label, ARMCondition(true_high_cond));
1353 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 }
1355 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001356 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001357 } else {
1358 Register right_high = right.AsRegisterPairHigh<Register>();
1359 Register right_low = right.AsRegisterPairLow<Register>();
1360
1361 __ cmp(left_high, ShifterOperand(right_high));
1362 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001363 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001364 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001365 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001366 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001367 __ b(true_label, ARMCondition(true_high_cond));
1368 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001369 }
1370 // Must be equal high, so compare the lows.
1371 __ cmp(left_low, ShifterOperand(right_low));
1372 }
1373 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001374 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001375 __ b(true_label, final_condition);
1376}
1377
David Brazdil0debae72015-11-12 18:37:00 +00001378void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1379 Label* true_target_in,
1380 Label* false_target_in) {
1381 // Generated branching requires both targets to be explicit. If either of the
1382 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1383 Label fallthrough_target;
1384 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1385 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1386
Roland Levillain4fa13f62015-07-06 18:11:54 +01001387 LocationSummary* locations = condition->GetLocations();
1388 Location left = locations->InAt(0);
1389 Location right = locations->InAt(1);
1390
Roland Levillain4fa13f62015-07-06 18:11:54 +01001391 Primitive::Type type = condition->InputAt(0)->GetType();
1392 switch (type) {
1393 case Primitive::kPrimLong:
1394 GenerateLongComparesAndJumps(condition, true_target, false_target);
1395 break;
1396 case Primitive::kPrimFloat:
1397 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1398 GenerateFPJumps(condition, true_target, false_target);
1399 break;
1400 case Primitive::kPrimDouble:
1401 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1402 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1403 GenerateFPJumps(condition, true_target, false_target);
1404 break;
1405 default:
1406 LOG(FATAL) << "Unexpected compare type " << type;
1407 }
1408
David Brazdil0debae72015-11-12 18:37:00 +00001409 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001410 __ b(false_target);
1411 }
David Brazdil0debae72015-11-12 18:37:00 +00001412
1413 if (fallthrough_target.IsLinked()) {
1414 __ Bind(&fallthrough_target);
1415 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001416}
1417
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001418void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001419 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001420 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001421 Label* false_target) {
1422 HInstruction* cond = instruction->InputAt(condition_input_index);
1423
1424 if (true_target == nullptr && false_target == nullptr) {
1425 // Nothing to do. The code always falls through.
1426 return;
1427 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001428 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001429 if (cond->AsIntConstant()->IsOne()) {
1430 if (true_target != nullptr) {
1431 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001432 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001433 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001434 DCHECK(cond->AsIntConstant()->IsZero());
1435 if (false_target != nullptr) {
1436 __ b(false_target);
1437 }
1438 }
1439 return;
1440 }
1441
1442 // The following code generates these patterns:
1443 // (1) true_target == nullptr && false_target != nullptr
1444 // - opposite condition true => branch to false_target
1445 // (2) true_target != nullptr && false_target == nullptr
1446 // - condition true => branch to true_target
1447 // (3) true_target != nullptr && false_target != nullptr
1448 // - condition true => branch to true_target
1449 // - branch to false_target
1450 if (IsBooleanValueOrMaterializedCondition(cond)) {
1451 // Condition has been materialized, compare the output to 0.
1452 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1453 DCHECK(cond_val.IsRegister());
1454 if (true_target == nullptr) {
1455 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1456 } else {
1457 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001458 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001459 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001460 // Condition has not been materialized. Use its inputs as the comparison and
1461 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001462 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001463
1464 // If this is a long or FP comparison that has been folded into
1465 // the HCondition, generate the comparison directly.
1466 Primitive::Type type = condition->InputAt(0)->GetType();
1467 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1468 GenerateCompareTestAndBranch(condition, true_target, false_target);
1469 return;
1470 }
1471
1472 LocationSummary* locations = cond->GetLocations();
1473 DCHECK(locations->InAt(0).IsRegister());
1474 Register left = locations->InAt(0).AsRegister<Register>();
1475 Location right = locations->InAt(1);
1476 if (right.IsRegister()) {
1477 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001478 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001479 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001480 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001481 }
1482 if (true_target == nullptr) {
1483 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1484 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001485 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001486 }
Dave Allison20dfc792014-06-16 20:44:29 -07001487 }
David Brazdil0debae72015-11-12 18:37:00 +00001488
1489 // If neither branch falls through (case 3), the conditional branch to `true_target`
1490 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1491 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001492 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001493 }
1494}
1495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001496void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1498 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001499 locations->SetInAt(0, Location::RequiresRegister());
1500 }
1501}
1502
1503void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001504 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1505 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1506 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1507 nullptr : codegen_->GetLabelOf(true_successor);
1508 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1509 nullptr : codegen_->GetLabelOf(false_successor);
1510 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001511}
1512
1513void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1514 LocationSummary* locations = new (GetGraph()->GetArena())
1515 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001516 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001517 locations->SetInAt(0, Location::RequiresRegister());
1518 }
1519}
1520
1521void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001522 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001523 GenerateTestAndBranch(deoptimize,
1524 /* condition_input_index */ 0,
1525 slow_path->GetEntryLabel(),
1526 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001527}
Dave Allison20dfc792014-06-16 20:44:29 -07001528
David Brazdil74eb1b22015-12-14 11:44:01 +00001529void LocationsBuilderARM::VisitSelect(HSelect* select) {
1530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1531 if (Primitive::IsFloatingPointType(select->GetType())) {
1532 locations->SetInAt(0, Location::RequiresFpuRegister());
1533 locations->SetInAt(1, Location::RequiresFpuRegister());
1534 } else {
1535 locations->SetInAt(0, Location::RequiresRegister());
1536 locations->SetInAt(1, Location::RequiresRegister());
1537 }
1538 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1539 locations->SetInAt(2, Location::RequiresRegister());
1540 }
1541 locations->SetOut(Location::SameAsFirstInput());
1542}
1543
1544void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1545 LocationSummary* locations = select->GetLocations();
1546 Label false_target;
1547 GenerateTestAndBranch(select,
1548 /* condition_input_index */ 2,
1549 /* true_target */ nullptr,
1550 &false_target);
1551 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1552 __ Bind(&false_target);
1553}
1554
David Srbecky0cf44932015-12-09 14:09:59 +00001555void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1556 new (GetGraph()->GetArena()) LocationSummary(info);
1557}
1558
1559void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001560 if (codegen_->HasStackMapAtCurrentPc()) {
1561 // Ensure that we do not collide with the stack map of the previous instruction.
1562 __ nop();
1563 }
David Srbecky0cf44932015-12-09 14:09:59 +00001564 codegen_->RecordPcInfo(info, info->GetDexPc());
1565}
1566
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001567void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001568 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001569 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001570 // Handle the long/FP comparisons made in instruction simplification.
1571 switch (cond->InputAt(0)->GetType()) {
1572 case Primitive::kPrimLong:
1573 locations->SetInAt(0, Location::RequiresRegister());
1574 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001575 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001576 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1577 }
1578 break;
1579
1580 case Primitive::kPrimFloat:
1581 case Primitive::kPrimDouble:
1582 locations->SetInAt(0, Location::RequiresFpuRegister());
1583 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001584 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1586 }
1587 break;
1588
1589 default:
1590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001592 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001593 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1594 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001595 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001596}
1597
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001598void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001599 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001600 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001601 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001602
1603 LocationSummary* locations = cond->GetLocations();
1604 Location left = locations->InAt(0);
1605 Location right = locations->InAt(1);
1606 Register out = locations->Out().AsRegister<Register>();
1607 Label true_label, false_label;
1608
1609 switch (cond->InputAt(0)->GetType()) {
1610 default: {
1611 // Integer case.
1612 if (right.IsRegister()) {
1613 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1614 } else {
1615 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001616 __ CmpConstant(left.AsRegister<Register>(),
1617 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001618 }
Aart Bike9f37602015-10-09 11:15:55 -07001619 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001620 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001621 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001622 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001623 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001624 return;
1625 }
1626 case Primitive::kPrimLong:
1627 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1628 break;
1629 case Primitive::kPrimFloat:
1630 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1631 GenerateFPJumps(cond, &true_label, &false_label);
1632 break;
1633 case Primitive::kPrimDouble:
1634 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1635 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1636 GenerateFPJumps(cond, &true_label, &false_label);
1637 break;
1638 }
1639
1640 // Convert the jumps into the result.
1641 Label done_label;
1642
1643 // False case: result = 0.
1644 __ Bind(&false_label);
1645 __ LoadImmediate(out, 0);
1646 __ b(&done_label);
1647
1648 // True case: result = 1.
1649 __ Bind(&true_label);
1650 __ LoadImmediate(out, 1);
1651 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001652}
1653
1654void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001655 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001656}
1657
1658void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001659 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001660}
1661
1662void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001663 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001664}
1665
1666void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001667 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001668}
1669
1670void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001671 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001672}
1673
1674void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001675 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001676}
1677
1678void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001679 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001680}
1681
1682void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001683 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001684}
1685
1686void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001687 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001688}
1689
1690void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001691 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001692}
1693
1694void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001695 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001696}
1697
1698void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001699 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001700}
1701
Aart Bike9f37602015-10-09 11:15:55 -07001702void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001703 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001704}
1705
1706void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001707 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001708}
1709
1710void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001711 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001712}
1713
1714void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001715 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001716}
1717
1718void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001719 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001720}
1721
1722void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001723 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001724}
1725
1726void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001727 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001728}
1729
1730void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001731 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001732}
1733
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001734void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001735 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001736}
1737
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001738void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1739 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001740}
1741
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001742void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001743 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001744}
1745
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001746void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001747 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001748}
1749
1750void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001751 LocationSummary* locations =
1752 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001753 switch (store->InputAt(1)->GetType()) {
1754 case Primitive::kPrimBoolean:
1755 case Primitive::kPrimByte:
1756 case Primitive::kPrimChar:
1757 case Primitive::kPrimShort:
1758 case Primitive::kPrimInt:
1759 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001760 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001761 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1762 break;
1763
1764 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001765 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001766 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1767 break;
1768
1769 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001770 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001771 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001772}
1773
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001774void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001775}
1776
1777void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001778 LocationSummary* locations =
1779 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001780 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001781}
1782
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001783void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001784 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785}
1786
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001787void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1790 locations->SetOut(Location::ConstantLocation(constant));
1791}
1792
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001793void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001794 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001795}
1796
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001797void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001800 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001801}
1802
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001803void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001804 // Will be generated at use site.
1805}
1806
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001807void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1808 LocationSummary* locations =
1809 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1810 locations->SetOut(Location::ConstantLocation(constant));
1811}
1812
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001813void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001814 // Will be generated at use site.
1815}
1816
1817void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1820 locations->SetOut(Location::ConstantLocation(constant));
1821}
1822
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001823void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001824 // Will be generated at use site.
1825}
1826
Calin Juravle27df7582015-04-17 19:12:31 +01001827void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1828 memory_barrier->SetLocations(nullptr);
1829}
1830
1831void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001832 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001833}
1834
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001836 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837}
1838
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001839void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001840 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001841}
1842
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 LocationSummary* locations =
1845 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001846 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847}
1848
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001849void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001850 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001851}
1852
Calin Juravle175dc732015-08-25 15:42:32 +01001853void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1854 // The trampoline uses the same calling convention as dex calling conventions,
1855 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1856 // the method_idx.
1857 HandleInvoke(invoke);
1858}
1859
1860void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1861 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1862}
1863
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001864void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001865 // Explicit clinit checks triggered by static invokes must have been pruned by
1866 // art::PrepareForRegisterAllocation.
1867 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001868
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001869 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001870 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001871 codegen_->GetInstructionSetFeatures());
1872 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001873 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1874 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1875 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001876 return;
1877 }
1878
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001879 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001880
1881 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1882 if (invoke->HasPcRelativeDexCache()) {
1883 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1884 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001885}
1886
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001887static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1888 if (invoke->GetLocations()->Intrinsified()) {
1889 IntrinsicCodeGeneratorARM intrinsic(codegen);
1890 intrinsic.Dispatch(invoke);
1891 return true;
1892 }
1893 return false;
1894}
1895
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001896void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001897 // Explicit clinit checks triggered by static invokes must have been pruned by
1898 // art::PrepareForRegisterAllocation.
1899 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001900
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001901 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1902 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001903 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001904
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001905 LocationSummary* locations = invoke->GetLocations();
1906 codegen_->GenerateStaticOrDirectCall(
1907 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001908 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001909}
1910
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001911void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001912 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001913 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001914}
1915
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001916void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001917 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001918 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001919 codegen_->GetInstructionSetFeatures());
1920 if (intrinsic.TryDispatch(invoke)) {
1921 return;
1922 }
1923
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001924 HandleInvoke(invoke);
1925}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001926
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001927void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001928 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1929 return;
1930 }
1931
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001932 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001933 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001934 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001935}
1936
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001937void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1938 HandleInvoke(invoke);
1939 // Add the hidden argument.
1940 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1941}
1942
1943void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1944 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001945 LocationSummary* locations = invoke->GetLocations();
1946 Register temp = locations->GetTemp(0).AsRegister<Register>();
1947 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001948 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1949 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001950 Location receiver = locations->InAt(0);
1951 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1952
Roland Levillain3b359c72015-11-17 19:35:12 +00001953 // Set the hidden argument. This is safe to do this here, as R12
1954 // won't be modified thereafter, before the `blx` (call) instruction.
1955 DCHECK_EQ(R12, hidden_reg);
1956 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001957
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001958 if (receiver.IsStackSlot()) {
1959 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001960 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001961 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1962 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001963 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001964 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001965 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001966 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001967 // Instead of simply (possibly) unpoisoning `temp` here, we should
1968 // emit a read barrier for the previous class reference load.
1969 // However this is not required in practice, as this is an
1970 // intermediate/temporary reference and because the current
1971 // concurrent copying collector keeps the from-space memory
1972 // intact/accessible until the end of the marking phase (the
1973 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001974 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001975 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001976 uint32_t entry_point =
1977 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001978 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1979 // LR = temp->GetEntryPoint();
1980 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1981 // LR();
1982 __ blx(LR);
1983 DCHECK(!codegen_->IsLeafMethod());
1984 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1985}
1986
Roland Levillain88cb1752014-10-20 16:36:47 +01001987void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1988 LocationSummary* locations =
1989 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1990 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001991 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001992 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001993 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1994 break;
1995 }
1996 case Primitive::kPrimLong: {
1997 locations->SetInAt(0, Location::RequiresRegister());
1998 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001999 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002000 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002001
Roland Levillain88cb1752014-10-20 16:36:47 +01002002 case Primitive::kPrimFloat:
2003 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002004 locations->SetInAt(0, Location::RequiresFpuRegister());
2005 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002006 break;
2007
2008 default:
2009 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2010 }
2011}
2012
2013void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2014 LocationSummary* locations = neg->GetLocations();
2015 Location out = locations->Out();
2016 Location in = locations->InAt(0);
2017 switch (neg->GetResultType()) {
2018 case Primitive::kPrimInt:
2019 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002020 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002021 break;
2022
2023 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002024 DCHECK(in.IsRegisterPair());
2025 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2026 __ rsbs(out.AsRegisterPairLow<Register>(),
2027 in.AsRegisterPairLow<Register>(),
2028 ShifterOperand(0));
2029 // We cannot emit an RSC (Reverse Subtract with Carry)
2030 // instruction here, as it does not exist in the Thumb-2
2031 // instruction set. We use the following approach
2032 // using SBC and SUB instead.
2033 //
2034 // out.hi = -C
2035 __ sbc(out.AsRegisterPairHigh<Register>(),
2036 out.AsRegisterPairHigh<Register>(),
2037 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2038 // out.hi = out.hi - in.hi
2039 __ sub(out.AsRegisterPairHigh<Register>(),
2040 out.AsRegisterPairHigh<Register>(),
2041 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2042 break;
2043
Roland Levillain88cb1752014-10-20 16:36:47 +01002044 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002045 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002047 break;
2048
Roland Levillain88cb1752014-10-20 16:36:47 +01002049 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002050 DCHECK(in.IsFpuRegisterPair());
2051 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2052 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002053 break;
2054
2055 default:
2056 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2057 }
2058}
2059
Roland Levillaindff1f282014-11-05 14:15:05 +00002060void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002061 Primitive::Type result_type = conversion->GetResultType();
2062 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002063 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002064
Roland Levillain5b3ee562015-04-14 16:02:41 +01002065 // The float-to-long, double-to-long and long-to-float type conversions
2066 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002067 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002068 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2069 && result_type == Primitive::kPrimLong)
2070 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002071 ? LocationSummary::kCall
2072 : LocationSummary::kNoCall;
2073 LocationSummary* locations =
2074 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2075
David Brazdilb2bd1c52015-03-25 11:17:37 +00002076 // The Java language does not allow treating boolean as an integral type but
2077 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002078
Roland Levillaindff1f282014-11-05 14:15:05 +00002079 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002080 case Primitive::kPrimByte:
2081 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002082 case Primitive::kPrimBoolean:
2083 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002084 case Primitive::kPrimShort:
2085 case Primitive::kPrimInt:
2086 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002087 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002088 locations->SetInAt(0, Location::RequiresRegister());
2089 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2090 break;
2091
2092 default:
2093 LOG(FATAL) << "Unexpected type conversion from " << input_type
2094 << " to " << result_type;
2095 }
2096 break;
2097
Roland Levillain01a8d712014-11-14 16:27:39 +00002098 case Primitive::kPrimShort:
2099 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002100 case Primitive::kPrimBoolean:
2101 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002102 case Primitive::kPrimByte:
2103 case Primitive::kPrimInt:
2104 case Primitive::kPrimChar:
2105 // Processing a Dex `int-to-short' instruction.
2106 locations->SetInAt(0, Location::RequiresRegister());
2107 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2108 break;
2109
2110 default:
2111 LOG(FATAL) << "Unexpected type conversion from " << input_type
2112 << " to " << result_type;
2113 }
2114 break;
2115
Roland Levillain946e1432014-11-11 17:35:19 +00002116 case Primitive::kPrimInt:
2117 switch (input_type) {
2118 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002119 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002120 locations->SetInAt(0, Location::Any());
2121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2122 break;
2123
2124 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002125 // Processing a Dex `float-to-int' instruction.
2126 locations->SetInAt(0, Location::RequiresFpuRegister());
2127 locations->SetOut(Location::RequiresRegister());
2128 locations->AddTemp(Location::RequiresFpuRegister());
2129 break;
2130
Roland Levillain946e1432014-11-11 17:35:19 +00002131 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002132 // Processing a Dex `double-to-int' instruction.
2133 locations->SetInAt(0, Location::RequiresFpuRegister());
2134 locations->SetOut(Location::RequiresRegister());
2135 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002136 break;
2137
2138 default:
2139 LOG(FATAL) << "Unexpected type conversion from " << input_type
2140 << " to " << result_type;
2141 }
2142 break;
2143
Roland Levillaindff1f282014-11-05 14:15:05 +00002144 case Primitive::kPrimLong:
2145 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002146 case Primitive::kPrimBoolean:
2147 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002148 case Primitive::kPrimByte:
2149 case Primitive::kPrimShort:
2150 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002151 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002152 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002153 locations->SetInAt(0, Location::RequiresRegister());
2154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2155 break;
2156
Roland Levillain624279f2014-12-04 11:54:28 +00002157 case Primitive::kPrimFloat: {
2158 // Processing a Dex `float-to-long' instruction.
2159 InvokeRuntimeCallingConvention calling_convention;
2160 locations->SetInAt(0, Location::FpuRegisterLocation(
2161 calling_convention.GetFpuRegisterAt(0)));
2162 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2163 break;
2164 }
2165
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002166 case Primitive::kPrimDouble: {
2167 // Processing a Dex `double-to-long' instruction.
2168 InvokeRuntimeCallingConvention calling_convention;
2169 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2170 calling_convention.GetFpuRegisterAt(0),
2171 calling_convention.GetFpuRegisterAt(1)));
2172 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002173 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002174 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002175
2176 default:
2177 LOG(FATAL) << "Unexpected type conversion from " << input_type
2178 << " to " << result_type;
2179 }
2180 break;
2181
Roland Levillain981e4542014-11-14 11:47:14 +00002182 case Primitive::kPrimChar:
2183 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002184 case Primitive::kPrimBoolean:
2185 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002186 case Primitive::kPrimByte:
2187 case Primitive::kPrimShort:
2188 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002189 // Processing a Dex `int-to-char' instruction.
2190 locations->SetInAt(0, Location::RequiresRegister());
2191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2192 break;
2193
2194 default:
2195 LOG(FATAL) << "Unexpected type conversion from " << input_type
2196 << " to " << result_type;
2197 }
2198 break;
2199
Roland Levillaindff1f282014-11-05 14:15:05 +00002200 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002201 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002202 case Primitive::kPrimBoolean:
2203 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002204 case Primitive::kPrimByte:
2205 case Primitive::kPrimShort:
2206 case Primitive::kPrimInt:
2207 case Primitive::kPrimChar:
2208 // Processing a Dex `int-to-float' instruction.
2209 locations->SetInAt(0, Location::RequiresRegister());
2210 locations->SetOut(Location::RequiresFpuRegister());
2211 break;
2212
Roland Levillain5b3ee562015-04-14 16:02:41 +01002213 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002214 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002215 InvokeRuntimeCallingConvention calling_convention;
2216 locations->SetInAt(0, Location::RegisterPairLocation(
2217 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2218 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002219 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002220 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002221
Roland Levillaincff13742014-11-17 14:32:17 +00002222 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002223 // Processing a Dex `double-to-float' instruction.
2224 locations->SetInAt(0, Location::RequiresFpuRegister());
2225 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002226 break;
2227
2228 default:
2229 LOG(FATAL) << "Unexpected type conversion from " << input_type
2230 << " to " << result_type;
2231 };
2232 break;
2233
Roland Levillaindff1f282014-11-05 14:15:05 +00002234 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002235 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002236 case Primitive::kPrimBoolean:
2237 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002238 case Primitive::kPrimByte:
2239 case Primitive::kPrimShort:
2240 case Primitive::kPrimInt:
2241 case Primitive::kPrimChar:
2242 // Processing a Dex `int-to-double' instruction.
2243 locations->SetInAt(0, Location::RequiresRegister());
2244 locations->SetOut(Location::RequiresFpuRegister());
2245 break;
2246
2247 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002248 // Processing a Dex `long-to-double' instruction.
2249 locations->SetInAt(0, Location::RequiresRegister());
2250 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002251 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002252 locations->AddTemp(Location::RequiresFpuRegister());
2253 break;
2254
Roland Levillaincff13742014-11-17 14:32:17 +00002255 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002256 // Processing a Dex `float-to-double' instruction.
2257 locations->SetInAt(0, Location::RequiresFpuRegister());
2258 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002259 break;
2260
2261 default:
2262 LOG(FATAL) << "Unexpected type conversion from " << input_type
2263 << " to " << result_type;
2264 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002265 break;
2266
2267 default:
2268 LOG(FATAL) << "Unexpected type conversion from " << input_type
2269 << " to " << result_type;
2270 }
2271}
2272
2273void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2274 LocationSummary* locations = conversion->GetLocations();
2275 Location out = locations->Out();
2276 Location in = locations->InAt(0);
2277 Primitive::Type result_type = conversion->GetResultType();
2278 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002279 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002280 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002281 case Primitive::kPrimByte:
2282 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002283 case Primitive::kPrimBoolean:
2284 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002285 case Primitive::kPrimShort:
2286 case Primitive::kPrimInt:
2287 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002288 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002289 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002290 break;
2291
2292 default:
2293 LOG(FATAL) << "Unexpected type conversion from " << input_type
2294 << " to " << result_type;
2295 }
2296 break;
2297
Roland Levillain01a8d712014-11-14 16:27:39 +00002298 case Primitive::kPrimShort:
2299 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002300 case Primitive::kPrimBoolean:
2301 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002302 case Primitive::kPrimByte:
2303 case Primitive::kPrimInt:
2304 case Primitive::kPrimChar:
2305 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002307 break;
2308
2309 default:
2310 LOG(FATAL) << "Unexpected type conversion from " << input_type
2311 << " to " << result_type;
2312 }
2313 break;
2314
Roland Levillain946e1432014-11-11 17:35:19 +00002315 case Primitive::kPrimInt:
2316 switch (input_type) {
2317 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002318 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002319 DCHECK(out.IsRegister());
2320 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002321 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002322 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002323 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002324 } else {
2325 DCHECK(in.IsConstant());
2326 DCHECK(in.GetConstant()->IsLongConstant());
2327 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002328 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002329 }
2330 break;
2331
Roland Levillain3f8f9362014-12-02 17:45:01 +00002332 case Primitive::kPrimFloat: {
2333 // Processing a Dex `float-to-int' instruction.
2334 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2335 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2336 __ vcvtis(temp, temp);
2337 __ vmovrs(out.AsRegister<Register>(), temp);
2338 break;
2339 }
2340
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002341 case Primitive::kPrimDouble: {
2342 // Processing a Dex `double-to-int' instruction.
2343 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2344 DRegister temp_d = FromLowSToD(temp_s);
2345 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2346 __ vcvtid(temp_s, temp_d);
2347 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002348 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002349 }
Roland Levillain946e1432014-11-11 17:35:19 +00002350
2351 default:
2352 LOG(FATAL) << "Unexpected type conversion from " << input_type
2353 << " to " << result_type;
2354 }
2355 break;
2356
Roland Levillaindff1f282014-11-05 14:15:05 +00002357 case Primitive::kPrimLong:
2358 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002359 case Primitive::kPrimBoolean:
2360 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002361 case Primitive::kPrimByte:
2362 case Primitive::kPrimShort:
2363 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002364 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002365 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002366 DCHECK(out.IsRegisterPair());
2367 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002369 // Sign extension.
2370 __ Asr(out.AsRegisterPairHigh<Register>(),
2371 out.AsRegisterPairLow<Register>(),
2372 31);
2373 break;
2374
2375 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002376 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002377 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2378 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002379 conversion->GetDexPc(),
2380 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002381 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002382 break;
2383
Roland Levillaindff1f282014-11-05 14:15:05 +00002384 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002385 // Processing a Dex `double-to-long' instruction.
2386 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2387 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002388 conversion->GetDexPc(),
2389 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002390 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002391 break;
2392
2393 default:
2394 LOG(FATAL) << "Unexpected type conversion from " << input_type
2395 << " to " << result_type;
2396 }
2397 break;
2398
Roland Levillain981e4542014-11-14 11:47:14 +00002399 case Primitive::kPrimChar:
2400 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002401 case Primitive::kPrimBoolean:
2402 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002403 case Primitive::kPrimByte:
2404 case Primitive::kPrimShort:
2405 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002406 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002407 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002408 break;
2409
2410 default:
2411 LOG(FATAL) << "Unexpected type conversion from " << input_type
2412 << " to " << result_type;
2413 }
2414 break;
2415
Roland Levillaindff1f282014-11-05 14:15:05 +00002416 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002417 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002418 case Primitive::kPrimBoolean:
2419 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002420 case Primitive::kPrimByte:
2421 case Primitive::kPrimShort:
2422 case Primitive::kPrimInt:
2423 case Primitive::kPrimChar: {
2424 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002425 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2426 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002427 break;
2428 }
2429
Roland Levillain5b3ee562015-04-14 16:02:41 +01002430 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002431 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002432 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2433 conversion,
2434 conversion->GetDexPc(),
2435 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002436 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002437 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002438
Roland Levillaincff13742014-11-17 14:32:17 +00002439 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002440 // Processing a Dex `double-to-float' instruction.
2441 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2442 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002443 break;
2444
2445 default:
2446 LOG(FATAL) << "Unexpected type conversion from " << input_type
2447 << " to " << result_type;
2448 };
2449 break;
2450
Roland Levillaindff1f282014-11-05 14:15:05 +00002451 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002452 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002453 case Primitive::kPrimBoolean:
2454 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002455 case Primitive::kPrimByte:
2456 case Primitive::kPrimShort:
2457 case Primitive::kPrimInt:
2458 case Primitive::kPrimChar: {
2459 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002460 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002461 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2462 out.AsFpuRegisterPairLow<SRegister>());
2463 break;
2464 }
2465
Roland Levillain647b9ed2014-11-27 12:06:00 +00002466 case Primitive::kPrimLong: {
2467 // Processing a Dex `long-to-double' instruction.
2468 Register low = in.AsRegisterPairLow<Register>();
2469 Register high = in.AsRegisterPairHigh<Register>();
2470 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2471 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002472 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002473 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002474 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2475 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002476
Roland Levillain682393c2015-04-14 15:57:52 +01002477 // temp_d = int-to-double(high)
2478 __ vmovsr(temp_s, high);
2479 __ vcvtdi(temp_d, temp_s);
2480 // constant_d = k2Pow32EncodingForDouble
2481 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2482 // out_d = unsigned-to-double(low)
2483 __ vmovsr(out_s, low);
2484 __ vcvtdu(out_d, out_s);
2485 // out_d += temp_d * constant_d
2486 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002487 break;
2488 }
2489
Roland Levillaincff13742014-11-17 14:32:17 +00002490 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002491 // Processing a Dex `float-to-double' instruction.
2492 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2493 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002494 break;
2495
2496 default:
2497 LOG(FATAL) << "Unexpected type conversion from " << input_type
2498 << " to " << result_type;
2499 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002500 break;
2501
2502 default:
2503 LOG(FATAL) << "Unexpected type conversion from " << input_type
2504 << " to " << result_type;
2505 }
2506}
2507
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002508void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002509 LocationSummary* locations =
2510 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002511 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002512 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002513 locations->SetInAt(0, Location::RequiresRegister());
2514 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002515 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2516 break;
2517 }
2518
2519 case Primitive::kPrimLong: {
2520 locations->SetInAt(0, Location::RequiresRegister());
2521 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002523 break;
2524 }
2525
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002526 case Primitive::kPrimFloat:
2527 case Primitive::kPrimDouble: {
2528 locations->SetInAt(0, Location::RequiresFpuRegister());
2529 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002530 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002531 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002532 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002533
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002534 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002535 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002536 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002537}
2538
2539void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2540 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002541 Location out = locations->Out();
2542 Location first = locations->InAt(0);
2543 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002544 switch (add->GetResultType()) {
2545 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002546 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002547 __ add(out.AsRegister<Register>(),
2548 first.AsRegister<Register>(),
2549 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002550 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002551 __ AddConstant(out.AsRegister<Register>(),
2552 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002553 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002554 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002555 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002556
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002557 case Primitive::kPrimLong: {
2558 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002559 __ adds(out.AsRegisterPairLow<Register>(),
2560 first.AsRegisterPairLow<Register>(),
2561 ShifterOperand(second.AsRegisterPairLow<Register>()));
2562 __ adc(out.AsRegisterPairHigh<Register>(),
2563 first.AsRegisterPairHigh<Register>(),
2564 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002565 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002566 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002567
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002568 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002569 __ vadds(out.AsFpuRegister<SRegister>(),
2570 first.AsFpuRegister<SRegister>(),
2571 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002572 break;
2573
2574 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002575 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2576 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2577 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002578 break;
2579
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002580 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002581 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002582 }
2583}
2584
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002585void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002586 LocationSummary* locations =
2587 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002588 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002589 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002590 locations->SetInAt(0, Location::RequiresRegister());
2591 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2593 break;
2594 }
2595
2596 case Primitive::kPrimLong: {
2597 locations->SetInAt(0, Location::RequiresRegister());
2598 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002599 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002600 break;
2601 }
Calin Juravle11351682014-10-23 15:38:15 +01002602 case Primitive::kPrimFloat:
2603 case Primitive::kPrimDouble: {
2604 locations->SetInAt(0, Location::RequiresFpuRegister());
2605 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002606 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002607 break;
Calin Juravle11351682014-10-23 15:38:15 +01002608 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002609 default:
Calin Juravle11351682014-10-23 15:38:15 +01002610 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002611 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002612}
2613
2614void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2615 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002616 Location out = locations->Out();
2617 Location first = locations->InAt(0);
2618 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002619 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002620 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002621 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002622 __ sub(out.AsRegister<Register>(),
2623 first.AsRegister<Register>(),
2624 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002625 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002626 __ AddConstant(out.AsRegister<Register>(),
2627 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002628 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002629 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002630 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002631 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002632
Calin Juravle11351682014-10-23 15:38:15 +01002633 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002634 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002635 __ subs(out.AsRegisterPairLow<Register>(),
2636 first.AsRegisterPairLow<Register>(),
2637 ShifterOperand(second.AsRegisterPairLow<Register>()));
2638 __ sbc(out.AsRegisterPairHigh<Register>(),
2639 first.AsRegisterPairHigh<Register>(),
2640 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002641 break;
Calin Juravle11351682014-10-23 15:38:15 +01002642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002643
Calin Juravle11351682014-10-23 15:38:15 +01002644 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002645 __ vsubs(out.AsFpuRegister<SRegister>(),
2646 first.AsFpuRegister<SRegister>(),
2647 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002648 break;
Calin Juravle11351682014-10-23 15:38:15 +01002649 }
2650
2651 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002652 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2653 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2654 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002655 break;
2656 }
2657
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002658
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002659 default:
Calin Juravle11351682014-10-23 15:38:15 +01002660 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002661 }
2662}
2663
Calin Juravle34bacdf2014-10-07 20:23:36 +01002664void LocationsBuilderARM::VisitMul(HMul* mul) {
2665 LocationSummary* locations =
2666 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2667 switch (mul->GetResultType()) {
2668 case Primitive::kPrimInt:
2669 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002670 locations->SetInAt(0, Location::RequiresRegister());
2671 locations->SetInAt(1, Location::RequiresRegister());
2672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002673 break;
2674 }
2675
Calin Juravleb5bfa962014-10-21 18:02:24 +01002676 case Primitive::kPrimFloat:
2677 case Primitive::kPrimDouble: {
2678 locations->SetInAt(0, Location::RequiresFpuRegister());
2679 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002680 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002681 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002682 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002683
2684 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002685 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002686 }
2687}
2688
2689void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2690 LocationSummary* locations = mul->GetLocations();
2691 Location out = locations->Out();
2692 Location first = locations->InAt(0);
2693 Location second = locations->InAt(1);
2694 switch (mul->GetResultType()) {
2695 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002696 __ mul(out.AsRegister<Register>(),
2697 first.AsRegister<Register>(),
2698 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002699 break;
2700 }
2701 case Primitive::kPrimLong: {
2702 Register out_hi = out.AsRegisterPairHigh<Register>();
2703 Register out_lo = out.AsRegisterPairLow<Register>();
2704 Register in1_hi = first.AsRegisterPairHigh<Register>();
2705 Register in1_lo = first.AsRegisterPairLow<Register>();
2706 Register in2_hi = second.AsRegisterPairHigh<Register>();
2707 Register in2_lo = second.AsRegisterPairLow<Register>();
2708
2709 // Extra checks to protect caused by the existence of R1_R2.
2710 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2711 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2712 DCHECK_NE(out_hi, in1_lo);
2713 DCHECK_NE(out_hi, in2_lo);
2714
2715 // input: in1 - 64 bits, in2 - 64 bits
2716 // output: out
2717 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2718 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2719 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2720
2721 // IP <- in1.lo * in2.hi
2722 __ mul(IP, in1_lo, in2_hi);
2723 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2724 __ mla(out_hi, in1_hi, in2_lo, IP);
2725 // out.lo <- (in1.lo * in2.lo)[31:0];
2726 __ umull(out_lo, IP, in1_lo, in2_lo);
2727 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2728 __ add(out_hi, out_hi, ShifterOperand(IP));
2729 break;
2730 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002731
2732 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002733 __ vmuls(out.AsFpuRegister<SRegister>(),
2734 first.AsFpuRegister<SRegister>(),
2735 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002736 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002737 }
2738
2739 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002740 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2741 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2742 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002743 break;
2744 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002745
2746 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002747 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002748 }
2749}
2750
Zheng Xuc6667102015-05-15 16:08:45 +08002751void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2752 DCHECK(instruction->IsDiv() || instruction->IsRem());
2753 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2754
2755 LocationSummary* locations = instruction->GetLocations();
2756 Location second = locations->InAt(1);
2757 DCHECK(second.IsConstant());
2758
2759 Register out = locations->Out().AsRegister<Register>();
2760 Register dividend = locations->InAt(0).AsRegister<Register>();
2761 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2762 DCHECK(imm == 1 || imm == -1);
2763
2764 if (instruction->IsRem()) {
2765 __ LoadImmediate(out, 0);
2766 } else {
2767 if (imm == 1) {
2768 __ Mov(out, dividend);
2769 } else {
2770 __ rsb(out, dividend, ShifterOperand(0));
2771 }
2772 }
2773}
2774
2775void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2776 DCHECK(instruction->IsDiv() || instruction->IsRem());
2777 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2778
2779 LocationSummary* locations = instruction->GetLocations();
2780 Location second = locations->InAt(1);
2781 DCHECK(second.IsConstant());
2782
2783 Register out = locations->Out().AsRegister<Register>();
2784 Register dividend = locations->InAt(0).AsRegister<Register>();
2785 Register temp = locations->GetTemp(0).AsRegister<Register>();
2786 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002787 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002788 int ctz_imm = CTZ(abs_imm);
2789
2790 if (ctz_imm == 1) {
2791 __ Lsr(temp, dividend, 32 - ctz_imm);
2792 } else {
2793 __ Asr(temp, dividend, 31);
2794 __ Lsr(temp, temp, 32 - ctz_imm);
2795 }
2796 __ add(out, temp, ShifterOperand(dividend));
2797
2798 if (instruction->IsDiv()) {
2799 __ Asr(out, out, ctz_imm);
2800 if (imm < 0) {
2801 __ rsb(out, out, ShifterOperand(0));
2802 }
2803 } else {
2804 __ ubfx(out, out, 0, ctz_imm);
2805 __ sub(out, out, ShifterOperand(temp));
2806 }
2807}
2808
2809void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2810 DCHECK(instruction->IsDiv() || instruction->IsRem());
2811 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2812
2813 LocationSummary* locations = instruction->GetLocations();
2814 Location second = locations->InAt(1);
2815 DCHECK(second.IsConstant());
2816
2817 Register out = locations->Out().AsRegister<Register>();
2818 Register dividend = locations->InAt(0).AsRegister<Register>();
2819 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2820 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2821 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2822
2823 int64_t magic;
2824 int shift;
2825 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2826
2827 __ LoadImmediate(temp1, magic);
2828 __ smull(temp2, temp1, dividend, temp1);
2829
2830 if (imm > 0 && magic < 0) {
2831 __ add(temp1, temp1, ShifterOperand(dividend));
2832 } else if (imm < 0 && magic > 0) {
2833 __ sub(temp1, temp1, ShifterOperand(dividend));
2834 }
2835
2836 if (shift != 0) {
2837 __ Asr(temp1, temp1, shift);
2838 }
2839
2840 if (instruction->IsDiv()) {
2841 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2842 } else {
2843 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2844 // TODO: Strength reduction for mls.
2845 __ LoadImmediate(temp2, imm);
2846 __ mls(out, temp1, temp2, dividend);
2847 }
2848}
2849
2850void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2851 DCHECK(instruction->IsDiv() || instruction->IsRem());
2852 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2853
2854 LocationSummary* locations = instruction->GetLocations();
2855 Location second = locations->InAt(1);
2856 DCHECK(second.IsConstant());
2857
2858 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2859 if (imm == 0) {
2860 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2861 } else if (imm == 1 || imm == -1) {
2862 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002863 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002864 DivRemByPowerOfTwo(instruction);
2865 } else {
2866 DCHECK(imm <= -2 || imm >= 2);
2867 GenerateDivRemWithAnyConstant(instruction);
2868 }
2869}
2870
Calin Juravle7c4954d2014-10-28 16:57:40 +00002871void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002872 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2873 if (div->GetResultType() == Primitive::kPrimLong) {
2874 // pLdiv runtime call.
2875 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002876 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2877 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002878 } else if (div->GetResultType() == Primitive::kPrimInt &&
2879 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2880 // pIdivmod runtime call.
2881 call_kind = LocationSummary::kCall;
2882 }
2883
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2885
Calin Juravle7c4954d2014-10-28 16:57:40 +00002886 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002887 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002888 if (div->InputAt(1)->IsConstant()) {
2889 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002890 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002891 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002892 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2893 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002894 // No temp register required.
2895 } else {
2896 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002897 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002898 locations->AddTemp(Location::RequiresRegister());
2899 }
2900 }
2901 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002902 locations->SetInAt(0, Location::RequiresRegister());
2903 locations->SetInAt(1, Location::RequiresRegister());
2904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2905 } else {
2906 InvokeRuntimeCallingConvention calling_convention;
2907 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2908 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2909 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2910 // we only need the former.
2911 locations->SetOut(Location::RegisterLocation(R0));
2912 }
Calin Juravled0d48522014-11-04 16:40:20 +00002913 break;
2914 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002915 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002916 InvokeRuntimeCallingConvention calling_convention;
2917 locations->SetInAt(0, Location::RegisterPairLocation(
2918 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2919 locations->SetInAt(1, Location::RegisterPairLocation(
2920 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002921 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002922 break;
2923 }
2924 case Primitive::kPrimFloat:
2925 case Primitive::kPrimDouble: {
2926 locations->SetInAt(0, Location::RequiresFpuRegister());
2927 locations->SetInAt(1, Location::RequiresFpuRegister());
2928 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2929 break;
2930 }
2931
2932 default:
2933 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2934 }
2935}
2936
2937void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2938 LocationSummary* locations = div->GetLocations();
2939 Location out = locations->Out();
2940 Location first = locations->InAt(0);
2941 Location second = locations->InAt(1);
2942
2943 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002944 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002945 if (second.IsConstant()) {
2946 GenerateDivRemConstantIntegral(div);
2947 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002948 __ sdiv(out.AsRegister<Register>(),
2949 first.AsRegister<Register>(),
2950 second.AsRegister<Register>());
2951 } else {
2952 InvokeRuntimeCallingConvention calling_convention;
2953 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2954 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2955 DCHECK_EQ(R0, out.AsRegister<Register>());
2956
2957 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002958 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002959 }
Calin Juravled0d48522014-11-04 16:40:20 +00002960 break;
2961 }
2962
Calin Juravle7c4954d2014-10-28 16:57:40 +00002963 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002964 InvokeRuntimeCallingConvention calling_convention;
2965 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2966 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2967 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2968 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2969 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002970 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002971
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002972 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002973 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002974 break;
2975 }
2976
2977 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002978 __ vdivs(out.AsFpuRegister<SRegister>(),
2979 first.AsFpuRegister<SRegister>(),
2980 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002981 break;
2982 }
2983
2984 case Primitive::kPrimDouble: {
2985 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2986 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2987 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
2988 break;
2989 }
2990
2991 default:
2992 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2993 }
2994}
2995
Calin Juravlebacfec32014-11-14 15:54:36 +00002996void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002997 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002998
2999 // Most remainders are implemented in the runtime.
3000 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003001 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3002 // sdiv will be replaced by other instruction sequence.
3003 call_kind = LocationSummary::kNoCall;
3004 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3005 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003006 // Have hardware divide instruction for int, do it with three instructions.
3007 call_kind = LocationSummary::kNoCall;
3008 }
3009
Calin Juravlebacfec32014-11-14 15:54:36 +00003010 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3011
Calin Juravled2ec87d2014-12-08 14:24:46 +00003012 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003013 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003014 if (rem->InputAt(1)->IsConstant()) {
3015 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003016 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003017 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003018 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3019 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003020 // No temp register required.
3021 } else {
3022 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003023 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003024 locations->AddTemp(Location::RequiresRegister());
3025 }
3026 }
3027 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003028 locations->SetInAt(0, Location::RequiresRegister());
3029 locations->SetInAt(1, Location::RequiresRegister());
3030 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3031 locations->AddTemp(Location::RequiresRegister());
3032 } else {
3033 InvokeRuntimeCallingConvention calling_convention;
3034 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3035 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3036 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3037 // we only need the latter.
3038 locations->SetOut(Location::RegisterLocation(R1));
3039 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003040 break;
3041 }
3042 case Primitive::kPrimLong: {
3043 InvokeRuntimeCallingConvention calling_convention;
3044 locations->SetInAt(0, Location::RegisterPairLocation(
3045 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3046 locations->SetInAt(1, Location::RegisterPairLocation(
3047 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3048 // The runtime helper puts the output in R2,R3.
3049 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3050 break;
3051 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003052 case Primitive::kPrimFloat: {
3053 InvokeRuntimeCallingConvention calling_convention;
3054 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3055 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3056 locations->SetOut(Location::FpuRegisterLocation(S0));
3057 break;
3058 }
3059
Calin Juravlebacfec32014-11-14 15:54:36 +00003060 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003061 InvokeRuntimeCallingConvention calling_convention;
3062 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3063 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3064 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3065 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3066 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003067 break;
3068 }
3069
3070 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003071 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003072 }
3073}
3074
3075void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3076 LocationSummary* locations = rem->GetLocations();
3077 Location out = locations->Out();
3078 Location first = locations->InAt(0);
3079 Location second = locations->InAt(1);
3080
Calin Juravled2ec87d2014-12-08 14:24:46 +00003081 Primitive::Type type = rem->GetResultType();
3082 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003083 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003084 if (second.IsConstant()) {
3085 GenerateDivRemConstantIntegral(rem);
3086 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003087 Register reg1 = first.AsRegister<Register>();
3088 Register reg2 = second.AsRegister<Register>();
3089 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003090
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003091 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003092 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003093 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003094 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003095 } else {
3096 InvokeRuntimeCallingConvention calling_convention;
3097 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3098 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3099 DCHECK_EQ(R1, out.AsRegister<Register>());
3100
3101 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003102 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003103 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003104 break;
3105 }
3106
3107 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003108 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003109 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003110 break;
3111 }
3112
Calin Juravled2ec87d2014-12-08 14:24:46 +00003113 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003114 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003115 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003116 break;
3117 }
3118
Calin Juravlebacfec32014-11-14 15:54:36 +00003119 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003120 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003121 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003122 break;
3123 }
3124
3125 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003126 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003127 }
3128}
3129
Calin Juravled0d48522014-11-04 16:40:20 +00003130void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003131 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3132 ? LocationSummary::kCallOnSlowPath
3133 : LocationSummary::kNoCall;
3134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003135 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003136 if (instruction->HasUses()) {
3137 locations->SetOut(Location::SameAsFirstInput());
3138 }
3139}
3140
3141void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003142 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003143 codegen_->AddSlowPath(slow_path);
3144
3145 LocationSummary* locations = instruction->GetLocations();
3146 Location value = locations->InAt(0);
3147
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003148 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003149 case Primitive::kPrimByte:
3150 case Primitive::kPrimChar:
3151 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003152 case Primitive::kPrimInt: {
3153 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003154 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003155 } else {
3156 DCHECK(value.IsConstant()) << value;
3157 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3158 __ b(slow_path->GetEntryLabel());
3159 }
3160 }
3161 break;
3162 }
3163 case Primitive::kPrimLong: {
3164 if (value.IsRegisterPair()) {
3165 __ orrs(IP,
3166 value.AsRegisterPairLow<Register>(),
3167 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3168 __ b(slow_path->GetEntryLabel(), EQ);
3169 } else {
3170 DCHECK(value.IsConstant()) << value;
3171 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3172 __ b(slow_path->GetEntryLabel());
3173 }
3174 }
3175 break;
3176 default:
3177 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3178 }
3179 }
Calin Juravled0d48522014-11-04 16:40:20 +00003180}
3181
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003182void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3183 Register in = locations->InAt(0).AsRegister<Register>();
3184 Location rhs = locations->InAt(1);
3185 Register out = locations->Out().AsRegister<Register>();
3186
3187 if (rhs.IsConstant()) {
3188 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3189 // so map all rotations to a +ve. equivalent in that range.
3190 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3191 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3192 if (rot) {
3193 // Rotate, mapping left rotations to right equivalents if necessary.
3194 // (e.g. left by 2 bits == right by 30.)
3195 __ Ror(out, in, rot);
3196 } else if (out != in) {
3197 __ Mov(out, in);
3198 }
3199 } else {
3200 __ Ror(out, in, rhs.AsRegister<Register>());
3201 }
3202}
3203
3204// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3205// rotates by swapping input regs (effectively rotating by the first 32-bits of
3206// a larger rotation) or flipping direction (thus treating larger right/left
3207// rotations as sub-word sized rotations in the other direction) as appropriate.
3208void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3209 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3210 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3211 Location rhs = locations->InAt(1);
3212 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3213 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3214
3215 if (rhs.IsConstant()) {
3216 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3217 // Map all rotations to +ve. equivalents on the interval [0,63].
3218 rot &= kMaxLongShiftValue;
3219 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3220 // logic below to a simple pair of binary orr.
3221 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3222 if (rot >= kArmBitsPerWord) {
3223 rot -= kArmBitsPerWord;
3224 std::swap(in_reg_hi, in_reg_lo);
3225 }
3226 // Rotate, or mov to out for zero or word size rotations.
3227 if (rot != 0u) {
3228 __ Lsr(out_reg_hi, in_reg_hi, rot);
3229 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3230 __ Lsr(out_reg_lo, in_reg_lo, rot);
3231 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3232 } else {
3233 __ Mov(out_reg_lo, in_reg_lo);
3234 __ Mov(out_reg_hi, in_reg_hi);
3235 }
3236 } else {
3237 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3238 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3239 Label end;
3240 Label shift_by_32_plus_shift_right;
3241
3242 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3243 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3244 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3245 __ b(&shift_by_32_plus_shift_right, CC);
3246
3247 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3248 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3249 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3250 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3251 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3252 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3253 __ Lsr(shift_left, in_reg_hi, shift_right);
3254 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3255 __ b(&end);
3256
3257 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3258 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3259 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3260 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3261 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3262 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3263 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3264 __ Lsl(shift_right, in_reg_hi, shift_left);
3265 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3266
3267 __ Bind(&end);
3268 }
3269}
3270void LocationsBuilderARM::HandleRotate(HRor* ror) {
3271 LocationSummary* locations =
3272 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3273 switch (ror->GetResultType()) {
3274 case Primitive::kPrimInt: {
3275 locations->SetInAt(0, Location::RequiresRegister());
3276 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3278 break;
3279 }
3280 case Primitive::kPrimLong: {
3281 locations->SetInAt(0, Location::RequiresRegister());
3282 if (ror->InputAt(1)->IsConstant()) {
3283 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3284 } else {
3285 locations->SetInAt(1, Location::RequiresRegister());
3286 locations->AddTemp(Location::RequiresRegister());
3287 locations->AddTemp(Location::RequiresRegister());
3288 }
3289 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3290 break;
3291 }
3292 default:
3293 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3294 }
3295}
3296
3297void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3298 LocationSummary* locations = ror->GetLocations();
3299 Primitive::Type type = ror->GetResultType();
3300 switch (type) {
3301 case Primitive::kPrimInt: {
3302 HandleIntegerRotate(locations);
3303 break;
3304 }
3305 case Primitive::kPrimLong: {
3306 HandleLongRotate(locations);
3307 break;
3308 }
3309 default:
3310 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003311 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003312 }
3313}
3314
3315void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003316 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003317}
3318
3319void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003320 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003321}
3322
Calin Juravle9aec02f2014-11-18 23:06:35 +00003323void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3324 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3325
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003326 LocationSummary* locations =
3327 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003328
3329 switch (op->GetResultType()) {
3330 case Primitive::kPrimInt: {
3331 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003332 if (op->InputAt(1)->IsConstant()) {
3333 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3335 } else {
3336 locations->SetInAt(1, Location::RequiresRegister());
3337 // Make the output overlap, as it will be used to hold the masked
3338 // second input.
3339 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3340 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003341 break;
3342 }
3343 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003344 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003345 if (op->InputAt(1)->IsConstant()) {
3346 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3347 // For simplicity, use kOutputOverlap even though we only require that low registers
3348 // don't clash with high registers which the register allocator currently guarantees.
3349 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3350 } else {
3351 locations->SetInAt(1, Location::RequiresRegister());
3352 locations->AddTemp(Location::RequiresRegister());
3353 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3354 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003355 break;
3356 }
3357 default:
3358 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3359 }
3360}
3361
3362void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3363 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3364
3365 LocationSummary* locations = op->GetLocations();
3366 Location out = locations->Out();
3367 Location first = locations->InAt(0);
3368 Location second = locations->InAt(1);
3369
3370 Primitive::Type type = op->GetResultType();
3371 switch (type) {
3372 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003373 Register out_reg = out.AsRegister<Register>();
3374 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003375 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003376 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003377 // ARM doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003378 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003379 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003380 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003381 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003382 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003383 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003384 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003385 }
3386 } else {
3387 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3388 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
Roland Levillainc9285912015-12-18 10:38:42 +00003389 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003390 __ Mov(out_reg, first_reg);
3391 } else if (op->IsShl()) {
3392 __ Lsl(out_reg, first_reg, shift_value);
3393 } else if (op->IsShr()) {
3394 __ Asr(out_reg, first_reg, shift_value);
3395 } else {
3396 __ Lsr(out_reg, first_reg, shift_value);
3397 }
3398 }
3399 break;
3400 }
3401 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003402 Register o_h = out.AsRegisterPairHigh<Register>();
3403 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003404
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003405 Register high = first.AsRegisterPairHigh<Register>();
3406 Register low = first.AsRegisterPairLow<Register>();
3407
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003408 if (second.IsRegister()) {
3409 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003410
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003411 Register second_reg = second.AsRegister<Register>();
3412
3413 if (op->IsShl()) {
3414 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3415 // Shift the high part
3416 __ Lsl(o_h, high, o_l);
3417 // Shift the low part and `or` what overflew on the high part
3418 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3419 __ Lsr(temp, low, temp);
3420 __ orr(o_h, o_h, ShifterOperand(temp));
3421 // If the shift is > 32 bits, override the high part
3422 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3423 __ it(PL);
3424 __ Lsl(o_h, low, temp, PL);
3425 // Shift the low part
3426 __ Lsl(o_l, low, o_l);
3427 } else if (op->IsShr()) {
3428 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3429 // Shift the low part
3430 __ Lsr(o_l, low, o_h);
3431 // Shift the high part and `or` what underflew on the low part
3432 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3433 __ Lsl(temp, high, temp);
3434 __ orr(o_l, o_l, ShifterOperand(temp));
3435 // If the shift is > 32 bits, override the low part
3436 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3437 __ it(PL);
3438 __ Asr(o_l, high, temp, PL);
3439 // Shift the high part
3440 __ Asr(o_h, high, o_h);
3441 } else {
3442 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3443 // same as Shr except we use `Lsr`s and not `Asr`s
3444 __ Lsr(o_l, low, o_h);
3445 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3446 __ Lsl(temp, high, temp);
3447 __ orr(o_l, o_l, ShifterOperand(temp));
3448 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3449 __ it(PL);
3450 __ Lsr(o_l, high, temp, PL);
3451 __ Lsr(o_h, high, o_h);
3452 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003453 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003454 // Register allocator doesn't create partial overlap.
3455 DCHECK_NE(o_l, high);
3456 DCHECK_NE(o_h, low);
3457 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3458 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3459 if (shift_value > 32) {
3460 if (op->IsShl()) {
3461 __ Lsl(o_h, low, shift_value - 32);
3462 __ LoadImmediate(o_l, 0);
3463 } else if (op->IsShr()) {
3464 __ Asr(o_l, high, shift_value - 32);
3465 __ Asr(o_h, high, 31);
3466 } else {
3467 __ Lsr(o_l, high, shift_value - 32);
3468 __ LoadImmediate(o_h, 0);
3469 }
3470 } else if (shift_value == 32) {
3471 if (op->IsShl()) {
3472 __ mov(o_h, ShifterOperand(low));
3473 __ LoadImmediate(o_l, 0);
3474 } else if (op->IsShr()) {
3475 __ mov(o_l, ShifterOperand(high));
3476 __ Asr(o_h, high, 31);
3477 } else {
3478 __ mov(o_l, ShifterOperand(high));
3479 __ LoadImmediate(o_h, 0);
3480 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003481 } else if (shift_value == 1) {
3482 if (op->IsShl()) {
3483 __ Lsls(o_l, low, 1);
3484 __ adc(o_h, high, ShifterOperand(high));
3485 } else if (op->IsShr()) {
3486 __ Asrs(o_h, high, 1);
3487 __ Rrx(o_l, low);
3488 } else {
3489 __ Lsrs(o_h, high, 1);
3490 __ Rrx(o_l, low);
3491 }
3492 } else {
3493 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003494 if (op->IsShl()) {
3495 __ Lsl(o_h, high, shift_value);
3496 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3497 __ Lsl(o_l, low, shift_value);
3498 } else if (op->IsShr()) {
3499 __ Lsr(o_l, low, shift_value);
3500 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3501 __ Asr(o_h, high, shift_value);
3502 } else {
3503 __ Lsr(o_l, low, shift_value);
3504 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3505 __ Lsr(o_h, high, shift_value);
3506 }
3507 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003508 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003509 break;
3510 }
3511 default:
3512 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003513 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003514 }
3515}
3516
3517void LocationsBuilderARM::VisitShl(HShl* shl) {
3518 HandleShift(shl);
3519}
3520
3521void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3522 HandleShift(shl);
3523}
3524
3525void LocationsBuilderARM::VisitShr(HShr* shr) {
3526 HandleShift(shr);
3527}
3528
3529void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3530 HandleShift(shr);
3531}
3532
3533void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3534 HandleShift(ushr);
3535}
3536
3537void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3538 HandleShift(ushr);
3539}
3540
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003541void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003542 LocationSummary* locations =
3543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
David Brazdil6de19382016-01-08 17:37:10 +00003544 if (instruction->IsStringAlloc()) {
3545 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3546 } else {
3547 InvokeRuntimeCallingConvention calling_convention;
3548 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3549 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3550 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003551 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003552}
3553
3554void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003555 // Note: if heap poisoning is enabled, the entry point takes cares
3556 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003557 if (instruction->IsStringAlloc()) {
3558 // String is allocated through StringFactory. Call NewEmptyString entry point.
3559 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3560 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3561 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3562 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3563 __ blx(LR);
3564 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3565 } else {
3566 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3567 instruction,
3568 instruction->GetDexPc(),
3569 nullptr);
3570 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3571 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003572}
3573
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003574void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3575 LocationSummary* locations =
3576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3577 InvokeRuntimeCallingConvention calling_convention;
3578 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003579 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003580 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003581 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003582}
3583
3584void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3585 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003586 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003587 // Note: if heap poisoning is enabled, the entry point takes cares
3588 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003589 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003590 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003591 instruction->GetDexPc(),
3592 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003593 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003594}
3595
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003596void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003597 LocationSummary* locations =
3598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003599 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3600 if (location.IsStackSlot()) {
3601 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3602 } else if (location.IsDoubleStackSlot()) {
3603 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003604 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003605 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003606}
3607
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003608void InstructionCodeGeneratorARM::VisitParameterValue(
3609 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003610 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003611}
3612
3613void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3614 LocationSummary* locations =
3615 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3616 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3617}
3618
3619void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3620 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003621}
3622
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003623void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003624 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003625 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003626 locations->SetInAt(0, Location::RequiresRegister());
3627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003628}
3629
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003630void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3631 LocationSummary* locations = not_->GetLocations();
3632 Location out = locations->Out();
3633 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003634 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003635 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003636 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003637 break;
3638
3639 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003640 __ mvn(out.AsRegisterPairLow<Register>(),
3641 ShifterOperand(in.AsRegisterPairLow<Register>()));
3642 __ mvn(out.AsRegisterPairHigh<Register>(),
3643 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003644 break;
3645
3646 default:
3647 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3648 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003649}
3650
David Brazdil66d126e2015-04-03 16:02:44 +01003651void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3652 LocationSummary* locations =
3653 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3654 locations->SetInAt(0, Location::RequiresRegister());
3655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3656}
3657
3658void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003659 LocationSummary* locations = bool_not->GetLocations();
3660 Location out = locations->Out();
3661 Location in = locations->InAt(0);
3662 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3663}
3664
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003665void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003666 LocationSummary* locations =
3667 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003668 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08003669 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003670 case Primitive::kPrimLong: {
3671 locations->SetInAt(0, Location::RequiresRegister());
3672 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003673 // Output overlaps because it is written before doing the low comparison.
3674 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003675 break;
3676 }
3677 case Primitive::kPrimFloat:
3678 case Primitive::kPrimDouble: {
3679 locations->SetInAt(0, Location::RequiresFpuRegister());
3680 locations->SetInAt(1, Location::RequiresFpuRegister());
3681 locations->SetOut(Location::RequiresRegister());
3682 break;
3683 }
3684 default:
3685 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3686 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003687}
3688
3689void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003690 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003691 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003692 Location left = locations->InAt(0);
3693 Location right = locations->InAt(1);
3694
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003695 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003696 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003697 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003698 switch (type) {
Aart Bika19616e2016-02-01 18:57:58 -08003699 case Primitive::kPrimInt: {
3700 __ LoadImmediate(out, 0);
3701 __ cmp(left.AsRegister<Register>(),
3702 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3703 less_cond = LT;
3704 break;
3705 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003706 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003707 __ cmp(left.AsRegisterPairHigh<Register>(),
3708 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003709 __ b(&less, LT);
3710 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003711 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003712 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003713 __ cmp(left.AsRegisterPairLow<Register>(),
3714 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003715 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003716 break;
3717 }
3718 case Primitive::kPrimFloat:
3719 case Primitive::kPrimDouble: {
3720 __ LoadImmediate(out, 0);
3721 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003723 } else {
3724 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3725 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3726 }
3727 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003728 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003729 break;
3730 }
3731 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003732 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003733 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003734 }
Aart Bika19616e2016-02-01 18:57:58 -08003735
Calin Juravleddb7df22014-11-25 20:56:51 +00003736 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003737 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003738
3739 __ Bind(&greater);
3740 __ LoadImmediate(out, 1);
3741 __ b(&done);
3742
3743 __ Bind(&less);
3744 __ LoadImmediate(out, -1);
3745
3746 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003747}
3748
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003749void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003750 LocationSummary* locations =
3751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003752 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3753 locations->SetInAt(i, Location::Any());
3754 }
3755 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003756}
3757
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003758void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003759 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003760}
3761
Roland Levillainc9285912015-12-18 10:38:42 +00003762void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3763 // TODO (ported from quick): revisit ARM barrier kinds.
3764 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003765 switch (kind) {
3766 case MemBarrierKind::kAnyStore:
3767 case MemBarrierKind::kLoadAny:
3768 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003769 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003770 break;
3771 }
3772 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003773 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003774 break;
3775 }
3776 default:
3777 LOG(FATAL) << "Unexpected memory barrier " << kind;
3778 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003779 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003780}
3781
3782void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3783 uint32_t offset,
3784 Register out_lo,
3785 Register out_hi) {
3786 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003787 // Ensure `out_lo` is different from `addr`, so that loading
3788 // `offset` into `out_lo` does not clutter `addr`.
3789 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003790 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003791 __ add(IP, addr, ShifterOperand(out_lo));
3792 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003793 }
3794 __ ldrexd(out_lo, out_hi, addr);
3795}
3796
3797void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3798 uint32_t offset,
3799 Register value_lo,
3800 Register value_hi,
3801 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003802 Register temp2,
3803 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003804 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003805 if (offset != 0) {
3806 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003807 __ add(IP, addr, ShifterOperand(temp1));
3808 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003809 }
3810 __ Bind(&fail);
3811 // We need a load followed by store. (The address used in a STREX instruction must
3812 // be the same as the address in the most recently executed LDREX instruction.)
3813 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003814 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003815 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003816 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003817}
3818
3819void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3820 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3821
Nicolas Geoffray39468442014-09-02 15:17:15 +01003822 LocationSummary* locations =
3823 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003824 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003825
Calin Juravle52c48962014-12-16 17:02:57 +00003826 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003827 if (Primitive::IsFloatingPointType(field_type)) {
3828 locations->SetInAt(1, Location::RequiresFpuRegister());
3829 } else {
3830 locations->SetInAt(1, Location::RequiresRegister());
3831 }
3832
Calin Juravle52c48962014-12-16 17:02:57 +00003833 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003834 bool generate_volatile = field_info.IsVolatile()
3835 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003836 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003837 bool needs_write_barrier =
3838 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003839 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003840 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003841 if (needs_write_barrier) {
3842 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003843 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003844 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003845 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003846 // - registers need to be consecutive
3847 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003848 // We don't test for ARM yet, and the assertion makes sure that we
3849 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003850 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3851
3852 locations->AddTemp(Location::RequiresRegister());
3853 locations->AddTemp(Location::RequiresRegister());
3854 if (field_type == Primitive::kPrimDouble) {
3855 // For doubles we need two more registers to copy the value.
3856 locations->AddTemp(Location::RegisterLocation(R2));
3857 locations->AddTemp(Location::RegisterLocation(R3));
3858 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003859 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003860}
3861
Calin Juravle52c48962014-12-16 17:02:57 +00003862void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003863 const FieldInfo& field_info,
3864 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003865 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3866
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003867 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003868 Register base = locations->InAt(0).AsRegister<Register>();
3869 Location value = locations->InAt(1);
3870
3871 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003872 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003873 Primitive::Type field_type = field_info.GetFieldType();
3874 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003875 bool needs_write_barrier =
3876 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003877
3878 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003879 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003880 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003881
3882 switch (field_type) {
3883 case Primitive::kPrimBoolean:
3884 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003885 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003886 break;
3887 }
3888
3889 case Primitive::kPrimShort:
3890 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003891 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003892 break;
3893 }
3894
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003895 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003896 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003897 if (kPoisonHeapReferences && needs_write_barrier) {
3898 // Note that in the case where `value` is a null reference,
3899 // we do not enter this block, as a null reference does not
3900 // need poisoning.
3901 DCHECK_EQ(field_type, Primitive::kPrimNot);
3902 Register temp = locations->GetTemp(0).AsRegister<Register>();
3903 __ Mov(temp, value.AsRegister<Register>());
3904 __ PoisonHeapReference(temp);
3905 __ StoreToOffset(kStoreWord, temp, base, offset);
3906 } else {
3907 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3908 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003909 break;
3910 }
3911
3912 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003913 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003914 GenerateWideAtomicStore(base, offset,
3915 value.AsRegisterPairLow<Register>(),
3916 value.AsRegisterPairHigh<Register>(),
3917 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003918 locations->GetTemp(1).AsRegister<Register>(),
3919 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003920 } else {
3921 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003922 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003923 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003924 break;
3925 }
3926
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003927 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003928 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003929 break;
3930 }
3931
3932 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003933 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003934 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003935 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3936 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3937
3938 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3939
3940 GenerateWideAtomicStore(base, offset,
3941 value_reg_lo,
3942 value_reg_hi,
3943 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003944 locations->GetTemp(3).AsRegister<Register>(),
3945 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003946 } else {
3947 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003948 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003949 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003950 break;
3951 }
3952
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003953 case Primitive::kPrimVoid:
3954 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003955 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003956 }
Calin Juravle52c48962014-12-16 17:02:57 +00003957
Calin Juravle77520bc2015-01-12 18:45:46 +00003958 // Longs and doubles are handled in the switch.
3959 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3960 codegen_->MaybeRecordImplicitNullCheck(instruction);
3961 }
3962
3963 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3964 Register temp = locations->GetTemp(0).AsRegister<Register>();
3965 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003966 codegen_->MarkGCCard(
3967 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003968 }
3969
Calin Juravle52c48962014-12-16 17:02:57 +00003970 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003971 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003972 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003973}
3974
Calin Juravle52c48962014-12-16 17:02:57 +00003975void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3976 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003977
3978 bool object_field_get_with_read_barrier =
3979 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003980 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003981 new (GetGraph()->GetArena()) LocationSummary(instruction,
3982 object_field_get_with_read_barrier ?
3983 LocationSummary::kCallOnSlowPath :
3984 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003985 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003986
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003987 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00003988 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003989 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00003990 // The output overlaps in case of volatile long: we don't want the
3991 // code generated by GenerateWideAtomicLoad to overwrite the
3992 // object's location. Likewise, in the case of an object field get
3993 // with read barriers enabled, we do not want the load to overwrite
3994 // the object's location, as we need it to emit the read barrier.
3995 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
3996 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01003997
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003998 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3999 locations->SetOut(Location::RequiresFpuRegister());
4000 } else {
4001 locations->SetOut(Location::RequiresRegister(),
4002 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4003 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004004 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004005 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004006 // - registers need to be consecutive
4007 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004008 // We don't test for ARM yet, and the assertion makes sure that we
4009 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004010 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4011 locations->AddTemp(Location::RequiresRegister());
4012 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004013 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4014 // We need a temporary register for the read barrier marking slow
4015 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4016 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004017 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004018}
4019
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004020Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4021 Opcode opcode) {
4022 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4023 if (constant->IsConstant() &&
4024 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4025 return Location::ConstantLocation(constant->AsConstant());
4026 }
4027 return Location::RequiresRegister();
4028}
4029
4030bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4031 Opcode opcode) {
4032 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4033 if (Primitive::Is64BitType(input_cst->GetType())) {
4034 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4035 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4036 } else {
4037 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4038 }
4039}
4040
4041bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4042 ShifterOperand so;
4043 ArmAssembler* assembler = codegen_->GetAssembler();
4044 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4045 return true;
4046 }
4047 Opcode neg_opcode = kNoOperand;
4048 switch (opcode) {
4049 case AND:
4050 neg_opcode = BIC;
4051 break;
4052 case ORR:
4053 neg_opcode = ORN;
4054 break;
4055 default:
4056 return false;
4057 }
4058 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4059}
4060
Calin Juravle52c48962014-12-16 17:02:57 +00004061void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4062 const FieldInfo& field_info) {
4063 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004064
Calin Juravle52c48962014-12-16 17:02:57 +00004065 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004066 Location base_loc = locations->InAt(0);
4067 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004068 Location out = locations->Out();
4069 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004070 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004071 Primitive::Type field_type = field_info.GetFieldType();
4072 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4073
4074 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004075 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004076 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004077 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004078
Roland Levillainc9285912015-12-18 10:38:42 +00004079 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004080 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004081 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004082
Roland Levillainc9285912015-12-18 10:38:42 +00004083 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004084 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004085 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004086
Roland Levillainc9285912015-12-18 10:38:42 +00004087 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004088 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004089 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004090
4091 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004092 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004093 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004094
4095 case Primitive::kPrimNot: {
4096 // /* HeapReference<Object> */ out = *(base + offset)
4097 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4098 Location temp_loc = locations->GetTemp(0);
4099 // Note that a potential implicit null check is handled in this
4100 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4101 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4102 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4103 if (is_volatile) {
4104 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4105 }
4106 } else {
4107 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4108 codegen_->MaybeRecordImplicitNullCheck(instruction);
4109 if (is_volatile) {
4110 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4111 }
4112 // If read barriers are enabled, emit read barriers other than
4113 // Baker's using a slow path (and also unpoison the loaded
4114 // reference, if heap poisoning is enabled).
4115 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4116 }
4117 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004118 }
4119
Roland Levillainc9285912015-12-18 10:38:42 +00004120 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004121 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004122 GenerateWideAtomicLoad(base, offset,
4123 out.AsRegisterPairLow<Register>(),
4124 out.AsRegisterPairHigh<Register>());
4125 } else {
4126 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4127 }
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::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004131 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004132 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004133
4134 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004135 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004136 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004137 Register lo = locations->GetTemp(0).AsRegister<Register>();
4138 Register hi = locations->GetTemp(1).AsRegister<Register>();
4139 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004140 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004141 __ vmovdrr(out_reg, lo, hi);
4142 } else {
4143 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004144 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004145 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004146 break;
4147 }
4148
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004149 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004150 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004151 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004152 }
Calin Juravle52c48962014-12-16 17:02:57 +00004153
Roland Levillainc9285912015-12-18 10:38:42 +00004154 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4155 // Potential implicit null checks, in the case of reference or
4156 // double fields, are handled in the previous switch statement.
4157 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004158 codegen_->MaybeRecordImplicitNullCheck(instruction);
4159 }
4160
Calin Juravle52c48962014-12-16 17:02:57 +00004161 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004162 if (field_type == Primitive::kPrimNot) {
4163 // Memory barriers, in the case of references, are also handled
4164 // in the previous switch statement.
4165 } else {
4166 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4167 }
Roland Levillain4d027112015-07-01 15:41:14 +01004168 }
Calin Juravle52c48962014-12-16 17:02:57 +00004169}
4170
4171void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4172 HandleFieldSet(instruction, instruction->GetFieldInfo());
4173}
4174
4175void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004176 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004177}
4178
4179void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4180 HandleFieldGet(instruction, instruction->GetFieldInfo());
4181}
4182
4183void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4184 HandleFieldGet(instruction, instruction->GetFieldInfo());
4185}
4186
4187void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4188 HandleFieldGet(instruction, instruction->GetFieldInfo());
4189}
4190
4191void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4192 HandleFieldGet(instruction, instruction->GetFieldInfo());
4193}
4194
4195void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4196 HandleFieldSet(instruction, instruction->GetFieldInfo());
4197}
4198
4199void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004200 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004201}
4202
Calin Juravlee460d1d2015-09-29 04:52:17 +01004203void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4204 HUnresolvedInstanceFieldGet* instruction) {
4205 FieldAccessCallingConventionARM calling_convention;
4206 codegen_->CreateUnresolvedFieldLocationSummary(
4207 instruction, instruction->GetFieldType(), calling_convention);
4208}
4209
4210void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4211 HUnresolvedInstanceFieldGet* instruction) {
4212 FieldAccessCallingConventionARM calling_convention;
4213 codegen_->GenerateUnresolvedFieldAccess(instruction,
4214 instruction->GetFieldType(),
4215 instruction->GetFieldIndex(),
4216 instruction->GetDexPc(),
4217 calling_convention);
4218}
4219
4220void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4221 HUnresolvedInstanceFieldSet* instruction) {
4222 FieldAccessCallingConventionARM calling_convention;
4223 codegen_->CreateUnresolvedFieldLocationSummary(
4224 instruction, instruction->GetFieldType(), calling_convention);
4225}
4226
4227void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4228 HUnresolvedInstanceFieldSet* instruction) {
4229 FieldAccessCallingConventionARM calling_convention;
4230 codegen_->GenerateUnresolvedFieldAccess(instruction,
4231 instruction->GetFieldType(),
4232 instruction->GetFieldIndex(),
4233 instruction->GetDexPc(),
4234 calling_convention);
4235}
4236
4237void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4238 HUnresolvedStaticFieldGet* instruction) {
4239 FieldAccessCallingConventionARM calling_convention;
4240 codegen_->CreateUnresolvedFieldLocationSummary(
4241 instruction, instruction->GetFieldType(), calling_convention);
4242}
4243
4244void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4245 HUnresolvedStaticFieldGet* instruction) {
4246 FieldAccessCallingConventionARM calling_convention;
4247 codegen_->GenerateUnresolvedFieldAccess(instruction,
4248 instruction->GetFieldType(),
4249 instruction->GetFieldIndex(),
4250 instruction->GetDexPc(),
4251 calling_convention);
4252}
4253
4254void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4255 HUnresolvedStaticFieldSet* instruction) {
4256 FieldAccessCallingConventionARM calling_convention;
4257 codegen_->CreateUnresolvedFieldLocationSummary(
4258 instruction, instruction->GetFieldType(), calling_convention);
4259}
4260
4261void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4262 HUnresolvedStaticFieldSet* instruction) {
4263 FieldAccessCallingConventionARM calling_convention;
4264 codegen_->GenerateUnresolvedFieldAccess(instruction,
4265 instruction->GetFieldType(),
4266 instruction->GetFieldIndex(),
4267 instruction->GetDexPc(),
4268 calling_convention);
4269}
4270
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004271void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004272 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4273 ? LocationSummary::kCallOnSlowPath
4274 : LocationSummary::kNoCall;
4275 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004276 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004277 if (instruction->HasUses()) {
4278 locations->SetOut(Location::SameAsFirstInput());
4279 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280}
4281
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004282void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004283 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4284 return;
4285 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004286 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004287
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004288 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4289 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4290}
4291
4292void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004293 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004294 codegen_->AddSlowPath(slow_path);
4295
4296 LocationSummary* locations = instruction->GetLocations();
4297 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004298
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004299 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300}
4301
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004302void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004303 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004304 GenerateImplicitNullCheck(instruction);
4305 } else {
4306 GenerateExplicitNullCheck(instruction);
4307 }
4308}
4309
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004310void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004311 bool object_array_get_with_read_barrier =
4312 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004313 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004314 new (GetGraph()->GetArena()) LocationSummary(instruction,
4315 object_array_get_with_read_barrier ?
4316 LocationSummary::kCallOnSlowPath :
4317 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004318 locations->SetInAt(0, Location::RequiresRegister());
4319 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004320 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4321 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4322 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004323 // The output overlaps in the case of an object array get with
4324 // read barriers enabled: we do not want the move to overwrite the
4325 // array's location, as we need it to emit the read barrier.
4326 locations->SetOut(
4327 Location::RequiresRegister(),
4328 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004329 }
Roland Levillainc9285912015-12-18 10:38:42 +00004330 // We need a temporary register for the read barrier marking slow
4331 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4332 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4333 locations->AddTemp(Location::RequiresRegister());
4334 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004335}
4336
4337void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4338 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004339 Location obj_loc = locations->InAt(0);
4340 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004341 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004342 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004343
Roland Levillainc9285912015-12-18 10:38:42 +00004344 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004345 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346 case Primitive::kPrimBoolean: {
4347 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004348 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004349 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004350 size_t offset =
4351 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4353 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004354 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004355 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4356 }
4357 break;
4358 }
4359
4360 case Primitive::kPrimByte: {
4361 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004362 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004363 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004364 size_t offset =
4365 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004366 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4367 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004368 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004369 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4370 }
4371 break;
4372 }
4373
4374 case Primitive::kPrimShort: {
4375 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004376 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004377 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004378 size_t offset =
4379 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004380 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4381 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004382 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004383 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4384 }
4385 break;
4386 }
4387
4388 case Primitive::kPrimChar: {
4389 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004390 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004391 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004392 size_t offset =
4393 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4395 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004396 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004397 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4398 }
4399 break;
4400 }
4401
Roland Levillainc9285912015-12-18 10:38:42 +00004402 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004404 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004405 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004406 size_t offset =
4407 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004408 __ LoadFromOffset(kLoadWord, out, obj, offset);
4409 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004410 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004411 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4412 }
4413 break;
4414 }
4415
Roland Levillainc9285912015-12-18 10:38:42 +00004416 case Primitive::kPrimNot: {
4417 static_assert(
4418 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4419 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4420 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4421 // /* HeapReference<Object> */ out =
4422 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4423 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4424 Location temp = locations->GetTemp(0);
4425 // Note that a potential implicit null check is handled in this
4426 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4427 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4428 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4429 } else {
4430 Register out = out_loc.AsRegister<Register>();
4431 if (index.IsConstant()) {
4432 size_t offset =
4433 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4434 __ LoadFromOffset(kLoadWord, out, obj, offset);
4435 codegen_->MaybeRecordImplicitNullCheck(instruction);
4436 // If read barriers are enabled, emit read barriers other than
4437 // Baker's using a slow path (and also unpoison the loaded
4438 // reference, if heap poisoning is enabled).
4439 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4440 } else {
4441 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4442 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4443 codegen_->MaybeRecordImplicitNullCheck(instruction);
4444 // If read barriers are enabled, emit read barriers other than
4445 // Baker's using a slow path (and also unpoison the loaded
4446 // reference, if heap poisoning is enabled).
4447 codegen_->MaybeGenerateReadBarrierSlow(
4448 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4449 }
4450 }
4451 break;
4452 }
4453
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004454 case Primitive::kPrimLong: {
4455 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
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_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004459 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004460 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004462 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004463 }
4464 break;
4465 }
4466
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004467 case Primitive::kPrimFloat: {
4468 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004469 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004470 if (index.IsConstant()) {
4471 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004472 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004473 } else {
4474 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004475 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004476 }
4477 break;
4478 }
4479
4480 case Primitive::kPrimDouble: {
4481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004482 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004483 if (index.IsConstant()) {
4484 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004485 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004486 } else {
4487 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004488 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004489 }
4490 break;
4491 }
4492
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004493 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004494 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004495 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004496 }
Roland Levillain4d027112015-07-01 15:41:14 +01004497
4498 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004499 // Potential implicit null checks, in the case of reference
4500 // arrays, are handled in the previous switch statement.
4501 } else {
4502 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004503 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004504}
4505
4506void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004507 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004508
4509 bool needs_write_barrier =
4510 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004511 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4512 bool object_array_set_with_read_barrier =
4513 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004514
Nicolas Geoffray39468442014-09-02 15:17:15 +01004515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004516 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004517 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4518 LocationSummary::kCallOnSlowPath :
4519 LocationSummary::kNoCall);
4520
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004521 locations->SetInAt(0, Location::RequiresRegister());
4522 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4523 if (Primitive::IsFloatingPointType(value_type)) {
4524 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004525 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004526 locations->SetInAt(2, Location::RequiresRegister());
4527 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004528 if (needs_write_barrier) {
4529 // Temporary registers for the write barrier.
4530 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004531 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004533}
4534
4535void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4536 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004537 Location array_loc = locations->InAt(0);
4538 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004539 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004540 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004541 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004542 bool needs_write_barrier =
4543 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004544
4545 switch (value_type) {
4546 case Primitive::kPrimBoolean:
4547 case Primitive::kPrimByte: {
4548 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004549 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004551 size_t offset =
4552 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004553 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004554 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004555 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004556 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4557 }
4558 break;
4559 }
4560
4561 case Primitive::kPrimShort:
4562 case Primitive::kPrimChar: {
4563 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004564 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004565 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004566 size_t offset =
4567 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004568 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004570 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004571 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4572 }
4573 break;
4574 }
4575
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004578 Location value_loc = locations->InAt(2);
4579 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004580 Register source = value;
4581
4582 if (instruction->InputAt(2)->IsNullConstant()) {
4583 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004584 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004585 size_t offset =
4586 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004587 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004588 } else {
4589 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004590 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004591 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004592 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004593 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004594 DCHECK(!needs_write_barrier);
4595 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004596 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004597 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004598
4599 DCHECK(needs_write_barrier);
4600 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4601 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4602 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4603 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4604 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4605 Label done;
4606 SlowPathCode* slow_path = nullptr;
4607
Roland Levillain3b359c72015-11-17 19:35:12 +00004608 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004609 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4610 codegen_->AddSlowPath(slow_path);
4611 if (instruction->GetValueCanBeNull()) {
4612 Label non_zero;
4613 __ CompareAndBranchIfNonZero(value, &non_zero);
4614 if (index.IsConstant()) {
4615 size_t offset =
4616 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4617 __ StoreToOffset(kStoreWord, value, array, offset);
4618 } else {
4619 DCHECK(index.IsRegister()) << index;
4620 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4621 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4622 }
4623 codegen_->MaybeRecordImplicitNullCheck(instruction);
4624 __ b(&done);
4625 __ Bind(&non_zero);
4626 }
4627
Roland Levillain3b359c72015-11-17 19:35:12 +00004628 if (kEmitCompilerReadBarrier) {
4629 // When read barriers are enabled, the type checking
4630 // instrumentation requires two read barriers:
4631 //
4632 // __ Mov(temp2, temp1);
4633 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4634 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004635 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004636 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4637 //
4638 // // /* HeapReference<Class> */ temp2 = value->klass_
4639 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004640 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004641 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4642 //
4643 // __ cmp(temp1, ShifterOperand(temp2));
4644 //
4645 // However, the second read barrier may trash `temp`, as it
4646 // is a temporary register, and as such would not be saved
4647 // along with live registers before calling the runtime (nor
4648 // restored afterwards). So in this case, we bail out and
4649 // delegate the work to the array set slow path.
4650 //
4651 // TODO: Extend the register allocator to support a new
4652 // "(locally) live temp" location so as to avoid always
4653 // going into the slow path when read barriers are enabled.
4654 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004655 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004656 // /* HeapReference<Class> */ temp1 = array->klass_
4657 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4658 codegen_->MaybeRecordImplicitNullCheck(instruction);
4659 __ MaybeUnpoisonHeapReference(temp1);
4660
4661 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4662 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4663 // /* HeapReference<Class> */ temp2 = value->klass_
4664 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4665 // If heap poisoning is enabled, no need to unpoison `temp1`
4666 // nor `temp2`, as we are comparing two poisoned references.
4667 __ cmp(temp1, ShifterOperand(temp2));
4668
4669 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4670 Label do_put;
4671 __ b(&do_put, EQ);
4672 // If heap poisoning is enabled, the `temp1` reference has
4673 // not been unpoisoned yet; unpoison it now.
4674 __ MaybeUnpoisonHeapReference(temp1);
4675
4676 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4677 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4678 // If heap poisoning is enabled, no need to unpoison
4679 // `temp1`, as we are comparing against null below.
4680 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4681 __ Bind(&do_put);
4682 } else {
4683 __ b(slow_path->GetEntryLabel(), NE);
4684 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004685 }
4686 }
4687
4688 if (kPoisonHeapReferences) {
4689 // Note that in the case where `value` is a null reference,
4690 // we do not enter this block, as a null reference does not
4691 // need poisoning.
4692 DCHECK_EQ(value_type, Primitive::kPrimNot);
4693 __ Mov(temp1, value);
4694 __ PoisonHeapReference(temp1);
4695 source = temp1;
4696 }
4697
4698 if (index.IsConstant()) {
4699 size_t offset =
4700 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4701 __ StoreToOffset(kStoreWord, source, array, offset);
4702 } else {
4703 DCHECK(index.IsRegister()) << index;
4704 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4705 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4706 }
4707
Roland Levillain3b359c72015-11-17 19:35:12 +00004708 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004709 codegen_->MaybeRecordImplicitNullCheck(instruction);
4710 }
4711
4712 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4713
4714 if (done.IsLinked()) {
4715 __ Bind(&done);
4716 }
4717
4718 if (slow_path != nullptr) {
4719 __ Bind(slow_path->GetExitLabel());
4720 }
4721
4722 break;
4723 }
4724
4725 case Primitive::kPrimInt: {
4726 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4727 Register value = locations->InAt(2).AsRegister<Register>();
4728 if (index.IsConstant()) {
4729 size_t offset =
4730 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4731 __ StoreToOffset(kStoreWord, value, array, offset);
4732 } else {
4733 DCHECK(index.IsRegister()) << index;
4734 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4735 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4736 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 break;
4738 }
4739
4740 case Primitive::kPrimLong: {
4741 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004742 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004744 size_t offset =
4745 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004746 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004747 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004748 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004749 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 }
4751 break;
4752 }
4753
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004754 case Primitive::kPrimFloat: {
4755 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4756 Location value = locations->InAt(2);
4757 DCHECK(value.IsFpuRegister());
4758 if (index.IsConstant()) {
4759 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004760 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004761 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004762 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004763 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4764 }
4765 break;
4766 }
4767
4768 case Primitive::kPrimDouble: {
4769 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4770 Location value = locations->InAt(2);
4771 DCHECK(value.IsFpuRegisterPair());
4772 if (index.IsConstant()) {
4773 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004774 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004775 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004776 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004777 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4778 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004779
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004780 break;
4781 }
4782
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004784 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004785 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004786 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004787
Roland Levillain80e67092016-01-08 16:04:55 +00004788 // Objects are handled in the switch.
4789 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004790 codegen_->MaybeRecordImplicitNullCheck(instruction);
4791 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004792}
4793
4794void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004795 LocationSummary* locations =
4796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004797 locations->SetInAt(0, Location::RequiresRegister());
4798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004799}
4800
4801void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4802 LocationSummary* locations = instruction->GetLocations();
4803 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004804 Register obj = locations->InAt(0).AsRegister<Register>();
4805 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004806 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004807 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004808}
4809
4810void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004811 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4812 ? LocationSummary::kCallOnSlowPath
4813 : LocationSummary::kNoCall;
4814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004815 locations->SetInAt(0, Location::RequiresRegister());
4816 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004817 if (instruction->HasUses()) {
4818 locations->SetOut(Location::SameAsFirstInput());
4819 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004820}
4821
4822void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4823 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004824 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004825 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004826 codegen_->AddSlowPath(slow_path);
4827
Roland Levillain271ab9c2014-11-27 15:23:57 +00004828 Register index = locations->InAt(0).AsRegister<Register>();
4829 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004830
4831 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004832 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004833}
4834
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004835void CodeGeneratorARM::MarkGCCard(Register temp,
4836 Register card,
4837 Register object,
4838 Register value,
4839 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004840 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004841 if (can_be_null) {
4842 __ CompareAndBranchIfZero(value, &is_null);
4843 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4845 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4846 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004847 if (can_be_null) {
4848 __ Bind(&is_null);
4849 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004850}
4851
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004852void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004853 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004854}
4855
4856void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004857 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4858}
4859
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004860void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4861 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4862}
4863
4864void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004865 HBasicBlock* block = instruction->GetBlock();
4866 if (block->GetLoopInformation() != nullptr) {
4867 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4868 // The back edge will generate the suspend check.
4869 return;
4870 }
4871 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4872 // The goto will generate the suspend check.
4873 return;
4874 }
4875 GenerateSuspendCheck(instruction, nullptr);
4876}
4877
4878void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4879 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004880 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004881 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4882 if (slow_path == nullptr) {
4883 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4884 instruction->SetSlowPath(slow_path);
4885 codegen_->AddSlowPath(slow_path);
4886 if (successor != nullptr) {
4887 DCHECK(successor->IsLoopHeader());
4888 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4889 }
4890 } else {
4891 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4892 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004893
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004894 __ LoadFromOffset(
4895 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004896 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004897 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004898 __ Bind(slow_path->GetReturnLabel());
4899 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004900 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004901 __ b(slow_path->GetEntryLabel());
4902 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004903}
4904
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004905ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4906 return codegen_->GetAssembler();
4907}
4908
4909void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004910 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004911 Location source = move->GetSource();
4912 Location destination = move->GetDestination();
4913
4914 if (source.IsRegister()) {
4915 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004916 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004917 } else if (destination.IsFpuRegister()) {
4918 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004919 } else {
4920 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004921 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004922 SP, destination.GetStackIndex());
4923 }
4924 } else if (source.IsStackSlot()) {
4925 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004926 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004927 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004928 } else if (destination.IsFpuRegister()) {
4929 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004930 } else {
4931 DCHECK(destination.IsStackSlot());
4932 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4933 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4934 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004935 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004936 if (destination.IsRegister()) {
4937 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
4938 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004939 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004940 } else {
4941 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004942 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4943 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004944 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004945 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004946 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4947 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004948 } else if (destination.IsRegisterPair()) {
4949 DCHECK(ExpectedPairLayout(destination));
4950 __ LoadFromOffset(
4951 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4952 } else {
4953 DCHECK(destination.IsFpuRegisterPair()) << destination;
4954 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4955 SP,
4956 source.GetStackIndex());
4957 }
4958 } else if (source.IsRegisterPair()) {
4959 if (destination.IsRegisterPair()) {
4960 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4961 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004962 } else if (destination.IsFpuRegisterPair()) {
4963 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4964 source.AsRegisterPairLow<Register>(),
4965 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004966 } else {
4967 DCHECK(destination.IsDoubleStackSlot()) << destination;
4968 DCHECK(ExpectedPairLayout(source));
4969 __ StoreToOffset(
4970 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4971 }
4972 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004973 if (destination.IsRegisterPair()) {
4974 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4975 destination.AsRegisterPairHigh<Register>(),
4976 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4977 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004978 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4979 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4980 } else {
4981 DCHECK(destination.IsDoubleStackSlot()) << destination;
4982 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
4983 SP,
4984 destination.GetStackIndex());
4985 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004986 } else {
4987 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004988 HConstant* constant = source.GetConstant();
4989 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4990 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004991 if (destination.IsRegister()) {
4992 __ LoadImmediate(destination.AsRegister<Register>(), value);
4993 } else {
4994 DCHECK(destination.IsStackSlot());
4995 __ LoadImmediate(IP, value);
4996 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4997 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004998 } else if (constant->IsLongConstant()) {
4999 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005000 if (destination.IsRegisterPair()) {
5001 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5002 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005003 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005004 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005005 __ LoadImmediate(IP, Low32Bits(value));
5006 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5007 __ LoadImmediate(IP, High32Bits(value));
5008 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5009 }
5010 } else if (constant->IsDoubleConstant()) {
5011 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005012 if (destination.IsFpuRegisterPair()) {
5013 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005014 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005015 DCHECK(destination.IsDoubleStackSlot()) << destination;
5016 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005017 __ LoadImmediate(IP, Low32Bits(int_value));
5018 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5019 __ LoadImmediate(IP, High32Bits(int_value));
5020 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5021 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005022 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005023 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005024 float value = constant->AsFloatConstant()->GetValue();
5025 if (destination.IsFpuRegister()) {
5026 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5027 } else {
5028 DCHECK(destination.IsStackSlot());
5029 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5030 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5031 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005032 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005033 }
5034}
5035
5036void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5037 __ Mov(IP, reg);
5038 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5039 __ StoreToOffset(kStoreWord, IP, SP, mem);
5040}
5041
5042void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5043 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5044 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5045 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5046 SP, mem1 + stack_offset);
5047 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5048 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5049 SP, mem2 + stack_offset);
5050 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5051}
5052
5053void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005054 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005055 Location source = move->GetSource();
5056 Location destination = move->GetDestination();
5057
5058 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005059 DCHECK_NE(source.AsRegister<Register>(), IP);
5060 DCHECK_NE(destination.AsRegister<Register>(), IP);
5061 __ Mov(IP, source.AsRegister<Register>());
5062 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5063 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005064 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005065 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005066 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005067 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005068 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5069 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005070 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005071 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005072 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005073 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005074 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005075 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005076 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005077 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005078 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5079 destination.AsRegisterPairHigh<Register>(),
5080 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005081 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005082 Register low_reg = source.IsRegisterPair()
5083 ? source.AsRegisterPairLow<Register>()
5084 : destination.AsRegisterPairLow<Register>();
5085 int mem = source.IsRegisterPair()
5086 ? destination.GetStackIndex()
5087 : source.GetStackIndex();
5088 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005089 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005090 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005091 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005092 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005093 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5094 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005095 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005096 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005097 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005098 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5099 DRegister reg = source.IsFpuRegisterPair()
5100 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5101 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5102 int mem = source.IsFpuRegisterPair()
5103 ? destination.GetStackIndex()
5104 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005105 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005106 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005107 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005108 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5109 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5110 : destination.AsFpuRegister<SRegister>();
5111 int mem = source.IsFpuRegister()
5112 ? destination.GetStackIndex()
5113 : source.GetStackIndex();
5114
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005115 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005116 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005117 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005118 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005119 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5120 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005121 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005122 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005123 }
5124}
5125
5126void ParallelMoveResolverARM::SpillScratch(int reg) {
5127 __ Push(static_cast<Register>(reg));
5128}
5129
5130void ParallelMoveResolverARM::RestoreScratch(int reg) {
5131 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005132}
5133
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005134void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005135 InvokeRuntimeCallingConvention calling_convention;
5136 CodeGenerator::CreateLoadClassLocationSummary(
5137 cls,
5138 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005139 Location::RegisterLocation(R0),
5140 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005141}
5142
5143void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005144 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005145 if (cls->NeedsAccessCheck()) {
5146 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5147 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5148 cls,
5149 cls->GetDexPc(),
5150 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005151 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005152 return;
5153 }
5154
Roland Levillain3b359c72015-11-17 19:35:12 +00005155 Location out_loc = locations->Out();
5156 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005157 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005158
Calin Juravle580b6092015-10-06 17:35:58 +01005159 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005160 DCHECK(!cls->CanCallRuntime());
5161 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005162 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5163 GenerateGcRootFieldLoad(
5164 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005165 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005166 // /* GcRoot<mirror::Class>[] */ out =
5167 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005168 __ LoadFromOffset(kLoadWord,
5169 out,
5170 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005171 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005172 // /* GcRoot<mirror::Class> */ out = out[type_index]
5173 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005174
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005175 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5176 DCHECK(cls->CanCallRuntime());
5177 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5178 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5179 codegen_->AddSlowPath(slow_path);
5180 if (!cls->IsInDexCache()) {
5181 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5182 }
5183 if (cls->MustGenerateClinitCheck()) {
5184 GenerateClassInitializationCheck(slow_path, out);
5185 } else {
5186 __ Bind(slow_path->GetExitLabel());
5187 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005188 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005189 }
5190}
5191
5192void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5193 LocationSummary* locations =
5194 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5195 locations->SetInAt(0, Location::RequiresRegister());
5196 if (check->HasUses()) {
5197 locations->SetOut(Location::SameAsFirstInput());
5198 }
5199}
5200
5201void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005202 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005203 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005204 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005205 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005206 GenerateClassInitializationCheck(slow_path,
5207 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005208}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005209
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005210void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005211 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005212 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5213 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5214 __ b(slow_path->GetEntryLabel(), LT);
5215 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5216 // properly. Therefore, we do a memory fence.
5217 __ dmb(ISH);
5218 __ Bind(slow_path->GetExitLabel());
5219}
5220
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005221void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005222 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5223 ? LocationSummary::kCallOnSlowPath
5224 : LocationSummary::kNoCall;
5225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005226 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005227 locations->SetOut(Location::RequiresRegister());
5228}
5229
5230void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005231 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005232 Location out_loc = locations->Out();
5233 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005234 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005235
Roland Levillainc9285912015-12-18 10:38:42 +00005236 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5237 GenerateGcRootFieldLoad(
5238 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005239 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005240 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005241 // /* GcRoot<mirror::String> */ out = out[string_index]
5242 GenerateGcRootFieldLoad(
5243 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005244
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005245 if (!load->IsInDexCache()) {
5246 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5247 codegen_->AddSlowPath(slow_path);
5248 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5249 __ Bind(slow_path->GetExitLabel());
5250 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005251}
5252
David Brazdilcb1c0552015-08-04 16:22:25 +01005253static int32_t GetExceptionTlsOffset() {
5254 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5255}
5256
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005257void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5258 LocationSummary* locations =
5259 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5260 locations->SetOut(Location::RequiresRegister());
5261}
5262
5263void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005264 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005265 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5266}
5267
5268void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5269 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5270}
5271
5272void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005273 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005274 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005275}
5276
5277void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5278 LocationSummary* locations =
5279 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5280 InvokeRuntimeCallingConvention calling_convention;
5281 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5282}
5283
5284void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5285 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005286 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005287 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005288}
5289
Roland Levillainc9285912015-12-18 10:38:42 +00005290static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5291 return kEmitCompilerReadBarrier &&
5292 (kUseBakerReadBarrier ||
5293 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5294 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5295 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5296}
5297
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005298void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005299 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005300 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5301 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005302 case TypeCheckKind::kExactCheck:
5303 case TypeCheckKind::kAbstractClassCheck:
5304 case TypeCheckKind::kClassHierarchyCheck:
5305 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005306 call_kind =
5307 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005308 break;
5309 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005310 case TypeCheckKind::kUnresolvedCheck:
5311 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005312 call_kind = LocationSummary::kCallOnSlowPath;
5313 break;
5314 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005315
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005317 locations->SetInAt(0, Location::RequiresRegister());
5318 locations->SetInAt(1, Location::RequiresRegister());
5319 // The "out" register is used as a temporary, so it overlaps with the inputs.
5320 // Note that TypeCheckSlowPathARM uses this register too.
5321 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5322 // When read barriers are enabled, we need a temporary register for
5323 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005324 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005325 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005326 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005327}
5328
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005329void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005330 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005331 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005332 Location obj_loc = locations->InAt(0);
5333 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005334 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005335 Location out_loc = locations->Out();
5336 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005337 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005338 locations->GetTemp(0) :
5339 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005340 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005341 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5342 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5343 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005344 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005345 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005346
5347 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005348 // avoid null check if we know obj is not null.
5349 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005350 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005351 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005352
Roland Levillain3b359c72015-11-17 19:35:12 +00005353 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005354 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005355
Roland Levillainc9285912015-12-18 10:38:42 +00005356 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005357 case TypeCheckKind::kExactCheck: {
5358 __ cmp(out, ShifterOperand(cls));
5359 // Classes must be equal for the instanceof to succeed.
5360 __ b(&zero, NE);
5361 __ LoadImmediate(out, 1);
5362 __ b(&done);
5363 break;
5364 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005365
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005366 case TypeCheckKind::kAbstractClassCheck: {
5367 // If the class is abstract, we eagerly fetch the super class of the
5368 // object to avoid doing a comparison we know will fail.
5369 Label loop;
5370 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005371 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005372 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005373 // If `out` is null, we use it for the result, and jump to `done`.
5374 __ CompareAndBranchIfZero(out, &done);
5375 __ cmp(out, ShifterOperand(cls));
5376 __ b(&loop, NE);
5377 __ LoadImmediate(out, 1);
5378 if (zero.IsLinked()) {
5379 __ b(&done);
5380 }
5381 break;
5382 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005383
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005384 case TypeCheckKind::kClassHierarchyCheck: {
5385 // Walk over the class hierarchy to find a match.
5386 Label loop, success;
5387 __ Bind(&loop);
5388 __ cmp(out, ShifterOperand(cls));
5389 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005390 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005391 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005392 __ CompareAndBranchIfNonZero(out, &loop);
5393 // If `out` is null, we use it for the result, and jump to `done`.
5394 __ b(&done);
5395 __ Bind(&success);
5396 __ LoadImmediate(out, 1);
5397 if (zero.IsLinked()) {
5398 __ b(&done);
5399 }
5400 break;
5401 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005402
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005403 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005404 // Do an exact check.
5405 Label exact_check;
5406 __ cmp(out, ShifterOperand(cls));
5407 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005408 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005409 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005410 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005411 // If `out` is null, we use it for the result, and jump to `done`.
5412 __ CompareAndBranchIfZero(out, &done);
5413 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5414 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5415 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005416 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005417 __ LoadImmediate(out, 1);
5418 __ b(&done);
5419 break;
5420 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005421
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005422 case TypeCheckKind::kArrayCheck: {
5423 __ cmp(out, ShifterOperand(cls));
5424 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005425 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5426 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005427 codegen_->AddSlowPath(slow_path);
5428 __ b(slow_path->GetEntryLabel(), NE);
5429 __ LoadImmediate(out, 1);
5430 if (zero.IsLinked()) {
5431 __ b(&done);
5432 }
5433 break;
5434 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005435
Calin Juravle98893e12015-10-02 21:05:03 +01005436 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005437 case TypeCheckKind::kInterfaceCheck: {
5438 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005439 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005440 // cases.
5441 //
5442 // We cannot directly call the InstanceofNonTrivial runtime
5443 // entry point without resorting to a type checking slow path
5444 // here (i.e. by calling InvokeRuntime directly), as it would
5445 // require to assign fixed registers for the inputs of this
5446 // HInstanceOf instruction (following the runtime calling
5447 // convention), which might be cluttered by the potential first
5448 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005449 //
5450 // TODO: Introduce a new runtime entry point taking the object
5451 // to test (instead of its class) as argument, and let it deal
5452 // with the read barrier issues. This will let us refactor this
5453 // case of the `switch` code as it was previously (with a direct
5454 // call to the runtime not using a type checking slow path).
5455 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005456 DCHECK(locations->OnlyCallsOnSlowPath());
5457 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5458 /* is_fatal */ false);
5459 codegen_->AddSlowPath(slow_path);
5460 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005461 if (zero.IsLinked()) {
5462 __ b(&done);
5463 }
5464 break;
5465 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005466 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005468 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005469 __ Bind(&zero);
5470 __ LoadImmediate(out, 0);
5471 }
5472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005473 if (done.IsLinked()) {
5474 __ Bind(&done);
5475 }
5476
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005477 if (slow_path != nullptr) {
5478 __ Bind(slow_path->GetExitLabel());
5479 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005480}
5481
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005482void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005483 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5484 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5485
Roland Levillain3b359c72015-11-17 19:35:12 +00005486 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5487 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005488 case TypeCheckKind::kExactCheck:
5489 case TypeCheckKind::kAbstractClassCheck:
5490 case TypeCheckKind::kClassHierarchyCheck:
5491 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005492 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5493 LocationSummary::kCallOnSlowPath :
5494 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005495 break;
5496 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005497 case TypeCheckKind::kUnresolvedCheck:
5498 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005499 call_kind = LocationSummary::kCallOnSlowPath;
5500 break;
5501 }
5502
Roland Levillain3b359c72015-11-17 19:35:12 +00005503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5504 locations->SetInAt(0, Location::RequiresRegister());
5505 locations->SetInAt(1, Location::RequiresRegister());
5506 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5507 locations->AddTemp(Location::RequiresRegister());
5508 // When read barriers are enabled, we need an additional temporary
5509 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005510 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005511 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005512 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005513}
5514
5515void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005516 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005517 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005518 Location obj_loc = locations->InAt(0);
5519 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005520 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005521 Location temp_loc = locations->GetTemp(0);
5522 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005523 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005524 locations->GetTemp(1) :
5525 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005526 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005527 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5528 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5529 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005530
Roland Levillain3b359c72015-11-17 19:35:12 +00005531 bool is_type_check_slow_path_fatal =
5532 (type_check_kind == TypeCheckKind::kExactCheck ||
5533 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5534 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5535 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5536 !instruction->CanThrowIntoCatchBlock();
5537 SlowPathCode* type_check_slow_path =
5538 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5539 is_type_check_slow_path_fatal);
5540 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005541
5542 Label done;
5543 // Avoid null check if we know obj is not null.
5544 if (instruction->MustDoNullCheck()) {
5545 __ CompareAndBranchIfZero(obj, &done);
5546 }
5547
Roland Levillain3b359c72015-11-17 19:35:12 +00005548 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005549 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005550
Roland Levillain3b359c72015-11-17 19:35:12 +00005551 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005552 case TypeCheckKind::kExactCheck:
5553 case TypeCheckKind::kArrayCheck: {
5554 __ cmp(temp, ShifterOperand(cls));
5555 // Jump to slow path for throwing the exception or doing a
5556 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005557 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005558 break;
5559 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005560
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005561 case TypeCheckKind::kAbstractClassCheck: {
5562 // If the class is abstract, we eagerly fetch the super class of the
5563 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005564 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005565 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005566 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005567 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005568
5569 // If the class reference currently in `temp` is not null, jump
5570 // to the `compare_classes` label to compare it with the checked
5571 // class.
5572 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5573 // Otherwise, jump to the slow path to throw the exception.
5574 //
5575 // But before, move back the object's class into `temp` before
5576 // going into the slow path, as it has been overwritten in the
5577 // meantime.
5578 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005579 GenerateReferenceLoadTwoRegisters(
5580 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005581 __ b(type_check_slow_path->GetEntryLabel());
5582
5583 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005584 __ cmp(temp, ShifterOperand(cls));
5585 __ b(&loop, NE);
5586 break;
5587 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005588
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005589 case TypeCheckKind::kClassHierarchyCheck: {
5590 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005591 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005592 __ Bind(&loop);
5593 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005594 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005595
Roland Levillain3b359c72015-11-17 19:35:12 +00005596 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005597 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005598
5599 // If the class reference currently in `temp` is not null, jump
5600 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005602 // Otherwise, jump to the slow path to throw the exception.
5603 //
5604 // But before, move back the object's class into `temp` before
5605 // going into the slow path, as it has been overwritten in the
5606 // meantime.
5607 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005608 GenerateReferenceLoadTwoRegisters(
5609 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005610 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005611 break;
5612 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005613
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005614 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005615 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005616 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005617 __ cmp(temp, ShifterOperand(cls));
5618 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005619
5620 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005621 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005622 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005623
5624 // If the component type is not null (i.e. the object is indeed
5625 // an array), jump to label `check_non_primitive_component_type`
5626 // to further check that this component type is not a primitive
5627 // type.
5628 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5629 // Otherwise, jump to the slow path to throw the exception.
5630 //
5631 // But before, move back the object's class into `temp` before
5632 // going into the slow path, as it has been overwritten in the
5633 // meantime.
5634 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005635 GenerateReferenceLoadTwoRegisters(
5636 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005637 __ b(type_check_slow_path->GetEntryLabel());
5638
5639 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005640 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005641 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5642 __ CompareAndBranchIfZero(temp, &done);
5643 // Same comment as above regarding `temp` and the slow path.
5644 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005645 GenerateReferenceLoadTwoRegisters(
5646 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005647 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005648 break;
5649 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005650
Calin Juravle98893e12015-10-02 21:05:03 +01005651 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005652 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005653 // We always go into the type check slow path for the unresolved
5654 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005655 //
5656 // We cannot directly call the CheckCast runtime entry point
5657 // without resorting to a type checking slow path here (i.e. by
5658 // calling InvokeRuntime directly), as it would require to
5659 // assign fixed registers for the inputs of this HInstanceOf
5660 // instruction (following the runtime calling convention), which
5661 // might be cluttered by the potential first read barrier
5662 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005663 //
5664 // TODO: Introduce a new runtime entry point taking the object
5665 // to test (instead of its class) as argument, and let it deal
5666 // with the read barrier issues. This will let us refactor this
5667 // case of the `switch` code as it was previously (with a direct
5668 // call to the runtime not using a type checking slow path).
5669 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005670 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 break;
5672 }
5673 __ Bind(&done);
5674
Roland Levillain3b359c72015-11-17 19:35:12 +00005675 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005676}
5677
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005678void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5679 LocationSummary* locations =
5680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5681 InvokeRuntimeCallingConvention calling_convention;
5682 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5683}
5684
5685void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5686 codegen_->InvokeRuntime(instruction->IsEnter()
5687 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5688 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005689 instruction->GetDexPc(),
5690 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005691 if (instruction->IsEnter()) {
5692 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5693 } else {
5694 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5695 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005696}
5697
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005698void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5699void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5700void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005701
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005702void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005703 LocationSummary* locations =
5704 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5705 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5706 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005707 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005708 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005709 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005710 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005711}
5712
5713void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5714 HandleBitwiseOperation(instruction);
5715}
5716
5717void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5718 HandleBitwiseOperation(instruction);
5719}
5720
5721void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5722 HandleBitwiseOperation(instruction);
5723}
5724
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005725void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5726 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5727 if (value == 0xffffffffu) {
5728 if (out != first) {
5729 __ mov(out, ShifterOperand(first));
5730 }
5731 return;
5732 }
5733 if (value == 0u) {
5734 __ mov(out, ShifterOperand(0));
5735 return;
5736 }
5737 ShifterOperand so;
5738 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5739 __ and_(out, first, so);
5740 } else {
5741 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5742 __ bic(out, first, ShifterOperand(~value));
5743 }
5744}
5745
5746void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5747 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5748 if (value == 0u) {
5749 if (out != first) {
5750 __ mov(out, ShifterOperand(first));
5751 }
5752 return;
5753 }
5754 if (value == 0xffffffffu) {
5755 __ mvn(out, ShifterOperand(0));
5756 return;
5757 }
5758 ShifterOperand so;
5759 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5760 __ orr(out, first, so);
5761 } else {
5762 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5763 __ orn(out, first, ShifterOperand(~value));
5764 }
5765}
5766
5767void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5768 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5769 if (value == 0u) {
5770 if (out != first) {
5771 __ mov(out, ShifterOperand(first));
5772 }
5773 return;
5774 }
5775 __ eor(out, first, ShifterOperand(value));
5776}
5777
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005778void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5779 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005780 Location first = locations->InAt(0);
5781 Location second = locations->InAt(1);
5782 Location out = locations->Out();
5783
5784 if (second.IsConstant()) {
5785 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5786 uint32_t value_low = Low32Bits(value);
5787 if (instruction->GetResultType() == Primitive::kPrimInt) {
5788 Register first_reg = first.AsRegister<Register>();
5789 Register out_reg = out.AsRegister<Register>();
5790 if (instruction->IsAnd()) {
5791 GenerateAndConst(out_reg, first_reg, value_low);
5792 } else if (instruction->IsOr()) {
5793 GenerateOrrConst(out_reg, first_reg, value_low);
5794 } else {
5795 DCHECK(instruction->IsXor());
5796 GenerateEorConst(out_reg, first_reg, value_low);
5797 }
5798 } else {
5799 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5800 uint32_t value_high = High32Bits(value);
5801 Register first_low = first.AsRegisterPairLow<Register>();
5802 Register first_high = first.AsRegisterPairHigh<Register>();
5803 Register out_low = out.AsRegisterPairLow<Register>();
5804 Register out_high = out.AsRegisterPairHigh<Register>();
5805 if (instruction->IsAnd()) {
5806 GenerateAndConst(out_low, first_low, value_low);
5807 GenerateAndConst(out_high, first_high, value_high);
5808 } else if (instruction->IsOr()) {
5809 GenerateOrrConst(out_low, first_low, value_low);
5810 GenerateOrrConst(out_high, first_high, value_high);
5811 } else {
5812 DCHECK(instruction->IsXor());
5813 GenerateEorConst(out_low, first_low, value_low);
5814 GenerateEorConst(out_high, first_high, value_high);
5815 }
5816 }
5817 return;
5818 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005819
5820 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005821 Register first_reg = first.AsRegister<Register>();
5822 ShifterOperand second_reg(second.AsRegister<Register>());
5823 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005824 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005825 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005826 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005827 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005828 } else {
5829 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005830 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005831 }
5832 } else {
5833 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005834 Register first_low = first.AsRegisterPairLow<Register>();
5835 Register first_high = first.AsRegisterPairHigh<Register>();
5836 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5837 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5838 Register out_low = out.AsRegisterPairLow<Register>();
5839 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005840 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005841 __ and_(out_low, first_low, second_low);
5842 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005843 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005844 __ orr(out_low, first_low, second_low);
5845 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005846 } else {
5847 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005848 __ eor(out_low, first_low, second_low);
5849 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005850 }
5851 }
5852}
5853
Roland Levillainc9285912015-12-18 10:38:42 +00005854void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5855 Location out,
5856 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005857 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005858 Register out_reg = out.AsRegister<Register>();
5859 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005860 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005861 if (kUseBakerReadBarrier) {
5862 // Load with fast path based Baker's read barrier.
5863 // /* HeapReference<Object> */ out = *(out + offset)
5864 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005865 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005866 } else {
5867 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005868 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00005869 // in the following move operation, as we will need it for the
5870 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005871 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00005872 // /* HeapReference<Object> */ out = *(out + offset)
5873 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005874 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00005875 }
5876 } else {
5877 // Plain load with no read barrier.
5878 // /* HeapReference<Object> */ out = *(out + offset)
5879 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5880 __ MaybeUnpoisonHeapReference(out_reg);
5881 }
5882}
5883
5884void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5885 Location out,
5886 Location obj,
5887 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005888 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005889 Register out_reg = out.AsRegister<Register>();
5890 Register obj_reg = obj.AsRegister<Register>();
5891 if (kEmitCompilerReadBarrier) {
5892 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005893 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005894 // Load with fast path based Baker's read barrier.
5895 // /* HeapReference<Object> */ out = *(obj + offset)
5896 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005897 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005898 } else {
5899 // Load with slow path based read barrier.
5900 // /* HeapReference<Object> */ out = *(obj + offset)
5901 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5902 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5903 }
5904 } else {
5905 // Plain load with no read barrier.
5906 // /* HeapReference<Object> */ out = *(obj + offset)
5907 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5908 __ MaybeUnpoisonHeapReference(out_reg);
5909 }
5910}
5911
5912void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
5913 Location root,
5914 Register obj,
5915 uint32_t offset) {
5916 Register root_reg = root.AsRegister<Register>();
5917 if (kEmitCompilerReadBarrier) {
5918 if (kUseBakerReadBarrier) {
5919 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5920 // Baker's read barrier are used:
5921 //
5922 // root = obj.field;
5923 // if (Thread::Current()->GetIsGcMarking()) {
5924 // root = ReadBarrier::Mark(root)
5925 // }
5926
5927 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5928 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5929 static_assert(
5930 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5931 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5932 "have different sizes.");
5933 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5934 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5935 "have different sizes.");
5936
5937 // Slow path used to mark the GC root `root`.
5938 SlowPathCode* slow_path =
5939 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
5940 codegen_->AddSlowPath(slow_path);
5941
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005942 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00005943 __ LoadFromOffset(
5944 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
5945 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
5946 __ Bind(slow_path->GetExitLabel());
5947 } else {
5948 // GC root loaded through a slow path for read barriers other
5949 // than Baker's.
5950 // /* GcRoot<mirror::Object>* */ root = obj + offset
5951 __ AddConstant(root_reg, obj, offset);
5952 // /* mirror::Object* */ root = root->Read()
5953 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5954 }
5955 } else {
5956 // Plain GC root load with no read barrier.
5957 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5958 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5959 // Note that GC roots are not affected by heap poisoning, thus we
5960 // do not have to unpoison `root_reg` here.
5961 }
5962}
5963
5964void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5965 Location ref,
5966 Register obj,
5967 uint32_t offset,
5968 Location temp,
5969 bool needs_null_check) {
5970 DCHECK(kEmitCompilerReadBarrier);
5971 DCHECK(kUseBakerReadBarrier);
5972
5973 // /* HeapReference<Object> */ ref = *(obj + offset)
5974 Location no_index = Location::NoLocation();
5975 GenerateReferenceLoadWithBakerReadBarrier(
5976 instruction, ref, obj, offset, no_index, temp, needs_null_check);
5977}
5978
5979void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5980 Location ref,
5981 Register obj,
5982 uint32_t data_offset,
5983 Location index,
5984 Location temp,
5985 bool needs_null_check) {
5986 DCHECK(kEmitCompilerReadBarrier);
5987 DCHECK(kUseBakerReadBarrier);
5988
5989 // /* HeapReference<Object> */ ref =
5990 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5991 GenerateReferenceLoadWithBakerReadBarrier(
5992 instruction, ref, obj, data_offset, index, temp, needs_null_check);
5993}
5994
5995void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5996 Location ref,
5997 Register obj,
5998 uint32_t offset,
5999 Location index,
6000 Location temp,
6001 bool needs_null_check) {
6002 DCHECK(kEmitCompilerReadBarrier);
6003 DCHECK(kUseBakerReadBarrier);
6004
6005 // In slow path based read barriers, the read barrier call is
6006 // inserted after the original load. However, in fast path based
6007 // Baker's read barriers, we need to perform the load of
6008 // mirror::Object::monitor_ *before* the original reference load.
6009 // This load-load ordering is required by the read barrier.
6010 // The fast path/slow path (for Baker's algorithm) should look like:
6011 //
6012 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6013 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6014 // HeapReference<Object> ref = *src; // Original reference load.
6015 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6016 // if (is_gray) {
6017 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6018 // }
6019 //
6020 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006021 // slightly more complex as it performs additional checks that we do
6022 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006023
6024 Register ref_reg = ref.AsRegister<Register>();
6025 Register temp_reg = temp.AsRegister<Register>();
6026 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6027
6028 // /* int32_t */ monitor = obj->monitor_
6029 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6030 if (needs_null_check) {
6031 MaybeRecordImplicitNullCheck(instruction);
6032 }
6033 // /* LockWord */ lock_word = LockWord(monitor)
6034 static_assert(sizeof(LockWord) == sizeof(int32_t),
6035 "art::LockWord and int32_t have different sizes.");
6036 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6037 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6038 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6039 static_assert(
6040 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6041 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6042
6043 // Introduce a dependency on the high bits of rb_state, which shall
6044 // be all zeroes, to prevent load-load reordering, and without using
6045 // a memory barrier (which would be more expensive).
6046 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6047 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6048 // obj is unchanged by this operation, but its value now depends on
6049 // IP, which depends on temp_reg.
6050 __ add(obj, obj, ShifterOperand(IP));
6051
6052 // The actual reference load.
6053 if (index.IsValid()) {
6054 static_assert(
6055 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6056 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6057 // /* HeapReference<Object> */ ref =
6058 // *(obj + offset + index * sizeof(HeapReference<Object>))
6059 if (index.IsConstant()) {
6060 size_t computed_offset =
6061 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6062 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6063 } else {
6064 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6065 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6066 }
6067 } else {
6068 // /* HeapReference<Object> */ ref = *(obj + offset)
6069 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6070 }
6071
6072 // Object* ref = ref_addr->AsMirrorPtr()
6073 __ MaybeUnpoisonHeapReference(ref_reg);
6074
6075 // Slow path used to mark the object `ref` when it is gray.
6076 SlowPathCode* slow_path =
6077 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6078 AddSlowPath(slow_path);
6079
6080 // if (rb_state == ReadBarrier::gray_ptr_)
6081 // ref = ReadBarrier::Mark(ref);
6082 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6083 __ b(slow_path->GetEntryLabel(), EQ);
6084 __ Bind(slow_path->GetExitLabel());
6085}
6086
6087void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6088 Location out,
6089 Location ref,
6090 Location obj,
6091 uint32_t offset,
6092 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006093 DCHECK(kEmitCompilerReadBarrier);
6094
Roland Levillainc9285912015-12-18 10:38:42 +00006095 // Insert a slow path based read barrier *after* the reference load.
6096 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006097 // If heap poisoning is enabled, the unpoisoning of the loaded
6098 // reference will be carried out by the runtime within the slow
6099 // path.
6100 //
6101 // Note that `ref` currently does not get unpoisoned (when heap
6102 // poisoning is enabled), which is alright as the `ref` argument is
6103 // not used by the artReadBarrierSlow entry point.
6104 //
6105 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6106 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6107 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6108 AddSlowPath(slow_path);
6109
Roland Levillain3b359c72015-11-17 19:35:12 +00006110 __ b(slow_path->GetEntryLabel());
6111 __ Bind(slow_path->GetExitLabel());
6112}
6113
Roland Levillainc9285912015-12-18 10:38:42 +00006114void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6115 Location out,
6116 Location ref,
6117 Location obj,
6118 uint32_t offset,
6119 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006120 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006121 // Baker's read barriers shall be handled by the fast path
6122 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6123 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006124 // If heap poisoning is enabled, unpoisoning will be taken care of
6125 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006126 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006127 } else if (kPoisonHeapReferences) {
6128 __ UnpoisonHeapReference(out.AsRegister<Register>());
6129 }
6130}
6131
Roland Levillainc9285912015-12-18 10:38:42 +00006132void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6133 Location out,
6134 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006135 DCHECK(kEmitCompilerReadBarrier);
6136
Roland Levillainc9285912015-12-18 10:38:42 +00006137 // Insert a slow path based read barrier *after* the GC root load.
6138 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006139 // Note that GC roots are not affected by heap poisoning, so we do
6140 // not need to do anything special for this here.
6141 SlowPathCode* slow_path =
6142 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6143 AddSlowPath(slow_path);
6144
Roland Levillain3b359c72015-11-17 19:35:12 +00006145 __ b(slow_path->GetEntryLabel());
6146 __ Bind(slow_path->GetExitLabel());
6147}
6148
Vladimir Markodc151b22015-10-15 18:02:30 +01006149HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6150 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6151 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006152 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6153 // We disable pc-relative load when there is an irreducible loop, as the optimization
6154 // is incompatible with it.
6155 if (GetGraph()->HasIrreducibleLoops() &&
6156 (dispatch_info.method_load_kind ==
6157 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6158 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6159 }
6160
6161 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006162 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6163 if (&outer_dex_file != target_method.dex_file) {
6164 // Calls across dex files are more likely to exceed the available BL range,
6165 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6166 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6167 (desired_dispatch_info.method_load_kind ==
6168 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6169 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6170 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6171 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006172 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006173 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006174 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006175 0u
6176 };
6177 }
6178 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006179 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006180}
6181
Vladimir Markob4536b72015-11-24 13:45:23 +00006182Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6183 Register temp) {
6184 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6185 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6186 if (!invoke->GetLocations()->Intrinsified()) {
6187 return location.AsRegister<Register>();
6188 }
6189 // For intrinsics we allow any location, so it may be on the stack.
6190 if (!location.IsRegister()) {
6191 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6192 return temp;
6193 }
6194 // For register locations, check if the register was saved. If so, get it from the stack.
6195 // Note: There is a chance that the register was saved but not overwritten, so we could
6196 // save one load. However, since this is just an intrinsic slow path we prefer this
6197 // simple and more robust approach rather that trying to determine if that's the case.
6198 SlowPathCode* slow_path = GetCurrentSlowPath();
6199 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6200 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6201 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6202 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6203 return temp;
6204 }
6205 return location.AsRegister<Register>();
6206}
6207
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006208void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006209 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006210 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006211 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6212 // LR = code address from literal pool with link-time patch.
6213 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006214 break;
6215 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6216 // LR = invoke->GetDirectCodePtr();
6217 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006218 break;
6219 default:
6220 break;
6221 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006222
Vladimir Marko58155012015-08-19 12:49:41 +00006223 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6224 switch (invoke->GetMethodLoadKind()) {
6225 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6226 // temp = thread->string_init_entrypoint
6227 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6228 break;
6229 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006230 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006231 break;
6232 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6233 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6234 break;
6235 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6236 __ LoadLiteral(temp.AsRegister<Register>(),
6237 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6238 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006239 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6240 HArmDexCacheArraysBase* base =
6241 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6242 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6243 temp.AsRegister<Register>());
6244 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6245 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6246 break;
6247 }
Vladimir Marko58155012015-08-19 12:49:41 +00006248 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006249 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006250 Register method_reg;
6251 Register reg = temp.AsRegister<Register>();
6252 if (current_method.IsRegister()) {
6253 method_reg = current_method.AsRegister<Register>();
6254 } else {
6255 DCHECK(invoke->GetLocations()->Intrinsified());
6256 DCHECK(!current_method.IsValid());
6257 method_reg = reg;
6258 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6259 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006260 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6261 __ LoadFromOffset(kLoadWord,
6262 reg,
6263 method_reg,
6264 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006265 // temp = temp[index_in_cache]
6266 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6267 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6268 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006269 }
Vladimir Marko58155012015-08-19 12:49:41 +00006270 }
6271
6272 switch (invoke->GetCodePtrLocation()) {
6273 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6274 __ bl(GetFrameEntryLabel());
6275 break;
6276 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006277 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006278 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006279 // Arbitrarily branch to the BL itself, override at link time.
6280 __ bl(&relative_call_patches_.back().label);
6281 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006282 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6283 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6284 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006285 // LR()
6286 __ blx(LR);
6287 break;
6288 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6289 // LR = callee_method->entry_point_from_quick_compiled_code_
6290 __ LoadFromOffset(
6291 kLoadWord, LR, callee_method.AsRegister<Register>(),
6292 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6293 // LR()
6294 __ blx(LR);
6295 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006296 }
6297
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006298 DCHECK(!IsLeafMethod());
6299}
6300
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006301void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6302 Register temp = temp_location.AsRegister<Register>();
6303 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6304 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006305
6306 // Use the calling convention instead of the location of the receiver, as
6307 // intrinsics may have put the receiver in a different register. In the intrinsics
6308 // slow path, the arguments have been moved to the right place, so here we are
6309 // guaranteed that the receiver is the first register of the calling convention.
6310 InvokeDexCallingConvention calling_convention;
6311 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006312 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006313 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006314 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006315 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006316 // Instead of simply (possibly) unpoisoning `temp` here, we should
6317 // emit a read barrier for the previous class reference load.
6318 // However this is not required in practice, as this is an
6319 // intermediate/temporary reference and because the current
6320 // concurrent copying collector keeps the from-space memory
6321 // intact/accessible until the end of the marking phase (the
6322 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006323 __ MaybeUnpoisonHeapReference(temp);
6324 // temp = temp->GetMethodAt(method_offset);
6325 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6326 kArmWordSize).Int32Value();
6327 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6328 // LR = temp->GetEntryPoint();
6329 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6330 // LR();
6331 __ blx(LR);
6332}
6333
Vladimir Marko58155012015-08-19 12:49:41 +00006334void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6335 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006336 size_t size =
6337 method_patches_.size() +
6338 call_patches_.size() +
6339 relative_call_patches_.size() +
6340 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006341 linker_patches->reserve(size);
6342 for (const auto& entry : method_patches_) {
6343 const MethodReference& target_method = entry.first;
6344 Literal* literal = entry.second;
6345 DCHECK(literal->GetLabel()->IsBound());
6346 uint32_t literal_offset = literal->GetLabel()->Position();
6347 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6348 target_method.dex_file,
6349 target_method.dex_method_index));
6350 }
6351 for (const auto& entry : call_patches_) {
6352 const MethodReference& target_method = entry.first;
6353 Literal* literal = entry.second;
6354 DCHECK(literal->GetLabel()->IsBound());
6355 uint32_t literal_offset = literal->GetLabel()->Position();
6356 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6357 target_method.dex_file,
6358 target_method.dex_method_index));
6359 }
6360 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6361 uint32_t literal_offset = info.label.Position();
6362 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6363 info.target_method.dex_file,
6364 info.target_method.dex_method_index));
6365 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006366 for (const auto& pair : dex_cache_arrays_base_labels_) {
6367 HArmDexCacheArraysBase* base = pair.first;
6368 const DexCacheArraysBaseLabels* labels = &pair.second;
6369 const DexFile& dex_file = base->GetDexFile();
6370 size_t base_element_offset = base->GetElementOffset();
6371 DCHECK(labels->add_pc_label.IsBound());
6372 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6373 // Add MOVW patch.
6374 DCHECK(labels->movw_label.IsBound());
6375 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6376 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6377 &dex_file,
6378 add_pc_offset,
6379 base_element_offset));
6380 // Add MOVT patch.
6381 DCHECK(labels->movt_label.IsBound());
6382 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6383 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6384 &dex_file,
6385 add_pc_offset,
6386 base_element_offset));
6387 }
Vladimir Marko58155012015-08-19 12:49:41 +00006388}
6389
6390Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6391 MethodToLiteralMap* map) {
6392 // Look up the literal for target_method.
6393 auto lb = map->lower_bound(target_method);
6394 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6395 return lb->second;
6396 }
6397 // We don't have a literal for this method yet, insert a new one.
6398 Literal* literal = __ NewLiteral<uint32_t>(0u);
6399 map->PutBefore(lb, target_method, literal);
6400 return literal;
6401}
6402
6403Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6404 return DeduplicateMethodLiteral(target_method, &method_patches_);
6405}
6406
6407Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6408 return DeduplicateMethodLiteral(target_method, &call_patches_);
6409}
6410
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006411void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006412 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006413 LOG(FATAL) << "Unreachable";
6414}
6415
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006416void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006417 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006418 LOG(FATAL) << "Unreachable";
6419}
6420
Mark Mendellfe57faa2015-09-18 09:26:15 -04006421// Simple implementation of packed switch - generate cascaded compare/jumps.
6422void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6423 LocationSummary* locations =
6424 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6425 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006426 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006427 codegen_->GetAssembler()->IsThumb()) {
6428 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6429 if (switch_instr->GetStartValue() != 0) {
6430 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6431 }
6432 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006433}
6434
6435void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6436 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006437 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006438 LocationSummary* locations = switch_instr->GetLocations();
6439 Register value_reg = locations->InAt(0).AsRegister<Register>();
6440 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6441
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006442 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006443 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006444 Register temp_reg = IP;
6445 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6446 // the immediate, because IP is used as the destination register. For the other
6447 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6448 // and they can be encoded in the instruction without making use of IP register.
6449 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6450
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006451 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006452 // Jump to successors[0] if value == lower_bound.
6453 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6454 int32_t last_index = 0;
6455 for (; num_entries - last_index > 2; last_index += 2) {
6456 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6457 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6458 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6459 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6460 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6461 }
6462 if (num_entries - last_index == 2) {
6463 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006464 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006465 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006466 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006467
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006468 // And the default for any other value.
6469 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6470 __ b(codegen_->GetLabelOf(default_block));
6471 }
6472 } else {
6473 // Create a table lookup.
6474 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6475
6476 // Materialize a pointer to the switch table
6477 std::vector<Label*> labels(num_entries);
6478 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6479 for (uint32_t i = 0; i < num_entries; i++) {
6480 labels[i] = codegen_->GetLabelOf(successors[i]);
6481 }
6482 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6483
6484 // Remove the bias.
6485 Register key_reg;
6486 if (lower_bound != 0) {
6487 key_reg = locations->GetTemp(1).AsRegister<Register>();
6488 __ AddConstant(key_reg, value_reg, -lower_bound);
6489 } else {
6490 key_reg = value_reg;
6491 }
6492
6493 // Check whether the value is in the table, jump to default block if not.
6494 __ CmpConstant(key_reg, num_entries - 1);
6495 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6496
6497 // Load the displacement from the table.
6498 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6499
6500 // Dispatch is a direct add to the PC (for Thumb2).
6501 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006502 }
6503}
6504
Vladimir Markob4536b72015-11-24 13:45:23 +00006505void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6507 locations->SetOut(Location::RequiresRegister());
6508 codegen_->AddDexCacheArraysBase(base);
6509}
6510
6511void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6512 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6513 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6514 __ BindTrackedLabel(&labels->movw_label);
6515 __ movw(base_reg, 0u);
6516 __ BindTrackedLabel(&labels->movt_label);
6517 __ movt(base_reg, 0u);
6518 __ BindTrackedLabel(&labels->add_pc_label);
6519 __ add(base_reg, base_reg, ShifterOperand(PC));
6520}
6521
Andreas Gampe85b62f22015-09-09 13:15:38 -07006522void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6523 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006524 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006525 return;
6526 }
6527
6528 DCHECK_NE(type, Primitive::kPrimVoid);
6529
6530 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6531 if (return_loc.Equals(trg)) {
6532 return;
6533 }
6534
6535 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6536 // with the last branch.
6537 if (type == Primitive::kPrimLong) {
6538 HParallelMove parallel_move(GetGraph()->GetArena());
6539 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6540 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6541 GetMoveResolver()->EmitNativeCode(&parallel_move);
6542 } else if (type == Primitive::kPrimDouble) {
6543 HParallelMove parallel_move(GetGraph()->GetArena());
6544 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6545 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6546 GetMoveResolver()->EmitNativeCode(&parallel_move);
6547 } else {
6548 // Let the parallel move resolver take care of all of this.
6549 HParallelMove parallel_move(GetGraph()->GetArena());
6550 parallel_move.AddMove(return_loc, trg, type, nullptr);
6551 GetMoveResolver()->EmitNativeCode(&parallel_move);
6552 }
6553}
6554
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006555void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
6556 LocationSummary* locations =
6557 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6558 locations->SetInAt(0, Location::RequiresRegister());
6559 locations->SetOut(Location::RequiresRegister());
6560}
6561
6562void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
6563 LocationSummary* locations = instruction->GetLocations();
6564 uint32_t method_offset = 0;
6565 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
6566 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6567 instruction->GetIndex(), kArmPointerSize).SizeValue();
6568 } else {
6569 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
6570 instruction->GetIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
6571 }
6572 __ LoadFromOffset(kLoadWord,
6573 locations->Out().AsRegister<Register>(),
6574 locations->InAt(0).AsRegister<Register>(),
6575 method_offset);
6576}
6577
Roland Levillain4d027112015-07-01 15:41:14 +01006578#undef __
6579#undef QUICK_ENTRY_POINT
6580
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006581} // namespace arm
6582} // namespace art