blob: e43493280a95783cc116a83d0ed26cd2bec0270f [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain62a46b22015-06-01 18:24:13 +010062#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())->
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010063#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064
Andreas Gampe85b62f22015-09-09 13:15:38 -070065class NullCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010067 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068
Alexandre Rames67555f72014-11-18 10:55:16 +000069 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010070 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000072 if (instruction_->CanThrowIntoCatchBlock()) {
73 // Live registers will be restored in the catch block if caught.
74 SaveLiveRegisters(codegen, instruction_->GetLocations());
75 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010076 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000077 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000078 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 }
80
Alexandre Rames8158f282015-08-07 10:26:17 +010081 bool IsFatal() const OVERRIDE { return true; }
82
Alexandre Rames9931f312015-06-19 14:47:01 +010083 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
84
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010086 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
88};
89
Andreas Gampe85b62f22015-09-09 13:15:38 -070090class DivZeroCheckSlowPathARM : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000091 public:
92 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {}
93
Alexandre Rames67555f72014-11-18 10:55:16 +000094 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000095 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
96 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000097 if (instruction_->CanThrowIntoCatchBlock()) {
98 // Live registers will be restored in the catch block if caught.
99 SaveLiveRegisters(codegen, instruction_->GetLocations());
100 }
Calin Juravled0d48522014-11-04 16:40:20 +0000101 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000102 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000103 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000104 }
105
Alexandre Rames8158f282015-08-07 10:26:17 +0100106 bool IsFatal() const OVERRIDE { return true; }
107
Alexandre Rames9931f312015-06-19 14:47:01 +0100108 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
109
Calin Juravled0d48522014-11-04 16:40:20 +0000110 private:
111 HDivZeroCheck* const instruction_;
112 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
113};
114
Andreas Gampe85b62f22015-09-09 13:15:38 -0700115class SuspendCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000117 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100118 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119
Alexandre Rames67555f72014-11-18 10:55:16 +0000120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100121 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000123 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100124 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000125 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000126 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000127 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 if (successor_ == nullptr) {
129 __ b(GetReturnLabel());
130 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100132 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000133 }
134
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 Label* GetReturnLabel() {
136 DCHECK(successor_ == nullptr);
137 return &return_label_;
138 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100140 HBasicBlock* GetSuccessor() const {
141 return successor_;
142 }
143
Alexandre Rames9931f312015-06-19 14:47:01 +0100144 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
145
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 private:
147 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100148 // If not null, the block to branch to after the suspend check.
149 HBasicBlock* const successor_;
150
151 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000152 Label return_label_;
153
154 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
155};
156
Andreas Gampe85b62f22015-09-09 13:15:38 -0700157class BoundsCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100159 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
160 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161
Alexandre Rames67555f72014-11-18 10:55:16 +0000162 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100164 LocationSummary* locations = instruction_->GetLocations();
165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000167 if (instruction_->CanThrowIntoCatchBlock()) {
168 // Live registers will be restored in the catch block if caught.
169 SaveLiveRegisters(codegen, instruction_->GetLocations());
170 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000171 // We're moving two locations to locations that could overlap, so we need a parallel
172 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000174 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100175 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000176 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100177 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100179 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
180 Primitive::kPrimInt);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100181 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000182 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000183 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100184 }
185
Alexandre Rames8158f282015-08-07 10:26:17 +0100186 bool IsFatal() const OVERRIDE { return true; }
187
Alexandre Rames9931f312015-06-19 14:47:01 +0100188 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
189
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100191 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192
193 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
194};
195
Andreas Gampe85b62f22015-09-09 13:15:38 -0700196class LoadClassSlowPathARM : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100197 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000198 LoadClassSlowPathARM(HLoadClass* cls,
199 HInstruction* at,
200 uint32_t dex_pc,
201 bool do_clinit)
202 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
203 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
204 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205
Alexandre Rames67555f72014-11-18 10:55:16 +0000206 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 LocationSummary* locations = at_->GetLocations();
208
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
210 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000211 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000214 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 int32_t entry_point_offset = do_clinit_
216 ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
217 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000218 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000219 if (do_clinit_) {
220 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
221 } else {
222 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
223 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224
225 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000226 Location out = locations->Out();
227 if (out.IsValid()) {
228 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
230 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232 __ b(GetExitLabel());
233 }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
236
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100237 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 // The class this slow path will load.
239 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 // The instruction where this slow path is happening.
242 // (Might be the load class or an initialization check).
243 HInstruction* const at_;
244
245 // The dex PC of `at_`.
246 const uint32_t dex_pc_;
247
248 // Whether to initialize the class.
249 const bool do_clinit_;
250
251 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252};
253
Andreas Gampe85b62f22015-09-09 13:15:38 -0700254class LoadStringSlowPathARM : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255 public:
256 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {}
257
Alexandre Rames67555f72014-11-18 10:55:16 +0000258 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000259 LocationSummary* locations = instruction_->GetLocations();
260 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
261
262 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
263 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000264 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000265
266 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800267 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000268 arm_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000269 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000270 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000271 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
272
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274 __ b(GetExitLabel());
275 }
276
Alexandre Rames9931f312015-06-19 14:47:01 +0100277 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
278
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000279 private:
280 HLoadString* const instruction_;
281
282 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
283};
284
Andreas Gampe85b62f22015-09-09 13:15:38 -0700285class TypeCheckSlowPathARM : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000287 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
288 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100292 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
293 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 DCHECK(instruction_->IsCheckCast()
295 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
298 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000300 if (!is_fatal_) {
301 SaveLiveRegisters(codegen, locations);
302 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
304 // We're moving two locations to locations that could overlap, so we need a parallel
305 // move resolver.
306 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100308 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100310 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100311 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100312 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
313 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 if (instruction_->IsInstanceOf()) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100316 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
317 instruction_,
318 instruction_->GetDexPc(),
319 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000320 CheckEntrypointTypes<
321 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
323 } else {
324 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
326 instruction_,
327 instruction_->GetDexPc(),
328 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000329 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 if (!is_fatal_) {
333 RestoreLiveRegisters(codegen, locations);
334 __ b(GetExitLabel());
335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 }
337
Alexandre Rames9931f312015-06-19 14:47:01 +0100338 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340 bool IsFatal() const OVERRIDE { return is_fatal_; }
341
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
347};
348
Andreas Gampe85b62f22015-09-09 13:15:38 -0700349class DeoptimizationSlowPathARM : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700350 public:
Aart Bik42249c32016-01-07 15:33:50 -0800351 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700352 : instruction_(instruction) {}
353
354 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800355 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700356 __ Bind(GetEntryLabel());
357 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800358 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
359 instruction_,
360 instruction_->GetDexPc(),
361 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000362 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700363 }
364
Alexandre Rames9931f312015-06-19 14:47:01 +0100365 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
366
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 private:
Aart Bik42249c32016-01-07 15:33:50 -0800368 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
370};
371
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100372class ArraySetSlowPathARM : public SlowPathCode {
373 public:
374 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {}
375
376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
377 LocationSummary* locations = instruction_->GetLocations();
378 __ Bind(GetEntryLabel());
379 SaveLiveRegisters(codegen, locations);
380
381 InvokeRuntimeCallingConvention calling_convention;
382 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
383 parallel_move.AddMove(
384 locations->InAt(0),
385 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
386 Primitive::kPrimNot,
387 nullptr);
388 parallel_move.AddMove(
389 locations->InAt(1),
390 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
391 Primitive::kPrimInt,
392 nullptr);
393 parallel_move.AddMove(
394 locations->InAt(2),
395 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
396 Primitive::kPrimNot,
397 nullptr);
398 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
399
400 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
401 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
402 instruction_,
403 instruction_->GetDexPc(),
404 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000405 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406 RestoreLiveRegisters(codegen, locations);
407 __ b(GetExitLabel());
408 }
409
410 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
411
412 private:
413 HInstruction* const instruction_;
414
415 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
416};
417
Roland Levillainc9285912015-12-18 10:38:42 +0000418// Slow path marking an object during a read barrier.
419class ReadBarrierMarkSlowPathARM : public SlowPathCode {
420 public:
421 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location out, Location obj)
422 : instruction_(instruction), out_(out), obj_(obj) {
423 DCHECK(kEmitCompilerReadBarrier);
424 }
425
426 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
427
428 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
429 LocationSummary* locations = instruction_->GetLocations();
430 Register reg_out = out_.AsRegister<Register>();
431 DCHECK(locations->CanCall());
432 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
433 DCHECK(instruction_->IsInstanceFieldGet() ||
434 instruction_->IsStaticFieldGet() ||
435 instruction_->IsArrayGet() ||
436 instruction_->IsLoadClass() ||
437 instruction_->IsLoadString() ||
438 instruction_->IsInstanceOf() ||
439 instruction_->IsCheckCast())
440 << "Unexpected instruction in read barrier marking slow path: "
441 << instruction_->DebugName();
442
443 __ Bind(GetEntryLabel());
444 SaveLiveRegisters(codegen, locations);
445
446 InvokeRuntimeCallingConvention calling_convention;
447 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
448 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
449 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
450 instruction_,
451 instruction_->GetDexPc(),
452 this);
453 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
454 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
455
456 RestoreLiveRegisters(codegen, locations);
457 __ b(GetExitLabel());
458 }
459
460 private:
461 HInstruction* const instruction_;
462 const Location out_;
463 const Location obj_;
464
465 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
466};
467
Roland Levillain3b359c72015-11-17 19:35:12 +0000468// Slow path generating a read barrier for a heap reference.
469class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCode {
470 public:
471 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
472 Location out,
473 Location ref,
474 Location obj,
475 uint32_t offset,
476 Location index)
477 : instruction_(instruction),
478 out_(out),
479 ref_(ref),
480 obj_(obj),
481 offset_(offset),
482 index_(index) {
483 DCHECK(kEmitCompilerReadBarrier);
484 // If `obj` is equal to `out` or `ref`, it means the initial object
485 // has been overwritten by (or after) the heap object reference load
486 // to be instrumented, e.g.:
487 //
488 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000489 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000490 //
491 // In that case, we have lost the information about the original
492 // object, and the emitted read barrier cannot work properly.
493 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
494 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
495 }
496
497 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
498 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
499 LocationSummary* locations = instruction_->GetLocations();
500 Register reg_out = out_.AsRegister<Register>();
501 DCHECK(locations->CanCall());
502 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
503 DCHECK(!instruction_->IsInvoke() ||
504 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillainc9285912015-12-18 10:38:42 +0000505 instruction_->GetLocations()->Intrinsified()))
506 << "Unexpected instruction in read barrier for heap reference slow path: "
507 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000508
509 __ Bind(GetEntryLabel());
510 SaveLiveRegisters(codegen, locations);
511
512 // We may have to change the index's value, but as `index_` is a
513 // constant member (like other "inputs" of this slow path),
514 // introduce a copy of it, `index`.
515 Location index = index_;
516 if (index_.IsValid()) {
517 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
518 if (instruction_->IsArrayGet()) {
519 // Compute the actual memory offset and store it in `index`.
520 Register index_reg = index_.AsRegister<Register>();
521 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
522 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
523 // We are about to change the value of `index_reg` (see the
524 // calls to art::arm::Thumb2Assembler::Lsl and
525 // art::arm::Thumb2Assembler::AddConstant below), but it has
526 // not been saved by the previous call to
527 // art::SlowPathCode::SaveLiveRegisters, as it is a
528 // callee-save register --
529 // art::SlowPathCode::SaveLiveRegisters does not consider
530 // callee-save registers, as it has been designed with the
531 // assumption that callee-save registers are supposed to be
532 // handled by the called function. So, as a callee-save
533 // register, `index_reg` _would_ eventually be saved onto
534 // the stack, but it would be too late: we would have
535 // changed its value earlier. Therefore, we manually save
536 // it here into another freely available register,
537 // `free_reg`, chosen of course among the caller-save
538 // registers (as a callee-save `free_reg` register would
539 // exhibit the same problem).
540 //
541 // Note we could have requested a temporary register from
542 // the register allocator instead; but we prefer not to, as
543 // this is a slow path, and we know we can find a
544 // caller-save register that is available.
545 Register free_reg = FindAvailableCallerSaveRegister(codegen);
546 __ Mov(free_reg, index_reg);
547 index_reg = free_reg;
548 index = Location::RegisterLocation(index_reg);
549 } else {
550 // The initial register stored in `index_` has already been
551 // saved in the call to art::SlowPathCode::SaveLiveRegisters
552 // (as it is not a callee-save register), so we can freely
553 // use it.
554 }
555 // Shifting the index value contained in `index_reg` by the scale
556 // factor (2) cannot overflow in practice, as the runtime is
557 // unable to allocate object arrays with a size larger than
558 // 2^26 - 1 (that is, 2^28 - 4 bytes).
559 __ Lsl(index_reg, index_reg, TIMES_4);
560 static_assert(
561 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
562 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
563 __ AddConstant(index_reg, index_reg, offset_);
564 } else {
565 DCHECK(instruction_->IsInvoke());
566 DCHECK(instruction_->GetLocations()->Intrinsified());
567 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
568 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
569 << instruction_->AsInvoke()->GetIntrinsic();
570 DCHECK_EQ(offset_, 0U);
571 DCHECK(index_.IsRegisterPair());
572 // UnsafeGet's offset location is a register pair, the low
573 // part contains the correct offset.
574 index = index_.ToLow();
575 }
576 }
577
578 // We're moving two or three locations to locations that could
579 // overlap, so we need a parallel move resolver.
580 InvokeRuntimeCallingConvention calling_convention;
581 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
582 parallel_move.AddMove(ref_,
583 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
584 Primitive::kPrimNot,
585 nullptr);
586 parallel_move.AddMove(obj_,
587 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
588 Primitive::kPrimNot,
589 nullptr);
590 if (index.IsValid()) {
591 parallel_move.AddMove(index,
592 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
593 Primitive::kPrimInt,
594 nullptr);
595 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
596 } else {
597 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
598 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
599 }
600 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
601 instruction_,
602 instruction_->GetDexPc(),
603 this);
604 CheckEntrypointTypes<
605 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
606 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
607
608 RestoreLiveRegisters(codegen, locations);
609 __ b(GetExitLabel());
610 }
611
612 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
613
614 private:
615 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
616 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
617 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
618 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
619 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
620 return static_cast<Register>(i);
621 }
622 }
623 // We shall never fail to find a free caller-save register, as
624 // there are more than two core caller-save registers on ARM
625 // (meaning it is possible to find one which is different from
626 // `ref` and `obj`).
627 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
628 LOG(FATAL) << "Could not find a free caller-save register";
629 UNREACHABLE();
630 }
631
632 HInstruction* const instruction_;
633 const Location out_;
634 const Location ref_;
635 const Location obj_;
636 const uint32_t offset_;
637 // An additional location containing an index to an array.
638 // Only used for HArrayGet and the UnsafeGetObject &
639 // UnsafeGetObjectVolatile intrinsics.
640 const Location index_;
641
642 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
643};
644
645// Slow path generating a read barrier for a GC root.
646class ReadBarrierForRootSlowPathARM : public SlowPathCode {
647 public:
648 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Roland Levillainc9285912015-12-18 10:38:42 +0000649 : instruction_(instruction), out_(out), root_(root) {
650 DCHECK(kEmitCompilerReadBarrier);
651 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000652
653 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
654 LocationSummary* locations = instruction_->GetLocations();
655 Register reg_out = out_.AsRegister<Register>();
656 DCHECK(locations->CanCall());
657 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000658 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
659 << "Unexpected instruction in read barrier for GC root slow path: "
660 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000661
662 __ Bind(GetEntryLabel());
663 SaveLiveRegisters(codegen, locations);
664
665 InvokeRuntimeCallingConvention calling_convention;
666 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
667 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
668 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
669 instruction_,
670 instruction_->GetDexPc(),
671 this);
672 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
673 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
674
675 RestoreLiveRegisters(codegen, locations);
676 __ b(GetExitLabel());
677 }
678
679 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
680
681 private:
682 HInstruction* const instruction_;
683 const Location out_;
684 const Location root_;
685
686 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
687};
688
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000689#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100690#define __ down_cast<ArmAssembler*>(GetAssembler())->
Dave Allison20dfc792014-06-16 20:44:29 -0700691
Aart Bike9f37602015-10-09 11:15:55 -0700692inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700693 switch (cond) {
694 case kCondEQ: return EQ;
695 case kCondNE: return NE;
696 case kCondLT: return LT;
697 case kCondLE: return LE;
698 case kCondGT: return GT;
699 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700700 case kCondB: return LO;
701 case kCondBE: return LS;
702 case kCondA: return HI;
703 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700704 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100705 LOG(FATAL) << "Unreachable";
706 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700707}
708
Aart Bike9f37602015-10-09 11:15:55 -0700709// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100710inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700711 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100712 case kCondEQ: return EQ;
713 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700714 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100715 case kCondLT: return LO;
716 case kCondLE: return LS;
717 case kCondGT: return HI;
718 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700719 // Unsigned remain unchanged.
720 case kCondB: return LO;
721 case kCondBE: return LS;
722 case kCondA: return HI;
723 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700724 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100725 LOG(FATAL) << "Unreachable";
726 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700727}
728
Vladimir Markod6e069b2016-01-18 11:11:01 +0000729inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
730 // The ARM condition codes can express all the necessary branches, see the
731 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
732 // There is no dex instruction or HIR that would need the missing conditions
733 // "equal or unordered" or "not equal".
734 switch (cond) {
735 case kCondEQ: return EQ;
736 case kCondNE: return NE /* unordered */;
737 case kCondLT: return gt_bias ? CC : LT /* unordered */;
738 case kCondLE: return gt_bias ? LS : LE /* unordered */;
739 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
740 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
741 default:
742 LOG(FATAL) << "UNREACHABLE";
743 UNREACHABLE();
744 }
745}
746
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100747void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100748 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749}
750
751void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100752 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100753}
754
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100755size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
756 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
757 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100758}
759
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100760size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
761 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
762 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100763}
764
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000765size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
766 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
767 return kArmWordSize;
768}
769
770size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
771 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
772 return kArmWordSize;
773}
774
Calin Juravle34166012014-12-19 17:22:29 +0000775CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000776 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100777 const CompilerOptions& compiler_options,
778 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000779 : CodeGenerator(graph,
780 kNumberOfCoreRegisters,
781 kNumberOfSRegisters,
782 kNumberOfRegisterPairs,
783 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
784 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000785 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
786 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100787 compiler_options,
788 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100789 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100790 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100791 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100792 move_resolver_(graph->GetArena(), this),
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000793 assembler_(),
Vladimir Marko58155012015-08-19 12:49:41 +0000794 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100795 method_patches_(MethodReferenceComparator(),
796 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
797 call_patches_(MethodReferenceComparator(),
798 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000799 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
800 dex_cache_arrays_base_labels_(std::less<HArmDexCacheArraysBase*>(),
801 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700802 // Always save the LR register to mimic Quick.
803 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100804}
805
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000806void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
807 // Ensure that we fix up branches and literal loads and emit the literal pool.
808 __ FinalizeCode();
809
810 // Adjust native pc offsets in stack maps.
811 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
812 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
813 uint32_t new_position = __ GetAdjustedPosition(old_position);
814 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
815 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100816 // Adjust pc offsets for the disassembly information.
817 if (disasm_info_ != nullptr) {
818 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
819 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
820 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
821 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
822 it.second.start = __ GetAdjustedPosition(it.second.start);
823 it.second.end = __ GetAdjustedPosition(it.second.end);
824 }
825 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
826 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
827 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
828 }
829 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000830
831 CodeGenerator::Finalize(allocator);
832}
833
David Brazdil58282f42016-01-14 12:45:10 +0000834void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100835 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100836 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100837
838 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100839 blocked_core_registers_[SP] = true;
840 blocked_core_registers_[LR] = true;
841 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100842
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100844 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100845
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100846 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100847 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100848
David Brazdil58282f42016-01-14 12:45:10 +0000849 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +0100850 // Stubs do not save callee-save floating point registers. If the graph
851 // is debuggable, we need to deal with these registers differently. For
852 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000853 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
854 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
855 }
856 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100857
858 UpdateBlockedPairRegisters();
859}
860
861void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
862 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
863 ArmManagedRegister current =
864 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
865 if (blocked_core_registers_[current.AsRegisterPairLow()]
866 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
867 blocked_register_pairs_[i] = true;
868 }
869 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870}
871
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100872InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800873 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100874 assembler_(codegen->GetAssembler()),
875 codegen_(codegen) {}
876
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000877void CodeGeneratorARM::ComputeSpillMask() {
878 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
879 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +0000880 // There is no easy instruction to restore just the PC on thumb2. We spill and
881 // restore another arbitrary register.
882 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000883 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
884 // We use vpush and vpop for saving and restoring floating point registers, which take
885 // a SRegister and the number of registers to save/restore after that SRegister. We
886 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
887 // but in the range.
888 if (fpu_spill_mask_ != 0) {
889 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
890 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
891 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
892 fpu_spill_mask_ |= (1 << i);
893 }
894 }
895}
896
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100897static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100898 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100899}
900
901static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100902 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100903}
904
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000905void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000906 bool skip_overflow_check =
907 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000908 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000909 __ Bind(&frame_entry_label_);
910
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000911 if (HasEmptyFrame()) {
912 return;
913 }
914
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100915 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000916 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
917 __ LoadFromOffset(kLoadWord, IP, IP, 0);
918 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100919 }
920
Andreas Gampe501fd632015-09-10 16:11:06 -0700921 __ PushList(core_spill_mask_);
922 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
923 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000924 if (fpu_spill_mask_ != 0) {
925 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
926 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100927 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +0100928 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000929 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100930 int adjust = GetFrameSize() - FrameEntrySpillSize();
931 __ AddConstant(SP, -adjust);
932 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100933 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000934}
935
936void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000937 if (HasEmptyFrame()) {
938 __ bx(LR);
939 return;
940 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100941 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100942 int adjust = GetFrameSize() - FrameEntrySpillSize();
943 __ AddConstant(SP, adjust);
944 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000945 if (fpu_spill_mask_ != 0) {
946 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
947 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100948 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_));
949 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000950 }
Andreas Gampe501fd632015-09-10 16:11:06 -0700951 // Pop LR into PC to return.
952 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
953 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
954 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +0100955 __ cfi().RestoreState();
956 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957}
958
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100959void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -0700960 Label* label = GetLabelOf(block);
961 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000962}
963
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100964Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const {
965 switch (load->GetType()) {
966 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100968 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100969
970 case Primitive::kPrimInt:
971 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100972 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100974
975 case Primitive::kPrimBoolean:
976 case Primitive::kPrimByte:
977 case Primitive::kPrimChar:
978 case Primitive::kPrimShort:
979 case Primitive::kPrimVoid:
980 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700981 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100982 }
983
984 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700985 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100986}
987
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100988Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100989 switch (type) {
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimInt:
995 case Primitive::kPrimNot: {
996 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000997 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100999 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001000 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001001 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001002 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001003 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001005 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001006 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001007 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001009 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001011 if (calling_convention.GetRegisterAt(index) == R1) {
1012 // Skip R1, and use R2_R3 instead.
1013 gp_index_++;
1014 index++;
1015 }
1016 }
1017 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1018 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001019 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001020
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001021 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001022 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001023 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001024 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1025 }
1026 }
1027
1028 case Primitive::kPrimFloat: {
1029 uint32_t stack_index = stack_index_++;
1030 if (float_index_ % 2 == 0) {
1031 float_index_ = std::max(double_index_, float_index_);
1032 }
1033 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1034 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1035 } else {
1036 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1037 }
1038 }
1039
1040 case Primitive::kPrimDouble: {
1041 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1042 uint32_t stack_index = stack_index_;
1043 stack_index_ += 2;
1044 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1045 uint32_t index = double_index_;
1046 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001047 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001048 calling_convention.GetFpuRegisterAt(index),
1049 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001050 DCHECK(ExpectedPairLayout(result));
1051 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001052 } else {
1053 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001054 }
1055 }
1056
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001057 case Primitive::kPrimVoid:
1058 LOG(FATAL) << "Unexpected parameter type " << type;
1059 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001060 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001061 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001062}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001063
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001064Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001065 switch (type) {
1066 case Primitive::kPrimBoolean:
1067 case Primitive::kPrimByte:
1068 case Primitive::kPrimChar:
1069 case Primitive::kPrimShort:
1070 case Primitive::kPrimInt:
1071 case Primitive::kPrimNot: {
1072 return Location::RegisterLocation(R0);
1073 }
1074
1075 case Primitive::kPrimFloat: {
1076 return Location::FpuRegisterLocation(S0);
1077 }
1078
1079 case Primitive::kPrimLong: {
1080 return Location::RegisterPairLocation(R0, R1);
1081 }
1082
1083 case Primitive::kPrimDouble: {
1084 return Location::FpuRegisterPairLocation(S0, S1);
1085 }
1086
1087 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001088 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001089 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001090
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001091 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001092}
1093
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001094Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1095 return Location::RegisterLocation(kMethodRegisterArgument);
1096}
1097
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098void CodeGeneratorARM::Move32(Location destination, Location source) {
1099 if (source.Equals(destination)) {
1100 return;
1101 }
1102 if (destination.IsRegister()) {
1103 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001106 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001108 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 } else if (destination.IsFpuRegister()) {
1111 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001112 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001114 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001115 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001116 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001117 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001119 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001121 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001123 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001125 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001126 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1127 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 }
1129 }
1130}
1131
1132void CodeGeneratorARM::Move64(Location destination, Location source) {
1133 if (source.Equals(destination)) {
1134 return;
1135 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001136 if (destination.IsRegisterPair()) {
1137 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001138 EmitParallelMoves(
1139 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1140 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001141 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001142 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001143 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1144 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001145 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001146 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001147 } else if (source.IsFpuRegisterPair()) {
1148 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1149 destination.AsRegisterPairHigh<Register>(),
1150 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
1152 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001153 DCHECK(ExpectedPairLayout(destination));
1154 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1155 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001157 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001159 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1160 SP,
1161 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001162 } else if (source.IsRegisterPair()) {
1163 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1164 source.AsRegisterPairLow<Register>(),
1165 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001166 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001167 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 } else {
1170 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001172 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 if (source.AsRegisterPairLow<Register>() == R1) {
1174 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001175 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1176 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 SP, destination.GetStackIndex());
1180 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001181 } else if (source.IsFpuRegisterPair()) {
1182 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1183 SP,
1184 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 } else {
1186 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001187 EmitParallelMoves(
1188 Location::StackSlot(source.GetStackIndex()),
1189 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001190 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001191 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001192 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1193 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194 }
1195 }
1196}
1197
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001198void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001199 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001200 if (instruction->IsCurrentMethod()) {
1201 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1202 } else if (locations != nullptr && locations->Out().Equals(location)) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001203 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001204 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001205 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001206 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1207 int32_t value = GetInt32ValueOf(const_to_move);
Calin Juravlea21f5982014-11-13 15:53:04 +00001208 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 __ LoadImmediate(location.AsRegister<Register>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001210 } else {
1211 DCHECK(location.IsStackSlot());
1212 __ LoadImmediate(IP, value);
1213 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1214 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001215 } else {
Nicolas Geoffray3747b482015-01-19 17:17:16 +00001216 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName();
Calin Juravlea21f5982014-11-13 15:53:04 +00001217 int64_t value = const_to_move->AsLongConstant()->GetValue();
1218 if (location.IsRegisterPair()) {
1219 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value));
1220 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value));
1221 } else {
1222 DCHECK(location.IsDoubleStackSlot());
1223 __ LoadImmediate(IP, Low32Bits(value));
1224 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex());
1225 __ LoadImmediate(IP, High32Bits(value));
1226 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize));
1227 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 }
Roland Levillain476df552014-10-09 17:51:36 +01001229 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
1231 switch (instruction->GetType()) {
1232 case Primitive::kPrimBoolean:
1233 case Primitive::kPrimByte:
1234 case Primitive::kPrimChar:
1235 case Primitive::kPrimShort:
1236 case Primitive::kPrimInt:
1237 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 Move32(location, Location::StackSlot(stack_slot));
1240 break;
1241
1242 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 Move64(location, Location::DoubleStackSlot(stack_slot));
1245 break;
1246
1247 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001250 } else if (instruction->IsTemporary()) {
1251 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001252 if (temp_location.IsStackSlot()) {
1253 Move32(location, temp_location);
1254 } else {
1255 DCHECK(temp_location.IsDoubleStackSlot());
1256 Move64(location, temp_location);
1257 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001258 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001259 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 switch (instruction->GetType()) {
1261 case Primitive::kPrimBoolean:
1262 case Primitive::kPrimByte:
1263 case Primitive::kPrimChar:
1264 case Primitive::kPrimShort:
1265 case Primitive::kPrimNot:
1266 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001267 case Primitive::kPrimFloat:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001268 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269 break;
1270
1271 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001272 case Primitive::kPrimDouble:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001273 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 break;
1275
1276 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001279 }
1280}
1281
Calin Juravle175dc732015-08-25 15:42:32 +01001282void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1283 DCHECK(location.IsRegister());
1284 __ LoadImmediate(location.AsRegister<Register>(), value);
1285}
1286
Calin Juravlee460d1d2015-09-29 04:52:17 +01001287void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001288 HParallelMove move(GetGraph()->GetArena());
1289 move.AddMove(src, dst, dst_type, nullptr);
1290 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001291}
1292
1293void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1294 if (location.IsRegister()) {
1295 locations->AddTemp(location);
1296 } else if (location.IsRegisterPair()) {
1297 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1298 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1299 } else {
1300 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1301 }
1302}
1303
Calin Juravle175dc732015-08-25 15:42:32 +01001304void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1305 HInstruction* instruction,
1306 uint32_t dex_pc,
1307 SlowPathCode* slow_path) {
1308 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(),
1309 instruction,
1310 dex_pc,
1311 slow_path);
1312}
1313
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001314void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset,
1315 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001316 uint32_t dex_pc,
1317 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001318 ValidateInvokeRuntime(instruction, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001319 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1320 __ blx(LR);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001321 RecordPcInfo(instruction, dex_pc, slow_path);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001322}
1323
David Brazdilfc6a86a2015-06-26 10:33:45 +00001324void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001325 DCHECK(!successor->IsExitBlock());
1326
1327 HBasicBlock* block = got->GetBlock();
1328 HInstruction* previous = got->GetPrevious();
1329
1330 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001331 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001332 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1333 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1334 return;
1335 }
1336
1337 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1338 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1339 }
1340 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001341 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001342 }
1343}
1344
David Brazdilfc6a86a2015-06-26 10:33:45 +00001345void LocationsBuilderARM::VisitGoto(HGoto* got) {
1346 got->SetLocations(nullptr);
1347}
1348
1349void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1350 HandleGoto(got, got->GetSuccessor());
1351}
1352
1353void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1354 try_boundary->SetLocations(nullptr);
1355}
1356
1357void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1358 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1359 if (!successor->IsExitBlock()) {
1360 HandleGoto(try_boundary, successor);
1361 }
1362}
1363
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001364void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001365 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001366}
1367
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001368void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001369}
1370
Roland Levillain4fa13f62015-07-06 18:11:54 +01001371void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1372 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001373 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001374 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001375 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001376}
1377
1378void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1379 Label* true_label,
1380 Label* false_label) {
1381 LocationSummary* locations = cond->GetLocations();
1382 Location left = locations->InAt(0);
1383 Location right = locations->InAt(1);
1384 IfCondition if_cond = cond->GetCondition();
1385
1386 Register left_high = left.AsRegisterPairHigh<Register>();
1387 Register left_low = left.AsRegisterPairLow<Register>();
1388 IfCondition true_high_cond = if_cond;
1389 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001390 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001391
1392 // Set the conditions for the test, remembering that == needs to be
1393 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001394 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001395 switch (if_cond) {
1396 case kCondEQ:
1397 case kCondNE:
1398 // Nothing to do.
1399 break;
1400 case kCondLT:
1401 false_high_cond = kCondGT;
1402 break;
1403 case kCondLE:
1404 true_high_cond = kCondLT;
1405 break;
1406 case kCondGT:
1407 false_high_cond = kCondLT;
1408 break;
1409 case kCondGE:
1410 true_high_cond = kCondGT;
1411 break;
Aart Bike9f37602015-10-09 11:15:55 -07001412 case kCondB:
1413 false_high_cond = kCondA;
1414 break;
1415 case kCondBE:
1416 true_high_cond = kCondB;
1417 break;
1418 case kCondA:
1419 false_high_cond = kCondB;
1420 break;
1421 case kCondAE:
1422 true_high_cond = kCondA;
1423 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001424 }
1425 if (right.IsConstant()) {
1426 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1427 int32_t val_low = Low32Bits(value);
1428 int32_t val_high = High32Bits(value);
1429
Vladimir Markoac6ac102015-12-17 12:14:00 +00001430 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001431 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001432 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001433 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001434 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001435 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001436 __ b(true_label, ARMCondition(true_high_cond));
1437 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001438 }
1439 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001440 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001441 } else {
1442 Register right_high = right.AsRegisterPairHigh<Register>();
1443 Register right_low = right.AsRegisterPairLow<Register>();
1444
1445 __ cmp(left_high, ShifterOperand(right_high));
1446 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001447 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001448 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001449 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001450 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001451 __ b(true_label, ARMCondition(true_high_cond));
1452 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001453 }
1454 // Must be equal high, so compare the lows.
1455 __ cmp(left_low, ShifterOperand(right_low));
1456 }
1457 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001458 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001459 __ b(true_label, final_condition);
1460}
1461
David Brazdil0debae72015-11-12 18:37:00 +00001462void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1463 Label* true_target_in,
1464 Label* false_target_in) {
1465 // Generated branching requires both targets to be explicit. If either of the
1466 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1467 Label fallthrough_target;
1468 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1469 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1470
Roland Levillain4fa13f62015-07-06 18:11:54 +01001471 LocationSummary* locations = condition->GetLocations();
1472 Location left = locations->InAt(0);
1473 Location right = locations->InAt(1);
1474
Roland Levillain4fa13f62015-07-06 18:11:54 +01001475 Primitive::Type type = condition->InputAt(0)->GetType();
1476 switch (type) {
1477 case Primitive::kPrimLong:
1478 GenerateLongComparesAndJumps(condition, true_target, false_target);
1479 break;
1480 case Primitive::kPrimFloat:
1481 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1482 GenerateFPJumps(condition, true_target, false_target);
1483 break;
1484 case Primitive::kPrimDouble:
1485 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1486 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1487 GenerateFPJumps(condition, true_target, false_target);
1488 break;
1489 default:
1490 LOG(FATAL) << "Unexpected compare type " << type;
1491 }
1492
David Brazdil0debae72015-11-12 18:37:00 +00001493 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001494 __ b(false_target);
1495 }
David Brazdil0debae72015-11-12 18:37:00 +00001496
1497 if (fallthrough_target.IsLinked()) {
1498 __ Bind(&fallthrough_target);
1499 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001500}
1501
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001502void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001503 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001504 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001505 Label* false_target) {
1506 HInstruction* cond = instruction->InputAt(condition_input_index);
1507
1508 if (true_target == nullptr && false_target == nullptr) {
1509 // Nothing to do. The code always falls through.
1510 return;
1511 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001512 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001513 if (cond->AsIntConstant()->IsOne()) {
1514 if (true_target != nullptr) {
1515 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001516 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001517 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001518 DCHECK(cond->AsIntConstant()->IsZero());
1519 if (false_target != nullptr) {
1520 __ b(false_target);
1521 }
1522 }
1523 return;
1524 }
1525
1526 // The following code generates these patterns:
1527 // (1) true_target == nullptr && false_target != nullptr
1528 // - opposite condition true => branch to false_target
1529 // (2) true_target != nullptr && false_target == nullptr
1530 // - condition true => branch to true_target
1531 // (3) true_target != nullptr && false_target != nullptr
1532 // - condition true => branch to true_target
1533 // - branch to false_target
1534 if (IsBooleanValueOrMaterializedCondition(cond)) {
1535 // Condition has been materialized, compare the output to 0.
1536 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1537 DCHECK(cond_val.IsRegister());
1538 if (true_target == nullptr) {
1539 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1540 } else {
1541 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001542 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001543 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001544 // Condition has not been materialized. Use its inputs as the comparison and
1545 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001546 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001547
1548 // If this is a long or FP comparison that has been folded into
1549 // the HCondition, generate the comparison directly.
1550 Primitive::Type type = condition->InputAt(0)->GetType();
1551 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1552 GenerateCompareTestAndBranch(condition, true_target, false_target);
1553 return;
1554 }
1555
1556 LocationSummary* locations = cond->GetLocations();
1557 DCHECK(locations->InAt(0).IsRegister());
1558 Register left = locations->InAt(0).AsRegister<Register>();
1559 Location right = locations->InAt(1);
1560 if (right.IsRegister()) {
1561 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001562 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001563 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001564 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001565 }
1566 if (true_target == nullptr) {
1567 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1568 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001569 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001570 }
Dave Allison20dfc792014-06-16 20:44:29 -07001571 }
David Brazdil0debae72015-11-12 18:37:00 +00001572
1573 // If neither branch falls through (case 3), the conditional branch to `true_target`
1574 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1575 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001576 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001577 }
1578}
1579
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001580void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001581 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1582 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001583 locations->SetInAt(0, Location::RequiresRegister());
1584 }
1585}
1586
1587void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001588 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1589 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1590 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1591 nullptr : codegen_->GetLabelOf(true_successor);
1592 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1593 nullptr : codegen_->GetLabelOf(false_successor);
1594 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001595}
1596
1597void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1598 LocationSummary* locations = new (GetGraph()->GetArena())
1599 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001600 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001601 locations->SetInAt(0, Location::RequiresRegister());
1602 }
1603}
1604
1605void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001606 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001607 GenerateTestAndBranch(deoptimize,
1608 /* condition_input_index */ 0,
1609 slow_path->GetEntryLabel(),
1610 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001611}
Dave Allison20dfc792014-06-16 20:44:29 -07001612
David Brazdil74eb1b22015-12-14 11:44:01 +00001613void LocationsBuilderARM::VisitSelect(HSelect* select) {
1614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1615 if (Primitive::IsFloatingPointType(select->GetType())) {
1616 locations->SetInAt(0, Location::RequiresFpuRegister());
1617 locations->SetInAt(1, Location::RequiresFpuRegister());
1618 } else {
1619 locations->SetInAt(0, Location::RequiresRegister());
1620 locations->SetInAt(1, Location::RequiresRegister());
1621 }
1622 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1623 locations->SetInAt(2, Location::RequiresRegister());
1624 }
1625 locations->SetOut(Location::SameAsFirstInput());
1626}
1627
1628void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1629 LocationSummary* locations = select->GetLocations();
1630 Label false_target;
1631 GenerateTestAndBranch(select,
1632 /* condition_input_index */ 2,
1633 /* true_target */ nullptr,
1634 &false_target);
1635 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1636 __ Bind(&false_target);
1637}
1638
David Srbecky0cf44932015-12-09 14:09:59 +00001639void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1640 new (GetGraph()->GetArena()) LocationSummary(info);
1641}
1642
1643void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001644 if (codegen_->HasStackMapAtCurrentPc()) {
1645 // Ensure that we do not collide with the stack map of the previous instruction.
1646 __ nop();
1647 }
David Srbecky0cf44932015-12-09 14:09:59 +00001648 codegen_->RecordPcInfo(info, info->GetDexPc());
1649}
1650
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001651void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001652 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001653 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001654 // Handle the long/FP comparisons made in instruction simplification.
1655 switch (cond->InputAt(0)->GetType()) {
1656 case Primitive::kPrimLong:
1657 locations->SetInAt(0, Location::RequiresRegister());
1658 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001659 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001660 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1661 }
1662 break;
1663
1664 case Primitive::kPrimFloat:
1665 case Primitive::kPrimDouble:
1666 locations->SetInAt(0, Location::RequiresFpuRegister());
1667 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00001668 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001669 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1670 }
1671 break;
1672
1673 default:
1674 locations->SetInAt(0, Location::RequiresRegister());
1675 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001676 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1678 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001679 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001680}
1681
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001682void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001683 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001684 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001685 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001686
1687 LocationSummary* locations = cond->GetLocations();
1688 Location left = locations->InAt(0);
1689 Location right = locations->InAt(1);
1690 Register out = locations->Out().AsRegister<Register>();
1691 Label true_label, false_label;
1692
1693 switch (cond->InputAt(0)->GetType()) {
1694 default: {
1695 // Integer case.
1696 if (right.IsRegister()) {
1697 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1698 } else {
1699 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001700 __ CmpConstant(left.AsRegister<Register>(),
1701 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001702 }
Aart Bike9f37602015-10-09 11:15:55 -07001703 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001704 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001705 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001706 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001707 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001708 return;
1709 }
1710 case Primitive::kPrimLong:
1711 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1712 break;
1713 case Primitive::kPrimFloat:
1714 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
1715 GenerateFPJumps(cond, &true_label, &false_label);
1716 break;
1717 case Primitive::kPrimDouble:
1718 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
1719 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
1720 GenerateFPJumps(cond, &true_label, &false_label);
1721 break;
1722 }
1723
1724 // Convert the jumps into the result.
1725 Label done_label;
1726
1727 // False case: result = 0.
1728 __ Bind(&false_label);
1729 __ LoadImmediate(out, 0);
1730 __ b(&done_label);
1731
1732 // True case: result = 1.
1733 __ Bind(&true_label);
1734 __ LoadImmediate(out, 1);
1735 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001736}
1737
1738void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001739 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001740}
1741
1742void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001743 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001744}
1745
1746void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001747 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001748}
1749
1750void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001751 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001752}
1753
1754void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001755 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001756}
1757
1758void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001759 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001760}
1761
1762void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001764}
1765
1766void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001768}
1769
1770void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001771 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001772}
1773
1774void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001776}
1777
1778void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001780}
1781
1782void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001784}
1785
Aart Bike9f37602015-10-09 11:15:55 -07001786void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001788}
1789
1790void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001792}
1793
1794void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001796}
1797
1798void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001799 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001800}
1801
1802void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001804}
1805
1806void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001808}
1809
1810void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001811 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001812}
1813
1814void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001815 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001816}
1817
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001818void LocationsBuilderARM::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001819 local->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001820}
1821
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001822void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) {
1823 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001824}
1825
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001826void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001827 load->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001828}
1829
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001830void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001831 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001832}
1833
1834void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001837 switch (store->InputAt(1)->GetType()) {
1838 case Primitive::kPrimBoolean:
1839 case Primitive::kPrimByte:
1840 case Primitive::kPrimChar:
1841 case Primitive::kPrimShort:
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001844 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1846 break;
1847
1848 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001849 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001850 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1851 break;
1852
1853 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001854 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001855 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856}
1857
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001858void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859}
1860
1861void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001862 LocationSummary* locations =
1863 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001864 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001865}
1866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001867void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001868 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001869}
1870
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001871void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1872 LocationSummary* locations =
1873 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1874 locations->SetOut(Location::ConstantLocation(constant));
1875}
1876
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001877void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001878 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001879}
1880
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001882 LocationSummary* locations =
1883 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001884 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885}
1886
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001887void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 // Will be generated at use site.
1889}
1890
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001891void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1892 LocationSummary* locations =
1893 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1894 locations->SetOut(Location::ConstantLocation(constant));
1895}
1896
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001897void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001898 // Will be generated at use site.
1899}
1900
1901void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1902 LocationSummary* locations =
1903 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1904 locations->SetOut(Location::ConstantLocation(constant));
1905}
1906
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001907void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001908 // Will be generated at use site.
1909}
1910
Calin Juravle27df7582015-04-17 19:12:31 +01001911void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1912 memory_barrier->SetLocations(nullptr);
1913}
1914
1915void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001916 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001917}
1918
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001919void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001920 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001921}
1922
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001923void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001924 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001925}
1926
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001927void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001928 LocationSummary* locations =
1929 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001930 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001931}
1932
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001933void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001934 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001935}
1936
Calin Juravle175dc732015-08-25 15:42:32 +01001937void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1938 // The trampoline uses the same calling convention as dex calling conventions,
1939 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1940 // the method_idx.
1941 HandleInvoke(invoke);
1942}
1943
1944void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1945 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1946}
1947
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001948void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001949 // Explicit clinit checks triggered by static invokes must have been pruned by
1950 // art::PrepareForRegisterAllocation.
1951 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001952
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001953 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001954 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001955 codegen_->GetInstructionSetFeatures());
1956 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001957 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1958 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1959 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001960 return;
1961 }
1962
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001963 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00001964
1965 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1966 if (invoke->HasPcRelativeDexCache()) {
1967 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
1968 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001969}
1970
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001971static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
1972 if (invoke->GetLocations()->Intrinsified()) {
1973 IntrinsicCodeGeneratorARM intrinsic(codegen);
1974 intrinsic.Dispatch(invoke);
1975 return true;
1976 }
1977 return false;
1978}
1979
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001980void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001981 // Explicit clinit checks triggered by static invokes must have been pruned by
1982 // art::PrepareForRegisterAllocation.
1983 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001984
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001985 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1986 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001987 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001988
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001989 LocationSummary* locations = invoke->GetLocations();
1990 codegen_->GenerateStaticOrDirectCall(
1991 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001992 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001993}
1994
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001996 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001997 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001998}
1999
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002000void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002001 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(),
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01002002 codegen_->GetAssembler(),
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002003 codegen_->GetInstructionSetFeatures());
2004 if (intrinsic.TryDispatch(invoke)) {
2005 return;
2006 }
2007
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002008 HandleInvoke(invoke);
2009}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002010
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002011void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002012 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2013 return;
2014 }
2015
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002016 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002017 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002018 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002019}
2020
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002021void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2022 HandleInvoke(invoke);
2023 // Add the hidden argument.
2024 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2025}
2026
2027void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2028 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002029 LocationSummary* locations = invoke->GetLocations();
2030 Register temp = locations->GetTemp(0).AsRegister<Register>();
2031 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002032 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2033 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002034 Location receiver = locations->InAt(0);
2035 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2036
Roland Levillain3b359c72015-11-17 19:35:12 +00002037 // Set the hidden argument. This is safe to do this here, as R12
2038 // won't be modified thereafter, before the `blx` (call) instruction.
2039 DCHECK_EQ(R12, hidden_reg);
2040 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002041
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002042 if (receiver.IsStackSlot()) {
2043 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002044 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002045 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2046 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002047 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002048 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002049 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002050 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002051 // Instead of simply (possibly) unpoisoning `temp` here, we should
2052 // emit a read barrier for the previous class reference load.
2053 // However this is not required in practice, as this is an
2054 // intermediate/temporary reference and because the current
2055 // concurrent copying collector keeps the from-space memory
2056 // intact/accessible until the end of the marking phase (the
2057 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002058 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002059 // temp = temp->GetImtEntryAt(method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002060 uint32_t entry_point =
2061 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002062 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2063 // LR = temp->GetEntryPoint();
2064 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2065 // LR();
2066 __ blx(LR);
2067 DCHECK(!codegen_->IsLeafMethod());
2068 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2069}
2070
Roland Levillain88cb1752014-10-20 16:36:47 +01002071void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2072 LocationSummary* locations =
2073 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2074 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002075 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002076 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2078 break;
2079 }
2080 case Primitive::kPrimLong: {
2081 locations->SetInAt(0, Location::RequiresRegister());
2082 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002083 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002084 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002085
Roland Levillain88cb1752014-10-20 16:36:47 +01002086 case Primitive::kPrimFloat:
2087 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002088 locations->SetInAt(0, Location::RequiresFpuRegister());
2089 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002090 break;
2091
2092 default:
2093 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2094 }
2095}
2096
2097void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2098 LocationSummary* locations = neg->GetLocations();
2099 Location out = locations->Out();
2100 Location in = locations->InAt(0);
2101 switch (neg->GetResultType()) {
2102 case Primitive::kPrimInt:
2103 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002105 break;
2106
2107 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002108 DCHECK(in.IsRegisterPair());
2109 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2110 __ rsbs(out.AsRegisterPairLow<Register>(),
2111 in.AsRegisterPairLow<Register>(),
2112 ShifterOperand(0));
2113 // We cannot emit an RSC (Reverse Subtract with Carry)
2114 // instruction here, as it does not exist in the Thumb-2
2115 // instruction set. We use the following approach
2116 // using SBC and SUB instead.
2117 //
2118 // out.hi = -C
2119 __ sbc(out.AsRegisterPairHigh<Register>(),
2120 out.AsRegisterPairHigh<Register>(),
2121 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2122 // out.hi = out.hi - in.hi
2123 __ sub(out.AsRegisterPairHigh<Register>(),
2124 out.AsRegisterPairHigh<Register>(),
2125 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2126 break;
2127
Roland Levillain88cb1752014-10-20 16:36:47 +01002128 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002129 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002131 break;
2132
Roland Levillain88cb1752014-10-20 16:36:47 +01002133 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002134 DCHECK(in.IsFpuRegisterPair());
2135 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2136 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002137 break;
2138
2139 default:
2140 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2141 }
2142}
2143
Roland Levillaindff1f282014-11-05 14:15:05 +00002144void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002145 Primitive::Type result_type = conversion->GetResultType();
2146 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002147 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002148
Roland Levillain5b3ee562015-04-14 16:02:41 +01002149 // The float-to-long, double-to-long and long-to-float type conversions
2150 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002151 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002152 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2153 && result_type == Primitive::kPrimLong)
2154 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Roland Levillain624279f2014-12-04 11:54:28 +00002155 ? LocationSummary::kCall
2156 : LocationSummary::kNoCall;
2157 LocationSummary* locations =
2158 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2159
David Brazdilb2bd1c52015-03-25 11:17:37 +00002160 // The Java language does not allow treating boolean as an integral type but
2161 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002162
Roland Levillaindff1f282014-11-05 14:15:05 +00002163 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002164 case Primitive::kPrimByte:
2165 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002166 case Primitive::kPrimBoolean:
2167 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002168 case Primitive::kPrimShort:
2169 case Primitive::kPrimInt:
2170 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002171 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002172 locations->SetInAt(0, Location::RequiresRegister());
2173 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2174 break;
2175
2176 default:
2177 LOG(FATAL) << "Unexpected type conversion from " << input_type
2178 << " to " << result_type;
2179 }
2180 break;
2181
Roland Levillain01a8d712014-11-14 16:27:39 +00002182 case Primitive::kPrimShort:
2183 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002184 case Primitive::kPrimBoolean:
2185 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002186 case Primitive::kPrimByte:
2187 case Primitive::kPrimInt:
2188 case Primitive::kPrimChar:
2189 // Processing a Dex `int-to-short' instruction.
2190 locations->SetInAt(0, Location::RequiresRegister());
2191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2192 break;
2193
2194 default:
2195 LOG(FATAL) << "Unexpected type conversion from " << input_type
2196 << " to " << result_type;
2197 }
2198 break;
2199
Roland Levillain946e1432014-11-11 17:35:19 +00002200 case Primitive::kPrimInt:
2201 switch (input_type) {
2202 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002203 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002204 locations->SetInAt(0, Location::Any());
2205 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2206 break;
2207
2208 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002209 // Processing a Dex `float-to-int' instruction.
2210 locations->SetInAt(0, Location::RequiresFpuRegister());
2211 locations->SetOut(Location::RequiresRegister());
2212 locations->AddTemp(Location::RequiresFpuRegister());
2213 break;
2214
Roland Levillain946e1432014-11-11 17:35:19 +00002215 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002216 // Processing a Dex `double-to-int' instruction.
2217 locations->SetInAt(0, Location::RequiresFpuRegister());
2218 locations->SetOut(Location::RequiresRegister());
2219 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002220 break;
2221
2222 default:
2223 LOG(FATAL) << "Unexpected type conversion from " << input_type
2224 << " to " << result_type;
2225 }
2226 break;
2227
Roland Levillaindff1f282014-11-05 14:15:05 +00002228 case Primitive::kPrimLong:
2229 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002230 case Primitive::kPrimBoolean:
2231 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002232 case Primitive::kPrimByte:
2233 case Primitive::kPrimShort:
2234 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002235 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002236 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002237 locations->SetInAt(0, Location::RequiresRegister());
2238 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2239 break;
2240
Roland Levillain624279f2014-12-04 11:54:28 +00002241 case Primitive::kPrimFloat: {
2242 // Processing a Dex `float-to-long' instruction.
2243 InvokeRuntimeCallingConvention calling_convention;
2244 locations->SetInAt(0, Location::FpuRegisterLocation(
2245 calling_convention.GetFpuRegisterAt(0)));
2246 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2247 break;
2248 }
2249
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002250 case Primitive::kPrimDouble: {
2251 // Processing a Dex `double-to-long' instruction.
2252 InvokeRuntimeCallingConvention calling_convention;
2253 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2254 calling_convention.GetFpuRegisterAt(0),
2255 calling_convention.GetFpuRegisterAt(1)));
2256 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002257 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002258 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 }
2264 break;
2265
Roland Levillain981e4542014-11-14 11:47:14 +00002266 case Primitive::kPrimChar:
2267 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002268 case Primitive::kPrimBoolean:
2269 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002270 case Primitive::kPrimByte:
2271 case Primitive::kPrimShort:
2272 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002273 // Processing a Dex `int-to-char' instruction.
2274 locations->SetInAt(0, Location::RequiresRegister());
2275 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2276 break;
2277
2278 default:
2279 LOG(FATAL) << "Unexpected type conversion from " << input_type
2280 << " to " << result_type;
2281 }
2282 break;
2283
Roland Levillaindff1f282014-11-05 14:15:05 +00002284 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002285 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002286 case Primitive::kPrimBoolean:
2287 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002288 case Primitive::kPrimByte:
2289 case Primitive::kPrimShort:
2290 case Primitive::kPrimInt:
2291 case Primitive::kPrimChar:
2292 // Processing a Dex `int-to-float' instruction.
2293 locations->SetInAt(0, Location::RequiresRegister());
2294 locations->SetOut(Location::RequiresFpuRegister());
2295 break;
2296
Roland Levillain5b3ee562015-04-14 16:02:41 +01002297 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002298 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002299 InvokeRuntimeCallingConvention calling_convention;
2300 locations->SetInAt(0, Location::RegisterPairLocation(
2301 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2302 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002303 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002304 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002305
Roland Levillaincff13742014-11-17 14:32:17 +00002306 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002307 // Processing a Dex `double-to-float' instruction.
2308 locations->SetInAt(0, Location::RequiresFpuRegister());
2309 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002310 break;
2311
2312 default:
2313 LOG(FATAL) << "Unexpected type conversion from " << input_type
2314 << " to " << result_type;
2315 };
2316 break;
2317
Roland Levillaindff1f282014-11-05 14:15:05 +00002318 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002319 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002320 case Primitive::kPrimBoolean:
2321 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002322 case Primitive::kPrimByte:
2323 case Primitive::kPrimShort:
2324 case Primitive::kPrimInt:
2325 case Primitive::kPrimChar:
2326 // Processing a Dex `int-to-double' instruction.
2327 locations->SetInAt(0, Location::RequiresRegister());
2328 locations->SetOut(Location::RequiresFpuRegister());
2329 break;
2330
2331 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002332 // Processing a Dex `long-to-double' instruction.
2333 locations->SetInAt(0, Location::RequiresRegister());
2334 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002335 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002336 locations->AddTemp(Location::RequiresFpuRegister());
2337 break;
2338
Roland Levillaincff13742014-11-17 14:32:17 +00002339 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002340 // Processing a Dex `float-to-double' instruction.
2341 locations->SetInAt(0, Location::RequiresFpuRegister());
2342 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002343 break;
2344
2345 default:
2346 LOG(FATAL) << "Unexpected type conversion from " << input_type
2347 << " to " << result_type;
2348 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002349 break;
2350
2351 default:
2352 LOG(FATAL) << "Unexpected type conversion from " << input_type
2353 << " to " << result_type;
2354 }
2355}
2356
2357void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2358 LocationSummary* locations = conversion->GetLocations();
2359 Location out = locations->Out();
2360 Location in = locations->InAt(0);
2361 Primitive::Type result_type = conversion->GetResultType();
2362 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002363 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002365 case Primitive::kPrimByte:
2366 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002367 case Primitive::kPrimBoolean:
2368 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002369 case Primitive::kPrimShort:
2370 case Primitive::kPrimInt:
2371 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002372 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002373 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002374 break;
2375
2376 default:
2377 LOG(FATAL) << "Unexpected type conversion from " << input_type
2378 << " to " << result_type;
2379 }
2380 break;
2381
Roland Levillain01a8d712014-11-14 16:27:39 +00002382 case Primitive::kPrimShort:
2383 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002384 case Primitive::kPrimBoolean:
2385 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002386 case Primitive::kPrimByte:
2387 case Primitive::kPrimInt:
2388 case Primitive::kPrimChar:
2389 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002390 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002391 break;
2392
2393 default:
2394 LOG(FATAL) << "Unexpected type conversion from " << input_type
2395 << " to " << result_type;
2396 }
2397 break;
2398
Roland Levillain946e1432014-11-11 17:35:19 +00002399 case Primitive::kPrimInt:
2400 switch (input_type) {
2401 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002402 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002403 DCHECK(out.IsRegister());
2404 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002405 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002406 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002407 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002408 } else {
2409 DCHECK(in.IsConstant());
2410 DCHECK(in.GetConstant()->IsLongConstant());
2411 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002412 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002413 }
2414 break;
2415
Roland Levillain3f8f9362014-12-02 17:45:01 +00002416 case Primitive::kPrimFloat: {
2417 // Processing a Dex `float-to-int' instruction.
2418 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2419 __ vmovs(temp, in.AsFpuRegister<SRegister>());
2420 __ vcvtis(temp, temp);
2421 __ vmovrs(out.AsRegister<Register>(), temp);
2422 break;
2423 }
2424
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002425 case Primitive::kPrimDouble: {
2426 // Processing a Dex `double-to-int' instruction.
2427 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
2428 DRegister temp_d = FromLowSToD(temp_s);
2429 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
2430 __ vcvtid(temp_s, temp_d);
2431 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002432 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002433 }
Roland Levillain946e1432014-11-11 17:35:19 +00002434
2435 default:
2436 LOG(FATAL) << "Unexpected type conversion from " << input_type
2437 << " to " << result_type;
2438 }
2439 break;
2440
Roland Levillaindff1f282014-11-05 14:15:05 +00002441 case Primitive::kPrimLong:
2442 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002443 case Primitive::kPrimBoolean:
2444 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002445 case Primitive::kPrimByte:
2446 case Primitive::kPrimShort:
2447 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002448 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002449 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002450 DCHECK(out.IsRegisterPair());
2451 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002452 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002453 // Sign extension.
2454 __ Asr(out.AsRegisterPairHigh<Register>(),
2455 out.AsRegisterPairLow<Register>(),
2456 31);
2457 break;
2458
2459 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002460 // Processing a Dex `float-to-long' instruction.
Roland Levillain624279f2014-12-04 11:54:28 +00002461 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2462 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002463 conversion->GetDexPc(),
2464 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002465 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002466 break;
2467
Roland Levillaindff1f282014-11-05 14:15:05 +00002468 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002469 // Processing a Dex `double-to-long' instruction.
2470 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2471 conversion,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00002472 conversion->GetDexPc(),
2473 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002474 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002475 break;
2476
2477 default:
2478 LOG(FATAL) << "Unexpected type conversion from " << input_type
2479 << " to " << result_type;
2480 }
2481 break;
2482
Roland Levillain981e4542014-11-14 11:47:14 +00002483 case Primitive::kPrimChar:
2484 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002485 case Primitive::kPrimBoolean:
2486 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002487 case Primitive::kPrimByte:
2488 case Primitive::kPrimShort:
2489 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002490 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002491 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002492 break;
2493
2494 default:
2495 LOG(FATAL) << "Unexpected type conversion from " << input_type
2496 << " to " << result_type;
2497 }
2498 break;
2499
Roland Levillaindff1f282014-11-05 14:15:05 +00002500 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002501 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002502 case Primitive::kPrimBoolean:
2503 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002504 case Primitive::kPrimByte:
2505 case Primitive::kPrimShort:
2506 case Primitive::kPrimInt:
2507 case Primitive::kPrimChar: {
2508 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002509 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2510 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002511 break;
2512 }
2513
Roland Levillain5b3ee562015-04-14 16:02:41 +01002514 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002515 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002516 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f),
2517 conversion,
2518 conversion->GetDexPc(),
2519 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002520 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002521 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002522
Roland Levillaincff13742014-11-17 14:32:17 +00002523 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002524 // Processing a Dex `double-to-float' instruction.
2525 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2526 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002527 break;
2528
2529 default:
2530 LOG(FATAL) << "Unexpected type conversion from " << input_type
2531 << " to " << result_type;
2532 };
2533 break;
2534
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002537 case Primitive::kPrimBoolean:
2538 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002539 case Primitive::kPrimByte:
2540 case Primitive::kPrimShort:
2541 case Primitive::kPrimInt:
2542 case Primitive::kPrimChar: {
2543 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002544 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002545 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2546 out.AsFpuRegisterPairLow<SRegister>());
2547 break;
2548 }
2549
Roland Levillain647b9ed2014-11-27 12:06:00 +00002550 case Primitive::kPrimLong: {
2551 // Processing a Dex `long-to-double' instruction.
2552 Register low = in.AsRegisterPairLow<Register>();
2553 Register high = in.AsRegisterPairHigh<Register>();
2554 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2555 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002556 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002557 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002558 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2559 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002560
Roland Levillain682393c2015-04-14 15:57:52 +01002561 // temp_d = int-to-double(high)
2562 __ vmovsr(temp_s, high);
2563 __ vcvtdi(temp_d, temp_s);
2564 // constant_d = k2Pow32EncodingForDouble
2565 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2566 // out_d = unsigned-to-double(low)
2567 __ vmovsr(out_s, low);
2568 __ vcvtdu(out_d, out_s);
2569 // out_d += temp_d * constant_d
2570 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002571 break;
2572 }
2573
Roland Levillaincff13742014-11-17 14:32:17 +00002574 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002575 // Processing a Dex `float-to-double' instruction.
2576 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2577 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002578 break;
2579
2580 default:
2581 LOG(FATAL) << "Unexpected type conversion from " << input_type
2582 << " to " << result_type;
2583 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002584 break;
2585
2586 default:
2587 LOG(FATAL) << "Unexpected type conversion from " << input_type
2588 << " to " << result_type;
2589 }
2590}
2591
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002592void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002593 LocationSummary* locations =
2594 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002595 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002596 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002597 locations->SetInAt(0, Location::RequiresRegister());
2598 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002599 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2600 break;
2601 }
2602
2603 case Primitive::kPrimLong: {
2604 locations->SetInAt(0, Location::RequiresRegister());
2605 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002606 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002607 break;
2608 }
2609
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002610 case Primitive::kPrimFloat:
2611 case Primitive::kPrimDouble: {
2612 locations->SetInAt(0, Location::RequiresFpuRegister());
2613 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002614 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002615 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002616 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002617
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002618 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002619 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002620 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002621}
2622
2623void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2624 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002625 Location out = locations->Out();
2626 Location first = locations->InAt(0);
2627 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002628 switch (add->GetResultType()) {
2629 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002630 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002631 __ add(out.AsRegister<Register>(),
2632 first.AsRegister<Register>(),
2633 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002634 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002635 __ AddConstant(out.AsRegister<Register>(),
2636 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002637 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002638 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002639 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002640
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002641 case Primitive::kPrimLong: {
2642 DCHECK(second.IsRegisterPair());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002643 __ adds(out.AsRegisterPairLow<Register>(),
2644 first.AsRegisterPairLow<Register>(),
2645 ShifterOperand(second.AsRegisterPairLow<Register>()));
2646 __ adc(out.AsRegisterPairHigh<Register>(),
2647 first.AsRegisterPairHigh<Register>(),
2648 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002649 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002650 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002651
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002652 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002653 __ vadds(out.AsFpuRegister<SRegister>(),
2654 first.AsFpuRegister<SRegister>(),
2655 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002656 break;
2657
2658 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002659 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2660 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2661 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002662 break;
2663
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002664 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002665 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002666 }
2667}
2668
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002669void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002670 LocationSummary* locations =
2671 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002672 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002673 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002674 locations->SetInAt(0, Location::RequiresRegister());
2675 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002676 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2677 break;
2678 }
2679
2680 case Primitive::kPrimLong: {
2681 locations->SetInAt(0, Location::RequiresRegister());
2682 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002683 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002684 break;
2685 }
Calin Juravle11351682014-10-23 15:38:15 +01002686 case Primitive::kPrimFloat:
2687 case Primitive::kPrimDouble: {
2688 locations->SetInAt(0, Location::RequiresFpuRegister());
2689 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002690 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002691 break;
Calin Juravle11351682014-10-23 15:38:15 +01002692 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002693 default:
Calin Juravle11351682014-10-23 15:38:15 +01002694 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002695 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002696}
2697
2698void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2699 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002700 Location out = locations->Out();
2701 Location first = locations->InAt(0);
2702 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002703 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002704 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002705 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002706 __ sub(out.AsRegister<Register>(),
2707 first.AsRegister<Register>(),
2708 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002709 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002710 __ AddConstant(out.AsRegister<Register>(),
2711 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002712 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002713 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002714 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002715 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002716
Calin Juravle11351682014-10-23 15:38:15 +01002717 case Primitive::kPrimLong: {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002718 DCHECK(second.IsRegisterPair());
Calin Juravle11351682014-10-23 15:38:15 +01002719 __ subs(out.AsRegisterPairLow<Register>(),
2720 first.AsRegisterPairLow<Register>(),
2721 ShifterOperand(second.AsRegisterPairLow<Register>()));
2722 __ sbc(out.AsRegisterPairHigh<Register>(),
2723 first.AsRegisterPairHigh<Register>(),
2724 ShifterOperand(second.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002725 break;
Calin Juravle11351682014-10-23 15:38:15 +01002726 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002727
Calin Juravle11351682014-10-23 15:38:15 +01002728 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002729 __ vsubs(out.AsFpuRegister<SRegister>(),
2730 first.AsFpuRegister<SRegister>(),
2731 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002732 break;
Calin Juravle11351682014-10-23 15:38:15 +01002733 }
2734
2735 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002736 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2737 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2738 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002739 break;
2740 }
2741
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002742
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002743 default:
Calin Juravle11351682014-10-23 15:38:15 +01002744 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002745 }
2746}
2747
Calin Juravle34bacdf2014-10-07 20:23:36 +01002748void LocationsBuilderARM::VisitMul(HMul* mul) {
2749 LocationSummary* locations =
2750 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2751 switch (mul->GetResultType()) {
2752 case Primitive::kPrimInt:
2753 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002754 locations->SetInAt(0, Location::RequiresRegister());
2755 locations->SetInAt(1, Location::RequiresRegister());
2756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002757 break;
2758 }
2759
Calin Juravleb5bfa962014-10-21 18:02:24 +01002760 case Primitive::kPrimFloat:
2761 case Primitive::kPrimDouble: {
2762 locations->SetInAt(0, Location::RequiresFpuRegister());
2763 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002764 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002765 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002766 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002767
2768 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002769 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002770 }
2771}
2772
2773void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2774 LocationSummary* locations = mul->GetLocations();
2775 Location out = locations->Out();
2776 Location first = locations->InAt(0);
2777 Location second = locations->InAt(1);
2778 switch (mul->GetResultType()) {
2779 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002780 __ mul(out.AsRegister<Register>(),
2781 first.AsRegister<Register>(),
2782 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002783 break;
2784 }
2785 case Primitive::kPrimLong: {
2786 Register out_hi = out.AsRegisterPairHigh<Register>();
2787 Register out_lo = out.AsRegisterPairLow<Register>();
2788 Register in1_hi = first.AsRegisterPairHigh<Register>();
2789 Register in1_lo = first.AsRegisterPairLow<Register>();
2790 Register in2_hi = second.AsRegisterPairHigh<Register>();
2791 Register in2_lo = second.AsRegisterPairLow<Register>();
2792
2793 // Extra checks to protect caused by the existence of R1_R2.
2794 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2795 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2796 DCHECK_NE(out_hi, in1_lo);
2797 DCHECK_NE(out_hi, in2_lo);
2798
2799 // input: in1 - 64 bits, in2 - 64 bits
2800 // output: out
2801 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2802 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2803 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2804
2805 // IP <- in1.lo * in2.hi
2806 __ mul(IP, in1_lo, in2_hi);
2807 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2808 __ mla(out_hi, in1_hi, in2_lo, IP);
2809 // out.lo <- (in1.lo * in2.lo)[31:0];
2810 __ umull(out_lo, IP, in1_lo, in2_lo);
2811 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2812 __ add(out_hi, out_hi, ShifterOperand(IP));
2813 break;
2814 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002815
2816 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002817 __ vmuls(out.AsFpuRegister<SRegister>(),
2818 first.AsFpuRegister<SRegister>(),
2819 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002820 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002821 }
2822
2823 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002824 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2825 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2826 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002827 break;
2828 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002829
2830 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002831 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002832 }
2833}
2834
Zheng Xuc6667102015-05-15 16:08:45 +08002835void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2836 DCHECK(instruction->IsDiv() || instruction->IsRem());
2837 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2838
2839 LocationSummary* locations = instruction->GetLocations();
2840 Location second = locations->InAt(1);
2841 DCHECK(second.IsConstant());
2842
2843 Register out = locations->Out().AsRegister<Register>();
2844 Register dividend = locations->InAt(0).AsRegister<Register>();
2845 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2846 DCHECK(imm == 1 || imm == -1);
2847
2848 if (instruction->IsRem()) {
2849 __ LoadImmediate(out, 0);
2850 } else {
2851 if (imm == 1) {
2852 __ Mov(out, dividend);
2853 } else {
2854 __ rsb(out, dividend, ShifterOperand(0));
2855 }
2856 }
2857}
2858
2859void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2860 DCHECK(instruction->IsDiv() || instruction->IsRem());
2861 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2862
2863 LocationSummary* locations = instruction->GetLocations();
2864 Location second = locations->InAt(1);
2865 DCHECK(second.IsConstant());
2866
2867 Register out = locations->Out().AsRegister<Register>();
2868 Register dividend = locations->InAt(0).AsRegister<Register>();
2869 Register temp = locations->GetTemp(0).AsRegister<Register>();
2870 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002871 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002872 int ctz_imm = CTZ(abs_imm);
2873
2874 if (ctz_imm == 1) {
2875 __ Lsr(temp, dividend, 32 - ctz_imm);
2876 } else {
2877 __ Asr(temp, dividend, 31);
2878 __ Lsr(temp, temp, 32 - ctz_imm);
2879 }
2880 __ add(out, temp, ShifterOperand(dividend));
2881
2882 if (instruction->IsDiv()) {
2883 __ Asr(out, out, ctz_imm);
2884 if (imm < 0) {
2885 __ rsb(out, out, ShifterOperand(0));
2886 }
2887 } else {
2888 __ ubfx(out, out, 0, ctz_imm);
2889 __ sub(out, out, ShifterOperand(temp));
2890 }
2891}
2892
2893void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2894 DCHECK(instruction->IsDiv() || instruction->IsRem());
2895 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2896
2897 LocationSummary* locations = instruction->GetLocations();
2898 Location second = locations->InAt(1);
2899 DCHECK(second.IsConstant());
2900
2901 Register out = locations->Out().AsRegister<Register>();
2902 Register dividend = locations->InAt(0).AsRegister<Register>();
2903 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2904 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2905 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2906
2907 int64_t magic;
2908 int shift;
2909 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2910
2911 __ LoadImmediate(temp1, magic);
2912 __ smull(temp2, temp1, dividend, temp1);
2913
2914 if (imm > 0 && magic < 0) {
2915 __ add(temp1, temp1, ShifterOperand(dividend));
2916 } else if (imm < 0 && magic > 0) {
2917 __ sub(temp1, temp1, ShifterOperand(dividend));
2918 }
2919
2920 if (shift != 0) {
2921 __ Asr(temp1, temp1, shift);
2922 }
2923
2924 if (instruction->IsDiv()) {
2925 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2926 } else {
2927 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2928 // TODO: Strength reduction for mls.
2929 __ LoadImmediate(temp2, imm);
2930 __ mls(out, temp1, temp2, dividend);
2931 }
2932}
2933
2934void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2935 DCHECK(instruction->IsDiv() || instruction->IsRem());
2936 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2937
2938 LocationSummary* locations = instruction->GetLocations();
2939 Location second = locations->InAt(1);
2940 DCHECK(second.IsConstant());
2941
2942 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2943 if (imm == 0) {
2944 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2945 } else if (imm == 1 || imm == -1) {
2946 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002947 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002948 DivRemByPowerOfTwo(instruction);
2949 } else {
2950 DCHECK(imm <= -2 || imm >= 2);
2951 GenerateDivRemWithAnyConstant(instruction);
2952 }
2953}
2954
Calin Juravle7c4954d2014-10-28 16:57:40 +00002955void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002956 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2957 if (div->GetResultType() == Primitive::kPrimLong) {
2958 // pLdiv runtime call.
2959 call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08002960 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
2961 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002962 } else if (div->GetResultType() == Primitive::kPrimInt &&
2963 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
2964 // pIdivmod runtime call.
2965 call_kind = LocationSummary::kCall;
2966 }
2967
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002968 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2969
Calin Juravle7c4954d2014-10-28 16:57:40 +00002970 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002971 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08002972 if (div->InputAt(1)->IsConstant()) {
2973 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00002974 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08002975 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002976 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
2977 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08002978 // No temp register required.
2979 } else {
2980 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002981 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002982 locations->AddTemp(Location::RequiresRegister());
2983 }
2984 }
2985 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07002986 locations->SetInAt(0, Location::RequiresRegister());
2987 locations->SetInAt(1, Location::RequiresRegister());
2988 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2989 } else {
2990 InvokeRuntimeCallingConvention calling_convention;
2991 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2992 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2993 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
2994 // we only need the former.
2995 locations->SetOut(Location::RegisterLocation(R0));
2996 }
Calin Juravled0d48522014-11-04 16:40:20 +00002997 break;
2998 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002999 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003000 InvokeRuntimeCallingConvention calling_convention;
3001 locations->SetInAt(0, Location::RegisterPairLocation(
3002 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3003 locations->SetInAt(1, Location::RegisterPairLocation(
3004 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003005 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003006 break;
3007 }
3008 case Primitive::kPrimFloat:
3009 case Primitive::kPrimDouble: {
3010 locations->SetInAt(0, Location::RequiresFpuRegister());
3011 locations->SetInAt(1, Location::RequiresFpuRegister());
3012 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3013 break;
3014 }
3015
3016 default:
3017 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3018 }
3019}
3020
3021void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3022 LocationSummary* locations = div->GetLocations();
3023 Location out = locations->Out();
3024 Location first = locations->InAt(0);
3025 Location second = locations->InAt(1);
3026
3027 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003028 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003029 if (second.IsConstant()) {
3030 GenerateDivRemConstantIntegral(div);
3031 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003032 __ sdiv(out.AsRegister<Register>(),
3033 first.AsRegister<Register>(),
3034 second.AsRegister<Register>());
3035 } else {
3036 InvokeRuntimeCallingConvention calling_convention;
3037 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3038 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3039 DCHECK_EQ(R0, out.AsRegister<Register>());
3040
3041 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003042 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003043 }
Calin Juravled0d48522014-11-04 16:40:20 +00003044 break;
3045 }
3046
Calin Juravle7c4954d2014-10-28 16:57:40 +00003047 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003048 InvokeRuntimeCallingConvention calling_convention;
3049 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3050 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3051 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3052 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3053 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003054 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003055
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003056 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003057 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003058 break;
3059 }
3060
3061 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003062 __ vdivs(out.AsFpuRegister<SRegister>(),
3063 first.AsFpuRegister<SRegister>(),
3064 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003065 break;
3066 }
3067
3068 case Primitive::kPrimDouble: {
3069 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3070 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3071 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3072 break;
3073 }
3074
3075 default:
3076 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3077 }
3078}
3079
Calin Juravlebacfec32014-11-14 15:54:36 +00003080void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003081 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003082
3083 // Most remainders are implemented in the runtime.
3084 LocationSummary::CallKind call_kind = LocationSummary::kCall;
Zheng Xuc6667102015-05-15 16:08:45 +08003085 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3086 // sdiv will be replaced by other instruction sequence.
3087 call_kind = LocationSummary::kNoCall;
3088 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3089 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003090 // Have hardware divide instruction for int, do it with three instructions.
3091 call_kind = LocationSummary::kNoCall;
3092 }
3093
Calin Juravlebacfec32014-11-14 15:54:36 +00003094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3095
Calin Juravled2ec87d2014-12-08 14:24:46 +00003096 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003097 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003098 if (rem->InputAt(1)->IsConstant()) {
3099 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003100 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003101 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003102 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3103 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003104 // No temp register required.
3105 } else {
3106 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003107 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003108 locations->AddTemp(Location::RequiresRegister());
3109 }
3110 }
3111 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003112 locations->SetInAt(0, Location::RequiresRegister());
3113 locations->SetInAt(1, Location::RequiresRegister());
3114 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3115 locations->AddTemp(Location::RequiresRegister());
3116 } else {
3117 InvokeRuntimeCallingConvention calling_convention;
3118 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3119 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3120 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3121 // we only need the latter.
3122 locations->SetOut(Location::RegisterLocation(R1));
3123 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003124 break;
3125 }
3126 case Primitive::kPrimLong: {
3127 InvokeRuntimeCallingConvention calling_convention;
3128 locations->SetInAt(0, Location::RegisterPairLocation(
3129 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3130 locations->SetInAt(1, Location::RegisterPairLocation(
3131 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3132 // The runtime helper puts the output in R2,R3.
3133 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3134 break;
3135 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003136 case Primitive::kPrimFloat: {
3137 InvokeRuntimeCallingConvention calling_convention;
3138 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3139 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3140 locations->SetOut(Location::FpuRegisterLocation(S0));
3141 break;
3142 }
3143
Calin Juravlebacfec32014-11-14 15:54:36 +00003144 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003145 InvokeRuntimeCallingConvention calling_convention;
3146 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3147 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3148 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3149 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3150 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003151 break;
3152 }
3153
3154 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003155 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003156 }
3157}
3158
3159void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3160 LocationSummary* locations = rem->GetLocations();
3161 Location out = locations->Out();
3162 Location first = locations->InAt(0);
3163 Location second = locations->InAt(1);
3164
Calin Juravled2ec87d2014-12-08 14:24:46 +00003165 Primitive::Type type = rem->GetResultType();
3166 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003167 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003168 if (second.IsConstant()) {
3169 GenerateDivRemConstantIntegral(rem);
3170 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003171 Register reg1 = first.AsRegister<Register>();
3172 Register reg2 = second.AsRegister<Register>();
3173 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003174
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003175 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003176 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003177 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003178 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003179 } else {
3180 InvokeRuntimeCallingConvention calling_convention;
3181 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3182 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3183 DCHECK_EQ(R1, out.AsRegister<Register>());
3184
3185 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003186 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003187 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003188 break;
3189 }
3190
3191 case Primitive::kPrimLong: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003192 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003193 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003194 break;
3195 }
3196
Calin Juravled2ec87d2014-12-08 14:24:46 +00003197 case Primitive::kPrimFloat: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003198 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003199 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003200 break;
3201 }
3202
Calin Juravlebacfec32014-11-14 15:54:36 +00003203 case Primitive::kPrimDouble: {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003204 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003205 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003206 break;
3207 }
3208
3209 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003210 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003211 }
3212}
3213
Calin Juravled0d48522014-11-04 16:40:20 +00003214void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003215 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3216 ? LocationSummary::kCallOnSlowPath
3217 : LocationSummary::kNoCall;
3218 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003219 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003220 if (instruction->HasUses()) {
3221 locations->SetOut(Location::SameAsFirstInput());
3222 }
3223}
3224
3225void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003226 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003227 codegen_->AddSlowPath(slow_path);
3228
3229 LocationSummary* locations = instruction->GetLocations();
3230 Location value = locations->InAt(0);
3231
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003232 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003233 case Primitive::kPrimByte:
3234 case Primitive::kPrimChar:
3235 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003236 case Primitive::kPrimInt: {
3237 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003238 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003239 } else {
3240 DCHECK(value.IsConstant()) << value;
3241 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3242 __ b(slow_path->GetEntryLabel());
3243 }
3244 }
3245 break;
3246 }
3247 case Primitive::kPrimLong: {
3248 if (value.IsRegisterPair()) {
3249 __ orrs(IP,
3250 value.AsRegisterPairLow<Register>(),
3251 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3252 __ b(slow_path->GetEntryLabel(), EQ);
3253 } else {
3254 DCHECK(value.IsConstant()) << value;
3255 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3256 __ b(slow_path->GetEntryLabel());
3257 }
3258 }
3259 break;
3260 default:
3261 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3262 }
3263 }
Calin Juravled0d48522014-11-04 16:40:20 +00003264}
3265
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003266void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3267 Register in = locations->InAt(0).AsRegister<Register>();
3268 Location rhs = locations->InAt(1);
3269 Register out = locations->Out().AsRegister<Register>();
3270
3271 if (rhs.IsConstant()) {
3272 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3273 // so map all rotations to a +ve. equivalent in that range.
3274 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3275 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3276 if (rot) {
3277 // Rotate, mapping left rotations to right equivalents if necessary.
3278 // (e.g. left by 2 bits == right by 30.)
3279 __ Ror(out, in, rot);
3280 } else if (out != in) {
3281 __ Mov(out, in);
3282 }
3283 } else {
3284 __ Ror(out, in, rhs.AsRegister<Register>());
3285 }
3286}
3287
3288// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3289// rotates by swapping input regs (effectively rotating by the first 32-bits of
3290// a larger rotation) or flipping direction (thus treating larger right/left
3291// rotations as sub-word sized rotations in the other direction) as appropriate.
3292void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3293 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3294 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3295 Location rhs = locations->InAt(1);
3296 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3297 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3298
3299 if (rhs.IsConstant()) {
3300 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3301 // Map all rotations to +ve. equivalents on the interval [0,63].
3302 rot &= kMaxLongShiftValue;
3303 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3304 // logic below to a simple pair of binary orr.
3305 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3306 if (rot >= kArmBitsPerWord) {
3307 rot -= kArmBitsPerWord;
3308 std::swap(in_reg_hi, in_reg_lo);
3309 }
3310 // Rotate, or mov to out for zero or word size rotations.
3311 if (rot != 0u) {
3312 __ Lsr(out_reg_hi, in_reg_hi, rot);
3313 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3314 __ Lsr(out_reg_lo, in_reg_lo, rot);
3315 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3316 } else {
3317 __ Mov(out_reg_lo, in_reg_lo);
3318 __ Mov(out_reg_hi, in_reg_hi);
3319 }
3320 } else {
3321 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3322 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3323 Label end;
3324 Label shift_by_32_plus_shift_right;
3325
3326 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3327 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3328 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3329 __ b(&shift_by_32_plus_shift_right, CC);
3330
3331 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3332 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3333 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3334 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3335 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3336 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3337 __ Lsr(shift_left, in_reg_hi, shift_right);
3338 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3339 __ b(&end);
3340
3341 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3342 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3343 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3344 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3345 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3346 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3347 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3348 __ Lsl(shift_right, in_reg_hi, shift_left);
3349 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3350
3351 __ Bind(&end);
3352 }
3353}
3354void LocationsBuilderARM::HandleRotate(HRor* ror) {
3355 LocationSummary* locations =
3356 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3357 switch (ror->GetResultType()) {
3358 case Primitive::kPrimInt: {
3359 locations->SetInAt(0, Location::RequiresRegister());
3360 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3362 break;
3363 }
3364 case Primitive::kPrimLong: {
3365 locations->SetInAt(0, Location::RequiresRegister());
3366 if (ror->InputAt(1)->IsConstant()) {
3367 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3368 } else {
3369 locations->SetInAt(1, Location::RequiresRegister());
3370 locations->AddTemp(Location::RequiresRegister());
3371 locations->AddTemp(Location::RequiresRegister());
3372 }
3373 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3374 break;
3375 }
3376 default:
3377 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3378 }
3379}
3380
3381void InstructionCodeGeneratorARM::HandleRotate(HRor* ror) {
3382 LocationSummary* locations = ror->GetLocations();
3383 Primitive::Type type = ror->GetResultType();
3384 switch (type) {
3385 case Primitive::kPrimInt: {
3386 HandleIntegerRotate(locations);
3387 break;
3388 }
3389 case Primitive::kPrimLong: {
3390 HandleLongRotate(locations);
3391 break;
3392 }
3393 default:
3394 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003395 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003396 }
3397}
3398
3399void LocationsBuilderARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003400 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003401}
3402
3403void InstructionCodeGeneratorARM::VisitRor(HRor* op) {
Vladimir Marko351dddf2015-12-11 16:34:46 +00003404 HandleRotate(op);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003405}
3406
Calin Juravle9aec02f2014-11-18 23:06:35 +00003407void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3408 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3409
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003410 LocationSummary* locations =
3411 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003412
3413 switch (op->GetResultType()) {
3414 case Primitive::kPrimInt: {
3415 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003416 if (op->InputAt(1)->IsConstant()) {
3417 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3419 } else {
3420 locations->SetInAt(1, Location::RequiresRegister());
3421 // Make the output overlap, as it will be used to hold the masked
3422 // second input.
3423 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3424 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003425 break;
3426 }
3427 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003428 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003429 if (op->InputAt(1)->IsConstant()) {
3430 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3431 // For simplicity, use kOutputOverlap even though we only require that low registers
3432 // don't clash with high registers which the register allocator currently guarantees.
3433 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3434 } else {
3435 locations->SetInAt(1, Location::RequiresRegister());
3436 locations->AddTemp(Location::RequiresRegister());
3437 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3438 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003439 break;
3440 }
3441 default:
3442 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3443 }
3444}
3445
3446void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3447 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3448
3449 LocationSummary* locations = op->GetLocations();
3450 Location out = locations->Out();
3451 Location first = locations->InAt(0);
3452 Location second = locations->InAt(1);
3453
3454 Primitive::Type type = op->GetResultType();
3455 switch (type) {
3456 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003457 Register out_reg = out.AsRegister<Register>();
3458 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003459 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003460 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003461 // ARM doesn't mask the shift count so we need to do it ourselves.
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003462 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003463 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003464 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003465 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003466 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003467 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003468 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003469 }
3470 } else {
3471 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3472 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue);
Roland Levillainc9285912015-12-18 10:38:42 +00003473 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003474 __ Mov(out_reg, first_reg);
3475 } else if (op->IsShl()) {
3476 __ Lsl(out_reg, first_reg, shift_value);
3477 } else if (op->IsShr()) {
3478 __ Asr(out_reg, first_reg, shift_value);
3479 } else {
3480 __ Lsr(out_reg, first_reg, shift_value);
3481 }
3482 }
3483 break;
3484 }
3485 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003486 Register o_h = out.AsRegisterPairHigh<Register>();
3487 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003488
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003489 Register high = first.AsRegisterPairHigh<Register>();
3490 Register low = first.AsRegisterPairLow<Register>();
3491
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003492 if (second.IsRegister()) {
3493 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003494
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003495 Register second_reg = second.AsRegister<Register>();
3496
3497 if (op->IsShl()) {
3498 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue));
3499 // Shift the high part
3500 __ Lsl(o_h, high, o_l);
3501 // Shift the low part and `or` what overflew on the high part
3502 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3503 __ Lsr(temp, low, temp);
3504 __ orr(o_h, o_h, ShifterOperand(temp));
3505 // If the shift is > 32 bits, override the high part
3506 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3507 __ it(PL);
3508 __ Lsl(o_h, low, temp, PL);
3509 // Shift the low part
3510 __ Lsl(o_l, low, o_l);
3511 } else if (op->IsShr()) {
3512 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3513 // Shift the low part
3514 __ Lsr(o_l, low, o_h);
3515 // Shift the high part and `or` what underflew on the low part
3516 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3517 __ Lsl(temp, high, temp);
3518 __ orr(o_l, o_l, ShifterOperand(temp));
3519 // If the shift is > 32 bits, override the low part
3520 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3521 __ it(PL);
3522 __ Asr(o_l, high, temp, PL);
3523 // Shift the high part
3524 __ Asr(o_h, high, o_h);
3525 } else {
3526 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue));
3527 // same as Shr except we use `Lsr`s and not `Asr`s
3528 __ Lsr(o_l, low, o_h);
3529 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3530 __ Lsl(temp, high, temp);
3531 __ orr(o_l, o_l, ShifterOperand(temp));
3532 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3533 __ it(PL);
3534 __ Lsr(o_l, high, temp, PL);
3535 __ Lsr(o_h, high, o_h);
3536 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003537 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003538 // Register allocator doesn't create partial overlap.
3539 DCHECK_NE(o_l, high);
3540 DCHECK_NE(o_h, low);
3541 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
3542 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxLongShiftValue);
3543 if (shift_value > 32) {
3544 if (op->IsShl()) {
3545 __ Lsl(o_h, low, shift_value - 32);
3546 __ LoadImmediate(o_l, 0);
3547 } else if (op->IsShr()) {
3548 __ Asr(o_l, high, shift_value - 32);
3549 __ Asr(o_h, high, 31);
3550 } else {
3551 __ Lsr(o_l, high, shift_value - 32);
3552 __ LoadImmediate(o_h, 0);
3553 }
3554 } else if (shift_value == 32) {
3555 if (op->IsShl()) {
3556 __ mov(o_h, ShifterOperand(low));
3557 __ LoadImmediate(o_l, 0);
3558 } else if (op->IsShr()) {
3559 __ mov(o_l, ShifterOperand(high));
3560 __ Asr(o_h, high, 31);
3561 } else {
3562 __ mov(o_l, ShifterOperand(high));
3563 __ LoadImmediate(o_h, 0);
3564 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003565 } else if (shift_value == 1) {
3566 if (op->IsShl()) {
3567 __ Lsls(o_l, low, 1);
3568 __ adc(o_h, high, ShifterOperand(high));
3569 } else if (op->IsShr()) {
3570 __ Asrs(o_h, high, 1);
3571 __ Rrx(o_l, low);
3572 } else {
3573 __ Lsrs(o_h, high, 1);
3574 __ Rrx(o_l, low);
3575 }
3576 } else {
3577 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003578 if (op->IsShl()) {
3579 __ Lsl(o_h, high, shift_value);
3580 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3581 __ Lsl(o_l, low, shift_value);
3582 } else if (op->IsShr()) {
3583 __ Lsr(o_l, low, shift_value);
3584 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3585 __ Asr(o_h, high, shift_value);
3586 } else {
3587 __ Lsr(o_l, low, shift_value);
3588 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3589 __ Lsr(o_h, high, shift_value);
3590 }
3591 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003592 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003593 break;
3594 }
3595 default:
3596 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003597 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003598 }
3599}
3600
3601void LocationsBuilderARM::VisitShl(HShl* shl) {
3602 HandleShift(shl);
3603}
3604
3605void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3606 HandleShift(shl);
3607}
3608
3609void LocationsBuilderARM::VisitShr(HShr* shr) {
3610 HandleShift(shr);
3611}
3612
3613void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3614 HandleShift(shr);
3615}
3616
3617void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3618 HandleShift(ushr);
3619}
3620
3621void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3622 HandleShift(ushr);
3623}
3624
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003625void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003626 LocationSummary* locations =
3627 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
David Brazdil6de19382016-01-08 17:37:10 +00003628 if (instruction->IsStringAlloc()) {
3629 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3630 } else {
3631 InvokeRuntimeCallingConvention calling_convention;
3632 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3633 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3634 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003635 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003636}
3637
3638void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003639 // Note: if heap poisoning is enabled, the entry point takes cares
3640 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003641 if (instruction->IsStringAlloc()) {
3642 // String is allocated through StringFactory. Call NewEmptyString entry point.
3643 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3644 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize);
3645 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3646 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3647 __ blx(LR);
3648 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3649 } else {
3650 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3651 instruction,
3652 instruction->GetDexPc(),
3653 nullptr);
3654 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3655 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003656}
3657
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003658void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3659 LocationSummary* locations =
3660 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3661 InvokeRuntimeCallingConvention calling_convention;
3662 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003663 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003664 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003665 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003666}
3667
3668void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3669 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003670 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003671 // Note: if heap poisoning is enabled, the entry point takes cares
3672 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003673 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003674 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00003675 instruction->GetDexPc(),
3676 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003677 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003678}
3679
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003680void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003681 LocationSummary* locations =
3682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003683 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3684 if (location.IsStackSlot()) {
3685 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3686 } else if (location.IsDoubleStackSlot()) {
3687 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003688 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003689 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003690}
3691
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003692void InstructionCodeGeneratorARM::VisitParameterValue(
3693 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003694 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003695}
3696
3697void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3698 LocationSummary* locations =
3699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3700 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3701}
3702
3703void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3704 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003705}
3706
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003707void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003708 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003709 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003710 locations->SetInAt(0, Location::RequiresRegister());
3711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003712}
3713
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003714void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3715 LocationSummary* locations = not_->GetLocations();
3716 Location out = locations->Out();
3717 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003718 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003719 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003720 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003721 break;
3722
3723 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003724 __ mvn(out.AsRegisterPairLow<Register>(),
3725 ShifterOperand(in.AsRegisterPairLow<Register>()));
3726 __ mvn(out.AsRegisterPairHigh<Register>(),
3727 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003728 break;
3729
3730 default:
3731 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3732 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003733}
3734
David Brazdil66d126e2015-04-03 16:02:44 +01003735void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3736 LocationSummary* locations =
3737 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3738 locations->SetInAt(0, Location::RequiresRegister());
3739 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3740}
3741
3742void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003743 LocationSummary* locations = bool_not->GetLocations();
3744 Location out = locations->Out();
3745 Location in = locations->InAt(0);
3746 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3747}
3748
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003749void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003750 LocationSummary* locations =
3751 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003752 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08003753 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003754 case Primitive::kPrimLong: {
3755 locations->SetInAt(0, Location::RequiresRegister());
3756 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003757 // Output overlaps because it is written before doing the low comparison.
3758 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003759 break;
3760 }
3761 case Primitive::kPrimFloat:
3762 case Primitive::kPrimDouble: {
3763 locations->SetInAt(0, Location::RequiresFpuRegister());
3764 locations->SetInAt(1, Location::RequiresFpuRegister());
3765 locations->SetOut(Location::RequiresRegister());
3766 break;
3767 }
3768 default:
3769 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3770 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003771}
3772
3773void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003774 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003776 Location left = locations->InAt(0);
3777 Location right = locations->InAt(1);
3778
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003779 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003780 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003781 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003782 switch (type) {
Aart Bika19616e2016-02-01 18:57:58 -08003783 case Primitive::kPrimInt: {
3784 __ LoadImmediate(out, 0);
3785 __ cmp(left.AsRegister<Register>(),
3786 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3787 less_cond = LT;
3788 break;
3789 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003790 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003791 __ cmp(left.AsRegisterPairHigh<Register>(),
3792 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003793 __ b(&less, LT);
3794 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003795 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003796 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003797 __ cmp(left.AsRegisterPairLow<Register>(),
3798 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003799 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003800 break;
3801 }
3802 case Primitive::kPrimFloat:
3803 case Primitive::kPrimDouble: {
3804 __ LoadImmediate(out, 0);
3805 if (type == Primitive::kPrimFloat) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003806 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003807 } else {
3808 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()),
3809 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>()));
3810 }
3811 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003812 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003813 break;
3814 }
3815 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003816 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003817 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003818 }
Aart Bika19616e2016-02-01 18:57:58 -08003819
Calin Juravleddb7df22014-11-25 20:56:51 +00003820 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003821 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003822
3823 __ Bind(&greater);
3824 __ LoadImmediate(out, 1);
3825 __ b(&done);
3826
3827 __ Bind(&less);
3828 __ LoadImmediate(out, -1);
3829
3830 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003831}
3832
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003833void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003834 LocationSummary* locations =
3835 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003836 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3837 locations->SetInAt(i, Location::Any());
3838 }
3839 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003840}
3841
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003842void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003843 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003844}
3845
Roland Levillainc9285912015-12-18 10:38:42 +00003846void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3847 // TODO (ported from quick): revisit ARM barrier kinds.
3848 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003849 switch (kind) {
3850 case MemBarrierKind::kAnyStore:
3851 case MemBarrierKind::kLoadAny:
3852 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003853 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003854 break;
3855 }
3856 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003857 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003858 break;
3859 }
3860 default:
3861 LOG(FATAL) << "Unexpected memory barrier " << kind;
3862 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003863 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003864}
3865
3866void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3867 uint32_t offset,
3868 Register out_lo,
3869 Register out_hi) {
3870 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003871 // Ensure `out_lo` is different from `addr`, so that loading
3872 // `offset` into `out_lo` does not clutter `addr`.
3873 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003874 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003875 __ add(IP, addr, ShifterOperand(out_lo));
3876 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003877 }
3878 __ ldrexd(out_lo, out_hi, addr);
3879}
3880
3881void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3882 uint32_t offset,
3883 Register value_lo,
3884 Register value_hi,
3885 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003886 Register temp2,
3887 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003888 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003889 if (offset != 0) {
3890 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003891 __ add(IP, addr, ShifterOperand(temp1));
3892 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003893 }
3894 __ Bind(&fail);
3895 // We need a load followed by store. (The address used in a STREX instruction must
3896 // be the same as the address in the most recently executed LDREX instruction.)
3897 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003898 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003899 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003900 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003901}
3902
3903void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3904 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3905
Nicolas Geoffray39468442014-09-02 15:17:15 +01003906 LocationSummary* locations =
3907 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003908 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003909
Calin Juravle52c48962014-12-16 17:02:57 +00003910 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003911 if (Primitive::IsFloatingPointType(field_type)) {
3912 locations->SetInAt(1, Location::RequiresFpuRegister());
3913 } else {
3914 locations->SetInAt(1, Location::RequiresRegister());
3915 }
3916
Calin Juravle52c48962014-12-16 17:02:57 +00003917 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003918 bool generate_volatile = field_info.IsVolatile()
3919 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003920 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003921 bool needs_write_barrier =
3922 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003923 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003924 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003925 if (needs_write_barrier) {
3926 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003927 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003928 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003929 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003930 // - registers need to be consecutive
3931 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003932 // We don't test for ARM yet, and the assertion makes sure that we
3933 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003934 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3935
3936 locations->AddTemp(Location::RequiresRegister());
3937 locations->AddTemp(Location::RequiresRegister());
3938 if (field_type == Primitive::kPrimDouble) {
3939 // For doubles we need two more registers to copy the value.
3940 locations->AddTemp(Location::RegisterLocation(R2));
3941 locations->AddTemp(Location::RegisterLocation(R3));
3942 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003943 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003944}
3945
Calin Juravle52c48962014-12-16 17:02:57 +00003946void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003947 const FieldInfo& field_info,
3948 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003949 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3950
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003951 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003952 Register base = locations->InAt(0).AsRegister<Register>();
3953 Location value = locations->InAt(1);
3954
3955 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003956 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00003957 Primitive::Type field_type = field_info.GetFieldType();
3958 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003959 bool needs_write_barrier =
3960 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003961
3962 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003963 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00003964 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003965
3966 switch (field_type) {
3967 case Primitive::kPrimBoolean:
3968 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003969 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003970 break;
3971 }
3972
3973 case Primitive::kPrimShort:
3974 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003975 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003976 break;
3977 }
3978
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003980 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003981 if (kPoisonHeapReferences && needs_write_barrier) {
3982 // Note that in the case where `value` is a null reference,
3983 // we do not enter this block, as a null reference does not
3984 // need poisoning.
3985 DCHECK_EQ(field_type, Primitive::kPrimNot);
3986 Register temp = locations->GetTemp(0).AsRegister<Register>();
3987 __ Mov(temp, value.AsRegister<Register>());
3988 __ PoisonHeapReference(temp);
3989 __ StoreToOffset(kStoreWord, temp, base, offset);
3990 } else {
3991 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
3992 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003993 break;
3994 }
3995
3996 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00003997 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00003998 GenerateWideAtomicStore(base, offset,
3999 value.AsRegisterPairLow<Register>(),
4000 value.AsRegisterPairHigh<Register>(),
4001 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004002 locations->GetTemp(1).AsRegister<Register>(),
4003 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004004 } else {
4005 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004006 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004007 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004008 break;
4009 }
4010
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004011 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004012 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004013 break;
4014 }
4015
4016 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004017 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004018 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004019 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4020 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4021
4022 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4023
4024 GenerateWideAtomicStore(base, offset,
4025 value_reg_lo,
4026 value_reg_hi,
4027 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004028 locations->GetTemp(3).AsRegister<Register>(),
4029 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004030 } else {
4031 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004032 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004033 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004034 break;
4035 }
4036
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004037 case Primitive::kPrimVoid:
4038 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004039 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004040 }
Calin Juravle52c48962014-12-16 17:02:57 +00004041
Calin Juravle77520bc2015-01-12 18:45:46 +00004042 // Longs and doubles are handled in the switch.
4043 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4044 codegen_->MaybeRecordImplicitNullCheck(instruction);
4045 }
4046
4047 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4048 Register temp = locations->GetTemp(0).AsRegister<Register>();
4049 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004050 codegen_->MarkGCCard(
4051 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004052 }
4053
Calin Juravle52c48962014-12-16 17:02:57 +00004054 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004055 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004056 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004057}
4058
Calin Juravle52c48962014-12-16 17:02:57 +00004059void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4060 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004061
4062 bool object_field_get_with_read_barrier =
4063 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004064 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004065 new (GetGraph()->GetArena()) LocationSummary(instruction,
4066 object_field_get_with_read_barrier ?
4067 LocationSummary::kCallOnSlowPath :
4068 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004069 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004070
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004071 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004072 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004073 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004074 // The output overlaps in case of volatile long: we don't want the
4075 // code generated by GenerateWideAtomicLoad to overwrite the
4076 // object's location. Likewise, in the case of an object field get
4077 // with read barriers enabled, we do not want the load to overwrite
4078 // the object's location, as we need it to emit the read barrier.
4079 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4080 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004081
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004082 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4083 locations->SetOut(Location::RequiresFpuRegister());
4084 } else {
4085 locations->SetOut(Location::RequiresRegister(),
4086 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4087 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004088 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004089 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004090 // - registers need to be consecutive
4091 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004092 // We don't test for ARM yet, and the assertion makes sure that we
4093 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004094 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4095 locations->AddTemp(Location::RequiresRegister());
4096 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004097 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4098 // We need a temporary register for the read barrier marking slow
4099 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4100 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004101 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102}
4103
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004104Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4105 Opcode opcode) {
4106 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4107 if (constant->IsConstant() &&
4108 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4109 return Location::ConstantLocation(constant->AsConstant());
4110 }
4111 return Location::RequiresRegister();
4112}
4113
4114bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4115 Opcode opcode) {
4116 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4117 if (Primitive::Is64BitType(input_cst->GetType())) {
4118 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) &&
4119 CanEncodeConstantAsImmediate(High32Bits(value), opcode);
4120 } else {
4121 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4122 }
4123}
4124
4125bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) {
4126 ShifterOperand so;
4127 ArmAssembler* assembler = codegen_->GetAssembler();
4128 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) {
4129 return true;
4130 }
4131 Opcode neg_opcode = kNoOperand;
4132 switch (opcode) {
4133 case AND:
4134 neg_opcode = BIC;
4135 break;
4136 case ORR:
4137 neg_opcode = ORN;
4138 break;
4139 default:
4140 return false;
4141 }
4142 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so);
4143}
4144
Calin Juravle52c48962014-12-16 17:02:57 +00004145void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4146 const FieldInfo& field_info) {
4147 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004148
Calin Juravle52c48962014-12-16 17:02:57 +00004149 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004150 Location base_loc = locations->InAt(0);
4151 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004152 Location out = locations->Out();
4153 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004154 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004155 Primitive::Type field_type = field_info.GetFieldType();
4156 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4157
4158 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004159 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004160 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004161 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004162
Roland Levillainc9285912015-12-18 10:38:42 +00004163 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004164 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004165 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004166
Roland Levillainc9285912015-12-18 10:38:42 +00004167 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004168 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004169 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004170
Roland Levillainc9285912015-12-18 10:38:42 +00004171 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004172 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004173 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004174
4175 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004176 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004177 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004178
4179 case Primitive::kPrimNot: {
4180 // /* HeapReference<Object> */ out = *(base + offset)
4181 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4182 Location temp_loc = locations->GetTemp(0);
4183 // Note that a potential implicit null check is handled in this
4184 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4185 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4186 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4187 if (is_volatile) {
4188 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4189 }
4190 } else {
4191 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4192 codegen_->MaybeRecordImplicitNullCheck(instruction);
4193 if (is_volatile) {
4194 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4195 }
4196 // If read barriers are enabled, emit read barriers other than
4197 // Baker's using a slow path (and also unpoison the loaded
4198 // reference, if heap poisoning is enabled).
4199 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4200 }
4201 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004202 }
4203
Roland Levillainc9285912015-12-18 10:38:42 +00004204 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004205 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004206 GenerateWideAtomicLoad(base, offset,
4207 out.AsRegisterPairLow<Register>(),
4208 out.AsRegisterPairHigh<Register>());
4209 } else {
4210 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4211 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004212 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004213
Roland Levillainc9285912015-12-18 10:38:42 +00004214 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004215 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004216 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004217
4218 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004219 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004220 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004221 Register lo = locations->GetTemp(0).AsRegister<Register>();
4222 Register hi = locations->GetTemp(1).AsRegister<Register>();
4223 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004224 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004225 __ vmovdrr(out_reg, lo, hi);
4226 } else {
4227 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004228 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004229 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004230 break;
4231 }
4232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004233 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004234 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004235 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236 }
Calin Juravle52c48962014-12-16 17:02:57 +00004237
Roland Levillainc9285912015-12-18 10:38:42 +00004238 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4239 // Potential implicit null checks, in the case of reference or
4240 // double fields, are handled in the previous switch statement.
4241 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004242 codegen_->MaybeRecordImplicitNullCheck(instruction);
4243 }
4244
Calin Juravle52c48962014-12-16 17:02:57 +00004245 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004246 if (field_type == Primitive::kPrimNot) {
4247 // Memory barriers, in the case of references, are also handled
4248 // in the previous switch statement.
4249 } else {
4250 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4251 }
Roland Levillain4d027112015-07-01 15:41:14 +01004252 }
Calin Juravle52c48962014-12-16 17:02:57 +00004253}
4254
4255void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4256 HandleFieldSet(instruction, instruction->GetFieldInfo());
4257}
4258
4259void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004260 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004261}
4262
4263void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4264 HandleFieldGet(instruction, instruction->GetFieldInfo());
4265}
4266
4267void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4268 HandleFieldGet(instruction, instruction->GetFieldInfo());
4269}
4270
4271void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4272 HandleFieldGet(instruction, instruction->GetFieldInfo());
4273}
4274
4275void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4276 HandleFieldGet(instruction, instruction->GetFieldInfo());
4277}
4278
4279void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4280 HandleFieldSet(instruction, instruction->GetFieldInfo());
4281}
4282
4283void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004284 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285}
4286
Calin Juravlee460d1d2015-09-29 04:52:17 +01004287void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4288 HUnresolvedInstanceFieldGet* instruction) {
4289 FieldAccessCallingConventionARM calling_convention;
4290 codegen_->CreateUnresolvedFieldLocationSummary(
4291 instruction, instruction->GetFieldType(), calling_convention);
4292}
4293
4294void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4295 HUnresolvedInstanceFieldGet* instruction) {
4296 FieldAccessCallingConventionARM calling_convention;
4297 codegen_->GenerateUnresolvedFieldAccess(instruction,
4298 instruction->GetFieldType(),
4299 instruction->GetFieldIndex(),
4300 instruction->GetDexPc(),
4301 calling_convention);
4302}
4303
4304void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4305 HUnresolvedInstanceFieldSet* instruction) {
4306 FieldAccessCallingConventionARM calling_convention;
4307 codegen_->CreateUnresolvedFieldLocationSummary(
4308 instruction, instruction->GetFieldType(), calling_convention);
4309}
4310
4311void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4312 HUnresolvedInstanceFieldSet* instruction) {
4313 FieldAccessCallingConventionARM calling_convention;
4314 codegen_->GenerateUnresolvedFieldAccess(instruction,
4315 instruction->GetFieldType(),
4316 instruction->GetFieldIndex(),
4317 instruction->GetDexPc(),
4318 calling_convention);
4319}
4320
4321void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4322 HUnresolvedStaticFieldGet* instruction) {
4323 FieldAccessCallingConventionARM calling_convention;
4324 codegen_->CreateUnresolvedFieldLocationSummary(
4325 instruction, instruction->GetFieldType(), calling_convention);
4326}
4327
4328void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4329 HUnresolvedStaticFieldGet* instruction) {
4330 FieldAccessCallingConventionARM calling_convention;
4331 codegen_->GenerateUnresolvedFieldAccess(instruction,
4332 instruction->GetFieldType(),
4333 instruction->GetFieldIndex(),
4334 instruction->GetDexPc(),
4335 calling_convention);
4336}
4337
4338void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4339 HUnresolvedStaticFieldSet* instruction) {
4340 FieldAccessCallingConventionARM calling_convention;
4341 codegen_->CreateUnresolvedFieldLocationSummary(
4342 instruction, instruction->GetFieldType(), calling_convention);
4343}
4344
4345void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4346 HUnresolvedStaticFieldSet* instruction) {
4347 FieldAccessCallingConventionARM calling_convention;
4348 codegen_->GenerateUnresolvedFieldAccess(instruction,
4349 instruction->GetFieldType(),
4350 instruction->GetFieldIndex(),
4351 instruction->GetDexPc(),
4352 calling_convention);
4353}
4354
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004355void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004356 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4357 ? LocationSummary::kCallOnSlowPath
4358 : LocationSummary::kNoCall;
4359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravle77520bc2015-01-12 18:45:46 +00004360 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004361 if (instruction->HasUses()) {
4362 locations->SetOut(Location::SameAsFirstInput());
4363 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364}
4365
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004366void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004367 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4368 return;
4369 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004370 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004371
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004372 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
4373 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4374}
4375
4376void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004377 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004378 codegen_->AddSlowPath(slow_path);
4379
4380 LocationSummary* locations = instruction->GetLocations();
4381 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004382
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004383 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384}
4385
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004386void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004387 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004388 GenerateImplicitNullCheck(instruction);
4389 } else {
4390 GenerateExplicitNullCheck(instruction);
4391 }
4392}
4393
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004394void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004395 bool object_array_get_with_read_barrier =
4396 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004397 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004398 new (GetGraph()->GetArena()) LocationSummary(instruction,
4399 object_array_get_with_read_barrier ?
4400 LocationSummary::kCallOnSlowPath :
4401 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004402 locations->SetInAt(0, Location::RequiresRegister());
4403 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004404 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4405 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4406 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004407 // The output overlaps in the case of an object array get with
4408 // read barriers enabled: we do not want the move to overwrite the
4409 // array's location, as we need it to emit the read barrier.
4410 locations->SetOut(
4411 Location::RequiresRegister(),
4412 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004413 }
Roland Levillainc9285912015-12-18 10:38:42 +00004414 // We need a temporary register for the read barrier marking slow
4415 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4416 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4417 locations->AddTemp(Location::RequiresRegister());
4418 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004419}
4420
4421void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4422 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004423 Location obj_loc = locations->InAt(0);
4424 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004426 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004427
Roland Levillainc9285912015-12-18 10:38:42 +00004428 Primitive::Type type = instruction->GetType();
Roland Levillain4d027112015-07-01 15:41:14 +01004429 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430 case Primitive::kPrimBoolean: {
4431 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004432 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004433 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004434 size_t offset =
4435 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004436 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
4437 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004438 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset);
4440 }
4441 break;
4442 }
4443
4444 case Primitive::kPrimByte: {
4445 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004446 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004447 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004448 size_t offset =
4449 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004450 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
4451 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004452 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004453 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset);
4454 }
4455 break;
4456 }
4457
4458 case Primitive::kPrimShort: {
4459 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004460 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004461 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004462 size_t offset =
4463 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004464 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
4465 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004466 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004467 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset);
4468 }
4469 break;
4470 }
4471
4472 case Primitive::kPrimChar: {
4473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004474 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004475 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004476 size_t offset =
4477 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004478 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
4479 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004480 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004481 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset);
4482 }
4483 break;
4484 }
4485
Roland Levillainc9285912015-12-18 10:38:42 +00004486 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004488 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004489 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004490 size_t offset =
4491 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004492 __ LoadFromOffset(kLoadWord, out, obj, offset);
4493 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004494 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004495 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4496 }
4497 break;
4498 }
4499
Roland Levillainc9285912015-12-18 10:38:42 +00004500 case Primitive::kPrimNot: {
4501 static_assert(
4502 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4503 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4504 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4505 // /* HeapReference<Object> */ out =
4506 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4507 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4508 Location temp = locations->GetTemp(0);
4509 // Note that a potential implicit null check is handled in this
4510 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4511 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4512 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4513 } else {
4514 Register out = out_loc.AsRegister<Register>();
4515 if (index.IsConstant()) {
4516 size_t offset =
4517 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4518 __ LoadFromOffset(kLoadWord, out, obj, offset);
4519 codegen_->MaybeRecordImplicitNullCheck(instruction);
4520 // If read barriers are enabled, emit read barriers other than
4521 // Baker's using a slow path (and also unpoison the loaded
4522 // reference, if heap poisoning is enabled).
4523 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4524 } else {
4525 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4526 __ LoadFromOffset(kLoadWord, out, IP, data_offset);
4527 codegen_->MaybeRecordImplicitNullCheck(instruction);
4528 // If read barriers are enabled, emit read barriers other than
4529 // Baker's using a slow path (and also unpoison the loaded
4530 // reference, if heap poisoning is enabled).
4531 codegen_->MaybeGenerateReadBarrierSlow(
4532 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4533 }
4534 }
4535 break;
4536 }
4537
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004538 case Primitive::kPrimLong: {
4539 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004540 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004541 size_t offset =
4542 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004543 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004544 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004545 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004546 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004547 }
4548 break;
4549 }
4550
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004551 case Primitive::kPrimFloat: {
4552 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004553 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004554 if (index.IsConstant()) {
4555 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004556 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004557 } else {
4558 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004559 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004560 }
4561 break;
4562 }
4563
4564 case Primitive::kPrimDouble: {
4565 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillainc9285912015-12-18 10:38:42 +00004566 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004567 if (index.IsConstant()) {
4568 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004569 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004570 } else {
4571 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004572 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004573 }
4574 break;
4575 }
4576
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004577 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004578 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004579 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004580 }
Roland Levillain4d027112015-07-01 15:41:14 +01004581
4582 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004583 // Potential implicit null checks, in the case of reference
4584 // arrays, are handled in the previous switch statement.
4585 } else {
4586 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004587 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588}
4589
4590void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004591 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004592
4593 bool needs_write_barrier =
4594 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004595 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4596 bool object_array_set_with_read_barrier =
4597 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004598
Nicolas Geoffray39468442014-09-02 15:17:15 +01004599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004600 instruction,
Roland Levillain3b359c72015-11-17 19:35:12 +00004601 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4602 LocationSummary::kCallOnSlowPath :
4603 LocationSummary::kNoCall);
4604
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004605 locations->SetInAt(0, Location::RequiresRegister());
4606 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4607 if (Primitive::IsFloatingPointType(value_type)) {
4608 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004609 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004610 locations->SetInAt(2, Location::RequiresRegister());
4611 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004612 if (needs_write_barrier) {
4613 // Temporary registers for the write barrier.
4614 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004615 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004616 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004617}
4618
4619void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4620 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004621 Location array_loc = locations->InAt(0);
4622 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004623 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004624 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004625 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004626 bool needs_write_barrier =
4627 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004628
4629 switch (value_type) {
4630 case Primitive::kPrimBoolean:
4631 case Primitive::kPrimByte: {
4632 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004633 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004634 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004635 size_t offset =
4636 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004637 __ StoreToOffset(kStoreByte, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004638 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004639 __ add(IP, array, ShifterOperand(index.AsRegister<Register>()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004640 __ StoreToOffset(kStoreByte, value, IP, data_offset);
4641 }
4642 break;
4643 }
4644
4645 case Primitive::kPrimShort:
4646 case Primitive::kPrimChar: {
4647 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004648 Register value = locations->InAt(2).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004649 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004650 size_t offset =
4651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004652 __ StoreToOffset(kStoreHalfword, value, array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004653 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004654 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004655 __ StoreToOffset(kStoreHalfword, value, IP, data_offset);
4656 }
4657 break;
4658 }
4659
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004661 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00004662 Location value_loc = locations->InAt(2);
4663 Register value = value_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004664 Register source = value;
4665
4666 if (instruction->InputAt(2)->IsNullConstant()) {
4667 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004668 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004669 size_t offset =
4670 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004671 __ StoreToOffset(kStoreWord, source, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004672 } else {
4673 DCHECK(index.IsRegister()) << index;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004674 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillain4d027112015-07-01 15:41:14 +01004675 __ StoreToOffset(kStoreWord, source, IP, data_offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004676 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004678 DCHECK(!needs_write_barrier);
4679 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004680 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004681 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004682
4683 DCHECK(needs_write_barrier);
4684 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4685 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4686 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4687 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4688 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4689 Label done;
4690 SlowPathCode* slow_path = nullptr;
4691
Roland Levillain3b359c72015-11-17 19:35:12 +00004692 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004693 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4694 codegen_->AddSlowPath(slow_path);
4695 if (instruction->GetValueCanBeNull()) {
4696 Label non_zero;
4697 __ CompareAndBranchIfNonZero(value, &non_zero);
4698 if (index.IsConstant()) {
4699 size_t offset =
4700 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4701 __ StoreToOffset(kStoreWord, value, array, offset);
4702 } else {
4703 DCHECK(index.IsRegister()) << index;
4704 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4705 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4706 }
4707 codegen_->MaybeRecordImplicitNullCheck(instruction);
4708 __ b(&done);
4709 __ Bind(&non_zero);
4710 }
4711
Roland Levillain3b359c72015-11-17 19:35:12 +00004712 if (kEmitCompilerReadBarrier) {
4713 // When read barriers are enabled, the type checking
4714 // instrumentation requires two read barriers:
4715 //
4716 // __ Mov(temp2, temp1);
4717 // // /* HeapReference<Class> */ temp1 = temp1->component_type_
4718 // __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004719 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004720 // instruction, temp1_loc, temp1_loc, temp2_loc, component_offset);
4721 //
4722 // // /* HeapReference<Class> */ temp2 = value->klass_
4723 // __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
Roland Levillainc9285912015-12-18 10:38:42 +00004724 // codegen_->GenerateReadBarrierSlow(
Roland Levillain3b359c72015-11-17 19:35:12 +00004725 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp1_loc);
4726 //
4727 // __ cmp(temp1, ShifterOperand(temp2));
4728 //
4729 // However, the second read barrier may trash `temp`, as it
4730 // is a temporary register, and as such would not be saved
4731 // along with live registers before calling the runtime (nor
4732 // restored afterwards). So in this case, we bail out and
4733 // delegate the work to the array set slow path.
4734 //
4735 // TODO: Extend the register allocator to support a new
4736 // "(locally) live temp" location so as to avoid always
4737 // going into the slow path when read barriers are enabled.
4738 __ b(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004739 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004740 // /* HeapReference<Class> */ temp1 = array->klass_
4741 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4742 codegen_->MaybeRecordImplicitNullCheck(instruction);
4743 __ MaybeUnpoisonHeapReference(temp1);
4744
4745 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4746 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4747 // /* HeapReference<Class> */ temp2 = value->klass_
4748 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4749 // If heap poisoning is enabled, no need to unpoison `temp1`
4750 // nor `temp2`, as we are comparing two poisoned references.
4751 __ cmp(temp1, ShifterOperand(temp2));
4752
4753 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4754 Label do_put;
4755 __ b(&do_put, EQ);
4756 // If heap poisoning is enabled, the `temp1` reference has
4757 // not been unpoisoned yet; unpoison it now.
4758 __ MaybeUnpoisonHeapReference(temp1);
4759
4760 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4761 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4762 // If heap poisoning is enabled, no need to unpoison
4763 // `temp1`, as we are comparing against null below.
4764 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4765 __ Bind(&do_put);
4766 } else {
4767 __ b(slow_path->GetEntryLabel(), NE);
4768 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004769 }
4770 }
4771
4772 if (kPoisonHeapReferences) {
4773 // Note that in the case where `value` is a null reference,
4774 // we do not enter this block, as a null reference does not
4775 // need poisoning.
4776 DCHECK_EQ(value_type, Primitive::kPrimNot);
4777 __ Mov(temp1, value);
4778 __ PoisonHeapReference(temp1);
4779 source = temp1;
4780 }
4781
4782 if (index.IsConstant()) {
4783 size_t offset =
4784 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4785 __ StoreToOffset(kStoreWord, source, array, offset);
4786 } else {
4787 DCHECK(index.IsRegister()) << index;
4788 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4789 __ StoreToOffset(kStoreWord, source, IP, data_offset);
4790 }
4791
Roland Levillain3b359c72015-11-17 19:35:12 +00004792 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004793 codegen_->MaybeRecordImplicitNullCheck(instruction);
4794 }
4795
4796 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4797
4798 if (done.IsLinked()) {
4799 __ Bind(&done);
4800 }
4801
4802 if (slow_path != nullptr) {
4803 __ Bind(slow_path->GetExitLabel());
4804 }
4805
4806 break;
4807 }
4808
4809 case Primitive::kPrimInt: {
4810 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4811 Register value = locations->InAt(2).AsRegister<Register>();
4812 if (index.IsConstant()) {
4813 size_t offset =
4814 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4815 __ StoreToOffset(kStoreWord, value, array, offset);
4816 } else {
4817 DCHECK(index.IsRegister()) << index;
4818 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
4819 __ StoreToOffset(kStoreWord, value, IP, data_offset);
4820 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 break;
4822 }
4823
4824 case Primitive::kPrimLong: {
4825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004826 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004828 size_t offset =
4829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004830 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004831 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004832 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004833 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004834 }
4835 break;
4836 }
4837
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004838 case Primitive::kPrimFloat: {
4839 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4840 Location value = locations->InAt(2);
4841 DCHECK(value.IsFpuRegister());
4842 if (index.IsConstant()) {
4843 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004844 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004845 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004846 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004847 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4848 }
4849 break;
4850 }
4851
4852 case Primitive::kPrimDouble: {
4853 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4854 Location value = locations->InAt(2);
4855 DCHECK(value.IsFpuRegisterPair());
4856 if (index.IsConstant()) {
4857 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004858 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004859 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004860 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004861 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
4862 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004863
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004864 break;
4865 }
4866
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004867 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004868 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004869 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004871
Roland Levillain80e67092016-01-08 16:04:55 +00004872 // Objects are handled in the switch.
4873 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 codegen_->MaybeRecordImplicitNullCheck(instruction);
4875 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004876}
4877
4878void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004879 LocationSummary* locations =
4880 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004881 locations->SetInAt(0, Location::RequiresRegister());
4882 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004883}
4884
4885void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
4886 LocationSummary* locations = instruction->GetLocations();
4887 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004888 Register obj = locations->InAt(0).AsRegister<Register>();
4889 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004891 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892}
4893
4894void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004895 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4896 ? LocationSummary::kCallOnSlowPath
4897 : LocationSummary::kNoCall;
4898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899 locations->SetInAt(0, Location::RequiresRegister());
4900 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004901 if (instruction->HasUses()) {
4902 locations->SetOut(Location::SameAsFirstInput());
4903 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004904}
4905
4906void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
4907 LocationSummary* locations = instruction->GetLocations();
Andreas Gampe85b62f22015-09-09 13:15:38 -07004908 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004909 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910 codegen_->AddSlowPath(slow_path);
4911
Roland Levillain271ab9c2014-11-27 15:23:57 +00004912 Register index = locations->InAt(0).AsRegister<Register>();
4913 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004914
4915 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01004916 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004917}
4918
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004919void CodeGeneratorARM::MarkGCCard(Register temp,
4920 Register card,
4921 Register object,
4922 Register value,
4923 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004924 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004925 if (can_be_null) {
4926 __ CompareAndBranchIfZero(value, &is_null);
4927 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value());
4929 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
4930 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004931 if (can_be_null) {
4932 __ Bind(&is_null);
4933 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934}
4935
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004936void LocationsBuilderARM::VisitTemporary(HTemporary* temp) {
4937 temp->SetLocations(nullptr);
4938}
4939
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004940void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004941 // Nothing to do, this is driven by the code generator.
4942}
4943
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004944void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004945 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004946}
4947
4948void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004949 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4950}
4951
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004952void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
4953 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4954}
4955
4956void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004957 HBasicBlock* block = instruction->GetBlock();
4958 if (block->GetLoopInformation() != nullptr) {
4959 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4960 // The back edge will generate the suspend check.
4961 return;
4962 }
4963 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4964 // The goto will generate the suspend check.
4965 return;
4966 }
4967 GenerateSuspendCheck(instruction, nullptr);
4968}
4969
4970void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
4971 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004972 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004973 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
4974 if (slow_path == nullptr) {
4975 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
4976 instruction->SetSlowPath(slow_path);
4977 codegen_->AddSlowPath(slow_path);
4978 if (successor != nullptr) {
4979 DCHECK(successor->IsLoopHeader());
4980 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4981 }
4982 } else {
4983 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4984 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004985
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00004986 __ LoadFromOffset(
4987 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004988 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004989 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004990 __ Bind(slow_path->GetReturnLabel());
4991 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004992 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004993 __ b(slow_path->GetEntryLabel());
4994 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004995}
4996
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004997ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
4998 return codegen_->GetAssembler();
4999}
5000
5001void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005002 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005003 Location source = move->GetSource();
5004 Location destination = move->GetDestination();
5005
5006 if (source.IsRegister()) {
5007 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005008 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005009 } else if (destination.IsFpuRegister()) {
5010 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005011 } else {
5012 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005013 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005014 SP, destination.GetStackIndex());
5015 }
5016 } else if (source.IsStackSlot()) {
5017 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005018 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005019 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005020 } else if (destination.IsFpuRegister()) {
5021 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005022 } else {
5023 DCHECK(destination.IsStackSlot());
5024 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5025 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5026 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005027 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005028 if (destination.IsRegister()) {
5029 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5030 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005031 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005032 } else {
5033 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005034 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5035 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005036 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005037 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005038 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5039 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005040 } else if (destination.IsRegisterPair()) {
5041 DCHECK(ExpectedPairLayout(destination));
5042 __ LoadFromOffset(
5043 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5044 } else {
5045 DCHECK(destination.IsFpuRegisterPair()) << destination;
5046 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5047 SP,
5048 source.GetStackIndex());
5049 }
5050 } else if (source.IsRegisterPair()) {
5051 if (destination.IsRegisterPair()) {
5052 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5053 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005054 } else if (destination.IsFpuRegisterPair()) {
5055 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5056 source.AsRegisterPairLow<Register>(),
5057 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005058 } else {
5059 DCHECK(destination.IsDoubleStackSlot()) << destination;
5060 DCHECK(ExpectedPairLayout(source));
5061 __ StoreToOffset(
5062 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5063 }
5064 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005065 if (destination.IsRegisterPair()) {
5066 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5067 destination.AsRegisterPairHigh<Register>(),
5068 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5069 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005070 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5071 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5072 } else {
5073 DCHECK(destination.IsDoubleStackSlot()) << destination;
5074 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5075 SP,
5076 destination.GetStackIndex());
5077 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005078 } else {
5079 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005080 HConstant* constant = source.GetConstant();
5081 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5082 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005083 if (destination.IsRegister()) {
5084 __ LoadImmediate(destination.AsRegister<Register>(), value);
5085 } else {
5086 DCHECK(destination.IsStackSlot());
5087 __ LoadImmediate(IP, value);
5088 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5089 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005090 } else if (constant->IsLongConstant()) {
5091 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005092 if (destination.IsRegisterPair()) {
5093 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5094 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005095 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005096 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005097 __ LoadImmediate(IP, Low32Bits(value));
5098 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5099 __ LoadImmediate(IP, High32Bits(value));
5100 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5101 }
5102 } else if (constant->IsDoubleConstant()) {
5103 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005104 if (destination.IsFpuRegisterPair()) {
5105 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005106 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005107 DCHECK(destination.IsDoubleStackSlot()) << destination;
5108 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005109 __ LoadImmediate(IP, Low32Bits(int_value));
5110 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5111 __ LoadImmediate(IP, High32Bits(int_value));
5112 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5113 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005114 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005115 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005116 float value = constant->AsFloatConstant()->GetValue();
5117 if (destination.IsFpuRegister()) {
5118 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5119 } else {
5120 DCHECK(destination.IsStackSlot());
5121 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5122 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5123 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005124 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005125 }
5126}
5127
5128void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5129 __ Mov(IP, reg);
5130 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5131 __ StoreToOffset(kStoreWord, IP, SP, mem);
5132}
5133
5134void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5135 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5136 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5137 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5138 SP, mem1 + stack_offset);
5139 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5140 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5141 SP, mem2 + stack_offset);
5142 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5143}
5144
5145void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005146 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005147 Location source = move->GetSource();
5148 Location destination = move->GetDestination();
5149
5150 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005151 DCHECK_NE(source.AsRegister<Register>(), IP);
5152 DCHECK_NE(destination.AsRegister<Register>(), IP);
5153 __ Mov(IP, source.AsRegister<Register>());
5154 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5155 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005156 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005157 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005158 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005159 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005160 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5161 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005162 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005163 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005164 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005165 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005166 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005167 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005168 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005169 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005170 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5171 destination.AsRegisterPairHigh<Register>(),
5172 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005173 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005174 Register low_reg = source.IsRegisterPair()
5175 ? source.AsRegisterPairLow<Register>()
5176 : destination.AsRegisterPairLow<Register>();
5177 int mem = source.IsRegisterPair()
5178 ? destination.GetStackIndex()
5179 : source.GetStackIndex();
5180 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005181 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005182 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005183 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005184 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005185 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5186 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005187 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005188 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005189 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005190 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5191 DRegister reg = source.IsFpuRegisterPair()
5192 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5193 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5194 int mem = source.IsFpuRegisterPair()
5195 ? destination.GetStackIndex()
5196 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005197 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005198 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005199 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005200 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5201 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5202 : destination.AsFpuRegister<SRegister>();
5203 int mem = source.IsFpuRegister()
5204 ? destination.GetStackIndex()
5205 : source.GetStackIndex();
5206
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005207 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005208 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005209 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005210 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005211 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5212 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005213 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005214 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005215 }
5216}
5217
5218void ParallelMoveResolverARM::SpillScratch(int reg) {
5219 __ Push(static_cast<Register>(reg));
5220}
5221
5222void ParallelMoveResolverARM::RestoreScratch(int reg) {
5223 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005224}
5225
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005226void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005227 InvokeRuntimeCallingConvention calling_convention;
5228 CodeGenerator::CreateLoadClassLocationSummary(
5229 cls,
5230 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain3b359c72015-11-17 19:35:12 +00005231 Location::RegisterLocation(R0),
5232 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005233}
5234
5235void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005236 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005237 if (cls->NeedsAccessCheck()) {
5238 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5239 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5240 cls,
5241 cls->GetDexPc(),
5242 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005243 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005244 return;
5245 }
5246
Roland Levillain3b359c72015-11-17 19:35:12 +00005247 Location out_loc = locations->Out();
5248 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005249 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005250
Calin Juravle580b6092015-10-06 17:35:58 +01005251 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005252 DCHECK(!cls->CanCallRuntime());
5253 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillainc9285912015-12-18 10:38:42 +00005254 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5255 GenerateGcRootFieldLoad(
5256 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005257 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005258 // /* GcRoot<mirror::Class>[] */ out =
5259 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005260 __ LoadFromOffset(kLoadWord,
5261 out,
5262 current_method,
Vladimir Marko05792b92015-08-03 11:56:49 +01005263 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005264 // /* GcRoot<mirror::Class> */ out = out[type_index]
5265 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005266
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005267 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5268 DCHECK(cls->CanCallRuntime());
5269 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
5270 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5271 codegen_->AddSlowPath(slow_path);
5272 if (!cls->IsInDexCache()) {
5273 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5274 }
5275 if (cls->MustGenerateClinitCheck()) {
5276 GenerateClassInitializationCheck(slow_path, out);
5277 } else {
5278 __ Bind(slow_path->GetExitLabel());
5279 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005280 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005281 }
5282}
5283
5284void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5285 LocationSummary* locations =
5286 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5287 locations->SetInAt(0, Location::RequiresRegister());
5288 if (check->HasUses()) {
5289 locations->SetOut(Location::SameAsFirstInput());
5290 }
5291}
5292
5293void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005294 // We assume the class is not null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005295 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005296 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005297 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005298 GenerateClassInitializationCheck(slow_path,
5299 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005300}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005301
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005302void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005303 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005304 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5305 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5306 __ b(slow_path->GetEntryLabel(), LT);
5307 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5308 // properly. Therefore, we do a memory fence.
5309 __ dmb(ISH);
5310 __ Bind(slow_path->GetExitLabel());
5311}
5312
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005313void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005314 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5315 ? LocationSummary::kCallOnSlowPath
5316 : LocationSummary::kNoCall;
5317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005318 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005319 locations->SetOut(Location::RequiresRegister());
5320}
5321
5322void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005323 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005324 Location out_loc = locations->Out();
5325 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005326 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005327
Roland Levillainc9285912015-12-18 10:38:42 +00005328 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5329 GenerateGcRootFieldLoad(
5330 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain3b359c72015-11-17 19:35:12 +00005331 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005332 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00005333 // /* GcRoot<mirror::String> */ out = out[string_index]
5334 GenerateGcRootFieldLoad(
5335 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain3b359c72015-11-17 19:35:12 +00005336
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005337 if (!load->IsInDexCache()) {
5338 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5339 codegen_->AddSlowPath(slow_path);
5340 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5341 __ Bind(slow_path->GetExitLabel());
5342 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005343}
5344
David Brazdilcb1c0552015-08-04 16:22:25 +01005345static int32_t GetExceptionTlsOffset() {
5346 return Thread::ExceptionOffset<kArmWordSize>().Int32Value();
5347}
5348
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005349void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5350 LocationSummary* locations =
5351 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5352 locations->SetOut(Location::RequiresRegister());
5353}
5354
5355void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005356 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005357 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5358}
5359
5360void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5361 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5362}
5363
5364void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005365 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005366 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005367}
5368
5369void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5370 LocationSummary* locations =
5371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5372 InvokeRuntimeCallingConvention calling_convention;
5373 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5374}
5375
5376void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
5377 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005378 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005379 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005380}
5381
Roland Levillainc9285912015-12-18 10:38:42 +00005382static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5383 return kEmitCompilerReadBarrier &&
5384 (kUseBakerReadBarrier ||
5385 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5386 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5387 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5388}
5389
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005390void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005391 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005392 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5393 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005394 case TypeCheckKind::kExactCheck:
5395 case TypeCheckKind::kAbstractClassCheck:
5396 case TypeCheckKind::kClassHierarchyCheck:
5397 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005398 call_kind =
5399 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005400 break;
5401 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005402 case TypeCheckKind::kUnresolvedCheck:
5403 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005404 call_kind = LocationSummary::kCallOnSlowPath;
5405 break;
5406 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005407
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005408 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain3b359c72015-11-17 19:35:12 +00005409 locations->SetInAt(0, Location::RequiresRegister());
5410 locations->SetInAt(1, Location::RequiresRegister());
5411 // The "out" register is used as a temporary, so it overlaps with the inputs.
5412 // Note that TypeCheckSlowPathARM uses this register too.
5413 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5414 // When read barriers are enabled, we need a temporary register for
5415 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005416 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005417 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005418 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005419}
5420
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005421void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005422 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005423 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005424 Location obj_loc = locations->InAt(0);
5425 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005426 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005427 Location out_loc = locations->Out();
5428 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005429 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005430 locations->GetTemp(0) :
5431 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005432 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005433 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5434 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5435 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005436 Label done, zero;
Andreas Gampe85b62f22015-09-09 13:15:38 -07005437 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005438
5439 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005440 // avoid null check if we know obj is not null.
5441 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005442 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005443 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005444
Roland Levillain3b359c72015-11-17 19:35:12 +00005445 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005446 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005447
Roland Levillainc9285912015-12-18 10:38:42 +00005448 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005449 case TypeCheckKind::kExactCheck: {
5450 __ cmp(out, ShifterOperand(cls));
5451 // Classes must be equal for the instanceof to succeed.
5452 __ b(&zero, NE);
5453 __ LoadImmediate(out, 1);
5454 __ b(&done);
5455 break;
5456 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005457
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005458 case TypeCheckKind::kAbstractClassCheck: {
5459 // If the class is abstract, we eagerly fetch the super class of the
5460 // object to avoid doing a comparison we know will fail.
5461 Label loop;
5462 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005463 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005464 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005465 // If `out` is null, we use it for the result, and jump to `done`.
5466 __ CompareAndBranchIfZero(out, &done);
5467 __ cmp(out, ShifterOperand(cls));
5468 __ b(&loop, NE);
5469 __ LoadImmediate(out, 1);
5470 if (zero.IsLinked()) {
5471 __ b(&done);
5472 }
5473 break;
5474 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005475
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005476 case TypeCheckKind::kClassHierarchyCheck: {
5477 // Walk over the class hierarchy to find a match.
5478 Label loop, success;
5479 __ Bind(&loop);
5480 __ cmp(out, ShifterOperand(cls));
5481 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005482 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005483 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005484 __ CompareAndBranchIfNonZero(out, &loop);
5485 // If `out` is null, we use it for the result, and jump to `done`.
5486 __ b(&done);
5487 __ Bind(&success);
5488 __ LoadImmediate(out, 1);
5489 if (zero.IsLinked()) {
5490 __ b(&done);
5491 }
5492 break;
5493 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005495 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005496 // Do an exact check.
5497 Label exact_check;
5498 __ cmp(out, ShifterOperand(cls));
5499 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005500 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005501 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005502 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005503 // If `out` is null, we use it for the result, and jump to `done`.
5504 __ CompareAndBranchIfZero(out, &done);
5505 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5506 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5507 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005508 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005509 __ LoadImmediate(out, 1);
5510 __ b(&done);
5511 break;
5512 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005513
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005514 case TypeCheckKind::kArrayCheck: {
5515 __ cmp(out, ShifterOperand(cls));
5516 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005517 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5518 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005519 codegen_->AddSlowPath(slow_path);
5520 __ b(slow_path->GetEntryLabel(), NE);
5521 __ LoadImmediate(out, 1);
5522 if (zero.IsLinked()) {
5523 __ b(&done);
5524 }
5525 break;
5526 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005527
Calin Juravle98893e12015-10-02 21:05:03 +01005528 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005529 case TypeCheckKind::kInterfaceCheck: {
5530 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005531 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005532 // cases.
5533 //
5534 // We cannot directly call the InstanceofNonTrivial runtime
5535 // entry point without resorting to a type checking slow path
5536 // here (i.e. by calling InvokeRuntime directly), as it would
5537 // require to assign fixed registers for the inputs of this
5538 // HInstanceOf instruction (following the runtime calling
5539 // convention), which might be cluttered by the potential first
5540 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005541 //
5542 // TODO: Introduce a new runtime entry point taking the object
5543 // to test (instead of its class) as argument, and let it deal
5544 // with the read barrier issues. This will let us refactor this
5545 // case of the `switch` code as it was previously (with a direct
5546 // call to the runtime not using a type checking slow path).
5547 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005548 DCHECK(locations->OnlyCallsOnSlowPath());
5549 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5550 /* is_fatal */ false);
5551 codegen_->AddSlowPath(slow_path);
5552 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005553 if (zero.IsLinked()) {
5554 __ b(&done);
5555 }
5556 break;
5557 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005558 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005559
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005560 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005561 __ Bind(&zero);
5562 __ LoadImmediate(out, 0);
5563 }
5564
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005565 if (done.IsLinked()) {
5566 __ Bind(&done);
5567 }
5568
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005569 if (slow_path != nullptr) {
5570 __ Bind(slow_path->GetExitLabel());
5571 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005572}
5573
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005574void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005575 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5576 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5577
Roland Levillain3b359c72015-11-17 19:35:12 +00005578 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5579 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005580 case TypeCheckKind::kExactCheck:
5581 case TypeCheckKind::kAbstractClassCheck:
5582 case TypeCheckKind::kClassHierarchyCheck:
5583 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005584 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5585 LocationSummary::kCallOnSlowPath :
5586 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005587 break;
5588 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005589 case TypeCheckKind::kUnresolvedCheck:
5590 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005591 call_kind = LocationSummary::kCallOnSlowPath;
5592 break;
5593 }
5594
Roland Levillain3b359c72015-11-17 19:35:12 +00005595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5596 locations->SetInAt(0, Location::RequiresRegister());
5597 locations->SetInAt(1, Location::RequiresRegister());
5598 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5599 locations->AddTemp(Location::RequiresRegister());
5600 // When read barriers are enabled, we need an additional temporary
5601 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005602 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005603 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005604 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005605}
5606
5607void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005608 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005609 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005610 Location obj_loc = locations->InAt(0);
5611 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005612 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005613 Location temp_loc = locations->GetTemp(0);
5614 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005615 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005616 locations->GetTemp(1) :
5617 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005618 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5620 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5621 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005622
Roland Levillain3b359c72015-11-17 19:35:12 +00005623 bool is_type_check_slow_path_fatal =
5624 (type_check_kind == TypeCheckKind::kExactCheck ||
5625 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5626 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5627 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5628 !instruction->CanThrowIntoCatchBlock();
5629 SlowPathCode* type_check_slow_path =
5630 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5631 is_type_check_slow_path_fatal);
5632 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005633
5634 Label done;
5635 // Avoid null check if we know obj is not null.
5636 if (instruction->MustDoNullCheck()) {
5637 __ CompareAndBranchIfZero(obj, &done);
5638 }
5639
Roland Levillain3b359c72015-11-17 19:35:12 +00005640 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005641 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005642
Roland Levillain3b359c72015-11-17 19:35:12 +00005643 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005644 case TypeCheckKind::kExactCheck:
5645 case TypeCheckKind::kArrayCheck: {
5646 __ cmp(temp, ShifterOperand(cls));
5647 // Jump to slow path for throwing the exception or doing a
5648 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005649 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005650 break;
5651 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005652
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005653 case TypeCheckKind::kAbstractClassCheck: {
5654 // If the class is abstract, we eagerly fetch the super class of the
5655 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005656 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005657 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005658 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005659 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005660
5661 // If the class reference currently in `temp` is not null, jump
5662 // to the `compare_classes` label to compare it with the checked
5663 // class.
5664 __ CompareAndBranchIfNonZero(temp, &compare_classes);
5665 // Otherwise, jump to the slow path to throw the exception.
5666 //
5667 // But before, move back the object's class into `temp` before
5668 // going into the slow path, as it has been overwritten in the
5669 // meantime.
5670 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005671 GenerateReferenceLoadTwoRegisters(
5672 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005673 __ b(type_check_slow_path->GetEntryLabel());
5674
5675 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676 __ cmp(temp, ShifterOperand(cls));
5677 __ b(&loop, NE);
5678 break;
5679 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005680
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 case TypeCheckKind::kClassHierarchyCheck: {
5682 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005683 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005684 __ Bind(&loop);
5685 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005686 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005687
Roland Levillain3b359c72015-11-17 19:35:12 +00005688 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005689 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005690
5691 // If the class reference currently in `temp` is not null, jump
5692 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005693 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005694 // Otherwise, jump to the slow path to throw the exception.
5695 //
5696 // But before, move back the object's class into `temp` before
5697 // going into the slow path, as it has been overwritten in the
5698 // meantime.
5699 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005700 GenerateReferenceLoadTwoRegisters(
5701 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005702 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005703 break;
5704 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005705
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005706 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005707 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005708 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005709 __ cmp(temp, ShifterOperand(cls));
5710 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005711
5712 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005713 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005714 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005715
5716 // If the component type is not null (i.e. the object is indeed
5717 // an array), jump to label `check_non_primitive_component_type`
5718 // to further check that this component type is not a primitive
5719 // type.
5720 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
5721 // Otherwise, jump to the slow path to throw the exception.
5722 //
5723 // But before, move back the object's class into `temp` before
5724 // going into the slow path, as it has been overwritten in the
5725 // meantime.
5726 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005727 GenerateReferenceLoadTwoRegisters(
5728 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005729 __ b(type_check_slow_path->GetEntryLabel());
5730
5731 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005732 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00005733 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
5734 __ CompareAndBranchIfZero(temp, &done);
5735 // Same comment as above regarding `temp` and the slow path.
5736 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005737 GenerateReferenceLoadTwoRegisters(
5738 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00005739 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005740 break;
5741 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005742
Calin Juravle98893e12015-10-02 21:05:03 +01005743 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005744 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005745 // We always go into the type check slow path for the unresolved
5746 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00005747 //
5748 // We cannot directly call the CheckCast runtime entry point
5749 // without resorting to a type checking slow path here (i.e. by
5750 // calling InvokeRuntime directly), as it would require to
5751 // assign fixed registers for the inputs of this HInstanceOf
5752 // instruction (following the runtime calling convention), which
5753 // might be cluttered by the potential first read barrier
5754 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005755 //
5756 // TODO: Introduce a new runtime entry point taking the object
5757 // to test (instead of its class) as argument, and let it deal
5758 // with the read barrier issues. This will let us refactor this
5759 // case of the `switch` code as it was previously (with a direct
5760 // call to the runtime not using a type checking slow path).
5761 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005762 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005763 break;
5764 }
5765 __ Bind(&done);
5766
Roland Levillain3b359c72015-11-17 19:35:12 +00005767 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005768}
5769
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005770void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5771 LocationSummary* locations =
5772 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5773 InvokeRuntimeCallingConvention calling_convention;
5774 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5775}
5776
5777void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
5778 codegen_->InvokeRuntime(instruction->IsEnter()
5779 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
5780 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00005781 instruction->GetDexPc(),
5782 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005783 if (instruction->IsEnter()) {
5784 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5785 } else {
5786 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5787 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005788}
5789
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005790void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
5791void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
5792void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005793
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005794void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005795 LocationSummary* locations =
5796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5797 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5798 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005799 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005800 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005801 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005803}
5804
5805void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
5806 HandleBitwiseOperation(instruction);
5807}
5808
5809void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
5810 HandleBitwiseOperation(instruction);
5811}
5812
5813void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
5814 HandleBitwiseOperation(instruction);
5815}
5816
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005817void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
5818 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
5819 if (value == 0xffffffffu) {
5820 if (out != first) {
5821 __ mov(out, ShifterOperand(first));
5822 }
5823 return;
5824 }
5825 if (value == 0u) {
5826 __ mov(out, ShifterOperand(0));
5827 return;
5828 }
5829 ShifterOperand so;
5830 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
5831 __ and_(out, first, so);
5832 } else {
5833 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
5834 __ bic(out, first, ShifterOperand(~value));
5835 }
5836}
5837
5838void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
5839 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
5840 if (value == 0u) {
5841 if (out != first) {
5842 __ mov(out, ShifterOperand(first));
5843 }
5844 return;
5845 }
5846 if (value == 0xffffffffu) {
5847 __ mvn(out, ShifterOperand(0));
5848 return;
5849 }
5850 ShifterOperand so;
5851 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
5852 __ orr(out, first, so);
5853 } else {
5854 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
5855 __ orn(out, first, ShifterOperand(~value));
5856 }
5857}
5858
5859void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
5860 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
5861 if (value == 0u) {
5862 if (out != first) {
5863 __ mov(out, ShifterOperand(first));
5864 }
5865 return;
5866 }
5867 __ eor(out, first, ShifterOperand(value));
5868}
5869
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005870void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
5871 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005872 Location first = locations->InAt(0);
5873 Location second = locations->InAt(1);
5874 Location out = locations->Out();
5875
5876 if (second.IsConstant()) {
5877 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
5878 uint32_t value_low = Low32Bits(value);
5879 if (instruction->GetResultType() == Primitive::kPrimInt) {
5880 Register first_reg = first.AsRegister<Register>();
5881 Register out_reg = out.AsRegister<Register>();
5882 if (instruction->IsAnd()) {
5883 GenerateAndConst(out_reg, first_reg, value_low);
5884 } else if (instruction->IsOr()) {
5885 GenerateOrrConst(out_reg, first_reg, value_low);
5886 } else {
5887 DCHECK(instruction->IsXor());
5888 GenerateEorConst(out_reg, first_reg, value_low);
5889 }
5890 } else {
5891 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5892 uint32_t value_high = High32Bits(value);
5893 Register first_low = first.AsRegisterPairLow<Register>();
5894 Register first_high = first.AsRegisterPairHigh<Register>();
5895 Register out_low = out.AsRegisterPairLow<Register>();
5896 Register out_high = out.AsRegisterPairHigh<Register>();
5897 if (instruction->IsAnd()) {
5898 GenerateAndConst(out_low, first_low, value_low);
5899 GenerateAndConst(out_high, first_high, value_high);
5900 } else if (instruction->IsOr()) {
5901 GenerateOrrConst(out_low, first_low, value_low);
5902 GenerateOrrConst(out_high, first_high, value_high);
5903 } else {
5904 DCHECK(instruction->IsXor());
5905 GenerateEorConst(out_low, first_low, value_low);
5906 GenerateEorConst(out_high, first_high, value_high);
5907 }
5908 }
5909 return;
5910 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005911
5912 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005913 Register first_reg = first.AsRegister<Register>();
5914 ShifterOperand second_reg(second.AsRegister<Register>());
5915 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005916 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005917 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005918 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005919 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005920 } else {
5921 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005922 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005923 }
5924 } else {
5925 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005926 Register first_low = first.AsRegisterPairLow<Register>();
5927 Register first_high = first.AsRegisterPairHigh<Register>();
5928 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
5929 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
5930 Register out_low = out.AsRegisterPairLow<Register>();
5931 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005932 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005933 __ and_(out_low, first_low, second_low);
5934 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005935 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005936 __ orr(out_low, first_low, second_low);
5937 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005938 } else {
5939 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005940 __ eor(out_low, first_low, second_low);
5941 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005942 }
5943 }
5944}
5945
Roland Levillainc9285912015-12-18 10:38:42 +00005946void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
5947 Location out,
5948 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005949 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005950 Register out_reg = out.AsRegister<Register>();
5951 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005952 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005953 if (kUseBakerReadBarrier) {
5954 // Load with fast path based Baker's read barrier.
5955 // /* HeapReference<Object> */ out = *(out + offset)
5956 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005957 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005958 } else {
5959 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005960 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00005961 // in the following move operation, as we will need it for the
5962 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005963 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00005964 // /* HeapReference<Object> */ out = *(out + offset)
5965 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005966 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00005967 }
5968 } else {
5969 // Plain load with no read barrier.
5970 // /* HeapReference<Object> */ out = *(out + offset)
5971 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
5972 __ MaybeUnpoisonHeapReference(out_reg);
5973 }
5974}
5975
5976void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5977 Location out,
5978 Location obj,
5979 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005980 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00005981 Register out_reg = out.AsRegister<Register>();
5982 Register obj_reg = obj.AsRegister<Register>();
5983 if (kEmitCompilerReadBarrier) {
5984 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005985 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00005986 // Load with fast path based Baker's read barrier.
5987 // /* HeapReference<Object> */ out = *(obj + offset)
5988 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005989 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00005990 } else {
5991 // Load with slow path based read barrier.
5992 // /* HeapReference<Object> */ out = *(obj + offset)
5993 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
5994 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5995 }
5996 } else {
5997 // Plain load with no read barrier.
5998 // /* HeapReference<Object> */ out = *(obj + offset)
5999 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6000 __ MaybeUnpoisonHeapReference(out_reg);
6001 }
6002}
6003
6004void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6005 Location root,
6006 Register obj,
6007 uint32_t offset) {
6008 Register root_reg = root.AsRegister<Register>();
6009 if (kEmitCompilerReadBarrier) {
6010 if (kUseBakerReadBarrier) {
6011 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6012 // Baker's read barrier are used:
6013 //
6014 // root = obj.field;
6015 // if (Thread::Current()->GetIsGcMarking()) {
6016 // root = ReadBarrier::Mark(root)
6017 // }
6018
6019 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6020 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6021 static_assert(
6022 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6023 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6024 "have different sizes.");
6025 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6026 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6027 "have different sizes.");
6028
6029 // Slow path used to mark the GC root `root`.
6030 SlowPathCode* slow_path =
6031 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root, root);
6032 codegen_->AddSlowPath(slow_path);
6033
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006034 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006035 __ LoadFromOffset(
6036 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmWordSize>().Int32Value());
6037 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6038 __ Bind(slow_path->GetExitLabel());
6039 } else {
6040 // GC root loaded through a slow path for read barriers other
6041 // than Baker's.
6042 // /* GcRoot<mirror::Object>* */ root = obj + offset
6043 __ AddConstant(root_reg, obj, offset);
6044 // /* mirror::Object* */ root = root->Read()
6045 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6046 }
6047 } else {
6048 // Plain GC root load with no read barrier.
6049 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6050 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6051 // Note that GC roots are not affected by heap poisoning, thus we
6052 // do not have to unpoison `root_reg` here.
6053 }
6054}
6055
6056void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6057 Location ref,
6058 Register obj,
6059 uint32_t offset,
6060 Location temp,
6061 bool needs_null_check) {
6062 DCHECK(kEmitCompilerReadBarrier);
6063 DCHECK(kUseBakerReadBarrier);
6064
6065 // /* HeapReference<Object> */ ref = *(obj + offset)
6066 Location no_index = Location::NoLocation();
6067 GenerateReferenceLoadWithBakerReadBarrier(
6068 instruction, ref, obj, offset, no_index, temp, needs_null_check);
6069}
6070
6071void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6072 Location ref,
6073 Register obj,
6074 uint32_t data_offset,
6075 Location index,
6076 Location temp,
6077 bool needs_null_check) {
6078 DCHECK(kEmitCompilerReadBarrier);
6079 DCHECK(kUseBakerReadBarrier);
6080
6081 // /* HeapReference<Object> */ ref =
6082 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6083 GenerateReferenceLoadWithBakerReadBarrier(
6084 instruction, ref, obj, data_offset, index, temp, needs_null_check);
6085}
6086
6087void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6088 Location ref,
6089 Register obj,
6090 uint32_t offset,
6091 Location index,
6092 Location temp,
6093 bool needs_null_check) {
6094 DCHECK(kEmitCompilerReadBarrier);
6095 DCHECK(kUseBakerReadBarrier);
6096
6097 // In slow path based read barriers, the read barrier call is
6098 // inserted after the original load. However, in fast path based
6099 // Baker's read barriers, we need to perform the load of
6100 // mirror::Object::monitor_ *before* the original reference load.
6101 // This load-load ordering is required by the read barrier.
6102 // The fast path/slow path (for Baker's algorithm) should look like:
6103 //
6104 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6105 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6106 // HeapReference<Object> ref = *src; // Original reference load.
6107 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6108 // if (is_gray) {
6109 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6110 // }
6111 //
6112 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006113 // slightly more complex as it performs additional checks that we do
6114 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006115
6116 Register ref_reg = ref.AsRegister<Register>();
6117 Register temp_reg = temp.AsRegister<Register>();
6118 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6119
6120 // /* int32_t */ monitor = obj->monitor_
6121 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6122 if (needs_null_check) {
6123 MaybeRecordImplicitNullCheck(instruction);
6124 }
6125 // /* LockWord */ lock_word = LockWord(monitor)
6126 static_assert(sizeof(LockWord) == sizeof(int32_t),
6127 "art::LockWord and int32_t have different sizes.");
6128 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6129 __ Lsr(temp_reg, temp_reg, LockWord::kReadBarrierStateShift);
6130 __ and_(temp_reg, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6131 static_assert(
6132 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6133 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6134
6135 // Introduce a dependency on the high bits of rb_state, which shall
6136 // be all zeroes, to prevent load-load reordering, and without using
6137 // a memory barrier (which would be more expensive).
6138 // IP = rb_state & ~LockWord::kReadBarrierStateMask = 0
6139 __ bic(IP, temp_reg, ShifterOperand(LockWord::kReadBarrierStateMask));
6140 // obj is unchanged by this operation, but its value now depends on
6141 // IP, which depends on temp_reg.
6142 __ add(obj, obj, ShifterOperand(IP));
6143
6144 // The actual reference load.
6145 if (index.IsValid()) {
6146 static_assert(
6147 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6148 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6149 // /* HeapReference<Object> */ ref =
6150 // *(obj + offset + index * sizeof(HeapReference<Object>))
6151 if (index.IsConstant()) {
6152 size_t computed_offset =
6153 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset;
6154 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6155 } else {
6156 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
6157 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6158 }
6159 } else {
6160 // /* HeapReference<Object> */ ref = *(obj + offset)
6161 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6162 }
6163
6164 // Object* ref = ref_addr->AsMirrorPtr()
6165 __ MaybeUnpoisonHeapReference(ref_reg);
6166
6167 // Slow path used to mark the object `ref` when it is gray.
6168 SlowPathCode* slow_path =
6169 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref, ref);
6170 AddSlowPath(slow_path);
6171
6172 // if (rb_state == ReadBarrier::gray_ptr_)
6173 // ref = ReadBarrier::Mark(ref);
6174 __ cmp(temp_reg, ShifterOperand(ReadBarrier::gray_ptr_));
6175 __ b(slow_path->GetEntryLabel(), EQ);
6176 __ Bind(slow_path->GetExitLabel());
6177}
6178
6179void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6180 Location out,
6181 Location ref,
6182 Location obj,
6183 uint32_t offset,
6184 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006185 DCHECK(kEmitCompilerReadBarrier);
6186
Roland Levillainc9285912015-12-18 10:38:42 +00006187 // Insert a slow path based read barrier *after* the reference load.
6188 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006189 // If heap poisoning is enabled, the unpoisoning of the loaded
6190 // reference will be carried out by the runtime within the slow
6191 // path.
6192 //
6193 // Note that `ref` currently does not get unpoisoned (when heap
6194 // poisoning is enabled), which is alright as the `ref` argument is
6195 // not used by the artReadBarrierSlow entry point.
6196 //
6197 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6198 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6199 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6200 AddSlowPath(slow_path);
6201
Roland Levillain3b359c72015-11-17 19:35:12 +00006202 __ b(slow_path->GetEntryLabel());
6203 __ Bind(slow_path->GetExitLabel());
6204}
6205
Roland Levillainc9285912015-12-18 10:38:42 +00006206void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6207 Location out,
6208 Location ref,
6209 Location obj,
6210 uint32_t offset,
6211 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006212 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006213 // Baker's read barriers shall be handled by the fast path
6214 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6215 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006216 // If heap poisoning is enabled, unpoisoning will be taken care of
6217 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006218 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006219 } else if (kPoisonHeapReferences) {
6220 __ UnpoisonHeapReference(out.AsRegister<Register>());
6221 }
6222}
6223
Roland Levillainc9285912015-12-18 10:38:42 +00006224void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6225 Location out,
6226 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006227 DCHECK(kEmitCompilerReadBarrier);
6228
Roland Levillainc9285912015-12-18 10:38:42 +00006229 // Insert a slow path based read barrier *after* the GC root load.
6230 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006231 // Note that GC roots are not affected by heap poisoning, so we do
6232 // not need to do anything special for this here.
6233 SlowPathCode* slow_path =
6234 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6235 AddSlowPath(slow_path);
6236
Roland Levillain3b359c72015-11-17 19:35:12 +00006237 __ b(slow_path->GetEntryLabel());
6238 __ Bind(slow_path->GetExitLabel());
6239}
6240
Vladimir Markodc151b22015-10-15 18:02:30 +01006241HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6242 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6243 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006244 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6245 // We disable pc-relative load when there is an irreducible loop, as the optimization
6246 // is incompatible with it.
6247 if (GetGraph()->HasIrreducibleLoops() &&
6248 (dispatch_info.method_load_kind ==
6249 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6250 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6251 }
6252
6253 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006254 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6255 if (&outer_dex_file != target_method.dex_file) {
6256 // Calls across dex files are more likely to exceed the available BL range,
6257 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6258 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6259 (desired_dispatch_info.method_load_kind ==
6260 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6261 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6262 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6263 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006264 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006265 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006266 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006267 0u
6268 };
6269 }
6270 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006271 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006272}
6273
Vladimir Markob4536b72015-11-24 13:45:23 +00006274Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6275 Register temp) {
6276 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6277 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6278 if (!invoke->GetLocations()->Intrinsified()) {
6279 return location.AsRegister<Register>();
6280 }
6281 // For intrinsics we allow any location, so it may be on the stack.
6282 if (!location.IsRegister()) {
6283 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6284 return temp;
6285 }
6286 // For register locations, check if the register was saved. If so, get it from the stack.
6287 // Note: There is a chance that the register was saved but not overwritten, so we could
6288 // save one load. However, since this is just an intrinsic slow path we prefer this
6289 // simple and more robust approach rather that trying to determine if that's the case.
6290 SlowPathCode* slow_path = GetCurrentSlowPath();
6291 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6292 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6293 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6294 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6295 return temp;
6296 }
6297 return location.AsRegister<Register>();
6298}
6299
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006300void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006301 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006302 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006303 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6304 // LR = code address from literal pool with link-time patch.
6305 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006306 break;
6307 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6308 // LR = invoke->GetDirectCodePtr();
6309 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006310 break;
6311 default:
6312 break;
6313 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006314
Vladimir Marko58155012015-08-19 12:49:41 +00006315 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6316 switch (invoke->GetMethodLoadKind()) {
6317 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6318 // temp = thread->string_init_entrypoint
6319 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6320 break;
6321 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006322 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006323 break;
6324 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6325 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6326 break;
6327 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6328 __ LoadLiteral(temp.AsRegister<Register>(),
6329 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6330 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006331 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6332 HArmDexCacheArraysBase* base =
6333 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6334 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6335 temp.AsRegister<Register>());
6336 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6337 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6338 break;
6339 }
Vladimir Marko58155012015-08-19 12:49:41 +00006340 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006341 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006342 Register method_reg;
6343 Register reg = temp.AsRegister<Register>();
6344 if (current_method.IsRegister()) {
6345 method_reg = current_method.AsRegister<Register>();
6346 } else {
6347 DCHECK(invoke->GetLocations()->Intrinsified());
6348 DCHECK(!current_method.IsValid());
6349 method_reg = reg;
6350 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6351 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006352 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6353 __ LoadFromOffset(kLoadWord,
6354 reg,
6355 method_reg,
6356 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006357 // temp = temp[index_in_cache]
6358 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
6359 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6360 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006361 }
Vladimir Marko58155012015-08-19 12:49:41 +00006362 }
6363
6364 switch (invoke->GetCodePtrLocation()) {
6365 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6366 __ bl(GetFrameEntryLabel());
6367 break;
6368 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006369 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006370 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006371 // Arbitrarily branch to the BL itself, override at link time.
6372 __ bl(&relative_call_patches_.back().label);
6373 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006374 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6375 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6376 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006377 // LR()
6378 __ blx(LR);
6379 break;
6380 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6381 // LR = callee_method->entry_point_from_quick_compiled_code_
6382 __ LoadFromOffset(
6383 kLoadWord, LR, callee_method.AsRegister<Register>(),
6384 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value());
6385 // LR()
6386 __ blx(LR);
6387 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006388 }
6389
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006390 DCHECK(!IsLeafMethod());
6391}
6392
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006393void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6394 Register temp = temp_location.AsRegister<Register>();
6395 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6396 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006397
6398 // Use the calling convention instead of the location of the receiver, as
6399 // intrinsics may have put the receiver in a different register. In the intrinsics
6400 // slow path, the arguments have been moved to the right place, so here we are
6401 // guaranteed that the receiver is the first register of the calling convention.
6402 InvokeDexCallingConvention calling_convention;
6403 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006404 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006405 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006406 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006407 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006408 // Instead of simply (possibly) unpoisoning `temp` here, we should
6409 // emit a read barrier for the previous class reference load.
6410 // However this is not required in practice, as this is an
6411 // intermediate/temporary reference and because the current
6412 // concurrent copying collector keeps the from-space memory
6413 // intact/accessible until the end of the marking phase (the
6414 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006415 __ MaybeUnpoisonHeapReference(temp);
6416 // temp = temp->GetMethodAt(method_offset);
6417 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
6418 kArmWordSize).Int32Value();
6419 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6420 // LR = temp->GetEntryPoint();
6421 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6422 // LR();
6423 __ blx(LR);
6424}
6425
Vladimir Marko58155012015-08-19 12:49:41 +00006426void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6427 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006428 size_t size =
6429 method_patches_.size() +
6430 call_patches_.size() +
6431 relative_call_patches_.size() +
6432 /* MOVW+MOVT for each base */ 2u * dex_cache_arrays_base_labels_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006433 linker_patches->reserve(size);
6434 for (const auto& entry : method_patches_) {
6435 const MethodReference& target_method = entry.first;
6436 Literal* literal = entry.second;
6437 DCHECK(literal->GetLabel()->IsBound());
6438 uint32_t literal_offset = literal->GetLabel()->Position();
6439 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6440 target_method.dex_file,
6441 target_method.dex_method_index));
6442 }
6443 for (const auto& entry : call_patches_) {
6444 const MethodReference& target_method = entry.first;
6445 Literal* literal = entry.second;
6446 DCHECK(literal->GetLabel()->IsBound());
6447 uint32_t literal_offset = literal->GetLabel()->Position();
6448 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6449 target_method.dex_file,
6450 target_method.dex_method_index));
6451 }
6452 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6453 uint32_t literal_offset = info.label.Position();
6454 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6455 info.target_method.dex_file,
6456 info.target_method.dex_method_index));
6457 }
Vladimir Markob4536b72015-11-24 13:45:23 +00006458 for (const auto& pair : dex_cache_arrays_base_labels_) {
6459 HArmDexCacheArraysBase* base = pair.first;
6460 const DexCacheArraysBaseLabels* labels = &pair.second;
6461 const DexFile& dex_file = base->GetDexFile();
6462 size_t base_element_offset = base->GetElementOffset();
6463 DCHECK(labels->add_pc_label.IsBound());
6464 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(labels->add_pc_label.Position());
6465 // Add MOVW patch.
6466 DCHECK(labels->movw_label.IsBound());
6467 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(labels->movw_label.Position());
6468 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6469 &dex_file,
6470 add_pc_offset,
6471 base_element_offset));
6472 // Add MOVT patch.
6473 DCHECK(labels->movt_label.IsBound());
6474 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(labels->movt_label.Position());
6475 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6476 &dex_file,
6477 add_pc_offset,
6478 base_element_offset));
6479 }
Vladimir Marko58155012015-08-19 12:49:41 +00006480}
6481
6482Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
6483 MethodToLiteralMap* map) {
6484 // Look up the literal for target_method.
6485 auto lb = map->lower_bound(target_method);
6486 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) {
6487 return lb->second;
6488 }
6489 // We don't have a literal for this method yet, insert a new one.
6490 Literal* literal = __ NewLiteral<uint32_t>(0u);
6491 map->PutBefore(lb, target_method, literal);
6492 return literal;
6493}
6494
6495Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
6496 return DeduplicateMethodLiteral(target_method, &method_patches_);
6497}
6498
6499Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
6500 return DeduplicateMethodLiteral(target_method, &call_patches_);
6501}
6502
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006503void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006504 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006505 LOG(FATAL) << "Unreachable";
6506}
6507
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006508void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006509 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006510 LOG(FATAL) << "Unreachable";
6511}
6512
Mark Mendellfe57faa2015-09-18 09:26:15 -04006513// Simple implementation of packed switch - generate cascaded compare/jumps.
6514void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6515 LocationSummary* locations =
6516 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6517 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006518 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006519 codegen_->GetAssembler()->IsThumb()) {
6520 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
6521 if (switch_instr->GetStartValue() != 0) {
6522 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
6523 }
6524 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006525}
6526
6527void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6528 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006529 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006530 LocationSummary* locations = switch_instr->GetLocations();
6531 Register value_reg = locations->InAt(0).AsRegister<Register>();
6532 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6533
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006534 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006535 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006536 Register temp_reg = IP;
6537 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
6538 // the immediate, because IP is used as the destination register. For the other
6539 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
6540 // and they can be encoded in the instruction without making use of IP register.
6541 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
6542
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006543 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006544 // Jump to successors[0] if value == lower_bound.
6545 __ b(codegen_->GetLabelOf(successors[0]), EQ);
6546 int32_t last_index = 0;
6547 for (; num_entries - last_index > 2; last_index += 2) {
6548 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
6549 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6550 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
6551 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6552 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
6553 }
6554 if (num_entries - last_index == 2) {
6555 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00006556 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006557 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006558 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04006559
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006560 // And the default for any other value.
6561 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6562 __ b(codegen_->GetLabelOf(default_block));
6563 }
6564 } else {
6565 // Create a table lookup.
6566 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6567
6568 // Materialize a pointer to the switch table
6569 std::vector<Label*> labels(num_entries);
6570 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6571 for (uint32_t i = 0; i < num_entries; i++) {
6572 labels[i] = codegen_->GetLabelOf(successors[i]);
6573 }
6574 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
6575
6576 // Remove the bias.
6577 Register key_reg;
6578 if (lower_bound != 0) {
6579 key_reg = locations->GetTemp(1).AsRegister<Register>();
6580 __ AddConstant(key_reg, value_reg, -lower_bound);
6581 } else {
6582 key_reg = value_reg;
6583 }
6584
6585 // Check whether the value is in the table, jump to default block if not.
6586 __ CmpConstant(key_reg, num_entries - 1);
6587 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
6588
6589 // Load the displacement from the table.
6590 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
6591
6592 // Dispatch is a direct add to the PC (for Thumb2).
6593 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006594 }
6595}
6596
Vladimir Markob4536b72015-11-24 13:45:23 +00006597void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6599 locations->SetOut(Location::RequiresRegister());
6600 codegen_->AddDexCacheArraysBase(base);
6601}
6602
6603void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
6604 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
6605 CodeGeneratorARM::DexCacheArraysBaseLabels* labels = codegen_->GetDexCacheArraysBaseLabels(base);
6606 __ BindTrackedLabel(&labels->movw_label);
6607 __ movw(base_reg, 0u);
6608 __ BindTrackedLabel(&labels->movt_label);
6609 __ movt(base_reg, 0u);
6610 __ BindTrackedLabel(&labels->add_pc_label);
6611 __ add(base_reg, base_reg, ShifterOperand(PC));
6612}
6613
Andreas Gampe85b62f22015-09-09 13:15:38 -07006614void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6615 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00006616 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006617 return;
6618 }
6619
6620 DCHECK_NE(type, Primitive::kPrimVoid);
6621
6622 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
6623 if (return_loc.Equals(trg)) {
6624 return;
6625 }
6626
6627 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6628 // with the last branch.
6629 if (type == Primitive::kPrimLong) {
6630 HParallelMove parallel_move(GetGraph()->GetArena());
6631 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
6632 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
6633 GetMoveResolver()->EmitNativeCode(&parallel_move);
6634 } else if (type == Primitive::kPrimDouble) {
6635 HParallelMove parallel_move(GetGraph()->GetArena());
6636 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
6637 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
6638 GetMoveResolver()->EmitNativeCode(&parallel_move);
6639 } else {
6640 // Let the parallel move resolver take care of all of this.
6641 HParallelMove parallel_move(GetGraph()->GetArena());
6642 parallel_move.AddMove(return_loc, trg, type, nullptr);
6643 GetMoveResolver()->EmitNativeCode(&parallel_move);
6644 }
6645}
6646
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006647void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
6648 LocationSummary* locations =
6649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6650 locations->SetInAt(0, Location::RequiresRegister());
6651 locations->SetOut(Location::RequiresRegister());
6652}
6653
6654void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
6655 LocationSummary* locations = instruction->GetLocations();
6656 uint32_t method_offset = 0;
6657 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
6658 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6659 instruction->GetIndex(), kArmPointerSize).SizeValue();
6660 } else {
6661 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
6662 instruction->GetIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value();
6663 }
6664 __ LoadFromOffset(kLoadWord,
6665 locations->Out().AsRegister<Register>(),
6666 locations->InAt(0).AsRegister<Register>(),
6667 method_offset);
6668}
6669
Roland Levillain4d027112015-07-01 15:41:14 +01006670#undef __
6671#undef QUICK_ENTRY_POINT
6672
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006673} // namespace arm
6674} // namespace art