blob: cdee35576ae22904b4e355fdca331820dca3e3de [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
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100729void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100730 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100731}
732
733void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100734 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735}
736
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100737size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
738 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
739 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100740}
741
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100742size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
743 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
744 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100745}
746
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000747size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
748 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
749 return kArmWordSize;
750}
751
752size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
753 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
754 return kArmWordSize;
755}
756
Calin Juravle34166012014-12-19 17:22:29 +0000757CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000758 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100759 const CompilerOptions& compiler_options,
760 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000761 : CodeGenerator(graph,
762 kNumberOfCoreRegisters,
763 kNumberOfSRegisters,
764 kNumberOfRegisterPairs,
765 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
766 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000767 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
768 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100769 compiler_options,
770 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100771 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100772 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100773 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100774 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000775 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000776 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100777 method_patches_(MethodReferenceComparator(),
778 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
779 call_patches_(MethodReferenceComparator(),
780 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000781 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
782 dex_cache_arrays_base_labels_(std::less<HArmDexCacheArraysBase*>(),
783 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700784 // Always save the LR register to mimic Quick.
785 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100786}
787
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000788void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
789 // Ensure that we fix up branches and literal loads and emit the literal pool.
790 __ FinalizeCode();
791
792 // Adjust native pc offsets in stack maps.
793 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
794 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
795 uint32_t new_position = __ GetAdjustedPosition(old_position);
796 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
797 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100798 // Adjust pc offsets for the disassembly information.
799 if (disasm_info_ != nullptr) {
800 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
801 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
802 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
803 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
804 it.second.start = __ GetAdjustedPosition(it.second.start);
805 it.second.end = __ GetAdjustedPosition(it.second.end);
806 }
807 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
808 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
809 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
810 }
811 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000812
813 CodeGenerator::Finalize(allocator);
814}
815
David Brazdil58282f42016-01-14 12:45:10 +0000816void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100817 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100818 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819
820 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100821 blocked_core_registers_[SP] = true;
822 blocked_core_registers_[LR] = true;
823 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100824
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100825 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100826 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100827
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100828 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100829 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100830
David Brazdil58282f42016-01-14 12:45:10 +0000831 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100832 // Stubs do not save callee-save floating point registers. If the graph
833 // is debuggable, we need to deal with these registers differently. For
834 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000835 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
836 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
837 }
838 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100839
840 UpdateBlockedPairRegisters();
841}
842
843void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
844 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
845 ArmManagedRegister current =
846 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
847 if (blocked_core_registers_[current.AsRegisterPairLow()]
848 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
849 blocked_register_pairs_[i] = true;
850 }
851 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100852}
853
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100854InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800855 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100856 assembler_(codegen->GetAssembler()),
857 codegen_(codegen) {}
858
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000859void CodeGeneratorARM::ComputeSpillMask() {
860 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
861 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000862 // There is no easy instruction to restore just the PC on thumb2. We spill and
863 // restore another arbitrary register.
864 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000865 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
866 // We use vpush and vpop for saving and restoring floating point registers, which take
867 // a SRegister and the number of registers to save/restore after that SRegister. We
868 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
869 // but in the range.
870 if (fpu_spill_mask_ != 0) {
871 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
872 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
873 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
874 fpu_spill_mask_ |= (1 << i);
875 }
876 }
877}
878
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100879static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100880 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100881}
882
883static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100884 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100885}
886
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000887void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000888 bool skip_overflow_check =
889 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000890 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000891 __ Bind(&frame_entry_label_);
892
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000893 if (HasEmptyFrame()) {
894 return;
895 }
896
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100897 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000898 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
899 __ LoadFromOffset(kLoadWord, IP, IP, 0);
900 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100901 }
902
Andreas Gampe501fd632015-09-10 16:11:06 -0700903 __ PushList(core_spill_mask_);
904 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
905 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000906 if (fpu_spill_mask_ != 0) {
907 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
908 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100909 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100910 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000911 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100912 int adjust = GetFrameSize() - FrameEntrySpillSize();
913 __ AddConstant(SP, -adjust);
914 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100915 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000916}
917
918void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000919 if (HasEmptyFrame()) {
920 __ bx(LR);
921 return;
922 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100923 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100924 int adjust = GetFrameSize() - FrameEntrySpillSize();
925 __ AddConstant(SP, adjust);
926 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000927 if (fpu_spill_mask_ != 0) {
928 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
929 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
931 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000932 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700933 // Pop LR into PC to return.
934 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
935 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
936 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100937 __ cfi().RestoreState();
938 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000939}
940
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100941void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700942 Label* label = GetLabelOf(block);
943 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000944}
945
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100946Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
947 switch (load->GetType()) {
948 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100949 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100950 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100951
952 case Primitive::kPrimInt:
953 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100954 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100955 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100956
957 case Primitive::kPrimBoolean:
958 case Primitive::kPrimByte:
959 case Primitive::kPrimChar:
960 case Primitive::kPrimShort:
961 case Primitive::kPrimVoid:
962 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700963 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100964 }
965
966 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700967 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100968}
969
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100970Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100971 switch (type) {
972 case Primitive::kPrimBoolean:
973 case Primitive::kPrimByte:
974 case Primitive::kPrimChar:
975 case Primitive::kPrimShort:
976 case Primitive::kPrimInt:
977 case Primitive::kPrimNot: {
978 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000979 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100980 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100981 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100982 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000983 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100984 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100985 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100986
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000987 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100988 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000989 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100990 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000991 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100992 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000993 if (calling_convention.GetRegisterAt(index) == R1) {
994 // Skip R1, and use R2_R3 instead.
995 gp_index_++;
996 index++;
997 }
998 }
999 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1000 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001001 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001002
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001003 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001004 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001006 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1007 }
1008 }
1009
1010 case Primitive::kPrimFloat: {
1011 uint32_t stack_index = stack_index_++;
1012 if (float_index_ % 2 == 0) {
1013 float_index_ = std::max(double_index_, float_index_);
1014 }
1015 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1016 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1017 } else {
1018 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1019 }
1020 }
1021
1022 case Primitive::kPrimDouble: {
1023 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1024 uint32_t stack_index = stack_index_;
1025 stack_index_ += 2;
1026 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1027 uint32_t index = double_index_;
1028 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001029 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001030 calling_convention.GetFpuRegisterAt(index),
1031 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001032 DCHECK(ExpectedPairLayout(result));
1033 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001034 } else {
1035 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001036 }
1037 }
1038
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001039 case Primitive::kPrimVoid:
1040 LOG(FATAL) << "Unexpected parameter type " << type;
1041 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001042 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001043 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001044}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001045
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001046Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001047 switch (type) {
1048 case Primitive::kPrimBoolean:
1049 case Primitive::kPrimByte:
1050 case Primitive::kPrimChar:
1051 case Primitive::kPrimShort:
1052 case Primitive::kPrimInt:
1053 case Primitive::kPrimNot: {
1054 return Location::RegisterLocation(R0);
1055 }
1056
1057 case Primitive::kPrimFloat: {
1058 return Location::FpuRegisterLocation(S0);
1059 }
1060
1061 case Primitive::kPrimLong: {
1062 return Location::RegisterPairLocation(R0, R1);
1063 }
1064
1065 case Primitive::kPrimDouble: {
1066 return Location::FpuRegisterPairLocation(S0, S1);
1067 }
1068
1069 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001070 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001071 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001072
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001073 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001074}
1075
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001076Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1077 return Location::RegisterLocation(kMethodRegisterArgument);
1078}
1079
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080void CodeGeneratorARM::Move32(Location destination, Location source) {
1081 if (source.Equals(destination)) {
1082 return;
1083 }
1084 if (destination.IsRegister()) {
1085 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001086 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001087 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001088 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001090 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 } else if (destination.IsFpuRegister()) {
1093 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001094 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001096 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001098 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001099 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001101 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001103 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001107 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001108 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1109 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110 }
1111 }
1112}
1113
1114void CodeGeneratorARM::Move64(Location destination, Location source) {
1115 if (source.Equals(destination)) {
1116 return;
1117 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001118 if (destination.IsRegisterPair()) {
1119 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001120 EmitParallelMoves(
1121 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1122 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001123 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001124 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001125 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1126 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001127 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001128 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001129 } else if (source.IsFpuRegisterPair()) {
1130 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1131 destination.AsRegisterPairHigh<Register>(),
1132 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001133 } else {
1134 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001135 DCHECK(ExpectedPairLayout(destination));
1136 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1137 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001138 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001139 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001140 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001141 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1142 SP,
1143 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001144 } else if (source.IsRegisterPair()) {
1145 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1146 source.AsRegisterPairLow<Register>(),
1147 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001149 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001150 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
1152 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001154 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 if (source.AsRegisterPairLow<Register>() == R1) {
1156 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001157 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1158 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001160 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 SP, destination.GetStackIndex());
1162 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001163 } else if (source.IsFpuRegisterPair()) {
1164 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1165 SP,
1166 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 } else {
1168 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001169 EmitParallelMoves(
1170 Location::StackSlot(source.GetStackIndex()),
1171 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001172 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001173 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001174 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1175 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001176 }
1177 }
1178}
1179
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001180void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001181 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001182 if (instruction->IsCurrentMethod()) {
1183 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1184 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001185 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001186 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001187 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001188 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1189 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +00001190 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001191 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001192 } else {
1193 DCHECK(location.IsStackSlot());
1194 __ LoadImmediate(IP, value);
1195 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1196 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001197 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +00001198 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +00001199 int64_t value = const_to_move->AsLongConstant()->GetValue();
1200 if (location.IsRegisterPair()) {
1201 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
1202 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
1203 } else {
1204 DCHECK(location.IsDoubleStackSlot());
1205 __ LoadImmediate(IP, Low32Bits(value));
1206 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1207 __ LoadImmediate(IP, High32Bits(value));
1208 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
1209 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001210 }
Roland Levillain476df552014-10-09 17:51:36 +01001211 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1213 switch (instruction->GetType()) {
1214 case Primitive::kPrimBoolean:
1215 case Primitive::kPrimByte:
1216 case Primitive::kPrimChar:
1217 case Primitive::kPrimShort:
1218 case Primitive::kPrimInt:
1219 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 Move32(location, Location::StackSlot(stack_slot));
1222 break;
1223
1224 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001225 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 Move64(location, Location::DoubleStackSlot(stack_slot));
1227 break;
1228
1229 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001231 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001232 } else if (instruction->IsTemporary()) {
1233 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001234 if (temp_location.IsStackSlot()) {
1235 Move32(location, temp_location);
1236 } else {
1237 DCHECK(temp_location.IsDoubleStackSlot());
1238 Move64(location, temp_location);
1239 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001240 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001241 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242 switch (instruction->GetType()) {
1243 case Primitive::kPrimBoolean:
1244 case Primitive::kPrimByte:
1245 case Primitive::kPrimChar:
1246 case Primitive::kPrimShort:
1247 case Primitive::kPrimNot:
1248 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001249 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001250 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 break;
1252
1253 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001254 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001255 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001256 break;
1257
1258 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001259 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001261 }
1262}
1263
Calin Juravle175dc732015-08-25 15:42:32 +01001264void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1265 DCHECK(location.IsRegister());
1266 __ LoadImmediate(location.AsRegister<Register>(), value);
1267}
1268
Calin Juravlee460d1d2015-09-29 04:52:17 +01001269void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1270 if (Primitive::Is64BitType(dst_type)) {
1271 Move64(dst, src);
1272 } else {
1273 Move32(dst, src);
1274 }
1275}
1276
1277void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1278 if (location.IsRegister()) {
1279 locations->AddTemp(location);
1280 } else if (location.IsRegisterPair()) {
1281 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1282 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1283 } else {
1284 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1285 }
1286}
1287
Calin Juravle175dc732015-08-25 15:42:32 +01001288void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1289 HInstruction* instruction,
1290 uint32_t dex_pc,
1291 SlowPathCode* slow_path) {
1292 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1293 instruction,
1294 dex_pc,
1295 slow_path);
1296}
1297
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001298void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1299 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001300 uint32_t dex_pc,
1301 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001302 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001303 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1304 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001305 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001306}
1307
David Brazdilfc6a86a2015-06-26 10:33:45 +00001308void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001309 DCHECK(!successor->IsExitBlock());
1310
1311 HBasicBlock* block = got->GetBlock();
1312 HInstruction* previous = got->GetPrevious();
1313
1314 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001315 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001316 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1317 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1318 return;
1319 }
1320
1321 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1322 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1323 }
1324 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001325 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001326 }
1327}
1328
David Brazdilfc6a86a2015-06-26 10:33:45 +00001329void LocationsBuilderARM::VisitGoto(HGoto* got) {
1330 got->SetLocations(nullptr);
1331}
1332
1333void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1334 HandleGoto(got, got->GetSuccessor());
1335}
1336
1337void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1338 try_boundary->SetLocations(nullptr);
1339}
1340
1341void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1342 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1343 if (!successor->IsExitBlock()) {
1344 HandleGoto(try_boundary, successor);
1345 }
1346}
1347
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001348void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001349 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001350}
1351
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001352void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001353}
1354
Roland Levillain4fa13f62015-07-06 18:11:54 +01001355void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1356 Label* true_label,
1357 Label* false_label) {
1358 __ vmstat(); // transfer FP status register to ARM APSR.
Aart Bike9f37602015-10-09 11:15:55 -07001359 // TODO: merge into a single branch (except "equal or unordered" and "not equal")
Roland Levillain4fa13f62015-07-06 18:11:54 +01001360 if (cond->IsFPConditionTrueIfNaN()) {
1361 __ b(true_label, VS); // VS for unordered.
1362 } else if (cond->IsFPConditionFalseIfNaN()) {
1363 __ b(false_label, VS); // VS for unordered.
1364 }
Aart Bike9f37602015-10-09 11:15:55 -07001365 __ b(true_label, ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001366}
1367
1368void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1369 Label* true_label,
1370 Label* false_label) {
1371 LocationSummary* locations = cond->GetLocations();
1372 Location left = locations->InAt(0);
1373 Location right = locations->InAt(1);
1374 IfCondition if_cond = cond->GetCondition();
1375
1376 Register left_high = left.AsRegisterPairHigh<Register>();
1377 Register left_low = left.AsRegisterPairLow<Register>();
1378 IfCondition true_high_cond = if_cond;
1379 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001380 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001381
1382 // Set the conditions for the test, remembering that == needs to be
1383 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001384 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001385 switch (if_cond) {
1386 case kCondEQ:
1387 case kCondNE:
1388 // Nothing to do.
1389 break;
1390 case kCondLT:
1391 false_high_cond = kCondGT;
1392 break;
1393 case kCondLE:
1394 true_high_cond = kCondLT;
1395 break;
1396 case kCondGT:
1397 false_high_cond = kCondLT;
1398 break;
1399 case kCondGE:
1400 true_high_cond = kCondGT;
1401 break;
Aart Bike9f37602015-10-09 11:15:55 -07001402 case kCondB:
1403 false_high_cond = kCondA;
1404 break;
1405 case kCondBE:
1406 true_high_cond = kCondB;
1407 break;
1408 case kCondA:
1409 false_high_cond = kCondB;
1410 break;
1411 case kCondAE:
1412 true_high_cond = kCondA;
1413 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001414 }
1415 if (right.IsConstant()) {
1416 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1417 int32_t val_low = Low32Bits(value);
1418 int32_t val_high = High32Bits(value);
1419
Vladimir Markoac6ac102015-12-17 12:14:00 +00001420 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001421 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001422 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001423 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001424 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001425 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001426 __ b(true_label, ARMCondition(true_high_cond));
1427 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001428 }
1429 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001430 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001431 } else {
1432 Register right_high = right.AsRegisterPairHigh<Register>();
1433 Register right_low = right.AsRegisterPairLow<Register>();
1434
1435 __ cmp(left_high, ShifterOperand(right_high));
1436 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001437 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001438 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001439 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001440 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001441 __ b(true_label, ARMCondition(true_high_cond));
1442 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001443 }
1444 // Must be equal high, so compare the lows.
1445 __ cmp(left_low, ShifterOperand(right_low));
1446 }
1447 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001448 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001449 __ b(true_label, final_condition);
1450}
1451
David Brazdil0debae72015-11-12 18:37:00 +00001452void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1453 Label* true_target_in,
1454 Label* false_target_in) {
1455 // Generated branching requires both targets to be explicit. If either of the
1456 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1457 Label fallthrough_target;
1458 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1459 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1460
Roland Levillain4fa13f62015-07-06 18:11:54 +01001461 LocationSummary* locations = condition->GetLocations();
1462 Location left = locations->InAt(0);
1463 Location right = locations->InAt(1);
1464
Roland Levillain4fa13f62015-07-06 18:11:54 +01001465 Primitive::Type type = condition->InputAt(0)->GetType();
1466 switch (type) {
1467 case Primitive::kPrimLong:
1468 GenerateLongComparesAndJumps(condition, true_target, false_target);
1469 break;
1470 case Primitive::kPrimFloat:
1471 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1472 GenerateFPJumps(condition, true_target, false_target);
1473 break;
1474 case Primitive::kPrimDouble:
1475 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1476 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1477 GenerateFPJumps(condition, true_target, false_target);
1478 break;
1479 default:
1480 LOG(FATAL) << "Unexpected compare type " << type;
1481 }
1482
David Brazdil0debae72015-11-12 18:37:00 +00001483 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001484 __ b(false_target);
1485 }
David Brazdil0debae72015-11-12 18:37:00 +00001486
1487 if (fallthrough_target.IsLinked()) {
1488 __ Bind(&fallthrough_target);
1489 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001490}
1491
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001492void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001493 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001494 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001495 Label* false_target) {
1496 HInstruction* cond = instruction->InputAt(condition_input_index);
1497
1498 if (true_target == nullptr && false_target == nullptr) {
1499 // Nothing to do. The code always falls through.
1500 return;
1501 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001502 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001503 if (cond->AsIntConstant()->IsOne()) {
1504 if (true_target != nullptr) {
1505 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001506 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001507 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001508 DCHECK(cond->AsIntConstant()->IsZero());
1509 if (false_target != nullptr) {
1510 __ b(false_target);
1511 }
1512 }
1513 return;
1514 }
1515
1516 // The following code generates these patterns:
1517 // (1) true_target == nullptr && false_target != nullptr
1518 // - opposite condition true => branch to false_target
1519 // (2) true_target != nullptr && false_target == nullptr
1520 // - condition true => branch to true_target
1521 // (3) true_target != nullptr && false_target != nullptr
1522 // - condition true => branch to true_target
1523 // - branch to false_target
1524 if (IsBooleanValueOrMaterializedCondition(cond)) {
1525 // Condition has been materialized, compare the output to 0.
1526 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1527 DCHECK(cond_val.IsRegister());
1528 if (true_target == nullptr) {
1529 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1530 } else {
1531 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001532 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001533 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001534 // Condition has not been materialized. Use its inputs as the comparison and
1535 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001536 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001537
1538 // If this is a long or FP comparison that has been folded into
1539 // the HCondition, generate the comparison directly.
1540 Primitive::Type type = condition->InputAt(0)->GetType();
1541 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1542 GenerateCompareTestAndBranch(condition, true_target, false_target);
1543 return;
1544 }
1545
1546 LocationSummary* locations = cond->GetLocations();
1547 DCHECK(locations->InAt(0).IsRegister());
1548 Register left = locations->InAt(0).AsRegister<Register>();
1549 Location right = locations->InAt(1);
1550 if (right.IsRegister()) {
1551 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001552 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001553 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001554 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001555 }
1556 if (true_target == nullptr) {
1557 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1558 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001559 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001560 }
Dave Allison20dfc792014-06-16 20:44:29 -07001561 }
David Brazdil0debae72015-11-12 18:37:00 +00001562
1563 // If neither branch falls through (case 3), the conditional branch to `true_target`
1564 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1565 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001566 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001567 }
1568}
1569
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001570void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1572 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001573 locations->SetInAt(0, Location::RequiresRegister());
1574 }
1575}
1576
1577void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001578 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1579 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1580 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1581 nullptr : codegen_->GetLabelOf(true_successor);
1582 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1583 nullptr : codegen_->GetLabelOf(false_successor);
1584 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001585}
1586
1587void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1588 LocationSummary* locations = new (GetGraph()->GetArena())
1589 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001590 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001591 locations->SetInAt(0, Location::RequiresRegister());
1592 }
1593}
1594
1595void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001596 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001597 GenerateTestAndBranch(deoptimize,
1598 /* condition_input_index */ 0,
1599 slow_path->GetEntryLabel(),
1600 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001601}
Dave Allison20dfc792014-06-16 20:44:29 -07001602
David Srbecky0cf44932015-12-09 14:09:59 +00001603void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1604 new (GetGraph()->GetArena()) LocationSummary(info);
1605}
1606
1607void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001608 if (codegen_->HasStackMapAtCurrentPc()) {
1609 // Ensure that we do not collide with the stack map of the previous instruction.
1610 __ nop();
1611 }
David Srbecky0cf44932015-12-09 14:09:59 +00001612 codegen_->RecordPcInfo(info, info->GetDexPc());
1613}
1614
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001615void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001616 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001617 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001618 // Handle the long/FP comparisons made in instruction simplification.
1619 switch (cond->InputAt(0)->GetType()) {
1620 case Primitive::kPrimLong:
1621 locations->SetInAt(0, Location::RequiresRegister());
1622 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1623 if (cond->NeedsMaterialization()) {
1624 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1625 }
1626 break;
1627
1628 case Primitive::kPrimFloat:
1629 case Primitive::kPrimDouble:
1630 locations->SetInAt(0, Location::RequiresFpuRegister());
1631 locations->SetInAt(1, Location::RequiresFpuRegister());
1632 if (cond->NeedsMaterialization()) {
1633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1634 }
1635 break;
1636
1637 default:
1638 locations->SetInAt(0, Location::RequiresRegister());
1639 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1640 if (cond->NeedsMaterialization()) {
1641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1642 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001643 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001644}
1645
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001646void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001647 if (!cond->NeedsMaterialization()) {
1648 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001649 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001650
1651 LocationSummary* locations = cond->GetLocations();
1652 Location left = locations->InAt(0);
1653 Location right = locations->InAt(1);
1654 Register out = locations->Out().AsRegister<Register>();
1655 Label true_label, false_label;
1656
1657 switch (cond->InputAt(0)->GetType()) {
1658 default: {
1659 // Integer case.
1660 if (right.IsRegister()) {
1661 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1662 } else {
1663 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001664 __ CmpConstant(left.AsRegister<Register>(),
1665 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001666 }
Aart Bike9f37602015-10-09 11:15:55 -07001667 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001668 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001669 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001670 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001671 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001672 return;
1673 }
1674 case Primitive::kPrimLong:
1675 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1676 break;
1677 case Primitive::kPrimFloat:
1678 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1679 GenerateFPJumps(cond, &true_label, &false_label);
1680 break;
1681 case Primitive::kPrimDouble:
1682 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1683 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1684 GenerateFPJumps(cond, &true_label, &false_label);
1685 break;
1686 }
1687
1688 // Convert the jumps into the result.
1689 Label done_label;
1690
1691 // False case: result = 0.
1692 __ Bind(&false_label);
1693 __ LoadImmediate(out, 0);
1694 __ b(&done_label);
1695
1696 // True case: result = 1.
1697 __ Bind(&true_label);
1698 __ LoadImmediate(out, 1);
1699 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001700}
1701
1702void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001703 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001704}
1705
1706void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001707 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001708}
1709
1710void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001711 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001712}
1713
1714void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001715 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001716}
1717
1718void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001719 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001720}
1721
1722void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001723 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001724}
1725
1726void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001727 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001728}
1729
1730void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001731 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001732}
1733
1734void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001735 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001736}
1737
1738void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001739 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001740}
1741
1742void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001743 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001744}
1745
1746void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001747 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001748}
1749
Aart Bike9f37602015-10-09 11:15:55 -07001750void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001751 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001752}
1753
1754void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001755 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001756}
1757
1758void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001759 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001760}
1761
1762void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001764}
1765
1766void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001768}
1769
1770void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001771 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001772}
1773
1774void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001776}
1777
1778void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001780}
1781
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001782void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001784}
1785
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001786void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1787 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001788}
1789
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001790void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001791 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001792}
1793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001794void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001795 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796}
1797
1798void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001799 LocationSummary* locations =
1800 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001801 switch (store->InputAt(1)->GetType()) {
1802 case Primitive::kPrimBoolean:
1803 case Primitive::kPrimByte:
1804 case Primitive::kPrimChar:
1805 case Primitive::kPrimShort:
1806 case Primitive::kPrimInt:
1807 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001808 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001809 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1810 break;
1811
1812 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001813 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1815 break;
1816
1817 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001818 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001819 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001820}
1821
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001822void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001823}
1824
1825void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001826 LocationSummary* locations =
1827 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001828 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001829}
1830
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001831void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001832 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001833}
1834
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001835void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1836 LocationSummary* locations =
1837 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1838 locations->SetOut(Location::ConstantLocation(constant));
1839}
1840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001841void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001842 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001843}
1844
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001846 LocationSummary* locations =
1847 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001848 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001849}
1850
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001851void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001852 // Will be generated at use site.
1853}
1854
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001855void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1856 LocationSummary* locations =
1857 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1858 locations->SetOut(Location::ConstantLocation(constant));
1859}
1860
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001861void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001862 // Will be generated at use site.
1863}
1864
1865void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1866 LocationSummary* locations =
1867 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1868 locations->SetOut(Location::ConstantLocation(constant));
1869}
1870
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001871void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001872 // Will be generated at use site.
1873}
1874
Calin Juravle27df7582015-04-17 19:12:31 +01001875void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1876 memory_barrier->SetLocations(nullptr);
1877}
1878
1879void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001880 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001881}
1882
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001884 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001885}
1886
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001887void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001888 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001889}
1890
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001891void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001892 LocationSummary* locations =
1893 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001894 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001895}
1896
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001897void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001898 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001899}
1900
Calin Juravle175dc732015-08-25 15:42:32 +01001901void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1902 // The trampoline uses the same calling convention as dex calling conventions,
1903 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1904 // the method_idx.
1905 HandleInvoke(invoke);
1906}
1907
1908void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1909 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1910}
1911
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001912void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001913 // Explicit clinit checks triggered by static invokes must have been pruned by
1914 // art::PrepareForRegisterAllocation.
1915 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001916
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)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001921 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1922 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1923 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001924 return;
1925 }
1926
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001927 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001928
1929 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1930 if (invoke->HasPcRelativeDexCache()) {
1931 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1932 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001933}
1934
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001935static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1936 if (invoke->GetLocations()->Intrinsified()) {
1937 IntrinsicCodeGeneratorARM intrinsic(codegen);
1938 intrinsic.Dispatch(invoke);
1939 return true;
1940 }
1941 return false;
1942}
1943
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001944void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001945 // Explicit clinit checks triggered by static invokes must have been pruned by
1946 // art::PrepareForRegisterAllocation.
1947 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001948
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001949 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1950 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001951 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001952
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001953 LocationSummary* locations = invoke->GetLocations();
1954 codegen_->GenerateStaticOrDirectCall(
1955 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001956 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001957}
1958
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001959void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001960 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001961 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001962}
1963
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001964void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001965 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001966 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001967 codegen_->GetInstructionSetFeatures());
1968 if (intrinsic.TryDispatch(invoke)) {
1969 return;
1970 }
1971
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001972 HandleInvoke(invoke);
1973}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001974
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001975void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001976 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1977 return;
1978 }
1979
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001980 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001981 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001982 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001983}
1984
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001985void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1986 HandleInvoke(invoke);
1987 // Add the hidden argument.
1988 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1989}
1990
1991void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1992 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001993 LocationSummary* locations = invoke->GetLocations();
1994 Register temp = locations->GetTemp(0).AsRegister<Register>();
1995 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001996 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1997 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001998 Location receiver = locations->InAt(0);
1999 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2000
Roland Levillain3b359c72015-11-17 19:35:12 +00002001 // Set the hidden argument. This is safe to do this here, as R12
2002 // won't be modified thereafter, before the `blx` (call) instruction.
2003 DCHECK_EQ(R12, hidden_reg);
2004 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002005
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002006 if (receiver.IsStackSlot()) {
2007 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002008 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002009 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2010 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002011 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002013 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002014 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002015 // Instead of simply (possibly) unpoisoning `temp` here, we should
2016 // emit a read barrier for the previous class reference load.
2017 // However this is not required in practice, as this is an
2018 // intermediate/temporary reference and because the current
2019 // concurrent copying collector keeps the from-space memory
2020 // intact/accessible until the end of the marking phase (the
2021 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002022 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002024 uint32_t entry_point =
2025 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002026 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2027 // LR = temp->GetEntryPoint();
2028 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2029 // LR();
2030 __ blx(LR);
2031 DCHECK(!codegen_->IsLeafMethod());
2032 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2033}
2034
Roland Levillain88cb1752014-10-20 16:36:47 +01002035void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2036 LocationSummary* locations =
2037 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2038 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002039 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002040 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002041 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2042 break;
2043 }
2044 case Primitive::kPrimLong: {
2045 locations->SetInAt(0, Location::RequiresRegister());
2046 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002047 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002048 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002049
Roland Levillain88cb1752014-10-20 16:36:47 +01002050 case Primitive::kPrimFloat:
2051 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002052 locations->SetInAt(0, Location::RequiresFpuRegister());
2053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2058 }
2059}
2060
2061void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2062 LocationSummary* locations = neg->GetLocations();
2063 Location out = locations->Out();
2064 Location in = locations->InAt(0);
2065 switch (neg->GetResultType()) {
2066 case Primitive::kPrimInt:
2067 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002068 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002069 break;
2070
2071 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002072 DCHECK(in.IsRegisterPair());
2073 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2074 __ rsbs(out.AsRegisterPairLow<Register>(),
2075 in.AsRegisterPairLow<Register>(),
2076 ShifterOperand(0));
2077 // We cannot emit an RSC (Reverse Subtract with Carry)
2078 // instruction here, as it does not exist in the Thumb-2
2079 // instruction set. We use the following approach
2080 // using SBC and SUB instead.
2081 //
2082 // out.hi = -C
2083 __ sbc(out.AsRegisterPairHigh<Register>(),
2084 out.AsRegisterPairHigh<Register>(),
2085 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2086 // out.hi = out.hi - in.hi
2087 __ sub(out.AsRegisterPairHigh<Register>(),
2088 out.AsRegisterPairHigh<Register>(),
2089 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2090 break;
2091
Roland Levillain88cb1752014-10-20 16:36:47 +01002092 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002093 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002095 break;
2096
Roland Levillain88cb1752014-10-20 16:36:47 +01002097 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002098 DCHECK(in.IsFpuRegisterPair());
2099 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2100 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002101 break;
2102
2103 default:
2104 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2105 }
2106}
2107
Roland Levillaindff1f282014-11-05 14:15:05 +00002108void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002109 Primitive::Type result_type = conversion->GetResultType();
2110 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002111 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002112
Roland Levillain5b3ee562015-04-14 16:02:41 +01002113 // The float-to-long, double-to-long and long-to-float type conversions
2114 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002115 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002116 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2117 && result_type == Primitive::kPrimLong)
2118 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002119 ? LocationSummary::kCall
2120 : LocationSummary::kNoCall;
2121 LocationSummary* locations =
2122 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2123
David Brazdilb2bd1c52015-03-25 11:17:37 +00002124 // The Java language does not allow treating boolean as an integral type but
2125 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002126
Roland Levillaindff1f282014-11-05 14:15:05 +00002127 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002128 case Primitive::kPrimByte:
2129 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002130 case Primitive::kPrimBoolean:
2131 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002132 case Primitive::kPrimShort:
2133 case Primitive::kPrimInt:
2134 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002135 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002136 locations->SetInAt(0, Location::RequiresRegister());
2137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2138 break;
2139
2140 default:
2141 LOG(FATAL) << "Unexpected type conversion from " << input_type
2142 << " to " << result_type;
2143 }
2144 break;
2145
Roland Levillain01a8d712014-11-14 16:27:39 +00002146 case Primitive::kPrimShort:
2147 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002148 case Primitive::kPrimBoolean:
2149 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002150 case Primitive::kPrimByte:
2151 case Primitive::kPrimInt:
2152 case Primitive::kPrimChar:
2153 // Processing a Dex `int-to-short' instruction.
2154 locations->SetInAt(0, Location::RequiresRegister());
2155 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2156 break;
2157
2158 default:
2159 LOG(FATAL) << "Unexpected type conversion from " << input_type
2160 << " to " << result_type;
2161 }
2162 break;
2163
Roland Levillain946e1432014-11-11 17:35:19 +00002164 case Primitive::kPrimInt:
2165 switch (input_type) {
2166 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002167 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002168 locations->SetInAt(0, Location::Any());
2169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2170 break;
2171
2172 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002173 // Processing a Dex `float-to-int' instruction.
2174 locations->SetInAt(0, Location::RequiresFpuRegister());
2175 locations->SetOut(Location::RequiresRegister());
2176 locations->AddTemp(Location::RequiresFpuRegister());
2177 break;
2178
Roland Levillain946e1432014-11-11 17:35:19 +00002179 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002180 // Processing a Dex `double-to-int' instruction.
2181 locations->SetInAt(0, Location::RequiresFpuRegister());
2182 locations->SetOut(Location::RequiresRegister());
2183 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002184 break;
2185
2186 default:
2187 LOG(FATAL) << "Unexpected type conversion from " << input_type
2188 << " to " << result_type;
2189 }
2190 break;
2191
Roland Levillaindff1f282014-11-05 14:15:05 +00002192 case Primitive::kPrimLong:
2193 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002194 case Primitive::kPrimBoolean:
2195 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002196 case Primitive::kPrimByte:
2197 case Primitive::kPrimShort:
2198 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002199 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002200 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002201 locations->SetInAt(0, Location::RequiresRegister());
2202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2203 break;
2204
Roland Levillain624279f2014-12-04 11:54:28 +00002205 case Primitive::kPrimFloat: {
2206 // Processing a Dex `float-to-long' instruction.
2207 InvokeRuntimeCallingConvention calling_convention;
2208 locations->SetInAt(0, Location::FpuRegisterLocation(
2209 calling_convention.GetFpuRegisterAt(0)));
2210 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2211 break;
2212 }
2213
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002214 case Primitive::kPrimDouble: {
2215 // Processing a Dex `double-to-long' instruction.
2216 InvokeRuntimeCallingConvention calling_convention;
2217 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2218 calling_convention.GetFpuRegisterAt(0),
2219 calling_convention.GetFpuRegisterAt(1)));
2220 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002222 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002223
2224 default:
2225 LOG(FATAL) << "Unexpected type conversion from " << input_type
2226 << " to " << result_type;
2227 }
2228 break;
2229
Roland Levillain981e4542014-11-14 11:47:14 +00002230 case Primitive::kPrimChar:
2231 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002232 case Primitive::kPrimBoolean:
2233 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002234 case Primitive::kPrimByte:
2235 case Primitive::kPrimShort:
2236 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002237 // Processing a Dex `int-to-char' instruction.
2238 locations->SetInAt(0, Location::RequiresRegister());
2239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2240 break;
2241
2242 default:
2243 LOG(FATAL) << "Unexpected type conversion from " << input_type
2244 << " to " << result_type;
2245 }
2246 break;
2247
Roland Levillaindff1f282014-11-05 14:15:05 +00002248 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002249 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002250 case Primitive::kPrimBoolean:
2251 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002252 case Primitive::kPrimByte:
2253 case Primitive::kPrimShort:
2254 case Primitive::kPrimInt:
2255 case Primitive::kPrimChar:
2256 // Processing a Dex `int-to-float' instruction.
2257 locations->SetInAt(0, Location::RequiresRegister());
2258 locations->SetOut(Location::RequiresFpuRegister());
2259 break;
2260
Roland Levillain5b3ee562015-04-14 16:02:41 +01002261 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002262 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002263 InvokeRuntimeCallingConvention calling_convention;
2264 locations->SetInAt(0, Location::RegisterPairLocation(
2265 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2266 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002267 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002268 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002269
Roland Levillaincff13742014-11-17 14:32:17 +00002270 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002271 // Processing a Dex `double-to-float' instruction.
2272 locations->SetInAt(0, Location::RequiresFpuRegister());
2273 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002274 break;
2275
2276 default:
2277 LOG(FATAL) << "Unexpected type conversion from " << input_type
2278 << " to " << result_type;
2279 };
2280 break;
2281
Roland Levillaindff1f282014-11-05 14:15:05 +00002282 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002283 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002284 case Primitive::kPrimBoolean:
2285 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002286 case Primitive::kPrimByte:
2287 case Primitive::kPrimShort:
2288 case Primitive::kPrimInt:
2289 case Primitive::kPrimChar:
2290 // Processing a Dex `int-to-double' instruction.
2291 locations->SetInAt(0, Location::RequiresRegister());
2292 locations->SetOut(Location::RequiresFpuRegister());
2293 break;
2294
2295 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002296 // Processing a Dex `long-to-double' instruction.
2297 locations->SetInAt(0, Location::RequiresRegister());
2298 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002299 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002300 locations->AddTemp(Location::RequiresFpuRegister());
2301 break;
2302
Roland Levillaincff13742014-11-17 14:32:17 +00002303 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002304 // Processing a Dex `float-to-double' instruction.
2305 locations->SetInAt(0, Location::RequiresFpuRegister());
2306 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002307 break;
2308
2309 default:
2310 LOG(FATAL) << "Unexpected type conversion from " << input_type
2311 << " to " << result_type;
2312 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002313 break;
2314
2315 default:
2316 LOG(FATAL) << "Unexpected type conversion from " << input_type
2317 << " to " << result_type;
2318 }
2319}
2320
2321void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2322 LocationSummary* locations = conversion->GetLocations();
2323 Location out = locations->Out();
2324 Location in = locations->InAt(0);
2325 Primitive::Type result_type = conversion->GetResultType();
2326 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002327 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002328 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002329 case Primitive::kPrimByte:
2330 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002331 case Primitive::kPrimBoolean:
2332 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002333 case Primitive::kPrimShort:
2334 case Primitive::kPrimInt:
2335 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002336 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002338 break;
2339
2340 default:
2341 LOG(FATAL) << "Unexpected type conversion from " << input_type
2342 << " to " << result_type;
2343 }
2344 break;
2345
Roland Levillain01a8d712014-11-14 16:27:39 +00002346 case Primitive::kPrimShort:
2347 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002348 case Primitive::kPrimBoolean:
2349 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002350 case Primitive::kPrimByte:
2351 case Primitive::kPrimInt:
2352 case Primitive::kPrimChar:
2353 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002354 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected type conversion from " << input_type
2359 << " to " << result_type;
2360 }
2361 break;
2362
Roland Levillain946e1432014-11-11 17:35:19 +00002363 case Primitive::kPrimInt:
2364 switch (input_type) {
2365 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002366 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002367 DCHECK(out.IsRegister());
2368 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002370 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002371 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002372 } else {
2373 DCHECK(in.IsConstant());
2374 DCHECK(in.GetConstant()->IsLongConstant());
2375 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002376 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002377 }
2378 break;
2379
Roland Levillain3f8f9362014-12-02 17:45:01 +00002380 case Primitive::kPrimFloat: {
2381 // Processing a Dex `float-to-int' instruction.
2382 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2383 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2384 __ vcvtis(temp, temp);
2385 __ vmovrs(out.AsRegister<Register>(), temp);
2386 break;
2387 }
2388
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002389 case Primitive::kPrimDouble: {
2390 // Processing a Dex `double-to-int' instruction.
2391 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2392 DRegister temp_d = FromLowSToD(temp_s);
2393 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2394 __ vcvtid(temp_s, temp_d);
2395 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002396 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002397 }
Roland Levillain946e1432014-11-11 17:35:19 +00002398
2399 default:
2400 LOG(FATAL) << "Unexpected type conversion from " << input_type
2401 << " to " << result_type;
2402 }
2403 break;
2404
Roland Levillaindff1f282014-11-05 14:15:05 +00002405 case Primitive::kPrimLong:
2406 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002407 case Primitive::kPrimBoolean:
2408 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002409 case Primitive::kPrimByte:
2410 case Primitive::kPrimShort:
2411 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002412 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002413 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002414 DCHECK(out.IsRegisterPair());
2415 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002417 // Sign extension.
2418 __ Asr(out.AsRegisterPairHigh<Register>(),
2419 out.AsRegisterPairLow<Register>(),
2420 31);
2421 break;
2422
2423 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002424 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002425 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2426 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002427 conversion->GetDexPc(),
2428 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002429 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002430 break;
2431
Roland Levillaindff1f282014-11-05 14:15:05 +00002432 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002433 // Processing a Dex `double-to-long' instruction.
2434 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2435 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002436 conversion->GetDexPc(),
2437 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002438 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002439 break;
2440
2441 default:
2442 LOG(FATAL) << "Unexpected type conversion from " << input_type
2443 << " to " << result_type;
2444 }
2445 break;
2446
Roland Levillain981e4542014-11-14 11:47:14 +00002447 case Primitive::kPrimChar:
2448 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002449 case Primitive::kPrimBoolean:
2450 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002451 case Primitive::kPrimByte:
2452 case Primitive::kPrimShort:
2453 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002454 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002456 break;
2457
2458 default:
2459 LOG(FATAL) << "Unexpected type conversion from " << input_type
2460 << " to " << result_type;
2461 }
2462 break;
2463
Roland Levillaindff1f282014-11-05 14:15:05 +00002464 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002465 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002466 case Primitive::kPrimBoolean:
2467 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002468 case Primitive::kPrimByte:
2469 case Primitive::kPrimShort:
2470 case Primitive::kPrimInt:
2471 case Primitive::kPrimChar: {
2472 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002473 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2474 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002475 break;
2476 }
2477
Roland Levillain5b3ee562015-04-14 16:02:41 +01002478 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002479 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002480 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2481 conversion,
2482 conversion->GetDexPc(),
2483 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002484 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002485 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002486
Roland Levillaincff13742014-11-17 14:32:17 +00002487 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002488 // Processing a Dex `double-to-float' instruction.
2489 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2490 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002491 break;
2492
2493 default:
2494 LOG(FATAL) << "Unexpected type conversion from " << input_type
2495 << " to " << result_type;
2496 };
2497 break;
2498
Roland Levillaindff1f282014-11-05 14:15:05 +00002499 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002500 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002501 case Primitive::kPrimBoolean:
2502 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002503 case Primitive::kPrimByte:
2504 case Primitive::kPrimShort:
2505 case Primitive::kPrimInt:
2506 case Primitive::kPrimChar: {
2507 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002508 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002509 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2510 out.AsFpuRegisterPairLow<SRegister>());
2511 break;
2512 }
2513
Roland Levillain647b9ed2014-11-27 12:06:00 +00002514 case Primitive::kPrimLong: {
2515 // Processing a Dex `long-to-double' instruction.
2516 Register low = in.AsRegisterPairLow<Register>();
2517 Register high = in.AsRegisterPairHigh<Register>();
2518 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2519 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002520 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002521 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002522 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2523 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002524
Roland Levillain682393c2015-04-14 15:57:52 +01002525 // temp_d = int-to-double(high)
2526 __ vmovsr(temp_s, high);
2527 __ vcvtdi(temp_d, temp_s);
2528 // constant_d = k2Pow32EncodingForDouble
2529 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2530 // out_d = unsigned-to-double(low)
2531 __ vmovsr(out_s, low);
2532 __ vcvtdu(out_d, out_s);
2533 // out_d += temp_d * constant_d
2534 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002535 break;
2536 }
2537
Roland Levillaincff13742014-11-17 14:32:17 +00002538 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002539 // Processing a Dex `float-to-double' instruction.
2540 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2541 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002542 break;
2543
2544 default:
2545 LOG(FATAL) << "Unexpected type conversion from " << input_type
2546 << " to " << result_type;
2547 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type conversion from " << input_type
2552 << " to " << result_type;
2553 }
2554}
2555
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002556void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002557 LocationSummary* locations =
2558 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002559 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002560 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002561 locations->SetInAt(0, Location::RequiresRegister());
2562 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2564 break;
2565 }
2566
2567 case Primitive::kPrimLong: {
2568 locations->SetInAt(0, Location::RequiresRegister());
2569 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002570 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002571 break;
2572 }
2573
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002574 case Primitive::kPrimFloat:
2575 case Primitive::kPrimDouble: {
2576 locations->SetInAt(0, Location::RequiresFpuRegister());
2577 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002578 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002579 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002580 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002581
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002582 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002583 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002584 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002585}
2586
2587void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2588 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002589 Location out = locations->Out();
2590 Location first = locations->InAt(0);
2591 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002592 switch (add->GetResultType()) {
2593 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002594 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002595 __ add(out.AsRegister<Register>(),
2596 first.AsRegister<Register>(),
2597 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002598 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002599 __ AddConstant(out.AsRegister<Register>(),
2600 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002601 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002602 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002603 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002604
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002605 case Primitive::kPrimLong: {
2606 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002607 __ adds(out.AsRegisterPairLow<Register>(),
2608 first.AsRegisterPairLow<Register>(),
2609 ShifterOperand(second.AsRegisterPairLow<Register>()));
2610 __ adc(out.AsRegisterPairHigh<Register>(),
2611 first.AsRegisterPairHigh<Register>(),
2612 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002613 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002614 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002615
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002616 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002617 __ vadds(out.AsFpuRegister<SRegister>(),
2618 first.AsFpuRegister<SRegister>(),
2619 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002620 break;
2621
2622 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002623 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2624 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2625 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002626 break;
2627
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002628 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002629 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002630 }
2631}
2632
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002633void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002634 LocationSummary* locations =
2635 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002636 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002637 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002638 locations->SetInAt(0, Location::RequiresRegister());
2639 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002640 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2641 break;
2642 }
2643
2644 case Primitive::kPrimLong: {
2645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002648 break;
2649 }
Calin Juravle11351682014-10-23 15:38:15 +01002650 case Primitive::kPrimFloat:
2651 case Primitive::kPrimDouble: {
2652 locations->SetInAt(0, Location::RequiresFpuRegister());
2653 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002654 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002655 break;
Calin Juravle11351682014-10-23 15:38:15 +01002656 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002657 default:
Calin Juravle11351682014-10-23 15:38:15 +01002658 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002659 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002660}
2661
2662void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2663 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002664 Location out = locations->Out();
2665 Location first = locations->InAt(0);
2666 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002667 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002668 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002669 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002670 __ sub(out.AsRegister<Register>(),
2671 first.AsRegister<Register>(),
2672 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002673 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002674 __ AddConstant(out.AsRegister<Register>(),
2675 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002676 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002677 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002678 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002679 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002680
Calin Juravle11351682014-10-23 15:38:15 +01002681 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002682 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002683 __ subs(out.AsRegisterPairLow<Register>(),
2684 first.AsRegisterPairLow<Register>(),
2685 ShifterOperand(second.AsRegisterPairLow<Register>()));
2686 __ sbc(out.AsRegisterPairHigh<Register>(),
2687 first.AsRegisterPairHigh<Register>(),
2688 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002689 break;
Calin Juravle11351682014-10-23 15:38:15 +01002690 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002691
Calin Juravle11351682014-10-23 15:38:15 +01002692 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002693 __ vsubs(out.AsFpuRegister<SRegister>(),
2694 first.AsFpuRegister<SRegister>(),
2695 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002696 break;
Calin Juravle11351682014-10-23 15:38:15 +01002697 }
2698
2699 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002700 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2701 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2702 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002703 break;
2704 }
2705
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002706
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002707 default:
Calin Juravle11351682014-10-23 15:38:15 +01002708 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002709 }
2710}
2711
Calin Juravle34bacdf2014-10-07 20:23:36 +01002712void LocationsBuilderARM::VisitMul(HMul* mul) {
2713 LocationSummary* locations =
2714 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2715 switch (mul->GetResultType()) {
2716 case Primitive::kPrimInt:
2717 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002718 locations->SetInAt(0, Location::RequiresRegister());
2719 locations->SetInAt(1, Location::RequiresRegister());
2720 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002721 break;
2722 }
2723
Calin Juravleb5bfa962014-10-21 18:02:24 +01002724 case Primitive::kPrimFloat:
2725 case Primitive::kPrimDouble: {
2726 locations->SetInAt(0, Location::RequiresFpuRegister());
2727 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002728 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002729 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002730 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002731
2732 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002733 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002734 }
2735}
2736
2737void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2738 LocationSummary* locations = mul->GetLocations();
2739 Location out = locations->Out();
2740 Location first = locations->InAt(0);
2741 Location second = locations->InAt(1);
2742 switch (mul->GetResultType()) {
2743 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002744 __ mul(out.AsRegister<Register>(),
2745 first.AsRegister<Register>(),
2746 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002747 break;
2748 }
2749 case Primitive::kPrimLong: {
2750 Register out_hi = out.AsRegisterPairHigh<Register>();
2751 Register out_lo = out.AsRegisterPairLow<Register>();
2752 Register in1_hi = first.AsRegisterPairHigh<Register>();
2753 Register in1_lo = first.AsRegisterPairLow<Register>();
2754 Register in2_hi = second.AsRegisterPairHigh<Register>();
2755 Register in2_lo = second.AsRegisterPairLow<Register>();
2756
2757 // Extra checks to protect caused by the existence of R1_R2.
2758 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2759 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2760 DCHECK_NE(out_hi, in1_lo);
2761 DCHECK_NE(out_hi, in2_lo);
2762
2763 // input: in1 - 64 bits, in2 - 64 bits
2764 // output: out
2765 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2766 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2767 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2768
2769 // IP <- in1.lo * in2.hi
2770 __ mul(IP, in1_lo, in2_hi);
2771 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2772 __ mla(out_hi, in1_hi, in2_lo, IP);
2773 // out.lo <- (in1.lo * in2.lo)[31:0];
2774 __ umull(out_lo, IP, in1_lo, in2_lo);
2775 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2776 __ add(out_hi, out_hi, ShifterOperand(IP));
2777 break;
2778 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002779
2780 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002781 __ vmuls(out.AsFpuRegister<SRegister>(),
2782 first.AsFpuRegister<SRegister>(),
2783 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002784 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002785 }
2786
2787 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002788 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2789 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2790 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002791 break;
2792 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002793
2794 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002795 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002796 }
2797}
2798
Zheng Xuc6667102015-05-15 16:08:45 +08002799void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2800 DCHECK(instruction->IsDiv() || instruction->IsRem());
2801 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2802
2803 LocationSummary* locations = instruction->GetLocations();
2804 Location second = locations->InAt(1);
2805 DCHECK(second.IsConstant());
2806
2807 Register out = locations->Out().AsRegister<Register>();
2808 Register dividend = locations->InAt(0).AsRegister<Register>();
2809 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2810 DCHECK(imm == 1 || imm == -1);
2811
2812 if (instruction->IsRem()) {
2813 __ LoadImmediate(out, 0);
2814 } else {
2815 if (imm == 1) {
2816 __ Mov(out, dividend);
2817 } else {
2818 __ rsb(out, dividend, ShifterOperand(0));
2819 }
2820 }
2821}
2822
2823void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2824 DCHECK(instruction->IsDiv() || instruction->IsRem());
2825 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2826
2827 LocationSummary* locations = instruction->GetLocations();
2828 Location second = locations->InAt(1);
2829 DCHECK(second.IsConstant());
2830
2831 Register out = locations->Out().AsRegister<Register>();
2832 Register dividend = locations->InAt(0).AsRegister<Register>();
2833 Register temp = locations->GetTemp(0).AsRegister<Register>();
2834 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002835 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002836 int ctz_imm = CTZ(abs_imm);
2837
2838 if (ctz_imm == 1) {
2839 __ Lsr(temp, dividend, 32 - ctz_imm);
2840 } else {
2841 __ Asr(temp, dividend, 31);
2842 __ Lsr(temp, temp, 32 - ctz_imm);
2843 }
2844 __ add(out, temp, ShifterOperand(dividend));
2845
2846 if (instruction->IsDiv()) {
2847 __ Asr(out, out, ctz_imm);
2848 if (imm < 0) {
2849 __ rsb(out, out, ShifterOperand(0));
2850 }
2851 } else {
2852 __ ubfx(out, out, 0, ctz_imm);
2853 __ sub(out, out, ShifterOperand(temp));
2854 }
2855}
2856
2857void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2858 DCHECK(instruction->IsDiv() || instruction->IsRem());
2859 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2860
2861 LocationSummary* locations = instruction->GetLocations();
2862 Location second = locations->InAt(1);
2863 DCHECK(second.IsConstant());
2864
2865 Register out = locations->Out().AsRegister<Register>();
2866 Register dividend = locations->InAt(0).AsRegister<Register>();
2867 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2868 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2869 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2870
2871 int64_t magic;
2872 int shift;
2873 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2874
2875 __ LoadImmediate(temp1, magic);
2876 __ smull(temp2, temp1, dividend, temp1);
2877
2878 if (imm > 0 && magic < 0) {
2879 __ add(temp1, temp1, ShifterOperand(dividend));
2880 } else if (imm < 0 && magic > 0) {
2881 __ sub(temp1, temp1, ShifterOperand(dividend));
2882 }
2883
2884 if (shift != 0) {
2885 __ Asr(temp1, temp1, shift);
2886 }
2887
2888 if (instruction->IsDiv()) {
2889 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2890 } else {
2891 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2892 // TODO: Strength reduction for mls.
2893 __ LoadImmediate(temp2, imm);
2894 __ mls(out, temp1, temp2, dividend);
2895 }
2896}
2897
2898void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2899 DCHECK(instruction->IsDiv() || instruction->IsRem());
2900 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2901
2902 LocationSummary* locations = instruction->GetLocations();
2903 Location second = locations->InAt(1);
2904 DCHECK(second.IsConstant());
2905
2906 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2907 if (imm == 0) {
2908 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2909 } else if (imm == 1 || imm == -1) {
2910 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002911 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002912 DivRemByPowerOfTwo(instruction);
2913 } else {
2914 DCHECK(imm <= -2 || imm >= 2);
2915 GenerateDivRemWithAnyConstant(instruction);
2916 }
2917}
2918
Calin Juravle7c4954d2014-10-28 16:57:40 +00002919void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002920 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2921 if (div->GetResultType() == Primitive::kPrimLong) {
2922 // pLdiv runtime call.
2923 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002924 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2925 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002926 } else if (div->GetResultType() == Primitive::kPrimInt &&
2927 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2928 // pIdivmod runtime call.
2929 call_kind = LocationSummary::kCall;
2930 }
2931
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002932 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2933
Calin Juravle7c4954d2014-10-28 16:57:40 +00002934 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002935 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002936 if (div->InputAt(1)->IsConstant()) {
2937 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002938 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002940 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2941 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002942 // No temp register required.
2943 } else {
2944 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002945 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002946 locations->AddTemp(Location::RequiresRegister());
2947 }
2948 }
2949 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetInAt(1, Location::RequiresRegister());
2952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2953 } else {
2954 InvokeRuntimeCallingConvention calling_convention;
2955 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2956 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2957 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2958 // we only need the former.
2959 locations->SetOut(Location::RegisterLocation(R0));
2960 }
Calin Juravled0d48522014-11-04 16:40:20 +00002961 break;
2962 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002963 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002964 InvokeRuntimeCallingConvention calling_convention;
2965 locations->SetInAt(0, Location::RegisterPairLocation(
2966 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2967 locations->SetInAt(1, Location::RegisterPairLocation(
2968 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002969 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002970 break;
2971 }
2972 case Primitive::kPrimFloat:
2973 case Primitive::kPrimDouble: {
2974 locations->SetInAt(0, Location::RequiresFpuRegister());
2975 locations->SetInAt(1, Location::RequiresFpuRegister());
2976 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2977 break;
2978 }
2979
2980 default:
2981 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2982 }
2983}
2984
2985void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2986 LocationSummary* locations = div->GetLocations();
2987 Location out = locations->Out();
2988 Location first = locations->InAt(0);
2989 Location second = locations->InAt(1);
2990
2991 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002992 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002993 if (second.IsConstant()) {
2994 GenerateDivRemConstantIntegral(div);
2995 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002996 __ sdiv(out.AsRegister<Register>(),
2997 first.AsRegister<Register>(),
2998 second.AsRegister<Register>());
2999 } else {
3000 InvokeRuntimeCallingConvention calling_convention;
3001 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3002 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3003 DCHECK_EQ(R0, out.AsRegister<Register>());
3004
3005 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003006 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003007 }
Calin Juravled0d48522014-11-04 16:40:20 +00003008 break;
3009 }
3010
Calin Juravle7c4954d2014-10-28 16:57:40 +00003011 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003012 InvokeRuntimeCallingConvention calling_convention;
3013 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3014 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3015 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3016 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3017 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003018 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003019
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003020 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003021 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003022 break;
3023 }
3024
3025 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003026 __ vdivs(out.AsFpuRegister<SRegister>(),
3027 first.AsFpuRegister<SRegister>(),
3028 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003029 break;
3030 }
3031
3032 case Primitive::kPrimDouble: {
3033 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3034 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3035 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3036 break;
3037 }
3038
3039 default:
3040 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3041 }
3042}
3043
Calin Juravlebacfec32014-11-14 15:54:36 +00003044void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003045 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003046
3047 // Most remainders are implemented in the runtime.
3048 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003049 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3050 // sdiv will be replaced by other instruction sequence.
3051 call_kind = LocationSummary::kNoCall;
3052 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3053 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003054 // Have hardware divide instruction for int, do it with three instructions.
3055 call_kind = LocationSummary::kNoCall;
3056 }
3057
Calin Juravlebacfec32014-11-14 15:54:36 +00003058 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3059
Calin Juravled2ec87d2014-12-08 14:24:46 +00003060 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003061 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003062 if (rem->InputAt(1)->IsConstant()) {
3063 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003064 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003066 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3067 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003068 // No temp register required.
3069 } else {
3070 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003071 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003072 locations->AddTemp(Location::RequiresRegister());
3073 }
3074 }
3075 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003076 locations->SetInAt(0, Location::RequiresRegister());
3077 locations->SetInAt(1, Location::RequiresRegister());
3078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3079 locations->AddTemp(Location::RequiresRegister());
3080 } else {
3081 InvokeRuntimeCallingConvention calling_convention;
3082 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3083 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3084 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3085 // we only need the latter.
3086 locations->SetOut(Location::RegisterLocation(R1));
3087 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003088 break;
3089 }
3090 case Primitive::kPrimLong: {
3091 InvokeRuntimeCallingConvention calling_convention;
3092 locations->SetInAt(0, Location::RegisterPairLocation(
3093 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3094 locations->SetInAt(1, Location::RegisterPairLocation(
3095 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3096 // The runtime helper puts the output in R2,R3.
3097 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3098 break;
3099 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003100 case Primitive::kPrimFloat: {
3101 InvokeRuntimeCallingConvention calling_convention;
3102 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3103 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3104 locations->SetOut(Location::FpuRegisterLocation(S0));
3105 break;
3106 }
3107
Calin Juravlebacfec32014-11-14 15:54:36 +00003108 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003109 InvokeRuntimeCallingConvention calling_convention;
3110 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3111 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3112 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3113 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3114 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003115 break;
3116 }
3117
3118 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003119 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003120 }
3121}
3122
3123void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3124 LocationSummary* locations = rem->GetLocations();
3125 Location out = locations->Out();
3126 Location first = locations->InAt(0);
3127 Location second = locations->InAt(1);
3128
Calin Juravled2ec87d2014-12-08 14:24:46 +00003129 Primitive::Type type = rem->GetResultType();
3130 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003131 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003132 if (second.IsConstant()) {
3133 GenerateDivRemConstantIntegral(rem);
3134 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003135 Register reg1 = first.AsRegister<Register>();
3136 Register reg2 = second.AsRegister<Register>();
3137 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003138
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003139 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003140 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003141 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003142 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003143 } else {
3144 InvokeRuntimeCallingConvention calling_convention;
3145 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3146 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3147 DCHECK_EQ(R1, out.AsRegister<Register>());
3148
3149 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003150 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003151 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003152 break;
3153 }
3154
3155 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003156 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003157 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003158 break;
3159 }
3160
Calin Juravled2ec87d2014-12-08 14:24:46 +00003161 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003162 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003163 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003164 break;
3165 }
3166
Calin Juravlebacfec32014-11-14 15:54:36 +00003167 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003168 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003169 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003170 break;
3171 }
3172
3173 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003174 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003175 }
3176}
3177
Calin Juravled0d48522014-11-04 16:40:20 +00003178void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003179 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3180 ? LocationSummary::kCallOnSlowPath
3181 : LocationSummary::kNoCall;
3182 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003183 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003184 if (instruction->HasUses()) {
3185 locations->SetOut(Location::SameAsFirstInput());
3186 }
3187}
3188
3189void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003190 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003191 codegen_->AddSlowPath(slow_path);
3192
3193 LocationSummary* locations = instruction->GetLocations();
3194 Location value = locations->InAt(0);
3195
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003196 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003197 case Primitive::kPrimByte:
3198 case Primitive::kPrimChar:
3199 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003200 case Primitive::kPrimInt: {
3201 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003202 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003203 } else {
3204 DCHECK(value.IsConstant()) << value;
3205 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3206 __ b(slow_path->GetEntryLabel());
3207 }
3208 }
3209 break;
3210 }
3211 case Primitive::kPrimLong: {
3212 if (value.IsRegisterPair()) {
3213 __ orrs(IP,
3214 value.AsRegisterPairLow<Register>(),
3215 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3216 __ b(slow_path->GetEntryLabel(), EQ);
3217 } else {
3218 DCHECK(value.IsConstant()) << value;
3219 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3220 __ b(slow_path->GetEntryLabel());
3221 }
3222 }
3223 break;
3224 default:
3225 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3226 }
3227 }
Calin Juravled0d48522014-11-04 16:40:20 +00003228}
3229
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003230void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3231 Register in = locations->InAt(0).AsRegister<Register>();
3232 Location rhs = locations->InAt(1);
3233 Register out = locations->Out().AsRegister<Register>();
3234
3235 if (rhs.IsConstant()) {
3236 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3237 // so map all rotations to a +ve. equivalent in that range.
3238 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3239 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3240 if (rot) {
3241 // Rotate, mapping left rotations to right equivalents if necessary.
3242 // (e.g. left by 2 bits == right by 30.)
3243 __ Ror(out, in, rot);
3244 } else if (out != in) {
3245 __ Mov(out, in);
3246 }
3247 } else {
3248 __ Ror(out, in, rhs.AsRegister<Register>());
3249 }
3250}
3251
3252// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3253// rotates by swapping input regs (effectively rotating by the first 32-bits of
3254// a larger rotation) or flipping direction (thus treating larger right/left
3255// rotations as sub-word sized rotations in the other direction) as appropriate.
3256void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3257 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3258 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3259 Location rhs = locations->InAt(1);
3260 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3261 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3262
3263 if (rhs.IsConstant()) {
3264 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3265 // Map all rotations to +ve. equivalents on the interval [0,63].
3266 rot &= kMaxLongShiftValue;
3267 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3268 // logic below to a simple pair of binary orr.
3269 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3270 if (rot >= kArmBitsPerWord) {
3271 rot -= kArmBitsPerWord;
3272 std::swap(in_reg_hi, in_reg_lo);
3273 }
3274 // Rotate, or mov to out for zero or word size rotations.
3275 if (rot != 0u) {
3276 __ Lsr(out_reg_hi, in_reg_hi, rot);
3277 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3278 __ Lsr(out_reg_lo, in_reg_lo, rot);
3279 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3280 } else {
3281 __ Mov(out_reg_lo, in_reg_lo);
3282 __ Mov(out_reg_hi, in_reg_hi);
3283 }
3284 } else {
3285 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3286 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3287 Label end;
3288 Label shift_by_32_plus_shift_right;
3289
3290 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3291 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3292 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3293 __ b(&shift_by_32_plus_shift_right, CC);
3294
3295 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3296 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3297 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3298 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3299 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3300 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3301 __ Lsr(shift_left, in_reg_hi, shift_right);
3302 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3303 __ b(&end);
3304
3305 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3306 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3307 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3308 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3309 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3310 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3311 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3312 __ Lsl(shift_right, in_reg_hi, shift_left);
3313 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3314
3315 __ Bind(&end);
3316 }
3317}
3318void LocationsBuilderARM::HandleRotate(HRor* ror) {
3319 LocationSummary* locations =
3320 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3321 switch (ror->GetResultType()) {
3322 case Primitive::kPrimInt: {
3323 locations->SetInAt(0, Location::RequiresRegister());
3324 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3326 break;
3327 }
3328 case Primitive::kPrimLong: {
3329 locations->SetInAt(0, Location::RequiresRegister());
3330 if (ror->InputAt(1)->IsConstant()) {
3331 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3332 } else {
3333 locations->SetInAt(1, Location::RequiresRegister());
3334 locations->AddTemp(Location::RequiresRegister());
3335 locations->AddTemp(Location::RequiresRegister());
3336 }
3337 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3338 break;
3339 }
3340 default:
3341 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3342 }
3343}
3344
3345void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3346 LocationSummary* locations = ror->GetLocations();
3347 Primitive::Type type = ror->GetResultType();
3348 switch (type) {
3349 case Primitive::kPrimInt: {
3350 HandleIntegerRotate(locations);
3351 break;
3352 }
3353 case Primitive::kPrimLong: {
3354 HandleLongRotate(locations);
3355 break;
3356 }
3357 default:
3358 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003359 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003360 }
3361}
3362
3363void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003364 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003365}
3366
3367void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003368 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003369}
3370
Calin Juravle9aec02f2014-11-18 23:06:35 +00003371void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3372 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3373
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003374 LocationSummary* locations =
3375 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003376
3377 switch (op->GetResultType()) {
3378 case Primitive::kPrimInt: {
3379 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003380 if (op->InputAt(1)->IsConstant()) {
3381 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3383 } else {
3384 locations->SetInAt(1, Location::RequiresRegister());
3385 // Make the output overlap, as it will be used to hold the masked
3386 // second input.
3387 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3388 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003389 break;
3390 }
3391 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003392 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003393 if (op->InputAt(1)->IsConstant()) {
3394 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3395 // For simplicity, use kOutputOverlap even though we only require that low registers
3396 // don't clash with high registers which the register allocator currently guarantees.
3397 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3398 } else {
3399 locations->SetInAt(1, Location::RequiresRegister());
3400 locations->AddTemp(Location::RequiresRegister());
3401 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3402 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003403 break;
3404 }
3405 default:
3406 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3407 }
3408}
3409
3410void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3411 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3412
3413 LocationSummary* locations = op->GetLocations();
3414 Location out = locations->Out();
3415 Location first = locations->InAt(0);
3416 Location second = locations->InAt(1);
3417
3418 Primitive::Type type = op->GetResultType();
3419 switch (type) {
3420 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003421 Register out_reg = out.AsRegister<Register>();
3422 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003423 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003424 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003425 // ARM doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003426 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003427 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003428 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003429 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003430 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003431 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003432 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003433 }
3434 } else {
3435 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3436 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
Roland Levillainc9285912015-12-18 10:38:42 +00003437 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003438 __ Mov(out_reg, first_reg);
3439 } else if (op->IsShl()) {
3440 __ Lsl(out_reg, first_reg, shift_value);
3441 } else if (op->IsShr()) {
3442 __ Asr(out_reg, first_reg, shift_value);
3443 } else {
3444 __ Lsr(out_reg, first_reg, shift_value);
3445 }
3446 }
3447 break;
3448 }
3449 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003450 Register o_h = out.AsRegisterPairHigh<Register>();
3451 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003452
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003453 Register high = first.AsRegisterPairHigh<Register>();
3454 Register low = first.AsRegisterPairLow<Register>();
3455
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003456 if (second.IsRegister()) {
3457 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003458
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003459 Register second_reg = second.AsRegister<Register>();
3460
3461 if (op->IsShl()) {
3462 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3463 // Shift the high part
3464 __ Lsl(o_h, high, o_l);
3465 // Shift the low part and `or` what overflew on the high part
3466 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3467 __ Lsr(temp, low, temp);
3468 __ orr(o_h, o_h, ShifterOperand(temp));
3469 // If the shift is > 32 bits, override the high part
3470 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3471 __ it(PL);
3472 __ Lsl(o_h, low, temp, PL);
3473 // Shift the low part
3474 __ Lsl(o_l, low, o_l);
3475 } else if (op->IsShr()) {
3476 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3477 // Shift the low part
3478 __ Lsr(o_l, low, o_h);
3479 // Shift the high part and `or` what underflew on the low part
3480 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3481 __ Lsl(temp, high, temp);
3482 __ orr(o_l, o_l, ShifterOperand(temp));
3483 // If the shift is > 32 bits, override the low part
3484 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3485 __ it(PL);
3486 __ Asr(o_l, high, temp, PL);
3487 // Shift the high part
3488 __ Asr(o_h, high, o_h);
3489 } else {
3490 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3491 // same as Shr except we use `Lsr`s and not `Asr`s
3492 __ Lsr(o_l, low, o_h);
3493 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3494 __ Lsl(temp, high, temp);
3495 __ orr(o_l, o_l, ShifterOperand(temp));
3496 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3497 __ it(PL);
3498 __ Lsr(o_l, high, temp, PL);
3499 __ Lsr(o_h, high, o_h);
3500 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003501 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003502 // Register allocator doesn't create partial overlap.
3503 DCHECK_NE(o_l, high);
3504 DCHECK_NE(o_h, low);
3505 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3506 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3507 if (shift_value > 32) {
3508 if (op->IsShl()) {
3509 __ Lsl(o_h, low, shift_value - 32);
3510 __ LoadImmediate(o_l, 0);
3511 } else if (op->IsShr()) {
3512 __ Asr(o_l, high, shift_value - 32);
3513 __ Asr(o_h, high, 31);
3514 } else {
3515 __ Lsr(o_l, high, shift_value - 32);
3516 __ LoadImmediate(o_h, 0);
3517 }
3518 } else if (shift_value == 32) {
3519 if (op->IsShl()) {
3520 __ mov(o_h, ShifterOperand(low));
3521 __ LoadImmediate(o_l, 0);
3522 } else if (op->IsShr()) {
3523 __ mov(o_l, ShifterOperand(high));
3524 __ Asr(o_h, high, 31);
3525 } else {
3526 __ mov(o_l, ShifterOperand(high));
3527 __ LoadImmediate(o_h, 0);
3528 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003529 } else if (shift_value == 1) {
3530 if (op->IsShl()) {
3531 __ Lsls(o_l, low, 1);
3532 __ adc(o_h, high, ShifterOperand(high));
3533 } else if (op->IsShr()) {
3534 __ Asrs(o_h, high, 1);
3535 __ Rrx(o_l, low);
3536 } else {
3537 __ Lsrs(o_h, high, 1);
3538 __ Rrx(o_l, low);
3539 }
3540 } else {
3541 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003542 if (op->IsShl()) {
3543 __ Lsl(o_h, high, shift_value);
3544 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3545 __ Lsl(o_l, low, shift_value);
3546 } else if (op->IsShr()) {
3547 __ Lsr(o_l, low, shift_value);
3548 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3549 __ Asr(o_h, high, shift_value);
3550 } else {
3551 __ Lsr(o_l, low, shift_value);
3552 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3553 __ Lsr(o_h, high, shift_value);
3554 }
3555 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003556 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003557 break;
3558 }
3559 default:
3560 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003561 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003562 }
3563}
3564
3565void LocationsBuilderARM::VisitShl(HShl* shl) {
3566 HandleShift(shl);
3567}
3568
3569void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3570 HandleShift(shl);
3571}
3572
3573void LocationsBuilderARM::VisitShr(HShr* shr) {
3574 HandleShift(shr);
3575}
3576
3577void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3578 HandleShift(shr);
3579}
3580
3581void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3582 HandleShift(ushr);
3583}
3584
3585void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3586 HandleShift(ushr);
3587}
3588
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003589void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003590 LocationSummary* locations =
3591 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
David Brazdil6de19382016-01-08 17:37:10 +00003592 if (instruction->IsStringAlloc()) {
3593 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3594 } else {
3595 InvokeRuntimeCallingConvention calling_convention;
3596 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3597 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3598 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003599 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003600}
3601
3602void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003603 // Note: if heap poisoning is enabled, the entry point takes cares
3604 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003605 if (instruction->IsStringAlloc()) {
3606 // String is allocated through StringFactory. Call NewEmptyString entry point.
3607 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3608 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3609 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3610 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3611 __ blx(LR);
3612 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3613 } else {
3614 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3615 instruction,
3616 instruction->GetDexPc(),
3617 nullptr);
3618 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3619 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003620}
3621
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003622void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3623 LocationSummary* locations =
3624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3625 InvokeRuntimeCallingConvention calling_convention;
3626 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003627 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003628 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003629 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003630}
3631
3632void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3633 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003634 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003635 // Note: if heap poisoning is enabled, the entry point takes cares
3636 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003637 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003638 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003639 instruction->GetDexPc(),
3640 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003641 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003642}
3643
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003644void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003645 LocationSummary* locations =
3646 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003647 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3648 if (location.IsStackSlot()) {
3649 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3650 } else if (location.IsDoubleStackSlot()) {
3651 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003652 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003653 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003654}
3655
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003656void InstructionCodeGeneratorARM::VisitParameterValue(
3657 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003658 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003659}
3660
3661void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3662 LocationSummary* locations =
3663 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3664 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3665}
3666
3667void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3668 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003669}
3670
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003671void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003672 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003673 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003674 locations->SetInAt(0, Location::RequiresRegister());
3675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003676}
3677
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003678void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3679 LocationSummary* locations = not_->GetLocations();
3680 Location out = locations->Out();
3681 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003682 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003683 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003685 break;
3686
3687 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003688 __ mvn(out.AsRegisterPairLow<Register>(),
3689 ShifterOperand(in.AsRegisterPairLow<Register>()));
3690 __ mvn(out.AsRegisterPairHigh<Register>(),
3691 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003692 break;
3693
3694 default:
3695 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3696 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003697}
3698
David Brazdil66d126e2015-04-03 16:02:44 +01003699void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3700 LocationSummary* locations =
3701 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3702 locations->SetInAt(0, Location::RequiresRegister());
3703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3704}
3705
3706void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003707 LocationSummary* locations = bool_not->GetLocations();
3708 Location out = locations->Out();
3709 Location in = locations->InAt(0);
3710 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3711}
3712
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003713void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003714 LocationSummary* locations =
3715 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003716 switch (compare->InputAt(0)->GetType()) {
3717 case Primitive::kPrimLong: {
3718 locations->SetInAt(0, Location::RequiresRegister());
3719 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003720 // Output overlaps because it is written before doing the low comparison.
3721 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003722 break;
3723 }
3724 case Primitive::kPrimFloat:
3725 case Primitive::kPrimDouble: {
3726 locations->SetInAt(0, Location::RequiresFpuRegister());
3727 locations->SetInAt(1, Location::RequiresFpuRegister());
3728 locations->SetOut(Location::RequiresRegister());
3729 break;
3730 }
3731 default:
3732 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3733 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003734}
3735
3736void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003737 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003739 Location left = locations->InAt(0);
3740 Location right = locations->InAt(1);
3741
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003742 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003743 Primitive::Type type = compare->InputAt(0)->GetType();
3744 switch (type) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003745 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003746 __ cmp(left.AsRegisterPairHigh<Register>(),
3747 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003748 __ b(&less, LT);
3749 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003750 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003751 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003752 __ cmp(left.AsRegisterPairLow<Register>(),
3753 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Calin Juravleddb7df22014-11-25 20:56:51 +00003754 break;
3755 }
3756 case Primitive::kPrimFloat:
3757 case Primitive::kPrimDouble: {
3758 __ LoadImmediate(out, 0);
3759 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003760 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003761 } else {
3762 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3763 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3764 }
3765 __ vmstat(); // transfer FP status register to ARM APSR.
3766 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003767 break;
3768 }
3769 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003770 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003771 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003772 __ b(&done, EQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003773 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats.
Calin Juravleddb7df22014-11-25 20:56:51 +00003774
3775 __ Bind(&greater);
3776 __ LoadImmediate(out, 1);
3777 __ b(&done);
3778
3779 __ Bind(&less);
3780 __ LoadImmediate(out, -1);
3781
3782 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003783}
3784
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003785void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003786 LocationSummary* locations =
3787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003788 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3789 locations->SetInAt(i, Location::Any());
3790 }
3791 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003792}
3793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003794void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003795 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003796}
3797
Roland Levillainc9285912015-12-18 10:38:42 +00003798void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3799 // TODO (ported from quick): revisit ARM barrier kinds.
3800 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003801 switch (kind) {
3802 case MemBarrierKind::kAnyStore:
3803 case MemBarrierKind::kLoadAny:
3804 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003805 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003806 break;
3807 }
3808 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003809 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003810 break;
3811 }
3812 default:
3813 LOG(FATAL) << "Unexpected memory barrier " << kind;
3814 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003815 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003816}
3817
3818void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3819 uint32_t offset,
3820 Register out_lo,
3821 Register out_hi) {
3822 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003823 // Ensure `out_lo` is different from `addr`, so that loading
3824 // `offset` into `out_lo` does not clutter `addr`.
3825 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003826 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003827 __ add(IP, addr, ShifterOperand(out_lo));
3828 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003829 }
3830 __ ldrexd(out_lo, out_hi, addr);
3831}
3832
3833void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3834 uint32_t offset,
3835 Register value_lo,
3836 Register value_hi,
3837 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003838 Register temp2,
3839 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003840 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003841 if (offset != 0) {
3842 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003843 __ add(IP, addr, ShifterOperand(temp1));
3844 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003845 }
3846 __ Bind(&fail);
3847 // We need a load followed by store. (The address used in a STREX instruction must
3848 // be the same as the address in the most recently executed LDREX instruction.)
3849 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003850 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003851 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003852 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003853}
3854
3855void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3856 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3857
Nicolas Geoffray39468442014-09-02 15:17:15 +01003858 LocationSummary* locations =
3859 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003860 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003861
Calin Juravle52c48962014-12-16 17:02:57 +00003862 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003863 if (Primitive::IsFloatingPointType(field_type)) {
3864 locations->SetInAt(1, Location::RequiresFpuRegister());
3865 } else {
3866 locations->SetInAt(1, Location::RequiresRegister());
3867 }
3868
Calin Juravle52c48962014-12-16 17:02:57 +00003869 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003870 bool generate_volatile = field_info.IsVolatile()
3871 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003872 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003873 bool needs_write_barrier =
3874 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003875 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003876 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003877 if (needs_write_barrier) {
3878 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003879 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003880 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003881 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003882 // - registers need to be consecutive
3883 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003884 // We don't test for ARM yet, and the assertion makes sure that we
3885 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003886 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3887
3888 locations->AddTemp(Location::RequiresRegister());
3889 locations->AddTemp(Location::RequiresRegister());
3890 if (field_type == Primitive::kPrimDouble) {
3891 // For doubles we need two more registers to copy the value.
3892 locations->AddTemp(Location::RegisterLocation(R2));
3893 locations->AddTemp(Location::RegisterLocation(R3));
3894 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003895 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003896}
3897
Calin Juravle52c48962014-12-16 17:02:57 +00003898void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003899 const FieldInfo& field_info,
3900 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003901 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003903 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003904 Register base = locations->InAt(0).AsRegister<Register>();
3905 Location value = locations->InAt(1);
3906
3907 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003908 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003909 Primitive::Type field_type = field_info.GetFieldType();
3910 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003911 bool needs_write_barrier =
3912 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003913
3914 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003915 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003916 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003917
3918 switch (field_type) {
3919 case Primitive::kPrimBoolean:
3920 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003921 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003922 break;
3923 }
3924
3925 case Primitive::kPrimShort:
3926 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003927 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003928 break;
3929 }
3930
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003931 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003932 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003933 if (kPoisonHeapReferences && needs_write_barrier) {
3934 // Note that in the case where `value` is a null reference,
3935 // we do not enter this block, as a null reference does not
3936 // need poisoning.
3937 DCHECK_EQ(field_type, Primitive::kPrimNot);
3938 Register temp = locations->GetTemp(0).AsRegister<Register>();
3939 __ Mov(temp, value.AsRegister<Register>());
3940 __ PoisonHeapReference(temp);
3941 __ StoreToOffset(kStoreWord, temp, base, offset);
3942 } else {
3943 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3944 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003945 break;
3946 }
3947
3948 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003949 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003950 GenerateWideAtomicStore(base, offset,
3951 value.AsRegisterPairLow<Register>(),
3952 value.AsRegisterPairHigh<Register>(),
3953 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003954 locations->GetTemp(1).AsRegister<Register>(),
3955 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003956 } else {
3957 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003958 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003959 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003960 break;
3961 }
3962
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003963 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003964 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003965 break;
3966 }
3967
3968 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003969 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003970 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003971 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3972 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3973
3974 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3975
3976 GenerateWideAtomicStore(base, offset,
3977 value_reg_lo,
3978 value_reg_hi,
3979 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003980 locations->GetTemp(3).AsRegister<Register>(),
3981 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003982 } else {
3983 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003984 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003985 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003986 break;
3987 }
3988
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003989 case Primitive::kPrimVoid:
3990 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003991 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003992 }
Calin Juravle52c48962014-12-16 17:02:57 +00003993
Calin Juravle77520bc2015-01-12 18:45:46 +00003994 // Longs and doubles are handled in the switch.
3995 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3996 codegen_->MaybeRecordImplicitNullCheck(instruction);
3997 }
3998
3999 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4000 Register temp = locations->GetTemp(0).AsRegister<Register>();
4001 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004002 codegen_->MarkGCCard(
4003 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004004 }
4005
Calin Juravle52c48962014-12-16 17:02:57 +00004006 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004007 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004008 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004009}
4010
Calin Juravle52c48962014-12-16 17:02:57 +00004011void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4012 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004013
4014 bool object_field_get_with_read_barrier =
4015 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004016 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004017 new (GetGraph()->GetArena()) LocationSummary(instruction,
4018 object_field_get_with_read_barrier ?
4019 LocationSummary::kCallOnSlowPath :
4020 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004021 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004022
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004023 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004024 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004025 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004026 // The output overlaps in case of volatile long: we don't want the
4027 // code generated by GenerateWideAtomicLoad to overwrite the
4028 // object's location. Likewise, in the case of an object field get
4029 // with read barriers enabled, we do not want the load to overwrite
4030 // the object's location, as we need it to emit the read barrier.
4031 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4032 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004033
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004034 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4035 locations->SetOut(Location::RequiresFpuRegister());
4036 } else {
4037 locations->SetOut(Location::RequiresRegister(),
4038 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4039 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004040 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004041 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004042 // - registers need to be consecutive
4043 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004044 // We don't test for ARM yet, and the assertion makes sure that we
4045 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004046 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4047 locations->AddTemp(Location::RequiresRegister());
4048 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004049 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4050 // We need a temporary register for the read barrier marking slow
4051 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4052 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004053 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004054}
4055
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004056Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4057 Opcode opcode) {
4058 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4059 if (constant->IsConstant() &&
4060 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4061 return Location::ConstantLocation(constant->AsConstant());
4062 }
4063 return Location::RequiresRegister();
4064}
4065
4066bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4067 Opcode opcode) {
4068 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4069 if (Primitive::Is64BitType(input_cst->GetType())) {
4070 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4071 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4072 } else {
4073 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4074 }
4075}
4076
4077bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4078 ShifterOperand so;
4079 ArmAssembler* assembler = codegen_->GetAssembler();
4080 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4081 return true;
4082 }
4083 Opcode neg_opcode = kNoOperand;
4084 switch (opcode) {
4085 case AND:
4086 neg_opcode = BIC;
4087 break;
4088 case ORR:
4089 neg_opcode = ORN;
4090 break;
4091 default:
4092 return false;
4093 }
4094 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4095}
4096
Calin Juravle52c48962014-12-16 17:02:57 +00004097void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4098 const FieldInfo& field_info) {
4099 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004100
Calin Juravle52c48962014-12-16 17:02:57 +00004101 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004102 Location base_loc = locations->InAt(0);
4103 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004104 Location out = locations->Out();
4105 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004106 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004107 Primitive::Type field_type = field_info.GetFieldType();
4108 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4109
4110 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004111 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004112 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004113 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004114
Roland Levillainc9285912015-12-18 10:38:42 +00004115 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004116 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004117 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004118
Roland Levillainc9285912015-12-18 10:38:42 +00004119 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004120 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004121 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004122
Roland Levillainc9285912015-12-18 10:38:42 +00004123 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004124 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004125 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004126
4127 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004128 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004129 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004130
4131 case Primitive::kPrimNot: {
4132 // /* HeapReference<Object> */ out = *(base + offset)
4133 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4134 Location temp_loc = locations->GetTemp(0);
4135 // Note that a potential implicit null check is handled in this
4136 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4137 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4138 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4139 if (is_volatile) {
4140 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4141 }
4142 } else {
4143 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4144 codegen_->MaybeRecordImplicitNullCheck(instruction);
4145 if (is_volatile) {
4146 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4147 }
4148 // If read barriers are enabled, emit read barriers other than
4149 // Baker's using a slow path (and also unpoison the loaded
4150 // reference, if heap poisoning is enabled).
4151 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4152 }
4153 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004154 }
4155
Roland Levillainc9285912015-12-18 10:38:42 +00004156 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004157 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004158 GenerateWideAtomicLoad(base, offset,
4159 out.AsRegisterPairLow<Register>(),
4160 out.AsRegisterPairHigh<Register>());
4161 } else {
4162 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4163 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004164 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165
Roland Levillainc9285912015-12-18 10:38:42 +00004166 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004167 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004168 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004169
4170 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004171 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004172 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004173 Register lo = locations->GetTemp(0).AsRegister<Register>();
4174 Register hi = locations->GetTemp(1).AsRegister<Register>();
4175 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004176 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004177 __ vmovdrr(out_reg, lo, hi);
4178 } else {
4179 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004180 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004181 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004182 break;
4183 }
4184
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004186 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004187 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004188 }
Calin Juravle52c48962014-12-16 17:02:57 +00004189
Roland Levillainc9285912015-12-18 10:38:42 +00004190 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4191 // Potential implicit null checks, in the case of reference or
4192 // double fields, are handled in the previous switch statement.
4193 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004194 codegen_->MaybeRecordImplicitNullCheck(instruction);
4195 }
4196
Calin Juravle52c48962014-12-16 17:02:57 +00004197 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004198 if (field_type == Primitive::kPrimNot) {
4199 // Memory barriers, in the case of references, are also handled
4200 // in the previous switch statement.
4201 } else {
4202 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4203 }
Roland Levillain4d027112015-07-01 15:41:14 +01004204 }
Calin Juravle52c48962014-12-16 17:02:57 +00004205}
4206
4207void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4208 HandleFieldSet(instruction, instruction->GetFieldInfo());
4209}
4210
4211void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004212 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004213}
4214
4215void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4216 HandleFieldGet(instruction, instruction->GetFieldInfo());
4217}
4218
4219void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4220 HandleFieldGet(instruction, instruction->GetFieldInfo());
4221}
4222
4223void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4224 HandleFieldGet(instruction, instruction->GetFieldInfo());
4225}
4226
4227void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4228 HandleFieldGet(instruction, instruction->GetFieldInfo());
4229}
4230
4231void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4232 HandleFieldSet(instruction, instruction->GetFieldInfo());
4233}
4234
4235void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004236 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004237}
4238
Calin Juravlee460d1d2015-09-29 04:52:17 +01004239void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4240 HUnresolvedInstanceFieldGet* instruction) {
4241 FieldAccessCallingConventionARM calling_convention;
4242 codegen_->CreateUnresolvedFieldLocationSummary(
4243 instruction, instruction->GetFieldType(), calling_convention);
4244}
4245
4246void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4247 HUnresolvedInstanceFieldGet* instruction) {
4248 FieldAccessCallingConventionARM calling_convention;
4249 codegen_->GenerateUnresolvedFieldAccess(instruction,
4250 instruction->GetFieldType(),
4251 instruction->GetFieldIndex(),
4252 instruction->GetDexPc(),
4253 calling_convention);
4254}
4255
4256void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4257 HUnresolvedInstanceFieldSet* instruction) {
4258 FieldAccessCallingConventionARM calling_convention;
4259 codegen_->CreateUnresolvedFieldLocationSummary(
4260 instruction, instruction->GetFieldType(), calling_convention);
4261}
4262
4263void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4264 HUnresolvedInstanceFieldSet* instruction) {
4265 FieldAccessCallingConventionARM calling_convention;
4266 codegen_->GenerateUnresolvedFieldAccess(instruction,
4267 instruction->GetFieldType(),
4268 instruction->GetFieldIndex(),
4269 instruction->GetDexPc(),
4270 calling_convention);
4271}
4272
4273void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4274 HUnresolvedStaticFieldGet* instruction) {
4275 FieldAccessCallingConventionARM calling_convention;
4276 codegen_->CreateUnresolvedFieldLocationSummary(
4277 instruction, instruction->GetFieldType(), calling_convention);
4278}
4279
4280void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4281 HUnresolvedStaticFieldGet* instruction) {
4282 FieldAccessCallingConventionARM calling_convention;
4283 codegen_->GenerateUnresolvedFieldAccess(instruction,
4284 instruction->GetFieldType(),
4285 instruction->GetFieldIndex(),
4286 instruction->GetDexPc(),
4287 calling_convention);
4288}
4289
4290void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4291 HUnresolvedStaticFieldSet* instruction) {
4292 FieldAccessCallingConventionARM calling_convention;
4293 codegen_->CreateUnresolvedFieldLocationSummary(
4294 instruction, instruction->GetFieldType(), calling_convention);
4295}
4296
4297void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4298 HUnresolvedStaticFieldSet* instruction) {
4299 FieldAccessCallingConventionARM calling_convention;
4300 codegen_->GenerateUnresolvedFieldAccess(instruction,
4301 instruction->GetFieldType(),
4302 instruction->GetFieldIndex(),
4303 instruction->GetDexPc(),
4304 calling_convention);
4305}
4306
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004307void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004308 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4309 ? LocationSummary::kCallOnSlowPath
4310 : LocationSummary::kNoCall;
4311 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004312 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004313 if (instruction->HasUses()) {
4314 locations->SetOut(Location::SameAsFirstInput());
4315 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316}
4317
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004318void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004319 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4320 return;
4321 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004322 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004323
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004324 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4325 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4326}
4327
4328void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004329 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004330 codegen_->AddSlowPath(slow_path);
4331
4332 LocationSummary* locations = instruction->GetLocations();
4333 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004334
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004335 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004336}
4337
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004338void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004339 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004340 GenerateImplicitNullCheck(instruction);
4341 } else {
4342 GenerateExplicitNullCheck(instruction);
4343 }
4344}
4345
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004346void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004347 bool object_array_get_with_read_barrier =
4348 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004349 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004350 new (GetGraph()->GetArena()) LocationSummary(instruction,
4351 object_array_get_with_read_barrier ?
4352 LocationSummary::kCallOnSlowPath :
4353 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004354 locations->SetInAt(0, Location::RequiresRegister());
4355 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004356 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4357 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4358 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004359 // The output overlaps in the case of an object array get with
4360 // read barriers enabled: we do not want the move to overwrite the
4361 // array's location, as we need it to emit the read barrier.
4362 locations->SetOut(
4363 Location::RequiresRegister(),
4364 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004365 }
Roland Levillainc9285912015-12-18 10:38:42 +00004366 // We need a temporary register for the read barrier marking slow
4367 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4368 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4369 locations->AddTemp(Location::RequiresRegister());
4370 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004371}
4372
4373void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4374 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004375 Location obj_loc = locations->InAt(0);
4376 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004377 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004378 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004379
Roland Levillainc9285912015-12-18 10:38:42 +00004380 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004381 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004382 case Primitive::kPrimBoolean: {
4383 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004384 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004385 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004386 size_t offset =
4387 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004388 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4389 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004390 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004391 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4392 }
4393 break;
4394 }
4395
4396 case Primitive::kPrimByte: {
4397 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004398 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004399 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004400 size_t offset =
4401 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004402 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4403 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004404 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004405 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4406 }
4407 break;
4408 }
4409
4410 case Primitive::kPrimShort: {
4411 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004412 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004413 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004414 size_t offset =
4415 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004416 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4417 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004418 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004419 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4420 }
4421 break;
4422 }
4423
4424 case Primitive::kPrimChar: {
4425 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004426 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004427 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004428 size_t offset =
4429 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4431 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004432 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004433 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4434 }
4435 break;
4436 }
4437
Roland Levillainc9285912015-12-18 10:38:42 +00004438 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004440 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004441 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004442 size_t offset =
4443 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004444 __ LoadFromOffset(kLoadWord, out, obj, offset);
4445 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004446 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004447 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4448 }
4449 break;
4450 }
4451
Roland Levillainc9285912015-12-18 10:38:42 +00004452 case Primitive::kPrimNot: {
4453 static_assert(
4454 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4455 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4456 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4457 // /* HeapReference<Object> */ out =
4458 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4459 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4460 Location temp = locations->GetTemp(0);
4461 // Note that a potential implicit null check is handled in this
4462 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4463 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4464 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4465 } else {
4466 Register out = out_loc.AsRegister<Register>();
4467 if (index.IsConstant()) {
4468 size_t offset =
4469 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4470 __ LoadFromOffset(kLoadWord, out, obj, offset);
4471 codegen_->MaybeRecordImplicitNullCheck(instruction);
4472 // If read barriers are enabled, emit read barriers other than
4473 // Baker's using a slow path (and also unpoison the loaded
4474 // reference, if heap poisoning is enabled).
4475 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4476 } else {
4477 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4478 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4479 codegen_->MaybeRecordImplicitNullCheck(instruction);
4480 // If read barriers are enabled, emit read barriers other than
4481 // Baker's using a slow path (and also unpoison the loaded
4482 // reference, if heap poisoning is enabled).
4483 codegen_->MaybeGenerateReadBarrierSlow(
4484 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4485 }
4486 }
4487 break;
4488 }
4489
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004490 case Primitive::kPrimLong: {
4491 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004492 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004493 size_t offset =
4494 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004495 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004496 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004497 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004498 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004499 }
4500 break;
4501 }
4502
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004503 case Primitive::kPrimFloat: {
4504 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004505 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004506 if (index.IsConstant()) {
4507 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004508 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004509 } else {
4510 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004511 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004512 }
4513 break;
4514 }
4515
4516 case Primitive::kPrimDouble: {
4517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004518 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004519 if (index.IsConstant()) {
4520 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004521 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004522 } else {
4523 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004524 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004525 }
4526 break;
4527 }
4528
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004530 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004531 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532 }
Roland Levillain4d027112015-07-01 15:41:14 +01004533
4534 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004535 // Potential implicit null checks, in the case of reference
4536 // arrays, are handled in the previous switch statement.
4537 } else {
4538 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004539 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540}
4541
4542void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004543 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004544
4545 bool needs_write_barrier =
4546 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004547 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4548 bool object_array_set_with_read_barrier =
4549 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004550
Nicolas Geoffray39468442014-09-02 15:17:15 +01004551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004552 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004553 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4554 LocationSummary::kCallOnSlowPath :
4555 LocationSummary::kNoCall);
4556
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004557 locations->SetInAt(0, Location::RequiresRegister());
4558 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4559 if (Primitive::IsFloatingPointType(value_type)) {
4560 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004561 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004562 locations->SetInAt(2, Location::RequiresRegister());
4563 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004564 if (needs_write_barrier) {
4565 // Temporary registers for the write barrier.
4566 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004567 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004568 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004569}
4570
4571void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4572 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004573 Location array_loc = locations->InAt(0);
4574 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004575 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004576 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004577 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004578 bool needs_write_barrier =
4579 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004580
4581 switch (value_type) {
4582 case Primitive::kPrimBoolean:
4583 case Primitive::kPrimByte: {
4584 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004585 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004586 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004587 size_t offset =
4588 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004589 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004591 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004592 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4593 }
4594 break;
4595 }
4596
4597 case Primitive::kPrimShort:
4598 case Primitive::kPrimChar: {
4599 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004600 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004601 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004602 size_t offset =
4603 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004604 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004606 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4608 }
4609 break;
4610 }
4611
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004612 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004613 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004614 Location value_loc = locations->InAt(2);
4615 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004616 Register source = value;
4617
4618 if (instruction->InputAt(2)->IsNullConstant()) {
4619 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004620 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004621 size_t offset =
4622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004623 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004624 } else {
4625 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004626 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004627 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004628 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004629 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004630 DCHECK(!needs_write_barrier);
4631 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004632 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004633 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004634
4635 DCHECK(needs_write_barrier);
4636 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4637 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4638 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4639 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4640 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4641 Label done;
4642 SlowPathCode* slow_path = nullptr;
4643
Roland Levillain3b359c72015-11-17 19:35:12 +00004644 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004645 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4646 codegen_->AddSlowPath(slow_path);
4647 if (instruction->GetValueCanBeNull()) {
4648 Label non_zero;
4649 __ CompareAndBranchIfNonZero(value, &non_zero);
4650 if (index.IsConstant()) {
4651 size_t offset =
4652 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4653 __ StoreToOffset(kStoreWord, value, array, offset);
4654 } else {
4655 DCHECK(index.IsRegister()) << index;
4656 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4657 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4658 }
4659 codegen_->MaybeRecordImplicitNullCheck(instruction);
4660 __ b(&done);
4661 __ Bind(&non_zero);
4662 }
4663
Roland Levillain3b359c72015-11-17 19:35:12 +00004664 if (kEmitCompilerReadBarrier) {
4665 // When read barriers are enabled, the type checking
4666 // instrumentation requires two read barriers:
4667 //
4668 // __ Mov(temp2, temp1);
4669 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4670 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004671 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004672 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4673 //
4674 // // /* HeapReference<Class> */ temp2 = value->klass_
4675 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004676 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004677 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4678 //
4679 // __ cmp(temp1, ShifterOperand(temp2));
4680 //
4681 // However, the second read barrier may trash `temp`, as it
4682 // is a temporary register, and as such would not be saved
4683 // along with live registers before calling the runtime (nor
4684 // restored afterwards). So in this case, we bail out and
4685 // delegate the work to the array set slow path.
4686 //
4687 // TODO: Extend the register allocator to support a new
4688 // "(locally) live temp" location so as to avoid always
4689 // going into the slow path when read barriers are enabled.
4690 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004691 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004692 // /* HeapReference<Class> */ temp1 = array->klass_
4693 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4694 codegen_->MaybeRecordImplicitNullCheck(instruction);
4695 __ MaybeUnpoisonHeapReference(temp1);
4696
4697 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4698 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4699 // /* HeapReference<Class> */ temp2 = value->klass_
4700 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4701 // If heap poisoning is enabled, no need to unpoison `temp1`
4702 // nor `temp2`, as we are comparing two poisoned references.
4703 __ cmp(temp1, ShifterOperand(temp2));
4704
4705 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4706 Label do_put;
4707 __ b(&do_put, EQ);
4708 // If heap poisoning is enabled, the `temp1` reference has
4709 // not been unpoisoned yet; unpoison it now.
4710 __ MaybeUnpoisonHeapReference(temp1);
4711
4712 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4713 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4714 // If heap poisoning is enabled, no need to unpoison
4715 // `temp1`, as we are comparing against null below.
4716 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4717 __ Bind(&do_put);
4718 } else {
4719 __ b(slow_path->GetEntryLabel(), NE);
4720 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004721 }
4722 }
4723
4724 if (kPoisonHeapReferences) {
4725 // Note that in the case where `value` is a null reference,
4726 // we do not enter this block, as a null reference does not
4727 // need poisoning.
4728 DCHECK_EQ(value_type, Primitive::kPrimNot);
4729 __ Mov(temp1, value);
4730 __ PoisonHeapReference(temp1);
4731 source = temp1;
4732 }
4733
4734 if (index.IsConstant()) {
4735 size_t offset =
4736 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4737 __ StoreToOffset(kStoreWord, source, array, offset);
4738 } else {
4739 DCHECK(index.IsRegister()) << index;
4740 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4741 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4742 }
4743
Roland Levillain3b359c72015-11-17 19:35:12 +00004744 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004745 codegen_->MaybeRecordImplicitNullCheck(instruction);
4746 }
4747
4748 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4749
4750 if (done.IsLinked()) {
4751 __ Bind(&done);
4752 }
4753
4754 if (slow_path != nullptr) {
4755 __ Bind(slow_path->GetExitLabel());
4756 }
4757
4758 break;
4759 }
4760
4761 case Primitive::kPrimInt: {
4762 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4763 Register value = locations->InAt(2).AsRegister<Register>();
4764 if (index.IsConstant()) {
4765 size_t offset =
4766 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4767 __ StoreToOffset(kStoreWord, value, array, offset);
4768 } else {
4769 DCHECK(index.IsRegister()) << index;
4770 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4771 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4772 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773 break;
4774 }
4775
4776 case Primitive::kPrimLong: {
4777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004778 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004779 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004780 size_t offset =
4781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004782 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004784 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004785 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004786 }
4787 break;
4788 }
4789
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004790 case Primitive::kPrimFloat: {
4791 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4792 Location value = locations->InAt(2);
4793 DCHECK(value.IsFpuRegister());
4794 if (index.IsConstant()) {
4795 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004796 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004797 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004798 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004799 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4800 }
4801 break;
4802 }
4803
4804 case Primitive::kPrimDouble: {
4805 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4806 Location value = locations->InAt(2);
4807 DCHECK(value.IsFpuRegisterPair());
4808 if (index.IsConstant()) {
4809 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004810 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004811 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004812 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004813 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4814 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004815
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004816 break;
4817 }
4818
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004819 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004820 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004821 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004822 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004823
Roland Levillain80e67092016-01-08 16:04:55 +00004824 // Objects are handled in the switch.
4825 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004826 codegen_->MaybeRecordImplicitNullCheck(instruction);
4827 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004828}
4829
4830void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004831 LocationSummary* locations =
4832 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004833 locations->SetInAt(0, Location::RequiresRegister());
4834 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004835}
4836
4837void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4838 LocationSummary* locations = instruction->GetLocations();
4839 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004840 Register obj = locations->InAt(0).AsRegister<Register>();
4841 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004843 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844}
4845
4846void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004847 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4848 ? LocationSummary::kCallOnSlowPath
4849 : LocationSummary::kNoCall;
4850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004851 locations->SetInAt(0, Location::RequiresRegister());
4852 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004853 if (instruction->HasUses()) {
4854 locations->SetOut(Location::SameAsFirstInput());
4855 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004856}
4857
4858void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4859 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004860 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004861 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862 codegen_->AddSlowPath(slow_path);
4863
Roland Levillain271ab9c2014-11-27 15:23:57 +00004864 Register index = locations->InAt(0).AsRegister<Register>();
4865 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004866
4867 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004868 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004869}
4870
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004871void CodeGeneratorARM::MarkGCCard(Register temp,
4872 Register card,
4873 Register object,
4874 Register value,
4875 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004876 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004877 if (can_be_null) {
4878 __ CompareAndBranchIfZero(value, &is_null);
4879 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4881 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4882 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004883 if (can_be_null) {
4884 __ Bind(&is_null);
4885 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886}
4887
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004888void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4889 temp->SetLocations(nullptr);
4890}
4891
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004892void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004893 // Nothing to do, this is driven by the code generator.
4894}
4895
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004896void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004897 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004898}
4899
4900void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004901 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4902}
4903
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004904void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4905 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4906}
4907
4908void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004909 HBasicBlock* block = instruction->GetBlock();
4910 if (block->GetLoopInformation() != nullptr) {
4911 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4912 // The back edge will generate the suspend check.
4913 return;
4914 }
4915 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4916 // The goto will generate the suspend check.
4917 return;
4918 }
4919 GenerateSuspendCheck(instruction, nullptr);
4920}
4921
4922void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4923 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004924 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004925 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4926 if (slow_path == nullptr) {
4927 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4928 instruction->SetSlowPath(slow_path);
4929 codegen_->AddSlowPath(slow_path);
4930 if (successor != nullptr) {
4931 DCHECK(successor->IsLoopHeader());
4932 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4933 }
4934 } else {
4935 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4936 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004937
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004938 __ LoadFromOffset(
4939 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004940 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004941 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004942 __ Bind(slow_path->GetReturnLabel());
4943 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004944 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004945 __ b(slow_path->GetEntryLabel());
4946 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004947}
4948
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004949ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4950 return codegen_->GetAssembler();
4951}
4952
4953void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004954 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004955 Location source = move->GetSource();
4956 Location destination = move->GetDestination();
4957
4958 if (source.IsRegister()) {
4959 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004960 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004961 } else {
4962 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004963 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004964 SP, destination.GetStackIndex());
4965 }
4966 } else if (source.IsStackSlot()) {
4967 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004968 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004969 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004970 } else if (destination.IsFpuRegister()) {
4971 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004972 } else {
4973 DCHECK(destination.IsStackSlot());
4974 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4975 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4976 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004977 } else if (source.IsFpuRegister()) {
4978 if (destination.IsFpuRegister()) {
4979 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004980 } else {
4981 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004982 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4983 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004984 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004985 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004986 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4987 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004988 } else if (destination.IsRegisterPair()) {
4989 DCHECK(ExpectedPairLayout(destination));
4990 __ LoadFromOffset(
4991 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4992 } else {
4993 DCHECK(destination.IsFpuRegisterPair()) << destination;
4994 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4995 SP,
4996 source.GetStackIndex());
4997 }
4998 } else if (source.IsRegisterPair()) {
4999 if (destination.IsRegisterPair()) {
5000 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5001 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
5002 } else {
5003 DCHECK(destination.IsDoubleStackSlot()) << destination;
5004 DCHECK(ExpectedPairLayout(source));
5005 __ StoreToOffset(
5006 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5007 }
5008 } else if (source.IsFpuRegisterPair()) {
5009 if (destination.IsFpuRegisterPair()) {
5010 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5011 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5012 } else {
5013 DCHECK(destination.IsDoubleStackSlot()) << destination;
5014 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5015 SP,
5016 destination.GetStackIndex());
5017 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005018 } else {
5019 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005020 HConstant* constant = source.GetConstant();
5021 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5022 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005023 if (destination.IsRegister()) {
5024 __ LoadImmediate(destination.AsRegister<Register>(), value);
5025 } else {
5026 DCHECK(destination.IsStackSlot());
5027 __ LoadImmediate(IP, value);
5028 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5029 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005030 } else if (constant->IsLongConstant()) {
5031 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005032 if (destination.IsRegisterPair()) {
5033 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5034 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005035 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005036 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005037 __ LoadImmediate(IP, Low32Bits(value));
5038 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5039 __ LoadImmediate(IP, High32Bits(value));
5040 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5041 }
5042 } else if (constant->IsDoubleConstant()) {
5043 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005044 if (destination.IsFpuRegisterPair()) {
5045 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005046 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005047 DCHECK(destination.IsDoubleStackSlot()) << destination;
5048 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005049 __ LoadImmediate(IP, Low32Bits(int_value));
5050 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5051 __ LoadImmediate(IP, High32Bits(int_value));
5052 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5053 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005054 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005055 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005056 float value = constant->AsFloatConstant()->GetValue();
5057 if (destination.IsFpuRegister()) {
5058 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5059 } else {
5060 DCHECK(destination.IsStackSlot());
5061 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5062 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5063 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005064 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005065 }
5066}
5067
5068void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5069 __ Mov(IP, reg);
5070 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5071 __ StoreToOffset(kStoreWord, IP, SP, mem);
5072}
5073
5074void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5075 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5076 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5077 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5078 SP, mem1 + stack_offset);
5079 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5080 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5081 SP, mem2 + stack_offset);
5082 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5083}
5084
5085void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005086 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005087 Location source = move->GetSource();
5088 Location destination = move->GetDestination();
5089
5090 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005091 DCHECK_NE(source.AsRegister<Register>(), IP);
5092 DCHECK_NE(destination.AsRegister<Register>(), IP);
5093 __ Mov(IP, source.AsRegister<Register>());
5094 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5095 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005096 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005097 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005098 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005099 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005100 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5101 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005102 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005103 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005104 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005105 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005106 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005107 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005108 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005109 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005110 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5111 destination.AsRegisterPairHigh<Register>(),
5112 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005113 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005114 Register low_reg = source.IsRegisterPair()
5115 ? source.AsRegisterPairLow<Register>()
5116 : destination.AsRegisterPairLow<Register>();
5117 int mem = source.IsRegisterPair()
5118 ? destination.GetStackIndex()
5119 : source.GetStackIndex();
5120 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005121 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005122 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005123 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005124 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005125 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5126 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005127 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005128 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005129 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005130 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5131 DRegister reg = source.IsFpuRegisterPair()
5132 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5133 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5134 int mem = source.IsFpuRegisterPair()
5135 ? destination.GetStackIndex()
5136 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005137 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005138 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005139 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005140 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5141 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5142 : destination.AsFpuRegister<SRegister>();
5143 int mem = source.IsFpuRegister()
5144 ? destination.GetStackIndex()
5145 : source.GetStackIndex();
5146
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005147 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005148 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005149 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005150 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005151 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5152 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005153 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005154 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005155 }
5156}
5157
5158void ParallelMoveResolverARM::SpillScratch(int reg) {
5159 __ Push(static_cast<Register>(reg));
5160}
5161
5162void ParallelMoveResolverARM::RestoreScratch(int reg) {
5163 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005164}
5165
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005166void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005167 InvokeRuntimeCallingConvention calling_convention;
5168 CodeGenerator::CreateLoadClassLocationSummary(
5169 cls,
5170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005171 Location::RegisterLocation(R0),
5172 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005173}
5174
5175void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005176 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005177 if (cls->NeedsAccessCheck()) {
5178 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5179 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5180 cls,
5181 cls->GetDexPc(),
5182 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005183 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005184 return;
5185 }
5186
Roland Levillain3b359c72015-11-17 19:35:12 +00005187 Location out_loc = locations->Out();
5188 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005189 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005190
Calin Juravle580b6092015-10-06 17:35:58 +01005191 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005192 DCHECK(!cls->CanCallRuntime());
5193 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005194 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5195 GenerateGcRootFieldLoad(
5196 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005197 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005198 // /* GcRoot<mirror::Class>[] */ out =
5199 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005200 __ LoadFromOffset(kLoadWord,
5201 out,
5202 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005203 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005204 // /* GcRoot<mirror::Class> */ out = out[type_index]
5205 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005206
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005207 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5208 DCHECK(cls->CanCallRuntime());
5209 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5210 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5211 codegen_->AddSlowPath(slow_path);
5212 if (!cls->IsInDexCache()) {
5213 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5214 }
5215 if (cls->MustGenerateClinitCheck()) {
5216 GenerateClassInitializationCheck(slow_path, out);
5217 } else {
5218 __ Bind(slow_path->GetExitLabel());
5219 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005220 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005221 }
5222}
5223
5224void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5225 LocationSummary* locations =
5226 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5227 locations->SetInAt(0, Location::RequiresRegister());
5228 if (check->HasUses()) {
5229 locations->SetOut(Location::SameAsFirstInput());
5230 }
5231}
5232
5233void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005234 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005235 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005236 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005237 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005238 GenerateClassInitializationCheck(slow_path,
5239 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005240}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005241
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005242void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005243 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005244 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5245 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5246 __ b(slow_path->GetEntryLabel(), LT);
5247 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5248 // properly. Therefore, we do a memory fence.
5249 __ dmb(ISH);
5250 __ Bind(slow_path->GetExitLabel());
5251}
5252
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005253void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005254 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5255 ? LocationSummary::kCallOnSlowPath
5256 : LocationSummary::kNoCall;
5257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005258 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005259 locations->SetOut(Location::RequiresRegister());
5260}
5261
5262void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005263 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005264 Location out_loc = locations->Out();
5265 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005266 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005267
Roland Levillainc9285912015-12-18 10:38:42 +00005268 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5269 GenerateGcRootFieldLoad(
5270 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005271 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005272 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005273 // /* GcRoot<mirror::String> */ out = out[string_index]
5274 GenerateGcRootFieldLoad(
5275 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005276
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005277 if (!load->IsInDexCache()) {
5278 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5279 codegen_->AddSlowPath(slow_path);
5280 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5281 __ Bind(slow_path->GetExitLabel());
5282 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005283}
5284
David Brazdilcb1c0552015-08-04 16:22:25 +01005285static int32_t GetExceptionTlsOffset() {
5286 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5287}
5288
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005289void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5290 LocationSummary* locations =
5291 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5292 locations->SetOut(Location::RequiresRegister());
5293}
5294
5295void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005296 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005297 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5298}
5299
5300void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5301 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5302}
5303
5304void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005305 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005306 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005307}
5308
5309void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5310 LocationSummary* locations =
5311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5312 InvokeRuntimeCallingConvention calling_convention;
5313 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5314}
5315
5316void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5317 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005318 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005319 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005320}
5321
Roland Levillainc9285912015-12-18 10:38:42 +00005322static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5323 return kEmitCompilerReadBarrier &&
5324 (kUseBakerReadBarrier ||
5325 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5326 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5327 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5328}
5329
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005330void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005331 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005332 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5333 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005334 case TypeCheckKind::kExactCheck:
5335 case TypeCheckKind::kAbstractClassCheck:
5336 case TypeCheckKind::kClassHierarchyCheck:
5337 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005338 call_kind =
5339 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005340 break;
5341 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005342 case TypeCheckKind::kUnresolvedCheck:
5343 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005344 call_kind = LocationSummary::kCallOnSlowPath;
5345 break;
5346 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005347
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005349 locations->SetInAt(0, Location::RequiresRegister());
5350 locations->SetInAt(1, Location::RequiresRegister());
5351 // The "out" register is used as a temporary, so it overlaps with the inputs.
5352 // Note that TypeCheckSlowPathARM uses this register too.
5353 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5354 // When read barriers are enabled, we need a temporary register for
5355 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005356 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005357 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005358 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005359}
5360
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005361void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005362 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005363 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005364 Location obj_loc = locations->InAt(0);
5365 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005366 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005367 Location out_loc = locations->Out();
5368 Register out = out_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005369 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5370 locations->GetTemp(0) :
5371 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005372 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005373 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5374 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5375 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005376 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005377 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005378
5379 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005380 // avoid null check if we know obj is not null.
5381 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005382 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005383 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005384
Roland Levillain3b359c72015-11-17 19:35:12 +00005385 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005386 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005387
Roland Levillainc9285912015-12-18 10:38:42 +00005388 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005389 case TypeCheckKind::kExactCheck: {
5390 __ cmp(out, ShifterOperand(cls));
5391 // Classes must be equal for the instanceof to succeed.
5392 __ b(&zero, NE);
5393 __ LoadImmediate(out, 1);
5394 __ b(&done);
5395 break;
5396 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005397
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005398 case TypeCheckKind::kAbstractClassCheck: {
5399 // If the class is abstract, we eagerly fetch the super class of the
5400 // object to avoid doing a comparison we know will fail.
5401 Label loop;
5402 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005403 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005404 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005405 // If `out` is null, we use it for the result, and jump to `done`.
5406 __ CompareAndBranchIfZero(out, &done);
5407 __ cmp(out, ShifterOperand(cls));
5408 __ b(&loop, NE);
5409 __ LoadImmediate(out, 1);
5410 if (zero.IsLinked()) {
5411 __ b(&done);
5412 }
5413 break;
5414 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005415
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005416 case TypeCheckKind::kClassHierarchyCheck: {
5417 // Walk over the class hierarchy to find a match.
5418 Label loop, success;
5419 __ Bind(&loop);
5420 __ cmp(out, ShifterOperand(cls));
5421 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005422 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005423 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005424 __ CompareAndBranchIfNonZero(out, &loop);
5425 // If `out` is null, we use it for the result, and jump to `done`.
5426 __ b(&done);
5427 __ Bind(&success);
5428 __ LoadImmediate(out, 1);
5429 if (zero.IsLinked()) {
5430 __ b(&done);
5431 }
5432 break;
5433 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005434
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005435 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005436 // Do an exact check.
5437 Label exact_check;
5438 __ cmp(out, ShifterOperand(cls));
5439 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005440 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005441 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005442 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005443 // If `out` is null, we use it for the result, and jump to `done`.
5444 __ CompareAndBranchIfZero(out, &done);
5445 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5446 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5447 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005448 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005449 __ LoadImmediate(out, 1);
5450 __ b(&done);
5451 break;
5452 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005453
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005454 case TypeCheckKind::kArrayCheck: {
5455 __ cmp(out, ShifterOperand(cls));
5456 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005457 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5458 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005459 codegen_->AddSlowPath(slow_path);
5460 __ b(slow_path->GetEntryLabel(), NE);
5461 __ LoadImmediate(out, 1);
5462 if (zero.IsLinked()) {
5463 __ b(&done);
5464 }
5465 break;
5466 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005467
Calin Juravle98893e12015-10-02 21:05:03 +01005468 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005469 case TypeCheckKind::kInterfaceCheck: {
5470 // Note that we indeed only call on slow path, but we always go
5471 // into the slow path for the unresolved & interface check
5472 // cases.
5473 //
5474 // We cannot directly call the InstanceofNonTrivial runtime
5475 // entry point without resorting to a type checking slow path
5476 // here (i.e. by calling InvokeRuntime directly), as it would
5477 // require to assign fixed registers for the inputs of this
5478 // HInstanceOf instruction (following the runtime calling
5479 // convention), which might be cluttered by the potential first
5480 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005481 //
5482 // TODO: Introduce a new runtime entry point taking the object
5483 // to test (instead of its class) as argument, and let it deal
5484 // with the read barrier issues. This will let us refactor this
5485 // case of the `switch` code as it was previously (with a direct
5486 // call to the runtime not using a type checking slow path).
5487 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005488 DCHECK(locations->OnlyCallsOnSlowPath());
5489 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5490 /* is_fatal */ false);
5491 codegen_->AddSlowPath(slow_path);
5492 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005493 if (zero.IsLinked()) {
5494 __ b(&done);
5495 }
5496 break;
5497 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005498 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005500 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005501 __ Bind(&zero);
5502 __ LoadImmediate(out, 0);
5503 }
5504
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005505 if (done.IsLinked()) {
5506 __ Bind(&done);
5507 }
5508
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005509 if (slow_path != nullptr) {
5510 __ Bind(slow_path->GetExitLabel());
5511 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005512}
5513
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005514void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005515 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5516 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5517
Roland Levillain3b359c72015-11-17 19:35:12 +00005518 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5519 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005520 case TypeCheckKind::kExactCheck:
5521 case TypeCheckKind::kAbstractClassCheck:
5522 case TypeCheckKind::kClassHierarchyCheck:
5523 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005524 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5525 LocationSummary::kCallOnSlowPath :
5526 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005527 break;
5528 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005529 case TypeCheckKind::kUnresolvedCheck:
5530 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005531 call_kind = LocationSummary::kCallOnSlowPath;
5532 break;
5533 }
5534
Roland Levillain3b359c72015-11-17 19:35:12 +00005535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5536 locations->SetInAt(0, Location::RequiresRegister());
5537 locations->SetInAt(1, Location::RequiresRegister());
5538 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5539 locations->AddTemp(Location::RequiresRegister());
5540 // When read barriers are enabled, we need an additional temporary
5541 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005542 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005543 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005544 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005545}
5546
5547void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005548 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005549 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005550 Location obj_loc = locations->InAt(0);
5551 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005552 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005553 Location temp_loc = locations->GetTemp(0);
5554 Register temp = temp_loc.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005555 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5556 locations->GetTemp(1) :
5557 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005558 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005559 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5560 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5561 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005562
Roland Levillain3b359c72015-11-17 19:35:12 +00005563 bool is_type_check_slow_path_fatal =
5564 (type_check_kind == TypeCheckKind::kExactCheck ||
5565 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5566 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5567 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5568 !instruction->CanThrowIntoCatchBlock();
5569 SlowPathCode* type_check_slow_path =
5570 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5571 is_type_check_slow_path_fatal);
5572 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005573
5574 Label done;
5575 // Avoid null check if we know obj is not null.
5576 if (instruction->MustDoNullCheck()) {
5577 __ CompareAndBranchIfZero(obj, &done);
5578 }
5579
Roland Levillain3b359c72015-11-17 19:35:12 +00005580 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005581 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005582
Roland Levillain3b359c72015-11-17 19:35:12 +00005583 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005584 case TypeCheckKind::kExactCheck:
5585 case TypeCheckKind::kArrayCheck: {
5586 __ cmp(temp, ShifterOperand(cls));
5587 // Jump to slow path for throwing the exception or doing a
5588 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005589 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005590 break;
5591 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005592
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005593 case TypeCheckKind::kAbstractClassCheck: {
5594 // If the class is abstract, we eagerly fetch the super class of the
5595 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005596 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005597 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005598 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005599 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005600
5601 // If the class reference currently in `temp` is not null, jump
5602 // to the `compare_classes` label to compare it with the checked
5603 // class.
5604 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5605 // Otherwise, jump to the slow path to throw the exception.
5606 //
5607 // But before, move back the object's class into `temp` before
5608 // going into the slow path, as it has been overwritten in the
5609 // meantime.
5610 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005611 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005612 __ b(type_check_slow_path->GetEntryLabel());
5613
5614 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005615 __ cmp(temp, ShifterOperand(cls));
5616 __ b(&loop, NE);
5617 break;
5618 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005619
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005620 case TypeCheckKind::kClassHierarchyCheck: {
5621 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005622 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005623 __ Bind(&loop);
5624 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005625 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005626
Roland Levillain3b359c72015-11-17 19:35:12 +00005627 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillainc9285912015-12-18 10:38:42 +00005628 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005629
5630 // If the class reference currently in `temp` is not null, jump
5631 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005632 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005633 // Otherwise, jump to the slow path to throw the exception.
5634 //
5635 // But before, move back the object's class into `temp` before
5636 // going into the slow path, as it has been overwritten in the
5637 // meantime.
5638 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005639 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005640 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005641 break;
5642 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005643
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005645 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005646 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005647 __ cmp(temp, ShifterOperand(cls));
5648 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005649
5650 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005651 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillainc9285912015-12-18 10:38:42 +00005652 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005653
5654 // If the component type is not null (i.e. the object is indeed
5655 // an array), jump to label `check_non_primitive_component_type`
5656 // to further check that this component type is not a primitive
5657 // type.
5658 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5659 // Otherwise, jump to the slow path to throw the exception.
5660 //
5661 // But before, move back the object's class into `temp` before
5662 // going into the slow path, as it has been overwritten in the
5663 // meantime.
5664 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005665 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005666 __ b(type_check_slow_path->GetEntryLabel());
5667
5668 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005669 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005670 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5671 __ CompareAndBranchIfZero(temp, &done);
5672 // Same comment as above regarding `temp` and the slow path.
5673 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillainc9285912015-12-18 10:38:42 +00005674 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005675 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676 break;
5677 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005678
Calin Juravle98893e12015-10-02 21:05:03 +01005679 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005680 case TypeCheckKind::kInterfaceCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005681 // We always go into the type check slow path for the unresolved &
5682 // interface check cases.
5683 //
5684 // We cannot directly call the CheckCast runtime entry point
5685 // without resorting to a type checking slow path here (i.e. by
5686 // calling InvokeRuntime directly), as it would require to
5687 // assign fixed registers for the inputs of this HInstanceOf
5688 // instruction (following the runtime calling convention), which
5689 // might be cluttered by the potential first read barrier
5690 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005691 //
5692 // TODO: Introduce a new runtime entry point taking the object
5693 // to test (instead of its class) as argument, and let it deal
5694 // with the read barrier issues. This will let us refactor this
5695 // case of the `switch` code as it was previously (with a direct
5696 // call to the runtime not using a type checking slow path).
5697 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005698 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005699 break;
5700 }
5701 __ Bind(&done);
5702
Roland Levillain3b359c72015-11-17 19:35:12 +00005703 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005704}
5705
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005706void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5707 LocationSummary* locations =
5708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5709 InvokeRuntimeCallingConvention calling_convention;
5710 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5711}
5712
5713void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5714 codegen_->InvokeRuntime(instruction->IsEnter()
5715 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5716 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005717 instruction->GetDexPc(),
5718 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005719 if (instruction->IsEnter()) {
5720 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5721 } else {
5722 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5723 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005724}
5725
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005726void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5727void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5728void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005729
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005730void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005731 LocationSummary* locations =
5732 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5733 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5734 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005735 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005736 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005737 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005739}
5740
5741void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5742 HandleBitwiseOperation(instruction);
5743}
5744
5745void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5746 HandleBitwiseOperation(instruction);
5747}
5748
5749void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5750 HandleBitwiseOperation(instruction);
5751}
5752
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005753void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5754 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5755 if (value == 0xffffffffu) {
5756 if (out != first) {
5757 __ mov(out, ShifterOperand(first));
5758 }
5759 return;
5760 }
5761 if (value == 0u) {
5762 __ mov(out, ShifterOperand(0));
5763 return;
5764 }
5765 ShifterOperand so;
5766 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5767 __ and_(out, first, so);
5768 } else {
5769 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5770 __ bic(out, first, ShifterOperand(~value));
5771 }
5772}
5773
5774void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5775 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5776 if (value == 0u) {
5777 if (out != first) {
5778 __ mov(out, ShifterOperand(first));
5779 }
5780 return;
5781 }
5782 if (value == 0xffffffffu) {
5783 __ mvn(out, ShifterOperand(0));
5784 return;
5785 }
5786 ShifterOperand so;
5787 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5788 __ orr(out, first, so);
5789 } else {
5790 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5791 __ orn(out, first, ShifterOperand(~value));
5792 }
5793}
5794
5795void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5796 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5797 if (value == 0u) {
5798 if (out != first) {
5799 __ mov(out, ShifterOperand(first));
5800 }
5801 return;
5802 }
5803 __ eor(out, first, ShifterOperand(value));
5804}
5805
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005806void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5807 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005808 Location first = locations->InAt(0);
5809 Location second = locations->InAt(1);
5810 Location out = locations->Out();
5811
5812 if (second.IsConstant()) {
5813 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5814 uint32_t value_low = Low32Bits(value);
5815 if (instruction->GetResultType() == Primitive::kPrimInt) {
5816 Register first_reg = first.AsRegister<Register>();
5817 Register out_reg = out.AsRegister<Register>();
5818 if (instruction->IsAnd()) {
5819 GenerateAndConst(out_reg, first_reg, value_low);
5820 } else if (instruction->IsOr()) {
5821 GenerateOrrConst(out_reg, first_reg, value_low);
5822 } else {
5823 DCHECK(instruction->IsXor());
5824 GenerateEorConst(out_reg, first_reg, value_low);
5825 }
5826 } else {
5827 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5828 uint32_t value_high = High32Bits(value);
5829 Register first_low = first.AsRegisterPairLow<Register>();
5830 Register first_high = first.AsRegisterPairHigh<Register>();
5831 Register out_low = out.AsRegisterPairLow<Register>();
5832 Register out_high = out.AsRegisterPairHigh<Register>();
5833 if (instruction->IsAnd()) {
5834 GenerateAndConst(out_low, first_low, value_low);
5835 GenerateAndConst(out_high, first_high, value_high);
5836 } else if (instruction->IsOr()) {
5837 GenerateOrrConst(out_low, first_low, value_low);
5838 GenerateOrrConst(out_high, first_high, value_high);
5839 } else {
5840 DCHECK(instruction->IsXor());
5841 GenerateEorConst(out_low, first_low, value_low);
5842 GenerateEorConst(out_high, first_high, value_high);
5843 }
5844 }
5845 return;
5846 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005847
5848 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005849 Register first_reg = first.AsRegister<Register>();
5850 ShifterOperand second_reg(second.AsRegister<Register>());
5851 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005852 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005853 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005854 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005855 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005856 } else {
5857 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005858 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005859 }
5860 } else {
5861 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005862 Register first_low = first.AsRegisterPairLow<Register>();
5863 Register first_high = first.AsRegisterPairHigh<Register>();
5864 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5865 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5866 Register out_low = out.AsRegisterPairLow<Register>();
5867 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005868 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005869 __ and_(out_low, first_low, second_low);
5870 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005871 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005872 __ orr(out_low, first_low, second_low);
5873 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005874 } else {
5875 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005876 __ eor(out_low, first_low, second_low);
5877 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005878 }
5879 }
5880}
5881
Roland Levillainc9285912015-12-18 10:38:42 +00005882void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5883 Location out,
5884 uint32_t offset,
5885 Location temp) {
5886 Register out_reg = out.AsRegister<Register>();
5887 if (kEmitCompilerReadBarrier) {
5888 if (kUseBakerReadBarrier) {
5889 // Load with fast path based Baker's read barrier.
5890 // /* HeapReference<Object> */ out = *(out + offset)
5891 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5892 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
5893 } else {
5894 // Load with slow path based read barrier.
5895 // Save the value of `out` into `temp` before overwriting it
5896 // in the following move operation, as we will need it for the
5897 // read barrier below.
5898 __ Mov(temp.AsRegister<Register>(), out_reg);
5899 // /* HeapReference<Object> */ out = *(out + offset)
5900 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5901 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
5902 }
5903 } else {
5904 // Plain load with no read barrier.
5905 // /* HeapReference<Object> */ out = *(out + offset)
5906 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5907 __ MaybeUnpoisonHeapReference(out_reg);
5908 }
5909}
5910
5911void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5912 Location out,
5913 Location obj,
5914 uint32_t offset,
5915 Location temp) {
5916 Register out_reg = out.AsRegister<Register>();
5917 Register obj_reg = obj.AsRegister<Register>();
5918 if (kEmitCompilerReadBarrier) {
5919 if (kUseBakerReadBarrier) {
5920 // Load with fast path based Baker's read barrier.
5921 // /* HeapReference<Object> */ out = *(obj + offset)
5922 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5923 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
5924 } else {
5925 // Load with slow path based read barrier.
5926 // /* HeapReference<Object> */ out = *(obj + offset)
5927 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5928 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5929 }
5930 } else {
5931 // Plain load with no read barrier.
5932 // /* HeapReference<Object> */ out = *(obj + offset)
5933 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5934 __ MaybeUnpoisonHeapReference(out_reg);
5935 }
5936}
5937
5938void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
5939 Location root,
5940 Register obj,
5941 uint32_t offset) {
5942 Register root_reg = root.AsRegister<Register>();
5943 if (kEmitCompilerReadBarrier) {
5944 if (kUseBakerReadBarrier) {
5945 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5946 // Baker's read barrier are used:
5947 //
5948 // root = obj.field;
5949 // if (Thread::Current()->GetIsGcMarking()) {
5950 // root = ReadBarrier::Mark(root)
5951 // }
5952
5953 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5954 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5955 static_assert(
5956 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5957 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5958 "have different sizes.");
5959 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5960 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5961 "have different sizes.");
5962
5963 // Slow path used to mark the GC root `root`.
5964 SlowPathCode* slow_path =
5965 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
5966 codegen_->AddSlowPath(slow_path);
5967
5968 __ LoadFromOffset(
5969 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
5970 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
5971 __ Bind(slow_path->GetExitLabel());
5972 } else {
5973 // GC root loaded through a slow path for read barriers other
5974 // than Baker's.
5975 // /* GcRoot<mirror::Object>* */ root = obj + offset
5976 __ AddConstant(root_reg, obj, offset);
5977 // /* mirror::Object* */ root = root->Read()
5978 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5979 }
5980 } else {
5981 // Plain GC root load with no read barrier.
5982 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5983 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5984 // Note that GC roots are not affected by heap poisoning, thus we
5985 // do not have to unpoison `root_reg` here.
5986 }
5987}
5988
5989void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5990 Location ref,
5991 Register obj,
5992 uint32_t offset,
5993 Location temp,
5994 bool needs_null_check) {
5995 DCHECK(kEmitCompilerReadBarrier);
5996 DCHECK(kUseBakerReadBarrier);
5997
5998 // /* HeapReference<Object> */ ref = *(obj + offset)
5999 Location no_index = Location::NoLocation();
6000 GenerateReferenceLoadWithBakerReadBarrier(
6001 instruction, ref, obj, offset, no_index, temp, needs_null_check);
6002}
6003
6004void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6005 Location ref,
6006 Register obj,
6007 uint32_t data_offset,
6008 Location index,
6009 Location temp,
6010 bool needs_null_check) {
6011 DCHECK(kEmitCompilerReadBarrier);
6012 DCHECK(kUseBakerReadBarrier);
6013
6014 // /* HeapReference<Object> */ ref =
6015 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6016 GenerateReferenceLoadWithBakerReadBarrier(
6017 instruction, ref, obj, data_offset, index, temp, needs_null_check);
6018}
6019
6020void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6021 Location ref,
6022 Register obj,
6023 uint32_t offset,
6024 Location index,
6025 Location temp,
6026 bool needs_null_check) {
6027 DCHECK(kEmitCompilerReadBarrier);
6028 DCHECK(kUseBakerReadBarrier);
6029
6030 // In slow path based read barriers, the read barrier call is
6031 // inserted after the original load. However, in fast path based
6032 // Baker's read barriers, we need to perform the load of
6033 // mirror::Object::monitor_ *before* the original reference load.
6034 // This load-load ordering is required by the read barrier.
6035 // The fast path/slow path (for Baker's algorithm) should look like:
6036 //
6037 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6038 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6039 // HeapReference<Object> ref = *src; // Original reference load.
6040 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6041 // if (is_gray) {
6042 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6043 // }
6044 //
6045 // Note: the original implementation in ReadBarrier::Barrier is
6046 // slightly more complex as:
6047 // - it implements the load-load fence using a data dependency on
6048 // the high-bits of rb_state, which are expected to be all zeroes;
6049 // - it performs additional checks that we do not do here for
6050 // performance reasons.
6051
6052 Register ref_reg = ref.AsRegister<Register>();
6053 Register temp_reg = temp.AsRegister<Register>();
6054 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6055
6056 // /* int32_t */ monitor = obj->monitor_
6057 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6058 if (needs_null_check) {
6059 MaybeRecordImplicitNullCheck(instruction);
6060 }
6061 // /* LockWord */ lock_word = LockWord(monitor)
6062 static_assert(sizeof(LockWord) == sizeof(int32_t),
6063 "art::LockWord and int32_t have different sizes.");
6064 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6065 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6066 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6067 static_assert(
6068 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6069 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6070
6071 // Introduce a dependency on the high bits of rb_state, which shall
6072 // be all zeroes, to prevent load-load reordering, and without using
6073 // a memory barrier (which would be more expensive).
6074 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6075 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6076 // obj is unchanged by this operation, but its value now depends on
6077 // IP, which depends on temp_reg.
6078 __ add(obj, obj, ShifterOperand(IP));
6079
6080 // The actual reference load.
6081 if (index.IsValid()) {
6082 static_assert(
6083 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6084 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6085 // /* HeapReference<Object> */ ref =
6086 // *(obj + offset + index * sizeof(HeapReference<Object>))
6087 if (index.IsConstant()) {
6088 size_t computed_offset =
6089 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6090 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6091 } else {
6092 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6093 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6094 }
6095 } else {
6096 // /* HeapReference<Object> */ ref = *(obj + offset)
6097 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6098 }
6099
6100 // Object* ref = ref_addr->AsMirrorPtr()
6101 __ MaybeUnpoisonHeapReference(ref_reg);
6102
6103 // Slow path used to mark the object `ref` when it is gray.
6104 SlowPathCode* slow_path =
6105 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6106 AddSlowPath(slow_path);
6107
6108 // if (rb_state == ReadBarrier::gray_ptr_)
6109 // ref = ReadBarrier::Mark(ref);
6110 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6111 __ b(slow_path->GetEntryLabel(), EQ);
6112 __ Bind(slow_path->GetExitLabel());
6113}
6114
6115void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6116 Location out,
6117 Location ref,
6118 Location obj,
6119 uint32_t offset,
6120 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006121 DCHECK(kEmitCompilerReadBarrier);
6122
Roland Levillainc9285912015-12-18 10:38:42 +00006123 // Insert a slow path based read barrier *after* the reference load.
6124 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006125 // If heap poisoning is enabled, the unpoisoning of the loaded
6126 // reference will be carried out by the runtime within the slow
6127 // path.
6128 //
6129 // Note that `ref` currently does not get unpoisoned (when heap
6130 // poisoning is enabled), which is alright as the `ref` argument is
6131 // not used by the artReadBarrierSlow entry point.
6132 //
6133 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6134 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6135 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6136 AddSlowPath(slow_path);
6137
Roland Levillain3b359c72015-11-17 19:35:12 +00006138 __ b(slow_path->GetEntryLabel());
6139 __ Bind(slow_path->GetExitLabel());
6140}
6141
Roland Levillainc9285912015-12-18 10:38:42 +00006142void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6143 Location out,
6144 Location ref,
6145 Location obj,
6146 uint32_t offset,
6147 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006148 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006149 // Baker's read barriers shall be handled by the fast path
6150 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6151 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006152 // If heap poisoning is enabled, unpoisoning will be taken care of
6153 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006154 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006155 } else if (kPoisonHeapReferences) {
6156 __ UnpoisonHeapReference(out.AsRegister<Register>());
6157 }
6158}
6159
Roland Levillainc9285912015-12-18 10:38:42 +00006160void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6161 Location out,
6162 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006163 DCHECK(kEmitCompilerReadBarrier);
6164
Roland Levillainc9285912015-12-18 10:38:42 +00006165 // Insert a slow path based read barrier *after* the GC root load.
6166 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006167 // Note that GC roots are not affected by heap poisoning, so we do
6168 // not need to do anything special for this here.
6169 SlowPathCode* slow_path =
6170 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6171 AddSlowPath(slow_path);
6172
Roland Levillain3b359c72015-11-17 19:35:12 +00006173 __ b(slow_path->GetEntryLabel());
6174 __ Bind(slow_path->GetExitLabel());
6175}
6176
Vladimir Markodc151b22015-10-15 18:02:30 +01006177HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6178 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6179 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006180 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6181 // We disable pc-relative load when there is an irreducible loop, as the optimization
6182 // is incompatible with it.
6183 if (GetGraph()->HasIrreducibleLoops() &&
6184 (dispatch_info.method_load_kind ==
6185 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6186 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6187 }
6188
6189 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006190 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6191 if (&outer_dex_file != target_method.dex_file) {
6192 // Calls across dex files are more likely to exceed the available BL range,
6193 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6194 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6195 (desired_dispatch_info.method_load_kind ==
6196 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6197 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6198 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6199 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006200 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006201 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006202 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006203 0u
6204 };
6205 }
6206 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006207 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006208}
6209
Vladimir Markob4536b72015-11-24 13:45:23 +00006210Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6211 Register temp) {
6212 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6213 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6214 if (!invoke->GetLocations()->Intrinsified()) {
6215 return location.AsRegister<Register>();
6216 }
6217 // For intrinsics we allow any location, so it may be on the stack.
6218 if (!location.IsRegister()) {
6219 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6220 return temp;
6221 }
6222 // For register locations, check if the register was saved. If so, get it from the stack.
6223 // Note: There is a chance that the register was saved but not overwritten, so we could
6224 // save one load. However, since this is just an intrinsic slow path we prefer this
6225 // simple and more robust approach rather that trying to determine if that's the case.
6226 SlowPathCode* slow_path = GetCurrentSlowPath();
6227 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6228 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6229 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6230 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6231 return temp;
6232 }
6233 return location.AsRegister<Register>();
6234}
6235
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006236void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006237 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006238 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006239 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6240 // LR = code address from literal pool with link-time patch.
6241 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006242 break;
6243 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6244 // LR = invoke->GetDirectCodePtr();
6245 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006246 break;
6247 default:
6248 break;
6249 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006250
Vladimir Marko58155012015-08-19 12:49:41 +00006251 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6252 switch (invoke->GetMethodLoadKind()) {
6253 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6254 // temp = thread->string_init_entrypoint
6255 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6256 break;
6257 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006258 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006259 break;
6260 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6261 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6262 break;
6263 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6264 __ LoadLiteral(temp.AsRegister<Register>(),
6265 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6266 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006267 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6268 HArmDexCacheArraysBase* base =
6269 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6270 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6271 temp.AsRegister<Register>());
6272 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6273 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6274 break;
6275 }
Vladimir Marko58155012015-08-19 12:49:41 +00006276 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006277 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006278 Register method_reg;
6279 Register reg = temp.AsRegister<Register>();
6280 if (current_method.IsRegister()) {
6281 method_reg = current_method.AsRegister<Register>();
6282 } else {
6283 DCHECK(invoke->GetLocations()->Intrinsified());
6284 DCHECK(!current_method.IsValid());
6285 method_reg = reg;
6286 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6287 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006288 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6289 __ LoadFromOffset(kLoadWord,
6290 reg,
6291 method_reg,
6292 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006293 // temp = temp[index_in_cache]
6294 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6295 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6296 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006297 }
Vladimir Marko58155012015-08-19 12:49:41 +00006298 }
6299
6300 switch (invoke->GetCodePtrLocation()) {
6301 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6302 __ bl(GetFrameEntryLabel());
6303 break;
6304 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006305 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006306 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006307 // Arbitrarily branch to the BL itself, override at link time.
6308 __ bl(&relative_call_patches_.back().label);
6309 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006310 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6311 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6312 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006313 // LR()
6314 __ blx(LR);
6315 break;
6316 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6317 // LR = callee_method->entry_point_from_quick_compiled_code_
6318 __ LoadFromOffset(
6319 kLoadWord, LR, callee_method.AsRegister<Register>(),
6320 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6321 // LR()
6322 __ blx(LR);
6323 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006324 }
6325
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006326 DCHECK(!IsLeafMethod());
6327}
6328
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006329void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6330 Register temp = temp_location.AsRegister<Register>();
6331 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6332 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006333
6334 // Use the calling convention instead of the location of the receiver, as
6335 // intrinsics may have put the receiver in a different register. In the intrinsics
6336 // slow path, the arguments have been moved to the right place, so here we are
6337 // guaranteed that the receiver is the first register of the calling convention.
6338 InvokeDexCallingConvention calling_convention;
6339 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006340 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006341 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006342 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006343 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006344 // Instead of simply (possibly) unpoisoning `temp` here, we should
6345 // emit a read barrier for the previous class reference load.
6346 // However this is not required in practice, as this is an
6347 // intermediate/temporary reference and because the current
6348 // concurrent copying collector keeps the from-space memory
6349 // intact/accessible until the end of the marking phase (the
6350 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006351 __ MaybeUnpoisonHeapReference(temp);
6352 // temp = temp->GetMethodAt(method_offset);
6353 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6354 kArmWordSize).Int32Value();
6355 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6356 // LR = temp->GetEntryPoint();
6357 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6358 // LR();
6359 __ blx(LR);
6360}
6361
Vladimir Marko58155012015-08-19 12:49:41 +00006362void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6363 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006364 size_t size =
6365 method_patches_.size() +
6366 call_patches_.size() +
6367 relative_call_patches_.size() +
6368 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006369 linker_patches->reserve(size);
6370 for (const auto& entry : method_patches_) {
6371 const MethodReference& target_method = entry.first;
6372 Literal* literal = entry.second;
6373 DCHECK(literal->GetLabel()->IsBound());
6374 uint32_t literal_offset = literal->GetLabel()->Position();
6375 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6376 target_method.dex_file,
6377 target_method.dex_method_index));
6378 }
6379 for (const auto& entry : call_patches_) {
6380 const MethodReference& target_method = entry.first;
6381 Literal* literal = entry.second;
6382 DCHECK(literal->GetLabel()->IsBound());
6383 uint32_t literal_offset = literal->GetLabel()->Position();
6384 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6385 target_method.dex_file,
6386 target_method.dex_method_index));
6387 }
6388 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6389 uint32_t literal_offset = info.label.Position();
6390 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6391 info.target_method.dex_file,
6392 info.target_method.dex_method_index));
6393 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006394 for (const auto& pair : dex_cache_arrays_base_labels_) {
6395 HArmDexCacheArraysBase* base = pair.first;
6396 const DexCacheArraysBaseLabels* labels = &pair.second;
6397 const DexFile& dex_file = base->GetDexFile();
6398 size_t base_element_offset = base->GetElementOffset();
6399 DCHECK(labels->add_pc_label.IsBound());
6400 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6401 // Add MOVW patch.
6402 DCHECK(labels->movw_label.IsBound());
6403 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6404 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6405 &dex_file,
6406 add_pc_offset,
6407 base_element_offset));
6408 // Add MOVT patch.
6409 DCHECK(labels->movt_label.IsBound());
6410 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6411 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6412 &dex_file,
6413 add_pc_offset,
6414 base_element_offset));
6415 }
Vladimir Marko58155012015-08-19 12:49:41 +00006416}
6417
6418Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6419 MethodToLiteralMap* map) {
6420 // Look up the literal for target_method.
6421 auto lb = map->lower_bound(target_method);
6422 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6423 return lb->second;
6424 }
6425 // We don't have a literal for this method yet, insert a new one.
6426 Literal* literal = __ NewLiteral<uint32_t>(0u);
6427 map->PutBefore(lb, target_method, literal);
6428 return literal;
6429}
6430
6431Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6432 return DeduplicateMethodLiteral(target_method, &method_patches_);
6433}
6434
6435Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6436 return DeduplicateMethodLiteral(target_method, &call_patches_);
6437}
6438
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006439void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006440 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006441 LOG(FATAL) << "Unreachable";
6442}
6443
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006444void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006445 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006446 LOG(FATAL) << "Unreachable";
6447}
6448
Mark Mendellfe57faa2015-09-18 09:26:15 -04006449// Simple implementation of packed switch - generate cascaded compare/jumps.
6450void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6451 LocationSummary* locations =
6452 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6453 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006454 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006455 codegen_->GetAssembler()->IsThumb()) {
6456 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6457 if (switch_instr->GetStartValue() != 0) {
6458 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6459 }
6460 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006461}
6462
6463void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6464 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006465 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006466 LocationSummary* locations = switch_instr->GetLocations();
6467 Register value_reg = locations->InAt(0).AsRegister<Register>();
6468 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6469
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006470 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006471 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006472 Register temp_reg = IP;
6473 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6474 // the immediate, because IP is used as the destination register. For the other
6475 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6476 // and they can be encoded in the instruction without making use of IP register.
6477 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6478
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006479 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006480 // Jump to successors[0] if value == lower_bound.
6481 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6482 int32_t last_index = 0;
6483 for (; num_entries - last_index > 2; last_index += 2) {
6484 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6485 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6486 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6487 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6488 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6489 }
6490 if (num_entries - last_index == 2) {
6491 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006492 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006493 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006494 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006495
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006496 // And the default for any other value.
6497 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6498 __ b(codegen_->GetLabelOf(default_block));
6499 }
6500 } else {
6501 // Create a table lookup.
6502 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6503
6504 // Materialize a pointer to the switch table
6505 std::vector<Label*> labels(num_entries);
6506 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6507 for (uint32_t i = 0; i < num_entries; i++) {
6508 labels[i] = codegen_->GetLabelOf(successors[i]);
6509 }
6510 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6511
6512 // Remove the bias.
6513 Register key_reg;
6514 if (lower_bound != 0) {
6515 key_reg = locations->GetTemp(1).AsRegister<Register>();
6516 __ AddConstant(key_reg, value_reg, -lower_bound);
6517 } else {
6518 key_reg = value_reg;
6519 }
6520
6521 // Check whether the value is in the table, jump to default block if not.
6522 __ CmpConstant(key_reg, num_entries - 1);
6523 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6524
6525 // Load the displacement from the table.
6526 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6527
6528 // Dispatch is a direct add to the PC (for Thumb2).
6529 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006530 }
6531}
6532
Vladimir Markob4536b72015-11-24 13:45:23 +00006533void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6534 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6535 locations->SetOut(Location::RequiresRegister());
6536 codegen_->AddDexCacheArraysBase(base);
6537}
6538
6539void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6540 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6541 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6542 __ BindTrackedLabel(&labels->movw_label);
6543 __ movw(base_reg, 0u);
6544 __ BindTrackedLabel(&labels->movt_label);
6545 __ movt(base_reg, 0u);
6546 __ BindTrackedLabel(&labels->add_pc_label);
6547 __ add(base_reg, base_reg, ShifterOperand(PC));
6548}
6549
Andreas Gampe85b62f22015-09-09 13:15:38 -07006550void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6551 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006552 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006553 return;
6554 }
6555
6556 DCHECK_NE(type, Primitive::kPrimVoid);
6557
6558 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6559 if (return_loc.Equals(trg)) {
6560 return;
6561 }
6562
6563 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6564 // with the last branch.
6565 if (type == Primitive::kPrimLong) {
6566 HParallelMove parallel_move(GetGraph()->GetArena());
6567 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6568 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6569 GetMoveResolver()->EmitNativeCode(&parallel_move);
6570 } else if (type == Primitive::kPrimDouble) {
6571 HParallelMove parallel_move(GetGraph()->GetArena());
6572 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6573 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6574 GetMoveResolver()->EmitNativeCode(&parallel_move);
6575 } else {
6576 // Let the parallel move resolver take care of all of this.
6577 HParallelMove parallel_move(GetGraph()->GetArena());
6578 parallel_move.AddMove(return_loc, trg, type, nullptr);
6579 GetMoveResolver()->EmitNativeCode(&parallel_move);
6580 }
6581}
6582
Roland Levillain4d027112015-07-01 15:41:14 +01006583#undef __
6584#undef QUICK_ENTRY_POINT
6585
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006586} // namespace arm
6587} // namespace art