blob: f60c5e9d861c3224cb8c10a2ddfaf7819c6a898f [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain62a46b22015-06-01 18:24:13 +010062#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010063#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064
Andreas Gampe85b62f22015-09-09 13:15:38 -070065class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010067 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068
Alexandre Rames67555f72014-11-18 10:55:16 +000069 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010070 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000072 if (instruction_->CanThrowIntoCatchBlock()) {
73 // Live registers will be restored in the catch block if caught.
74 SaveLiveRegisters(codegen, instruction_->GetLocations());
75 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010076 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000077 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000078 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 }
80
Alexandre Rames8158f282015-08-07 10:26:17 +010081 bool IsFatal() const OVERRIDE { return true; }
82
Alexandre Rames9931f312015-06-19 14:47:01 +010083 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
84
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010086 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
88};
89
Andreas Gampe85b62f22015-09-09 13:15:38 -070090class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000091 public:
92 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
93
Alexandre Rames67555f72014-11-18 10:55:16 +000094 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000095 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
96 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000097 if (instruction_->CanThrowIntoCatchBlock()) {
98 // Live registers will be restored in the catch block if caught.
99 SaveLiveRegisters(codegen, instruction_->GetLocations());
100 }
Calin Juravled0d48522014-11-04 16:40:20 +0000101 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000102 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000103 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000104 }
105
Alexandre Rames8158f282015-08-07 10:26:17 +0100106 bool IsFatal() const OVERRIDE { return true; }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
111 HDivZeroCheck* const instruction_;
112 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
113};
114
Andreas Gampe85b62f22015-09-09 13:15:38 -0700115class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000117 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100118 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames67555f72014-11-18 10:55:16 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100124 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000125 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000126 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000127 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 if (successor_ == nullptr) {
129 __ b(GetReturnLabel());
130 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 }
134
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 Label* GetReturnLabel() {
136 DCHECK(successor_ == nullptr);
137 return &return_label_;
138 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100140 HBasicBlock* GetSuccessor() const {
141 return successor_;
142 }
143
Alexandre Rames9931f312015-06-19 14:47:01 +0100144 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
145
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 private:
147 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 // If not null, the block to branch to after the suspend check.
149 HBasicBlock* const successor_;
150
151 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152 Label return_label_;
153
154 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
155};
156
Andreas Gampe85b62f22015-09-09 13:15:38 -0700157class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100159 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
160 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161
Alexandre Rames67555f72014-11-18 10:55:16 +0000162 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100164 LocationSummary* locations = instruction_->GetLocations();
165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 if (instruction_->CanThrowIntoCatchBlock()) {
168 // Live registers will be restored in the catch block if caught.
169 SaveLiveRegisters(codegen, instruction_->GetLocations());
170 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000171 // We're moving two locations to locations that could overlap, so we need a parallel
172 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100175 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000176 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100177 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100179 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
180 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100181 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000182 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000183 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 }
185
Alexandre Rames8158f282015-08-07 10:26:17 +0100186 bool IsFatal() const OVERRIDE { return true; }
187
Alexandre Rames9931f312015-06-19 14:47:01 +0100188 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
189
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100191 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192
193 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
194};
195
Andreas Gampe85b62f22015-09-09 13:15:38 -0700196class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100197 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000198 LoadClassSlowPathARM(HLoadClass* cls,
199 HInstruction* at,
200 uint32_t dex_pc,
201 bool do_clinit)
202 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
203 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
204 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205
Alexandre Rames67555f72014-11-18 10:55:16 +0000206 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 LocationSummary* locations = at_->GetLocations();
208
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
210 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 int32_t entry_point_offset = do_clinit_
216 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
217 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000218 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000219 if (do_clinit_) {
220 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
221 } else {
222 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
223 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224
225 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000226 Location out = locations->Out();
227 if (out.IsValid()) {
228 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
230 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232 __ b(GetExitLabel());
233 }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
236
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100237 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 // The class this slow path will load.
239 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 // The instruction where this slow path is happening.
242 // (Might be the load class or an initialization check).
243 HInstruction* const at_;
244
245 // The dex PC of `at_`.
246 const uint32_t dex_pc_;
247
248 // Whether to initialize the class.
249 const bool do_clinit_;
250
251 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252};
253
Andreas Gampe85b62f22015-09-09 13:15:38 -0700254class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255 public:
256 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
257
Alexandre Rames67555f72014-11-18 10:55:16 +0000258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000259 LocationSummary* locations = instruction_->GetLocations();
260 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
261
262 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
263 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000265
266 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000268 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000270 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
272
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 __ b(GetExitLabel());
275 }
276
Alexandre Rames9931f312015-06-19 14:47:01 +0100277 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
278
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000279 private:
280 HLoadString* const instruction_;
281
282 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
283};
284
Andreas Gampe85b62f22015-09-09 13:15:38 -0700285class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000287 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
288 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100292 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
293 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 DCHECK(instruction_->IsCheckCast()
295 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
298 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000300 if (!is_fatal_) {
301 SaveLiveRegisters(codegen, locations);
302 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
304 // We're moving two locations to locations that could overlap, so we need a parallel
305 // move resolver.
306 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100308 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100310 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100311 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100312 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
313 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100316 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
317 instruction_,
318 instruction_->GetDexPc(),
319 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000320 CheckEntrypointTypes<
321 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
323 } else {
324 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
326 instruction_,
327 instruction_->GetDexPc(),
328 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000329 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 if (!is_fatal_) {
333 RestoreLiveRegisters(codegen, locations);
334 __ b(GetExitLabel());
335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 }
337
Alexandre Rames9931f312015-06-19 14:47:01 +0100338 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340 bool IsFatal() const OVERRIDE { return is_fatal_; }
341
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
347};
348
Andreas Gampe85b62f22015-09-09 13:15:38 -0700349class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700350 public:
Aart Bik42249c32016-01-07 15:33:50 -0800351 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700352 : instruction_(instruction) {}
353
354 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800355 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700356 __ Bind(GetEntryLabel());
357 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800358 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
359 instruction_,
360 instruction_->GetDexPc(),
361 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000362 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700363 }
364
Alexandre Rames9931f312015-06-19 14:47:01 +0100365 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
366
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 private:
Aart Bik42249c32016-01-07 15:33:50 -0800368 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
370};
371
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100372class ArraySetSlowPathARM : public SlowPathCode {
373 public:
374 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
375
376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
377 LocationSummary* locations = instruction_->GetLocations();
378 __ Bind(GetEntryLabel());
379 SaveLiveRegisters(codegen, locations);
380
381 InvokeRuntimeCallingConvention calling_convention;
382 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
383 parallel_move.AddMove(
384 locations->InAt(0),
385 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
386 Primitive::kPrimNot,
387 nullptr);
388 parallel_move.AddMove(
389 locations->InAt(1),
390 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
391 Primitive::kPrimInt,
392 nullptr);
393 parallel_move.AddMove(
394 locations->InAt(2),
395 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
396 Primitive::kPrimNot,
397 nullptr);
398 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
399
400 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
401 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
402 instruction_,
403 instruction_->GetDexPc(),
404 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000405 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406 RestoreLiveRegisters(codegen, locations);
407 __ b(GetExitLabel());
408 }
409
410 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
411
412 private:
413 HInstruction* const instruction_;
414
415 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
416};
417
Roland Levillainc9285912015-12-18 10:38:42 +0000418// Slow path marking an object during a read barrier.
419class ReadBarrierMarkSlowPathARM : public SlowPathCode {
420 public:
421 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location out, Location obj)
422 : instruction_(instruction), out_(out), obj_(obj) {
423 DCHECK(kEmitCompilerReadBarrier);
424 }
425
426 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
427
428 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
429 LocationSummary* locations = instruction_->GetLocations();
430 Register reg_out = out_.AsRegister<Register>();
431 DCHECK(locations->CanCall());
432 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
433 DCHECK(instruction_->IsInstanceFieldGet() ||
434 instruction_->IsStaticFieldGet() ||
435 instruction_->IsArrayGet() ||
436 instruction_->IsLoadClass() ||
437 instruction_->IsLoadString() ||
438 instruction_->IsInstanceOf() ||
439 instruction_->IsCheckCast())
440 << "Unexpected instruction in read barrier marking slow path: "
441 << instruction_->DebugName();
442
443 __ Bind(GetEntryLabel());
444 SaveLiveRegisters(codegen, locations);
445
446 InvokeRuntimeCallingConvention calling_convention;
447 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
448 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
449 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
450 instruction_,
451 instruction_->GetDexPc(),
452 this);
453 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
454 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
455
456 RestoreLiveRegisters(codegen, locations);
457 __ b(GetExitLabel());
458 }
459
460 private:
461 HInstruction* const instruction_;
462 const Location out_;
463 const Location obj_;
464
465 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
466};
467
Roland Levillain3b359c72015-11-17 19:35:12 +0000468// Slow path generating a read barrier for a heap reference.
469class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
470 public:
471 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
472 Location out,
473 Location ref,
474 Location obj,
475 uint32_t offset,
476 Location index)
477 : instruction_(instruction),
478 out_(out),
479 ref_(ref),
480 obj_(obj),
481 offset_(offset),
482 index_(index) {
483 DCHECK(kEmitCompilerReadBarrier);
484 // If `obj` is equal to `out` or `ref`, it means the initial object
485 // has been overwritten by (or after) the heap object reference load
486 // to be instrumented, e.g.:
487 //
488 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000489 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000490 //
491 // In that case, we have lost the information about the original
492 // object, and the emitted read barrier cannot work properly.
493 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
494 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
495 }
496
497 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
498 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
499 LocationSummary* locations = instruction_->GetLocations();
500 Register reg_out = out_.AsRegister<Register>();
501 DCHECK(locations->CanCall());
502 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
503 DCHECK(!instruction_->IsInvoke() ||
504 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillainc9285912015-12-18 10:38:42 +0000505 instruction_->GetLocations()->Intrinsified()))
506 << "Unexpected instruction in read barrier for heap reference slow path: "
507 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000508
509 __ Bind(GetEntryLabel());
510 SaveLiveRegisters(codegen, locations);
511
512 // We may have to change the index's value, but as `index_` is a
513 // constant member (like other "inputs" of this slow path),
514 // introduce a copy of it, `index`.
515 Location index = index_;
516 if (index_.IsValid()) {
517 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
518 if (instruction_->IsArrayGet()) {
519 // Compute the actual memory offset and store it in `index`.
520 Register index_reg = index_.AsRegister<Register>();
521 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
522 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
523 // We are about to change the value of `index_reg` (see the
524 // calls to art::arm::Thumb2Assembler::Lsl and
525 // art::arm::Thumb2Assembler::AddConstant below), but it has
526 // not been saved by the previous call to
527 // art::SlowPathCode::SaveLiveRegisters, as it is a
528 // callee-save register --
529 // art::SlowPathCode::SaveLiveRegisters does not consider
530 // callee-save registers, as it has been designed with the
531 // assumption that callee-save registers are supposed to be
532 // handled by the called function. So, as a callee-save
533 // register, `index_reg` _would_ eventually be saved onto
534 // the stack, but it would be too late: we would have
535 // changed its value earlier. Therefore, we manually save
536 // it here into another freely available register,
537 // `free_reg`, chosen of course among the caller-save
538 // registers (as a callee-save `free_reg` register would
539 // exhibit the same problem).
540 //
541 // Note we could have requested a temporary register from
542 // the register allocator instead; but we prefer not to, as
543 // this is a slow path, and we know we can find a
544 // caller-save register that is available.
545 Register free_reg = FindAvailableCallerSaveRegister(codegen);
546 __ Mov(free_reg, index_reg);
547 index_reg = free_reg;
548 index = Location::RegisterLocation(index_reg);
549 } else {
550 // The initial register stored in `index_` has already been
551 // saved in the call to art::SlowPathCode::SaveLiveRegisters
552 // (as it is not a callee-save register), so we can freely
553 // use it.
554 }
555 // Shifting the index value contained in `index_reg` by the scale
556 // factor (2) cannot overflow in practice, as the runtime is
557 // unable to allocate object arrays with a size larger than
558 // 2^26 - 1 (that is, 2^28 - 4 bytes).
559 __ Lsl(index_reg, index_reg, TIMES_4);
560 static_assert(
561 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
562 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
563 __ AddConstant(index_reg, index_reg, offset_);
564 } else {
565 DCHECK(instruction_->IsInvoke());
566 DCHECK(instruction_->GetLocations()->Intrinsified());
567 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
568 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
569 << instruction_->AsInvoke()->GetIntrinsic();
570 DCHECK_EQ(offset_, 0U);
571 DCHECK(index_.IsRegisterPair());
572 // UnsafeGet's offset location is a register pair, the low
573 // part contains the correct offset.
574 index = index_.ToLow();
575 }
576 }
577
578 // We're moving two or three locations to locations that could
579 // overlap, so we need a parallel move resolver.
580 InvokeRuntimeCallingConvention calling_convention;
581 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
582 parallel_move.AddMove(ref_,
583 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
584 Primitive::kPrimNot,
585 nullptr);
586 parallel_move.AddMove(obj_,
587 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
588 Primitive::kPrimNot,
589 nullptr);
590 if (index.IsValid()) {
591 parallel_move.AddMove(index,
592 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
593 Primitive::kPrimInt,
594 nullptr);
595 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
596 } else {
597 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
598 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
599 }
600 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
601 instruction_,
602 instruction_->GetDexPc(),
603 this);
604 CheckEntrypointTypes<
605 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
606 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
607
608 RestoreLiveRegisters(codegen, locations);
609 __ b(GetExitLabel());
610 }
611
612 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
613
614 private:
615 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
616 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
617 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
618 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
619 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
620 return static_cast<Register>(i);
621 }
622 }
623 // We shall never fail to find a free caller-save register, as
624 // there are more than two core caller-save registers on ARM
625 // (meaning it is possible to find one which is different from
626 // `ref` and `obj`).
627 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
628 LOG(FATAL) << "Could not find a free caller-save register";
629 UNREACHABLE();
630 }
631
632 HInstruction* const instruction_;
633 const Location out_;
634 const Location ref_;
635 const Location obj_;
636 const uint32_t offset_;
637 // An additional location containing an index to an array.
638 // Only used for HArrayGet and the UnsafeGetObject &
639 // UnsafeGetObjectVolatile intrinsics.
640 const Location index_;
641
642 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
643};
644
645// Slow path generating a read barrier for a GC root.
646class ReadBarrierForRootSlowPathARM : public SlowPathCode {
647 public:
648 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Roland Levillainc9285912015-12-18 10:38:42 +0000649 : instruction_(instruction), out_(out), root_(root) {
650 DCHECK(kEmitCompilerReadBarrier);
651 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000652
653 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
654 LocationSummary* locations = instruction_->GetLocations();
655 Register reg_out = out_.AsRegister<Register>();
656 DCHECK(locations->CanCall());
657 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000658 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
659 << "Unexpected instruction in read barrier for GC root slow path: "
660 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000661
662 __ Bind(GetEntryLabel());
663 SaveLiveRegisters(codegen, locations);
664
665 InvokeRuntimeCallingConvention calling_convention;
666 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
667 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
668 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
669 instruction_,
670 instruction_->GetDexPc(),
671 this);
672 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
673 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
674
675 RestoreLiveRegisters(codegen, locations);
676 __ b(GetExitLabel());
677 }
678
679 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
680
681 private:
682 HInstruction* const instruction_;
683 const Location out_;
684 const Location root_;
685
686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
687};
688
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000689#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100690#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700691
Aart Bike9f37602015-10-09 11:15:55 -0700692inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700693 switch (cond) {
694 case kCondEQ: return EQ;
695 case kCondNE: return NE;
696 case kCondLT: return LT;
697 case kCondLE: return LE;
698 case kCondGT: return GT;
699 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700700 case kCondB: return LO;
701 case kCondBE: return LS;
702 case kCondA: return HI;
703 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700704 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100705 LOG(FATAL) << "Unreachable";
706 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700707}
708
Aart Bike9f37602015-10-09 11:15:55 -0700709// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100710inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700711 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100712 case kCondEQ: return EQ;
713 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700714 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100715 case kCondLT: return LO;
716 case kCondLE: return LS;
717 case kCondGT: return HI;
718 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700719 // Unsigned remain unchanged.
720 case kCondB: return LO;
721 case kCondBE: return LS;
722 case kCondA: return HI;
723 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700724 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100725 LOG(FATAL) << "Unreachable";
726 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700727}
728
Vladimir Markod6e069b2016-01-18 11:11:01 +0000729inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
730 // The ARM condition codes can express all the necessary branches, see the
731 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
732 // There is no dex instruction or HIR that would need the missing conditions
733 // "equal or unordered" or "not equal".
734 switch (cond) {
735 case kCondEQ: return EQ;
736 case kCondNE: return NE /* unordered */;
737 case kCondLT: return gt_bias ? CC : LT /* unordered */;
738 case kCondLE: return gt_bias ? LS : LE /* unordered */;
739 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
740 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
741 default:
742 LOG(FATAL) << "UNREACHABLE";
743 UNREACHABLE();
744 }
745}
746
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100747void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100748 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749}
750
751void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100752 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100753}
754
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100755size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
756 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
757 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100758}
759
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100760size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
761 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
762 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100763}
764
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000765size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
766 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
767 return kArmWordSize;
768}
769
770size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
771 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
772 return kArmWordSize;
773}
774
Calin Juravle34166012014-12-19 17:22:29 +0000775CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000776 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100777 const CompilerOptions& compiler_options,
778 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000779 : CodeGenerator(graph,
780 kNumberOfCoreRegisters,
781 kNumberOfSRegisters,
782 kNumberOfRegisterPairs,
783 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
784 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000785 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
786 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100787 compiler_options,
788 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100789 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100790 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100791 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100792 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000793 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000794 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100795 method_patches_(MethodReferenceComparator(),
796 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
797 call_patches_(MethodReferenceComparator(),
798 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000799 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
800 dex_cache_arrays_base_labels_(std::less<HArmDexCacheArraysBase*>(),
801 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700802 // Always save the LR register to mimic Quick.
803 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100804}
805
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000806void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
807 // Ensure that we fix up branches and literal loads and emit the literal pool.
808 __ FinalizeCode();
809
810 // Adjust native pc offsets in stack maps.
811 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
812 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
813 uint32_t new_position = __ GetAdjustedPosition(old_position);
814 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
815 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100816 // Adjust pc offsets for the disassembly information.
817 if (disasm_info_ != nullptr) {
818 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
819 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
820 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
821 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
822 it.second.start = __ GetAdjustedPosition(it.second.start);
823 it.second.end = __ GetAdjustedPosition(it.second.end);
824 }
825 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
826 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
827 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
828 }
829 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000830
831 CodeGenerator::Finalize(allocator);
832}
833
David Brazdil58282f42016-01-14 12:45:10 +0000834void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100835 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100836 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100837
838 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100839 blocked_core_registers_[SP] = true;
840 blocked_core_registers_[LR] = true;
841 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100842
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100844 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100845
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100846 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100847 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100848
David Brazdil58282f42016-01-14 12:45:10 +0000849 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100850 // Stubs do not save callee-save floating point registers. If the graph
851 // is debuggable, we need to deal with these registers differently. For
852 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000853 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
854 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
855 }
856 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100857
858 UpdateBlockedPairRegisters();
859}
860
861void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
862 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
863 ArmManagedRegister current =
864 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
865 if (blocked_core_registers_[current.AsRegisterPairLow()]
866 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
867 blocked_register_pairs_[i] = true;
868 }
869 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870}
871
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100872InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800873 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100874 assembler_(codegen->GetAssembler()),
875 codegen_(codegen) {}
876
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000877void CodeGeneratorARM::ComputeSpillMask() {
878 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
879 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000880 // There is no easy instruction to restore just the PC on thumb2. We spill and
881 // restore another arbitrary register.
882 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000883 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
884 // We use vpush and vpop for saving and restoring floating point registers, which take
885 // a SRegister and the number of registers to save/restore after that SRegister. We
886 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
887 // but in the range.
888 if (fpu_spill_mask_ != 0) {
889 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
890 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
891 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
892 fpu_spill_mask_ |= (1 << i);
893 }
894 }
895}
896
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100897static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100898 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100899}
900
901static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100902 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100903}
904
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000905void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000906 bool skip_overflow_check =
907 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000908 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000909 __ Bind(&frame_entry_label_);
910
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000911 if (HasEmptyFrame()) {
912 return;
913 }
914
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100915 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000916 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
917 __ LoadFromOffset(kLoadWord, IP, IP, 0);
918 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100919 }
920
Andreas Gampe501fd632015-09-10 16:11:06 -0700921 __ PushList(core_spill_mask_);
922 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
923 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000924 if (fpu_spill_mask_ != 0) {
925 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
926 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100927 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100928 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000929 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930 int adjust = GetFrameSize() - FrameEntrySpillSize();
931 __ AddConstant(SP, -adjust);
932 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100933 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000934}
935
936void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000937 if (HasEmptyFrame()) {
938 __ bx(LR);
939 return;
940 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100941 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100942 int adjust = GetFrameSize() - FrameEntrySpillSize();
943 __ AddConstant(SP, adjust);
944 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000945 if (fpu_spill_mask_ != 0) {
946 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
947 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100948 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
949 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000950 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700951 // Pop LR into PC to return.
952 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
953 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
954 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100955 __ cfi().RestoreState();
956 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957}
958
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100959void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700960 Label* label = GetLabelOf(block);
961 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000962}
963
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100964Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
965 switch (load->GetType()) {
966 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100968 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100969
970 case Primitive::kPrimInt:
971 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100972 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100974
975 case Primitive::kPrimBoolean:
976 case Primitive::kPrimByte:
977 case Primitive::kPrimChar:
978 case Primitive::kPrimShort:
979 case Primitive::kPrimVoid:
980 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700981 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100982 }
983
984 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700985 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100986}
987
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100988Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100989 switch (type) {
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimInt:
995 case Primitive::kPrimNot: {
996 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000997 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100999 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001000 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001001 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001002 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001003 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001005 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001006 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001007 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001009 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001011 if (calling_convention.GetRegisterAt(index) == R1) {
1012 // Skip R1, and use R2_R3 instead.
1013 gp_index_++;
1014 index++;
1015 }
1016 }
1017 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1018 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001019 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001020
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001021 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001022 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001023 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001024 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1025 }
1026 }
1027
1028 case Primitive::kPrimFloat: {
1029 uint32_t stack_index = stack_index_++;
1030 if (float_index_ % 2 == 0) {
1031 float_index_ = std::max(double_index_, float_index_);
1032 }
1033 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1034 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1035 } else {
1036 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1037 }
1038 }
1039
1040 case Primitive::kPrimDouble: {
1041 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1042 uint32_t stack_index = stack_index_;
1043 stack_index_ += 2;
1044 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1045 uint32_t index = double_index_;
1046 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001047 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001048 calling_convention.GetFpuRegisterAt(index),
1049 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001050 DCHECK(ExpectedPairLayout(result));
1051 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001052 } else {
1053 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001054 }
1055 }
1056
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001057 case Primitive::kPrimVoid:
1058 LOG(FATAL) << "Unexpected parameter type " << type;
1059 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001060 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001061 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001062}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001063
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001064Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001065 switch (type) {
1066 case Primitive::kPrimBoolean:
1067 case Primitive::kPrimByte:
1068 case Primitive::kPrimChar:
1069 case Primitive::kPrimShort:
1070 case Primitive::kPrimInt:
1071 case Primitive::kPrimNot: {
1072 return Location::RegisterLocation(R0);
1073 }
1074
1075 case Primitive::kPrimFloat: {
1076 return Location::FpuRegisterLocation(S0);
1077 }
1078
1079 case Primitive::kPrimLong: {
1080 return Location::RegisterPairLocation(R0, R1);
1081 }
1082
1083 case Primitive::kPrimDouble: {
1084 return Location::FpuRegisterPairLocation(S0, S1);
1085 }
1086
1087 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001088 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001089 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001090
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001091 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001092}
1093
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001094Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1095 return Location::RegisterLocation(kMethodRegisterArgument);
1096}
1097
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098void CodeGeneratorARM::Move32(Location destination, Location source) {
1099 if (source.Equals(destination)) {
1100 return;
1101 }
1102 if (destination.IsRegister()) {
1103 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001106 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001108 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 } else if (destination.IsFpuRegister()) {
1111 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001112 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001114 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001115 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001116 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001119 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001121 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001123 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001125 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001126 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1127 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 }
1129 }
1130}
1131
1132void CodeGeneratorARM::Move64(Location destination, Location source) {
1133 if (source.Equals(destination)) {
1134 return;
1135 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001136 if (destination.IsRegisterPair()) {
1137 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001138 EmitParallelMoves(
1139 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1140 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001141 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001142 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001143 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1144 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001145 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001146 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001147 } else if (source.IsFpuRegisterPair()) {
1148 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1149 destination.AsRegisterPairHigh<Register>(),
1150 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
1152 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001153 DCHECK(ExpectedPairLayout(destination));
1154 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1155 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001157 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001159 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1160 SP,
1161 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001162 } else if (source.IsRegisterPair()) {
1163 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1164 source.AsRegisterPairLow<Register>(),
1165 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001167 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 } else {
1170 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001172 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 if (source.AsRegisterPairLow<Register>() == R1) {
1174 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001175 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1176 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 SP, destination.GetStackIndex());
1180 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001181 } else if (source.IsFpuRegisterPair()) {
1182 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1183 SP,
1184 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 } else {
1186 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001187 EmitParallelMoves(
1188 Location::StackSlot(source.GetStackIndex()),
1189 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001190 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001191 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001192 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1193 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 }
1195 }
1196}
1197
Calin Juravle175dc732015-08-25 15:42:32 +01001198void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1199 DCHECK(location.IsRegister());
1200 __ LoadImmediate(location.AsRegister<Register>(), value);
1201}
1202
Calin Juravlee460d1d2015-09-29 04:52:17 +01001203void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001204 HParallelMove move(GetGraph()->GetArena());
1205 move.AddMove(src, dst, dst_type, nullptr);
1206 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207}
1208
1209void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1210 if (location.IsRegister()) {
1211 locations->AddTemp(location);
1212 } else if (location.IsRegisterPair()) {
1213 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1214 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1215 } else {
1216 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1217 }
1218}
1219
Calin Juravle175dc732015-08-25 15:42:32 +01001220void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1221 HInstruction* instruction,
1222 uint32_t dex_pc,
1223 SlowPathCode* slow_path) {
1224 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1225 instruction,
1226 dex_pc,
1227 slow_path);
1228}
1229
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001230void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1231 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001232 uint32_t dex_pc,
1233 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001234 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001235 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1236 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001237 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001238}
1239
David Brazdilfc6a86a2015-06-26 10:33:45 +00001240void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001241 DCHECK(!successor->IsExitBlock());
1242
1243 HBasicBlock* block = got->GetBlock();
1244 HInstruction* previous = got->GetPrevious();
1245
1246 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001247 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001248 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1249 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1250 return;
1251 }
1252
1253 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1254 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1255 }
1256 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001257 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001258 }
1259}
1260
David Brazdilfc6a86a2015-06-26 10:33:45 +00001261void LocationsBuilderARM::VisitGoto(HGoto* got) {
1262 got->SetLocations(nullptr);
1263}
1264
1265void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1266 HandleGoto(got, got->GetSuccessor());
1267}
1268
1269void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1270 try_boundary->SetLocations(nullptr);
1271}
1272
1273void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1274 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1275 if (!successor->IsExitBlock()) {
1276 HandleGoto(try_boundary, successor);
1277 }
1278}
1279
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001280void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001281 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001282}
1283
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001284void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001285}
1286
Roland Levillain4fa13f62015-07-06 18:11:54 +01001287void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1288 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001289 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001290 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001291 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001292}
1293
1294void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1295 Label* true_label,
1296 Label* false_label) {
1297 LocationSummary* locations = cond->GetLocations();
1298 Location left = locations->InAt(0);
1299 Location right = locations->InAt(1);
1300 IfCondition if_cond = cond->GetCondition();
1301
1302 Register left_high = left.AsRegisterPairHigh<Register>();
1303 Register left_low = left.AsRegisterPairLow<Register>();
1304 IfCondition true_high_cond = if_cond;
1305 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001306 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001307
1308 // Set the conditions for the test, remembering that == needs to be
1309 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001310 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001311 switch (if_cond) {
1312 case kCondEQ:
1313 case kCondNE:
1314 // Nothing to do.
1315 break;
1316 case kCondLT:
1317 false_high_cond = kCondGT;
1318 break;
1319 case kCondLE:
1320 true_high_cond = kCondLT;
1321 break;
1322 case kCondGT:
1323 false_high_cond = kCondLT;
1324 break;
1325 case kCondGE:
1326 true_high_cond = kCondGT;
1327 break;
Aart Bike9f37602015-10-09 11:15:55 -07001328 case kCondB:
1329 false_high_cond = kCondA;
1330 break;
1331 case kCondBE:
1332 true_high_cond = kCondB;
1333 break;
1334 case kCondA:
1335 false_high_cond = kCondB;
1336 break;
1337 case kCondAE:
1338 true_high_cond = kCondA;
1339 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001340 }
1341 if (right.IsConstant()) {
1342 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1343 int32_t val_low = Low32Bits(value);
1344 int32_t val_high = High32Bits(value);
1345
Vladimir Markoac6ac102015-12-17 12:14:00 +00001346 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001347 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001348 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001349 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001350 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001351 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001352 __ b(true_label, ARMCondition(true_high_cond));
1353 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001354 }
1355 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001356 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001357 } else {
1358 Register right_high = right.AsRegisterPairHigh<Register>();
1359 Register right_low = right.AsRegisterPairLow<Register>();
1360
1361 __ cmp(left_high, ShifterOperand(right_high));
1362 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001363 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001364 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001365 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001366 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001367 __ b(true_label, ARMCondition(true_high_cond));
1368 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001369 }
1370 // Must be equal high, so compare the lows.
1371 __ cmp(left_low, ShifterOperand(right_low));
1372 }
1373 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001374 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001375 __ b(true_label, final_condition);
1376}
1377
David Brazdil0debae72015-11-12 18:37:00 +00001378void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1379 Label* true_target_in,
1380 Label* false_target_in) {
1381 // Generated branching requires both targets to be explicit. If either of the
1382 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1383 Label fallthrough_target;
1384 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1385 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1386
Roland Levillain4fa13f62015-07-06 18:11:54 +01001387 LocationSummary* locations = condition->GetLocations();
1388 Location left = locations->InAt(0);
1389 Location right = locations->InAt(1);
1390
Roland Levillain4fa13f62015-07-06 18:11:54 +01001391 Primitive::Type type = condition->InputAt(0)->GetType();
1392 switch (type) {
1393 case Primitive::kPrimLong:
1394 GenerateLongComparesAndJumps(condition, true_target, false_target);
1395 break;
1396 case Primitive::kPrimFloat:
1397 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1398 GenerateFPJumps(condition, true_target, false_target);
1399 break;
1400 case Primitive::kPrimDouble:
1401 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1402 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1403 GenerateFPJumps(condition, true_target, false_target);
1404 break;
1405 default:
1406 LOG(FATAL) << "Unexpected compare type " << type;
1407 }
1408
David Brazdil0debae72015-11-12 18:37:00 +00001409 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001410 __ b(false_target);
1411 }
David Brazdil0debae72015-11-12 18:37:00 +00001412
1413 if (fallthrough_target.IsLinked()) {
1414 __ Bind(&fallthrough_target);
1415 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001416}
1417
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001418void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001419 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001420 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001421 Label* false_target) {
1422 HInstruction* cond = instruction->InputAt(condition_input_index);
1423
1424 if (true_target == nullptr && false_target == nullptr) {
1425 // Nothing to do. The code always falls through.
1426 return;
1427 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001428 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001429 if (cond->AsIntConstant()->IsOne()) {
1430 if (true_target != nullptr) {
1431 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001432 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001433 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001434 DCHECK(cond->AsIntConstant()->IsZero());
1435 if (false_target != nullptr) {
1436 __ b(false_target);
1437 }
1438 }
1439 return;
1440 }
1441
1442 // The following code generates these patterns:
1443 // (1) true_target == nullptr && false_target != nullptr
1444 // - opposite condition true => branch to false_target
1445 // (2) true_target != nullptr && false_target == nullptr
1446 // - condition true => branch to true_target
1447 // (3) true_target != nullptr && false_target != nullptr
1448 // - condition true => branch to true_target
1449 // - branch to false_target
1450 if (IsBooleanValueOrMaterializedCondition(cond)) {
1451 // Condition has been materialized, compare the output to 0.
1452 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1453 DCHECK(cond_val.IsRegister());
1454 if (true_target == nullptr) {
1455 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1456 } else {
1457 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001458 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001459 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001460 // Condition has not been materialized. Use its inputs as the comparison and
1461 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001462 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001463
1464 // If this is a long or FP comparison that has been folded into
1465 // the HCondition, generate the comparison directly.
1466 Primitive::Type type = condition->InputAt(0)->GetType();
1467 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1468 GenerateCompareTestAndBranch(condition, true_target, false_target);
1469 return;
1470 }
1471
1472 LocationSummary* locations = cond->GetLocations();
1473 DCHECK(locations->InAt(0).IsRegister());
1474 Register left = locations->InAt(0).AsRegister<Register>();
1475 Location right = locations->InAt(1);
1476 if (right.IsRegister()) {
1477 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001478 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001479 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001480 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001481 }
1482 if (true_target == nullptr) {
1483 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1484 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001485 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001486 }
Dave Allison20dfc792014-06-16 20:44:29 -07001487 }
David Brazdil0debae72015-11-12 18:37:00 +00001488
1489 // If neither branch falls through (case 3), the conditional branch to `true_target`
1490 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1491 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001492 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001493 }
1494}
1495
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001496void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1498 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001499 locations->SetInAt(0, Location::RequiresRegister());
1500 }
1501}
1502
1503void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001504 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1505 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1506 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1507 nullptr : codegen_->GetLabelOf(true_successor);
1508 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1509 nullptr : codegen_->GetLabelOf(false_successor);
1510 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001511}
1512
1513void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1514 LocationSummary* locations = new (GetGraph()->GetArena())
1515 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001516 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001517 locations->SetInAt(0, Location::RequiresRegister());
1518 }
1519}
1520
1521void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001522 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001523 GenerateTestAndBranch(deoptimize,
1524 /* condition_input_index */ 0,
1525 slow_path->GetEntryLabel(),
1526 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001527}
Dave Allison20dfc792014-06-16 20:44:29 -07001528
David Brazdil74eb1b22015-12-14 11:44:01 +00001529void LocationsBuilderARM::VisitSelect(HSelect* select) {
1530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1531 if (Primitive::IsFloatingPointType(select->GetType())) {
1532 locations->SetInAt(0, Location::RequiresFpuRegister());
1533 locations->SetInAt(1, Location::RequiresFpuRegister());
1534 } else {
1535 locations->SetInAt(0, Location::RequiresRegister());
1536 locations->SetInAt(1, Location::RequiresRegister());
1537 }
1538 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1539 locations->SetInAt(2, Location::RequiresRegister());
1540 }
1541 locations->SetOut(Location::SameAsFirstInput());
1542}
1543
1544void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1545 LocationSummary* locations = select->GetLocations();
1546 Label false_target;
1547 GenerateTestAndBranch(select,
1548 /* condition_input_index */ 2,
1549 /* true_target */ nullptr,
1550 &false_target);
1551 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1552 __ Bind(&false_target);
1553}
1554
David Srbecky0cf44932015-12-09 14:09:59 +00001555void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1556 new (GetGraph()->GetArena()) LocationSummary(info);
1557}
1558
1559void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyc7098ff2016-02-09 14:30:11 +00001560 codegen_->MaybeRecordNativeDebugInfo(info, info->GetDexPc());
1561}
1562
1563void CodeGeneratorARM::GenerateNop() {
1564 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001565}
1566
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001567void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001568 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001569 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001570 // Handle the long/FP comparisons made in instruction simplification.
1571 switch (cond->InputAt(0)->GetType()) {
1572 case Primitive::kPrimLong:
1573 locations->SetInAt(0, Location::RequiresRegister());
1574 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001575 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001576 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1577 }
1578 break;
1579
1580 case Primitive::kPrimFloat:
1581 case Primitive::kPrimDouble:
1582 locations->SetInAt(0, Location::RequiresFpuRegister());
1583 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001584 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1586 }
1587 break;
1588
1589 default:
1590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001592 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001593 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1594 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001595 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001596}
1597
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001598void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001599 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001600 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001601 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001602
1603 LocationSummary* locations = cond->GetLocations();
1604 Location left = locations->InAt(0);
1605 Location right = locations->InAt(1);
1606 Register out = locations->Out().AsRegister<Register>();
1607 Label true_label, false_label;
1608
1609 switch (cond->InputAt(0)->GetType()) {
1610 default: {
1611 // Integer case.
1612 if (right.IsRegister()) {
1613 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1614 } else {
1615 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001616 __ CmpConstant(left.AsRegister<Register>(),
1617 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001618 }
Aart Bike9f37602015-10-09 11:15:55 -07001619 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001620 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001621 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001622 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001623 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001624 return;
1625 }
1626 case Primitive::kPrimLong:
1627 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1628 break;
1629 case Primitive::kPrimFloat:
1630 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1631 GenerateFPJumps(cond, &true_label, &false_label);
1632 break;
1633 case Primitive::kPrimDouble:
1634 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1635 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1636 GenerateFPJumps(cond, &true_label, &false_label);
1637 break;
1638 }
1639
1640 // Convert the jumps into the result.
1641 Label done_label;
1642
1643 // False case: result = 0.
1644 __ Bind(&false_label);
1645 __ LoadImmediate(out, 0);
1646 __ b(&done_label);
1647
1648 // True case: result = 1.
1649 __ Bind(&true_label);
1650 __ LoadImmediate(out, 1);
1651 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001652}
1653
1654void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001655 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001656}
1657
1658void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001659 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001660}
1661
1662void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001663 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001664}
1665
1666void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001667 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001668}
1669
1670void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001671 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001672}
1673
1674void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001675 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001676}
1677
1678void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001679 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001680}
1681
1682void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001683 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001684}
1685
1686void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001687 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001688}
1689
1690void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001691 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001692}
1693
1694void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001695 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001696}
1697
1698void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001699 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001700}
1701
Aart Bike9f37602015-10-09 11:15:55 -07001702void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001703 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001704}
1705
1706void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001707 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001708}
1709
1710void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001711 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001712}
1713
1714void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001715 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001716}
1717
1718void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001719 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001720}
1721
1722void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001723 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001724}
1725
1726void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001727 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001728}
1729
1730void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001731 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001732}
1733
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001734void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001735 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001736}
1737
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001738void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1739 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001740}
1741
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001742void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001743 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001744}
1745
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001746void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001747 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001748}
1749
1750void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001751 LocationSummary* locations =
1752 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001753 switch (store->InputAt(1)->GetType()) {
1754 case Primitive::kPrimBoolean:
1755 case Primitive::kPrimByte:
1756 case Primitive::kPrimChar:
1757 case Primitive::kPrimShort:
1758 case Primitive::kPrimInt:
1759 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001760 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001761 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1762 break;
1763
1764 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001765 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001766 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1767 break;
1768
1769 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001770 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001771 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001772}
1773
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001774void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001775}
1776
1777void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001778 LocationSummary* locations =
1779 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001780 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001781}
1782
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001783void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001784 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001785}
1786
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001787void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1790 locations->SetOut(Location::ConstantLocation(constant));
1791}
1792
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001793void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001794 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001795}
1796
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001797void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001800 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001801}
1802
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001803void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001804 // Will be generated at use site.
1805}
1806
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001807void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1808 LocationSummary* locations =
1809 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1810 locations->SetOut(Location::ConstantLocation(constant));
1811}
1812
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001813void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001814 // Will be generated at use site.
1815}
1816
1817void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1820 locations->SetOut(Location::ConstantLocation(constant));
1821}
1822
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001823void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001824 // Will be generated at use site.
1825}
1826
Calin Juravle27df7582015-04-17 19:12:31 +01001827void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1828 memory_barrier->SetLocations(nullptr);
1829}
1830
1831void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001832 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001833}
1834
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001836 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837}
1838
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001839void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001840 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001841}
1842
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 LocationSummary* locations =
1845 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001846 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847}
1848
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001849void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001850 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001851}
1852
Calin Juravle175dc732015-08-25 15:42:32 +01001853void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1854 // The trampoline uses the same calling convention as dex calling conventions,
1855 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1856 // the method_idx.
1857 HandleInvoke(invoke);
1858}
1859
1860void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1861 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1862}
1863
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001864void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001865 // Explicit clinit checks triggered by static invokes must have been pruned by
1866 // art::PrepareForRegisterAllocation.
1867 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001868
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001869 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001870 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001871 codegen_->GetInstructionSetFeatures());
1872 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001873 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1874 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1875 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001876 return;
1877 }
1878
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001879 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001880
1881 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1882 if (invoke->HasPcRelativeDexCache()) {
1883 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1884 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001885}
1886
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001887static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1888 if (invoke->GetLocations()->Intrinsified()) {
1889 IntrinsicCodeGeneratorARM intrinsic(codegen);
1890 intrinsic.Dispatch(invoke);
1891 return true;
1892 }
1893 return false;
1894}
1895
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001896void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001897 // Explicit clinit checks triggered by static invokes must have been pruned by
1898 // art::PrepareForRegisterAllocation.
1899 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001900
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001901 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1902 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001903 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001904
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001905 LocationSummary* locations = invoke->GetLocations();
1906 codegen_->GenerateStaticOrDirectCall(
1907 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001908 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001909}
1910
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001911void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001912 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001913 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001914}
1915
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001916void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001917 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001918 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001919 codegen_->GetInstructionSetFeatures());
1920 if (intrinsic.TryDispatch(invoke)) {
1921 return;
1922 }
1923
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001924 HandleInvoke(invoke);
1925}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001926
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001927void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001928 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1929 return;
1930 }
1931
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001932 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001933 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001934 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001935}
1936
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001937void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1938 HandleInvoke(invoke);
1939 // Add the hidden argument.
1940 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
1941}
1942
1943void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
1944 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00001945 LocationSummary* locations = invoke->GetLocations();
1946 Register temp = locations->GetTemp(0).AsRegister<Register>();
1947 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001948 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1949 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001950 Location receiver = locations->InAt(0);
1951 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1952
Roland Levillain3b359c72015-11-17 19:35:12 +00001953 // Set the hidden argument. This is safe to do this here, as R12
1954 // won't be modified thereafter, before the `blx` (call) instruction.
1955 DCHECK_EQ(R12, hidden_reg);
1956 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001957
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001958 if (receiver.IsStackSlot()) {
1959 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00001960 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001961 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
1962 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00001963 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001964 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001965 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001966 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00001967 // Instead of simply (possibly) unpoisoning `temp` here, we should
1968 // emit a read barrier for the previous class reference load.
1969 // However this is not required in practice, as this is an
1970 // intermediate/temporary reference and because the current
1971 // concurrent copying collector keeps the from-space memory
1972 // intact/accessible until the end of the marking phase (the
1973 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01001974 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001975 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001976 uint32_t entry_point =
1977 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001978 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
1979 // LR = temp->GetEntryPoint();
1980 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
1981 // LR();
1982 __ blx(LR);
1983 DCHECK(!codegen_->IsLeafMethod());
1984 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1985}
1986
Roland Levillain88cb1752014-10-20 16:36:47 +01001987void LocationsBuilderARM::VisitNeg(HNeg* neg) {
1988 LocationSummary* locations =
1989 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1990 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001991 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01001992 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00001993 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1994 break;
1995 }
1996 case Primitive::kPrimLong: {
1997 locations->SetInAt(0, Location::RequiresRegister());
1998 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01001999 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002000 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002001
Roland Levillain88cb1752014-10-20 16:36:47 +01002002 case Primitive::kPrimFloat:
2003 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002004 locations->SetInAt(0, Location::RequiresFpuRegister());
2005 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002006 break;
2007
2008 default:
2009 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2010 }
2011}
2012
2013void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2014 LocationSummary* locations = neg->GetLocations();
2015 Location out = locations->Out();
2016 Location in = locations->InAt(0);
2017 switch (neg->GetResultType()) {
2018 case Primitive::kPrimInt:
2019 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002020 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002021 break;
2022
2023 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002024 DCHECK(in.IsRegisterPair());
2025 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2026 __ rsbs(out.AsRegisterPairLow<Register>(),
2027 in.AsRegisterPairLow<Register>(),
2028 ShifterOperand(0));
2029 // We cannot emit an RSC (Reverse Subtract with Carry)
2030 // instruction here, as it does not exist in the Thumb-2
2031 // instruction set. We use the following approach
2032 // using SBC and SUB instead.
2033 //
2034 // out.hi = -C
2035 __ sbc(out.AsRegisterPairHigh<Register>(),
2036 out.AsRegisterPairHigh<Register>(),
2037 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2038 // out.hi = out.hi - in.hi
2039 __ sub(out.AsRegisterPairHigh<Register>(),
2040 out.AsRegisterPairHigh<Register>(),
2041 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2042 break;
2043
Roland Levillain88cb1752014-10-20 16:36:47 +01002044 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002045 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002047 break;
2048
Roland Levillain88cb1752014-10-20 16:36:47 +01002049 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002050 DCHECK(in.IsFpuRegisterPair());
2051 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2052 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002053 break;
2054
2055 default:
2056 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2057 }
2058}
2059
Roland Levillaindff1f282014-11-05 14:15:05 +00002060void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002061 Primitive::Type result_type = conversion->GetResultType();
2062 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002063 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002064
Roland Levillain5b3ee562015-04-14 16:02:41 +01002065 // The float-to-long, double-to-long and long-to-float type conversions
2066 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002067 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002068 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2069 && result_type == Primitive::kPrimLong)
2070 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002071 ? LocationSummary::kCall
2072 : LocationSummary::kNoCall;
2073 LocationSummary* locations =
2074 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2075
David Brazdilb2bd1c52015-03-25 11:17:37 +00002076 // The Java language does not allow treating boolean as an integral type but
2077 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002078
Roland Levillaindff1f282014-11-05 14:15:05 +00002079 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002080 case Primitive::kPrimByte:
2081 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002082 case Primitive::kPrimLong:
2083 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002084 case Primitive::kPrimBoolean:
2085 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002086 case Primitive::kPrimShort:
2087 case Primitive::kPrimInt:
2088 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002089 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002090 locations->SetInAt(0, Location::RequiresRegister());
2091 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2092 break;
2093
2094 default:
2095 LOG(FATAL) << "Unexpected type conversion from " << input_type
2096 << " to " << result_type;
2097 }
2098 break;
2099
Roland Levillain01a8d712014-11-14 16:27:39 +00002100 case Primitive::kPrimShort:
2101 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002102 case Primitive::kPrimLong:
2103 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002104 case Primitive::kPrimBoolean:
2105 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002106 case Primitive::kPrimByte:
2107 case Primitive::kPrimInt:
2108 case Primitive::kPrimChar:
2109 // Processing a Dex `int-to-short' instruction.
2110 locations->SetInAt(0, Location::RequiresRegister());
2111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2112 break;
2113
2114 default:
2115 LOG(FATAL) << "Unexpected type conversion from " << input_type
2116 << " to " << result_type;
2117 }
2118 break;
2119
Roland Levillain946e1432014-11-11 17:35:19 +00002120 case Primitive::kPrimInt:
2121 switch (input_type) {
2122 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002123 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002124 locations->SetInAt(0, Location::Any());
2125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2126 break;
2127
2128 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002129 // Processing a Dex `float-to-int' instruction.
2130 locations->SetInAt(0, Location::RequiresFpuRegister());
2131 locations->SetOut(Location::RequiresRegister());
2132 locations->AddTemp(Location::RequiresFpuRegister());
2133 break;
2134
Roland Levillain946e1432014-11-11 17:35:19 +00002135 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002136 // Processing a Dex `double-to-int' instruction.
2137 locations->SetInAt(0, Location::RequiresFpuRegister());
2138 locations->SetOut(Location::RequiresRegister());
2139 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002140 break;
2141
2142 default:
2143 LOG(FATAL) << "Unexpected type conversion from " << input_type
2144 << " to " << result_type;
2145 }
2146 break;
2147
Roland Levillaindff1f282014-11-05 14:15:05 +00002148 case Primitive::kPrimLong:
2149 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002150 case Primitive::kPrimBoolean:
2151 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002152 case Primitive::kPrimByte:
2153 case Primitive::kPrimShort:
2154 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002155 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002156 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002157 locations->SetInAt(0, Location::RequiresRegister());
2158 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2159 break;
2160
Roland Levillain624279f2014-12-04 11:54:28 +00002161 case Primitive::kPrimFloat: {
2162 // Processing a Dex `float-to-long' instruction.
2163 InvokeRuntimeCallingConvention calling_convention;
2164 locations->SetInAt(0, Location::FpuRegisterLocation(
2165 calling_convention.GetFpuRegisterAt(0)));
2166 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2167 break;
2168 }
2169
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002170 case Primitive::kPrimDouble: {
2171 // Processing a Dex `double-to-long' instruction.
2172 InvokeRuntimeCallingConvention calling_convention;
2173 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2174 calling_convention.GetFpuRegisterAt(0),
2175 calling_convention.GetFpuRegisterAt(1)));
2176 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002177 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002178 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002179
2180 default:
2181 LOG(FATAL) << "Unexpected type conversion from " << input_type
2182 << " to " << result_type;
2183 }
2184 break;
2185
Roland Levillain981e4542014-11-14 11:47:14 +00002186 case Primitive::kPrimChar:
2187 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002188 case Primitive::kPrimLong:
2189 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002190 case Primitive::kPrimBoolean:
2191 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002192 case Primitive::kPrimByte:
2193 case Primitive::kPrimShort:
2194 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002195 // Processing a Dex `int-to-char' instruction.
2196 locations->SetInAt(0, Location::RequiresRegister());
2197 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2198 break;
2199
2200 default:
2201 LOG(FATAL) << "Unexpected type conversion from " << input_type
2202 << " to " << result_type;
2203 }
2204 break;
2205
Roland Levillaindff1f282014-11-05 14:15:05 +00002206 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002207 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002208 case Primitive::kPrimBoolean:
2209 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002210 case Primitive::kPrimByte:
2211 case Primitive::kPrimShort:
2212 case Primitive::kPrimInt:
2213 case Primitive::kPrimChar:
2214 // Processing a Dex `int-to-float' instruction.
2215 locations->SetInAt(0, Location::RequiresRegister());
2216 locations->SetOut(Location::RequiresFpuRegister());
2217 break;
2218
Roland Levillain5b3ee562015-04-14 16:02:41 +01002219 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002220 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002221 InvokeRuntimeCallingConvention calling_convention;
2222 locations->SetInAt(0, Location::RegisterPairLocation(
2223 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2224 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002225 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002226 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002227
Roland Levillaincff13742014-11-17 14:32:17 +00002228 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002229 // Processing a Dex `double-to-float' instruction.
2230 locations->SetInAt(0, Location::RequiresFpuRegister());
2231 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002232 break;
2233
2234 default:
2235 LOG(FATAL) << "Unexpected type conversion from " << input_type
2236 << " to " << result_type;
2237 };
2238 break;
2239
Roland Levillaindff1f282014-11-05 14:15:05 +00002240 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002241 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002242 case Primitive::kPrimBoolean:
2243 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002244 case Primitive::kPrimByte:
2245 case Primitive::kPrimShort:
2246 case Primitive::kPrimInt:
2247 case Primitive::kPrimChar:
2248 // Processing a Dex `int-to-double' instruction.
2249 locations->SetInAt(0, Location::RequiresRegister());
2250 locations->SetOut(Location::RequiresFpuRegister());
2251 break;
2252
2253 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002254 // Processing a Dex `long-to-double' instruction.
2255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002257 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002258 locations->AddTemp(Location::RequiresFpuRegister());
2259 break;
2260
Roland Levillaincff13742014-11-17 14:32:17 +00002261 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002262 // Processing a Dex `float-to-double' instruction.
2263 locations->SetInAt(0, Location::RequiresFpuRegister());
2264 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002265 break;
2266
2267 default:
2268 LOG(FATAL) << "Unexpected type conversion from " << input_type
2269 << " to " << result_type;
2270 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002271 break;
2272
2273 default:
2274 LOG(FATAL) << "Unexpected type conversion from " << input_type
2275 << " to " << result_type;
2276 }
2277}
2278
2279void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2280 LocationSummary* locations = conversion->GetLocations();
2281 Location out = locations->Out();
2282 Location in = locations->InAt(0);
2283 Primitive::Type result_type = conversion->GetResultType();
2284 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002285 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002286 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002287 case Primitive::kPrimByte:
2288 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002289 case Primitive::kPrimLong:
2290 // Type conversion from long to byte is a result of code transformations.
2291 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2292 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002293 case Primitive::kPrimBoolean:
2294 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002295 case Primitive::kPrimShort:
2296 case Primitive::kPrimInt:
2297 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002298 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002299 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002300 break;
2301
2302 default:
2303 LOG(FATAL) << "Unexpected type conversion from " << input_type
2304 << " to " << result_type;
2305 }
2306 break;
2307
Roland Levillain01a8d712014-11-14 16:27:39 +00002308 case Primitive::kPrimShort:
2309 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002310 case Primitive::kPrimLong:
2311 // Type conversion from long to short is a result of code transformations.
2312 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2313 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002314 case Primitive::kPrimBoolean:
2315 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002316 case Primitive::kPrimByte:
2317 case Primitive::kPrimInt:
2318 case Primitive::kPrimChar:
2319 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002320 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002321 break;
2322
2323 default:
2324 LOG(FATAL) << "Unexpected type conversion from " << input_type
2325 << " to " << result_type;
2326 }
2327 break;
2328
Roland Levillain946e1432014-11-11 17:35:19 +00002329 case Primitive::kPrimInt:
2330 switch (input_type) {
2331 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002332 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002333 DCHECK(out.IsRegister());
2334 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002335 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002336 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002338 } else {
2339 DCHECK(in.IsConstant());
2340 DCHECK(in.GetConstant()->IsLongConstant());
2341 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002342 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002343 }
2344 break;
2345
Roland Levillain3f8f9362014-12-02 17:45:01 +00002346 case Primitive::kPrimFloat: {
2347 // Processing a Dex `float-to-int' instruction.
2348 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2349 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2350 __ vcvtis(temp, temp);
2351 __ vmovrs(out.AsRegister<Register>(), temp);
2352 break;
2353 }
2354
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002355 case Primitive::kPrimDouble: {
2356 // Processing a Dex `double-to-int' instruction.
2357 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2358 DRegister temp_d = FromLowSToD(temp_s);
2359 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2360 __ vcvtid(temp_s, temp_d);
2361 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002362 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002363 }
Roland Levillain946e1432014-11-11 17:35:19 +00002364
2365 default:
2366 LOG(FATAL) << "Unexpected type conversion from " << input_type
2367 << " to " << result_type;
2368 }
2369 break;
2370
Roland Levillaindff1f282014-11-05 14:15:05 +00002371 case Primitive::kPrimLong:
2372 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002373 case Primitive::kPrimBoolean:
2374 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002375 case Primitive::kPrimByte:
2376 case Primitive::kPrimShort:
2377 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002378 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002379 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002380 DCHECK(out.IsRegisterPair());
2381 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002382 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002383 // Sign extension.
2384 __ Asr(out.AsRegisterPairHigh<Register>(),
2385 out.AsRegisterPairLow<Register>(),
2386 31);
2387 break;
2388
2389 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002390 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002391 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2392 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002393 conversion->GetDexPc(),
2394 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002395 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002396 break;
2397
Roland Levillaindff1f282014-11-05 14:15:05 +00002398 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002399 // Processing a Dex `double-to-long' instruction.
2400 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2401 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002402 conversion->GetDexPc(),
2403 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002404 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002405 break;
2406
2407 default:
2408 LOG(FATAL) << "Unexpected type conversion from " << input_type
2409 << " to " << result_type;
2410 }
2411 break;
2412
Roland Levillain981e4542014-11-14 11:47:14 +00002413 case Primitive::kPrimChar:
2414 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002415 case Primitive::kPrimLong:
2416 // Type conversion from long to char is a result of code transformations.
2417 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2418 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002419 case Primitive::kPrimBoolean:
2420 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002421 case Primitive::kPrimByte:
2422 case Primitive::kPrimShort:
2423 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002424 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002425 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002426 break;
2427
2428 default:
2429 LOG(FATAL) << "Unexpected type conversion from " << input_type
2430 << " to " << result_type;
2431 }
2432 break;
2433
Roland Levillaindff1f282014-11-05 14:15:05 +00002434 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002435 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002436 case Primitive::kPrimBoolean:
2437 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002438 case Primitive::kPrimByte:
2439 case Primitive::kPrimShort:
2440 case Primitive::kPrimInt:
2441 case Primitive::kPrimChar: {
2442 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002443 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2444 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002445 break;
2446 }
2447
Roland Levillain5b3ee562015-04-14 16:02:41 +01002448 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002449 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002450 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2451 conversion,
2452 conversion->GetDexPc(),
2453 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002454 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002455 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002456
Roland Levillaincff13742014-11-17 14:32:17 +00002457 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002458 // Processing a Dex `double-to-float' instruction.
2459 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2460 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002461 break;
2462
2463 default:
2464 LOG(FATAL) << "Unexpected type conversion from " << input_type
2465 << " to " << result_type;
2466 };
2467 break;
2468
Roland Levillaindff1f282014-11-05 14:15:05 +00002469 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002470 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002471 case Primitive::kPrimBoolean:
2472 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002473 case Primitive::kPrimByte:
2474 case Primitive::kPrimShort:
2475 case Primitive::kPrimInt:
2476 case Primitive::kPrimChar: {
2477 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002478 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002479 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2480 out.AsFpuRegisterPairLow<SRegister>());
2481 break;
2482 }
2483
Roland Levillain647b9ed2014-11-27 12:06:00 +00002484 case Primitive::kPrimLong: {
2485 // Processing a Dex `long-to-double' instruction.
2486 Register low = in.AsRegisterPairLow<Register>();
2487 Register high = in.AsRegisterPairHigh<Register>();
2488 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2489 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002490 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002491 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002492 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2493 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002494
Roland Levillain682393c2015-04-14 15:57:52 +01002495 // temp_d = int-to-double(high)
2496 __ vmovsr(temp_s, high);
2497 __ vcvtdi(temp_d, temp_s);
2498 // constant_d = k2Pow32EncodingForDouble
2499 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2500 // out_d = unsigned-to-double(low)
2501 __ vmovsr(out_s, low);
2502 __ vcvtdu(out_d, out_s);
2503 // out_d += temp_d * constant_d
2504 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002505 break;
2506 }
2507
Roland Levillaincff13742014-11-17 14:32:17 +00002508 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002509 // Processing a Dex `float-to-double' instruction.
2510 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2511 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002512 break;
2513
2514 default:
2515 LOG(FATAL) << "Unexpected type conversion from " << input_type
2516 << " to " << result_type;
2517 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002518 break;
2519
2520 default:
2521 LOG(FATAL) << "Unexpected type conversion from " << input_type
2522 << " to " << result_type;
2523 }
2524}
2525
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002526void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002527 LocationSummary* locations =
2528 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002529 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002530 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002531 locations->SetInAt(0, Location::RequiresRegister());
2532 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2534 break;
2535 }
2536
2537 case Primitive::kPrimLong: {
2538 locations->SetInAt(0, Location::RequiresRegister());
2539 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002540 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002541 break;
2542 }
2543
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002544 case Primitive::kPrimFloat:
2545 case Primitive::kPrimDouble: {
2546 locations->SetInAt(0, Location::RequiresFpuRegister());
2547 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002548 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002549 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002550 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002551
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002552 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002553 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002554 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002555}
2556
2557void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2558 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002559 Location out = locations->Out();
2560 Location first = locations->InAt(0);
2561 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002562 switch (add->GetResultType()) {
2563 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002564 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002565 __ add(out.AsRegister<Register>(),
2566 first.AsRegister<Register>(),
2567 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002568 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002569 __ AddConstant(out.AsRegister<Register>(),
2570 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002571 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002572 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002573 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002574
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002575 case Primitive::kPrimLong: {
2576 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002577 __ adds(out.AsRegisterPairLow<Register>(),
2578 first.AsRegisterPairLow<Register>(),
2579 ShifterOperand(second.AsRegisterPairLow<Register>()));
2580 __ adc(out.AsRegisterPairHigh<Register>(),
2581 first.AsRegisterPairHigh<Register>(),
2582 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002583 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002584 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002585
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002586 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002587 __ vadds(out.AsFpuRegister<SRegister>(),
2588 first.AsFpuRegister<SRegister>(),
2589 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002590 break;
2591
2592 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002593 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2594 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2595 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002596 break;
2597
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002598 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002599 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002600 }
2601}
2602
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002603void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002604 LocationSummary* locations =
2605 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002606 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002607 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002608 locations->SetInAt(0, Location::RequiresRegister());
2609 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2611 break;
2612 }
2613
2614 case Primitive::kPrimLong: {
2615 locations->SetInAt(0, Location::RequiresRegister());
2616 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002617 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002618 break;
2619 }
Calin Juravle11351682014-10-23 15:38:15 +01002620 case Primitive::kPrimFloat:
2621 case Primitive::kPrimDouble: {
2622 locations->SetInAt(0, Location::RequiresFpuRegister());
2623 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002624 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002625 break;
Calin Juravle11351682014-10-23 15:38:15 +01002626 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002627 default:
Calin Juravle11351682014-10-23 15:38:15 +01002628 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002629 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002630}
2631
2632void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2633 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002634 Location out = locations->Out();
2635 Location first = locations->InAt(0);
2636 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002637 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002638 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002639 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002640 __ sub(out.AsRegister<Register>(),
2641 first.AsRegister<Register>(),
2642 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002643 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ AddConstant(out.AsRegister<Register>(),
2645 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002646 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002647 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002648 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002649 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002650
Calin Juravle11351682014-10-23 15:38:15 +01002651 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002652 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002653 __ subs(out.AsRegisterPairLow<Register>(),
2654 first.AsRegisterPairLow<Register>(),
2655 ShifterOperand(second.AsRegisterPairLow<Register>()));
2656 __ sbc(out.AsRegisterPairHigh<Register>(),
2657 first.AsRegisterPairHigh<Register>(),
2658 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002659 break;
Calin Juravle11351682014-10-23 15:38:15 +01002660 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002661
Calin Juravle11351682014-10-23 15:38:15 +01002662 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002663 __ vsubs(out.AsFpuRegister<SRegister>(),
2664 first.AsFpuRegister<SRegister>(),
2665 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002666 break;
Calin Juravle11351682014-10-23 15:38:15 +01002667 }
2668
2669 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002670 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2671 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2672 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002673 break;
2674 }
2675
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002676
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002677 default:
Calin Juravle11351682014-10-23 15:38:15 +01002678 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002679 }
2680}
2681
Calin Juravle34bacdf2014-10-07 20:23:36 +01002682void LocationsBuilderARM::VisitMul(HMul* mul) {
2683 LocationSummary* locations =
2684 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2685 switch (mul->GetResultType()) {
2686 case Primitive::kPrimInt:
2687 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002688 locations->SetInAt(0, Location::RequiresRegister());
2689 locations->SetInAt(1, Location::RequiresRegister());
2690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002691 break;
2692 }
2693
Calin Juravleb5bfa962014-10-21 18:02:24 +01002694 case Primitive::kPrimFloat:
2695 case Primitive::kPrimDouble: {
2696 locations->SetInAt(0, Location::RequiresFpuRegister());
2697 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002698 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002699 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002700 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002701
2702 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002703 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002704 }
2705}
2706
2707void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2708 LocationSummary* locations = mul->GetLocations();
2709 Location out = locations->Out();
2710 Location first = locations->InAt(0);
2711 Location second = locations->InAt(1);
2712 switch (mul->GetResultType()) {
2713 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002714 __ mul(out.AsRegister<Register>(),
2715 first.AsRegister<Register>(),
2716 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002717 break;
2718 }
2719 case Primitive::kPrimLong: {
2720 Register out_hi = out.AsRegisterPairHigh<Register>();
2721 Register out_lo = out.AsRegisterPairLow<Register>();
2722 Register in1_hi = first.AsRegisterPairHigh<Register>();
2723 Register in1_lo = first.AsRegisterPairLow<Register>();
2724 Register in2_hi = second.AsRegisterPairHigh<Register>();
2725 Register in2_lo = second.AsRegisterPairLow<Register>();
2726
2727 // Extra checks to protect caused by the existence of R1_R2.
2728 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2729 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2730 DCHECK_NE(out_hi, in1_lo);
2731 DCHECK_NE(out_hi, in2_lo);
2732
2733 // input: in1 - 64 bits, in2 - 64 bits
2734 // output: out
2735 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2736 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2737 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2738
2739 // IP <- in1.lo * in2.hi
2740 __ mul(IP, in1_lo, in2_hi);
2741 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2742 __ mla(out_hi, in1_hi, in2_lo, IP);
2743 // out.lo <- (in1.lo * in2.lo)[31:0];
2744 __ umull(out_lo, IP, in1_lo, in2_lo);
2745 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2746 __ add(out_hi, out_hi, ShifterOperand(IP));
2747 break;
2748 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002749
2750 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002751 __ vmuls(out.AsFpuRegister<SRegister>(),
2752 first.AsFpuRegister<SRegister>(),
2753 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002754 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002755 }
2756
2757 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002758 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2759 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2760 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002761 break;
2762 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002763
2764 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002765 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002766 }
2767}
2768
Zheng Xuc6667102015-05-15 16:08:45 +08002769void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2770 DCHECK(instruction->IsDiv() || instruction->IsRem());
2771 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2772
2773 LocationSummary* locations = instruction->GetLocations();
2774 Location second = locations->InAt(1);
2775 DCHECK(second.IsConstant());
2776
2777 Register out = locations->Out().AsRegister<Register>();
2778 Register dividend = locations->InAt(0).AsRegister<Register>();
2779 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2780 DCHECK(imm == 1 || imm == -1);
2781
2782 if (instruction->IsRem()) {
2783 __ LoadImmediate(out, 0);
2784 } else {
2785 if (imm == 1) {
2786 __ Mov(out, dividend);
2787 } else {
2788 __ rsb(out, dividend, ShifterOperand(0));
2789 }
2790 }
2791}
2792
2793void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2794 DCHECK(instruction->IsDiv() || instruction->IsRem());
2795 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2796
2797 LocationSummary* locations = instruction->GetLocations();
2798 Location second = locations->InAt(1);
2799 DCHECK(second.IsConstant());
2800
2801 Register out = locations->Out().AsRegister<Register>();
2802 Register dividend = locations->InAt(0).AsRegister<Register>();
2803 Register temp = locations->GetTemp(0).AsRegister<Register>();
2804 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002805 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002806 int ctz_imm = CTZ(abs_imm);
2807
2808 if (ctz_imm == 1) {
2809 __ Lsr(temp, dividend, 32 - ctz_imm);
2810 } else {
2811 __ Asr(temp, dividend, 31);
2812 __ Lsr(temp, temp, 32 - ctz_imm);
2813 }
2814 __ add(out, temp, ShifterOperand(dividend));
2815
2816 if (instruction->IsDiv()) {
2817 __ Asr(out, out, ctz_imm);
2818 if (imm < 0) {
2819 __ rsb(out, out, ShifterOperand(0));
2820 }
2821 } else {
2822 __ ubfx(out, out, 0, ctz_imm);
2823 __ sub(out, out, ShifterOperand(temp));
2824 }
2825}
2826
2827void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2828 DCHECK(instruction->IsDiv() || instruction->IsRem());
2829 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2830
2831 LocationSummary* locations = instruction->GetLocations();
2832 Location second = locations->InAt(1);
2833 DCHECK(second.IsConstant());
2834
2835 Register out = locations->Out().AsRegister<Register>();
2836 Register dividend = locations->InAt(0).AsRegister<Register>();
2837 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2838 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2839 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2840
2841 int64_t magic;
2842 int shift;
2843 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2844
2845 __ LoadImmediate(temp1, magic);
2846 __ smull(temp2, temp1, dividend, temp1);
2847
2848 if (imm > 0 && magic < 0) {
2849 __ add(temp1, temp1, ShifterOperand(dividend));
2850 } else if (imm < 0 && magic > 0) {
2851 __ sub(temp1, temp1, ShifterOperand(dividend));
2852 }
2853
2854 if (shift != 0) {
2855 __ Asr(temp1, temp1, shift);
2856 }
2857
2858 if (instruction->IsDiv()) {
2859 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2860 } else {
2861 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2862 // TODO: Strength reduction for mls.
2863 __ LoadImmediate(temp2, imm);
2864 __ mls(out, temp1, temp2, dividend);
2865 }
2866}
2867
2868void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2869 DCHECK(instruction->IsDiv() || instruction->IsRem());
2870 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2871
2872 LocationSummary* locations = instruction->GetLocations();
2873 Location second = locations->InAt(1);
2874 DCHECK(second.IsConstant());
2875
2876 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2877 if (imm == 0) {
2878 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2879 } else if (imm == 1 || imm == -1) {
2880 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002881 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002882 DivRemByPowerOfTwo(instruction);
2883 } else {
2884 DCHECK(imm <= -2 || imm >= 2);
2885 GenerateDivRemWithAnyConstant(instruction);
2886 }
2887}
2888
Calin Juravle7c4954d2014-10-28 16:57:40 +00002889void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002890 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2891 if (div->GetResultType() == Primitive::kPrimLong) {
2892 // pLdiv runtime call.
2893 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002894 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2895 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002896 } else if (div->GetResultType() == Primitive::kPrimInt &&
2897 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2898 // pIdivmod runtime call.
2899 call_kind = LocationSummary::kCall;
2900 }
2901
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2903
Calin Juravle7c4954d2014-10-28 16:57:40 +00002904 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002905 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002906 if (div->InputAt(1)->IsConstant()) {
2907 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002908 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002909 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002910 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2911 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002912 // No temp register required.
2913 } else {
2914 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002915 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002916 locations->AddTemp(Location::RequiresRegister());
2917 }
2918 }
2919 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002920 locations->SetInAt(0, Location::RequiresRegister());
2921 locations->SetInAt(1, Location::RequiresRegister());
2922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2923 } else {
2924 InvokeRuntimeCallingConvention calling_convention;
2925 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2926 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2927 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2928 // we only need the former.
2929 locations->SetOut(Location::RegisterLocation(R0));
2930 }
Calin Juravled0d48522014-11-04 16:40:20 +00002931 break;
2932 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002933 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002934 InvokeRuntimeCallingConvention calling_convention;
2935 locations->SetInAt(0, Location::RegisterPairLocation(
2936 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2937 locations->SetInAt(1, Location::RegisterPairLocation(
2938 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002939 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002940 break;
2941 }
2942 case Primitive::kPrimFloat:
2943 case Primitive::kPrimDouble: {
2944 locations->SetInAt(0, Location::RequiresFpuRegister());
2945 locations->SetInAt(1, Location::RequiresFpuRegister());
2946 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2947 break;
2948 }
2949
2950 default:
2951 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2952 }
2953}
2954
2955void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
2956 LocationSummary* locations = div->GetLocations();
2957 Location out = locations->Out();
2958 Location first = locations->InAt(0);
2959 Location second = locations->InAt(1);
2960
2961 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002962 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002963 if (second.IsConstant()) {
2964 GenerateDivRemConstantIntegral(div);
2965 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002966 __ sdiv(out.AsRegister<Register>(),
2967 first.AsRegister<Register>(),
2968 second.AsRegister<Register>());
2969 } else {
2970 InvokeRuntimeCallingConvention calling_convention;
2971 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
2972 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
2973 DCHECK_EQ(R0, out.AsRegister<Register>());
2974
2975 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002976 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002977 }
Calin Juravled0d48522014-11-04 16:40:20 +00002978 break;
2979 }
2980
Calin Juravle7c4954d2014-10-28 16:57:40 +00002981 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002982 InvokeRuntimeCallingConvention calling_convention;
2983 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2984 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2985 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2986 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2987 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002988 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002989
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002990 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002991 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002992 break;
2993 }
2994
2995 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002996 __ vdivs(out.AsFpuRegister<SRegister>(),
2997 first.AsFpuRegister<SRegister>(),
2998 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002999 break;
3000 }
3001
3002 case Primitive::kPrimDouble: {
3003 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3004 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3005 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3006 break;
3007 }
3008
3009 default:
3010 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3011 }
3012}
3013
Calin Juravlebacfec32014-11-14 15:54:36 +00003014void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003015 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003016
3017 // Most remainders are implemented in the runtime.
3018 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003019 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3020 // sdiv will be replaced by other instruction sequence.
3021 call_kind = LocationSummary::kNoCall;
3022 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3023 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003024 // Have hardware divide instruction for int, do it with three instructions.
3025 call_kind = LocationSummary::kNoCall;
3026 }
3027
Calin Juravlebacfec32014-11-14 15:54:36 +00003028 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3029
Calin Juravled2ec87d2014-12-08 14:24:46 +00003030 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003031 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003032 if (rem->InputAt(1)->IsConstant()) {
3033 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003034 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003036 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3037 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003038 // No temp register required.
3039 } else {
3040 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003041 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003042 locations->AddTemp(Location::RequiresRegister());
3043 }
3044 }
3045 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003046 locations->SetInAt(0, Location::RequiresRegister());
3047 locations->SetInAt(1, Location::RequiresRegister());
3048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3049 locations->AddTemp(Location::RequiresRegister());
3050 } else {
3051 InvokeRuntimeCallingConvention calling_convention;
3052 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3053 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3054 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3055 // we only need the latter.
3056 locations->SetOut(Location::RegisterLocation(R1));
3057 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003058 break;
3059 }
3060 case Primitive::kPrimLong: {
3061 InvokeRuntimeCallingConvention calling_convention;
3062 locations->SetInAt(0, Location::RegisterPairLocation(
3063 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3064 locations->SetInAt(1, Location::RegisterPairLocation(
3065 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3066 // The runtime helper puts the output in R2,R3.
3067 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3068 break;
3069 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003070 case Primitive::kPrimFloat: {
3071 InvokeRuntimeCallingConvention calling_convention;
3072 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3073 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3074 locations->SetOut(Location::FpuRegisterLocation(S0));
3075 break;
3076 }
3077
Calin Juravlebacfec32014-11-14 15:54:36 +00003078 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003079 InvokeRuntimeCallingConvention calling_convention;
3080 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3081 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3082 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3083 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3084 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003085 break;
3086 }
3087
3088 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003089 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003090 }
3091}
3092
3093void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3094 LocationSummary* locations = rem->GetLocations();
3095 Location out = locations->Out();
3096 Location first = locations->InAt(0);
3097 Location second = locations->InAt(1);
3098
Calin Juravled2ec87d2014-12-08 14:24:46 +00003099 Primitive::Type type = rem->GetResultType();
3100 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003101 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003102 if (second.IsConstant()) {
3103 GenerateDivRemConstantIntegral(rem);
3104 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003105 Register reg1 = first.AsRegister<Register>();
3106 Register reg2 = second.AsRegister<Register>();
3107 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003108
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003109 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003110 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003111 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003112 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003113 } else {
3114 InvokeRuntimeCallingConvention calling_convention;
3115 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3116 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3117 DCHECK_EQ(R1, out.AsRegister<Register>());
3118
3119 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003120 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003121 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003122 break;
3123 }
3124
3125 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003126 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003127 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003128 break;
3129 }
3130
Calin Juravled2ec87d2014-12-08 14:24:46 +00003131 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003132 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003133 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003134 break;
3135 }
3136
Calin Juravlebacfec32014-11-14 15:54:36 +00003137 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003138 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003139 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003140 break;
3141 }
3142
3143 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003144 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003145 }
3146}
3147
Calin Juravled0d48522014-11-04 16:40:20 +00003148void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003149 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3150 ? LocationSummary::kCallOnSlowPath
3151 : LocationSummary::kNoCall;
3152 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003153 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003154 if (instruction->HasUses()) {
3155 locations->SetOut(Location::SameAsFirstInput());
3156 }
3157}
3158
3159void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003160 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003161 codegen_->AddSlowPath(slow_path);
3162
3163 LocationSummary* locations = instruction->GetLocations();
3164 Location value = locations->InAt(0);
3165
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003166 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003167 case Primitive::kPrimByte:
3168 case Primitive::kPrimChar:
3169 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003170 case Primitive::kPrimInt: {
3171 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003172 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003173 } else {
3174 DCHECK(value.IsConstant()) << value;
3175 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3176 __ b(slow_path->GetEntryLabel());
3177 }
3178 }
3179 break;
3180 }
3181 case Primitive::kPrimLong: {
3182 if (value.IsRegisterPair()) {
3183 __ orrs(IP,
3184 value.AsRegisterPairLow<Register>(),
3185 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3186 __ b(slow_path->GetEntryLabel(), EQ);
3187 } else {
3188 DCHECK(value.IsConstant()) << value;
3189 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3190 __ b(slow_path->GetEntryLabel());
3191 }
3192 }
3193 break;
3194 default:
3195 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3196 }
3197 }
Calin Juravled0d48522014-11-04 16:40:20 +00003198}
3199
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003200void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3201 Register in = locations->InAt(0).AsRegister<Register>();
3202 Location rhs = locations->InAt(1);
3203 Register out = locations->Out().AsRegister<Register>();
3204
3205 if (rhs.IsConstant()) {
3206 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3207 // so map all rotations to a +ve. equivalent in that range.
3208 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3209 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3210 if (rot) {
3211 // Rotate, mapping left rotations to right equivalents if necessary.
3212 // (e.g. left by 2 bits == right by 30.)
3213 __ Ror(out, in, rot);
3214 } else if (out != in) {
3215 __ Mov(out, in);
3216 }
3217 } else {
3218 __ Ror(out, in, rhs.AsRegister<Register>());
3219 }
3220}
3221
3222// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3223// rotates by swapping input regs (effectively rotating by the first 32-bits of
3224// a larger rotation) or flipping direction (thus treating larger right/left
3225// rotations as sub-word sized rotations in the other direction) as appropriate.
3226void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3227 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3228 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3229 Location rhs = locations->InAt(1);
3230 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3231 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3232
3233 if (rhs.IsConstant()) {
3234 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3235 // Map all rotations to +ve. equivalents on the interval [0,63].
3236 rot &= kMaxLongShiftValue;
3237 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3238 // logic below to a simple pair of binary orr.
3239 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3240 if (rot >= kArmBitsPerWord) {
3241 rot -= kArmBitsPerWord;
3242 std::swap(in_reg_hi, in_reg_lo);
3243 }
3244 // Rotate, or mov to out for zero or word size rotations.
3245 if (rot != 0u) {
3246 __ Lsr(out_reg_hi, in_reg_hi, rot);
3247 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3248 __ Lsr(out_reg_lo, in_reg_lo, rot);
3249 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3250 } else {
3251 __ Mov(out_reg_lo, in_reg_lo);
3252 __ Mov(out_reg_hi, in_reg_hi);
3253 }
3254 } else {
3255 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3256 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3257 Label end;
3258 Label shift_by_32_plus_shift_right;
3259
3260 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3261 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3262 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3263 __ b(&shift_by_32_plus_shift_right, CC);
3264
3265 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3266 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3267 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3268 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3269 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3270 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3271 __ Lsr(shift_left, in_reg_hi, shift_right);
3272 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3273 __ b(&end);
3274
3275 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3276 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3277 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3278 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3279 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3280 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3281 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3282 __ Lsl(shift_right, in_reg_hi, shift_left);
3283 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3284
3285 __ Bind(&end);
3286 }
3287}
3288void LocationsBuilderARM::HandleRotate(HRor* ror) {
3289 LocationSummary* locations =
3290 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3291 switch (ror->GetResultType()) {
3292 case Primitive::kPrimInt: {
3293 locations->SetInAt(0, Location::RequiresRegister());
3294 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3296 break;
3297 }
3298 case Primitive::kPrimLong: {
3299 locations->SetInAt(0, Location::RequiresRegister());
3300 if (ror->InputAt(1)->IsConstant()) {
3301 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3302 } else {
3303 locations->SetInAt(1, Location::RequiresRegister());
3304 locations->AddTemp(Location::RequiresRegister());
3305 locations->AddTemp(Location::RequiresRegister());
3306 }
3307 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3308 break;
3309 }
3310 default:
3311 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3312 }
3313}
3314
3315void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3316 LocationSummary* locations = ror->GetLocations();
3317 Primitive::Type type = ror->GetResultType();
3318 switch (type) {
3319 case Primitive::kPrimInt: {
3320 HandleIntegerRotate(locations);
3321 break;
3322 }
3323 case Primitive::kPrimLong: {
3324 HandleLongRotate(locations);
3325 break;
3326 }
3327 default:
3328 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003329 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003330 }
3331}
3332
3333void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003334 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003335}
3336
3337void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003338 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003339}
3340
Calin Juravle9aec02f2014-11-18 23:06:35 +00003341void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3342 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3343
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003344 LocationSummary* locations =
3345 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003346
3347 switch (op->GetResultType()) {
3348 case Primitive::kPrimInt: {
3349 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003350 if (op->InputAt(1)->IsConstant()) {
3351 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3353 } else {
3354 locations->SetInAt(1, Location::RequiresRegister());
3355 // Make the output overlap, as it will be used to hold the masked
3356 // second input.
3357 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3358 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003359 break;
3360 }
3361 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003362 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003363 if (op->InputAt(1)->IsConstant()) {
3364 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3365 // For simplicity, use kOutputOverlap even though we only require that low registers
3366 // don't clash with high registers which the register allocator currently guarantees.
3367 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3368 } else {
3369 locations->SetInAt(1, Location::RequiresRegister());
3370 locations->AddTemp(Location::RequiresRegister());
3371 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3372 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003373 break;
3374 }
3375 default:
3376 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3377 }
3378}
3379
3380void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3381 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3382
3383 LocationSummary* locations = op->GetLocations();
3384 Location out = locations->Out();
3385 Location first = locations->InAt(0);
3386 Location second = locations->InAt(1);
3387
3388 Primitive::Type type = op->GetResultType();
3389 switch (type) {
3390 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003391 Register out_reg = out.AsRegister<Register>();
3392 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003393 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003394 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003395 // ARM doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003396 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003397 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003398 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003399 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003400 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003401 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003402 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003403 }
3404 } else {
3405 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3406 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
Roland Levillainc9285912015-12-18 10:38:42 +00003407 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003408 __ Mov(out_reg, first_reg);
3409 } else if (op->IsShl()) {
3410 __ Lsl(out_reg, first_reg, shift_value);
3411 } else if (op->IsShr()) {
3412 __ Asr(out_reg, first_reg, shift_value);
3413 } else {
3414 __ Lsr(out_reg, first_reg, shift_value);
3415 }
3416 }
3417 break;
3418 }
3419 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003420 Register o_h = out.AsRegisterPairHigh<Register>();
3421 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003422
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003423 Register high = first.AsRegisterPairHigh<Register>();
3424 Register low = first.AsRegisterPairLow<Register>();
3425
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003426 if (second.IsRegister()) {
3427 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003428
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003429 Register second_reg = second.AsRegister<Register>();
3430
3431 if (op->IsShl()) {
3432 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3433 // Shift the high part
3434 __ Lsl(o_h, high, o_l);
3435 // Shift the low part and `or` what overflew on the high part
3436 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3437 __ Lsr(temp, low, temp);
3438 __ orr(o_h, o_h, ShifterOperand(temp));
3439 // If the shift is > 32 bits, override the high part
3440 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3441 __ it(PL);
3442 __ Lsl(o_h, low, temp, PL);
3443 // Shift the low part
3444 __ Lsl(o_l, low, o_l);
3445 } else if (op->IsShr()) {
3446 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3447 // Shift the low part
3448 __ Lsr(o_l, low, o_h);
3449 // Shift the high part and `or` what underflew on the low part
3450 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3451 __ Lsl(temp, high, temp);
3452 __ orr(o_l, o_l, ShifterOperand(temp));
3453 // If the shift is > 32 bits, override the low part
3454 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3455 __ it(PL);
3456 __ Asr(o_l, high, temp, PL);
3457 // Shift the high part
3458 __ Asr(o_h, high, o_h);
3459 } else {
3460 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3461 // same as Shr except we use `Lsr`s and not `Asr`s
3462 __ Lsr(o_l, low, o_h);
3463 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3464 __ Lsl(temp, high, temp);
3465 __ orr(o_l, o_l, ShifterOperand(temp));
3466 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3467 __ it(PL);
3468 __ Lsr(o_l, high, temp, PL);
3469 __ Lsr(o_h, high, o_h);
3470 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003471 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003472 // Register allocator doesn't create partial overlap.
3473 DCHECK_NE(o_l, high);
3474 DCHECK_NE(o_h, low);
3475 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3476 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3477 if (shift_value > 32) {
3478 if (op->IsShl()) {
3479 __ Lsl(o_h, low, shift_value - 32);
3480 __ LoadImmediate(o_l, 0);
3481 } else if (op->IsShr()) {
3482 __ Asr(o_l, high, shift_value - 32);
3483 __ Asr(o_h, high, 31);
3484 } else {
3485 __ Lsr(o_l, high, shift_value - 32);
3486 __ LoadImmediate(o_h, 0);
3487 }
3488 } else if (shift_value == 32) {
3489 if (op->IsShl()) {
3490 __ mov(o_h, ShifterOperand(low));
3491 __ LoadImmediate(o_l, 0);
3492 } else if (op->IsShr()) {
3493 __ mov(o_l, ShifterOperand(high));
3494 __ Asr(o_h, high, 31);
3495 } else {
3496 __ mov(o_l, ShifterOperand(high));
3497 __ LoadImmediate(o_h, 0);
3498 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003499 } else if (shift_value == 1) {
3500 if (op->IsShl()) {
3501 __ Lsls(o_l, low, 1);
3502 __ adc(o_h, high, ShifterOperand(high));
3503 } else if (op->IsShr()) {
3504 __ Asrs(o_h, high, 1);
3505 __ Rrx(o_l, low);
3506 } else {
3507 __ Lsrs(o_h, high, 1);
3508 __ Rrx(o_l, low);
3509 }
3510 } else {
3511 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003512 if (op->IsShl()) {
3513 __ Lsl(o_h, high, shift_value);
3514 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3515 __ Lsl(o_l, low, shift_value);
3516 } else if (op->IsShr()) {
3517 __ Lsr(o_l, low, shift_value);
3518 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3519 __ Asr(o_h, high, shift_value);
3520 } else {
3521 __ Lsr(o_l, low, shift_value);
3522 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3523 __ Lsr(o_h, high, shift_value);
3524 }
3525 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003526 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003527 break;
3528 }
3529 default:
3530 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003531 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003532 }
3533}
3534
3535void LocationsBuilderARM::VisitShl(HShl* shl) {
3536 HandleShift(shl);
3537}
3538
3539void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3540 HandleShift(shl);
3541}
3542
3543void LocationsBuilderARM::VisitShr(HShr* shr) {
3544 HandleShift(shr);
3545}
3546
3547void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3548 HandleShift(shr);
3549}
3550
3551void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3552 HandleShift(ushr);
3553}
3554
3555void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3556 HandleShift(ushr);
3557}
3558
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003559void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003560 LocationSummary* locations =
3561 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
David Brazdil6de19382016-01-08 17:37:10 +00003562 if (instruction->IsStringAlloc()) {
3563 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3564 } else {
3565 InvokeRuntimeCallingConvention calling_convention;
3566 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3567 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3568 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003569 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003570}
3571
3572void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003573 // Note: if heap poisoning is enabled, the entry point takes cares
3574 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003575 if (instruction->IsStringAlloc()) {
3576 // String is allocated through StringFactory. Call NewEmptyString entry point.
3577 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3578 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3579 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3580 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3581 __ blx(LR);
3582 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3583 } else {
3584 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3585 instruction,
3586 instruction->GetDexPc(),
3587 nullptr);
3588 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3589 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003590}
3591
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003592void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3593 LocationSummary* locations =
3594 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3595 InvokeRuntimeCallingConvention calling_convention;
3596 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003597 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003598 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003599 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003600}
3601
3602void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3603 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003604 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003605 // Note: if heap poisoning is enabled, the entry point takes cares
3606 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003607 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003608 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003609 instruction->GetDexPc(),
3610 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003611 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003612}
3613
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003614void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003615 LocationSummary* locations =
3616 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003617 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3618 if (location.IsStackSlot()) {
3619 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3620 } else if (location.IsDoubleStackSlot()) {
3621 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003622 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003623 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003624}
3625
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003626void InstructionCodeGeneratorARM::VisitParameterValue(
3627 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003628 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003629}
3630
3631void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3632 LocationSummary* locations =
3633 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3634 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3635}
3636
3637void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3638 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003639}
3640
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003641void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003642 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003643 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003644 locations->SetInAt(0, Location::RequiresRegister());
3645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003646}
3647
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003648void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3649 LocationSummary* locations = not_->GetLocations();
3650 Location out = locations->Out();
3651 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003652 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003653 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003655 break;
3656
3657 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003658 __ mvn(out.AsRegisterPairLow<Register>(),
3659 ShifterOperand(in.AsRegisterPairLow<Register>()));
3660 __ mvn(out.AsRegisterPairHigh<Register>(),
3661 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003662 break;
3663
3664 default:
3665 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3666 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003667}
3668
David Brazdil66d126e2015-04-03 16:02:44 +01003669void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3670 LocationSummary* locations =
3671 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3672 locations->SetInAt(0, Location::RequiresRegister());
3673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3674}
3675
3676void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003677 LocationSummary* locations = bool_not->GetLocations();
3678 Location out = locations->Out();
3679 Location in = locations->InAt(0);
3680 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3681}
3682
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003683void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003684 LocationSummary* locations =
3685 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003686 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08003687 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003688 case Primitive::kPrimLong: {
3689 locations->SetInAt(0, Location::RequiresRegister());
3690 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003691 // Output overlaps because it is written before doing the low comparison.
3692 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003693 break;
3694 }
3695 case Primitive::kPrimFloat:
3696 case Primitive::kPrimDouble: {
3697 locations->SetInAt(0, Location::RequiresFpuRegister());
3698 locations->SetInAt(1, Location::RequiresFpuRegister());
3699 locations->SetOut(Location::RequiresRegister());
3700 break;
3701 }
3702 default:
3703 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3704 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003705}
3706
3707void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003708 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003709 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003710 Location left = locations->InAt(0);
3711 Location right = locations->InAt(1);
3712
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003713 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003714 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003715 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003716 switch (type) {
Aart Bika19616e2016-02-01 18:57:58 -08003717 case Primitive::kPrimInt: {
3718 __ LoadImmediate(out, 0);
3719 __ cmp(left.AsRegister<Register>(),
3720 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3721 less_cond = LT;
3722 break;
3723 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003724 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003725 __ cmp(left.AsRegisterPairHigh<Register>(),
3726 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003727 __ b(&less, LT);
3728 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003729 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003730 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003731 __ cmp(left.AsRegisterPairLow<Register>(),
3732 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003733 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003734 break;
3735 }
3736 case Primitive::kPrimFloat:
3737 case Primitive::kPrimDouble: {
3738 __ LoadImmediate(out, 0);
3739 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003740 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003741 } else {
3742 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3743 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3744 }
3745 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003746 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003747 break;
3748 }
3749 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003750 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003751 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003752 }
Aart Bika19616e2016-02-01 18:57:58 -08003753
Calin Juravleddb7df22014-11-25 20:56:51 +00003754 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003755 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003756
3757 __ Bind(&greater);
3758 __ LoadImmediate(out, 1);
3759 __ b(&done);
3760
3761 __ Bind(&less);
3762 __ LoadImmediate(out, -1);
3763
3764 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003765}
3766
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003767void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003768 LocationSummary* locations =
3769 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003770 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3771 locations->SetInAt(i, Location::Any());
3772 }
3773 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003774}
3775
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003776void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003777 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003778}
3779
Roland Levillainc9285912015-12-18 10:38:42 +00003780void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3781 // TODO (ported from quick): revisit ARM barrier kinds.
3782 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003783 switch (kind) {
3784 case MemBarrierKind::kAnyStore:
3785 case MemBarrierKind::kLoadAny:
3786 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003787 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003788 break;
3789 }
3790 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003791 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003792 break;
3793 }
3794 default:
3795 LOG(FATAL) << "Unexpected memory barrier " << kind;
3796 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003797 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003798}
3799
3800void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3801 uint32_t offset,
3802 Register out_lo,
3803 Register out_hi) {
3804 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003805 // Ensure `out_lo` is different from `addr`, so that loading
3806 // `offset` into `out_lo` does not clutter `addr`.
3807 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003808 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003809 __ add(IP, addr, ShifterOperand(out_lo));
3810 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003811 }
3812 __ ldrexd(out_lo, out_hi, addr);
3813}
3814
3815void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3816 uint32_t offset,
3817 Register value_lo,
3818 Register value_hi,
3819 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003820 Register temp2,
3821 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003822 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003823 if (offset != 0) {
3824 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003825 __ add(IP, addr, ShifterOperand(temp1));
3826 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003827 }
3828 __ Bind(&fail);
3829 // We need a load followed by store. (The address used in a STREX instruction must
3830 // be the same as the address in the most recently executed LDREX instruction.)
3831 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003832 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003833 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003834 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003835}
3836
3837void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3838 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3839
Nicolas Geoffray39468442014-09-02 15:17:15 +01003840 LocationSummary* locations =
3841 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003842 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003843
Calin Juravle52c48962014-12-16 17:02:57 +00003844 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003845 if (Primitive::IsFloatingPointType(field_type)) {
3846 locations->SetInAt(1, Location::RequiresFpuRegister());
3847 } else {
3848 locations->SetInAt(1, Location::RequiresRegister());
3849 }
3850
Calin Juravle52c48962014-12-16 17:02:57 +00003851 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003852 bool generate_volatile = field_info.IsVolatile()
3853 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003854 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003855 bool needs_write_barrier =
3856 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003857 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003858 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003859 if (needs_write_barrier) {
3860 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003861 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003862 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003863 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003864 // - registers need to be consecutive
3865 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003866 // We don't test for ARM yet, and the assertion makes sure that we
3867 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003868 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3869
3870 locations->AddTemp(Location::RequiresRegister());
3871 locations->AddTemp(Location::RequiresRegister());
3872 if (field_type == Primitive::kPrimDouble) {
3873 // For doubles we need two more registers to copy the value.
3874 locations->AddTemp(Location::RegisterLocation(R2));
3875 locations->AddTemp(Location::RegisterLocation(R3));
3876 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003877 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003878}
3879
Calin Juravle52c48962014-12-16 17:02:57 +00003880void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003881 const FieldInfo& field_info,
3882 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003883 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3884
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003885 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003886 Register base = locations->InAt(0).AsRegister<Register>();
3887 Location value = locations->InAt(1);
3888
3889 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003890 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003891 Primitive::Type field_type = field_info.GetFieldType();
3892 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003893 bool needs_write_barrier =
3894 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003895
3896 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003897 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003898 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003899
3900 switch (field_type) {
3901 case Primitive::kPrimBoolean:
3902 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003903 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003904 break;
3905 }
3906
3907 case Primitive::kPrimShort:
3908 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003909 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003910 break;
3911 }
3912
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003913 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003914 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003915 if (kPoisonHeapReferences && needs_write_barrier) {
3916 // Note that in the case where `value` is a null reference,
3917 // we do not enter this block, as a null reference does not
3918 // need poisoning.
3919 DCHECK_EQ(field_type, Primitive::kPrimNot);
3920 Register temp = locations->GetTemp(0).AsRegister<Register>();
3921 __ Mov(temp, value.AsRegister<Register>());
3922 __ PoisonHeapReference(temp);
3923 __ StoreToOffset(kStoreWord, temp, base, offset);
3924 } else {
3925 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3926 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003927 break;
3928 }
3929
3930 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003931 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003932 GenerateWideAtomicStore(base, offset,
3933 value.AsRegisterPairLow<Register>(),
3934 value.AsRegisterPairHigh<Register>(),
3935 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003936 locations->GetTemp(1).AsRegister<Register>(),
3937 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003938 } else {
3939 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003940 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003941 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003942 break;
3943 }
3944
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003945 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003946 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003947 break;
3948 }
3949
3950 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003951 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00003952 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003953 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
3954 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
3955
3956 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
3957
3958 GenerateWideAtomicStore(base, offset,
3959 value_reg_lo,
3960 value_reg_hi,
3961 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00003962 locations->GetTemp(3).AsRegister<Register>(),
3963 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003964 } else {
3965 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00003966 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003967 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003968 break;
3969 }
3970
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003971 case Primitive::kPrimVoid:
3972 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003973 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003974 }
Calin Juravle52c48962014-12-16 17:02:57 +00003975
Calin Juravle77520bc2015-01-12 18:45:46 +00003976 // Longs and doubles are handled in the switch.
3977 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
3978 codegen_->MaybeRecordImplicitNullCheck(instruction);
3979 }
3980
3981 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3982 Register temp = locations->GetTemp(0).AsRegister<Register>();
3983 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003984 codegen_->MarkGCCard(
3985 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00003986 }
3987
Calin Juravle52c48962014-12-16 17:02:57 +00003988 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003989 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00003990 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003991}
3992
Calin Juravle52c48962014-12-16 17:02:57 +00003993void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3994 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00003995
3996 bool object_field_get_with_read_barrier =
3997 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003998 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00003999 new (GetGraph()->GetArena()) LocationSummary(instruction,
4000 object_field_get_with_read_barrier ?
4001 LocationSummary::kCallOnSlowPath :
4002 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004003 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004004
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004005 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004006 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004007 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004008 // The output overlaps in case of volatile long: we don't want the
4009 // code generated by GenerateWideAtomicLoad to overwrite the
4010 // object's location. Likewise, in the case of an object field get
4011 // with read barriers enabled, we do not want the load to overwrite
4012 // the object's location, as we need it to emit the read barrier.
4013 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4014 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004015
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004016 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4017 locations->SetOut(Location::RequiresFpuRegister());
4018 } else {
4019 locations->SetOut(Location::RequiresRegister(),
4020 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4021 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004022 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004023 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004024 // - registers need to be consecutive
4025 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004026 // We don't test for ARM yet, and the assertion makes sure that we
4027 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004028 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4029 locations->AddTemp(Location::RequiresRegister());
4030 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004031 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4032 // We need a temporary register for the read barrier marking slow
4033 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4034 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004035 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004036}
4037
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004038Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4039 Opcode opcode) {
4040 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4041 if (constant->IsConstant() &&
4042 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4043 return Location::ConstantLocation(constant->AsConstant());
4044 }
4045 return Location::RequiresRegister();
4046}
4047
4048bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4049 Opcode opcode) {
4050 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4051 if (Primitive::Is64BitType(input_cst->GetType())) {
4052 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4053 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4054 } else {
4055 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4056 }
4057}
4058
4059bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4060 ShifterOperand so;
4061 ArmAssembler* assembler = codegen_->GetAssembler();
4062 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4063 return true;
4064 }
4065 Opcode neg_opcode = kNoOperand;
4066 switch (opcode) {
4067 case AND:
4068 neg_opcode = BIC;
4069 break;
4070 case ORR:
4071 neg_opcode = ORN;
4072 break;
4073 default:
4074 return false;
4075 }
4076 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4077}
4078
Calin Juravle52c48962014-12-16 17:02:57 +00004079void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4080 const FieldInfo& field_info) {
4081 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004082
Calin Juravle52c48962014-12-16 17:02:57 +00004083 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004084 Location base_loc = locations->InAt(0);
4085 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004086 Location out = locations->Out();
4087 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004088 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004089 Primitive::Type field_type = field_info.GetFieldType();
4090 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4091
4092 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004093 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004094 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004095 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004096
Roland Levillainc9285912015-12-18 10:38:42 +00004097 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004098 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004099 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004100
Roland Levillainc9285912015-12-18 10:38:42 +00004101 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004102 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004103 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004104
Roland Levillainc9285912015-12-18 10:38:42 +00004105 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004106 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004108
4109 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004110 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004111 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004112
4113 case Primitive::kPrimNot: {
4114 // /* HeapReference<Object> */ out = *(base + offset)
4115 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4116 Location temp_loc = locations->GetTemp(0);
4117 // Note that a potential implicit null check is handled in this
4118 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4119 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4120 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4121 if (is_volatile) {
4122 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4123 }
4124 } else {
4125 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4126 codegen_->MaybeRecordImplicitNullCheck(instruction);
4127 if (is_volatile) {
4128 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4129 }
4130 // If read barriers are enabled, emit read barriers other than
4131 // Baker's using a slow path (and also unpoison the loaded
4132 // reference, if heap poisoning is enabled).
4133 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4134 }
4135 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004136 }
4137
Roland Levillainc9285912015-12-18 10:38:42 +00004138 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004139 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004140 GenerateWideAtomicLoad(base, offset,
4141 out.AsRegisterPairLow<Register>(),
4142 out.AsRegisterPairHigh<Register>());
4143 } else {
4144 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4145 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004146 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004147
Roland Levillainc9285912015-12-18 10:38:42 +00004148 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004149 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004150 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004151
4152 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004153 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004154 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004155 Register lo = locations->GetTemp(0).AsRegister<Register>();
4156 Register hi = locations->GetTemp(1).AsRegister<Register>();
4157 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004158 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004159 __ vmovdrr(out_reg, lo, hi);
4160 } else {
4161 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004162 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004163 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004164 break;
4165 }
4166
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004167 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004168 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004169 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004170 }
Calin Juravle52c48962014-12-16 17:02:57 +00004171
Roland Levillainc9285912015-12-18 10:38:42 +00004172 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4173 // Potential implicit null checks, in the case of reference or
4174 // double fields, are handled in the previous switch statement.
4175 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004176 codegen_->MaybeRecordImplicitNullCheck(instruction);
4177 }
4178
Calin Juravle52c48962014-12-16 17:02:57 +00004179 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004180 if (field_type == Primitive::kPrimNot) {
4181 // Memory barriers, in the case of references, are also handled
4182 // in the previous switch statement.
4183 } else {
4184 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4185 }
Roland Levillain4d027112015-07-01 15:41:14 +01004186 }
Calin Juravle52c48962014-12-16 17:02:57 +00004187}
4188
4189void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4190 HandleFieldSet(instruction, instruction->GetFieldInfo());
4191}
4192
4193void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004194 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004195}
4196
4197void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4198 HandleFieldGet(instruction, instruction->GetFieldInfo());
4199}
4200
4201void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4202 HandleFieldGet(instruction, instruction->GetFieldInfo());
4203}
4204
4205void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4206 HandleFieldGet(instruction, instruction->GetFieldInfo());
4207}
4208
4209void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4210 HandleFieldGet(instruction, instruction->GetFieldInfo());
4211}
4212
4213void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4214 HandleFieldSet(instruction, instruction->GetFieldInfo());
4215}
4216
4217void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004218 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004219}
4220
Calin Juravlee460d1d2015-09-29 04:52:17 +01004221void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4222 HUnresolvedInstanceFieldGet* instruction) {
4223 FieldAccessCallingConventionARM calling_convention;
4224 codegen_->CreateUnresolvedFieldLocationSummary(
4225 instruction, instruction->GetFieldType(), calling_convention);
4226}
4227
4228void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4229 HUnresolvedInstanceFieldGet* instruction) {
4230 FieldAccessCallingConventionARM calling_convention;
4231 codegen_->GenerateUnresolvedFieldAccess(instruction,
4232 instruction->GetFieldType(),
4233 instruction->GetFieldIndex(),
4234 instruction->GetDexPc(),
4235 calling_convention);
4236}
4237
4238void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4239 HUnresolvedInstanceFieldSet* instruction) {
4240 FieldAccessCallingConventionARM calling_convention;
4241 codegen_->CreateUnresolvedFieldLocationSummary(
4242 instruction, instruction->GetFieldType(), calling_convention);
4243}
4244
4245void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4246 HUnresolvedInstanceFieldSet* instruction) {
4247 FieldAccessCallingConventionARM calling_convention;
4248 codegen_->GenerateUnresolvedFieldAccess(instruction,
4249 instruction->GetFieldType(),
4250 instruction->GetFieldIndex(),
4251 instruction->GetDexPc(),
4252 calling_convention);
4253}
4254
4255void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4256 HUnresolvedStaticFieldGet* instruction) {
4257 FieldAccessCallingConventionARM calling_convention;
4258 codegen_->CreateUnresolvedFieldLocationSummary(
4259 instruction, instruction->GetFieldType(), calling_convention);
4260}
4261
4262void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4263 HUnresolvedStaticFieldGet* instruction) {
4264 FieldAccessCallingConventionARM calling_convention;
4265 codegen_->GenerateUnresolvedFieldAccess(instruction,
4266 instruction->GetFieldType(),
4267 instruction->GetFieldIndex(),
4268 instruction->GetDexPc(),
4269 calling_convention);
4270}
4271
4272void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4273 HUnresolvedStaticFieldSet* instruction) {
4274 FieldAccessCallingConventionARM calling_convention;
4275 codegen_->CreateUnresolvedFieldLocationSummary(
4276 instruction, instruction->GetFieldType(), calling_convention);
4277}
4278
4279void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4280 HUnresolvedStaticFieldSet* instruction) {
4281 FieldAccessCallingConventionARM calling_convention;
4282 codegen_->GenerateUnresolvedFieldAccess(instruction,
4283 instruction->GetFieldType(),
4284 instruction->GetFieldIndex(),
4285 instruction->GetDexPc(),
4286 calling_convention);
4287}
4288
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004290 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4291 ? LocationSummary::kCallOnSlowPath
4292 : LocationSummary::kNoCall;
4293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004294 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004295 if (instruction->HasUses()) {
4296 locations->SetOut(Location::SameAsFirstInput());
4297 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004298}
4299
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004300void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004301 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4302 return;
4303 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004304 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004305
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004306 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4307 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4308}
4309
4310void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004311 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004312 codegen_->AddSlowPath(slow_path);
4313
4314 LocationSummary* locations = instruction->GetLocations();
4315 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004317 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004318}
4319
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004320void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004321 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004322 GenerateImplicitNullCheck(instruction);
4323 } else {
4324 GenerateExplicitNullCheck(instruction);
4325 }
4326}
4327
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004328void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004329 bool object_array_get_with_read_barrier =
4330 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004331 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004332 new (GetGraph()->GetArena()) LocationSummary(instruction,
4333 object_array_get_with_read_barrier ?
4334 LocationSummary::kCallOnSlowPath :
4335 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004336 locations->SetInAt(0, Location::RequiresRegister());
4337 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004338 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4339 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4340 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004341 // The output overlaps in the case of an object array get with
4342 // read barriers enabled: we do not want the move to overwrite the
4343 // array's location, as we need it to emit the read barrier.
4344 locations->SetOut(
4345 Location::RequiresRegister(),
4346 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004347 }
Roland Levillainc9285912015-12-18 10:38:42 +00004348 // We need a temporary register for the read barrier marking slow
4349 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4350 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4351 locations->AddTemp(Location::RequiresRegister());
4352 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004353}
4354
4355void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4356 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004357 Location obj_loc = locations->InAt(0);
4358 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004359 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004360 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361
Roland Levillainc9285912015-12-18 10:38:42 +00004362 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004363 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004364 case Primitive::kPrimBoolean: {
4365 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004366 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004367 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004368 size_t offset =
4369 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004370 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4371 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004372 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004373 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4374 }
4375 break;
4376 }
4377
4378 case Primitive::kPrimByte: {
4379 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004380 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004381 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004382 size_t offset =
4383 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004384 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4385 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004386 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004387 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4388 }
4389 break;
4390 }
4391
4392 case Primitive::kPrimShort: {
4393 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004394 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004395 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004396 size_t offset =
4397 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004398 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4399 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004400 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004401 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4402 }
4403 break;
4404 }
4405
4406 case Primitive::kPrimChar: {
4407 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004408 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004409 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004410 size_t offset =
4411 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004412 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4413 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004414 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004415 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4416 }
4417 break;
4418 }
4419
Roland Levillainc9285912015-12-18 10:38:42 +00004420 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004421 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004422 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004423 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004424 size_t offset =
4425 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004426 __ LoadFromOffset(kLoadWord, out, obj, offset);
4427 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004428 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004429 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4430 }
4431 break;
4432 }
4433
Roland Levillainc9285912015-12-18 10:38:42 +00004434 case Primitive::kPrimNot: {
4435 static_assert(
4436 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4437 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4438 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4439 // /* HeapReference<Object> */ out =
4440 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4441 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4442 Location temp = locations->GetTemp(0);
4443 // Note that a potential implicit null check is handled in this
4444 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4445 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4446 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4447 } else {
4448 Register out = out_loc.AsRegister<Register>();
4449 if (index.IsConstant()) {
4450 size_t offset =
4451 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4452 __ LoadFromOffset(kLoadWord, out, obj, offset);
4453 codegen_->MaybeRecordImplicitNullCheck(instruction);
4454 // If read barriers are enabled, emit read barriers other than
4455 // Baker's using a slow path (and also unpoison the loaded
4456 // reference, if heap poisoning is enabled).
4457 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4458 } else {
4459 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4460 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4461 codegen_->MaybeRecordImplicitNullCheck(instruction);
4462 // If read barriers are enabled, emit read barriers other than
4463 // Baker's using a slow path (and also unpoison the loaded
4464 // reference, if heap poisoning is enabled).
4465 codegen_->MaybeGenerateReadBarrierSlow(
4466 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4467 }
4468 }
4469 break;
4470 }
4471
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004472 case Primitive::kPrimLong: {
4473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004474 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004475 size_t offset =
4476 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004477 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004478 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004479 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004480 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004481 }
4482 break;
4483 }
4484
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004485 case Primitive::kPrimFloat: {
4486 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004487 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004488 if (index.IsConstant()) {
4489 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004490 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004491 } else {
4492 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004493 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004494 }
4495 break;
4496 }
4497
4498 case Primitive::kPrimDouble: {
4499 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004500 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004501 if (index.IsConstant()) {
4502 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004503 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004504 } else {
4505 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004506 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004507 }
4508 break;
4509 }
4510
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004511 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004512 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004513 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004514 }
Roland Levillain4d027112015-07-01 15:41:14 +01004515
4516 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004517 // Potential implicit null checks, in the case of reference
4518 // arrays, are handled in the previous switch statement.
4519 } else {
4520 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004521 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004522}
4523
4524void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004525 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004526
4527 bool needs_write_barrier =
4528 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004529 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4530 bool object_array_set_with_read_barrier =
4531 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004532
Nicolas Geoffray39468442014-09-02 15:17:15 +01004533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004534 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004535 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4536 LocationSummary::kCallOnSlowPath :
4537 LocationSummary::kNoCall);
4538
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004539 locations->SetInAt(0, Location::RequiresRegister());
4540 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4541 if (Primitive::IsFloatingPointType(value_type)) {
4542 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004543 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004544 locations->SetInAt(2, Location::RequiresRegister());
4545 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004546 if (needs_write_barrier) {
4547 // Temporary registers for the write barrier.
4548 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004549 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004551}
4552
4553void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4554 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004555 Location array_loc = locations->InAt(0);
4556 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004557 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004558 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004559 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004560 bool needs_write_barrier =
4561 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004562
4563 switch (value_type) {
4564 case Primitive::kPrimBoolean:
4565 case Primitive::kPrimByte: {
4566 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004567 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004568 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004569 size_t offset =
4570 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004571 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004572 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004573 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4575 }
4576 break;
4577 }
4578
4579 case Primitive::kPrimShort:
4580 case Primitive::kPrimChar: {
4581 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004582 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004583 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004584 size_t offset =
4585 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004586 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004587 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004588 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004589 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4590 }
4591 break;
4592 }
4593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004594 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004595 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004596 Location value_loc = locations->InAt(2);
4597 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004598 Register source = value;
4599
4600 if (instruction->InputAt(2)->IsNullConstant()) {
4601 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004602 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004603 size_t offset =
4604 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004605 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004606 } else {
4607 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004608 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004609 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004610 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004611 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004612 DCHECK(!needs_write_barrier);
4613 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004614 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004615 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004616
4617 DCHECK(needs_write_barrier);
4618 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4619 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4620 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4621 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4622 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4623 Label done;
4624 SlowPathCode* slow_path = nullptr;
4625
Roland Levillain3b359c72015-11-17 19:35:12 +00004626 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004627 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4628 codegen_->AddSlowPath(slow_path);
4629 if (instruction->GetValueCanBeNull()) {
4630 Label non_zero;
4631 __ CompareAndBranchIfNonZero(value, &non_zero);
4632 if (index.IsConstant()) {
4633 size_t offset =
4634 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4635 __ StoreToOffset(kStoreWord, value, array, offset);
4636 } else {
4637 DCHECK(index.IsRegister()) << index;
4638 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4639 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4640 }
4641 codegen_->MaybeRecordImplicitNullCheck(instruction);
4642 __ b(&done);
4643 __ Bind(&non_zero);
4644 }
4645
Roland Levillain3b359c72015-11-17 19:35:12 +00004646 if (kEmitCompilerReadBarrier) {
4647 // When read barriers are enabled, the type checking
4648 // instrumentation requires two read barriers:
4649 //
4650 // __ Mov(temp2, temp1);
4651 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4652 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004653 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004654 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4655 //
4656 // // /* HeapReference<Class> */ temp2 = value->klass_
4657 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004658 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004659 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4660 //
4661 // __ cmp(temp1, ShifterOperand(temp2));
4662 //
4663 // However, the second read barrier may trash `temp`, as it
4664 // is a temporary register, and as such would not be saved
4665 // along with live registers before calling the runtime (nor
4666 // restored afterwards). So in this case, we bail out and
4667 // delegate the work to the array set slow path.
4668 //
4669 // TODO: Extend the register allocator to support a new
4670 // "(locally) live temp" location so as to avoid always
4671 // going into the slow path when read barriers are enabled.
4672 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004673 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004674 // /* HeapReference<Class> */ temp1 = array->klass_
4675 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4676 codegen_->MaybeRecordImplicitNullCheck(instruction);
4677 __ MaybeUnpoisonHeapReference(temp1);
4678
4679 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4680 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4681 // /* HeapReference<Class> */ temp2 = value->klass_
4682 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4683 // If heap poisoning is enabled, no need to unpoison `temp1`
4684 // nor `temp2`, as we are comparing two poisoned references.
4685 __ cmp(temp1, ShifterOperand(temp2));
4686
4687 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4688 Label do_put;
4689 __ b(&do_put, EQ);
4690 // If heap poisoning is enabled, the `temp1` reference has
4691 // not been unpoisoned yet; unpoison it now.
4692 __ MaybeUnpoisonHeapReference(temp1);
4693
4694 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4695 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4696 // If heap poisoning is enabled, no need to unpoison
4697 // `temp1`, as we are comparing against null below.
4698 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4699 __ Bind(&do_put);
4700 } else {
4701 __ b(slow_path->GetEntryLabel(), NE);
4702 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004703 }
4704 }
4705
4706 if (kPoisonHeapReferences) {
4707 // Note that in the case where `value` is a null reference,
4708 // we do not enter this block, as a null reference does not
4709 // need poisoning.
4710 DCHECK_EQ(value_type, Primitive::kPrimNot);
4711 __ Mov(temp1, value);
4712 __ PoisonHeapReference(temp1);
4713 source = temp1;
4714 }
4715
4716 if (index.IsConstant()) {
4717 size_t offset =
4718 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4719 __ StoreToOffset(kStoreWord, source, array, offset);
4720 } else {
4721 DCHECK(index.IsRegister()) << index;
4722 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4723 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4724 }
4725
Roland Levillain3b359c72015-11-17 19:35:12 +00004726 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004727 codegen_->MaybeRecordImplicitNullCheck(instruction);
4728 }
4729
4730 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4731
4732 if (done.IsLinked()) {
4733 __ Bind(&done);
4734 }
4735
4736 if (slow_path != nullptr) {
4737 __ Bind(slow_path->GetExitLabel());
4738 }
4739
4740 break;
4741 }
4742
4743 case Primitive::kPrimInt: {
4744 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4745 Register value = locations->InAt(2).AsRegister<Register>();
4746 if (index.IsConstant()) {
4747 size_t offset =
4748 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4749 __ StoreToOffset(kStoreWord, value, array, offset);
4750 } else {
4751 DCHECK(index.IsRegister()) << index;
4752 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4753 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4754 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755 break;
4756 }
4757
4758 case Primitive::kPrimLong: {
4759 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004760 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004761 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004762 size_t offset =
4763 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004764 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004765 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004766 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004767 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 }
4769 break;
4770 }
4771
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004772 case Primitive::kPrimFloat: {
4773 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4774 Location value = locations->InAt(2);
4775 DCHECK(value.IsFpuRegister());
4776 if (index.IsConstant()) {
4777 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004778 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004779 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004780 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004781 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4782 }
4783 break;
4784 }
4785
4786 case Primitive::kPrimDouble: {
4787 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4788 Location value = locations->InAt(2);
4789 DCHECK(value.IsFpuRegisterPair());
4790 if (index.IsConstant()) {
4791 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004792 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004793 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004795 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4796 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004797
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004798 break;
4799 }
4800
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004802 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004803 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004804 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004805
Roland Levillain80e67092016-01-08 16:04:55 +00004806 // Objects are handled in the switch.
4807 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004808 codegen_->MaybeRecordImplicitNullCheck(instruction);
4809 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810}
4811
4812void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004813 LocationSummary* locations =
4814 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004815 locations->SetInAt(0, Location::RequiresRegister());
4816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817}
4818
4819void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4820 LocationSummary* locations = instruction->GetLocations();
4821 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004822 Register obj = locations->InAt(0).AsRegister<Register>();
4823 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004824 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004825 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004826}
4827
4828void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004829 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4830 ? LocationSummary::kCallOnSlowPath
4831 : LocationSummary::kNoCall;
4832 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004833 locations->SetInAt(0, Location::RequiresRegister());
4834 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004835 if (instruction->HasUses()) {
4836 locations->SetOut(Location::SameAsFirstInput());
4837 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004838}
4839
4840void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4841 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004842 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004843 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844 codegen_->AddSlowPath(slow_path);
4845
Roland Levillain271ab9c2014-11-27 15:23:57 +00004846 Register index = locations->InAt(0).AsRegister<Register>();
4847 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848
4849 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004850 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004851}
4852
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004853void CodeGeneratorARM::MarkGCCard(Register temp,
4854 Register card,
4855 Register object,
4856 Register value,
4857 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004858 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004859 if (can_be_null) {
4860 __ CompareAndBranchIfZero(value, &is_null);
4861 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4863 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4864 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004865 if (can_be_null) {
4866 __ Bind(&is_null);
4867 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004868}
4869
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004870void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004871 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004872}
4873
4874void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004875 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4876}
4877
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004878void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4879 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4880}
4881
4882void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004883 HBasicBlock* block = instruction->GetBlock();
4884 if (block->GetLoopInformation() != nullptr) {
4885 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4886 // The back edge will generate the suspend check.
4887 return;
4888 }
4889 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4890 // The goto will generate the suspend check.
4891 return;
4892 }
4893 GenerateSuspendCheck(instruction, nullptr);
4894}
4895
4896void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4897 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004898 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004899 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4900 if (slow_path == nullptr) {
4901 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4902 instruction->SetSlowPath(slow_path);
4903 codegen_->AddSlowPath(slow_path);
4904 if (successor != nullptr) {
4905 DCHECK(successor->IsLoopHeader());
4906 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4907 }
4908 } else {
4909 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4910 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004911
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004912 __ LoadFromOffset(
4913 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004914 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004915 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004916 __ Bind(slow_path->GetReturnLabel());
4917 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004918 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004919 __ b(slow_path->GetEntryLabel());
4920 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004921}
4922
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004923ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4924 return codegen_->GetAssembler();
4925}
4926
4927void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004928 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004929 Location source = move->GetSource();
4930 Location destination = move->GetDestination();
4931
4932 if (source.IsRegister()) {
4933 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004934 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004935 } else if (destination.IsFpuRegister()) {
4936 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004937 } else {
4938 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004939 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004940 SP, destination.GetStackIndex());
4941 }
4942 } else if (source.IsStackSlot()) {
4943 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004944 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004945 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004946 } else if (destination.IsFpuRegister()) {
4947 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004948 } else {
4949 DCHECK(destination.IsStackSlot());
4950 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
4951 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
4952 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004953 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004954 if (destination.IsRegister()) {
4955 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
4956 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004957 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004958 } else {
4959 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004960 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
4961 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004962 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004963 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00004964 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
4965 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004966 } else if (destination.IsRegisterPair()) {
4967 DCHECK(ExpectedPairLayout(destination));
4968 __ LoadFromOffset(
4969 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
4970 } else {
4971 DCHECK(destination.IsFpuRegisterPair()) << destination;
4972 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4973 SP,
4974 source.GetStackIndex());
4975 }
4976 } else if (source.IsRegisterPair()) {
4977 if (destination.IsRegisterPair()) {
4978 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
4979 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00004980 } else if (destination.IsFpuRegisterPair()) {
4981 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4982 source.AsRegisterPairLow<Register>(),
4983 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004984 } else {
4985 DCHECK(destination.IsDoubleStackSlot()) << destination;
4986 DCHECK(ExpectedPairLayout(source));
4987 __ StoreToOffset(
4988 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
4989 }
4990 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00004991 if (destination.IsRegisterPair()) {
4992 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
4993 destination.AsRegisterPairHigh<Register>(),
4994 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4995 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00004996 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
4997 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
4998 } else {
4999 DCHECK(destination.IsDoubleStackSlot()) << destination;
5000 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5001 SP,
5002 destination.GetStackIndex());
5003 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005004 } else {
5005 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005006 HConstant* constant = source.GetConstant();
5007 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5008 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005009 if (destination.IsRegister()) {
5010 __ LoadImmediate(destination.AsRegister<Register>(), value);
5011 } else {
5012 DCHECK(destination.IsStackSlot());
5013 __ LoadImmediate(IP, value);
5014 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5015 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005016 } else if (constant->IsLongConstant()) {
5017 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005018 if (destination.IsRegisterPair()) {
5019 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5020 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005021 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005022 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005023 __ LoadImmediate(IP, Low32Bits(value));
5024 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5025 __ LoadImmediate(IP, High32Bits(value));
5026 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5027 }
5028 } else if (constant->IsDoubleConstant()) {
5029 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005030 if (destination.IsFpuRegisterPair()) {
5031 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005032 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005033 DCHECK(destination.IsDoubleStackSlot()) << destination;
5034 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005035 __ LoadImmediate(IP, Low32Bits(int_value));
5036 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5037 __ LoadImmediate(IP, High32Bits(int_value));
5038 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5039 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005040 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005041 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005042 float value = constant->AsFloatConstant()->GetValue();
5043 if (destination.IsFpuRegister()) {
5044 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5045 } else {
5046 DCHECK(destination.IsStackSlot());
5047 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5048 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5049 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005050 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005051 }
5052}
5053
5054void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5055 __ Mov(IP, reg);
5056 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5057 __ StoreToOffset(kStoreWord, IP, SP, mem);
5058}
5059
5060void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5061 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5062 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5063 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5064 SP, mem1 + stack_offset);
5065 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5066 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5067 SP, mem2 + stack_offset);
5068 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5069}
5070
5071void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005072 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005073 Location source = move->GetSource();
5074 Location destination = move->GetDestination();
5075
5076 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005077 DCHECK_NE(source.AsRegister<Register>(), IP);
5078 DCHECK_NE(destination.AsRegister<Register>(), IP);
5079 __ Mov(IP, source.AsRegister<Register>());
5080 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5081 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005082 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005083 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005084 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005085 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005086 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5087 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005088 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005089 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005090 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005091 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005092 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005093 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005094 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005095 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005096 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5097 destination.AsRegisterPairHigh<Register>(),
5098 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005099 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005100 Register low_reg = source.IsRegisterPair()
5101 ? source.AsRegisterPairLow<Register>()
5102 : destination.AsRegisterPairLow<Register>();
5103 int mem = source.IsRegisterPair()
5104 ? destination.GetStackIndex()
5105 : source.GetStackIndex();
5106 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005107 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005108 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005109 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005110 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005111 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5112 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005113 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005114 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005115 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005116 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5117 DRegister reg = source.IsFpuRegisterPair()
5118 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5119 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5120 int mem = source.IsFpuRegisterPair()
5121 ? destination.GetStackIndex()
5122 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005123 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005124 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005125 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005126 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5127 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5128 : destination.AsFpuRegister<SRegister>();
5129 int mem = source.IsFpuRegister()
5130 ? destination.GetStackIndex()
5131 : source.GetStackIndex();
5132
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005133 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005134 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005135 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005136 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005137 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5138 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005139 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005140 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005141 }
5142}
5143
5144void ParallelMoveResolverARM::SpillScratch(int reg) {
5145 __ Push(static_cast<Register>(reg));
5146}
5147
5148void ParallelMoveResolverARM::RestoreScratch(int reg) {
5149 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005150}
5151
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005152void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005153 InvokeRuntimeCallingConvention calling_convention;
5154 CodeGenerator::CreateLoadClassLocationSummary(
5155 cls,
5156 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005157 Location::RegisterLocation(R0),
5158 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005159}
5160
5161void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005162 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005163 if (cls->NeedsAccessCheck()) {
5164 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5165 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5166 cls,
5167 cls->GetDexPc(),
5168 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005169 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005170 return;
5171 }
5172
Roland Levillain3b359c72015-11-17 19:35:12 +00005173 Location out_loc = locations->Out();
5174 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005175 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005176
Calin Juravle580b6092015-10-06 17:35:58 +01005177 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005178 DCHECK(!cls->CanCallRuntime());
5179 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005180 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5181 GenerateGcRootFieldLoad(
5182 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005183 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005184 // /* GcRoot<mirror::Class>[] */ out =
5185 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005186 __ LoadFromOffset(kLoadWord,
5187 out,
5188 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005189 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005190 // /* GcRoot<mirror::Class> */ out = out[type_index]
5191 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005192
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005193 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5194 DCHECK(cls->CanCallRuntime());
5195 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5196 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5197 codegen_->AddSlowPath(slow_path);
5198 if (!cls->IsInDexCache()) {
5199 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5200 }
5201 if (cls->MustGenerateClinitCheck()) {
5202 GenerateClassInitializationCheck(slow_path, out);
5203 } else {
5204 __ Bind(slow_path->GetExitLabel());
5205 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005206 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005207 }
5208}
5209
5210void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5211 LocationSummary* locations =
5212 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5213 locations->SetInAt(0, Location::RequiresRegister());
5214 if (check->HasUses()) {
5215 locations->SetOut(Location::SameAsFirstInput());
5216 }
5217}
5218
5219void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005220 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005221 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005222 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005223 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005224 GenerateClassInitializationCheck(slow_path,
5225 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005226}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005227
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005228void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005229 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005230 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5231 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5232 __ b(slow_path->GetEntryLabel(), LT);
5233 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5234 // properly. Therefore, we do a memory fence.
5235 __ dmb(ISH);
5236 __ Bind(slow_path->GetExitLabel());
5237}
5238
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005239void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005240 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5241 ? LocationSummary::kCallOnSlowPath
5242 : LocationSummary::kNoCall;
5243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005244 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005245 locations->SetOut(Location::RequiresRegister());
5246}
5247
5248void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005249 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005250 Location out_loc = locations->Out();
5251 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005252 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005253
Roland Levillainc9285912015-12-18 10:38:42 +00005254 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5255 GenerateGcRootFieldLoad(
5256 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005257 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005258 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005259 // /* GcRoot<mirror::String> */ out = out[string_index]
5260 GenerateGcRootFieldLoad(
5261 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005262
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005263 if (!load->IsInDexCache()) {
5264 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5265 codegen_->AddSlowPath(slow_path);
5266 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5267 __ Bind(slow_path->GetExitLabel());
5268 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005269}
5270
David Brazdilcb1c0552015-08-04 16:22:25 +01005271static int32_t GetExceptionTlsOffset() {
5272 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5273}
5274
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005275void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5276 LocationSummary* locations =
5277 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5278 locations->SetOut(Location::RequiresRegister());
5279}
5280
5281void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005282 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005283 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5284}
5285
5286void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5287 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5288}
5289
5290void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005291 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005292 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005293}
5294
5295void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5296 LocationSummary* locations =
5297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5298 InvokeRuntimeCallingConvention calling_convention;
5299 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5300}
5301
5302void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5303 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005304 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005305 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005306}
5307
Roland Levillainc9285912015-12-18 10:38:42 +00005308static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5309 return kEmitCompilerReadBarrier &&
5310 (kUseBakerReadBarrier ||
5311 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5312 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5313 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5314}
5315
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005316void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005317 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005318 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5319 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005320 case TypeCheckKind::kExactCheck:
5321 case TypeCheckKind::kAbstractClassCheck:
5322 case TypeCheckKind::kClassHierarchyCheck:
5323 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005324 call_kind =
5325 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005326 break;
5327 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005328 case TypeCheckKind::kUnresolvedCheck:
5329 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005330 call_kind = LocationSummary::kCallOnSlowPath;
5331 break;
5332 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005333
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005335 locations->SetInAt(0, Location::RequiresRegister());
5336 locations->SetInAt(1, Location::RequiresRegister());
5337 // The "out" register is used as a temporary, so it overlaps with the inputs.
5338 // Note that TypeCheckSlowPathARM uses this register too.
5339 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5340 // When read barriers are enabled, we need a temporary register for
5341 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005342 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005343 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005344 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005345}
5346
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005347void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005348 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005349 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005350 Location obj_loc = locations->InAt(0);
5351 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005352 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005353 Location out_loc = locations->Out();
5354 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005355 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005356 locations->GetTemp(0) :
5357 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005358 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005359 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5360 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5361 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005362 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005363 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005364
5365 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005366 // avoid null check if we know obj is not null.
5367 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005368 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005369 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005370
Roland Levillain3b359c72015-11-17 19:35:12 +00005371 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005372 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005373
Roland Levillainc9285912015-12-18 10:38:42 +00005374 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005375 case TypeCheckKind::kExactCheck: {
5376 __ cmp(out, ShifterOperand(cls));
5377 // Classes must be equal for the instanceof to succeed.
5378 __ b(&zero, NE);
5379 __ LoadImmediate(out, 1);
5380 __ b(&done);
5381 break;
5382 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005383
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005384 case TypeCheckKind::kAbstractClassCheck: {
5385 // If the class is abstract, we eagerly fetch the super class of the
5386 // object to avoid doing a comparison we know will fail.
5387 Label loop;
5388 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005389 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005390 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005391 // If `out` is null, we use it for the result, and jump to `done`.
5392 __ CompareAndBranchIfZero(out, &done);
5393 __ cmp(out, ShifterOperand(cls));
5394 __ b(&loop, NE);
5395 __ LoadImmediate(out, 1);
5396 if (zero.IsLinked()) {
5397 __ b(&done);
5398 }
5399 break;
5400 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005401
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005402 case TypeCheckKind::kClassHierarchyCheck: {
5403 // Walk over the class hierarchy to find a match.
5404 Label loop, success;
5405 __ Bind(&loop);
5406 __ cmp(out, ShifterOperand(cls));
5407 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005408 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005409 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005410 __ CompareAndBranchIfNonZero(out, &loop);
5411 // If `out` is null, we use it for the result, and jump to `done`.
5412 __ b(&done);
5413 __ Bind(&success);
5414 __ LoadImmediate(out, 1);
5415 if (zero.IsLinked()) {
5416 __ b(&done);
5417 }
5418 break;
5419 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005420
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005421 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005422 // Do an exact check.
5423 Label exact_check;
5424 __ cmp(out, ShifterOperand(cls));
5425 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005426 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005427 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005428 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005429 // If `out` is null, we use it for the result, and jump to `done`.
5430 __ CompareAndBranchIfZero(out, &done);
5431 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5432 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5433 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005434 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005435 __ LoadImmediate(out, 1);
5436 __ b(&done);
5437 break;
5438 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005439
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005440 case TypeCheckKind::kArrayCheck: {
5441 __ cmp(out, ShifterOperand(cls));
5442 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005443 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5444 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005445 codegen_->AddSlowPath(slow_path);
5446 __ b(slow_path->GetEntryLabel(), NE);
5447 __ LoadImmediate(out, 1);
5448 if (zero.IsLinked()) {
5449 __ b(&done);
5450 }
5451 break;
5452 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005453
Calin Juravle98893e12015-10-02 21:05:03 +01005454 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005455 case TypeCheckKind::kInterfaceCheck: {
5456 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005457 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005458 // cases.
5459 //
5460 // We cannot directly call the InstanceofNonTrivial runtime
5461 // entry point without resorting to a type checking slow path
5462 // here (i.e. by calling InvokeRuntime directly), as it would
5463 // require to assign fixed registers for the inputs of this
5464 // HInstanceOf instruction (following the runtime calling
5465 // convention), which might be cluttered by the potential first
5466 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005467 //
5468 // TODO: Introduce a new runtime entry point taking the object
5469 // to test (instead of its class) as argument, and let it deal
5470 // with the read barrier issues. This will let us refactor this
5471 // case of the `switch` code as it was previously (with a direct
5472 // call to the runtime not using a type checking slow path).
5473 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005474 DCHECK(locations->OnlyCallsOnSlowPath());
5475 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5476 /* is_fatal */ false);
5477 codegen_->AddSlowPath(slow_path);
5478 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005479 if (zero.IsLinked()) {
5480 __ b(&done);
5481 }
5482 break;
5483 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005484 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005486 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005487 __ Bind(&zero);
5488 __ LoadImmediate(out, 0);
5489 }
5490
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005491 if (done.IsLinked()) {
5492 __ Bind(&done);
5493 }
5494
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005495 if (slow_path != nullptr) {
5496 __ Bind(slow_path->GetExitLabel());
5497 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005498}
5499
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005500void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005501 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5502 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5503
Roland Levillain3b359c72015-11-17 19:35:12 +00005504 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5505 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005506 case TypeCheckKind::kExactCheck:
5507 case TypeCheckKind::kAbstractClassCheck:
5508 case TypeCheckKind::kClassHierarchyCheck:
5509 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005510 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5511 LocationSummary::kCallOnSlowPath :
5512 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005513 break;
5514 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005515 case TypeCheckKind::kUnresolvedCheck:
5516 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005517 call_kind = LocationSummary::kCallOnSlowPath;
5518 break;
5519 }
5520
Roland Levillain3b359c72015-11-17 19:35:12 +00005521 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5522 locations->SetInAt(0, Location::RequiresRegister());
5523 locations->SetInAt(1, Location::RequiresRegister());
5524 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5525 locations->AddTemp(Location::RequiresRegister());
5526 // When read barriers are enabled, we need an additional temporary
5527 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005528 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005529 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005530 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005531}
5532
5533void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005534 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005535 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005536 Location obj_loc = locations->InAt(0);
5537 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005538 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005539 Location temp_loc = locations->GetTemp(0);
5540 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005541 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005542 locations->GetTemp(1) :
5543 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005544 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005545 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5546 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5547 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005548
Roland Levillain3b359c72015-11-17 19:35:12 +00005549 bool is_type_check_slow_path_fatal =
5550 (type_check_kind == TypeCheckKind::kExactCheck ||
5551 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5552 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5553 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5554 !instruction->CanThrowIntoCatchBlock();
5555 SlowPathCode* type_check_slow_path =
5556 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5557 is_type_check_slow_path_fatal);
5558 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005559
5560 Label done;
5561 // Avoid null check if we know obj is not null.
5562 if (instruction->MustDoNullCheck()) {
5563 __ CompareAndBranchIfZero(obj, &done);
5564 }
5565
Roland Levillain3b359c72015-11-17 19:35:12 +00005566 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005567 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005568
Roland Levillain3b359c72015-11-17 19:35:12 +00005569 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005570 case TypeCheckKind::kExactCheck:
5571 case TypeCheckKind::kArrayCheck: {
5572 __ cmp(temp, ShifterOperand(cls));
5573 // Jump to slow path for throwing the exception or doing a
5574 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005575 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005576 break;
5577 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005578
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005579 case TypeCheckKind::kAbstractClassCheck: {
5580 // If the class is abstract, we eagerly fetch the super class of the
5581 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005582 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005583 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005584 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005585 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005586
5587 // If the class reference currently in `temp` is not null, jump
5588 // to the `compare_classes` label to compare it with the checked
5589 // class.
5590 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5591 // Otherwise, jump to the slow path to throw the exception.
5592 //
5593 // But before, move back the object's class into `temp` before
5594 // going into the slow path, as it has been overwritten in the
5595 // meantime.
5596 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005597 GenerateReferenceLoadTwoRegisters(
5598 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005599 __ b(type_check_slow_path->GetEntryLabel());
5600
5601 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005602 __ cmp(temp, ShifterOperand(cls));
5603 __ b(&loop, NE);
5604 break;
5605 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005606
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005607 case TypeCheckKind::kClassHierarchyCheck: {
5608 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005609 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005610 __ Bind(&loop);
5611 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005612 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005613
Roland Levillain3b359c72015-11-17 19:35:12 +00005614 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005615 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005616
5617 // If the class reference currently in `temp` is not null, jump
5618 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005620 // Otherwise, jump to the slow path to throw the exception.
5621 //
5622 // But before, move back the object's class into `temp` before
5623 // going into the slow path, as it has been overwritten in the
5624 // meantime.
5625 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005626 GenerateReferenceLoadTwoRegisters(
5627 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005628 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005629 break;
5630 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005631
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005632 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005633 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005634 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005635 __ cmp(temp, ShifterOperand(cls));
5636 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005637
5638 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005639 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005640 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005641
5642 // If the component type is not null (i.e. the object is indeed
5643 // an array), jump to label `check_non_primitive_component_type`
5644 // to further check that this component type is not a primitive
5645 // type.
5646 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5647 // Otherwise, jump to the slow path to throw the exception.
5648 //
5649 // But before, move back the object's class into `temp` before
5650 // going into the slow path, as it has been overwritten in the
5651 // meantime.
5652 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005653 GenerateReferenceLoadTwoRegisters(
5654 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005655 __ b(type_check_slow_path->GetEntryLabel());
5656
5657 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005658 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005659 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5660 __ CompareAndBranchIfZero(temp, &done);
5661 // Same comment as above regarding `temp` and the slow path.
5662 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005663 GenerateReferenceLoadTwoRegisters(
5664 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005665 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005666 break;
5667 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005668
Calin Juravle98893e12015-10-02 21:05:03 +01005669 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005670 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005671 // We always go into the type check slow path for the unresolved
5672 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005673 //
5674 // We cannot directly call the CheckCast runtime entry point
5675 // without resorting to a type checking slow path here (i.e. by
5676 // calling InvokeRuntime directly), as it would require to
5677 // assign fixed registers for the inputs of this HInstanceOf
5678 // instruction (following the runtime calling convention), which
5679 // might be cluttered by the potential first read barrier
5680 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005681 //
5682 // TODO: Introduce a new runtime entry point taking the object
5683 // to test (instead of its class) as argument, and let it deal
5684 // with the read barrier issues. This will let us refactor this
5685 // case of the `switch` code as it was previously (with a direct
5686 // call to the runtime not using a type checking slow path).
5687 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005688 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005689 break;
5690 }
5691 __ Bind(&done);
5692
Roland Levillain3b359c72015-11-17 19:35:12 +00005693 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005694}
5695
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005696void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5697 LocationSummary* locations =
5698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5699 InvokeRuntimeCallingConvention calling_convention;
5700 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5701}
5702
5703void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5704 codegen_->InvokeRuntime(instruction->IsEnter()
5705 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5706 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005707 instruction->GetDexPc(),
5708 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005709 if (instruction->IsEnter()) {
5710 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5711 } else {
5712 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5713 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005714}
5715
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005716void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5717void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5718void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005719
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005720void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005721 LocationSummary* locations =
5722 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5723 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5724 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005725 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005726 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005727 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005728 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005729}
5730
5731void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5732 HandleBitwiseOperation(instruction);
5733}
5734
5735void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5736 HandleBitwiseOperation(instruction);
5737}
5738
5739void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5740 HandleBitwiseOperation(instruction);
5741}
5742
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005743void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5744 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5745 if (value == 0xffffffffu) {
5746 if (out != first) {
5747 __ mov(out, ShifterOperand(first));
5748 }
5749 return;
5750 }
5751 if (value == 0u) {
5752 __ mov(out, ShifterOperand(0));
5753 return;
5754 }
5755 ShifterOperand so;
5756 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5757 __ and_(out, first, so);
5758 } else {
5759 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5760 __ bic(out, first, ShifterOperand(~value));
5761 }
5762}
5763
5764void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5765 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5766 if (value == 0u) {
5767 if (out != first) {
5768 __ mov(out, ShifterOperand(first));
5769 }
5770 return;
5771 }
5772 if (value == 0xffffffffu) {
5773 __ mvn(out, ShifterOperand(0));
5774 return;
5775 }
5776 ShifterOperand so;
5777 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5778 __ orr(out, first, so);
5779 } else {
5780 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5781 __ orn(out, first, ShifterOperand(~value));
5782 }
5783}
5784
5785void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5786 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5787 if (value == 0u) {
5788 if (out != first) {
5789 __ mov(out, ShifterOperand(first));
5790 }
5791 return;
5792 }
5793 __ eor(out, first, ShifterOperand(value));
5794}
5795
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005796void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5797 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005798 Location first = locations->InAt(0);
5799 Location second = locations->InAt(1);
5800 Location out = locations->Out();
5801
5802 if (second.IsConstant()) {
5803 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5804 uint32_t value_low = Low32Bits(value);
5805 if (instruction->GetResultType() == Primitive::kPrimInt) {
5806 Register first_reg = first.AsRegister<Register>();
5807 Register out_reg = out.AsRegister<Register>();
5808 if (instruction->IsAnd()) {
5809 GenerateAndConst(out_reg, first_reg, value_low);
5810 } else if (instruction->IsOr()) {
5811 GenerateOrrConst(out_reg, first_reg, value_low);
5812 } else {
5813 DCHECK(instruction->IsXor());
5814 GenerateEorConst(out_reg, first_reg, value_low);
5815 }
5816 } else {
5817 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5818 uint32_t value_high = High32Bits(value);
5819 Register first_low = first.AsRegisterPairLow<Register>();
5820 Register first_high = first.AsRegisterPairHigh<Register>();
5821 Register out_low = out.AsRegisterPairLow<Register>();
5822 Register out_high = out.AsRegisterPairHigh<Register>();
5823 if (instruction->IsAnd()) {
5824 GenerateAndConst(out_low, first_low, value_low);
5825 GenerateAndConst(out_high, first_high, value_high);
5826 } else if (instruction->IsOr()) {
5827 GenerateOrrConst(out_low, first_low, value_low);
5828 GenerateOrrConst(out_high, first_high, value_high);
5829 } else {
5830 DCHECK(instruction->IsXor());
5831 GenerateEorConst(out_low, first_low, value_low);
5832 GenerateEorConst(out_high, first_high, value_high);
5833 }
5834 }
5835 return;
5836 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005837
5838 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005839 Register first_reg = first.AsRegister<Register>();
5840 ShifterOperand second_reg(second.AsRegister<Register>());
5841 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005842 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005843 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005844 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005845 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005846 } else {
5847 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005848 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005849 }
5850 } else {
5851 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005852 Register first_low = first.AsRegisterPairLow<Register>();
5853 Register first_high = first.AsRegisterPairHigh<Register>();
5854 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5855 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5856 Register out_low = out.AsRegisterPairLow<Register>();
5857 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005858 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005859 __ and_(out_low, first_low, second_low);
5860 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005861 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005862 __ orr(out_low, first_low, second_low);
5863 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005864 } else {
5865 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005866 __ eor(out_low, first_low, second_low);
5867 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005868 }
5869 }
5870}
5871
Roland Levillainc9285912015-12-18 10:38:42 +00005872void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5873 Location out,
5874 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005875 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005876 Register out_reg = out.AsRegister<Register>();
5877 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005878 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005879 if (kUseBakerReadBarrier) {
5880 // Load with fast path based Baker's read barrier.
5881 // /* HeapReference<Object> */ out = *(out + offset)
5882 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005883 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005884 } else {
5885 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005886 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00005887 // in the following move operation, as we will need it for the
5888 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005889 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00005890 // /* HeapReference<Object> */ out = *(out + offset)
5891 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005892 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00005893 }
5894 } else {
5895 // Plain load with no read barrier.
5896 // /* HeapReference<Object> */ out = *(out + offset)
5897 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5898 __ MaybeUnpoisonHeapReference(out_reg);
5899 }
5900}
5901
5902void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5903 Location out,
5904 Location obj,
5905 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005906 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005907 Register out_reg = out.AsRegister<Register>();
5908 Register obj_reg = obj.AsRegister<Register>();
5909 if (kEmitCompilerReadBarrier) {
5910 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005911 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005912 // Load with fast path based Baker's read barrier.
5913 // /* HeapReference<Object> */ out = *(obj + offset)
5914 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005915 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005916 } else {
5917 // Load with slow path based read barrier.
5918 // /* HeapReference<Object> */ out = *(obj + offset)
5919 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5920 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5921 }
5922 } else {
5923 // Plain load with no read barrier.
5924 // /* HeapReference<Object> */ out = *(obj + offset)
5925 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5926 __ MaybeUnpoisonHeapReference(out_reg);
5927 }
5928}
5929
5930void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
5931 Location root,
5932 Register obj,
5933 uint32_t offset) {
5934 Register root_reg = root.AsRegister<Register>();
5935 if (kEmitCompilerReadBarrier) {
5936 if (kUseBakerReadBarrier) {
5937 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5938 // Baker's read barrier are used:
5939 //
5940 // root = obj.field;
5941 // if (Thread::Current()->GetIsGcMarking()) {
5942 // root = ReadBarrier::Mark(root)
5943 // }
5944
5945 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5946 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5947 static_assert(
5948 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5949 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5950 "have different sizes.");
5951 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5952 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5953 "have different sizes.");
5954
5955 // Slow path used to mark the GC root `root`.
5956 SlowPathCode* slow_path =
5957 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
5958 codegen_->AddSlowPath(slow_path);
5959
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005960 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00005961 __ LoadFromOffset(
5962 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
5963 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
5964 __ Bind(slow_path->GetExitLabel());
5965 } else {
5966 // GC root loaded through a slow path for read barriers other
5967 // than Baker's.
5968 // /* GcRoot<mirror::Object>* */ root = obj + offset
5969 __ AddConstant(root_reg, obj, offset);
5970 // /* mirror::Object* */ root = root->Read()
5971 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5972 }
5973 } else {
5974 // Plain GC root load with no read barrier.
5975 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5976 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5977 // Note that GC roots are not affected by heap poisoning, thus we
5978 // do not have to unpoison `root_reg` here.
5979 }
5980}
5981
5982void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5983 Location ref,
5984 Register obj,
5985 uint32_t offset,
5986 Location temp,
5987 bool needs_null_check) {
5988 DCHECK(kEmitCompilerReadBarrier);
5989 DCHECK(kUseBakerReadBarrier);
5990
5991 // /* HeapReference<Object> */ ref = *(obj + offset)
5992 Location no_index = Location::NoLocation();
5993 GenerateReferenceLoadWithBakerReadBarrier(
5994 instruction, ref, obj, offset, no_index, temp, needs_null_check);
5995}
5996
5997void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5998 Location ref,
5999 Register obj,
6000 uint32_t data_offset,
6001 Location index,
6002 Location temp,
6003 bool needs_null_check) {
6004 DCHECK(kEmitCompilerReadBarrier);
6005 DCHECK(kUseBakerReadBarrier);
6006
6007 // /* HeapReference<Object> */ ref =
6008 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6009 GenerateReferenceLoadWithBakerReadBarrier(
6010 instruction, ref, obj, data_offset, index, temp, needs_null_check);
6011}
6012
6013void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6014 Location ref,
6015 Register obj,
6016 uint32_t offset,
6017 Location index,
6018 Location temp,
6019 bool needs_null_check) {
6020 DCHECK(kEmitCompilerReadBarrier);
6021 DCHECK(kUseBakerReadBarrier);
6022
6023 // In slow path based read barriers, the read barrier call is
6024 // inserted after the original load. However, in fast path based
6025 // Baker's read barriers, we need to perform the load of
6026 // mirror::Object::monitor_ *before* the original reference load.
6027 // This load-load ordering is required by the read barrier.
6028 // The fast path/slow path (for Baker's algorithm) should look like:
6029 //
6030 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6031 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6032 // HeapReference<Object> ref = *src; // Original reference load.
6033 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6034 // if (is_gray) {
6035 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6036 // }
6037 //
6038 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006039 // slightly more complex as it performs additional checks that we do
6040 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006041
6042 Register ref_reg = ref.AsRegister<Register>();
6043 Register temp_reg = temp.AsRegister<Register>();
6044 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6045
6046 // /* int32_t */ monitor = obj->monitor_
6047 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6048 if (needs_null_check) {
6049 MaybeRecordImplicitNullCheck(instruction);
6050 }
6051 // /* LockWord */ lock_word = LockWord(monitor)
6052 static_assert(sizeof(LockWord) == sizeof(int32_t),
6053 "art::LockWord and int32_t have different sizes.");
6054 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6055 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6056 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6057 static_assert(
6058 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6059 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6060
6061 // Introduce a dependency on the high bits of rb_state, which shall
6062 // be all zeroes, to prevent load-load reordering, and without using
6063 // a memory barrier (which would be more expensive).
6064 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6065 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6066 // obj is unchanged by this operation, but its value now depends on
6067 // IP, which depends on temp_reg.
6068 __ add(obj, obj, ShifterOperand(IP));
6069
6070 // The actual reference load.
6071 if (index.IsValid()) {
6072 static_assert(
6073 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6074 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6075 // /* HeapReference<Object> */ ref =
6076 // *(obj + offset + index * sizeof(HeapReference<Object>))
6077 if (index.IsConstant()) {
6078 size_t computed_offset =
6079 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6080 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6081 } else {
6082 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6083 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6084 }
6085 } else {
6086 // /* HeapReference<Object> */ ref = *(obj + offset)
6087 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6088 }
6089
6090 // Object* ref = ref_addr->AsMirrorPtr()
6091 __ MaybeUnpoisonHeapReference(ref_reg);
6092
6093 // Slow path used to mark the object `ref` when it is gray.
6094 SlowPathCode* slow_path =
6095 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6096 AddSlowPath(slow_path);
6097
6098 // if (rb_state == ReadBarrier::gray_ptr_)
6099 // ref = ReadBarrier::Mark(ref);
6100 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6101 __ b(slow_path->GetEntryLabel(), EQ);
6102 __ Bind(slow_path->GetExitLabel());
6103}
6104
6105void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6106 Location out,
6107 Location ref,
6108 Location obj,
6109 uint32_t offset,
6110 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006111 DCHECK(kEmitCompilerReadBarrier);
6112
Roland Levillainc9285912015-12-18 10:38:42 +00006113 // Insert a slow path based read barrier *after* the reference load.
6114 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006115 // If heap poisoning is enabled, the unpoisoning of the loaded
6116 // reference will be carried out by the runtime within the slow
6117 // path.
6118 //
6119 // Note that `ref` currently does not get unpoisoned (when heap
6120 // poisoning is enabled), which is alright as the `ref` argument is
6121 // not used by the artReadBarrierSlow entry point.
6122 //
6123 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6124 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6125 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6126 AddSlowPath(slow_path);
6127
Roland Levillain3b359c72015-11-17 19:35:12 +00006128 __ b(slow_path->GetEntryLabel());
6129 __ Bind(slow_path->GetExitLabel());
6130}
6131
Roland Levillainc9285912015-12-18 10:38:42 +00006132void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6133 Location out,
6134 Location ref,
6135 Location obj,
6136 uint32_t offset,
6137 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006138 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006139 // Baker's read barriers shall be handled by the fast path
6140 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6141 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006142 // If heap poisoning is enabled, unpoisoning will be taken care of
6143 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006144 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006145 } else if (kPoisonHeapReferences) {
6146 __ UnpoisonHeapReference(out.AsRegister<Register>());
6147 }
6148}
6149
Roland Levillainc9285912015-12-18 10:38:42 +00006150void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6151 Location out,
6152 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006153 DCHECK(kEmitCompilerReadBarrier);
6154
Roland Levillainc9285912015-12-18 10:38:42 +00006155 // Insert a slow path based read barrier *after* the GC root load.
6156 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006157 // Note that GC roots are not affected by heap poisoning, so we do
6158 // not need to do anything special for this here.
6159 SlowPathCode* slow_path =
6160 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6161 AddSlowPath(slow_path);
6162
Roland Levillain3b359c72015-11-17 19:35:12 +00006163 __ b(slow_path->GetEntryLabel());
6164 __ Bind(slow_path->GetExitLabel());
6165}
6166
Vladimir Markodc151b22015-10-15 18:02:30 +01006167HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6168 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6169 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006170 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6171 // We disable pc-relative load when there is an irreducible loop, as the optimization
6172 // is incompatible with it.
6173 if (GetGraph()->HasIrreducibleLoops() &&
6174 (dispatch_info.method_load_kind ==
6175 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6176 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6177 }
6178
6179 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006180 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6181 if (&outer_dex_file != target_method.dex_file) {
6182 // Calls across dex files are more likely to exceed the available BL range,
6183 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6184 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6185 (desired_dispatch_info.method_load_kind ==
6186 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6187 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6188 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6189 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006190 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006191 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006192 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006193 0u
6194 };
6195 }
6196 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006197 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006198}
6199
Vladimir Markob4536b72015-11-24 13:45:23 +00006200Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6201 Register temp) {
6202 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6203 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6204 if (!invoke->GetLocations()->Intrinsified()) {
6205 return location.AsRegister<Register>();
6206 }
6207 // For intrinsics we allow any location, so it may be on the stack.
6208 if (!location.IsRegister()) {
6209 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6210 return temp;
6211 }
6212 // For register locations, check if the register was saved. If so, get it from the stack.
6213 // Note: There is a chance that the register was saved but not overwritten, so we could
6214 // save one load. However, since this is just an intrinsic slow path we prefer this
6215 // simple and more robust approach rather that trying to determine if that's the case.
6216 SlowPathCode* slow_path = GetCurrentSlowPath();
6217 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6218 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6219 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6220 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6221 return temp;
6222 }
6223 return location.AsRegister<Register>();
6224}
6225
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006226void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006227 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006228 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006229 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6230 // LR = code address from literal pool with link-time patch.
6231 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006232 break;
6233 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6234 // LR = invoke->GetDirectCodePtr();
6235 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006236 break;
6237 default:
6238 break;
6239 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006240
Vladimir Marko58155012015-08-19 12:49:41 +00006241 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6242 switch (invoke->GetMethodLoadKind()) {
6243 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6244 // temp = thread->string_init_entrypoint
6245 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6246 break;
6247 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006248 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006249 break;
6250 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6251 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6252 break;
6253 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6254 __ LoadLiteral(temp.AsRegister<Register>(),
6255 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6256 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006257 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6258 HArmDexCacheArraysBase* base =
6259 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6260 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6261 temp.AsRegister<Register>());
6262 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6263 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6264 break;
6265 }
Vladimir Marko58155012015-08-19 12:49:41 +00006266 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006267 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006268 Register method_reg;
6269 Register reg = temp.AsRegister<Register>();
6270 if (current_method.IsRegister()) {
6271 method_reg = current_method.AsRegister<Register>();
6272 } else {
6273 DCHECK(invoke->GetLocations()->Intrinsified());
6274 DCHECK(!current_method.IsValid());
6275 method_reg = reg;
6276 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6277 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006278 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6279 __ LoadFromOffset(kLoadWord,
6280 reg,
6281 method_reg,
6282 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006283 // temp = temp[index_in_cache]
6284 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6285 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6286 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006287 }
Vladimir Marko58155012015-08-19 12:49:41 +00006288 }
6289
6290 switch (invoke->GetCodePtrLocation()) {
6291 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6292 __ bl(GetFrameEntryLabel());
6293 break;
6294 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006295 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006296 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006297 // Arbitrarily branch to the BL itself, override at link time.
6298 __ bl(&relative_call_patches_.back().label);
6299 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006300 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6301 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6302 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006303 // LR()
6304 __ blx(LR);
6305 break;
6306 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6307 // LR = callee_method->entry_point_from_quick_compiled_code_
6308 __ LoadFromOffset(
6309 kLoadWord, LR, callee_method.AsRegister<Register>(),
6310 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6311 // LR()
6312 __ blx(LR);
6313 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006314 }
6315
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006316 DCHECK(!IsLeafMethod());
6317}
6318
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006319void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6320 Register temp = temp_location.AsRegister<Register>();
6321 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6322 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006323
6324 // Use the calling convention instead of the location of the receiver, as
6325 // intrinsics may have put the receiver in a different register. In the intrinsics
6326 // slow path, the arguments have been moved to the right place, so here we are
6327 // guaranteed that the receiver is the first register of the calling convention.
6328 InvokeDexCallingConvention calling_convention;
6329 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006330 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006331 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006332 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006333 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006334 // Instead of simply (possibly) unpoisoning `temp` here, we should
6335 // emit a read barrier for the previous class reference load.
6336 // However this is not required in practice, as this is an
6337 // intermediate/temporary reference and because the current
6338 // concurrent copying collector keeps the from-space memory
6339 // intact/accessible until the end of the marking phase (the
6340 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006341 __ MaybeUnpoisonHeapReference(temp);
6342 // temp = temp->GetMethodAt(method_offset);
6343 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6344 kArmWordSize).Int32Value();
6345 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6346 // LR = temp->GetEntryPoint();
6347 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6348 // LR();
6349 __ blx(LR);
6350}
6351
Vladimir Marko58155012015-08-19 12:49:41 +00006352void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6353 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006354 size_t size =
6355 method_patches_.size() +
6356 call_patches_.size() +
6357 relative_call_patches_.size() +
6358 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006359 linker_patches->reserve(size);
6360 for (const auto& entry : method_patches_) {
6361 const MethodReference& target_method = entry.first;
6362 Literal* literal = entry.second;
6363 DCHECK(literal->GetLabel()->IsBound());
6364 uint32_t literal_offset = literal->GetLabel()->Position();
6365 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6366 target_method.dex_file,
6367 target_method.dex_method_index));
6368 }
6369 for (const auto& entry : call_patches_) {
6370 const MethodReference& target_method = entry.first;
6371 Literal* literal = entry.second;
6372 DCHECK(literal->GetLabel()->IsBound());
6373 uint32_t literal_offset = literal->GetLabel()->Position();
6374 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6375 target_method.dex_file,
6376 target_method.dex_method_index));
6377 }
6378 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6379 uint32_t literal_offset = info.label.Position();
6380 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6381 info.target_method.dex_file,
6382 info.target_method.dex_method_index));
6383 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006384 for (const auto& pair : dex_cache_arrays_base_labels_) {
6385 HArmDexCacheArraysBase* base = pair.first;
6386 const DexCacheArraysBaseLabels* labels = &pair.second;
6387 const DexFile& dex_file = base->GetDexFile();
6388 size_t base_element_offset = base->GetElementOffset();
6389 DCHECK(labels->add_pc_label.IsBound());
6390 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6391 // Add MOVW patch.
6392 DCHECK(labels->movw_label.IsBound());
6393 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6394 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6395 &dex_file,
6396 add_pc_offset,
6397 base_element_offset));
6398 // Add MOVT patch.
6399 DCHECK(labels->movt_label.IsBound());
6400 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6401 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6402 &dex_file,
6403 add_pc_offset,
6404 base_element_offset));
6405 }
Vladimir Marko58155012015-08-19 12:49:41 +00006406}
6407
6408Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6409 MethodToLiteralMap* map) {
6410 // Look up the literal for target_method.
6411 auto lb = map->lower_bound(target_method);
6412 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6413 return lb->second;
6414 }
6415 // We don't have a literal for this method yet, insert a new one.
6416 Literal* literal = __ NewLiteral<uint32_t>(0u);
6417 map->PutBefore(lb, target_method, literal);
6418 return literal;
6419}
6420
6421Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6422 return DeduplicateMethodLiteral(target_method, &method_patches_);
6423}
6424
6425Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6426 return DeduplicateMethodLiteral(target_method, &call_patches_);
6427}
6428
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006429void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006430 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006431 LOG(FATAL) << "Unreachable";
6432}
6433
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006434void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006435 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006436 LOG(FATAL) << "Unreachable";
6437}
6438
Mark Mendellfe57faa2015-09-18 09:26:15 -04006439// Simple implementation of packed switch - generate cascaded compare/jumps.
6440void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6441 LocationSummary* locations =
6442 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6443 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006444 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006445 codegen_->GetAssembler()->IsThumb()) {
6446 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6447 if (switch_instr->GetStartValue() != 0) {
6448 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6449 }
6450 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006451}
6452
6453void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6454 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006455 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006456 LocationSummary* locations = switch_instr->GetLocations();
6457 Register value_reg = locations->InAt(0).AsRegister<Register>();
6458 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6459
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006460 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006461 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006462 Register temp_reg = IP;
6463 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6464 // the immediate, because IP is used as the destination register. For the other
6465 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6466 // and they can be encoded in the instruction without making use of IP register.
6467 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6468
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006469 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006470 // Jump to successors[0] if value == lower_bound.
6471 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6472 int32_t last_index = 0;
6473 for (; num_entries - last_index > 2; last_index += 2) {
6474 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6475 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6476 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6477 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6478 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6479 }
6480 if (num_entries - last_index == 2) {
6481 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006482 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006483 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006484 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006485
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006486 // And the default for any other value.
6487 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6488 __ b(codegen_->GetLabelOf(default_block));
6489 }
6490 } else {
6491 // Create a table lookup.
6492 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6493
6494 // Materialize a pointer to the switch table
6495 std::vector<Label*> labels(num_entries);
6496 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6497 for (uint32_t i = 0; i < num_entries; i++) {
6498 labels[i] = codegen_->GetLabelOf(successors[i]);
6499 }
6500 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6501
6502 // Remove the bias.
6503 Register key_reg;
6504 if (lower_bound != 0) {
6505 key_reg = locations->GetTemp(1).AsRegister<Register>();
6506 __ AddConstant(key_reg, value_reg, -lower_bound);
6507 } else {
6508 key_reg = value_reg;
6509 }
6510
6511 // Check whether the value is in the table, jump to default block if not.
6512 __ CmpConstant(key_reg, num_entries - 1);
6513 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6514
6515 // Load the displacement from the table.
6516 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6517
6518 // Dispatch is a direct add to the PC (for Thumb2).
6519 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006520 }
6521}
6522
Vladimir Markob4536b72015-11-24 13:45:23 +00006523void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6525 locations->SetOut(Location::RequiresRegister());
6526 codegen_->AddDexCacheArraysBase(base);
6527}
6528
6529void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6530 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6531 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6532 __ BindTrackedLabel(&labels->movw_label);
6533 __ movw(base_reg, 0u);
6534 __ BindTrackedLabel(&labels->movt_label);
6535 __ movt(base_reg, 0u);
6536 __ BindTrackedLabel(&labels->add_pc_label);
6537 __ add(base_reg, base_reg, ShifterOperand(PC));
6538}
6539
Andreas Gampe85b62f22015-09-09 13:15:38 -07006540void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6541 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006542 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006543 return;
6544 }
6545
6546 DCHECK_NE(type, Primitive::kPrimVoid);
6547
6548 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6549 if (return_loc.Equals(trg)) {
6550 return;
6551 }
6552
6553 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6554 // with the last branch.
6555 if (type == Primitive::kPrimLong) {
6556 HParallelMove parallel_move(GetGraph()->GetArena());
6557 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6558 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6559 GetMoveResolver()->EmitNativeCode(&parallel_move);
6560 } else if (type == Primitive::kPrimDouble) {
6561 HParallelMove parallel_move(GetGraph()->GetArena());
6562 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6563 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6564 GetMoveResolver()->EmitNativeCode(&parallel_move);
6565 } else {
6566 // Let the parallel move resolver take care of all of this.
6567 HParallelMove parallel_move(GetGraph()->GetArena());
6568 parallel_move.AddMove(return_loc, trg, type, nullptr);
6569 GetMoveResolver()->EmitNativeCode(&parallel_move);
6570 }
6571}
6572
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006573void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
6574 LocationSummary* locations =
6575 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6576 locations->SetInAt(0, Location::RequiresRegister());
6577 locations->SetOut(Location::RequiresRegister());
6578}
6579
6580void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
6581 LocationSummary* locations = instruction->GetLocations();
6582 uint32_t method_offset = 0;
6583 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
6584 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6585 instruction->GetIndex(), kArmPointerSize).SizeValue();
6586 } else {
6587 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
6588 instruction->GetIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
6589 }
6590 __ LoadFromOffset(kLoadWord,
6591 locations->Out().AsRegister<Register>(),
6592 locations->InAt(0).AsRegister<Register>(),
6593 method_offset);
6594}
6595
Roland Levillain4d027112015-07-01 15:41:14 +01006596#undef __
6597#undef QUICK_ENTRY_POINT
6598
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006599} // namespace arm
6600} // namespace art